Coverage Report

Created: 2023-11-19 06:09

/src/nss/lib/pk11wrap/pk11pars.c
Line
Count
Source (jump to first uncovered line)
1
/* This Source Code Form is subject to the terms of the Mozilla Public
2
 * License, v. 2.0. If a copy of the MPL was not distributed with this
3
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
4
/*
5
 * The following handles the loading, unloading and management of
6
 * various PCKS #11 modules
7
 */
8
9
#include <ctype.h>
10
#include <assert.h>
11
#include "pkcs11.h"
12
#include "seccomon.h"
13
#include "secmod.h"
14
#include "secmodi.h"
15
#include "secmodti.h"
16
#include "pki3hack.h"
17
#include "secerr.h"
18
#include "nss.h"
19
#include "utilpars.h"
20
#include "pk11pub.h"
21
22
/* create a new module */
23
static SECMODModule *
24
secmod_NewModule(void)
25
74.3k
{
26
74.3k
    SECMODModule *newMod;
27
74.3k
    PLArenaPool *arena;
28
29
    /* create an arena in which dllName and commonName can be
30
     * allocated.
31
     */
32
74.3k
    arena = PORT_NewArena(512);
33
74.3k
    if (arena == NULL) {
34
0
        return NULL;
35
0
    }
36
37
74.3k
    newMod = (SECMODModule *)PORT_ArenaAlloc(arena, sizeof(SECMODModule));
38
74.3k
    if (newMod == NULL) {
39
0
        PORT_FreeArena(arena, PR_FALSE);
40
0
        return NULL;
41
0
    }
42
43
    /*
44
     * initialize of the fields of the module
45
     */
46
74.3k
    newMod->arena = arena;
47
74.3k
    newMod->internal = PR_FALSE;
48
74.3k
    newMod->loaded = PR_FALSE;
49
74.3k
    newMod->isFIPS = PR_FALSE;
50
74.3k
    newMod->dllName = NULL;
51
74.3k
    newMod->commonName = NULL;
52
74.3k
    newMod->library = NULL;
53
74.3k
    newMod->functionList = NULL;
54
74.3k
    newMod->slotCount = 0;
55
74.3k
    newMod->slots = NULL;
56
74.3k
    newMod->slotInfo = NULL;
57
74.3k
    newMod->slotInfoCount = 0;
58
74.3k
    newMod->refCount = 1;
59
74.3k
    newMod->ssl[0] = 0;
60
74.3k
    newMod->ssl[1] = 0;
61
74.3k
    newMod->libraryParams = NULL;
62
74.3k
    newMod->moduleDBFunc = NULL;
63
74.3k
    newMod->parent = NULL;
64
74.3k
    newMod->isCritical = PR_FALSE;
65
74.3k
    newMod->isModuleDB = PR_FALSE;
66
74.3k
    newMod->moduleDBOnly = PR_FALSE;
67
74.3k
    newMod->trustOrder = 0;
68
74.3k
    newMod->cipherOrder = 0;
69
74.3k
    newMod->evControlMask = 0;
70
74.3k
    newMod->refLock = PZ_NewLock(nssILockRefLock);
71
74.3k
    if (newMod->refLock == NULL) {
72
0
        PORT_FreeArena(arena, PR_FALSE);
73
0
        return NULL;
74
0
    }
75
74.3k
    return newMod;
76
74.3k
}
77
78
/* private flags for isModuleDB (field in SECMODModule). */
79
/* The meaing of these flags is as follows:
80
 *
81
 * SECMOD_FLAG_MODULE_DB_IS_MODULE_DB - This is a module that accesses the
82
 *   database of other modules to load. Module DBs are loadable modules that
83
 *   tells NSS which PKCS #11 modules to load and when. These module DBs are
84
 *   chainable. That is, one module DB can load another one. NSS system init
85
 *   design takes advantage of this feature. In system NSS, a fixed system
86
 *   module DB loads the system defined libraries, then chains out to the
87
 *   traditional module DBs to load any system or user configured modules
88
 *   (like smart cards). This bit is the same as the already existing meaning
89
 *   of  isModuleDB = PR_TRUE. None of the other module db flags should be set
90
 *   if this flag isn't on.
91
 *
92
 * SECMOD_FLAG_MODULE_DB_SKIP_FIRST - This flag tells NSS to skip the first
93
 *   PKCS #11 module presented by a module DB. This allows the OS to load a
94
 *   softoken from the system module, then ask the existing module DB code to
95
 *   load the other PKCS #11 modules in that module DB (skipping it's request
96
 *   to load softoken). This gives the system init finer control over the
97
 *   configuration of that softoken module.
98
 *
99
 * SECMOD_FLAG_MODULE_DB_DEFAULT_MODDB - This flag allows system init to mark a
100
 *   different module DB as the 'default' module DB (the one in which
101
 *   'Add module' changes will go). Without this flag NSS takes the first
102
 *   module as the default Module DB, but in system NSS, that first module
103
 *   is the system module, which is likely read only (at least to the user).
104
 *   This  allows system NSS to delegate those changes to the user's module DB,
105
 *   preserving the user's ability to load new PKCS #11 modules (which only
106
 *   affect him), from existing applications like Firefox.
107
 */
108
37.1k
#define SECMOD_FLAG_MODULE_DB_IS_MODULE_DB 0x01 /* must be set if any of the \
109
                                                 *other flags are set */
110
37.1k
#define SECMOD_FLAG_MODULE_DB_SKIP_FIRST 0x02
111
37.1k
#define SECMOD_FLAG_MODULE_DB_DEFAULT_MODDB 0x04
112
74.3k
#define SECMOD_FLAG_MODULE_DB_POLICY_ONLY 0x08
113
114
/* private flags for internal (field in SECMODModule). */
115
/* The meaing of these flags is as follows:
116
 *
117
 * SECMOD_FLAG_INTERNAL_IS_INTERNAL - This is a marks the the module is
118
 *   the internal module (that is, softoken). This bit is the same as the
119
 *   already existing meaning of internal = PR_TRUE. None of the other
120
 *   internal flags should be set if this flag isn't on.
121
 *
122
 * SECMOD_FLAG_MODULE_INTERNAL_KEY_SLOT - This flag allows system init to mark
123
 *   a  different slot returned byt PK11_GetInternalKeySlot(). The 'primary'
124
 *   slot defined by this module will be the new internal key slot.
125
 */
126
74.3k
#define SECMOD_FLAG_INTERNAL_IS_INTERNAL 0x01 /* must be set if any of \
127
                                               *the other flags are set */
128
148k
#define SECMOD_FLAG_INTERNAL_KEY_SLOT 0x02
129
130
/* private flags for policy check. */
131
0
#define SECMOD_FLAG_POLICY_CHECK_IDENTIFIER 0x01
132
0
#define SECMOD_FLAG_POLICY_CHECK_VALUE 0x02
133
134
/*
135
 * for 3.4 we continue to use the old SECMODModule structure
136
 */
137
SECMODModule *
138
SECMOD_CreateModule(const char *library, const char *moduleName,
139
                    const char *parameters, const char *nss)
140
0
{
141
0
    return SECMOD_CreateModuleEx(library, moduleName, parameters, nss, NULL);
142
0
}
143
144
/*
145
 * NSS config options format:
146
 *
147
 * The specified ciphers will be allowed by policy, but an application
148
 * may allow more by policy explicitly:
149
 * config="allow=curve1:curve2:hash1:hash2:rsa-1024..."
150
 *
151
 * Only the specified hashes and curves will be allowed:
152
 * config="disallow=all allow=sha1:sha256:secp256r1:secp384r1"
153
 *
154
 * Only the specified hashes and curves will be allowed, and
155
 *  RSA keys of 2048 or more will be accepted, and DH key exchange
156
 *  with 1024-bit primes or more:
157
 * config="disallow=all allow=sha1:sha256:secp256r1:secp384r1:min-rsa=2048:min-dh=1024"
158
 *
159
 * A policy that enables the AES ciphersuites and the SECP256/384 curves:
160
 * config="allow=aes128-cbc:aes128-gcm:TLS1.0:TLS1.2:TLS1.1:HMAC-SHA1:SHA1:SHA256:SHA384:RSA:ECDHE-RSA:SECP256R1:SECP384R1"
161
 *
162
 * Disallow values are parsed first, then allow values, independent of the
163
 * order they appear.
164
 *
165
 * flags: turn on the following flags:
166
 *    policy-lock: turn off the ability for applications to change policy with
167
 *                 the call NSS_SetAlgorithmPolicy or the other system policy
168
 *                 calls (SSL_SetPolicy, etc.)
169
 *    ssl-lock:    turn off the ability to change the ssl defaults.
170
 *
171
 * The following only apply to ssl cipher suites (future smime)
172
 *
173
 * enable: turn on ciphersuites by default.
174
 * disable: turn off ciphersuites by default without disallowing them by policy.
175
 *
176
 *
177
 */
178
179
typedef struct {
180
    const char *name;
181
    unsigned name_size;
182
    SECOidTag oid;
183
    PRUint32 val;
184
} oidValDef;
185
186
typedef struct {
187
    const char *name;
188
    unsigned name_size;
189
    PRInt32 option;
190
} optionFreeDef;
191
192
typedef struct {
193
    const char *name;
194
    unsigned name_size;
195
    PRUint32 flag;
196
} policyFlagDef;
197
198
/*
199
 *  This table should be merged with the SECOID table.
200
 */
