Tor 0.4.9.2-alpha-dev
All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
crypto_nss_mgt.c
Go to the documentation of this file.
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-2021, The Tor Project, Inc. */
5/* See LICENSE for licensing information */
6
7/**
8 * \file crypto_nss_mgt.c
9 *
10 * \brief Manage the NSS library (if used)
11 **/
12
14
15#include "lib/log/log.h"
16#include "lib/log/util_bug.h"
17#include "lib/string/printf.h"
18
19DISABLE_GCC_WARNING("-Wredundant-decls")
20DISABLE_GCC_WARNING("-Wstrict-prototypes")
21#include <nss.h>
22#include <pk11func.h>
23#include <ssl.h>
24
25#include <prerror.h>
26#include <prtypes.h>
27#include <prinit.h>
28ENABLE_GCC_WARNING("-Wstrict-prototypes")
29ENABLE_GCC_WARNING("-Wredundant-decls")
30
31const char *
32crypto_nss_get_version_str(void)
33{
34 return NSS_GetVersion();
35}
36const char *
37crypto_nss_get_header_version_str(void)
38{
39 return NSS_VERSION;
40}
41
42/** A password function that always returns NULL. */
43static char *
45 PRBool retry,
46 void *arg)
47{
48 (void) slot;
49 (void) retry;
50 (void) arg;
51 return NULL;
52}
53
54void
55crypto_nss_early_init(int nss_only)
56{
57 if (! nss_only) {
58 PR_Init(PR_USER_THREAD, PR_PRIORITY_NORMAL, 0);
59 PK11_SetPasswordFunc(nss_password_func_always_fail);
60 }
61
62 /* Eventually we should use NSS_Init() instead -- but that wants a
63 directory. The documentation says that we can't use this if we want
64 to use OpenSSL. */
65 if (NSS_NoDB_Init(NULL) == SECFailure) {
66 log_err(LD_CRYPTO, "Unable to initialize NSS.");
67 crypto_nss_log_errors(LOG_ERR, "initializing NSS");
68 tor_assert_unreached();
69 }
70
71 if (NSS_SetDomesticPolicy() == SECFailure) {
72 log_err(LD_CRYPTO, "Unable to set NSS cipher policy.");
73 crypto_nss_log_errors(LOG_ERR, "setting cipher policy");
74 tor_assert_unreached();
75 }
76
77 /* We need to override the default here, or NSS will reject all the
78 * legacy Tor certificates. */
79 SECStatus rv = NSS_OptionSet(NSS_RSA_MIN_KEY_SIZE, 1024);
80 if (rv != SECSuccess) {
81 log_err(LD_CRYPTO, "Unable to set NSS min RSA key size");
82 crypto_nss_log_errors(LOG_ERR, "setting cipher option.");
83 tor_assert_unreached();
84 }
85}
86
87void
88crypto_nss_log_errors(int severity, const char *doing)
89{
90 PRErrorCode code = PR_GetError();
91 const char *string = PORT_ErrorToString(code);
92 const char *name = PORT_ErrorToName(code);
93 char buf[16];
94 if (!string)
95 string = "<unrecognized>";
96 if (!name) {
97 tor_snprintf(buf, sizeof(buf), "%d", code);
98 name = buf;
99 }
100 if (doing) {
101 tor_log(severity, LD_CRYPTO, "NSS error %s while %s: %s",
102 name, doing, string);
103 } else {
104 tor_log(severity, LD_CRYPTO, "NSS error %s: %s", name, string);
105 }
106}
107
108int
109crypto_nss_late_init(void)
110{
111 /* Possibly, SSL_OptionSetDefault? */
112
113 return 0;
114}
115
116void
117crypto_nss_global_cleanup(void)
118{
119 NSS_Shutdown();
120 PL_ArenaFinish();
121 PR_Cleanup();
122}
123
124void
125crypto_nss_prefork(void)
126{
127 NSS_Shutdown();
128}
129
130void
131crypto_nss_postfork(void)
132{
133 crypto_nss_early_init(1);
134}
const char * name
Definition: config.c:2471
static char * nss_password_func_always_fail(PK11SlotInfo *slot, PRBool retry, void *arg)
Headers for crypto_nss_mgt.c.
void tor_log(int severity, log_domain_mask_t domain, const char *format,...)
Definition: log.c:591
Headers for log.c.
#define LD_CRYPTO
Definition: log.h:64
#define LOG_ERR
Definition: log.h:56
int tor_snprintf(char *str, size_t size, const char *format,...)
Definition: printf.c:27
Header for printf.c.
Macros to manage assertions, fatal and non-fatal.