crypto_nss_mgt.c 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. /* Copyright (c) 2001, Matej Pfajfar.
  2. * Copyright (c) 2001-2004, Roger Dingledine.
  3. * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
  4. * Copyright (c) 2007-2018, The Tor Project, Inc. */
  5. /* See LICENSE for licensing information */
  6. /**
  7. * \file crypto_nss_mgt.c
  8. *
  9. * \brief Manage the NSS library (if used)
  10. **/
  11. #include "lib/crypt_ops/crypto_nss_mgt.h"
  12. #include "lib/log/log.h"
  13. #include "lib/log/util_bug.h"
  14. #include <nss.h>
  15. #include <pk11func.h>
  16. #include <ssl.h>
  17. #include <prerror.h>
  18. #include <prtypes.h>
  19. #include <prinit.h>
  20. const char *
  21. crypto_nss_get_version_str(void)
  22. {
  23. return NSS_GetVersion();
  24. }
  25. const char *
  26. crypto_nss_get_header_version_str(void)
  27. {
  28. return NSS_VERSION;
  29. }
  30. /** A password function that always returns NULL. */
  31. static char *
  32. nss_password_func_always_fail(PK11SlotInfo *slot,
  33. PRBool retry,
  34. void *arg)
  35. {
  36. (void) slot;
  37. (void) retry;
  38. (void) arg;
  39. return NULL;
  40. }
  41. void
  42. crypto_nss_early_init(void)
  43. {
  44. PR_Init(PR_USER_THREAD, PR_PRIORITY_NORMAL, 0);
  45. PK11_SetPasswordFunc(nss_password_func_always_fail);
  46. /* Eventually we should use NSS_Init() instead -- but that wants a
  47. directory. The documentation says that we can't use this if we want
  48. to use OpenSSL. */
  49. if (NSS_NoDB_Init(NULL) == SECFailure) {
  50. log_err(LD_CRYPTO, "Unable to initialize NSS.");
  51. crypto_nss_log_errors(LOG_ERR, "initializing NSS");
  52. tor_assert_unreached();
  53. }
  54. if (NSS_SetDomesticPolicy() == SECFailure) {
  55. log_err(LD_CRYPTO, "Unable to set NSS cipher policy.");
  56. crypto_nss_log_errors(LOG_ERR, "setting cipher policy");
  57. tor_assert_unreached();
  58. }
  59. }
  60. void
  61. crypto_nss_log_errors(int severity, const char *doing)
  62. {
  63. PRErrorCode code = PR_GetError();
  64. /* XXXX how do I convert errors to strings? */
  65. if (doing) {
  66. tor_log(severity, LD_CRYPTO, "NSS error %u while %s", code, doing);
  67. } else {
  68. tor_log(severity, LD_CRYPTO, "NSS error %u", code);
  69. }
  70. }
  71. int
  72. crypto_nss_late_init(void)
  73. {
  74. /* Possibly, SSL_OptionSetDefault? */
  75. return 0;
  76. }
  77. void
  78. crypto_nss_global_cleanup(void)
  79. {
  80. NSS_Shutdown();
  81. }
  82. void
  83. crypto_nss_postfork(void)
  84. {
  85. crypto_nss_global_cleanup();
  86. crypto_nss_early_init();
  87. }