201
#define CIPHER_NAME(x) x, (sizeof(x) - 1)
202
static const oidValDef curveOptList[] = {
203
    /* Curves */
204
    { CIPHER_NAME("PRIME192V1"), SEC_OID_ANSIX962_EC_PRIME192V1,
205
      NSS_USE_ALG_IN_SSL_KX | NSS_USE_ALG_IN_CERT_SIGNATURE },
206
    { CIPHER_NAME("PRIME192V2"), SEC_OID_ANSIX962_EC_PRIME192V2,
207
      NSS_USE_ALG_IN_SSL_KX | NSS_USE_ALG_IN_CERT_SIGNATURE },
208
    { CIPHER_NAME("PRIME192V3"), SEC_OID_ANSIX962_EC_PRIME192V3,
209
      NSS_USE_ALG_IN_SSL_KX | NSS_USE_ALG_IN_CERT_SIGNATURE },
210
    { CIPHER_NAME("PRIME239V1"), SEC_OID_ANSIX962_EC_PRIME239V1,
211
      NSS_USE_ALG_IN_SSL_KX | NSS_USE_ALG_IN_CERT_SIGNATURE },
212
    { CIPHER_NAME("PRIME239V2"), SEC_OID_ANSIX962_EC_PRIME239V2,
213
      NSS_USE_ALG_IN_SSL_KX | NSS_USE_ALG_IN_CERT_SIGNATURE },
214
    { CIPHER_NAME("PRIME239V3"), SEC_OID_ANSIX962_EC_PRIME239V3,
215
      NSS_USE_ALG_IN_SSL_KX | NSS_USE_ALG_IN_CERT_SIGNATURE },
216
    { CIPHER_NAME("PRIME256V1"), SEC_OID_ANSIX962_EC_PRIME256V1,
217
      NSS_USE_ALG_IN_SSL_KX | NSS_USE_ALG_IN_CERT_SIGNATURE },
218
    { CIPHER_NAME("SECP112R1"), SEC_OID_SECG_EC_SECP112R1,
219
      NSS_USE_ALG_IN_SSL_KX | NSS_USE_ALG_IN_CERT_SIGNATURE },
220
    { CIPHER_NAME("SECP112R2"), SEC_OID_SECG_EC_SECP112R2,
221
      NSS_USE_ALG_IN_SSL_KX | NSS_USE_ALG_IN_CERT_SIGNATURE },
222
    { CIPHER_NAME("SECP128R1"), SEC_OID_SECG_EC_SECP128R1,
223
      NSS_USE_ALG_IN_SSL_KX | NSS_USE_ALG_IN_CERT_SIGNATURE },
224
    { CIPHER_NAME("SECP128R2"), SEC_OID_SECG_EC_SECP128R2,
225
      NSS_USE_ALG_IN_SSL_KX | NSS_USE_ALG_IN_CERT_SIGNATURE },
226
    { CIPHER_NAME("SECP160K1"), SEC_OID_SECG_EC_SECP160K1,
227
      NSS_USE_ALG_IN_SSL_KX | NSS_USE_ALG_IN_CERT_SIGNATURE },
228
    { CIPHER_NAME("SECP160R1"), SEC_OID_SECG_EC_SECP160R1,
229
      NSS_USE_ALG_IN_SSL_KX | NSS_USE_ALG_IN_CERT_SIGNATURE },
230
    { CIPHER_NAME("SECP160R2"), SEC_OID_SECG_EC_SECP160R2,
231
      NSS_USE_ALG_IN_SSL_KX | NSS_USE_ALG_IN_CERT_SIGNATURE },
232
    { CIPHER_NAME("SECP192K1"), SEC_OID_SECG_EC_SECP192K1,
233
      NSS_USE_ALG_IN_SSL_KX | NSS_USE_ALG_IN_CERT_SIGNATURE },
234
    { CIPHER_NAME("SECP192R1"), SEC_OID_ANSIX962_EC_PRIME192V1,
235
      NSS_USE_ALG_IN_SSL_KX | NSS_USE_ALG_IN_CERT_SIGNATURE },
236
    { CIPHER_NAME("SECP224K1"), SEC_OID_SECG_EC_SECP224K1,
237
      NSS_USE_ALG_IN_SSL_KX | NSS_USE_ALG_IN_CERT_SIGNATURE },
238
    { CIPHER_NAME("SECP256K1"), SEC_OID_SECG_EC_SECP256K1,
239
      NSS_USE_ALG_IN_SSL_KX | NSS_USE_ALG_IN_CERT_SIGNATURE },
240
    { CIPHER_NAME("SECP256R1"), SEC_OID_ANSIX962_EC_PRIME256V1,
241
      NSS_USE_ALG_IN_SSL_KX | NSS_USE_ALG_IN_CERT_SIGNATURE },
242
    { CIPHER_NAME("SECP384R1"), SEC_OID_SECG_EC_SECP384R1,
243
      NSS_USE_ALG_IN_SSL_KX | NSS_USE_ALG_IN_CERT_SIGNATURE },
244
    { CIPHER_NAME("SECP521R1"), SEC_OID_SECG_EC_SECP521R1,
245
      NSS_USE_ALG_IN_SSL_KX | NSS_USE_ALG_IN_CERT_SIGNATURE },
246
    { CIPHER_NAME("CURVE25519"), SEC_OID_CURVE25519,
247
      NSS_USE_ALG_IN_SSL_KX | NSS_USE_ALG_IN_CERT_SIGNATURE },
248
    /* ANSI X9.62 named elliptic curves (characteristic two field) */
249
    { CIPHER_NAME("C2PNB163V1"), SEC_OID_ANSIX962_EC_C2PNB163V1,
250
      NSS_USE_ALG_IN_SSL_KX | NSS_USE_ALG_IN_CERT_SIGNATURE },
251
    { CIPHER_NAME("C2PNB163V2"), SEC_OID_ANSIX962_EC_C2PNB163V2,
252
      NSS_USE_ALG_IN_SSL_KX | NSS_USE_ALG_IN_CERT_SIGNATURE },
253
    { CIPHER_NAME("C2PNB163V3"), SEC_OID_ANSIX962_EC_C2PNB163V3,
254
      NSS_USE_ALG_IN_SSL_KX | NSS_USE_ALG_IN_CERT_SIGNATURE },
255
    { CIPHER_NAME("C2PNB176V1"), SEC_OID_ANSIX962_EC_C2PNB176V1,
256
      NSS_USE_ALG_IN_SSL_KX | NSS_USE_ALG_IN_CERT_SIGNATURE },
257
    { CIPHER_NAME("C2TNB191V1"), SEC_OID_ANSIX962_EC_C2TNB191V1,
258
      NSS_USE_ALG_IN_SSL_KX | NSS_USE_ALG_IN_CERT_SIGNATURE },
259
    { CIPHER_NAME("C2TNB191V2"), SEC_OID_ANSIX962_EC_C2TNB191V2,
260
      NSS_USE_ALG_IN_SSL_KX | NSS_USE_ALG_IN_CERT_SIGNATURE },
261
    { CIPHER_NAME("C2TNB191V3"), SEC_OID_ANSIX962_EC_C2TNB191V3,
262
      NSS_USE_ALG_IN_SSL_KX | NSS_USE_ALG_IN_CERT_SIGNATURE },
263
    { CIPHER_NAME("C2ONB191V4"), SEC_OID_ANSIX962_EC_C2ONB191V4,
264
      NSS_USE_ALG_IN_SSL_KX | NSS_USE_ALG_IN_CERT_SIGNATURE },
265
    { CIPHER_NAME("C2ONB191V5"), SEC_OID_ANSIX962_EC_C2ONB191V5,
266
      NSS_USE_ALG_IN_SSL_KX | NSS_USE_ALG_IN_CERT_SIGNATURE },
267
    { CIPHER_NAME("C2PNB208W1"), SEC_OID_ANSIX962_EC_C2PNB208W1,
268
      NSS_USE_ALG_IN_SSL_KX | NSS_USE_ALG_IN_CERT_SIGNATURE },
269
    { CIPHER_NAME("C2TNB239V1"), SEC_OID_ANSIX962_EC_C2TNB239V1,
270
      NSS_USE_ALG_IN_SSL_KX | NSS_USE_ALG_IN_CERT_SIGNATURE },
271
    { CIPHER_NAME("C2TNB239V2"), SEC_OID_ANSIX962_EC_C2TNB239V2,
272
      NSS_USE_ALG_IN_SSL_KX | NSS_USE_ALG_IN_CERT_SIGNATURE },
273
    { CIPHER_NAME("C2TNB239V3"), SEC_OID_ANSIX962_EC_C2TNB239V3,
274
      NSS_USE_ALG_IN_SSL_KX | NSS_USE_ALG_IN_CERT_SIGNATURE },
275
    { CIPHER_NAME("C2ONB239V4"), SEC_OID_ANSIX962_EC_C2ONB239V4,
276
      NSS_USE_ALG_IN_SSL_KX | NSS_USE_ALG_IN_CERT_SIGNATURE },
277
    { CIPHER_NAME("C2ONB239V5"), SEC_OID_ANSIX962_EC_C2ONB239V5,
278
      NSS_USE_ALG_IN_SSL_KX | NSS_USE_ALG_IN_CERT_SIGNATURE },
279
    { CIPHER_NAME("C2PNB272W1"), SEC_OID_ANSIX962_EC_C2PNB272W1,
280
      NSS_USE_ALG_IN_SSL_KX | NSS_USE_ALG_IN_CERT_SIGNATURE },
281
    { CIPHER_NAME("C2PNB304W1"), SEC_OID_ANSIX962_EC_C2PNB304W1,
282
      NSS_USE_ALG_IN_SSL_KX | NSS_USE_ALG_IN_CERT_SIGNATURE },
283
    { CIPHER_NAME("C2TNB359V1"), SEC_OID_ANSIX962_EC_C2TNB359V1,
284
      NSS_USE_ALG_IN_SSL_KX | NSS_USE_ALG_IN_CERT_SIGNATURE },
285
    { CIPHER_NAME("C2PNB368W1"), SEC_OID_ANSIX962_EC_C2PNB368W1,
286
      NSS_USE_ALG_IN_SSL_KX | NSS_USE_ALG_IN_CERT_SIGNATURE },
287
    { CIPHER_NAME("C2TNB431R1"), SEC_OID_ANSIX962_EC_C2TNB431R1,
288
      NSS_USE_ALG_IN_SSL_KX | NSS_USE_ALG_IN_CERT_SIGNATURE },
289
    /* SECG named elliptic curves (characteristic two field) */
290
    { CIPHER_NAME("SECT113R1"), SEC_OID_SECG_EC_SECT113R1,
291
      NSS_USE_ALG_IN_SSL_KX | NSS_USE_ALG_IN_CERT_SIGNATURE },
292
    { CIPHER_NAME("SECT131R1"), SEC_OID_SECG_EC_SECT113R2,
293
      NSS_USE_ALG_IN_SSL_KX | NSS_USE_ALG_IN_CERT_SIGNATURE },
294
    { CIPHER_NAME("SECT131R1"), SEC_OID_SECG_EC_SECT131R1,
295
      NSS_USE_ALG_IN_SSL_KX | NSS_USE_ALG_IN_CERT_SIGNATURE },
296
    { CIPHER_NAME("SECT131R2"), SEC_OID_SECG_EC_SECT131R2,
297
      NSS_USE_ALG_IN_SSL_KX | NSS_USE_ALG_IN_CERT_SIGNATURE },
298
    { CIPHER_NAME("SECT163K1"), SEC_OID_SECG_EC_SECT163K1,
299
      NSS_USE_ALG_IN_SSL_KX | NSS_USE_ALG_IN_CERT_SIGNATURE },
300
    { CIPHER_NAME("SECT163R1"), SEC_OID_SECG_EC_SECT163R1,
301
      NSS_USE_ALG_IN_SSL_KX | NSS_USE_ALG_IN_CERT_SIGNATURE },
302
    { CIPHER_NAME("SECT163R2"), SEC_OID_SECG_EC_SECT163R2,
303
      NSS_USE_ALG_IN_SSL_KX | NSS_USE_ALG_IN_CERT_SIGNATURE },
304
    { CIPHER_NAME("SECT193R1"), SEC_OID_SECG_EC_SECT193R1,
305
      NSS_USE_ALG_IN_SSL_KX | NSS_USE_ALG_IN_CERT_SIGNATURE },
306
    { CIPHER_NAME("SECT193R2"), SEC_OID_SECG_EC_SECT193R2,
307
      NSS_USE_ALG_IN_SSL_KX | NSS_USE_ALG_IN_CERT_SIGNATURE },
308
    { CIPHER_NAME("SECT233K1"), SEC_OID_SECG_EC_SECT233K1,
309
      NSS_USE_ALG_IN_SSL_KX | NSS_USE_ALG_IN_CERT_SIGNATURE },
310
    { CIPHER_NAME("SECT233R1"), SEC_OID_SECG_EC_SECT233R1,
311
      NSS_USE_ALG_IN_SSL_KX | NSS_USE_ALG_IN_CERT_SIGNATURE },
312
    { CIPHER_NAME("SECT239K1"), SEC_OID_SECG_EC_SECT239K1,
313
      NSS_USE_ALG_IN_SSL_KX | NSS_USE_ALG_IN_CERT_SIGNATURE },
314
    { CIPHER_NAME("SECT283K1"), SEC_OID_SECG_EC_SECT283K1,
315
      NSS_USE_ALG_IN_SSL_KX | NSS_USE_ALG_IN_CERT_SIGNATURE },
316
    { CIPHER_NAME("SECT283R1"), SEC_OID_SECG_EC_SECT283R1,
317
      NSS_USE_ALG_IN_SSL_KX | NSS_USE_ALG_IN_CERT_SIGNATURE },
318
    { CIPHER_NAME("SECT409K1"), SEC_OID_SECG_EC_SECT409K1,
319
      NSS_USE_ALG_IN_SSL_KX | NSS_USE_ALG_IN_CERT_SIGNATURE },
320
    { CIPHER_NAME("SECT409R1"), SEC_OID_SECG_EC_SECT409R1,
321
      NSS_USE_ALG_IN_SSL_KX | NSS_USE_ALG_IN_CERT_SIGNATURE },
322
    { CIPHER_NAME("SECT571K1"), SEC_OID_SECG_EC_SECT571K1,
323
      NSS_USE_ALG_IN_SSL_KX | NSS_USE_ALG_IN_CERT_SIGNATURE },
324
    { CIPHER_NAME("SECT571R1"), SEC_OID_SECG_EC_SECT571R1,
325
      NSS_USE_ALG_IN_SSL_KX | NSS_USE_ALG_IN_CERT_SIGNATURE },
326
};
327
328
static const oidValDef hashOptList[] = {
329
    /* Hashes */
330
    { CIPHER_NAME("MD2"), SEC_OID_MD2,
331
      NSS_USE_ALG_IN_SSL_KX | NSS_USE_ALG_IN_SIGNATURE },
332
    { CIPHER_NAME("MD4"), SEC_OID_MD4,
333
      NSS_USE_ALG_IN_SSL_KX | NSS_USE_ALG_IN_SIGNATURE },
334
    { CIPHER_NAME("MD5"), SEC_OID_MD5,
335
      NSS_USE_ALG_IN_SSL_KX | NSS_USE_ALG_IN_SIGNATURE },
336
    { CIPHER_NAME("SHA1"), SEC_OID_SHA1,
337
      NSS_USE_ALG_IN_SSL_KX | NSS_USE_ALG_IN_SIGNATURE },
338
    { CIPHER_NAME("SHA224"), SEC_OID_SHA224,
339
      NSS_USE_ALG_IN_SSL_KX | NSS_USE_ALG_IN_SIGNATURE },
340
    { CIPHER_NAME("SHA256"), SEC_OID_SHA256,
341
      NSS_USE_ALG_IN_SSL_KX | NSS_USE_ALG_IN_SIGNATURE },
342
    { CIPHER_NAME("SHA384"), SEC_OID_SHA384,
343
      NSS_USE_ALG_IN_SSL_KX | NSS_USE_ALG_IN_SIGNATURE },
344
    { CIPHER_NAME("SHA512"), SEC_OID_SHA512,
345
      NSS_USE_ALG_IN_SSL_KX | NSS_USE_ALG_IN_SIGNATURE }
346
};
347
348
static const oidValDef macOptList[] = {
349
    /* MACs */
350
    { CIPHER_NAME("HMAC-SHA1"), SEC_OID_HMAC_SHA1, NSS_USE_ALG_IN_SSL },
351
    { CIPHER_NAME("HMAC-SHA224"), SEC_OID_HMAC_SHA224, NSS_USE_ALG_IN_SSL },
352
    { CIPHER_NAME("HMAC-SHA256"), SEC_OID_HMAC_SHA256, NSS_USE_ALG_IN_SSL },
353
    { CIPHER_NAME("HMAC-SHA384"), SEC_OID_HMAC_SHA384, NSS_USE_ALG_IN_SSL },
354
    { CIPHER_NAME("HMAC-SHA512"), SEC_OID_HMAC_SHA512, NSS_USE_ALG_IN_SSL },
355
    { CIPHER_NAME("HMAC-MD5"), SEC_OID_HMAC_MD5, NSS_USE_ALG_IN_SSL },
356
};
357
358
static const oidValDef cipherOptList[] = {
359
    /* Ciphers */
360
    { CIPHER_NAME("AES128-CBC"), SEC_OID_AES_128_CBC, NSS_USE_ALG_IN_SSL },
361
    { CIPHER_NAME("AES192-CBC"), SEC_OID_AES_192_CBC, NSS_USE_ALG_IN_SSL },
362
    { CIPHER_NAME("AES256-CBC"), SEC_OID_AES_256_CBC, NSS_USE_ALG_IN_SSL },
363
    { CIPHER_NAME("AES128-GCM"), SEC_OID_AES_128_GCM, NSS_USE_ALG_IN_SSL },
364
    { CIPHER_NAME("AES192-GCM"), SEC_OID_AES_192_GCM, NSS_USE_ALG_IN_SSL },
365
    { CIPHER_NAME("AES256-GCM"), SEC_OID_AES_256_GCM, NSS_USE_ALG_IN_SSL },
366
    { CIPHER_NAME("CAMELLIA128-CBC"), SEC_OID_CAMELLIA_128_CBC, NSS_USE_ALG_IN_SSL },
367
    { CIPHER_NAME("CAMELLIA192-CBC"), SEC_OID_CAMELLIA_192_CBC, NSS_USE_ALG_IN_SSL },
368
    { CIPHER_NAME("CAMELLIA256-CBC"), SEC_OID_CAMELLIA_256_CBC, NSS_USE_ALG_IN_SSL },
369
    { CIPHER_NAME("CHACHA20-POLY1305"), SEC_OID_CHACHA20_POLY1305, NSS_USE_ALG_IN_SSL },
370
    { CIPHER_NAME("SEED-CBC"), SEC_OID_SEED_CBC, NSS_USE_ALG_IN_SSL },
371
    { CIPHER_NAME("DES-EDE3-CBC"), SEC_OID_DES_EDE3_CBC, NSS_USE_ALG_IN_SSL },
372
    { CIPHER_NAME("DES-40-CBC"), SEC_OID_DES_40_CBC, NSS_USE_ALG_IN_SSL },
373
    { CIPHER_NAME("DES-CBC"), SEC_OID_DES_CBC, NSS_USE_ALG_IN_SSL },
374
    { CIPHER_NAME("NULL-CIPHER"), SEC_OID_NULL_CIPHER, NSS_USE_ALG_IN_SSL },
375
    { CIPHER_NAME("RC2"), SEC_OID_RC2_CBC, NSS_USE_ALG_IN_SSL },
376
    { CIPHER_NAME("RC4"), SEC_OID_RC4, NSS_USE_ALG_IN_SSL },
377
    { CIPHER_NAME("IDEA"), SEC_OID_IDEA_CBC, NSS_USE_ALG_IN_SSL },
378
};
379
380
static const oidValDef kxOptList[] = {
381
    /* Key exchange */
382
    { CIPHER_NAME("RSA"), SEC_OID_TLS_RSA, NSS_USE_ALG_IN_SSL_KX },
383
    { CIPHER_NAME("RSA-EXPORT"), SEC_OID_TLS_RSA_EXPORT, NSS_USE_ALG_IN_SSL_KX },
384
    { CIPHER_NAME("DHE-RSA"), SEC_OID_TLS_DHE_RSA, NSS_USE_ALG_IN_SSL_KX },
385
    { CIPHER_NAME("DHE-DSS"), SEC_OID_TLS_DHE_DSS, NSS_USE_ALG_IN_SSL_KX },
386
    { CIPHER_NAME("DH-RSA"), SEC_OID_TLS_DH_RSA, NSS_USE_ALG_IN_SSL_KX },
387
    { CIPHER_NAME("DH-DSS"), SEC_OID_TLS_DH_DSS, NSS_USE_ALG_IN_SSL_KX },
388
    { CIPHER_NAME("ECDHE-ECDSA"), SEC_OID_TLS_ECDHE_ECDSA, NSS_USE_ALG_IN_SSL_KX },
389
    { CIPHER_NAME("ECDHE-RSA"), SEC_OID_TLS_ECDHE_RSA, NSS_USE_ALG_IN_SSL_KX },
390
    { CIPHER_NAME("ECDH-ECDSA"), SEC_OID_TLS_ECDH_ECDSA, NSS_USE_ALG_IN_SSL_KX },
391
    { CIPHER_NAME("ECDH-RSA"), SEC_OID_TLS_ECDH_RSA, NSS_USE_ALG_IN_SSL_KX },
392
};
393
394
static const oidValDef signOptList[] = {
395
    /* Signatures */
396
    { CIPHER_NAME("DSA"), SEC_OID_ANSIX9_DSA_SIGNATURE,
397
      NSS_USE_ALG_IN_SSL_KX | NSS_USE_ALG_IN_SIGNATURE },
398
    { CIPHER_NAME("RSA-PKCS"), SEC_OID_PKCS1_RSA_ENCRYPTION,
399
      NSS_USE_ALG_IN_SSL_KX | NSS_USE_ALG_IN_SIGNATURE },
400
    { CIPHER_NAME("RSA-PSS"), SEC_OID_PKCS1_RSA_PSS_SIGNATURE,
401
      NSS_USE_ALG_IN_SSL_KX | NSS_USE_ALG_IN_SIGNATURE },
402
    { CIPHER_NAME("ECDSA"), SEC_OID_ANSIX962_EC_PUBLIC_KEY,
403
      NSS_USE_ALG_IN_SSL_KX | NSS_USE_ALG_IN_SIGNATURE },
404
};
405
406
typedef struct {
407
    const oidValDef *list;
408
    PRUint32 entries;
409
    const char *description;
410
    PRBool allowEmpty;
411
} algListsDef;
412
413
static const algListsDef algOptLists[] = {
414
    { curveOptList, PR_ARRAY_SIZE(curveOptList), "ECC", PR_FALSE },
415
    { hashOptList, PR_ARRAY_SIZE(hashOptList), "HASH", PR_FALSE },
416
    { macOptList, PR_ARRAY_SIZE(macOptList), "MAC", PR_FALSE },
417
    { cipherOptList, PR_ARRAY_SIZE(cipherOptList), "CIPHER", PR_FALSE },
418
    { kxOptList, PR_ARRAY_SIZE(kxOptList), "OTHER-KX", PR_FALSE },
419
    { signOptList, PR_ARRAY_SIZE(signOptList), "OTHER-SIGN", PR_FALSE },
420
};
421
422
static const optionFreeDef sslOptList[] = {
423
    /* Versions */
424
    { CIPHER_NAME("SSL2.0"), 0x002 },
425
    { CIPHER_NAME("SSL3.0"), 0x300 },
426
    { CIPHER_NAME("SSL3.1"), 0x301 },
427
    { CIPHER_NAME("TLS1.0"), 0x301 },
428
    { CIPHER_NAME("TLS1.1"), 0x302 },
429
    { CIPHER_NAME("TLS1.2"), 0x303 },
430
    { CIPHER_NAME("TLS1.3"), 0x304 },
431
    { CIPHER_NAME("DTLS1.0"), 0x302 },
432
    { CIPHER_NAME("DTLS1.1"), 0x302 },
433
    { CIPHER_NAME("DTLS1.2"), 0x303 },
434
    { CIPHER_NAME("DTLS1.3"), 0x304 },
435
};
436
437
static const optionFreeDef keySizeFlagsList[] = {
438
    { CIPHER_NAME("KEY-SIZE-SSL"), NSS_KEY_SIZE_POLICY_SSL_FLAG },
439
    { CIPHER_NAME("KEY-SIZE-SIGN"), NSS_KEY_SIZE_POLICY_SIGN_FLAG },
440
    { CIPHER_NAME("KEY-SIZE-VERIFY"), NSS_KEY_SIZE_POLICY_VERIFY_FLAG },
441
};
442
443
static const optionFreeDef freeOptList[] = {
444
445
    /* Restrictions for asymetric keys */
446
    { CIPHER_NAME("RSA-MIN"), NSS_RSA_MIN_KEY_SIZE },
447
    { CIPHER_NAME("DH-MIN"), NSS_DH_MIN_KEY_SIZE },
448
    { CIPHER_NAME("DSA-MIN"), NSS_DSA_MIN_KEY_SIZE },
449
    { CIPHER_NAME("ECC-MIN"), NSS_ECC_MIN_KEY_SIZE },
450
    /* what operations doe the key size apply to */
451
    { CIPHER_NAME("KEY-SIZE-FLAGS"), NSS_KEY_SIZE_POLICY_FLAGS },
452
    /* constraints on SSL Protocols */
453
    { CIPHER_NAME("TLS-VERSION-MIN"), NSS_TLS_VERSION_MIN_POLICY },
454
    { CIPHER_NAME("TLS-VERSION-MAX"), NSS_TLS_VERSION_MAX_POLICY },
455
    /* constraints on DTLS Protocols */
456
    { CIPHER_NAME("DTLS-VERSION-MIN"), NSS_DTLS_VERSION_MIN_POLICY },
457
    { CIPHER_NAME("DTLS-VERSION-MAX"), NSS_DTLS_VERSION_MAX_POLICY }
458
};
459
460
static const policyFlagDef policyFlagList[] = {
461
    { CIPHER_NAME("SSL"), NSS_USE_ALG_IN_SSL },
462
    { CIPHER_NAME("SSL-KEY-EXCHANGE"), NSS_USE_ALG_IN_SSL_KX },
463
    /* add other key exhanges in the future */
464
    { CIPHER_NAME("KEY-EXCHANGE"), NSS_USE_ALG_IN_SSL_KX },
465
    { CIPHER_NAME("CERT-SIGNATURE"), NSS_USE_ALG_IN_CERT_SIGNATURE },
466
    { CIPHER_NAME("CMS-SIGNATURE"), NSS_USE_ALG_IN_CMS_SIGNATURE },
467
    { CIPHER_NAME("ALL-SIGNATURE"), NSS_USE_ALG_IN_SIGNATURE },
468
    /* sign turns off all signatures, but doesn't change the
469
     * allowance for specific sigantures... for example:
470
     * disallow=sha256/all allow=sha256/signature doesn't allow
471
     * cert-sigantures, where disallow=sha256/all allow=sha256/all-signature
472
     * does.
473
     * however, disallow=sha356/signature and disallow=sha256/all-siganture are
474
     * equivalent in effect */
475
    { CIPHER_NAME("SIGNATURE"), NSS_USE_ALG_IN_ANY_SIGNATURE },
476
    /* enable/disable everything */
477
    { CIPHER_NAME("ALL"), NSS_USE_ALG_IN_SSL | NSS_USE_ALG_IN_SSL_KX |
478
                              NSS_USE_ALG_IN_SIGNATURE },
479
    { CIPHER_NAME("NONE"), 0 }
480
};
481
482
/*
483
 *  Get the next cipher on the list. point to the next one in 'next'.
484
 *  return the length;
485
 */
