crypto_nss_mgt.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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-2019, 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 "lib/string/printf.h"
  15. DISABLE_GCC_WARNING(strict-prototypes)
  16. #include <nss.h>
  17. #include <pk11func.h>
  18. #include <ssl.h>
  19. #include <prerror.h>
  20. #include <prtypes.h>
  21. #include <prinit.h>
  22. ENABLE_GCC_WARNING(strict-prototypes)
  23. const char *
  24. crypto_nss_get_version_str(void)
  25. {
  26. return NSS_GetVersion();
  27. }
  28. const char *
  29. crypto_nss_get_header_version_str(void)
  30. {
  31. return NSS_VERSION;
  32. }
  33. /** A password function that always returns NULL. */
  34. static char *
  35. nss_password_func_always_fail(PK11SlotInfo *slot,
  36. PRBool retry,
  37. void *arg)
  38. {
  39. (void) slot;
  40. (void) retry;
  41. (void) arg;
  42. return NULL;
  43. }
  44. void
  45. crypto_nss_early_init(int nss_only)
  46. {
  47. if (! nss_only) {
  48. PR_Init(PR_USER_THREAD, PR_PRIORITY_NORMAL, 0);
  49. PK11_SetPasswordFunc(nss_password_func_always_fail);
  50. }
  51. /* Eventually we should use NSS_Init() instead -- but that wants a
  52. directory. The documentation says that we can't use this if we want
  53. to use OpenSSL. */
  54. if (NSS_NoDB_Init(NULL) == SECFailure) {
  55. log_err(LD_CRYPTO, "Unable to initialize NSS.");
  56. crypto_nss_log_errors(LOG_ERR, "initializing NSS");
  57. tor_assert_unreached();
  58. }
  59. if (NSS_SetDomesticPolicy() == SECFailure) {
  60. log_err(LD_CRYPTO, "Unable to set NSS cipher policy.");
  61. crypto_nss_log_errors(LOG_ERR, "setting cipher policy");
  62. tor_assert_unreached();
  63. }
  64. /* We need to override the default here, or NSS will reject all the
  65. * legacy Tor certificates. */
  66. SECStatus rv = NSS_OptionSet(NSS_RSA_MIN_KEY_SIZE, 1024);
  67. if (rv != SECSuccess) {
  68. log_err(LD_CRYPTO, "Unable to set NSS min RSA key size");
  69. crypto_nss_log_errors(LOG_ERR, "setting cipher option.");
  70. tor_assert_unreached();
  71. }
  72. }
  73. void
  74. crypto_nss_log_errors(int severity, const char *doing)
  75. {
  76. PRErrorCode code = PR_GetError();
  77. const char *string = PORT_ErrorToString(code);
  78. const char *name = PORT_ErrorToName(code);
  79. char buf[16];
  80. if (!string)
  81. string = "<unrecognized>";
  82. if (!name) {
  83. tor_snprintf(buf, sizeof(buf), "%d", code);
  84. name = buf;
  85. }
  86. if (doing) {
  87. tor_log(severity, LD_CRYPTO, "NSS error %s while %s: %s",
  88. name, doing, string);
  89. } else {
  90. tor_log(severity, LD_CRYPTO, "NSS error %s: %s", name, string);
  91. }
  92. }
  93. int
  94. crypto_nss_late_init(void)
  95. {
  96. /* Possibly, SSL_OptionSetDefault? */
  97. return 0;
  98. }
  99. void
  100. crypto_nss_global_cleanup(void)
  101. {
  102. NSS_Shutdown();
  103. PL_ArenaFinish();
  104. PR_Cleanup();
  105. }
  106. void
  107. crypto_nss_prefork(void)
  108. {
  109. NSS_Shutdown();
  110. }
  111. void
  112. crypto_nss_postfork(void)
  113. {
  114. crypto_nss_early_init(1);
  115. }