nsNSSErrors.cpp 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
  2. *
  3. * This Source Code Form is subject to the terms of the Mozilla Public
  4. * License, v. 2.0. If a copy of the MPL was not distributed with this
  5. * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  6. #include "nsNSSComponent.h"
  7. #include "secerr.h"
  8. #include "sslerr.h"
  9. const char *
  10. nsNSSErrors::getDefaultErrorStringName(PRErrorCode err)
  11. {
  12. return PR_ErrorToName(err);
  13. }
  14. const char *
  15. nsNSSErrors::getOverrideErrorStringName(PRErrorCode aErrorCode)
  16. {
  17. const char *id_str = nullptr;
  18. switch (aErrorCode) {
  19. case SSL_ERROR_SSL_DISABLED:
  20. id_str = "PSMERR_SSL_Disabled";
  21. break;
  22. case SSL_ERROR_SSL2_DISABLED:
  23. id_str = "PSMERR_SSL2_Disabled";
  24. break;
  25. case SEC_ERROR_REUSED_ISSUER_AND_SERIAL:
  26. id_str = "PSMERR_HostReusedIssuerSerial";
  27. break;
  28. }
  29. return id_str;
  30. }
  31. nsresult
  32. nsNSSErrors::getErrorMessageFromCode(PRErrorCode err,
  33. nsINSSComponent *component,
  34. nsString &returnedMessage)
  35. {
  36. NS_ENSURE_ARG_POINTER(component);
  37. returnedMessage.Truncate();
  38. const char *nss_error_id_str = getDefaultErrorStringName(err);
  39. const char *id_str = getOverrideErrorStringName(err);
  40. if (id_str || nss_error_id_str)
  41. {
  42. nsString defMsg;
  43. nsresult rv;
  44. if (id_str)
  45. {
  46. rv = component->GetPIPNSSBundleString(id_str, defMsg);
  47. }
  48. else
  49. {
  50. rv = component->GetNSSBundleString(nss_error_id_str, defMsg);
  51. }
  52. if (NS_SUCCEEDED(rv))
  53. {
  54. returnedMessage.Append(defMsg);
  55. returnedMessage.Append('\n');
  56. }
  57. }
  58. if (returnedMessage.IsEmpty())
  59. {
  60. // no localized string available, use NSS' internal
  61. returnedMessage.AppendASCII(PR_ErrorToString(err, PR_LANGUAGE_EN));
  62. returnedMessage.Append('\n');
  63. }
  64. if (nss_error_id_str)
  65. {
  66. nsresult rv;
  67. nsCString error_id(nss_error_id_str);
  68. NS_ConvertASCIItoUTF16 idU(error_id);
  69. const char16_t *params[1];
  70. params[0] = idU.get();
  71. nsString formattedString;
  72. rv = component->PIPBundleFormatStringFromName("certErrorCodePrefix",
  73. params, 1,
  74. formattedString);
  75. if (NS_SUCCEEDED(rv)) {
  76. returnedMessage.Append('\n');
  77. returnedMessage.Append(formattedString);
  78. returnedMessage.Append('\n');
  79. }
  80. else {
  81. returnedMessage.Append('(');
  82. returnedMessage.Append(idU);
  83. returnedMessage.Append(')');
  84. }
  85. }
  86. return NS_OK;
  87. }