486
static const char *
487
secmod_ArgGetSubValue(const char *cipher, char sep1, char sep2,
488
                      int *len, const char **next)
489
0
{
490
0
    const char *start = cipher;
491
492
0
    if (start == NULL) {
493
0
        *len = 0;
494
0
        *next = NULL;
495
0
        return start;
496
0
    }
497
498
0
    for (; *cipher && *cipher != sep2; cipher++) {
499
0
        if (*cipher == sep1) {
500
0
            *next = cipher + 1;
501
0
            *len = cipher - start;
502
0
            return start;
503
0
        }
504
0
    }
505
0
    *next = NULL;
506
0
    *len = cipher - start;
507
0
    return start;
508
0
}
509
510
static PRUint32
511
secmod_parsePolicyValue(const char *policyFlags, int policyLength,
512
                        PRBool printPolicyFeedback, PRUint32 policyCheckFlags)
513
0
{
514
0
    const char *flag, *currentString;
515
0
    PRUint32 flags = 0;
516
0
    int i;
517
518
0
    for (currentString = policyFlags; currentString &&
519
0
                                      currentString < policyFlags + policyLength;) {
520
0
        int length;
521
0
        PRBool unknown = PR_TRUE;
522
0
        flag = secmod_ArgGetSubValue(currentString, ',', ':', &length,
523
0
                                     &currentString);
524
0
        if (length == 0) {
525
0
            continue;
526
0
        }
527
0
        for (i = 0; i < PR_ARRAY_SIZE(policyFlagList); i++) {
528
0
            const policyFlagDef *policy = &policyFlagList[i];
529
0
            unsigned name_size = policy->name_size;
530
0
            if ((policy->name_size == length) &&
531
0
                PORT_Strncasecmp(policy->name, flag, name_size) == 0) {
532
0
                flags |= policy->flag;
533
0
                unknown = PR_FALSE;
534
0
                break;
535
0
            }
536
0
        }
537
0
        if (unknown && printPolicyFeedback &&
538
0
            (policyCheckFlags & SECMOD_FLAG_POLICY_CHECK_VALUE)) {
539
0
            PR_SetEnv("NSS_POLICY_FAIL=1");
540
0
            fprintf(stderr, "NSS-POLICY-FAIL %.*s: unknown value: %.*s\n",
541
0
                    policyLength, policyFlags, length, flag);
542
0
        }
543
0
    }
544
0
    return flags;
545
0
}
546
547
/* allow symbolic names for values. The only ones currently defines or
548
 * SSL protocol versions. */
549
static SECStatus
550
secmod_getPolicyOptValue(const char *policyValue, int policyValueLength,
551
                         PRInt32 *result)
552
0
{
553
0
    PRInt32 val = atoi(policyValue);
554
0
    int i;
555
556
0
    if ((val != 0) || (*policyValue == '0')) {
557
0
        *result = val;
558
0
        return SECSuccess;
559
0
    }
560
    /* handle any ssl strings */
561
0
    for (i = 0; i < PR_ARRAY_SIZE(sslOptList); i++) {
562
0
        if (policyValueLength == sslOptList[i].name_size &&
563
0
            PORT_Strncasecmp(sslOptList[i].name, policyValue,
564
0
                             sslOptList[i].name_size) == 0) {
565
0
            *result = sslOptList[i].option;
566
0
            return SECSuccess;
567
0
        }
568
0
    }
569
    /* handle key_size flags. Each flag represents a bit, which
570
     * gets or'd together. They can be separated by , | or + */
571
0
    val = 0;
572
0
    while (*policyValue) {
573
0
        PRBool found = PR_FALSE;
574
0
        for (i = 0; i < PR_ARRAY_SIZE(keySizeFlagsList); i++) {
575
0
            if (PORT_Strncasecmp(keySizeFlagsList[i].name, policyValue,
576
0
                                 keySizeFlagsList[i].name_size) == 0) {
577
0
                val |= keySizeFlagsList[i].option;
578
0
                found = PR_TRUE;
579
0
                policyValue += keySizeFlagsList[i].name_size;
580
0
                break;
581
0
            }
582
0
        }
583
0
        if (!found) {
584
0
            return SECFailure;
585
0
        }
586
0
        if (*policyValue == ',' || *policyValue == '|' || *policyValue == '+') {
587
0
            policyValue++;
588
0
        }
589
0
    }
590
0
    *result = val;
591
0
    return SECSuccess;
592
0
}
593
594
/* Policy operations:
595
 *     Disallow: operation is disallowed by policy. Implies disabled.
596
 *     Allow: operation is allowed by policy (but could be disabled).
597
 *     Disable: operation is turned off by default (but could be allowed).
598
 *     Enable: operation is enabled by default. Implies allowed.
599
 */
600
typedef enum {
601
    NSS_DISALLOW,
602
    NSS_ALLOW,
603
    NSS_DISABLE,
604
    NSS_ENABLE
605
} NSSPolicyOperation;
606
607
/* apply the operator specific policy */
608
SECStatus
609
secmod_setPolicyOperation(SECOidTag oid, NSSPolicyOperation operation,
610
                          PRUint32 value)
