crypto_nss_mgt.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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 "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(void)
  46. {
  47. PR_Init(PR_USER_THREAD, PR_PRIORITY_NORMAL, 0);
  48. PK11_SetPasswordFunc(nss_password_func_always_fail);
  49. /* Eventually we should use NSS_Init() instead -- but that wants a
  50. directory. The documentation says that we can't use this if we want
  51. to use OpenSSL. */
  52. if (NSS_NoDB_Init(NULL) == SECFailure) {
  53. log_err(LD_CRYPTO, "Unable to initialize NSS.");
  54. crypto_nss_log_errors(LOG_ERR, "initializing NSS");
  55. tor_assert_unreached();
  56. }
  57. if (NSS_SetDomesticPolicy() == SECFailure) {
  58. log_err(LD_CRYPTO, "Unable to set NSS cipher policy.");
  59. crypto_nss_log_errors(LOG_ERR, "setting cipher policy");
  60. tor_assert_unreached();
  61. }
  62. /* We need to override the default here, or NSS will reject all the
  63. * legacy Tor certificates. */
  64. SECStatus rv = NSS_OptionSet(NSS_RSA_MIN_KEY_SIZE, 1024);
  65. if (rv != SECSuccess) {
  66. log_err(LD_CRYPTO, "Unable to set NSS min RSA key size");
  67. crypto_nss_log_errors(LOG_ERR, "setting cipher option.");
  68. tor_assert_unreached();
  69. }
  70. }
  71. void
  72. crypto_nss_log_errors(int severity, const char *doing)
  73. {
  74. PRErrorCode code = PR_GetError();
  75. const char *string = PORT_ErrorToString(code);
  76. const char *name = PORT_ErrorToName(code);
  77. char buf[16];
  78. if (!string)
  79. string = "<unrecognized>";
  80. if (!name) {
  81. tor_snprintf(buf, sizeof(buf), "%d", code);
  82. name = buf;
  83. }
  84. if (doing) {
  85. tor_log(severity, LD_CRYPTO, "NSS error %s while %s: %s",
  86. name, doing, string);
  87. } else {
  88. tor_log(severity, LD_CRYPTO, "NSS error %s: %s", name, string);
  89. }
  90. }
  91. int
  92. crypto_nss_late_init(void)
  93. {
  94. /* Possibly, SSL_OptionSetDefault? */
  95. return 0;
  96. }
  97. void
  98. crypto_nss_global_cleanup(void)
  99. {
  100. NSS_Shutdown();
  101. }
  102. void
  103. crypto_nss_postfork(void)
  104. {
  105. crypto_nss_global_cleanup();
  106. crypto_nss_early_init();
  107. }