nsCrypto.cpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
  2. /* This Source Code Form is subject to the terms of the Mozilla Public
  3. * License, v. 2.0. If a copy of the MPL was not distributed with this
  4. * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  5. #include "nsCrypto.h"
  6. #include "nsNSSComponent.h"
  7. #include "nsNativeCharsetUtils.h"
  8. #include "nsServiceManagerUtils.h"
  9. #include "ScopedNSSTypes.h"
  10. // QueryInterface implementation for nsPkcs11
  11. NS_INTERFACE_MAP_BEGIN(nsPkcs11)
  12. NS_INTERFACE_MAP_ENTRY(nsIPKCS11)
  13. NS_INTERFACE_MAP_ENTRY(nsISupports)
  14. NS_INTERFACE_MAP_END
  15. NS_IMPL_ADDREF(nsPkcs11)
  16. NS_IMPL_RELEASE(nsPkcs11)
  17. nsPkcs11::nsPkcs11()
  18. {
  19. }
  20. nsPkcs11::~nsPkcs11()
  21. {
  22. nsNSSShutDownPreventionLock locker;
  23. if (isAlreadyShutDown()) {
  24. return;
  25. }
  26. shutdown(ShutdownCalledFrom::Object);
  27. }
  28. // Delete a PKCS11 module from the user's profile.
  29. NS_IMETHODIMP
  30. nsPkcs11::DeleteModule(const nsAString& aModuleName)
  31. {
  32. nsNSSShutDownPreventionLock locker;
  33. if (isAlreadyShutDown()) {
  34. return NS_ERROR_NOT_AVAILABLE;
  35. }
  36. if (aModuleName.IsEmpty()) {
  37. return NS_ERROR_INVALID_ARG;
  38. }
  39. NS_ConvertUTF16toUTF8 moduleName(aModuleName);
  40. // Introduce additional scope for module so all references to it are released
  41. // before we call SECMOD_DeleteModule, below.
  42. #ifndef MOZ_NO_SMART_CARDS
  43. {
  44. mozilla::UniqueSECMODModule module(SECMOD_FindModule(moduleName.get()));
  45. if (!module) {
  46. return NS_ERROR_FAILURE;
  47. }
  48. nsCOMPtr<nsINSSComponent> nssComponent(
  49. do_GetService(PSM_COMPONENT_CONTRACTID));
  50. nssComponent->ShutdownSmartCardThread(module.get());
  51. }
  52. #endif
  53. // modType is an output variable. We ignore it.
  54. int32_t modType;
  55. SECStatus srv = SECMOD_DeleteModule(moduleName.get(), &modType);
  56. if (srv != SECSuccess) {
  57. return NS_ERROR_FAILURE;
  58. }
  59. return NS_OK;
  60. }
  61. // Add a new PKCS11 module to the user's profile.
  62. NS_IMETHODIMP
  63. nsPkcs11::AddModule(const nsAString& aModuleName,
  64. const nsAString& aLibraryFullPath,
  65. int32_t aCryptoMechanismFlags,
  66. int32_t aCipherFlags)
  67. {
  68. nsNSSShutDownPreventionLock locker;
  69. if (isAlreadyShutDown()) {
  70. return NS_ERROR_NOT_AVAILABLE;
  71. }
  72. if (aModuleName.IsEmpty()) {
  73. return NS_ERROR_INVALID_ARG;
  74. }
  75. NS_ConvertUTF16toUTF8 moduleName(aModuleName);
  76. nsCString fullPath;
  77. // NSS doesn't support Unicode path. Use native charset
  78. NS_CopyUnicodeToNative(aLibraryFullPath, fullPath);
  79. uint32_t mechFlags = SECMOD_PubMechFlagstoInternal(aCryptoMechanismFlags);
  80. uint32_t cipherFlags = SECMOD_PubCipherFlagstoInternal(aCipherFlags);
  81. SECStatus srv = SECMOD_AddNewModule(moduleName.get(), fullPath.get(),
  82. mechFlags, cipherFlags);
  83. if (srv != SECSuccess) {
  84. return NS_ERROR_FAILURE;
  85. }
  86. #ifndef MOZ_NO_SMART_CARDS
  87. mozilla::UniqueSECMODModule module(SECMOD_FindModule(moduleName.get()));
  88. if (!module) {
  89. return NS_ERROR_FAILURE;
  90. }
  91. nsCOMPtr<nsINSSComponent> nssComponent(
  92. do_GetService(PSM_COMPONENT_CONTRACTID));
  93. nssComponent->LaunchSmartCardThread(module.get());
  94. #endif
  95. return NS_OK;
  96. }