611
0
{
612
0
    SECStatus rv = SECSuccess;
613
0
    switch (operation) {
614
0
        case NSS_DISALLOW:
615
            /* clear the requested policy bits */
616
0
            rv = NSS_SetAlgorithmPolicy(oid, 0, value);
617
0
            break;
618
0
        case NSS_ALLOW:
619
            /* set the requested policy bits */
620
0
            rv = NSS_SetAlgorithmPolicy(oid, value, 0);
621
0
            break;
622
        /* enable/disable only apply to SSL cipher suites (future S/MIME).
623
         * Enable/disable is implemented by clearing the DEFAULT_NOT_VALID
624
         * flag, then setting the NSS_USE_DEFAULT_SSL_ENABLE flag to the
625
         * correct value. The ssl policy code will then sort out what to
626
         * set based on ciphers and cipher suite values.*/
627
0
        case NSS_DISABLE:
628
0
            if (value & (NSS_USE_ALG_IN_SSL | NSS_USE_ALG_IN_SSL_KX)) {
629
                /* clear not valid and enable */
630
0
                rv = NSS_SetAlgorithmPolicy(oid, 0,
631
0
                                            NSS_USE_DEFAULT_NOT_VALID |
632
0
                                                NSS_USE_DEFAULT_SSL_ENABLE);
633
0
            }
634
0
            break;
635
0
        case NSS_ENABLE:
636
0
            if (value & (NSS_USE_ALG_IN_SSL | NSS_USE_ALG_IN_SSL_KX)) {
637
                /* set enable, clear not valid. NOTE: enable implies allow! */
638
0
                rv = NSS_SetAlgorithmPolicy(oid, value | NSS_USE_DEFAULT_SSL_ENABLE,
639
0
                                            NSS_USE_DEFAULT_NOT_VALID);
640
0
            }
641
0
            break;
642
0
        default:
643
0
            PORT_SetError(SEC_ERROR_LIBRARY_FAILURE);
644
0
            rv = SECFailure;
645
0
            break;
646
0
    }
647
0
    return rv;
648
0
}
649
650
const char *
651
secmod_getOperationString(NSSPolicyOperation operation)
652
0
{
653
0
    switch (operation) {
654
0
        case NSS_DISALLOW:
655
0
            return "disallow";
656
0
        case NSS_ALLOW:
657
0
            return "allow";
658
0
        case NSS_DISABLE:
659
0
            return "disable";
660
0
        case NSS_ENABLE:
661
0
            return "enable";
662
0
        default:
663
0
            break;
664
0
    }
665
0
    return "invalid";
666
0
}
667
668
static SECStatus
669
secmod_applyCryptoPolicy(const char *policyString, NSSPolicyOperation operation,
670
                         PRBool printPolicyFeedback, PRUint32 policyCheckFlags)
671
0
{
672
0
    const char *cipher, *currentString;
673
0
    unsigned i, j;
674
0
    SECStatus rv = SECSuccess;
675
0
    PRBool unknown;
676
677
0
    if (policyString == NULL || policyString[0] == 0) {
678
0
        return SECSuccess; /* do nothing */
679
0
    }
680
681
    /* if we change any of these, make sure it gets applied in ssl as well */
682
0
    NSS_SetAlgorithmPolicy(SEC_OID_APPLY_SSL_POLICY, NSS_USE_POLICY_IN_SSL, 0);
683
684
0
    for (currentString = policyString; currentString;) {
685
0
        int length;
686
0
        PRBool newValue = PR_FALSE;
687
688
0
        cipher = secmod_ArgGetSubValue(currentString, ':', 0, &length,
689
0
                                       &currentString);
690
0
        unknown = PR_TRUE;
691
0
        if (length >= 3 && cipher[3] == '/') {
692
0
            newValue = PR_TRUE;
693
0
        }
694
0
        if ((newValue || (length == 3)) && PORT_Strncasecmp(cipher, "all", 3) == 0) {
695
            /* disable or enable all options by default */
696
0
            PRUint32 value = 0;
697
0
            if (newValue) {
698
0
                value = secmod_parsePolicyValue(&cipher[3] + 1, length - 3 - 1, printPolicyFeedback, policyCheckFlags);
699
0
            }
700
0
            for (i = 0; i < PR_ARRAY_SIZE(algOptLists); i++) {
701
0
                const algListsDef *algOptList = &algOptLists[i];
702
0
                for (j = 0; j < algOptList->entries; j++) {
703
0
                    if (!newValue) {
704
0
                        value = algOptList->list[j].val;
705
0
                    }
706
0
                    secmod_setPolicyOperation(algOptList->list[j].oid, operation, value);
707
0
                }
708
0
            }
709
0
            continue;
710
0
        }
711
712
0
        for (i = 0; i < PR_ARRAY_SIZE(algOptLists); i++) {
713
0
            const algListsDef *algOptList = &algOptLists[i];
714
0
            for (j = 0; j < algOptList->entries; j++) {
715
0
                const oidValDef *algOpt = &algOptList->list[j];
716
0
                unsigned name_size = algOpt->name_size;
717
0
                PRBool newOption = PR_FALSE;
718
719
0
                if ((length >= name_size) && (cipher[name_size] == '/')) {
720
0
                    newOption = PR_TRUE;
721
0
                }
722
0
                if ((newOption || algOpt->name_size == length) &&
723
0
                    PORT_Strncasecmp(algOpt->name, cipher, name_size) == 0) {
724
0
                    PRUint32 value = algOpt->val;
725
0
                    if (newOption) {
726
0
                        value = secmod_parsePolicyValue(&cipher[name_size] + 1,
727
0
                                                        length - name_size - 1,
728
0
                                                        printPolicyFeedback,
729
0
                                                        policyCheckFlags);
730
0
                    }
731
0
                    rv = secmod_setPolicyOperation(algOptList->list[j].oid, operation, value);
732
0
                    if (rv != SECSuccess) {
733
                        /* could not enable option */
734
                        /* NSS_SetAlgorithPolicy should have set the error code */
735
0
                        return SECFailure;
736
0
                    }
737
0
                    unknown = PR_FALSE;
738
0
                    break;
739
0
                }
740
0
            }
741
0
        }
742
0
        if (!unknown) {
743
0
            continue;
744
0
        }
745
746
0
        for (i = 0; i < PR_ARRAY_SIZE(freeOptList); i++) {
747
0
            const optionFreeDef *freeOpt = &freeOptList[i];
748
0
            unsigned name_size = freeOpt->name_size;
749
750
0
            if ((length > name_size) && cipher[name_size] == '=' &&
751
0
                PORT_Strncasecmp(freeOpt->name, cipher, name_size) == 0) {
752
0
                PRInt32 val;
753
0
                const char *policyValue = &cipher[name_size + 1];
754
0
                int policyValueLength = length - name_size - 1;
755
0
                rv = secmod_getPolicyOptValue(policyValue, policyValueLength,
756
0
                                              &val);
757
0
                if (rv != SECSuccess) {
758
0
                    if (printPolicyFeedback &&
759
0
                        (policyCheckFlags & SECMOD_FLAG_POLICY_CHECK_VALUE)) {
760
0
                        PR_SetEnv("NSS_POLICY_FAIL=1");
761
0
                        fprintf(stderr, "NSS-POLICY-FAIL %.*s: unknown value: %.*s\n",
762
0
                                length, cipher, policyValueLength, policyValue);
763
0
                    }
764
0
                    return SECFailure;
765
0
                }
766
0
                rv = NSS_OptionSet(freeOpt->option, val);
767
0
                if (rv != SECSuccess) {
768
                    /* could not enable option */
769
                    /* NSS_OptionSet should have set the error code */
770
0
                    return SECFailure;
771
0
                }
772
                /* to allow the policy to expand in the future. ignore ciphers
773
                 * we don't understand */
774
0
                unknown = PR_FALSE;
775
0
                break;
776
0
            }
777
0
        }
778
779
0
        if (unknown && printPolicyFeedback &&
780
0
            (policyCheckFlags & SECMOD_FLAG_POLICY_CHECK_IDENTIFIER)) {
781
0
            PR_SetEnv("NSS_POLICY_FAIL=1");
782
0
            fprintf(stderr, "NSS-POLICY-FAIL %s: unknown identifier: %.*s\n",
783
0
                    secmod_getOperationString(operation), length, cipher);
784
0
        }
785
0
    }
786
0
    return rv;
787
0
}
788
789
static void
790
secmod_sanityCheckCryptoPolicy(void)
791
0
{
792
0
    unsigned i, j;
793
0
    SECStatus rv = SECSuccess;
794
0
    unsigned num_kx_enabled = 0;
795
0
    unsigned num_ssl_enabled = 0;
796
0
    unsigned num_sig_enabled = 0;
797
0
    unsigned enabledCount[PR_ARRAY_SIZE(algOptLists)];
798
0
    const char *sWarn = "WARN";
799
0
    const char *sInfo = "INFO";
800
0
    PRBool haveWarning = PR_FALSE;
801
802
0
    for (i = 0; i < PR_ARRAY_SIZE(algOptLists); i++) {
803
0
        const algListsDef *algOptList = &algOptLists[i];
804
0
        enabledCount[i] = 0;
805
0
        for (j = 0; j < algOptList->entries; j++) {
806
0
            const oidValDef *algOpt = &algOptList->list[j];
807
0
            PRUint32 value;
808
0
            PRBool anyEnabled = PR_FALSE;
809
0
            rv = NSS_GetAlgorithmPolicy(algOpt->oid, &value);
810
0
            if (rv != SECSuccess) {
811
0
                PR_SetEnv("NSS_POLICY_FAIL=1");
812
0
                fprintf(stderr, "NSS-POLICY-FAIL: internal failure with NSS_GetAlgorithmPolicy at %u\n", i);
813
0
                return;
814
0
            }
815
816
0
            if ((algOpt->val & NSS_USE_ALG_IN_SSL_KX) && (value & NSS_USE_ALG_IN_SSL_KX)) {
817
0
                ++num_kx_enabled;
818
0
                anyEnabled = PR_TRUE;
819
0
                fprintf(stderr, "NSS-POLICY-INFO: %s is enabled for KX\n", algOpt->name);
820
0
            }
821
0
            if ((algOpt->val & NSS_USE_ALG_IN_SSL) && (value & NSS_USE_ALG_IN_SSL)) {
822
0
                ++num_ssl_enabled;
823
0
                anyEnabled = PR_TRUE;
824
0
                fprintf(stderr, "NSS-POLICY-INFO: %s is enabled for SSL\n", algOpt->name);
825
0
            }
826
0
            if ((algOpt->val & NSS_USE_ALG_IN_CERT_SIGNATURE) &&
827
0
                ((value & NSS_USE_CERT_SIGNATURE_OK) == NSS_USE_CERT_SIGNATURE_OK)) {
828
0
                ++num_sig_enabled;
829
0
                anyEnabled = PR_TRUE;
830
0
                fprintf(stderr, "NSS-POLICY-INFO: %s is enabled for CERT-SIGNATURE\n", algOpt->name);
831
0
            }
832
0
            if (anyEnabled) {
833
0
                ++enabledCount[i];
834
0
            }
835
0
        }
836
0
    }
837
0
    fprintf(stderr, "NSS-POLICY-%s: NUMBER-OF-SSL-ALG-KX: %u\n", num_kx_enabled ? sInfo : sWarn, num_kx_enabled);
838
0
    fprintf(stderr, "NSS-POLICY-%s: NUMBER-OF-SSL-ALG: %u\n", num_ssl_enabled ? sInfo : sWarn, num_ssl_enabled);
839
0
    fprintf(stderr, "NSS-POLICY-%s: NUMBER-OF-CERT-SIG: %u\n", num_sig_enabled ? sInfo : sWarn, num_sig_enabled);
840
0
    if (!num_kx_enabled || !num_ssl_enabled || !num_sig_enabled) {
841
0
        haveWarning = PR_TRUE;
842
0
    }
843
0
    for (i = 0; i < PR_ARRAY_SIZE(algOptLists); i++) {
844
0
        const algListsDef *algOptList = &algOptLists[i];
845
0
        fprintf(stderr, "NSS-POLICY-%s: NUMBER-OF-%s: %u\n", enabledCount[i] ? sInfo : sWarn, algOptList->description, enabledCount[i]);
846
0
        if (!enabledCount[i] && !algOptList->allowEmpty) {
847
0
            haveWarning = PR_TRUE;
848
0
        }
849
0
    }
850
0
    if (haveWarning) {
851
0
        PR_SetEnv("NSS_POLICY_WARN=1");
852
0
    }
853
0
}
854
855
static SECStatus
856
secmod_parseCryptoPolicy(const char *policyConfig, PRBool printPolicyFeedback,
857
                         PRUint32 policyCheckFlags)
