Skip to content

Latest commit

 

History

History
305 lines (274 loc) · 9.19 KB

nss-policy-check.c

File metadata and controls

305 lines (274 loc) · 9.19 KB
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
/* This program can be used to check the validity of a NSS crypto policy
* configuration file, specified using a config= line.
*
* Exit codes:
* failure: 2
* warning: 1
* success: 0
*/
#include <limits.h>
#include <errno.h>
#include <stdio.h>
#include "utilparst.h"
#include "nss.h"
#include "secport.h"
#include "secutil.h"
#include "secmod.h"
#include "ssl.h"
#include "prenv.h"
Jun 15, 2022
Jun 15, 2022
24
#include "plgetopt.h"
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
const char *sWarn = "WARN";
const char *sInfo = "INFO";
void
get_tls_info(SSLProtocolVariant protocolVariant, const char *display)
{
SSLVersionRange vrange_supported, vrange_enabled;
unsigned num_enabled = 0;
PRBool failed = PR_FALSE;
/* We assume SSL v2 is inactive, and therefore SSL_VersionRangeGetDefault
* gives complete information. */
if ((SSL_VersionRangeGetSupported(protocolVariant, &vrange_supported) != SECSuccess) ||
(SSL_VersionRangeGetDefault(protocolVariant, &vrange_enabled) != SECSuccess) ||
!vrange_enabled.min ||
!vrange_enabled.max ||
vrange_enabled.max < vrange_supported.min ||
vrange_enabled.min > vrange_supported.max) {
failed = PR_TRUE;
} else {
if (vrange_enabled.min < vrange_supported.min) {
vrange_enabled.min = vrange_supported.min;
}
if (vrange_enabled.max > vrange_supported.max) {
vrange_enabled.max = vrange_supported.max;
}
if (vrange_enabled.min > vrange_enabled.max) {
failed = PR_TRUE;
}
}
if (failed) {
num_enabled = 0;
} else {
num_enabled = vrange_enabled.max - vrange_enabled.min + 1;
}
fprintf(stderr, "NSS-POLICY-%s: NUMBER-OF-%s-VERSIONS: %u\n",
num_enabled ? sInfo : sWarn, display, num_enabled);
if (!num_enabled) {
PR_SetEnv("NSS_POLICY_WARN=1");
}
}
#ifndef PATH_MAX
#define PATH_MAX 1024
#endif
Jun 15, 2022
Jun 15, 2022
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
/* private flags for policy check; should be in sync with pk11wrap/pk11pars.c */
#define SECMOD_FLAG_POLICY_CHECK_IDENTIFIER 0x01
#define SECMOD_FLAG_POLICY_CHECK_VALUE 0x02
static void
printUsage(const char *progName)
{
fprintf(stderr,
"Usage: %s [options] <path-to-policy-file>\n"
"\tWhere options are:\n"
"\t-f flag\t Set policy check flag (can be specified multiple times)\n"
"\t \t Possible flags: \"identifier\" or \"value\"\n",
progName);
}
87
88
89
90
91
92
93
94
95
96
97
98
int
main(int argc, char **argv)
{
const PRUint16 *cipherSuites = SSL_ImplementedCiphers;
int i;
SECStatus rv;
SECMODModule *module = NULL;
char path[PATH_MAX];
const char *filename;
char moduleSpec[1024 + PATH_MAX];
unsigned num_enabled = 0;
int result = 0;
Jun 15, 2022
Jun 15, 2022
99
100
101
102
char *flags = NULL;
PLOptState *optstate = NULL;
PLOptStatus status;
char *fullPath = NULL;
Jun 15, 2022
Jun 15, 2022
104
105
char *progName = NULL;
PRUint32 policyCheckFlags = 0;
Jun 15, 2022
Jun 15, 2022
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
progName = PORT_Strdup(argv[0]);
if (!progName) {
result = 2;
goto loser_no_shutdown;
}
/* Use PL_CreateOptState as SECU_ParseCommandLine ignores
* positional parameters */
optstate = PL_CreateOptState(argc, argv, "f:");
if (!optstate) {
result = 2;
goto loser_no_shutdown;
}
while ((status = PL_GetNextOpt(optstate)) == PL_OPT_OK) {
switch (optstate->option) {
case 0: /* positional parameter */
fullPath = PL_strdup(optstate->value);
if (!fullPath) {
result = 2;
goto loser_no_shutdown;
}
goto breakout;
case 'f':
if (!PORT_Strcmp(optstate->value, "identifier")) {
policyCheckFlags |= SECMOD_FLAG_POLICY_CHECK_IDENTIFIER;
} else if (!PORT_Strcmp(optstate->value, "value")) {
policyCheckFlags |= SECMOD_FLAG_POLICY_CHECK_VALUE;
} else {
fprintf(stderr, "not a valid flag: %s\n", optstate->value);
printUsage(progName);
result = 2;
goto loser_no_shutdown;
}
break;
default:
printUsage(progName);
result = 2;
goto loser_no_shutdown;
}
}
breakout:
if (status != PL_OPT_OK || !fullPath) {
printUsage(progName);
Jul 23, 2018
Jul 23, 2018
151
goto loser_no_shutdown;
Jun 15, 2022
Jun 15, 2022
154
fullPathLen = strlen(fullPath);
Jun 15, 2022
Jun 15, 2022
156
157
if (!fullPathLen || PR_Access(fullPath, PR_ACCESS_READ_OK) != PR_SUCCESS) {
fprintf(stderr, "Error: cannot read file %s\n", fullPath);
Jul 23, 2018
Jul 23, 2018
159
goto loser_no_shutdown;
160
161
162
163
164
}
if (fullPathLen >= PATH_MAX) {
fprintf(stderr, "Error: filename parameter is too long\n");
result = 2;
Jul 23, 2018
Jul 23, 2018
165
goto loser_no_shutdown;
166
167
168
}
path[0] = 0;
Jun 15, 2022
Jun 15, 2022
169
170
filename = fullPath + fullPathLen - 1;
while ((filename > fullPath) && (*filename != NSSUTIL_PATH_SEPARATOR[0])) {
171
172
173
filename--;
}
Jun 15, 2022
Jun 15, 2022
174
if (filename == fullPath) {
175
176
177
PORT_Strcpy(path, ".");
} else {
filename++; /* Go past the path separator. */
Jun 15, 2022
Jun 15, 2022
178
PORT_Strncat(path, fullPath, (filename - fullPath));
179
180
181
182
183
184
185
}
PR_SetEnv("NSS_IGNORE_SYSTEM_POLICY=1");
rv = NSS_NoDB_Init(NULL);
if (rv != SECSuccess) {
fprintf(stderr, "NSS_Init failed: %s\n", PORT_ErrorToString(PR_GetError()));
result = 2;
Jul 23, 2018
Jul 23, 2018
186
goto loser_no_shutdown;
187
188
189
190
191
192
}
PR_SetEnv("NSS_POLICY_LOADED=0");
PR_SetEnv("NSS_POLICY_FAIL=0");
PR_SetEnv("NSS_POLICY_WARN=0");
Jun 15, 2022
Jun 15, 2022
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
flags = PORT_Strdup("internal,moduleDB,skipFirst,moduleDBOnly,critical,printPolicyFeedback");
if (!flags) {
result = 2;
goto loser_no_shutdown;
}
if (policyCheckFlags & SECMOD_FLAG_POLICY_CHECK_IDENTIFIER) {
char *newflags;
newflags = PORT_Realloc(flags, strlen(flags) + strlen(",policyCheckIdentifier") + 1);
if (!newflags) {
result = 2;
goto loser_no_shutdown;
}
flags = newflags;
PORT_Strcat(flags, ",policyCheckIdentifier");
}
if (policyCheckFlags & SECMOD_FLAG_POLICY_CHECK_VALUE) {
char *newflags;
newflags = PORT_Realloc(flags, strlen(flags) + strlen(",policyCheckValue") + 1);
if (!newflags) {
result = 2;
goto loser_no_shutdown;
}
flags = newflags;
PORT_Strcat(flags, ",policyCheckValue");
}
Mar 16, 2023
Mar 16, 2023
223
snprintf(moduleSpec, sizeof(moduleSpec),
Apr 12, 2023
Apr 12, 2023
224
225
226
227
228
229
"name=\"Policy File\" "
"parameters=\"configdir='sql:%s' "
"secmod='%s' "
"flags=readOnly,noCertDB,forceSecmodChoice,forceOpen\" "
"NSS=\"flags=%s\"",
path, filename, flags);
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
module = SECMOD_LoadModule(moduleSpec, NULL, PR_TRUE);
if (!module || !module->loaded || atoi(PR_GetEnvSecure("NSS_POLICY_LOADED")) != 1) {
fprintf(stderr, "Error: failed to load policy file\n");
result = 2;
goto loser;
}
rv = SSL_OptionSetDefault(SSL_SECURITY, PR_TRUE);
if (rv != SECSuccess) {
fprintf(stderr, "enable SSL_SECURITY failed: %s\n", PORT_ErrorToString(PR_GetError()));
result = 2;
goto loser;
}
for (i = 0; i < SSL_NumImplementedCiphers; i++) {
PRUint16 suite = cipherSuites[i];
PRBool enabled;
SSLCipherSuiteInfo info;
rv = SSL_CipherPrefGetDefault(suite, &enabled);
if (rv != SECSuccess) {
fprintf(stderr,
"SSL_CipherPrefGetDefault didn't like value 0x%04x (i = %d): %s\n",
suite, i, PORT_ErrorToString(PR_GetError()));
continue;
}
rv = SSL_GetCipherSuiteInfo(suite, &info, (int)(sizeof info));
if (rv != SECSuccess) {
fprintf(stderr,
"SSL_GetCipherSuiteInfo didn't like value 0x%04x (i = %d): %s\n",
suite, i, PORT_ErrorToString(PR_GetError()));
continue;
}
if (enabled) {
++num_enabled;
fprintf(stderr, "NSS-POLICY-INFO: ciphersuite %s is enabled\n", info.cipherSuiteName);
}
}
fprintf(stderr, "NSS-POLICY-%s: NUMBER-OF-CIPHERSUITES: %u\n", num_enabled ? sInfo : sWarn, num_enabled);
if (!num_enabled) {
PR_SetEnv("NSS_POLICY_WARN=1");
}
get_tls_info(ssl_variant_stream, "TLS");
get_tls_info(ssl_variant_datagram, "DTLS");
if (atoi(PR_GetEnvSecure("NSS_POLICY_FAIL")) != 0) {
result = 2;
} else if (atoi(PR_GetEnvSecure("NSS_POLICY_WARN")) != 0) {
result = 1;
}
loser:
if (module) {
SECMOD_DestroyModule(module);
}
rv = NSS_Shutdown();
if (rv != SECSuccess) {
fprintf(stderr, "NSS_Shutdown failed: %s\n", PORT_ErrorToString(PR_GetError()));
result = 2;
}
Jul 23, 2018
Jul 23, 2018
292
loser_no_shutdown:
Jun 15, 2022
Jun 15, 2022
293
294
295
296
297
298
if (optstate) {
PL_DestroyOptState(optstate);
}
PORT_Free(flags);
PORT_Free(progName);
PORT_Free(fullPath);
299
300
301
302
303
304
305
if (result == 2) {
fprintf(stderr, "NSS-POLICY-FAIL\n");
} else if (result == 1) {
fprintf(stderr, "NSS-POLICY-WARN\n");
}
return result;
}