858
74.3k
{
859
74.3k
    char *args;
860
74.3k
    SECStatus rv;
861
862
74.3k
    if (policyConfig == NULL) {
863
74.3k
        return SECSuccess; /* no policy given */
864
74.3k
    }
865
    /* make sure we initialize the oid table and set all the default policy
866
     * values first so we can override them here */
867
0
    rv = SECOID_Init();
868
0
    if (rv != SECSuccess) {
869
0
        return rv;
870
0
    }
871
0
    args = NSSUTIL_ArgGetParamValue("disallow", policyConfig);
872
0
    rv = secmod_applyCryptoPolicy(args, NSS_DISALLOW, printPolicyFeedback,
873
0
                                  policyCheckFlags);
874
0
    if (args)
875
0
        PORT_Free(args);
876
0
    if (rv != SECSuccess) {
877
0
        return rv;
878
0
    }
879
0
    args = NSSUTIL_ArgGetParamValue("allow", policyConfig);
880
0
    rv = secmod_applyCryptoPolicy(args, NSS_ALLOW, printPolicyFeedback,
881
0
                                  policyCheckFlags);
882
0
    if (args)
883
0
        PORT_Free(args);
884
0
    if (rv != SECSuccess) {
885
0
        return rv;
886
0
    }
887
0
    args = NSSUTIL_ArgGetParamValue("disable", policyConfig);
888
0
    rv = secmod_applyCryptoPolicy(args, NSS_DISABLE, printPolicyFeedback,
889
0
                                  policyCheckFlags);
890
0
    if (args)
891
0
        PORT_Free(args);
892
0
    if (rv != SECSuccess) {
893
0
        return rv;
894
0
    }
895
0
    args = NSSUTIL_ArgGetParamValue("enable", policyConfig);
896
0
    rv = secmod_applyCryptoPolicy(args, NSS_ENABLE, printPolicyFeedback,
897
0
                                  policyCheckFlags);
898
0
    if (args)
899
0
        PORT_Free(args);
900
0
    if (rv != SECSuccess) {
901
0
        return rv;
902
0
    }
903
    /* this has to be last. Everything after this will be a noop */
904
0
    if (NSSUTIL_ArgHasFlag("flags", "ssl-lock", policyConfig)) {
905
0
        PRInt32 locks;
906
        /* don't overwrite other (future) lock flags */
907
0
        rv = NSS_OptionGet(NSS_DEFAULT_LOCKS, &locks);
908
0
        if (rv == SECSuccess) {
909
0
            rv = NSS_OptionSet(NSS_DEFAULT_LOCKS, locks | NSS_DEFAULT_SSL_LOCK);
910
0
        }
911
0
        if (rv != SECSuccess) {
912
0
            return rv;
913
0
        }
914
0
    }
915
0
    if (NSSUTIL_ArgHasFlag("flags", "policy-lock", policyConfig)) {
916
0
        NSS_LockPolicy();
917
0
    }
918
0
    if (printPolicyFeedback) {
919
        /* This helps to distinguish configurations that don't contain any
920
         * policy config= statement. */
921
0
        PR_SetEnv("NSS_POLICY_LOADED=1");
922
0
        fprintf(stderr, "NSS-POLICY-INFO: LOADED-SUCCESSFULLY\n");
923
0
        secmod_sanityCheckCryptoPolicy();
924
0
    }
925
0
    return rv;
926
0
}
927
928
static PRUint32
929
secmod_parsePolicyCheckFlags(const char *nss)
930
148k
{
931
148k
    PRUint32 policyCheckFlags = 0;
932
933
148k
    if (NSSUTIL_ArgHasFlag("flags", "policyCheckIdentifier", nss)) {
934
0
        policyCheckFlags |= SECMOD_FLAG_POLICY_CHECK_IDENTIFIER;
935
0
    }
936
937
148k
    if (NSSUTIL_ArgHasFlag("flags", "policyCheckValue", nss)) {
938
0
        policyCheckFlags |= SECMOD_FLAG_POLICY_CHECK_VALUE;
939
0
    }
940
941
148k
    return policyCheckFlags;
942
148k
}
943
944
/*
945
 * for 3.4 we continue to use the old SECMODModule structure
946
 */
947
SECMODModule *
948
SECMOD_CreateModuleEx(const char *library, const char *moduleName,
949
                      const char *parameters, const char *nss,
950
                      const char *config)
951
74.3k
{
952
74.3k
    SECMODModule *mod;
953
74.3k
    SECStatus rv;
954
74.3k
    char *slotParams, *ciphers;
955
74.3k
    PRBool printPolicyFeedback = NSSUTIL_ArgHasFlag("flags", "printPolicyFeedback", nss);
956
74.3k
    PRUint32 policyCheckFlags = secmod_parsePolicyCheckFlags(nss);
957
958
74.3k
    rv = secmod_parseCryptoPolicy(config, printPolicyFeedback, policyCheckFlags);
959
960
    /* do not load the module if policy parsing fails */
961
74.3k
    if (rv != SECSuccess) {
962
0
        if (printPolicyFeedback) {
963
0
            PR_SetEnv("NSS_POLICY_FAIL=1");
964
0
            fprintf(stderr, "NSS-POLICY-FAIL: policy config parsing failed, not loading module %s\n", moduleName);
965
0
        }
966
0
        return NULL;
967
0
    }
968
969
74.3k
    mod = secmod_NewModule();
970
74.3k
    if (mod == NULL)
971
0
        return NULL;
972
973
74.3k
    mod->commonName = PORT_ArenaStrdup(mod->arena, moduleName ? moduleName : "");
974
74.3k
    if (library) {
975
0
        mod->dllName = PORT_ArenaStrdup(mod->arena, library);
976
0
    }
977
    /* new field */
978
74.3k
    if (parameters) {
979
74.3k
        mod->libraryParams = PORT_ArenaStrdup(mod->arena, parameters);
980
74.3k
    }
981
982
74.3k
    mod->internal = NSSUTIL_ArgHasFlag("flags", "internal", nss);
983
74.3k
    mod->isFIPS = NSSUTIL_ArgHasFlag("flags", "FIPS", nss);
984
    /* if the system FIPS mode is enabled, force FIPS to be on */
985
74.3k
    if (SECMOD_GetSystemFIPSEnabled()) {
986
0
        mod->isFIPS = PR_TRUE;
987
0
    }
988
74.3k
    mod->isCritical = NSSUTIL_ArgHasFlag("flags", "critical", nss);
989
74.3k
    slotParams = NSSUTIL_ArgGetParamValue("slotParams", nss);
990
74.3k
    mod->slotInfo = NSSUTIL_ArgParseSlotInfo(mod->arena, slotParams,
991
74.3k
                                             &mod->slotInfoCount);
992
74.3k
    if (slotParams)
993
37.1k
        PORT_Free(slotParams);
994
    /* new field */
995
74.3k
    mod->trustOrder = NSSUTIL_ArgReadLong("trustOrder", nss,
996
74.3k
                                          NSSUTIL_DEFAULT_TRUST_ORDER, NULL);
997
    /* new field */
998
74.3k
    mod->cipherOrder = NSSUTIL_ArgReadLong("cipherOrder", nss,
999
74.3k
                                           NSSUTIL_DEFAULT_CIPHER_ORDER, NULL);
1000
    /* new field */
1001
74.3k
    mod->isModuleDB = NSSUTIL_ArgHasFlag("flags", "moduleDB", nss);
1002
74.3k
    mod->moduleDBOnly = NSSUTIL_ArgHasFlag("flags", "moduleDBOnly", nss);
1003
74.3k
    if (mod->moduleDBOnly)
1004
37.1k
        mod->isModuleDB = PR_TRUE;
1005
1006
    /* we need more bits, but we also want to preserve binary compatibility
1007
     * so we overload the isModuleDB PRBool with additional flags.
1008
     * These flags are only valid if mod->isModuleDB is already set.
1009
     * NOTE: this depends on the fact that PRBool is at least a char on
1010
     * all platforms. These flags are only valid if moduleDB is set, so
1011
     * code checking if (mod->isModuleDB) will continue to work correctly. */
1012
74.3k
    if (mod->isModuleDB) {
1013
37.1k
        char flags = SECMOD_FLAG_MODULE_DB_IS_MODULE_DB;
1014
37.1k
        if (NSSUTIL_ArgHasFlag("flags", "skipFirst", nss)) {
1015
0
            flags |= SECMOD_FLAG_MODULE_DB_SKIP_FIRST;
1016
0
        }
1017
37.1k
        if (NSSUTIL_ArgHasFlag("flags", "defaultModDB", nss)) {
1018
37.1k
            flags |= SECMOD_FLAG_MODULE_DB_DEFAULT_MODDB;
1019
37.1k
        }
1020
37.1k
        if (NSSUTIL_ArgHasFlag("flags", "policyOnly", nss)) {
1021
0
            flags |= SECMOD_FLAG_MODULE_DB_POLICY_ONLY;
1022
0
        }
1023
        /* additional moduleDB flags could be added here in the future */
1024
37.1k
        mod->isModuleDB = (PRBool)flags;
1025
37.1k
    }
1026
1027
74.3k
    if (mod->internal) {
1028
74.3k
        char flags = SECMOD_FLAG_INTERNAL_IS_INTERNAL;
1029
1030
74.3k
        if (NSSUTIL_ArgHasFlag("flags", "internalKeySlot", nss)) {
1031
37.1k
            flags |= SECMOD_FLAG_INTERNAL_KEY_SLOT;
1032
37.1k
        }
1033
74.3k
        mod->internal = (PRBool)flags;
1034
74.3k
    }
1035
1036
74.3k
    ciphers = NSSUTIL_ArgGetParamValue("ciphers", nss);
1037
74.3k
    NSSUTIL_ArgParseCipherFlags(&mod->ssl[0], ciphers);
1038
74.3k
    if (ciphers)
1039
0
        PORT_Free(ciphers);
1040
1041
74.3k
    secmod_PrivateModuleCount++;
1042
1043
74.3k
    return mod;
1044
74.3k
}
1045
1046
PRBool
1047
SECMOD_GetSkipFirstFlag(SECMODModule *mod)
1048
37.1k
{
1049
37.1k
    char flags = (char)mod->isModuleDB;
1050
1051
37.1k
    return (flags & SECMOD_FLAG_MODULE_DB_SKIP_FIRST) ? PR_TRUE : PR_FALSE;
1052
37.1k
}
1053
1054
PRBool
1055
SECMOD_GetDefaultModDBFlag(SECMODModule *mod)
1056
0
{
1057
0
    char flags = (char)mod->isModuleDB;
1058
1059
0
    return (flags & SECMOD_FLAG_MODULE_DB_DEFAULT_MODDB) ? PR_TRUE : PR_FALSE;
1060
0
}
1061
1062
PRBool
1063
secmod_PolicyOnly(SECMODModule *mod)
1064
74.3k
{
1065
74.3k
    char flags = (char)mod->isModuleDB;
1066
1067
74.3k
    return (flags & SECMOD_FLAG_MODULE_DB_POLICY_ONLY) ? PR_TRUE : PR_FALSE;
1068
74.3k
}
1069
1070
PRBool
1071
secmod_IsInternalKeySlot(SECMODModule *mod)
1072
111k
{
1073
111k
    char flags = (char)mod->internal;
1074
1075
111k
    return (flags & SECMOD_FLAG_INTERNAL_KEY_SLOT) ? PR_TRUE : PR_FALSE;
1076
111k
}
1077
1078
void
1079
secmod_SetInternalKeySlotFlag(SECMODModule *mod, PRBool val)
1080
0
{
1081
0
    char flags = (char)mod->internal;
1082
1083
0
    if (val) {
1084
0
        flags |= SECMOD_FLAG_INTERNAL_KEY_SLOT;
1085
0
    } else {
1086
0
        flags &= ~SECMOD_FLAG_INTERNAL_KEY_SLOT;
1087
0
    }
1088
0
    mod->internal = flags;
1089
0
}
1090
1091
/*
1092
 * copy desc and value into target. Target is known to be big enough to
1093
 * hold desc +2 +value, which is good because the result of this will be
1094
 * *desc"*value". We may, however, have to add some escapes for special
1095
 * characters imbedded into value (rare). This string potentially comes from
1096
 * a user, so we don't want the user overflowing the target buffer by using
1097
 * excessive escapes. To prevent this we count the escapes we need to add and
1098
 * try to expand the buffer with Realloc.
1099
 */
1100
static char *
1101
secmod_doDescCopy(char *target, char **base, int *baseLen,
1102
                  const char *desc, int descLen, char *value)
1103
0
{
1104
0
    int diff, esc_len;
1105
1106
0
    esc_len = NSSUTIL_EscapeSize(value, '\"') - 1;
1107
0
    diff = esc_len - strlen(value);
1108
0
    if (diff > 0) {
1109
        /* we need to escape... expand newSpecPtr as well to make sure
1110
         * we don't overflow it */
1111
0
        int offset = target - *base;
1112
0
        char *newPtr = PORT_Realloc(*base, *baseLen + diff);
1113
0
        if (!newPtr) {
1114
0
            return target; /* not enough space, just drop the whole copy */
1115
0
        }
1116
0
        *baseLen += diff;
1117
0
        target = newPtr + offset;
1118
0
        *base = newPtr;
1119
0
        value = NSSUTIL_Escape(value, '\"');
1120
0
        if (value == NULL) {
1121
0
            return target; /* couldn't escape value, just drop the copy */
1122
0
        }
1123
0
    }
1124
0
    PORT_Memcpy(target, desc, descLen);
1125
0
    target += descLen;
1126
0
    *target++ = '\"';
1127
0
    PORT_Memcpy(target, value, esc_len);
1128
0
    target += esc_len;
1129
0
    *target++ = '\"';
1130
0
    if (diff > 0) {
1131
0
        PORT_Free(value);
1132
0
    }
1133
0
    return target;
1134
0
}
1135
1136
#define SECMOD_SPEC_COPY(new, start, end) \
1137
0
    if (end > start) {                    \
1138
0
        int _cnt = end - start;           \
1139
0
        PORT_Memcpy(new, start, _cnt);    \
1140
0
        new += _cnt;                      \
1141
0
    }
1142
#define SECMOD_TOKEN_DESCRIPTION "tokenDescription="
1143
#define SECMOD_SLOT_DESCRIPTION "slotDescription="
1144
1145
/*
1146
 * Find any tokens= values in the module spec.
1147
 * Always return a new spec which does not have any tokens= arguments.
1148
 * If tokens= arguments are found, Split the the various tokens defined into
1149
 * an array of child specs to return.
1150
 *
1151
 * Caller is responsible for freeing the child spec and the new token
1152
 * spec.
1153
 */
1154
char *
1155
secmod_ParseModuleSpecForTokens(PRBool convert, PRBool isFIPS,
1156
                                const char *moduleSpec, char ***children,
1157
                                CK_SLOT_ID **ids)
1158
0
{
1159
0
    int newSpecLen = PORT_Strlen(moduleSpec) + 2;
1160
0
    char *newSpec = PORT_Alloc(newSpecLen);
1161
0
    char *newSpecPtr = newSpec;
1162
0
    const char *modulePrev = moduleSpec;
1163
0
    char *target = NULL;
1164
0
    char *tmp = NULL;
1165
0
    char **childArray = NULL;
1166
0
    const char *tokenIndex;
1167
0
    CK_SLOT_ID *idArray = NULL;
1168
0
    int tokenCount = 0;
1169
0
    int i;
1170
1171
0
    if (newSpec == NULL) {
1172
0
        return NULL;
1173
0
    }
1174
1175
0
    *children = NULL;
1176
0
    if (ids) {
1177
0
        *ids = NULL;
1178
0
    }
1179
0
    moduleSpec = NSSUTIL_ArgStrip(moduleSpec);
1180
0
    SECMOD_SPEC_COPY(newSpecPtr, modulePrev, moduleSpec);
1181
1182
    /* Notes on 'convert' and 'isFIPS' flags: The base parameters for opening
1183
     * a new softoken module takes the following parameters to name the
1184
     * various tokens:
1185
     *
1186
     *  cryptoTokenDescription: name of the non-fips crypto token.
1187
     *  cryptoSlotDescription: name of the non-fips crypto slot.
1188
     *  dbTokenDescription: name of the non-fips db token.
1189
     *  dbSlotDescription: name of the non-fips db slot.
1190
     *  FIPSTokenDescription: name of the fips db/crypto token.
1191
     *  FIPSSlotDescription: name of the fips db/crypto slot.
1192
     *
1193
     * if we are opening a new slot, we need to have the following
1194
     * parameters:
1195
     *  tokenDescription: name of the token.
1196
     *  slotDescription: name of the slot.
1197
     *
1198
     *
1199
     * The convert flag tells us to drop the unnecessary *TokenDescription
1200
     * and *SlotDescription arguments and convert the appropriate pair
1201
     * (either db or FIPS based on the isFIPS flag) to tokenDescription and
1202
     * slotDescription).
1203
     */
1204
    /*
1205
     * walk down the list. if we find a tokens= argument, save it,
1206
     * otherise copy the argument.
1207
     */
1208
0
    while (*moduleSpec) {
1209
0
        int next;
1210
0
        modulePrev = moduleSpec;
1211
0
        NSSUTIL_HANDLE_STRING_ARG(moduleSpec, target, "tokens=",
1212
0
                                  modulePrev = moduleSpec;
1213
0
                                  /* skip copying */)
1214
0
        NSSUTIL_HANDLE_STRING_ARG(
1215
0
            moduleSpec, tmp, "cryptoTokenDescription=",
1216
0
            if (convert) { modulePrev = moduleSpec; })
1217
0
        NSSUTIL_HANDLE_STRING_ARG(
1218
0
            moduleSpec, tmp, "cryptoSlotDescription=",
1219
0
            if (convert) { modulePrev = moduleSpec; })
1220
0
        NSSUTIL_HANDLE_STRING_ARG(
1221
0
            moduleSpec, tmp, "dbTokenDescription=",
1222
0
            if (convert) {
1223
0
                modulePrev = moduleSpec;
1224
0
                if (!isFIPS) {
1225
0
                    newSpecPtr = secmod_doDescCopy(newSpecPtr,
1226
0
                                                   &newSpec, &newSpecLen,
1227
0
                                                   SECMOD_TOKEN_DESCRIPTION,
1228
0
                                                   sizeof(SECMOD_TOKEN_DESCRIPTION) - 1,
1229
0
                                                   tmp);
1230
0
                }
1231
0
            })
1232
0
        NSSUTIL_HANDLE_STRING_ARG(
1233
0
            moduleSpec, tmp, "dbSlotDescription=",
1234
0
            if (convert) {
1235
0
                modulePrev = moduleSpec; /* skip copying */
1236
0
                if (!isFIPS) {
1237
0
                    newSpecPtr = secmod_doDescCopy(newSpecPtr,
1238
0
                                                   &newSpec, &newSpecLen,
1239
0
                                                   SECMOD_SLOT_DESCRIPTION,
1240
0
                                                   sizeof(SECMOD_SLOT_DESCRIPTION) - 1,
1241
0
                                                   tmp);
1242
0
                }
1243
0
            })
1244
0
        NSSUTIL_HANDLE_STRING_ARG(
1245
0
            moduleSpec, tmp, "FIPSTokenDescription=",
1246
0
            if (convert) {
1247
0
                modulePrev = moduleSpec; /* skip copying */
1248
0
                if (isFIPS) {
1249
0
                    newSpecPtr = secmod_doDescCopy(newSpecPtr,
1250
0
                                                   &newSpec, &newSpecLen,
1251
0
                                                   SECMOD_TOKEN_DESCRIPTION,
1252
0
                                                   sizeof(SECMOD_TOKEN_DESCRIPTION) - 1,
1253
0
                                                   tmp);
1254
0
                }
1255
0
            })
1256
0
        NSSUTIL_HANDLE_STRING_ARG(
1257
0
            moduleSpec, tmp, "FIPSSlotDescription=",
1258
0
            if (convert) {
1259
0
                modulePrev = moduleSpec; /* skip copying */
1260
0
                if (isFIPS) {
1261
0
                    newSpecPtr = secmod_doDescCopy(newSpecPtr,
1262
0
                                                   &newSpec, &newSpecLen,
1263
0
                                                   SECMOD_SLOT_DESCRIPTION,
1264
0
                                                   sizeof(SECMOD_SLOT_DESCRIPTION) - 1,
1265
0
                                                   tmp);
1266
0
                }
1267
0
            })
1268
0
        NSSUTIL_HANDLE_FINAL_ARG(moduleSpec)
1269
0
        SECMOD_SPEC_COPY(newSpecPtr, modulePrev, moduleSpec);
1270
0
    }
1271
0
    if (tmp) {
1272
0
        PORT_Free(tmp);
1273
0
        tmp = NULL;
1274
0
    }
1275
0
    *newSpecPtr = 0;
1276
1277
    /* no target found, return the newSpec */
1278
0
    if (target == NULL) {
1279
0
        return newSpec;
1280
0
    }
1281
1282
    /* now build the child array from target */
1283
    /*first count them */
1284
0
    for (tokenIndex = NSSUTIL_ArgStrip(target); *tokenIndex;
1285
0
         tokenIndex = NSSUTIL_ArgStrip(NSSUTIL_ArgSkipParameter(tokenIndex))) {
1286
0
        tokenCount++;
1287
0
    }
1288
1289
0
    childArray = PORT_NewArray(char *, tokenCount + 1);
1290
0
    if (childArray == NULL) {
1291
        /* just return the spec as is then */
1292
0
        PORT_Free(target);
1293
0
        return newSpec;
1294
0
    }
1295
0
    if (ids) {
1296
0
        idArray = PORT_NewArray(CK_SLOT_ID, tokenCount + 1);
1297
0
        if (idArray == NULL) {
1298
0
            PORT_Free(childArray);
1299
0
            PORT_Free(target);
1300
0
            return newSpec;
1301
0
        }
1302
0
    }
1303
1304
    /* now fill them in */
1305
0
    for (tokenIndex = NSSUTIL_ArgStrip(target), i = 0;
1306
0
         *tokenIndex && (i < tokenCount);
1307
0
         tokenIndex = NSSUTIL_ArgStrip(tokenIndex)) {
1308
0
        int next;
1309
0
        char *name = NSSUTIL_ArgGetLabel(tokenIndex, &next);
1310
0
        tokenIndex += next;
1311
1312
0
        if (idArray) {
1313
0
            idArray[i] = NSSUTIL_ArgDecodeNumber(name);
1314
0
        }
1315
1316
0
        PORT_Free(name); /* drop the explicit number */
1317
1318
        /* if anything is left, copy the args to the child array */
1319
0
        if (!NSSUTIL_ArgIsBlank(*tokenIndex)) {
1320
0
            childArray[i++] = NSSUTIL_ArgFetchValue(tokenIndex, &next);
1321
0
            tokenIndex += next;
1322
0
        }
1323
0
    }
1324
1325
0
    PORT_Free(target);
1326
0
    childArray[i] = 0;
1327
0
    if (idArray) {
1328
0
        idArray[i] = 0;
1329
0
    }
1330
1331
    /* return it */
1332
0
    *children = childArray;
1333
0
    if (ids) {
1334
0
        *ids = idArray;
1335
0
    }
1336
0
    return newSpec;
1337
0
}
1338
1339
/* get the database and flags from the spec */
1340
static char *
1341
secmod_getConfigDir(const char *spec, char **certPrefix, char **keyPrefix,
1342
                    PRBool *readOnly)
1343
0
{
1344
0
    char *config = NULL;
1345
1346
0
    *certPrefix = NULL;
1347
0
    *keyPrefix = NULL;
1348
0
    *readOnly = NSSUTIL_ArgHasFlag("flags", "readOnly", spec);
1349
0
    if (NSSUTIL_ArgHasFlag("flags", "nocertdb", spec) ||
1350
0
        NSSUTIL_ArgHasFlag("flags", "nokeydb", spec)) {
1351
0
        return NULL;
1352
0
    }
1353
1354
0
    spec = NSSUTIL_ArgStrip(spec);
1355
0
    while (*spec) {
1356
0
        int next;
1357
0
        NSSUTIL_HANDLE_STRING_ARG(spec, config, "configdir=", ;)
1358
0
        NSSUTIL_HANDLE_STRING_ARG(spec, *certPrefix, "certPrefix=", ;)
1359
0
        NSSUTIL_HANDLE_STRING_ARG(spec, *keyPrefix, "keyPrefix=", ;)
1360
0
        NSSUTIL_HANDLE_FINAL_ARG(spec)
1361
0
    }
1362
0
    return config;
1363
0
}
1364
1365
struct SECMODConfigListStr {
1366
    char *config;
1367
    char *certPrefix;
1368
    char *keyPrefix;
1369
    PRBool isReadOnly;
1370
};
1371
1372
/*
1373
 * return an array of already openned databases from a spec list.
1374
 */
1375
SECMODConfigList *
1376
secmod_GetConfigList(PRBool isFIPS, char *spec, int *count)
1377
0
{
1378
0
    char **children;
1379
0
    CK_SLOT_ID *ids;
1380
0
    char *strippedSpec;
1381
0
    int childCount;
1382
0
    SECMODConfigList *conflist = NULL;
1383
0
    int i;
1384
1385
0
    strippedSpec = secmod_ParseModuleSpecForTokens(PR_TRUE, isFIPS,
1386
0
                                                   spec, &children, &ids);
1387
0
    if (strippedSpec == NULL) {
1388
0
        return NULL;
1389
0
    }
1390
1391
0
    for (childCount = 0; children && children[childCount]; childCount++)
1392
0
        ;
1393
0
    *count = childCount + 1; /* include strippedSpec */
1394
0
    conflist = PORT_NewArray(SECMODConfigList, *count);
1395
0
    if (conflist == NULL) {
1396
0
        *count = 0;
1397
0
        goto loser;
1398
0
    }
1399
1400
0
    conflist[0].config = secmod_getConfigDir(strippedSpec,
1401
0
                                             &conflist[0].certPrefix,
1402
0
                                             &conflist[0].keyPrefix,
1403
0
                                             &conflist[0].isReadOnly);
1404
0
    for (i = 0; i < childCount; i++) {
1405
0
        conflist[i + 1].config = secmod_getConfigDir(children[i],
1406
0
                                                     &conflist[i + 1].certPrefix,
1407
0
                                                     &conflist[i + 1].keyPrefix,
1408
0
                                                     &conflist[i + 1].isReadOnly);
1409
0
    }
1410
1411
0
loser:
1412
0
    secmod_FreeChildren(children, ids);
1413
0
    PORT_Free(strippedSpec);
1414
0
    return conflist;
1415
0
}
1416
1417
/*
1418
 * determine if we are trying to open an old dbm database. For this test
1419
 * RDB databases should return PR_FALSE.
1420
 */
1421
static PRBool
1422
secmod_configIsDBM(char *configDir)
1423
0
{
1424
0
    char *env;
1425
1426
    /* explicit dbm open */
1427
0
    if (strncmp(configDir, "dbm:", 4) == 0) {
1428
0
        return PR_TRUE;
1429
0
    }
1430
    /* explicit open of a non-dbm database */
1431
0
    if ((strncmp(configDir, "sql:", 4) == 0) ||
1432
0
        (strncmp(configDir, "rdb:", 4) == 0) ||
1433
0
        (strncmp(configDir, "extern:", 7) == 0)) {
1434
0
        return PR_FALSE;
1435
0
    }
1436
0
    env = PR_GetEnvSecure("NSS_DEFAULT_DB_TYPE");
1437
    /* implicit dbm open */
1438
0
    if ((env == NULL) || (strcmp(env, "dbm") == 0)) {
1439
0
        return PR_TRUE;
1440
0
    }
1441
    /* implicit non-dbm open */
1442
0
    return PR_FALSE;
1443
0
}
1444
1445
/*
1446
 * match two prefixes. prefix may be NULL. NULL patches '\0'
1447
 */
1448
static PRBool
1449
secmod_matchPrefix(char *prefix1, char *prefix2)
1450
0
{
1451
0
    if ((prefix1 == NULL) || (*prefix1 == 0)) {
1452
0
        if ((prefix2 == NULL) || (*prefix2 == 0)) {
1453
0
            return PR_TRUE;
1454
0
        }
1455
0
        return PR_FALSE;
1456
0
    }
1457
0
    if (strcmp(prefix1, prefix2) == 0) {
1458
0
        return PR_TRUE;
1459
0
    }
1460
0
    return PR_FALSE;
1461
0
}
1462
1463
/* do two config paramters match? Not all callers are compariing
1464
 * SECMODConfigLists directly, so this function breaks them out to their
1465
 * components. */
1466
static PRBool
1467
secmod_matchConfig(char *configDir1, char *configDir2,
1468
                   char *certPrefix1, char *certPrefix2,
1469
                   char *keyPrefix1, char *keyPrefix2,
1470
                   PRBool isReadOnly1, PRBool isReadOnly2)
1471
0
{
1472
    /* TODO: Document the answer to the question:
1473
     *       "Why not allow them to match if they are both NULL?"
1474
     * See: https://bugzilla.mozilla.org/show_bug.cgi?id=1318633#c1
1475
     */
1476
0
    if ((configDir1 == NULL) || (configDir2 == NULL)) {
1477
0
        return PR_FALSE;
1478
0
    }
1479
0
    if (strcmp(configDir1, configDir2) != 0) {
1480
0
        return PR_FALSE;
1481
0
    }
1482
0
    if (!secmod_matchPrefix(certPrefix1, certPrefix2)) {
1483
0
        return PR_FALSE;
1484
0
    }
1485
0
    if (!secmod_matchPrefix(keyPrefix1, keyPrefix2)) {
1486
0
        return PR_FALSE;
1487
0
    }
1488
    /* these last test -- if we just need the DB open read only,
1489
     * than any open will suffice, but if we requested it read/write
1490
     * and it's only open read only, we need to open it again */
1491
0
    if (isReadOnly1) {
1492
0
        return PR_TRUE;
1493
0
    }
1494
0
    if (isReadOnly2) { /* isReadonly1 == PR_FALSE */
1495
0
        return PR_FALSE;
1496
0
    }
1497
0
    return PR_TRUE;
1498
0
}
1499
1500
/*
1501
 * return true if we are requesting a database that is already openned.
1502
 */
1503
PRBool
1504
secmod_MatchConfigList(const char *spec, SECMODConfigList *conflist, int count)
1505
0
{
1506
0
    char *config;
1507
0
    char *certPrefix;
1508
0
    char *keyPrefix;
1509
0
    PRBool isReadOnly;
1510
0
    PRBool ret = PR_FALSE;
1511
0
    int i;
1512
1513
0
    config = secmod_getConfigDir(spec, &certPrefix, &keyPrefix, &isReadOnly);
1514
0
    if (!config) {
1515
0
        goto done;
1516
0
    }
1517
1518
    /* NOTE: we dbm isn't multiple open safe. If we open the same database
1519
     * twice from two different locations, then we can corrupt our database
1520
     * (the cache will be inconsistent). Protect against this by claiming
1521
     * for comparison only that we are always openning dbm databases read only.
1522
     */
1523
0
    if (secmod_configIsDBM(config)) {
1524
0
        isReadOnly = 1;
1525
0
    }
1526
0
    for (i = 0; i < count; i++) {
1527
0
        if (secmod_matchConfig(config, conflist[i].config, certPrefix,
1528
0
                               conflist[i].certPrefix, keyPrefix,
1529
0
                               conflist[i].keyPrefix, isReadOnly,
1530
0
                               conflist[i].isReadOnly)) {
1531
0
            ret = PR_TRUE;
1532
0
            goto done;
1533
0
        }
1534
0
    }
1535
1536
0
    ret = PR_FALSE;
1537
0
done:
1538
0
    PORT_Free(config);
1539
0
    PORT_Free(certPrefix);
1540
0
    PORT_Free(keyPrefix);
1541
0
    return ret;
1542
0
}
1543
1544
/*
1545
 * Find the slot id from the module spec. If the slot is the database slot, we
1546
 * can get the slot id from the default database slot.
1547
 */
1548
CK_SLOT_ID
1549
secmod_GetSlotIDFromModuleSpec(const char *moduleSpec, SECMODModule *module)
1550
0
{
1551
0
    char *tmp_spec = NULL;
1552
0
    char **children, **thisChild;
1553
0
    CK_SLOT_ID *ids, *thisID, slotID = -1;
1554
0
    char *inConfig = NULL, *thisConfig = NULL;
1555
0
    char *inCertPrefix = NULL, *thisCertPrefix = NULL;
1556
0
    char *inKeyPrefix = NULL, *thisKeyPrefix = NULL;
1557
0
    PRBool inReadOnly, thisReadOnly;
1558
1559
0
    inConfig = secmod_getConfigDir(moduleSpec, &inCertPrefix, &inKeyPrefix,
1560
0
                                   &inReadOnly);
1561
0
    if (!inConfig) {
1562
0
        goto done;
1563
0
    }
1564
1565
0
    if (secmod_configIsDBM(inConfig)) {
1566
0
        inReadOnly = 1;
1567
0
    }
1568
1569
0
    tmp_spec = secmod_ParseModuleSpecForTokens(PR_TRUE, module->isFIPS,
1570
0
                                               module->libraryParams, &children, &ids);
1571
0
    if (tmp_spec == NULL) {
1572
0
        goto done;
1573
0
    }
1574
1575
    /* first check to see if the parent is the database */
1576
0
    thisConfig = secmod_getConfigDir(tmp_spec, &thisCertPrefix, &thisKeyPrefix,
1577
0
                                     &thisReadOnly);
1578
0
    if (!thisConfig) {
1579
0
        goto done;
1580
0
    }
1581
0
    if (secmod_matchConfig(inConfig, thisConfig, inCertPrefix, thisCertPrefix,
1582
0
                           inKeyPrefix, thisKeyPrefix, inReadOnly, thisReadOnly)) {
1583
        /* yup it's the default key slot, get the id for it */
1584
0
        PK11SlotInfo *slot = PK11_GetInternalKeySlot();
1585
0
        if (slot) {
1586
0
            slotID = slot->slotID;
1587
0
            PK11_FreeSlot(slot);
1588
0
        }
1589
0
        goto done;
1590
0
    }
1591
1592
    /* find id of the token */
1593
0
    for (thisChild = children, thisID = ids; thisChild && *thisChild; thisChild++, thisID++) {
1594
0
        PORT_Free(thisConfig);
1595
0
        PORT_Free(thisCertPrefix);
1596
0
        PORT_Free(thisKeyPrefix);
1597
0
        thisConfig = secmod_getConfigDir(*thisChild, &thisCertPrefix,
1598
0
                                         &thisKeyPrefix, &thisReadOnly);
1599
0
        if (thisConfig == NULL) {
1600
0
            continue;
1601
0
        }
1602
0
        if (secmod_matchConfig(inConfig, thisConfig, inCertPrefix, thisCertPrefix,
1603
0
                               inKeyPrefix, thisKeyPrefix, inReadOnly, thisReadOnly)) {
1604
0
            slotID = *thisID;
1605
0
            break;
1606
0
        }
1607
0
    }
1608
1609
0
done:
1610
0
    PORT_Free(inConfig);
1611
0
    PORT_Free(inCertPrefix);
1612
0
    PORT_Free(inKeyPrefix);
1613
0
    PORT_Free(thisConfig);
1614
0
    PORT_Free(thisCertPrefix);
1615
0
    PORT_Free(thisKeyPrefix);
1616
0
    if (tmp_spec) {
1617
0
        secmod_FreeChildren(children, ids);
1618
0
        PORT_Free(tmp_spec);
1619
0
    }
1620
0
    return slotID;
1621
0
}
1622
1623
void
1624
secmod_FreeConfigList(SECMODConfigList *conflist, int count)
1625
0
{
1626
0
    int i;
1627
0
    for (i = 0; i < count; i++) {
1628
0
        PORT_Free(conflist[i].config);
1629
0
        PORT_Free(conflist[i].certPrefix);
1630
0
        PORT_Free(conflist[i].keyPrefix);
1631
0
    }
1632
0
    PORT_Free(conflist);
1633
0
}
1634
1635
void
1636
secmod_FreeChildren(char **children, CK_SLOT_ID *ids)
1637
0
{
1638
0
    char **thisChild;
1639
1640
0
    if (!children) {
1641
0
        return;
1642
0
    }
1643
1644
0
    for (thisChild = children; thisChild && *thisChild; thisChild++) {
1645
0
        PORT_Free(*thisChild);
1646
0
    }
1647
0
    PORT_Free(children);
1648
0
    if (ids) {
1649
0
        PORT_Free(ids);
1650
0
    }
1651
0
    return;
1652
0
}
1653
1654
/*
1655
 * caclulate the length of each child record:
1656
 * " 0x{id}=<{escaped_child}>"
1657
 */
1658
static int
1659
secmod_getChildLength(char *child, CK_SLOT_ID id)
1660
0
{
1661
0
    int length = NSSUTIL_DoubleEscapeSize(child, '>', ']');
1662
0
    if (id == 0) {
1663
0
        length++;
1664
0
    }
1665
0
    while (id) {
1666
0
        length++;
1667
0
        id = id >> 4;
1668
0
    }
1669
0
    length += 6; /* {sp}0x[id]=<{child}> */
1670
0
    return length;
1671
0
}
1672
1673
/*
1674
 * Build a child record:
1675
 * " 0x{id}=<{escaped_child}>"
1676
 */
1677
static SECStatus
1678
secmod_mkTokenChild(char **next, int *length, char *child, CK_SLOT_ID id)
1679
0
{
1680
0
    int len;
1681
0
    char *escSpec;
1682
1683
0
    len = PR_snprintf(*next, *length, " 0x%x=<", id);
1684
0
    if (len < 0) {
1685
0
        return SECFailure;
1686
0
    }
1687
0
    *next += len;
1688
0
    *length -= len;
1689
0
    escSpec = NSSUTIL_DoubleEscape(child, '>', ']');
1690
0
    if (escSpec == NULL) {
1691
0
        return SECFailure;
1692
0
    }
1693
0
    if (*child && (*escSpec == 0)) {
1694
0
        PORT_Free(escSpec);
1695
0
        return SECFailure;
1696
0
    }
1697
0
    len = strlen(escSpec);
1698
0
    if (len + 1 > *length) {
1699
0
        PORT_Free(escSpec);
1700
0
        return SECFailure;
1701
0
    }
1702
0
    PORT_Memcpy(*next, escSpec, len);
1703
0
    *next += len;
1704
0
    *length -= len;
1705
0
    PORT_Free(escSpec);
1706
0
    **next = '>';
1707
0
    (*next)++;
1708
0
    (*length)--;
1709
0
    return SECSuccess;
1710
0
}
1711
1712
0
#define TOKEN_STRING " tokens=["
1713
1714
char *
1715
secmod_MkAppendTokensList(PLArenaPool *arena, char *oldParam, char *newToken,
1716
                          CK_SLOT_ID newID, char **children, CK_SLOT_ID *ids)
1717
0
{
1718
0
    char *rawParam = NULL;  /* oldParam with tokens stripped off */
1719
0
    char *newParam = NULL;  /* space for the return parameter */
1720
0
    char *nextParam = NULL; /* current end of the new parameter */
1721
0
    char **oldChildren = NULL;
1722
0
    CK_SLOT_ID *oldIds = NULL;
1723
0
    void *mark = NULL; /* mark the arena pool in case we need
1724
                        * to release it */
1725
0
    int length, i, tmpLen;
1726
0
    SECStatus rv;
1727
1728
    /* first strip out and save the old tokenlist */
1729
0
    rawParam = secmod_ParseModuleSpecForTokens(PR_FALSE, PR_FALSE,
1730
0
                                               oldParam, &oldChildren, &oldIds);
1731
0
    if (!rawParam) {
1732
0
        goto loser;
1733
0
    }
1734
1735
    /* now calculate the total length of the new buffer */
1736
    /* First the 'fixed stuff', length of rawparam (does not include a NULL),
1737
     * length of the token string (does include the NULL), closing bracket */
1738
0
    length = strlen(rawParam) + sizeof(TOKEN_STRING) + 1;
1739
    /* now add then length of all the old children */
1740
0
    for (i = 0; oldChildren && oldChildren[i]; i++) {
1741
0
        length += secmod_getChildLength(oldChildren[i], oldIds[i]);
1742
0
    }
1743
1744
    /* add the new token */
1745
0
    length += secmod_getChildLength(newToken, newID);
1746
1747
    /* and it's new children */
1748
0
    for (i = 0; children && children[i]; i++) {
1749
0
        if (ids[i] == -1) {
1750
0
            continue;
1751
0
        }
1752
0
        length += secmod_getChildLength(children[i], ids[i]);
1753
0
    }
1754
1755
    /* now allocate and build the string */
1756
0
    mark = PORT_ArenaMark(arena);
1757
0
    if (!mark) {
1758
0
        goto loser;
1759
0
    }
1760
0
    newParam = PORT_ArenaAlloc(arena, length);
1761
0
    if (!newParam) {
1762
0
        goto loser;
1763
0
    }
1764
1765
0
    PORT_Strcpy(newParam, oldParam);
1766
0
    tmpLen = strlen(oldParam);
1767
0
    nextParam = newParam + tmpLen;
1768
0
    length -= tmpLen;
1769
0
    PORT_Memcpy(nextParam, TOKEN_STRING, sizeof(TOKEN_STRING) - 1);
1770
0
    nextParam += sizeof(TOKEN_STRING) - 1;
1771
0
    length -= sizeof(TOKEN_STRING) - 1;
1772
1773
0
    for (i = 0; oldChildren && oldChildren[i]; i++) {
1774
0
        rv = secmod_mkTokenChild(&nextParam, &length, oldChildren[i], oldIds[i]);
1775
0
        if (rv != SECSuccess) {
1776
0
            goto loser;
1777
0
        }
1778
0
    }
1779
1780
0
    rv = secmod_mkTokenChild(&nextParam, &length, newToken, newID);
1781
0
    if (rv != SECSuccess) {
1782
0
        goto loser;
1783
0
    }
1784
1785
0
    for (i = 0; children && children[i]; i++) {
1786
0
        if (ids[i] == -1) {
1787
0
            continue;
1788
0
        }
1789
0
        rv = secmod_mkTokenChild(&nextParam, &length, children[i], ids[i]);
1790
0
        if (rv != SECSuccess) {
1791
0
            goto loser;
1792
0
        }
1793
0
    }
1794
1795
0
    if (length < 2) {
1796
0
        goto loser;
1797
0
    }
1798
1799
0
    *nextParam++ = ']';
1800
0
    *nextParam++ = 0;
1801
1802
    /* we are going to return newParam now, don't release the mark */
1803
0
    PORT_ArenaUnmark(arena, mark);
1804
0
    mark = NULL;
1805
1806
0
loser:
1807
0
    if (mark) {
1808
0
        PORT_ArenaRelease(arena, mark);
1809
0
        newParam = NULL; /* if the mark is still active,
1810
                          * don't return the param */
1811
0
    }
1812
0
    if (rawParam) {
1813
0
        PORT_Free(rawParam);
1814
0
    }
1815
0
    if (oldChildren) {
1816
0
        secmod_FreeChildren(oldChildren, oldIds);
1817
0
    }
1818
0
    return newParam;
1819
0
}
1820
1821
static char *
1822
secmod_mkModuleSpec(SECMODModule *module)
1823
0
{
1824
0
    char *nss = NULL, *modSpec = NULL, **slotStrings = NULL;
1825
0
    int slotCount, i, si;
1826
0
    SECMODListLock *moduleLock = SECMOD_GetDefaultModuleListLock();
1827
1828
    /* allocate target slot info strings */
1829
0
    slotCount = 0;
1830
1831
0
    SECMOD_GetReadLock(moduleLock);
1832
0
    if (module->slotCount) {
1833
0
        for (i = 0; i < module->slotCount; i++) {
1834
0
            if (module->slots[i]->defaultFlags != 0) {
1835
0
                slotCount++;
1836
0
            }
1837
0
        }
1838
0
    } else {
1839
0
        slotCount = module->slotInfoCount;
1840
0
    }
1841
1842
0
    slotStrings = (char **)PORT_ZAlloc(slotCount * sizeof(char *));
1843
0
    if (slotStrings == NULL) {
1844
0
        SECMOD_ReleaseReadLock(moduleLock);
1845
0
        goto loser;
1846
0
    }
1847
1848
    /* build the slot info strings */
1849
0
    if (module->slotCount) {
1850
0
        for (i = 0, si = 0; i < module->slotCount; i++) {
1851
0
            if (module->slots[i]->defaultFlags) {
1852
0
                PORT_Assert(si < slotCount);
1853
0
                if (si >= slotCount)
1854
0
                    break;
1855
0
                slotStrings[si] = NSSUTIL_MkSlotString(module->slots[i]->slotID,
1856
0
                                                       module->slots[i]->defaultFlags,
1857
0
                                                       module->slots[i]->timeout,
1858
0
                                                       module->slots[i]->askpw,
1859
0
                                                       module->slots[i]->hasRootCerts,
1860
0
                                                       module->slots[i]->hasRootTrust);
1861
0
                si++;
1862
0
            }
1863
0
        }
1864
0
    } else {
1865
0
        for (i = 0; i < slotCount; i++) {
1866
0
            slotStrings[i] = NSSUTIL_MkSlotString(
1867
0
                module->slotInfo[i].slotID,
1868
0
                module->slotInfo[i].defaultFlags,
1869
0
                module->slotInfo[i].timeout,
1870
0
                module->slotInfo[i].askpw,
1871
0
                module->slotInfo[i].hasRootCerts,
1872
0
                module->slotInfo[i].hasRootTrust);
1873
0
        }
1874
0
    }
1875
1876
0
    SECMOD_ReleaseReadLock(moduleLock);
1877
0
    nss = NSSUTIL_MkNSSString(slotStrings, slotCount, module->internal,
1878
0
                              module->isFIPS, module->isModuleDB,
1879
0
                              module->moduleDBOnly, module->isCritical,
1880
0
                              module->trustOrder, module->cipherOrder,
1881
0
                              module->ssl[0], module->ssl[1]);
1882
0
    modSpec = NSSUTIL_MkModuleSpec(module->dllName, module->commonName,
1883
0
                                   module->libraryParams, nss);
1884
0
    PORT_Free(slotStrings);
1885
0
    PR_smprintf_free(nss);
1886
0
loser:
1887
0
    return (modSpec);
1888
0
}
1889
1890
char **
1891
SECMOD_GetModuleSpecList(SECMODModule *module)
1892
37.1k
{
1893
37.1k
    SECMODModuleDBFunc func = (SECMODModuleDBFunc)module->moduleDBFunc;
1894
37.1k
    if (func) {
1895
37.1k
        return (*func)(SECMOD_MODULE_DB_FUNCTION_FIND,
1896
37.1k
                       module->libraryParams, NULL);
1897
37.1k
    }
1898
0
    return NULL;
1899
37.1k
}
1900
1901
SECStatus
1902
SECMOD_AddPermDB(SECMODModule *module)
1903
0
{
1904
0
    SECMODModuleDBFunc func;
1905
0
    char *moduleSpec;
1906
0
    char **retString;
1907
1908
0
    if (module->parent == NULL)
1909
0
        return SECFailure;
1910
1911
0
    func = (SECMODModuleDBFunc)module->parent->moduleDBFunc;
1912
0
    if (func) {
1913
0
        moduleSpec = secmod_mkModuleSpec(module);
1914
0
        retString = (*func)(SECMOD_MODULE_DB_FUNCTION_ADD,
1915
0
                            module->parent->libraryParams, moduleSpec);
1916
0
        PORT_Free(moduleSpec);
1917
0
        if (retString != NULL)
1918
0
            return SECSuccess;
1919
0
    }
1920
0
    return SECFailure;
1921
0
}
1922
1923
SECStatus
1924
SECMOD_DeletePermDB(SECMODModule *module)
1925
0
{
1926
0
    SECMODModuleDBFunc func;
1927
0
    char *moduleSpec;
1928
0
    char **retString;
1929
1930
0
    if (module->parent == NULL)
1931
0
        return SECFailure;
1932
1933
0
    func = (SECMODModuleDBFunc)module->parent->moduleDBFunc;
1934
0
    if (func) {
1935
0
        moduleSpec = secmod_mkModuleSpec(module);
1936
0
        retString = (*func)(SECMOD_MODULE_DB_FUNCTION_DEL,
1937
0
                            module->parent->libraryParams, moduleSpec);
1938
0
        PORT_Free(moduleSpec);
1939
0
        if (retString != NULL)
1940
0
            return SECSuccess;
1941
0
    }
1942
0
    return SECFailure;
1943
0
}
1944
1945
SECStatus
1946
SECMOD_FreeModuleSpecList(SECMODModule *module, char **moduleSpecList)
1947
37.1k
{
1948
37.1k
    SECMODModuleDBFunc func = (SECMODModuleDBFunc)module->moduleDBFunc;
1949
37.1k
    char **retString;
1950
37.1k
    if (func) {
1951
37.1k
        retString = (*func)(SECMOD_MODULE_DB_FUNCTION_RELEASE,
1952
37.1k
                            module->libraryParams, moduleSpecList);
1953
37.1k
        if (retString != NULL)
1954
37.1k
            return SECSuccess;
1955
37.1k
    }
1956
0
    return SECFailure;
1957
37.1k
}
1958
1959
/*
1960
 * load a PKCS#11 module but do not add it to the default NSS trust domain
1961
 */
1962
SECMODModule *
1963
SECMOD_LoadModule(char *modulespec, SECMODModule *parent, PRBool recurse)
1964
74.3k
{
1965
74.3k
    char *library = NULL, *moduleName = NULL, *parameters = NULL, *nss = NULL;
1966
74.3k
    char *config = NULL;
1967
74.3k
    SECStatus status;
1968
74.3k
    SECMODModule *module = NULL;
1969
74.3k
    SECMODModule *oldModule = NULL;
1970
74.3k
    SECStatus rv;
1971
74.3k
    PRBool forwardPolicyFeedback = PR_FALSE;
1972
74.3k
    PRUint32 forwardPolicyCheckFlags;
1973
1974
    /* initialize the underlying module structures */
1975
74.3k
    SECMOD_Init();
1976
1977
74.3k
    status = NSSUTIL_ArgParseModuleSpecEx(modulespec, &library, &moduleName,
1978
74.3k
                                          &parameters, &nss,
1979
74.3k
                                          &config);
1980
74.3k
    if (status != SECSuccess) {
1981
0
        goto loser;
1982
0
    }
1983
1984
74.3k
    module = SECMOD_CreateModuleEx(library, moduleName, parameters, nss, config);
1985
74.3k
    forwardPolicyFeedback = NSSUTIL_ArgHasFlag("flags", "printPolicyFeedback", nss);
1986
74.3k
    forwardPolicyCheckFlags = secmod_parsePolicyCheckFlags(nss);
1987
1988
74.3k
    if (library)
1989
0
        PORT_Free(library);
1990
74.3k
    if (moduleName)
1991
74.3k
        PORT_Free(moduleName);
1992
74.3k
    if (parameters)
1993
74.3k
        PORT_Free(parameters);
1994
74.3k
    if (nss)
1995
74.3k
        PORT_Free(nss);
1996
74.3k
    if (config)
1997
0
        PORT_Free(config);
1998
74.3k
    if (!module) {
1999
0
        goto loser;
2000
0
    }
2001
2002
    /* a policy only stanza doesn't actually get 'loaded'. policy has already
2003
     * been parsed as a side effect of the CreateModuleEx call */
2004
74.3k
    if (secmod_PolicyOnly(module)) {
2005
0
        return module;
2006
0
    }
2007
74.3k
    if (parent) {
2008
37.1k
        module->parent = SECMOD_ReferenceModule(parent);
2009
37.1k
        if (module->internal && secmod_IsInternalKeySlot(parent)) {
2010
37.1k
            module->internal = parent->internal;
2011
37.1k
        }
2012
37.1k
    }
2013
2014
    /* load it */
2015
74.3k
    rv = secmod_LoadPKCS11Module(module, &oldModule);
2016
74.3k
    if (rv != SECSuccess) {
2017
0
        goto loser;
2018
0
    }
2019
2020
    /* if we just reload an old module, no need to add it to any lists.
2021
     * we simple release all our references */
2022
74.3k
    if (oldModule) {
2023
        /* This module already exists, don't link it anywhere. This
2024
         * will probably destroy this module */
2025
0
        SECMOD_DestroyModule(module);
2026
0
        return oldModule;
2027
0
    }
2028
2029
74.3k
    if (recurse && module->isModuleDB) {
2030
37.1k
        char **moduleSpecList;
2031
37.1k
        PORT_SetError(0);
2032
2033
37.1k
        moduleSpecList = SECMOD_GetModuleSpecList(module);
2034
37.1k
        if (moduleSpecList) {
2035
37.1k
            char **index;
2036
2037
37.1k
            index = moduleSpecList;
2038
37.1k
            if (*index && SECMOD_GetSkipFirstFlag(module)) {
2039
0
                index++;
2040
0
            }
2041
2042
74.3k
            for (; *index; index++) {
2043
37.1k
                SECMODModule *child;
2044
37.1k
                if (0 == PORT_Strcmp(*index, modulespec)) {
2045
                    /* avoid trivial infinite recursion */
2046
0
                    PORT_SetError(SEC_ERROR_NO_MODULE);
2047
0
                    rv = SECFailure;
2048
0
                    break;
2049
0
                }
2050
37.1k
                if (!forwardPolicyFeedback) {
2051
37.1k
                    child = SECMOD_LoadModule(*index, module, PR_TRUE);
2052
37.1k
                } else {
2053
                    /* Add printPolicyFeedback to the nss flags */
2054
0
                    char *specWithForwards =
2055
0
                        NSSUTIL_AddNSSFlagToModuleSpec(*index, "printPolicyFeedback");
2056
0
                    char *tmp;
2057
0
                    if (forwardPolicyCheckFlags & SECMOD_FLAG_POLICY_CHECK_IDENTIFIER) {
2058
0
                        tmp = NSSUTIL_AddNSSFlagToModuleSpec(specWithForwards, "policyCheckIdentifier");
2059
0
                        PORT_Free(specWithForwards);
2060
0
                        specWithForwards = tmp;
2061
0
                    }
2062
0
                    if (forwardPolicyCheckFlags & SECMOD_FLAG_POLICY_CHECK_VALUE) {
2063
0
                        tmp = NSSUTIL_AddNSSFlagToModuleSpec(specWithForwards, "policyCheckValue");
2064
0
                        PORT_Free(specWithForwards);
2065
0
                        specWithForwards = tmp;
2066
0
                    }
2067
0
                    child = SECMOD_LoadModule(specWithForwards, module, PR_TRUE);
2068
0
                    PORT_Free(specWithForwards);
2069
0
                }
2070
37.1k
                if (!child)
2071
0
                    break;
2072
37.1k
                if (child->isCritical && !child->loaded) {
2073
0
                    int err = PORT_GetError();
2074
0
                    if (!err)
2075
0
                        err = SEC_ERROR_NO_MODULE;
2076
0
                    SECMOD_DestroyModule(child);
2077
0
                    PORT_SetError(err);
2078
0
                    rv = SECFailure;
2079
0
                    break;
2080
0
                }
2081
37.1k
                SECMOD_DestroyModule(child);
2082
37.1k
            }
2083
37.1k
            SECMOD_FreeModuleSpecList(module, moduleSpecList);
2084
37.1k
        } else {
2085
0
            if (!PORT_GetError())
2086
0
                PORT_SetError(SEC_ERROR_NO_MODULE);
2087
0
            rv = SECFailure;
2088
0
        }
2089
37.1k
    }
2090
2091
74.3k
    if (rv != SECSuccess) {
2092
0
        goto loser;
2093
0
    }
2094
2095
    /* inherit the reference */
2096
74.3k
    if (!module->moduleDBOnly) {
2097
37.1k
        SECMOD_AddModuleToList(module);
2098
37.1k
    } else {
2099
37.1k
        SECMOD_AddModuleToDBOnlyList(module);
2100
37.1k
    }
2101
2102
    /* handle any additional work here */
2103
74.3k
    return module;
2104
2105
0
loser:
2106
0
    if (module) {
2107
0
        if (module->loaded) {
2108
0
            SECMOD_UnloadModule(module);
2109
0
        }
2110
0
        SECMOD_AddModuleToUnloadList(module);
2111
0
    }
2112
0
    return module;
2113
74.3k
}
2114
2115
/*
2116
 * load a PKCS#11 module and add it to the default NSS trust domain
2117
 */
2118
SECMODModule *
2119
SECMOD_LoadUserModule(char *modulespec, SECMODModule *parent, PRBool recurse)
2120
0
{
2121
0
    SECStatus rv = SECSuccess;
2122
0
    SECMODModule *newmod = SECMOD_LoadModule(modulespec, parent, recurse);
2123
0
    SECMODListLock *moduleLock = SECMOD_GetDefaultModuleListLock();
2124
2125
0
    if (newmod) {
2126
0
        SECMOD_GetReadLock(moduleLock);
2127
0
        rv = STAN_AddModuleToDefaultTrustDomain(newmod);
2128
0
        SECMOD_ReleaseReadLock(moduleLock);
2129
0
        if (SECSuccess != rv) {
2130
0
            SECMOD_DestroyModule(newmod);
2131
0
            return NULL;
2132
0
        }
2133
0
    }
2134
0
    return newmod;
2135
0
}
2136
2137
/*
2138
 * remove the PKCS#11 module from the default NSS trust domain, call
2139
 * C_Finalize, and destroy the module structure
2140
 */
2141
SECStatus
2142
SECMOD_UnloadUserModule(SECMODModule *mod)
2143
0
{
2144
0
    SECStatus rv = SECSuccess;
2145
0
    int atype = 0;
2146
0
    SECMODListLock *moduleLock = SECMOD_GetDefaultModuleListLock();
2147
0
    if (!mod) {
2148
0
        return SECFailure;
2149
0
    }
2150
2151
0
    SECMOD_GetReadLock(moduleLock);
2152
0
    rv = STAN_RemoveModuleFromDefaultTrustDomain(mod);
2153
0
    SECMOD_ReleaseReadLock(moduleLock);
2154
0
    if (SECSuccess != rv) {
2155
0
        return SECFailure;
2156
0
    }
2157
0
    return SECMOD_DeleteModuleEx(NULL, mod, &atype, PR_FALSE);
2158
0
}