Bug Summary

File:root/firefox-clang/security/nss/lib/softoken/pkcs11.c
Warning:line 4876, column 13
Value stored to 'rv' is never read

Annotated Source Code

Press '?' to see keyboard shortcuts

clang -cc1 -cc1 -triple x86_64-pc-linux-gnu -O2 -analyze -disable-free -clear-ast-before-backend -disable-llvm-verifier -discard-value-names -main-file-name pkcs11.c -analyzer-checker=core -analyzer-checker=apiModeling -analyzer-checker=unix -analyzer-checker=deadcode -analyzer-checker=security.insecureAPI.UncheckedReturn -analyzer-checker=security.insecureAPI.getpw -analyzer-checker=security.insecureAPI.gets -analyzer-checker=security.insecureAPI.mktemp -analyzer-checker=security.insecureAPI.mkstemp -analyzer-checker=security.insecureAPI.vfork -analyzer-checker=nullability.NullPassedToNonnull -analyzer-checker=nullability.NullReturnedFromNonnull -analyzer-output plist -w -setup-static-analyzer -analyzer-config-compatibility-mode=true -mrelocation-model pic -pic-level 2 -fhalf-no-semantic-interposition -mframe-pointer=all -relaxed-aliasing -ffp-contract=off -fno-rounding-math -mconstructor-aliases -funwind-tables=2 -target-cpu x86-64 -tune-cpu generic -debugger-tuning=gdb -fdebug-compilation-dir=/root/firefox-clang/obj-x86_64-pc-linux-gnu/security/nss/lib/softoken/softoken_softokn -fcoverage-compilation-dir=/root/firefox-clang/obj-x86_64-pc-linux-gnu/security/nss/lib/softoken/softoken_softokn -resource-dir /usr/lib/llvm-23/lib/clang/23 -include /root/firefox-clang/obj-x86_64-pc-linux-gnu/mozilla-config.h -U _FORTIFY_SOURCE -D _FORTIFY_SOURCE=2 -D DEBUG -D SHLIB_SUFFIX="so" -D SHLIB_PREFIX="lib" -D SOFTOKEN_LIB_NAME="libsoftokn3.so" -D SHLIB_VERSION="3" -D NSS_FIPS_DISABLED -D NSS_NO_INIT_SUPPORT -D NSS_X86_OR_X64 -D NSS_X64 -D NSS_USE_64 -D USE_UTIL_DIRECTLY -D NO_NSPR_10_SUPPORT -D SSL_DISABLE_DEPRECATED_CIPHER_SUITE_NAMES -D LINUX2_1 -D LINUX -D linux -D _DEFAULT_SOURCE -D _BSD_SOURCE -D _POSIX_SOURCE -D SDB_MEASURE_USE_TEMP_DIR -D HAVE_STRERROR -D XP_UNIX -D _REENTRANT -D NSS_DISABLE_DBM -D NSS_DISABLE_LIBPKIX -D NSS_USE_PKCS5_PBKD2_PARAMS2_ONLY -D SOFTOKEN_USE_PKCS5_PBKD2_PARAMS2_ONLY -I /root/firefox-clang/security/nss/lib/softoken -I /root/firefox-clang/obj-x86_64-pc-linux-gnu/security/nss/lib/softoken/softoken_softokn -I /root/firefox-clang/obj-x86_64-pc-linux-gnu/dist/include/nspr -I /root/firefox-clang/obj-x86_64-pc-linux-gnu/dist/include/private/nss -I /root/firefox-clang/obj-x86_64-pc-linux-gnu/dist/include/nss -I /root/firefox-clang/obj-x86_64-pc-linux-gnu/dist/include -D MOZILLA_CLIENT -internal-isystem /usr/lib/llvm-23/lib/clang/23/include -internal-isystem /usr/local/include -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/16/../../../../x86_64-linux-gnu/include -internal-externc-isystem /usr/include/x86_64-linux-gnu -internal-externc-isystem /include -internal-externc-isystem /usr/include -Wno-error=tautological-type-limit-compare -Wno-range-loop-analysis -Wno-error=deprecated-declarations -Wno-error=array-bounds -Wno-error=free-nonheap-object -Wno-error=atomic-alignment -Wno-error=deprecated-builtins -Wno-psabi -Wno-error=builtin-macro-redefined -Wno-unknown-warning-option -Wno-character-conversion -ferror-limit 19 -fstrict-flex-arrays=1 -stack-protector 2 -fstack-clash-protection -ftrivial-auto-var-init=pattern -fgnuc-version=4.2.1 -fskip-odr-check-in-gmf -fdiagnostics-absolute-paths -vectorize-loops -vectorize-slp -analyzer-checker optin.performance.Padding -analyzer-output=html -analyzer-config stable-report-filename=true -mllvm -dwarf-linkage-names=Abstract -faddrsig -fdwarf2-cfi-asm -o /tmp/scan-build-2026-10-17-231001-351276-1 -x c /root/firefox-clang/security/nss/lib/softoken/pkcs11.c
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 * This file implements PKCS 11 on top of our existing security modules
6 *
7 * For more information about PKCS 11 See PKCS 11 Token Inteface Standard.
8 * This implementation has two slots:
9 * slot 1 is our generic crypto support. It does not require login.
10 * It supports Public Key ops, and all they bulk ciphers and hashes.
11 * It can also support Private Key ops for imported Private keys. It does
12 * not have any token storage.
13 * slot 2 is our private key support. It requires a login before use. It
14 * can store Private Keys and Certs as token objects. Currently only private
15 * keys and their associated Certificates are saved on the token.
16 *
17 * In this implementation, session objects are only visible to the session
18 * that created or generated them.
19 */
20#include "seccomon.h"
21#include "eccutil.h"
22#include "secitem.h"
23/* we need to use the deprecated mechanisms values for backward compatibility */
24#include "pkcs11.h"
25#include "pkcs11i.h"
26#include "softoken.h"
27#include "lowkeyi.h"
28#include "blapi.h"
29#include "secder.h"
30#include "secport.h"
31#include "secrng.h"
32#include "prlock.h"
33#include "prtypes.h"
34#include "nspr.h"
35#include "softkver.h"
36#include "secoid.h"
37#include "sftkdb.h"
38#include "utilpars.h"
39#include "ec.h"
40#include "secasn1.h"
41#include "secerr.h"
42#include "lgglue.h"
43#include "kem.h"
44#include "kyber.h"
45
46PRBool parentForkedAfterC_Initialize;
47
48#ifndef NO_FORK_CHECK
49
50PRBool sftkForkCheckDisabled;
51
52#if defined(CHECK_FORK_PTHREAD) || defined(CHECK_FORK_MIXED)
53PRBool forked = PR_FALSE0;
54#endif
55
56#if defined(CHECK_FORK_GETPID) || defined(CHECK_FORK_MIXED)
57#include <unistd.h>
58pid_t myPid;
59#endif
60
61#ifdef CHECK_FORK_MIXED
62#include <sys/systeminfo.h>
63PRBool usePthread_atfork;
64#endif
65
66#endif
67
68#ifdef XP_UNIX1
69#define LIB_PARAM_DEFAULT_FILE_LOCATION"/etc/nss/params.config" "/etc/nss/params.config"
70#endif
71
72#define LIB_PARAM_DEFAULT" configdir='' certPrefix='' keyPrefix='' secmod='' flags=noCertDB,noModDB " " configdir='' certPrefix='' keyPrefix='' secmod='' flags=noCertDB,noModDB "
73/*
74 * ******************** Static data *******************************
75 */
76
77/* The next three strings must be exactly 32 characters long */
78static char *manufacturerID = "Mozilla Foundation ";
79static char manufacturerID_space[33];
80static char *libraryDescription = "NSS Internal Crypto Services ";
81static char libraryDescription_space[33];
82
83/*
84 * In FIPS mode, we disallow login attempts for 1 second after a login
85 * failure so that there are at most 60 login attempts per minute.
86 */
87static PRIntervalTime loginWaitTime;
88
89#undef __PASTE
90#define __PASTE(x, y) x##y
91
92/*
93 * we renamed all our internal functions, get the correct
94 * definitions for them...
95 */
96#undef CK_PKCS11_FUNCTION_INFO
97#undef CK_NEED_ARG_LIST
98
99#define CK_PKCS11_3_21 1
100#define CK_PKCS11_3_01 1
101#define CK_EXTERNextern extern
102#define CK_PKCS11_FUNCTION_INFO(func) \
103 CK_RV __PASTE(NS, func)
104#define CK_NEED_ARG_LIST 1
105
106#include "pkcs11f.h"
107
108#ifndef NSS_FIPS_DISABLE
109/* ------------- forward declare all the FIPS functions ------------- */
110#undef CK_NEED_ARG_LIST
111#undef CK_PKCS11_FUNCTION_INFO
112
113#define CK_PKCS11_FUNCTION_INFO(name) CK_RV __PASTE(F, name)
114#define CK_NEED_ARG_LIST 1
115
116#include "pkcs11f.h"
117#endif
118
119/* build the crypto module table */
120static CK_FUNCTION_LIST_3_2 sftk_funcList_v32 = {
121 { 3, 2 },
122
123#undef CK_PKCS11_FUNCTION_INFO
124#undef CK_NEED_ARG_LIST
125
126#define CK_PKCS11_3_2_ONLY 1
127#define CK_PKCS11_FUNCTION_INFO(func) \
128 __PASTE(NS, func) \
129 ,
130#include "pkcs11f.h"
131
132};
133#undef CK_PKCS11_3_2_ONLY
134
135/* build the crypto module table */
136static CK_FUNCTION_LIST_3_0 sftk_funcList_v30 = {
137 { 3, 0 },
138
139#undef CK_PKCS11_FUNCTION_INFO
140#undef CK_NEED_ARG_LIST
141
142#define CK_PKCS11_3_0_ONLY 1
143#define CK_PKCS11_FUNCTION_INFO(func) \
144 __PASTE(NS, func) \
145 ,
146#include "pkcs11f.h"
147
148};
149#undef CK_PKCS11_3_0_ONLY
150
151/* need a special version of get info for version 2 which returns the version
152 * 2.4 version number */
153CK_RV NSC_GetInfoV2(CK_INFO_PTR pInfo);
154CK_RV NSC_GetMechanismInfoV2(CK_SLOT_ID slotID, CK_MECHANISM_TYPE type,
155 CK_MECHANISM_INFO_PTR pInfo);
156
157/* build the crypto module table */
158static CK_FUNCTION_LIST sftk_funcList_v2 = {
159 { 2, 40 },
160
161#define CK_PKCS11_2_0_ONLY1 1
162#undef CK_PKCS11_FUNCTION_INFO
163#undef CK_NEED_ARG_LIST
164#define C_GetInfo C_GetInfoV2
165#define C_GetMechanismInfo C_GetMechanismInfoV2
166
167#define CK_PKCS11_FUNCTION_INFO(func) \
168 __PASTE(NS, func) \
169 ,
170#include "pkcs11f.h"
171
172};
173
174#undef C_GetInfo
175#undef C_GetMechanismInfo
176#undef CK_PKCS_11_2_0_ONLY
177#undef CK_PKCS11_FUNCTION_INFO
178#undef CK_NEED_ARG_LIST
179
180#undef __PASTE
181
182CK_NSS_MODULE_FUNCTIONS sftk_module_funcList = {
183 { 1, 0 },
184 NSC_ModuleDBFunc
185};
186
187static CK_RV
188nsc_NSSGetFIPSStatus(CK_SESSION_HANDLE hSession,
189 CK_OBJECT_HANDLE hObject,
190 CK_ULONG ulOperationType,
191 CK_ULONG *pulFIPSStatus);
192CK_NSS_FIPS_FUNCTIONS sftk_fips_funcList = {
193 { 1, 0 },
194 nsc_NSSGetFIPSStatus
195};
196
197CK_NSS_KEM_FUNCTIONS sftk_kem_funcList = {
198 { 1, 0 },
199 NSC_Encapsulate,
200 NSC_Decapsulate
201};
202
203/*
204 * Array is orderd by default first
205 */
206static CK_INTERFACE nss_interfaces[] = {
207 { (CK_UTF8CHAR_PTR) "PKCS 11", &sftk_funcList_v32, NSS_INTERFACE_FLAGS0x00000001UL },
208 { (CK_UTF8CHAR_PTR) "PKCS 11", &sftk_funcList_v30, NSS_INTERFACE_FLAGS0x00000001UL },
209 { (CK_UTF8CHAR_PTR) "PKCS 11", &sftk_funcList_v2, NSS_INTERFACE_FLAGS0x00000001UL },
210 { (CK_UTF8CHAR_PTR) "Vendor NSS Module Interface", &sftk_module_funcList, NSS_INTERFACE_FLAGS0x00000001UL },
211 { (CK_UTF8CHAR_PTR) "Vendor NSS FIPS Interface", &sftk_fips_funcList, NSS_INTERFACE_FLAGS0x00000001UL },
212 { (CK_UTF8CHAR_PTR) "Vendor NSS KEM Interface", &sftk_kem_funcList, NSS_INTERFACE_FLAGS0x00000001UL }
213};
214/* must match the count of interfaces in nss_interfaces above */
215#define NSS_INTERFACE_COUNT(sizeof(nss_interfaces) / sizeof((nss_interfaces)[0])) PR_ARRAY_SIZE(nss_interfaces)(sizeof(nss_interfaces) / sizeof((nss_interfaces)[0]))
216
217/* List of DES Weak Keys */
218typedef unsigned char desKey[8];
219static const desKey sftk_desWeakTable[] = {
220#ifdef noParity
221 /* weak */
222 { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 },
223 { 0x1e, 0x1e, 0x1e, 0x1e, 0x0e, 0x0e, 0x0e, 0x0e },
224 { 0xe0, 0xe0, 0xe0, 0xe0, 0xf0, 0xf0, 0xf0, 0xf0 },
225 { 0xfe, 0xfe, 0xfe, 0xfe, 0xfe, 0xfe, 0xfe, 0xfe },
226 /* semi-weak */
227 { 0x00, 0xfe, 0x00, 0xfe, 0x00, 0xfe, 0x00, 0xfe },
228 { 0xfe, 0x00, 0xfe, 0x00, 0x00, 0xfe, 0x00, 0xfe },
229
230 { 0x1e, 0xe0, 0x1e, 0xe0, 0x0e, 0xf0, 0x0e, 0xf0 },
231 { 0xe0, 0x1e, 0xe0, 0x1e, 0xf0, 0x0e, 0xf0, 0x0e },
232
233 { 0x00, 0xe0, 0x00, 0xe0, 0x00, 0x0f, 0x00, 0x0f },
234 { 0xe0, 0x00, 0xe0, 0x00, 0xf0, 0x00, 0xf0, 0x00 },
235
236 { 0x1e, 0xfe, 0x1e, 0xfe, 0x0e, 0xfe, 0x0e, 0xfe },
237 { 0xfe, 0x1e, 0xfe, 0x1e, 0xfe, 0x0e, 0xfe, 0x0e },
238
239 { 0x00, 0x1e, 0x00, 0x1e, 0x00, 0x0e, 0x00, 0x0e },
240 { 0x1e, 0x00, 0x1e, 0x00, 0x0e, 0x00, 0x0e, 0x00 },
241
242 { 0xe0, 0xfe, 0xe0, 0xfe, 0xf0, 0xfe, 0xf0, 0xfe },
243 { 0xfe, 0xe0, 0xfe, 0xe0, 0xfe, 0xf0, 0xfe, 0xf0 },
244#else
245 /* weak */
246 { 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01 },
247 { 0x1f, 0x1f, 0x1f, 0x1f, 0x0e, 0x0e, 0x0e, 0x0e },
248 { 0xe0, 0xe0, 0xe0, 0xe0, 0xf1, 0xf1, 0xf1, 0xf1 },
249 { 0xfe, 0xfe, 0xfe, 0xfe, 0xfe, 0xfe, 0xfe, 0xfe },
250
251 /* semi-weak */
252 { 0x01, 0xfe, 0x01, 0xfe, 0x01, 0xfe, 0x01, 0xfe },
253 { 0xfe, 0x01, 0xfe, 0x01, 0xfe, 0x01, 0xfe, 0x01 },
254
255 { 0x1f, 0xe0, 0x1f, 0xe0, 0x0e, 0xf1, 0x0e, 0xf1 },
256 { 0xe0, 0x1f, 0xe0, 0x1f, 0xf1, 0x0e, 0xf1, 0x0e },
257
258 { 0x01, 0xe0, 0x01, 0xe0, 0x01, 0xf1, 0x01, 0xf1 },
259 { 0xe0, 0x01, 0xe0, 0x01, 0xf1, 0x01, 0xf1, 0x01 },
260
261 { 0x1f, 0xfe, 0x1f, 0xfe, 0x0e, 0xfe, 0x0e, 0xfe },
262 { 0xfe, 0x1f, 0xfe, 0x1f, 0xfe, 0x0e, 0xfe, 0x0e },
263
264 { 0x01, 0x1f, 0x01, 0x1f, 0x01, 0x0e, 0x01, 0x0e },
265 { 0x1f, 0x01, 0x1f, 0x01, 0x0e, 0x01, 0x0e, 0x01 },
266
267 { 0xe0, 0xfe, 0xe0, 0xfe, 0xf1, 0xfe, 0xf1, 0xfe },
268 { 0xfe, 0xe0, 0xfe, 0xe0, 0xfe, 0xf1, 0xfe, 0xf1 }
269#endif
270};
271
272static const int sftk_desWeakTableSize = sizeof(sftk_desWeakTable) /
273 sizeof(sftk_desWeakTable[0]);
274
275/* DES KEY Parity conversion table. Takes each byte/2 as an index, returns
276 * that byte with the proper parity bit set */
277static const unsigned char parityTable[256] = {
278 /* Even...0x00,0x02,0x04,0x06,0x08,0x0a,0x0c,0x0e */
279 /* E */ 0x01, 0x02, 0x04, 0x07, 0x08, 0x0b, 0x0d, 0x0e,
280 /* Odd....0x10,0x12,0x14,0x16,0x18,0x1a,0x1c,0x1e */
281 /* O */ 0x10, 0x13, 0x15, 0x16, 0x19, 0x1a, 0x1c, 0x1f,
282 /* Odd....0x20,0x22,0x24,0x26,0x28,0x2a,0x2c,0x2e */
283 /* O */ 0x20, 0x23, 0x25, 0x26, 0x29, 0x2a, 0x2c, 0x2f,
284 /* Even...0x30,0x32,0x34,0x36,0x38,0x3a,0x3c,0x3e */
285 /* E */ 0x31, 0x32, 0x34, 0x37, 0x38, 0x3b, 0x3d, 0x3e,
286 /* Odd....0x40,0x42,0x44,0x46,0x48,0x4a,0x4c,0x4e */
287 /* O */ 0x40, 0x43, 0x45, 0x46, 0x49, 0x4a, 0x4c, 0x4f,
288 /* Even...0x50,0x52,0x54,0x56,0x58,0x5a,0x5c,0x5e */
289 /* E */ 0x51, 0x52, 0x54, 0x57, 0x58, 0x5b, 0x5d, 0x5e,
290 /* Even...0x60,0x62,0x64,0x66,0x68,0x6a,0x6c,0x6e */
291 /* E */ 0x61, 0x62, 0x64, 0x67, 0x68, 0x6b, 0x6d, 0x6e,
292 /* Odd....0x70,0x72,0x74,0x76,0x78,0x7a,0x7c,0x7e */
293 /* O */ 0x70, 0x73, 0x75, 0x76, 0x79, 0x7a, 0x7c, 0x7f,
294 /* Odd....0x80,0x82,0x84,0x86,0x88,0x8a,0x8c,0x8e */
295 /* O */ 0x80, 0x83, 0x85, 0x86, 0x89, 0x8a, 0x8c, 0x8f,
296 /* Even...0x90,0x92,0x94,0x96,0x98,0x9a,0x9c,0x9e */
297 /* E */ 0x91, 0x92, 0x94, 0x97, 0x98, 0x9b, 0x9d, 0x9e,
298 /* Even...0xa0,0xa2,0xa4,0xa6,0xa8,0xaa,0xac,0xae */
299 /* E */ 0xa1, 0xa2, 0xa4, 0xa7, 0xa8, 0xab, 0xad, 0xae,
300 /* Odd....0xb0,0xb2,0xb4,0xb6,0xb8,0xba,0xbc,0xbe */
301 /* O */ 0xb0, 0xb3, 0xb5, 0xb6, 0xb9, 0xba, 0xbc, 0xbf,
302 /* Even...0xc0,0xc2,0xc4,0xc6,0xc8,0xca,0xcc,0xce */
303 /* E */ 0xc1, 0xc2, 0xc4, 0xc7, 0xc8, 0xcb, 0xcd, 0xce,
304 /* Odd....0xd0,0xd2,0xd4,0xd6,0xd8,0xda,0xdc,0xde */
305 /* O */ 0xd0, 0xd3, 0xd5, 0xd6, 0xd9, 0xda, 0xdc, 0xdf,
306 /* Odd....0xe0,0xe2,0xe4,0xe6,0xe8,0xea,0xec,0xee */
307 /* O */ 0xe0, 0xe3, 0xe5, 0xe6, 0xe9, 0xea, 0xec, 0xef,
308 /* Even...0xf0,0xf2,0xf4,0xf6,0xf8,0xfa,0xfc,0xfe */
309 /* E */ 0xf1, 0xf2, 0xf4, 0xf7, 0xf8, 0xfb, 0xfd, 0xfe
310};
311
312/* Mechanisms */
313struct mechanismList {
314 CK_MECHANISM_TYPE type;
315 CK_MECHANISM_INFO info;
316 PRBool privkey;
317};
318
319/*
320 * the following table includes a complete list of mechanism defined by
321 * PKCS #11 version 2.01. Those Mechanisms not supported by this PKCS #11
322 * module are ifdef'ed out.
323 */
324#define CKF_EN_DE0x00000100UL | 0x00000200UL CKF_ENCRYPT0x00000100UL | CKF_DECRYPT0x00000200UL
325#define CKF_WR_UN0x00020000UL | 0x00040000UL CKF_WRAP0x00020000UL | CKF_UNWRAP0x00040000UL
326#define CKF_SN_VR0x00000800UL | 0x00002000 CKF_SIGN0x00000800UL | CKF_VERIFY0x00002000
327#define CKF_SN_RE0x00001000UL | 0x00004000UL CKF_SIGN_RECOVER0x00001000UL | CKF_VERIFY_RECOVER0x00004000UL
328#define CKF_EN_DE_MSG0x00000100UL | 0x00000200UL | 0x00000002UL | 0x00000004UL CKF_ENCRYPT0x00000100UL | CKF_DECRYPT0x00000200UL | CKF_MESSAGE_ENCRYPT0x00000002UL | CKF_MESSAGE_DECRYPT0x00000004UL
329#define CKF_KEM0x10000000UL | 0x20000000UL CKF_ENCAPSULATE0x10000000UL | CKF_DECAPSULATE0x20000000UL
330
331#define CKF_EN_DE_WR_UN0x00000100UL | 0x00000200UL | 0x00020000UL | 0x00040000UL CKF_EN_DE0x00000100UL | 0x00000200UL | CKF_WR_UN0x00020000UL | 0x00040000UL
332#define CKF_SN_VR_RE0x00000800UL | 0x00002000 | 0x00001000UL | 0x00004000UL CKF_SN_VR0x00000800UL | 0x00002000 | CKF_SN_RE0x00001000UL | 0x00004000UL
333#define CKF_DUZ_IT_ALL0x00000100UL | 0x00000200UL | 0x00020000UL | 0x00040000UL | 0x00000800UL
| 0x00002000 | 0x00001000UL | 0x00004000UL
CKF_EN_DE_WR_UN0x00000100UL | 0x00000200UL | 0x00020000UL | 0x00040000UL | CKF_SN_VR_RE0x00000800UL | 0x00002000 | 0x00001000UL | 0x00004000UL
334
335#define CKF_EC_PNU0x00100000UL | 0x00800000UL | 0x01000000UL CKF_EC_F_P0x00100000UL | CKF_EC_NAMEDCURVE0x00800000UL | CKF_EC_UNCOMPRESS0x01000000UL
336
337#define CKF_EC_BPNU0x00200000UL | 0x00100000UL | 0x00800000UL | 0x01000000UL CKF_EC_F_2M0x00200000UL | CKF_EC_PNU0x00100000UL | 0x00800000UL | 0x01000000UL
338#define CKF_EC_POC0x00100000UL | 0x00800000UL | 0x02000000UL CKF_EC_F_P0x00100000UL | CKF_EC_OID0x00800000UL | CKF_EC_COMPRESS0x02000000UL
339
340#define CK_MAX0xffffffff 0xffffffff
341
342static const struct mechanismList mechanisms[] = {
343
344 /*
345 * PKCS #11 Mechanism List.
346 *
347 * The first argument is the PKCS #11 Mechanism we support.
348 * The second argument is Mechanism info structure. It includes:
349 * The minimum key size,
350 * in bits for RSA, DSA, DH, EC*, KEA, RC2 and RC4 * algs.
351 * in bytes for RC5, AES, Camellia, and CAST*
352 * ignored for DES*, IDEA and FORTEZZA based
353 * The maximum key size,
354 * in bits for RSA, DSA, DH, EC*, KEA, RC2 and RC4 * algs.
355 * in bytes for RC5, AES, Camellia, and CAST*
356 * ignored for DES*, IDEA and FORTEZZA based
357 * Flags
358 * What operations are supported by this mechanism.
359 * The third argument is a bool which tells if this mechanism is
360 * supported in the database token.
361 *
362 */
363
364 /* ------------------------- RSA Operations ---------------------------*/
365 { CKM_RSA_PKCS_KEY_PAIR_GEN0x00000000UL, { RSA_MIN_MODULUS_BITS128, CK_MAX0xffffffff, CKF_GENERATE_KEY_PAIR0x00010000UL }, PR_TRUE1 },
366 { CKM_RSA_PKCS0x00000001UL, { RSA_MIN_MODULUS_BITS128, CK_MAX0xffffffff, CKF_DUZ_IT_ALL0x00000100UL | 0x00000200UL | 0x00020000UL | 0x00040000UL | 0x00000800UL
| 0x00002000 | 0x00001000UL | 0x00004000UL
}, PR_TRUE1 },
367 { CKM_RSA_PKCS_PSS0x0000000DUL, { RSA_MIN_MODULUS_BITS128, CK_MAX0xffffffff, CKF_SN_VR0x00000800UL | 0x00002000 }, PR_TRUE1 },
368 { CKM_RSA_PKCS_OAEP0x00000009UL, { RSA_MIN_MODULUS_BITS128, CK_MAX0xffffffff, CKF_EN_DE_WR_UN0x00000100UL | 0x00000200UL | 0x00020000UL | 0x00040000UL }, PR_TRUE1 },
369#ifdef SFTK_RSA9796_SUPPORTED
370 { CKM_RSA_97960x00000002UL, { RSA_MIN_MODULUS_BITS128, CK_MAX0xffffffff, CKF_DUZ_IT_ALL0x00000100UL | 0x00000200UL | 0x00020000UL | 0x00040000UL | 0x00000800UL
| 0x00002000 | 0x00001000UL | 0x00004000UL
}, PR_TRUE1 },
371#endif
372 { CKM_RSA_X_5090x00000003UL, { RSA_MIN_MODULUS_BITS128, CK_MAX0xffffffff, CKF_DUZ_IT_ALL0x00000100UL | 0x00000200UL | 0x00020000UL | 0x00040000UL | 0x00000800UL
| 0x00002000 | 0x00001000UL | 0x00004000UL
}, PR_TRUE1 },
373 /* -------------- RSA Multipart Signing Operations -------------------- */
374 { CKM_MD2_RSA_PKCS0x00000004UL, { RSA_MIN_MODULUS_BITS128, CK_MAX0xffffffff, CKF_SN_VR0x00000800UL | 0x00002000 }, PR_TRUE1 },
375 { CKM_MD5_RSA_PKCS0x00000005UL, { RSA_MIN_MODULUS_BITS128, CK_MAX0xffffffff, CKF_SN_VR0x00000800UL | 0x00002000 }, PR_TRUE1 },
376 { CKM_SHA1_RSA_PKCS0x00000006UL, { RSA_MIN_MODULUS_BITS128, CK_MAX0xffffffff, CKF_SN_VR0x00000800UL | 0x00002000 }, PR_TRUE1 },
377 { CKM_SHA224_RSA_PKCS0x00000046UL, { RSA_MIN_MODULUS_BITS128, CK_MAX0xffffffff, CKF_SN_VR0x00000800UL | 0x00002000 }, PR_TRUE1 },
378 { CKM_SHA256_RSA_PKCS0x00000040UL, { RSA_MIN_MODULUS_BITS128, CK_MAX0xffffffff, CKF_SN_VR0x00000800UL | 0x00002000 }, PR_TRUE1 },
379 { CKM_SHA384_RSA_PKCS0x00000041UL, { RSA_MIN_MODULUS_BITS128, CK_MAX0xffffffff, CKF_SN_VR0x00000800UL | 0x00002000 }, PR_TRUE1 },
380 { CKM_SHA512_RSA_PKCS0x00000042UL, { RSA_MIN_MODULUS_BITS128, CK_MAX0xffffffff, CKF_SN_VR0x00000800UL | 0x00002000 }, PR_TRUE1 },
381 { CKM_SHA1_RSA_PKCS_PSS0x0000000EUL, { RSA_MIN_MODULUS_BITS128, CK_MAX0xffffffff, CKF_SN_VR0x00000800UL | 0x00002000 }, PR_TRUE1 },
382 { CKM_SHA224_RSA_PKCS_PSS0x00000047UL, { RSA_MIN_MODULUS_BITS128, CK_MAX0xffffffff, CKF_SN_VR0x00000800UL | 0x00002000 }, PR_TRUE1 },
383 { CKM_SHA256_RSA_PKCS_PSS0x00000043UL, { RSA_MIN_MODULUS_BITS128, CK_MAX0xffffffff, CKF_SN_VR0x00000800UL | 0x00002000 }, PR_TRUE1 },
384 { CKM_SHA384_RSA_PKCS_PSS0x00000044UL, { RSA_MIN_MODULUS_BITS128, CK_MAX0xffffffff, CKF_SN_VR0x00000800UL | 0x00002000 }, PR_TRUE1 },
385 { CKM_SHA512_RSA_PKCS_PSS0x00000045UL, { RSA_MIN_MODULUS_BITS128, CK_MAX0xffffffff, CKF_SN_VR0x00000800UL | 0x00002000 }, PR_TRUE1 },
386/* ------------------------- DSA Operations --------------------------- */
387#ifndef NSS_DISABLE_DSA
388 { CKM_DSA_KEY_PAIR_GEN0x00000010UL, { DSA_MIN_P_BITS512, DSA_MAX_P_BITS3072, CKF_GENERATE_KEY_PAIR0x00010000UL }, PR_TRUE1 },
389 { CKM_DSA0x00000011UL, { DSA_MIN_P_BITS512, DSA_MAX_P_BITS3072, CKF_SN_VR0x00000800UL | 0x00002000 }, PR_TRUE1 },
390 { CKM_DSA_PARAMETER_GEN0x00002000UL, { DSA_MIN_P_BITS512, DSA_MAX_P_BITS3072, CKF_GENERATE0x00008000UL }, PR_TRUE1 },
391 { CKM_DSA_SHA10x00000012UL, { DSA_MIN_P_BITS512, DSA_MAX_P_BITS3072, CKF_SN_VR0x00000800UL | 0x00002000 }, PR_TRUE1 },
392 { CKM_DSA_SHA2240x00000013UL, { DSA_MIN_P_BITS512, DSA_MAX_P_BITS3072, CKF_SN_VR0x00000800UL | 0x00002000 }, PR_TRUE1 },
393 { CKM_DSA_SHA2560x00000014UL, { DSA_MIN_P_BITS512, DSA_MAX_P_BITS3072, CKF_SN_VR0x00000800UL | 0x00002000 }, PR_TRUE1 },
394 { CKM_DSA_SHA3840x00000015UL, { DSA_MIN_P_BITS512, DSA_MAX_P_BITS3072, CKF_SN_VR0x00000800UL | 0x00002000 }, PR_TRUE1 },
395 { CKM_DSA_SHA5120x00000016UL, { DSA_MIN_P_BITS512, DSA_MAX_P_BITS3072, CKF_SN_VR0x00000800UL | 0x00002000 }, PR_TRUE1 },
396#endif
397 /* -------------------- Diffie Hellman Operations --------------------- */
398 /* no diffie hellman yet */
399 { CKM_DH_PKCS_KEY_PAIR_GEN0x00000020UL, { DH_MIN_P_BITS128, DH_MAX_P_BITS16384, CKF_GENERATE_KEY_PAIR0x00010000UL }, PR_TRUE1 },
400 { CKM_DH_PKCS_DERIVE0x00000021UL, { DH_MIN_P_BITS128, DH_MAX_P_BITS16384, CKF_DERIVE0x00080000UL }, PR_TRUE1 },
401 /* -------------------- Elliptic Curve Operations --------------------- */
402 { CKM_EC_KEY_PAIR_GEN0x00001040UL, { EC_MIN_KEY_BITS256, EC_MAX_KEY_BITS521, CKF_GENERATE_KEY_PAIR0x00010000UL | CKF_EC_BPNU0x00200000UL | 0x00100000UL | 0x00800000UL | 0x01000000UL }, PR_TRUE1 },
403 { CKM_NSS_ECDHE_NO_PAIRWISE_CHECK_KEY_PAIR_GEN((0x80000000UL | 0x4E534350) + 47), { EC_MIN_KEY_BITS256, EC_MAX_KEY_BITS521, CKF_GENERATE_KEY_PAIR0x00010000UL | CKF_EC_BPNU0x00200000UL | 0x00100000UL | 0x00800000UL | 0x01000000UL }, PR_TRUE1 },
404 { CKM_ECDH1_DERIVE0x00001050UL, { EC_MIN_KEY_BITS256, EC_MAX_KEY_BITS521, CKF_DERIVE0x00080000UL | CKF_EC_BPNU0x00200000UL | 0x00100000UL | 0x00800000UL | 0x01000000UL }, PR_TRUE1 },
405 { CKM_ECDSA0x00001041UL, { EC_MIN_KEY_BITS256, EC_MAX_KEY_BITS521, CKF_SN_VR0x00000800UL | 0x00002000 | CKF_EC_BPNU0x00200000UL | 0x00100000UL | 0x00800000UL | 0x01000000UL }, PR_TRUE1 },
406 { CKM_ECDSA_SHA10x00001042UL, { EC_MIN_KEY_BITS256, EC_MAX_KEY_BITS521, CKF_SN_VR0x00000800UL | 0x00002000 | CKF_EC_BPNU0x00200000UL | 0x00100000UL | 0x00800000UL | 0x01000000UL }, PR_TRUE1 },
407 { CKM_ECDSA_SHA2240x00001043UL, { EC_MIN_KEY_BITS256, EC_MAX_KEY_BITS521, CKF_SN_VR0x00000800UL | 0x00002000 | CKF_EC_BPNU0x00200000UL | 0x00100000UL | 0x00800000UL | 0x01000000UL }, PR_TRUE1 },
408 { CKM_ECDSA_SHA2560x00001044UL, { EC_MIN_KEY_BITS256, EC_MAX_KEY_BITS521, CKF_SN_VR0x00000800UL | 0x00002000 | CKF_EC_BPNU0x00200000UL | 0x00100000UL | 0x00800000UL | 0x01000000UL }, PR_TRUE1 },
409 { CKM_ECDSA_SHA3840x00001045UL, { EC_MIN_KEY_BITS256, EC_MAX_KEY_BITS521, CKF_SN_VR0x00000800UL | 0x00002000 | CKF_EC_BPNU0x00200000UL | 0x00100000UL | 0x00800000UL | 0x01000000UL }, PR_TRUE1 },
410 { CKM_ECDSA_SHA5120x00001046UL, { EC_MIN_KEY_BITS256, EC_MAX_KEY_BITS521, CKF_SN_VR0x00000800UL | 0x00002000 | CKF_EC_BPNU0x00200000UL | 0x00100000UL | 0x00800000UL | 0x01000000UL }, PR_TRUE1 },
411 { CKM_EC_EDWARDS_KEY_PAIR_GEN0x00001055UL, { ECD_MIN_KEY_BITS255, ECD_MAX_KEY_BITS255, CKF_GENERATE_KEY_PAIR0x00010000UL }, PR_TRUE1 },
412 { CKM_EC_MONTGOMERY_KEY_PAIR_GEN0x00001056UL, { ECD_MIN_KEY_BITS255, ECD_MAX_KEY_BITS255, CKF_GENERATE_KEY_PAIR0x00010000UL }, PR_TRUE1 },
413 { CKM_EDDSA0x00001057UL, { ECD_MIN_KEY_BITS255, ECD_MAX_KEY_BITS255, CKF_SN_VR0x00000800UL | 0x00002000 | CKF_EC_POC0x00100000UL | 0x00800000UL | 0x02000000UL }, PR_TRUE1 },
414 /* ------------------------- RC2 Operations --------------------------- */
415 { CKM_RC2_KEY_GEN0x00000100UL, { 1, 128, CKF_GENERATE0x00008000UL }, PR_TRUE1 },
416 { CKM_RC2_ECB0x00000101UL, { 1, 128, CKF_EN_DE_WR_UN0x00000100UL | 0x00000200UL | 0x00020000UL | 0x00040000UL }, PR_TRUE1 },
417 { CKM_RC2_CBC0x00000102UL, { 1, 128, CKF_EN_DE_WR_UN0x00000100UL | 0x00000200UL | 0x00020000UL | 0x00040000UL }, PR_TRUE1 },
418 { CKM_RC2_MAC0x00000103UL, { 1, 128, CKF_SN_VR0x00000800UL | 0x00002000 }, PR_TRUE1 },
419 { CKM_RC2_MAC_GENERAL0x00000104UL, { 1, 128, CKF_SN_VR0x00000800UL | 0x00002000 }, PR_TRUE1 },
420 { CKM_RC2_CBC_PAD0x00000105UL, { 1, 128, CKF_EN_DE_WR_UN0x00000100UL | 0x00000200UL | 0x00020000UL | 0x00040000UL }, PR_TRUE1 },
421 /* ------------------------- RC4 Operations --------------------------- */
422 { CKM_RC4_KEY_GEN0x00000110UL, { 1, 256, CKF_GENERATE0x00008000UL }, PR_FALSE0 },
423 { CKM_RC40x00000111UL, { 1, 256, CKF_EN_DE_WR_UN0x00000100UL | 0x00000200UL | 0x00020000UL | 0x00040000UL }, PR_FALSE0 },
424 /* ------------------------- DES Operations --------------------------- */
425 { CKM_DES_KEY_GEN0x00000120UL, { 8, 8, CKF_GENERATE0x00008000UL }, PR_TRUE1 },
426 { CKM_DES_ECB0x00000121UL, { 8, 8, CKF_EN_DE_WR_UN0x00000100UL | 0x00000200UL | 0x00020000UL | 0x00040000UL }, PR_TRUE1 },
427 { CKM_DES_CBC0x00000122UL, { 8, 8, CKF_EN_DE_WR_UN0x00000100UL | 0x00000200UL | 0x00020000UL | 0x00040000UL }, PR_TRUE1 },
428 { CKM_DES_MAC0x00000123UL, { 8, 8, CKF_SN_VR0x00000800UL | 0x00002000 }, PR_TRUE1 },
429 { CKM_DES_MAC_GENERAL0x00000124UL, { 8, 8, CKF_SN_VR0x00000800UL | 0x00002000 }, PR_TRUE1 },
430 { CKM_DES_CBC_PAD0x00000125UL, { 8, 8, CKF_EN_DE_WR_UN0x00000100UL | 0x00000200UL | 0x00020000UL | 0x00040000UL }, PR_TRUE1 },
431 { CKM_DES2_KEY_GEN0x00000130UL, { 24, 24, CKF_GENERATE0x00008000UL }, PR_TRUE1 },
432 { CKM_DES3_KEY_GEN0x00000131UL, { 24, 24, CKF_GENERATE0x00008000UL }, PR_TRUE1 },
433 { CKM_DES3_ECB0x00000132UL, { 24, 24, CKF_EN_DE_WR_UN0x00000100UL | 0x00000200UL | 0x00020000UL | 0x00040000UL }, PR_TRUE1 },
434 { CKM_DES3_CBC0x00000133UL, { 24, 24, CKF_EN_DE_WR_UN0x00000100UL | 0x00000200UL | 0x00020000UL | 0x00040000UL }, PR_TRUE1 },
435 { CKM_DES3_MAC0x00000134UL, { 24, 24, CKF_SN_VR0x00000800UL | 0x00002000 }, PR_TRUE1 },
436 { CKM_DES3_MAC_GENERAL0x00000135UL, { 24, 24, CKF_SN_VR0x00000800UL | 0x00002000 }, PR_TRUE1 },
437 { CKM_DES3_CBC_PAD0x00000136UL, { 24, 24, CKF_EN_DE_WR_UN0x00000100UL | 0x00000200UL | 0x00020000UL | 0x00040000UL }, PR_TRUE1 },
438 /* ------------------------- CDMF Operations --------------------------- */
439 { CKM_CDMF_KEY_GEN0x00000140UL, { 8, 8, CKF_GENERATE0x00008000UL }, PR_TRUE1 },
440 { CKM_CDMF_ECB0x00000141UL, { 8, 8, CKF_EN_DE_WR_UN0x00000100UL | 0x00000200UL | 0x00020000UL | 0x00040000UL }, PR_TRUE1 },
441 { CKM_CDMF_CBC0x00000142UL, { 8, 8, CKF_EN_DE_WR_UN0x00000100UL | 0x00000200UL | 0x00020000UL | 0x00040000UL }, PR_TRUE1 },
442 { CKM_CDMF_MAC0x00000143UL, { 8, 8, CKF_SN_VR0x00000800UL | 0x00002000 }, PR_TRUE1 },
443 { CKM_CDMF_MAC_GENERAL0x00000144UL, { 8, 8, CKF_SN_VR0x00000800UL | 0x00002000 }, PR_TRUE1 },
444 { CKM_CDMF_CBC_PAD0x00000145UL, { 8, 8, CKF_EN_DE_WR_UN0x00000100UL | 0x00000200UL | 0x00020000UL | 0x00040000UL }, PR_TRUE1 },
445 /* ------------------------- AES Operations --------------------------- */
446 { CKM_AES_KEY_GEN0x00001080UL, { 16, 32, CKF_GENERATE0x00008000UL }, PR_TRUE1 },
447 { CKM_AES_ECB0x00001081UL, { 16, 32, CKF_EN_DE_WR_UN0x00000100UL | 0x00000200UL | 0x00020000UL | 0x00040000UL }, PR_TRUE1 },
448 { CKM_AES_CBC0x00001082UL, { 16, 32, CKF_EN_DE_WR_UN0x00000100UL | 0x00000200UL | 0x00020000UL | 0x00040000UL }, PR_TRUE1 },
449 { CKM_AES_MAC0x00001083UL, { 16, 32, CKF_SN_VR0x00000800UL | 0x00002000 }, PR_TRUE1 },
450 { CKM_AES_MAC_GENERAL0x00001084UL, { 16, 32, CKF_SN_VR0x00000800UL | 0x00002000 }, PR_TRUE1 },
451 { CKM_AES_CMAC0x0000108AUL, { 16, 32, CKF_SN_VR0x00000800UL | 0x00002000 }, PR_TRUE1 },
452 { CKM_AES_CMAC_GENERAL0x0000108BUL, { 16, 32, CKF_SN_VR0x00000800UL | 0x00002000 }, PR_TRUE1 },
453 { CKM_AES_CBC_PAD0x00001085UL, { 16, 32, CKF_EN_DE_WR_UN0x00000100UL | 0x00000200UL | 0x00020000UL | 0x00040000UL }, PR_TRUE1 },
454 { CKM_AES_CTS0x00001089UL, { 16, 32, CKF_EN_DE0x00000100UL | 0x00000200UL }, PR_TRUE1 },
455 { CKM_AES_CTR0x00001086UL, { 16, 32, CKF_EN_DE0x00000100UL | 0x00000200UL }, PR_TRUE1 },
456 { CKM_AES_GCM0x00001087UL, { 16, 32, CKF_EN_DE_MSG0x00000100UL | 0x00000200UL | 0x00000002UL | 0x00000004UL }, PR_TRUE1 },
457 { CKM_AES_XCBC_MAC_960x0000108DUL, { 12, 12, CKF_SN_VR0x00000800UL | 0x00002000 }, PR_TRUE1 },
458 { CKM_AES_XCBC_MAC0x0000108CUL, { 16, 16, CKF_SN_VR0x00000800UL | 0x00002000 }, PR_TRUE1 },
459 /* ------------------------- Camellia Operations --------------------- */
460 { CKM_CAMELLIA_KEY_GEN0x00000550UL, { 16, 32, CKF_GENERATE0x00008000UL }, PR_TRUE1 },
461 { CKM_CAMELLIA_ECB0x00000551UL, { 16, 32, CKF_EN_DE_WR_UN0x00000100UL | 0x00000200UL | 0x00020000UL | 0x00040000UL }, PR_TRUE1 },
462 { CKM_CAMELLIA_CBC0x00000552UL, { 16, 32, CKF_EN_DE_WR_UN0x00000100UL | 0x00000200UL | 0x00020000UL | 0x00040000UL }, PR_TRUE1 },
463 { CKM_CAMELLIA_MAC0x00000553UL, { 16, 32, CKF_SN_VR0x00000800UL | 0x00002000 }, PR_TRUE1 },
464 { CKM_CAMELLIA_MAC_GENERAL0x00000554UL, { 16, 32, CKF_SN_VR0x00000800UL | 0x00002000 }, PR_TRUE1 },
465 { CKM_CAMELLIA_CBC_PAD0x00000555UL, { 16, 32, CKF_EN_DE_WR_UN0x00000100UL | 0x00000200UL | 0x00020000UL | 0x00040000UL }, PR_TRUE1 },
466/* ------------------------- SEED Operations --------------------------- */
467#ifndef NSS_DISABLE_DEPRECATED_SEED
468 { CKM_SEED_KEY_GEN0x00000650UL, { 16, 16, CKF_GENERATE0x00008000UL }, PR_TRUE1 },
469 { CKM_SEED_ECB0x00000651UL, { 16, 16, CKF_EN_DE_WR_UN0x00000100UL | 0x00000200UL | 0x00020000UL | 0x00040000UL }, PR_TRUE1 },
470 { CKM_SEED_CBC0x00000652UL, { 16, 16, CKF_EN_DE_WR_UN0x00000100UL | 0x00000200UL | 0x00020000UL | 0x00040000UL }, PR_TRUE1 },
471 { CKM_SEED_MAC0x00000653UL, { 16, 16, CKF_SN_VR0x00000800UL | 0x00002000 }, PR_TRUE1 },
472 { CKM_SEED_MAC_GENERAL0x00000654UL, { 16, 16, CKF_SN_VR0x00000800UL | 0x00002000 }, PR_TRUE1 },
473 { CKM_SEED_CBC_PAD0x00000655UL, { 16, 16, CKF_EN_DE_WR_UN0x00000100UL | 0x00000200UL | 0x00020000UL | 0x00040000UL }, PR_TRUE1 },
474#endif
475/* ------------------------- ChaCha20 Operations ---------------------- */
476#ifndef NSS_DISABLE_CHACHAPOLY
477 { CKM_NSS_CHACHA20_KEY_GEN((0x80000000UL | 0x4E534350) + 27), { 32, 32, CKF_GENERATE0x00008000UL }, PR_TRUE1 },
478 { CKM_NSS_CHACHA20_POLY1305((0x80000000UL | 0x4E534350) + 28), { 32, 32, CKF_EN_DE0x00000100UL | 0x00000200UL }, PR_TRUE1 },
479 { CKM_NSS_CHACHA20_CTR((0x80000000UL | 0x4E534350) + 33), { 32, 32, CKF_EN_DE0x00000100UL | 0x00000200UL }, PR_TRUE1 },
480 { CKM_CHACHA20_KEY_GEN0x00001225UL, { 32, 32, CKF_GENERATE0x00008000UL }, PR_TRUE1 },
481 { CKM_CHACHA200x00001226UL, { 32, 32, CKF_EN_DE0x00000100UL | 0x00000200UL }, PR_TRUE1 },
482 { CKM_CHACHA20_POLY13050x00004021UL, { 32, 32, CKF_EN_DE_MSG0x00000100UL | 0x00000200UL | 0x00000002UL | 0x00000004UL }, PR_TRUE1 },
483#endif /* NSS_DISABLE_CHACHAPOLY */
484 /* ------------------------- Hashing Operations ----------------------- */
485 { CKM_MD20x00000200UL, { 0, 0, CKF_DIGEST0x00000400UL }, PR_FALSE0 },
486 { CKM_MD2_HMAC0x00000201UL, { 1, 128, CKF_SN_VR0x00000800UL | 0x00002000 }, PR_TRUE1 },
487 { CKM_MD2_HMAC_GENERAL0x00000202UL, { 1, 128, CKF_SN_VR0x00000800UL | 0x00002000 }, PR_TRUE1 },
488 { CKM_MD50x00000210UL, { 0, 0, CKF_DIGEST0x00000400UL }, PR_FALSE0 },
489 { CKM_MD5_HMAC0x00000211UL, { 1, 128, CKF_SN_VR0x00000800UL | 0x00002000 }, PR_TRUE1 },
490 { CKM_MD5_HMAC_GENERAL0x00000212UL, { 1, 128, CKF_SN_VR0x00000800UL | 0x00002000 }, PR_TRUE1 },
491 { CKM_SHA_10x00000220UL, { 0, 0, CKF_DIGEST0x00000400UL }, PR_FALSE0 },
492 { CKM_SHA_1_HMAC0x00000221UL, { 1, 128, CKF_SN_VR0x00000800UL | 0x00002000 }, PR_TRUE1 },
493 { CKM_SHA_1_HMAC_GENERAL0x00000222UL, { 1, 128, CKF_SN_VR0x00000800UL | 0x00002000 }, PR_TRUE1 },
494 { CKM_SHA2240x00000255UL, { 0, 0, CKF_DIGEST0x00000400UL }, PR_FALSE0 },
495 { CKM_SHA224_HMAC0x00000256UL, { 1, 128, CKF_SN_VR0x00000800UL | 0x00002000 }, PR_TRUE1 },
496 { CKM_SHA224_HMAC_GENERAL0x00000257UL, { 1, 128, CKF_SN_VR0x00000800UL | 0x00002000 }, PR_TRUE1 },
497 { CKM_SHA2560x00000250UL, { 0, 0, CKF_DIGEST0x00000400UL }, PR_FALSE0 },
498 { CKM_SHA256_HMAC0x00000251UL, { 1, 128, CKF_SN_VR0x00000800UL | 0x00002000 }, PR_TRUE1 },
499 { CKM_SHA256_HMAC_GENERAL0x00000252UL, { 1, 128, CKF_SN_VR0x00000800UL | 0x00002000 }, PR_TRUE1 },
500 { CKM_SHA3840x00000260UL, { 0, 0, CKF_DIGEST0x00000400UL }, PR_FALSE0 },
501 { CKM_SHA384_HMAC0x00000261UL, { 1, 128, CKF_SN_VR0x00000800UL | 0x00002000 }, PR_TRUE1 },
502 { CKM_SHA384_HMAC_GENERAL0x00000262UL, { 1, 128, CKF_SN_VR0x00000800UL | 0x00002000 }, PR_TRUE1 },
503 { CKM_SHA5120x00000270UL, { 0, 0, CKF_DIGEST0x00000400UL }, PR_FALSE0 },
504 { CKM_SHA512_HMAC0x00000271UL, { 1, 128, CKF_SN_VR0x00000800UL | 0x00002000 }, PR_TRUE1 },
505 { CKM_SHA512_HMAC_GENERAL0x00000272UL, { 1, 128, CKF_SN_VR0x00000800UL | 0x00002000 }, PR_TRUE1 },
506 { CKM_SHA3_2240x000002B5UL, { 0, 0, CKF_DIGEST0x00000400UL }, PR_FALSE0 },
507 { CKM_SHA3_224_HMAC0x000002B6UL, { 1, 128, CKF_SN_VR0x00000800UL | 0x00002000 }, PR_TRUE1 },
508 { CKM_SHA3_224_HMAC_GENERAL0x000002B7UL, { 1, 128, CKF_SN_VR0x00000800UL | 0x00002000 }, PR_TRUE1 },
509 { CKM_SHA3_2560x000002B0UL, { 0, 0, CKF_DIGEST0x00000400UL }, PR_FALSE0 },
510 { CKM_SHA3_256_HMAC0x000002B1UL, { 1, 128, CKF_SN_VR0x00000800UL | 0x00002000 }, PR_TRUE1 },
511 { CKM_SHA3_256_HMAC_GENERAL0x000002B2UL, { 1, 128, CKF_SN_VR0x00000800UL | 0x00002000 }, PR_TRUE1 },
512 { CKM_SHA3_3840x000002C0UL, { 0, 0, CKF_DIGEST0x00000400UL }, PR_FALSE0 },
513 { CKM_SHA3_384_HMAC0x000002C1UL, { 1, 128, CKF_SN_VR0x00000800UL | 0x00002000 }, PR_TRUE1 },
514 { CKM_SHA3_384_HMAC_GENERAL0x000002C2UL, { 1, 128, CKF_SN_VR0x00000800UL | 0x00002000 }, PR_TRUE1 },
515 { CKM_SHA3_5120x000002D0UL, { 0, 0, CKF_DIGEST0x00000400UL }, PR_FALSE0 },
516 { CKM_SHA3_512_HMAC0x000002D1UL, { 1, 128, CKF_SN_VR0x00000800UL | 0x00002000 }, PR_TRUE1 },
517 { CKM_SHA3_512_HMAC_GENERAL0x000002D2UL, { 1, 128, CKF_SN_VR0x00000800UL | 0x00002000 }, PR_TRUE1 },
518 { CKM_TLS_PRF_GENERAL0x80000373UL, { 0, 512, CKF_SN_VR0x00000800UL | 0x00002000 }, PR_FALSE0 },
519 { CKM_TLS_MAC0x000003E4UL, { 0, 512, CKF_SN_VR0x00000800UL | 0x00002000 }, PR_FALSE0 },
520 { CKM_NSS_TLS_PRF_GENERAL_SHA256((0x80000000UL | 0x4E534350) + 21),
521 { 0, 512, CKF_SN_VR0x00000800UL | 0x00002000 },
522 PR_FALSE0 },
523 /* ------------------------- HKDF Operations -------------------------- */
524 { CKM_HKDF_DERIVE0x0000402aUL, { 1, 255 * 64, CKF_DERIVE0x00080000UL }, PR_TRUE1 },
525 { CKM_HKDF_DATA0x0000402bUL, { 1, 255 * 64, CKF_DERIVE0x00080000UL }, PR_TRUE1 },
526 { CKM_HKDF_KEY_GEN0x0000402cUL, { 20, 64, CKF_GENERATE0x00008000UL }, PR_TRUE1 },
527 { CKM_NSS_HKDF_SHA1((0x80000000UL | 0x4E534350) + 3), { 1, 128, CKF_DERIVE0x00080000UL }, PR_TRUE1 },
528 { CKM_NSS_HKDF_SHA256((0x80000000UL | 0x4E534350) + 4), { 1, 128, CKF_DERIVE0x00080000UL }, PR_TRUE1 },
529 { CKM_NSS_HKDF_SHA384((0x80000000UL | 0x4E534350) + 5), { 1, 128, CKF_DERIVE0x00080000UL }, PR_TRUE1 },
530 { CKM_NSS_HKDF_SHA512((0x80000000UL | 0x4E534350) + 6), { 1, 128, CKF_DERIVE0x00080000UL }, PR_TRUE1 },
531/* ------------------------- CAST Operations --------------------------- */
532#ifdef NSS_SOFTOKEN_DOES_CAST
533 /* Cast operations are not supported ( yet? ) */
534 { CKM_CAST_KEY_GEN0x00000300UL, { 1, 8, CKF_GENERATE0x00008000UL }, PR_TRUE1 },
535 { CKM_CAST_ECB0x00000301UL, { 1, 8, CKF_EN_DE_WR_UN0x00000100UL | 0x00000200UL | 0x00020000UL | 0x00040000UL }, PR_TRUE1 },
536 { CKM_CAST_CBC0x00000302UL, { 1, 8, CKF_EN_DE_WR_UN0x00000100UL | 0x00000200UL | 0x00020000UL | 0x00040000UL }, PR_TRUE1 },
537 { CKM_CAST_MAC0x00000303UL, { 1, 8, CKF_SN_VR0x00000800UL | 0x00002000 }, PR_TRUE1 },
538 { CKM_CAST_MAC_GENERAL0x00000304UL, { 1, 8, CKF_SN_VR0x00000800UL | 0x00002000 }, PR_TRUE1 },
539 { CKM_CAST_CBC_PAD0x00000305UL, { 1, 8, CKF_EN_DE_WR_UN0x00000100UL | 0x00000200UL | 0x00020000UL | 0x00040000UL }, PR_TRUE1 },
540 { CKM_CAST3_KEY_GEN0x00000310UL, { 1, 16, CKF_GENERATE0x00008000UL }, PR_TRUE1 },
541 { CKM_CAST3_ECB0x00000311UL, { 1, 16, CKF_EN_DE_WR_UN0x00000100UL | 0x00000200UL | 0x00020000UL | 0x00040000UL }, PR_TRUE1 },
542 { CKM_CAST3_CBC0x00000312UL, { 1, 16, CKF_EN_DE_WR_UN0x00000100UL | 0x00000200UL | 0x00020000UL | 0x00040000UL }, PR_TRUE1 },
543 { CKM_CAST3_MAC0x00000313UL, { 1, 16, CKF_SN_VR0x00000800UL | 0x00002000 }, PR_TRUE1 },
544 { CKM_CAST3_MAC_GENERAL0x00000314UL, { 1, 16, CKF_SN_VR0x00000800UL | 0x00002000 }, PR_TRUE1 },
545 { CKM_CAST3_CBC_PAD0x00000315UL, { 1, 16, CKF_EN_DE_WR_UN0x00000100UL | 0x00000200UL | 0x00020000UL | 0x00040000UL }, PR_TRUE1 },
546 { CKM_CAST5_KEY_GEN0x00000320UL, { 1, 16, CKF_GENERATE0x00008000UL }, PR_TRUE1 },
547 { CKM_CAST5_ECB0x00000321UL, { 1, 16, CKF_EN_DE_WR_UN0x00000100UL | 0x00000200UL | 0x00020000UL | 0x00040000UL }, PR_TRUE1 },
548 { CKM_CAST5_CBC0x00000322UL, { 1, 16, CKF_EN_DE_WR_UN0x00000100UL | 0x00000200UL | 0x00020000UL | 0x00040000UL }, PR_TRUE1 },
549 { CKM_CAST5_MAC0x00000323UL, { 1, 16, CKF_SN_VR0x00000800UL | 0x00002000 }, PR_TRUE1 },
550 { CKM_CAST5_MAC_GENERAL0x00000324UL, { 1, 16, CKF_SN_VR0x00000800UL | 0x00002000 }, PR_TRUE1 },
551 { CKM_CAST5_CBC_PAD0x00000325UL, { 1, 16, CKF_EN_DE_WR_UN0x00000100UL | 0x00000200UL | 0x00020000UL | 0x00040000UL }, PR_TRUE1 },
552#endif
553#if NSS_SOFTOKEN_DOES_RC5
554 /* ------------------------- RC5 Operations --------------------------- */
555 { CKM_RC5_KEY_GEN0x00000330UL, { 1, 32, CKF_GENERATE0x00008000UL }, PR_TRUE1 },
556 { CKM_RC5_ECB0x00000331UL, { 1, 32, CKF_EN_DE_WR_UN0x00000100UL | 0x00000200UL | 0x00020000UL | 0x00040000UL }, PR_TRUE1 },
557 { CKM_RC5_CBC0x00000332UL, { 1, 32, CKF_EN_DE_WR_UN0x00000100UL | 0x00000200UL | 0x00020000UL | 0x00040000UL }, PR_TRUE1 },
558 { CKM_RC5_MAC0x00000333UL, { 1, 32, CKF_SN_VR0x00000800UL | 0x00002000 }, PR_TRUE1 },
559 { CKM_RC5_MAC_GENERAL0x00000334UL, { 1, 32, CKF_SN_VR0x00000800UL | 0x00002000 }, PR_TRUE1 },
560 { CKM_RC5_CBC_PAD0x00000335UL, { 1, 32, CKF_EN_DE_WR_UN0x00000100UL | 0x00000200UL | 0x00020000UL | 0x00040000UL }, PR_TRUE1 },
561#endif
562#ifdef NSS_SOFTOKEN_DOES_IDEA
563 /* ------------------------- IDEA Operations -------------------------- */
564 { CKM_IDEA_KEY_GEN0x00000340UL, { 16, 16, CKF_GENERATE0x00008000UL }, PR_TRUE1 },
565 { CKM_IDEA_ECB0x00000341UL, { 16, 16, CKF_EN_DE_WR_UN0x00000100UL | 0x00000200UL | 0x00020000UL | 0x00040000UL }, PR_TRUE1 },
566 { CKM_IDEA_CBC0x00000342UL, { 16, 16, CKF_EN_DE_WR_UN0x00000100UL | 0x00000200UL | 0x00020000UL | 0x00040000UL }, PR_TRUE1 },
567 { CKM_IDEA_MAC0x00000343UL, { 16, 16, CKF_SN_VR0x00000800UL | 0x00002000 }, PR_TRUE1 },
568 { CKM_IDEA_MAC_GENERAL0x00000344UL, { 16, 16, CKF_SN_VR0x00000800UL | 0x00002000 }, PR_TRUE1 },
569 { CKM_IDEA_CBC_PAD0x00000345UL, { 16, 16, CKF_EN_DE_WR_UN0x00000100UL | 0x00000200UL | 0x00020000UL | 0x00040000UL }, PR_TRUE1 },
570#endif
571 /* --------------------- Secret Key Operations ------------------------ */
572 { CKM_GENERIC_SECRET_KEY_GEN0x00000350UL, { 1, 32, CKF_GENERATE0x00008000UL }, PR_TRUE1 },
573 { CKM_CONCATENATE_BASE_AND_KEY0x00000360UL, { 1, 32, CKF_DERIVE0x00080000UL }, PR_FALSE0 },
574 { CKM_CONCATENATE_BASE_AND_DATA0x00000362UL, { 1, 32, CKF_DERIVE0x00080000UL }, PR_FALSE0 },
575 { CKM_CONCATENATE_DATA_AND_BASE0x00000363UL, { 1, 32, CKF_DERIVE0x00080000UL }, PR_FALSE0 },
576 { CKM_XOR_BASE_AND_DATA0x00000364UL, { 1, 32, CKF_DERIVE0x00080000UL }, PR_FALSE0 },
577 { CKM_EXTRACT_KEY_FROM_KEY0x00000365UL, { 1, 32, CKF_DERIVE0x00080000UL }, PR_FALSE0 },
578 { CKM_DES3_ECB_ENCRYPT_DATA0x00001102UL, { 1, 32, CKF_DERIVE0x00080000UL }, PR_FALSE0 },
579 { CKM_DES3_CBC_ENCRYPT_DATA0x00001103UL, { 1, 32, CKF_DERIVE0x00080000UL }, PR_FALSE0 },
580 { CKM_AES_ECB_ENCRYPT_DATA0x00001104UL, { 1, 32, CKF_DERIVE0x00080000UL }, PR_FALSE0 },
581 { CKM_AES_CBC_ENCRYPT_DATA0x00001105UL, { 1, 32, CKF_DERIVE0x00080000UL }, PR_FALSE0 },
582 { CKM_CAMELLIA_ECB_ENCRYPT_DATA0x00000556UL, { 1, 32, CKF_DERIVE0x00080000UL }, PR_FALSE0 },
583 { CKM_CAMELLIA_CBC_ENCRYPT_DATA0x00000557UL, { 1, 32, CKF_DERIVE0x00080000UL }, PR_FALSE0 },
584#ifndef NSS_DISABLE_DEPRECATED_SEED
585 { CKM_SEED_ECB_ENCRYPT_DATA0x00000656UL, { 1, 32, CKF_DERIVE0x00080000UL }, PR_FALSE0 },
586 { CKM_SEED_CBC_ENCRYPT_DATA0x00000657UL, { 1, 32, CKF_DERIVE0x00080000UL }, PR_FALSE0 },
587#endif
588 /* ---------------------- SSL Key Derivations ------------------------- */
589 { CKM_SSL3_PRE_MASTER_KEY_GEN0x00000370UL, { 48, 48, CKF_GENERATE0x00008000UL }, PR_FALSE0 },
590 { CKM_SSL3_MASTER_KEY_DERIVE0x00000371UL, { 48, 48, CKF_DERIVE0x00080000UL }, PR_FALSE0 },
591 { CKM_SSL3_MASTER_KEY_DERIVE_DH0x00000373UL, { 8, 128, CKF_DERIVE0x00080000UL }, PR_FALSE0 },
592 { CKM_SSL3_KEY_AND_MAC_DERIVE0x00000372UL, { 48, 48, CKF_DERIVE0x00080000UL }, PR_FALSE0 },
593 { CKM_SSL3_MD5_MAC0x00000380UL, { 0, 16, CKF_DERIVE0x00080000UL }, PR_FALSE0 },
594 { CKM_SSL3_SHA1_MAC0x00000381UL, { 0, 20, CKF_DERIVE0x00080000UL }, PR_FALSE0 },
595 { CKM_MD5_KEY_DERIVATION0x00000390UL, { 0, 16, CKF_DERIVE0x00080000UL }, PR_FALSE0 },
596 { CKM_MD2_KEY_DERIVATION0x00000391UL, { 0, 16, CKF_DERIVE0x00080000UL }, PR_FALSE0 },
597 { CKM_SHA1_KEY_DERIVATION0x00000392UL, { 0, 20, CKF_DERIVE0x00080000UL }, PR_FALSE0 },
598 { CKM_SHA224_KEY_DERIVATION0x00000396UL, { 0, 28, CKF_DERIVE0x00080000UL }, PR_FALSE0 },
599 { CKM_SHA256_KEY_DERIVATION0x00000393UL, { 0, 32, CKF_DERIVE0x00080000UL }, PR_FALSE0 },
600 { CKM_SHA384_KEY_DERIVATION0x00000394UL, { 0, 48, CKF_DERIVE0x00080000UL }, PR_FALSE0 },
601 { CKM_SHA512_KEY_DERIVATION0x00000395UL, { 0, 64, CKF_DERIVE0x00080000UL }, PR_FALSE0 },
602 { CKM_TLS_MASTER_KEY_DERIVE0x00000375UL, { 48, 48, CKF_DERIVE0x00080000UL }, PR_FALSE0 },
603 { CKM_TLS12_MASTER_KEY_DERIVE0x000003E0UL, { 48, 48, CKF_DERIVE0x00080000UL }, PR_FALSE0 },
604 { CKM_NSS_TLS_MASTER_KEY_DERIVE_SHA256((0x80000000UL | 0x4E534350) + 22),
605 { 48, 48, CKF_DERIVE0x00080000UL },
606 PR_FALSE0 },
607 { CKM_TLS_MASTER_KEY_DERIVE_DH0x00000377UL, { 8, 128, CKF_DERIVE0x00080000UL }, PR_FALSE0 },
608 { CKM_TLS12_MASTER_KEY_DERIVE_DH0x000003E2UL, { 8, 128, CKF_DERIVE0x00080000UL }, PR_FALSE0 },
609 { CKM_TLS12_EXTENDED_MASTER_KEY_DERIVE0x00000056UL, { 48, 128, CKF_DERIVE0x00080000UL }, PR_FALSE0 },
610 { CKM_TLS12_EXTENDED_MASTER_KEY_DERIVE_DH0x00000057UL, { 48, 128, CKF_DERIVE0x00080000UL }, PR_FALSE0 },
611 { CKM_NSS_TLS_MASTER_KEY_DERIVE_DH_SHA256((0x80000000UL | 0x4E534350) + 24),
612 { 8, 128, CKF_DERIVE0x00080000UL },
613 PR_FALSE0 },
614 { CKM_TLS_KEY_AND_MAC_DERIVE0x00000376UL, { 48, 48, CKF_DERIVE0x00080000UL }, PR_FALSE0 },
615 { CKM_TLS12_KEY_AND_MAC_DERIVE0x000003E1UL, { 48, 48, CKF_DERIVE0x00080000UL }, PR_FALSE0 },
616 { CKM_NSS_TLS_KEY_AND_MAC_DERIVE_SHA256((0x80000000UL | 0x4E534350) + 23),
617 { 48, 48, CKF_DERIVE0x00080000UL },
618 PR_FALSE0 },
619 { CKM_NSS_TLS_EXTENDED_MASTER_KEY_DERIVE((0x80000000UL | 0x4E534350) + 25),
620 { 48, 128, CKF_DERIVE0x00080000UL },
621 PR_FALSE0 },
622 { CKM_NSS_TLS_EXTENDED_MASTER_KEY_DERIVE_DH((0x80000000UL | 0x4E534350) + 26),
623 { 48, 128, CKF_DERIVE0x00080000UL },
624 PR_FALSE0 },
625 /* ---------------------- PBE Key Derivations ------------------------ */
626 { CKM_PBE_MD2_DES_CBC0x000003A0UL, { 8, 8, CKF_DERIVE0x00080000UL }, PR_TRUE1 },
627 { CKM_PBE_MD5_DES_CBC0x000003A1UL, { 8, 8, CKF_DERIVE0x00080000UL }, PR_TRUE1 },
628 /* ------------------ NSS PBE Key Derivations ------------------- */
629 { CKM_NSS_PBE_SHA1_DES_CBC0x80000002UL, { 8, 8, CKF_GENERATE0x00008000UL }, PR_TRUE1 },
630 { CKM_NSS_PBE_SHA1_FAULTY_3DES_CBC0x80000008UL, { 24, 24, CKF_GENERATE0x00008000UL }, PR_TRUE1 },
631 { CKM_PBE_SHA1_DES3_EDE_CBC0x000003A8UL, { 24, 24, CKF_GENERATE0x00008000UL }, PR_TRUE1 },
632 { CKM_PBE_SHA1_DES2_EDE_CBC0x000003A9UL, { 24, 24, CKF_GENERATE0x00008000UL }, PR_TRUE1 },
633 { CKM_PBE_SHA1_RC2_40_CBC0x000003ABUL, { 40, 40, CKF_GENERATE0x00008000UL }, PR_TRUE1 },
634 { CKM_PBE_SHA1_RC2_128_CBC0x000003AAUL, { 128, 128, CKF_GENERATE0x00008000UL }, PR_TRUE1 },
635 { CKM_PBE_SHA1_RC4_400x000003A7UL, { 40, 40, CKF_GENERATE0x00008000UL }, PR_TRUE1 },
636 { CKM_PBE_SHA1_RC4_1280x000003A6UL, { 128, 128, CKF_GENERATE0x00008000UL }, PR_TRUE1 },
637 { CKM_PBA_SHA1_WITH_SHA1_HMAC0x000003C0UL, { 20, 20, CKF_GENERATE0x00008000UL }, PR_TRUE1 },
638 { CKM_PKCS5_PBKD20x000003B0UL, { 1, 256, CKF_GENERATE0x00008000UL }, PR_TRUE1 },
639 { CKM_NSS_PBE_SHA1_HMAC_KEY_GEN0x80000009UL, { 20, 20, CKF_GENERATE0x00008000UL }, PR_TRUE1 },
640 { CKM_NSS_PBE_MD5_HMAC_KEY_GEN0x8000000aUL, { 16, 16, CKF_GENERATE0x00008000UL }, PR_TRUE1 },
641 { CKM_NSS_PBE_MD2_HMAC_KEY_GEN0x8000000bUL, { 16, 16, CKF_GENERATE0x00008000UL }, PR_TRUE1 },
642 { CKM_NSS_PKCS12_PBE_SHA224_HMAC_KEY_GEN((0x80000000UL | 0x4E534350) + 29), { 28, 28, CKF_GENERATE0x00008000UL }, PR_TRUE1 },
643 { CKM_NSS_PKCS12_PBE_SHA256_HMAC_KEY_GEN((0x80000000UL | 0x4E534350) + 30), { 32, 32, CKF_GENERATE0x00008000UL }, PR_TRUE1 },
644 { CKM_NSS_PKCS12_PBE_SHA384_HMAC_KEY_GEN((0x80000000UL | 0x4E534350) + 31), { 48, 48, CKF_GENERATE0x00008000UL }, PR_TRUE1 },
645 { CKM_NSS_PKCS12_PBE_SHA512_HMAC_KEY_GEN((0x80000000UL | 0x4E534350) + 32), { 64, 64, CKF_GENERATE0x00008000UL }, PR_TRUE1 },
646 /* ------------------ NIST 800-108 Key Derivations ------------------- */
647 { CKM_SP800_108_COUNTER_KDF0x000003acUL, { 0, CK_MAX0xffffffff, CKF_DERIVE0x00080000UL }, PR_TRUE1 },
648 { CKM_SP800_108_FEEDBACK_KDF0x000003adUL, { 0, CK_MAX0xffffffff, CKF_DERIVE0x00080000UL }, PR_TRUE1 },
649 { CKM_SP800_108_DOUBLE_PIPELINE_KDF0x000003aeUL, { 0, CK_MAX0xffffffff, CKF_DERIVE0x00080000UL }, PR_TRUE1 },
650 { CKM_NSS_SP800_108_COUNTER_KDF_DERIVE_DATA((0x80000000UL | 0x4E534350) + 42), { 0, CK_MAX0xffffffff, CKF_DERIVE0x00080000UL }, PR_TRUE1 },
651 { CKM_NSS_SP800_108_FEEDBACK_KDF_DERIVE_DATA((0x80000000UL | 0x4E534350) + 43), { 0, CK_MAX0xffffffff, CKF_DERIVE0x00080000UL }, PR_TRUE1 },
652 { CKM_NSS_SP800_108_DOUBLE_PIPELINE_KDF_DERIVE_DATA((0x80000000UL | 0x4E534350) + 44), { 0, CK_MAX0xffffffff, CKF_DERIVE0x00080000UL }, PR_TRUE1 },
653 /* ------------------ AES Key Wrap (also encrypt) ------------------- */
654 { CKM_NSS_AES_KEY_WRAP((0x80000000UL | 0x4E534350) + 1), { 16, 32, CKF_EN_DE_WR_UN0x00000100UL | 0x00000200UL | 0x00020000UL | 0x00040000UL }, PR_TRUE1 },
655 { CKM_NSS_AES_KEY_WRAP_PAD((0x80000000UL | 0x4E534350) + 2), { 16, 32, CKF_EN_DE_WR_UN0x00000100UL | 0x00000200UL | 0x00020000UL | 0x00040000UL }, PR_TRUE1 },
656 { CKM_AES_KEY_WRAP0x00002109UL, { 16, 32, CKF_EN_DE_WR_UN0x00000100UL | 0x00000200UL | 0x00020000UL | 0x00040000UL }, PR_TRUE1 },
657 { CKM_AES_KEY_WRAP_PAD0x0000210AUL, { 16, 32, CKF_EN_DE_WR_UN0x00000100UL | 0x00000200UL | 0x00020000UL | 0x00040000UL }, PR_TRUE1 },
658 { CKM_AES_KEY_WRAP_KWP0x0000210BUL, { 1, 32, CKF_EN_DE_WR_UN0x00000100UL | 0x00000200UL | 0x00020000UL | 0x00040000UL }, PR_TRUE1 },
659 /* --------------------------- J-PAKE -------------------------------- */
660 { CKM_NSS_JPAKE_ROUND1_SHA1((0x80000000UL | 0x4E534350) + 7), { 0, 0, CKF_GENERATE0x00008000UL }, PR_TRUE1 },
661 { CKM_NSS_JPAKE_ROUND1_SHA256((0x80000000UL | 0x4E534350) + 8), { 0, 0, CKF_GENERATE0x00008000UL }, PR_TRUE1 },
662 { CKM_NSS_JPAKE_ROUND1_SHA384((0x80000000UL | 0x4E534350) + 9), { 0, 0, CKF_GENERATE0x00008000UL }, PR_TRUE1 },
663 { CKM_NSS_JPAKE_ROUND1_SHA512((0x80000000UL | 0x4E534350) + 10), { 0, 0, CKF_GENERATE0x00008000UL }, PR_TRUE1 },
664 { CKM_NSS_JPAKE_ROUND2_SHA1((0x80000000UL | 0x4E534350) + 11), { 0, 0, CKF_DERIVE0x00080000UL }, PR_TRUE1 },
665 { CKM_NSS_JPAKE_ROUND2_SHA256((0x80000000UL | 0x4E534350) + 12), { 0, 0, CKF_DERIVE0x00080000UL }, PR_TRUE1 },
666 { CKM_NSS_JPAKE_ROUND2_SHA384((0x80000000UL | 0x4E534350) + 13), { 0, 0, CKF_DERIVE0x00080000UL }, PR_TRUE1 },
667 { CKM_NSS_JPAKE_ROUND2_SHA512((0x80000000UL | 0x4E534350) + 14), { 0, 0, CKF_DERIVE0x00080000UL }, PR_TRUE1 },
668 { CKM_NSS_JPAKE_FINAL_SHA1((0x80000000UL | 0x4E534350) + 15), { 0, 0, CKF_DERIVE0x00080000UL }, PR_TRUE1 },
669 { CKM_NSS_JPAKE_FINAL_SHA256((0x80000000UL | 0x4E534350) + 16), { 0, 0, CKF_DERIVE0x00080000UL }, PR_TRUE1 },
670 { CKM_NSS_JPAKE_FINAL_SHA384((0x80000000UL | 0x4E534350) + 17), { 0, 0, CKF_DERIVE0x00080000UL }, PR_TRUE1 },
671 { CKM_NSS_JPAKE_FINAL_SHA512((0x80000000UL | 0x4E534350) + 18), { 0, 0, CKF_DERIVE0x00080000UL }, PR_TRUE1 },
672 /* -------------------- Constant Time TLS MACs ----------------------- */
673 { CKM_NSS_HMAC_CONSTANT_TIME((0x80000000UL | 0x4E534350) + 19), { 0, 0, CKF_DIGEST0x00000400UL }, PR_TRUE1 },
674 { CKM_NSS_SSL3_MAC_CONSTANT_TIME((0x80000000UL | 0x4E534350) + 20), { 0, 0, CKF_DIGEST0x00000400UL }, PR_TRUE1 },
675 /* -------------------- IPSEC ----------------------- */
676 { CKM_IKE2_PRF_PLUS_DERIVE0x0000402eUL, { 8, 255 * 64, CKF_DERIVE0x00080000UL }, PR_TRUE1 },
677 { CKM_IKE_PRF_DERIVE0x0000402fUL, { 8, 64, CKF_DERIVE0x00080000UL }, PR_TRUE1 },
678 { CKM_IKE1_PRF_DERIVE0x00004030UL, { 8, 64, CKF_DERIVE0x00080000UL }, PR_TRUE1 },
679 { CKM_IKE1_EXTENDED_DERIVE0x00004031UL, { 8, 255 * 64, CKF_DERIVE0x00080000UL }, PR_TRUE1 },
680 { CKM_NSS_IKE_PRF_PLUS_DERIVE((0x80000000UL | 0x4E534350) + 34), { 8, 255 * 64, CKF_DERIVE0x00080000UL }, PR_TRUE1 },
681 { CKM_NSS_IKE_PRF_DERIVE((0x80000000UL | 0x4E534350) + 35), { 8, 64, CKF_DERIVE0x00080000UL }, PR_TRUE1 },
682 { CKM_NSS_IKE1_PRF_DERIVE((0x80000000UL | 0x4E534350) + 36), { 8, 64, CKF_DERIVE0x00080000UL }, PR_TRUE1 },
683 { CKM_NSS_IKE1_APP_B_PRF_DERIVE((0x80000000UL | 0x4E534350) + 37), { 8, 255 * 64, CKF_DERIVE0x00080000UL }, PR_TRUE1 },
684 /* -------------------- Kyber Operations ----------------------- */
685 { CKM_NSS_ML_KEM_KEY_PAIR_GEN((0x80000000UL | 0x4E534350) + 48), { MLKEM512_PUBLIC_KEY_BYTES800U, MLKEM1024_PUBLIC_KEY_BYTES1568U, CKF_GENERATE_KEY_PAIR0x00010000UL }, PR_TRUE1 },
686 { CKM_NSS_ML_KEM((0x80000000UL | 0x4E534350) + 49), { MLKEM512_PUBLIC_KEY_BYTES800U, MLKEM1024_PUBLIC_KEY_BYTES1568U, CKF_KEM0x10000000UL | 0x20000000UL }, PR_TRUE1 },
687 { CKM_ML_KEM_KEY_PAIR_GEN0x0000000fUL, { MLKEM512_PUBLIC_KEY_BYTES800U, MLKEM1024_PUBLIC_KEY_BYTES1568U, CKF_GENERATE_KEY_PAIR0x00010000UL }, PR_TRUE1 },
688 { CKM_ML_KEM0x00000017UL, { KYBER768_PUBLIC_KEY_BYTES1184U, MLKEM1024_PUBLIC_KEY_BYTES1568U, CKF_KEM0x10000000UL | 0x20000000UL }, PR_TRUE1 },
689 { CKM_ML_DSA_KEY_PAIR_GEN0x0000001cUL, { ML_DSA_44_PUBLICKEY_LEN1312, ML_DSA_87_PUBLICKEY_LEN2592, CKF_GENERATE0x00008000UL }, PR_TRUE1 },
690 { CKM_ML_DSA0x0000001dUL, { ML_DSA_44_PUBLICKEY_LEN1312, ML_DSA_87_PUBLICKEY_LEN2592, CKF_SN_VR0x00000800UL | 0x00002000 }, PR_TRUE1 },
691};
692static const CK_ULONG mechanismCount = sizeof(mechanisms) / sizeof(mechanisms[0]);
693
694/* sigh global so fipstokn can read it */
695PRBool nsc_init = PR_FALSE0;
696
697#if defined(CHECK_FORK_PTHREAD) || defined(CHECK_FORK_MIXED)
698
699#include <pthread.h>
700
701static void
702ForkedChild(void)
703{
704 if (nsc_init || nsf_init) {
705 forked = PR_TRUE1;
706 }
707}
708
709#endif
710
711#define SFTKFreeWrap(ctxtype, mmm)void SFTKFree_mmm(void *vp) { ctxtype *p = vp; mmm(p); } \
712 void SFTKFree_##mmm(void *vp) \
713 { \
714 ctxtype *p = vp; \
715 mmm(p); \
716 }
717
718SFTKFreeWrap(NSSLOWKEYPublicKey, nsslowkey_DestroyPublicKey)void SFTKFree_nsslowkey_DestroyPublicKey(void *vp) { NSSLOWKEYPublicKey
*p = vp; nsslowkey_DestroyPublicKey(p); }
;
719SFTKFreeWrap(NSSLOWKEYPrivateKey, nsslowkey_DestroyPrivateKey)void SFTKFree_nsslowkey_DestroyPrivateKey(void *vp) { NSSLOWKEYPrivateKey
*p = vp; nsslowkey_DestroyPrivateKey(p); }
;
720
721static char *
722sftk_setStringName(const char *inString, char *buffer, int buffer_length, PRBool nullTerminate)
723{
724 int full_length, string_length;
725
726 full_length = nullTerminate ? buffer_length - 1 : buffer_length;
727 string_length = PORT_Strlen(inString)strlen(inString);
728 /*
729 * shorten the string, respecting utf8 encoding
730 * to do so, we work backward from the end
731 * bytes looking from the end are either:
732 * - ascii [0x00,0x7f]
733 * - the [2-n]th byte of a multibyte sequence
734 * [0x3F,0xBF], i.e, most significant 2 bits are '10'
735 * - the first byte of a multibyte sequence [0xC0,0xFD],
736 * i.e, most significant 2 bits are '11'
737 *
738 * When the string is too long, we lop off any trailing '10' bytes,
739 * if any. When these are all eliminated we lop off
740 * one additional byte. Thus if we lopped any '10'
741 * we'll be lopping a '11' byte (the first byte of the multibyte sequence),
742 * otherwise we're lopping off an ascii character.
743 *
744 * To test for '10' bytes, we first AND it with
745 * 11000000 (0xc0) so that we get 10000000 (0x80) if and only if
746 * the byte starts with 10. We test for equality.
747 */
748 while (string_length > full_length) {
749 /* need to shorten */
750 while (string_length > 0 &&
751 ((inString[string_length - 1] & (char)0xc0) == (char)0x80)) {
752 /* lop off '10' byte */
753 string_length--;
754 }
755 /*
756 * test string_length in case bad data is received
757 * and string consisted of all '10' bytes,
758 * avoiding any infinite loop
759 */
760 if (string_length) {
761 /* remove either '11' byte or an asci byte */
762 string_length--;
763 }
764 }
765 PORT_Memsetmemset(buffer, ' ', full_length);
766 if (nullTerminate) {
767 buffer[full_length] = 0;
768 }
769 PORT_Memcpymemcpy(buffer, inString, string_length);
770 return buffer;
771}
772/*
773 * Configuration utils
774 */
775static CK_RV
776sftk_configure(const char *man, const char *libdes)
777{
778
779 /* make sure the internationalization was done correctly... */
780 if (man) {
781 manufacturerID = sftk_setStringName(man, manufacturerID_space,
782 sizeof(manufacturerID_space), PR_TRUE1);
783 }
784 if (libdes) {
785 libraryDescription = sftk_setStringName(libdes,
786 libraryDescription_space, sizeof(libraryDescription_space),
787 PR_TRUE1);
788 }
789
790 return CKR_OK0x00000000UL;
791}
792
793/*
794 * ******************** Password Utilities *******************************
795 */
796
797/*
798 * see if the key DB password is enabled
799 */
800static PRBool
801sftk_hasNullPassword(SFTKSlot *slot, SFTKDBHandle *keydb)
802{
803 PRBool pwenabled;
804
805 pwenabled = PR_FALSE0;
806 if (sftkdb_HasPasswordSet(keydb) == SECSuccess) {
807 PRBool tokenRemoved = PR_FALSE0;
808 SECStatus rv = sftkdb_CheckPasswordNull(keydb, &tokenRemoved);
809 if (tokenRemoved) {
810 sftk_CloseAllSessions(slot, PR_FALSE0);
811 }
812 return (rv == SECSuccess);
813 }
814
815 return pwenabled;
816}
817
818/*
819 * ******************** Object Creation Utilities ***************************
820 */
821
822/* Make sure a given attribute exists. If it doesn't, initialize it to
823 * value and len
824 */
825CK_RV
826sftk_defaultAttribute(SFTKObject *object, CK_ATTRIBUTE_TYPE type,
827 const void *value, unsigned int len)
828{
829 if (!sftk_hasAttribute(object, type)) {
830 return sftk_AddAttributeType(object, type, value, len);
831 }
832 return CKR_OK0x00000000UL;
833}
834
835/*
836 * check the consistancy and initialize a Data Object
837 */
838static CK_RV
839sftk_handleDataObject(SFTKSession *session, SFTKObject *object)
840{
841 CK_RV crv;
842
843 /* first reject private and token data objects */
844 if (sftk_isTrue(object, CKA_PRIVATE0x00000002UL) || sftk_isTrue(object, CKA_TOKEN0x00000001UL)) {
845 return CKR_ATTRIBUTE_VALUE_INVALID0x00000013UL;
846 }
847
848 /* now just verify the required date fields */
849 crv = sftk_defaultAttribute(object, CKA_APPLICATION0x00000010UL, NULL((void*)0), 0);
850 if (crv != CKR_OK0x00000000UL)
851 return crv;
852 crv = sftk_defaultAttribute(object, CKA_VALUE0x00000011UL, NULL((void*)0), 0);
853 if (crv != CKR_OK0x00000000UL)
854 return crv;
855
856 return CKR_OK0x00000000UL;
857}
858
859/*
860 * check the consistancy and initialize a Certificate Object
861 */
862static CK_RV
863sftk_handleCertObject(SFTKSession *session, SFTKObject *object)
864{
865 CK_CERTIFICATE_TYPE type;
866 SFTKAttribute *attribute;
867 CK_RV crv;
868
869 /* certificates must have a type */
870 if (!sftk_hasAttribute(object, CKA_CERTIFICATE_TYPE0x00000080UL)) {
871 return CKR_TEMPLATE_INCOMPLETE0x000000D0UL;
872 }
873
874 /* we can't store any certs private */
875 if (sftk_isTrue(object, CKA_PRIVATE0x00000002UL)) {
876 return CKR_ATTRIBUTE_VALUE_INVALID0x00000013UL;
877 }
878
879 /* We only support X.509 Certs for now */
880 attribute = sftk_FindAttribute(object, CKA_CERTIFICATE_TYPE0x00000080UL);
881 if (attribute == NULL((void*)0))
882 return CKR_TEMPLATE_INCOMPLETE0x000000D0UL;
883 type = *(CK_CERTIFICATE_TYPE *)attribute->attrib.pValue;
884 sftk_FreeAttribute(attribute);
885
886 if (type != CKC_X_5090x00000000UL) {
887 return CKR_ATTRIBUTE_VALUE_INVALID0x00000013UL;
888 }
889
890 /* X.509 Certificate */
891
892 /* make sure we have a cert */
893 if (!sftk_hasAttribute(object, CKA_VALUE0x00000011UL)) {
894 return CKR_TEMPLATE_INCOMPLETE0x000000D0UL;
895 }
896
897 /* in PKCS #11, Subject is a required field */
898 if (!sftk_hasAttribute(object, CKA_SUBJECT0x00000101UL)) {
899 return CKR_TEMPLATE_INCOMPLETE0x000000D0UL;
900 }
901
902 /* in PKCS #11, Issuer is a required field */
903 if (!sftk_hasAttribute(object, CKA_ISSUER0x00000081UL)) {
904 return CKR_TEMPLATE_INCOMPLETE0x000000D0UL;
905 }
906
907 /* in PKCS #11, Serial is a required field */
908 if (!sftk_hasAttribute(object, CKA_SERIAL_NUMBER0x00000082UL)) {
909 return CKR_TEMPLATE_INCOMPLETE0x000000D0UL;
910 }
911
912 /* add it to the object */
913 object->objectInfo = NULL((void*)0);
914 object->infoFree = (SFTKFree)NULL((void*)0);
915
916 /* now just verify the required date fields */
917 crv = sftk_defaultAttribute(object, CKA_ID0x00000102UL, NULL((void*)0), 0);
918 if (crv != CKR_OK0x00000000UL) {
919 return crv;
920 }
921
922 if (sftk_isTrue(object, CKA_TOKEN0x00000001UL)) {
923 SFTKSlot *slot = session->slot;
924 SFTKDBHandle *certHandle = sftk_getCertDB(slot);
925
926 if (certHandle == NULL((void*)0)) {
927 return CKR_TOKEN_WRITE_PROTECTED0x000000E2UL;
928 }
929
930 crv = sftkdb_write(certHandle, object, &object->handle);
931 sftk_freeDB(certHandle);
932 return crv;
933 }
934
935 return CKR_OK0x00000000UL;
936}
937
938/*
939 * check the consistancy and initialize a Trust Object
940 */
941static CK_RV
942sftk_handleNSSTrustObject(SFTKSession *session, SFTKObject *object)
943{
944 /* we can't store any certs private */
945 if (sftk_isTrue(object, CKA_PRIVATE0x00000002UL)) {
946 return CKR_ATTRIBUTE_VALUE_INVALID0x00000013UL;
947 }
948
949 /* certificates must have a type */
950 if (!sftk_hasAttribute(object, CKA_ISSUER0x00000081UL)) {
951 return CKR_TEMPLATE_INCOMPLETE0x000000D0UL;
952 }
953 if (!sftk_hasAttribute(object, CKA_SERIAL_NUMBER0x00000082UL)) {
954 return CKR_TEMPLATE_INCOMPLETE0x000000D0UL;
955 }
956 if (!sftk_hasAttribute(object, CKA_NSS_CERT_SHA1_HASH(((0x80000000UL | 0x4E534350) + 0x2000) + 100))) {
957 return CKR_TEMPLATE_INCOMPLETE0x000000D0UL;
958 }
959 if (!sftk_hasAttribute(object, CKA_NSS_CERT_MD5_HASH(((0x80000000UL | 0x4E534350) + 0x2000) + 101))) {
960 return CKR_TEMPLATE_INCOMPLETE0x000000D0UL;
961 }
962
963 if (sftk_isTrue(object, CKA_TOKEN0x00000001UL)) {
964 SFTKSlot *slot = session->slot;
965 SFTKDBHandle *certHandle = sftk_getCertDB(slot);
966 CK_RV crv;
967
968 if (certHandle == NULL((void*)0)) {
969 return CKR_TOKEN_WRITE_PROTECTED0x000000E2UL;
970 }
971
972 crv = sftkdb_write(certHandle, object, &object->handle);
973 sftk_freeDB(certHandle);
974 return crv;
975 }
976
977 return CKR_OK0x00000000UL;
978}
979
980/*
981 * check the consistancy and initialize a Trust Object
982 */
983static CK_RV
984sftk_handleTrustObject(SFTKSession *session, SFTKObject *object)
985{
986 /* we can't store any certs private */
987 if (sftk_isTrue(object, CKA_PRIVATE0x00000002UL)) {
988 return CKR_ATTRIBUTE_VALUE_INVALID0x00000013UL;
989 }
990
991 /* certificates must have a type */
992 if (!sftk_hasAttribute(object, CKA_ISSUER0x00000081UL)) {
993 return CKR_TEMPLATE_INCOMPLETE0x000000D0UL;
994 }
995 if (!sftk_hasAttribute(object, CKA_SERIAL_NUMBER0x00000082UL)) {
996 return CKR_TEMPLATE_INCOMPLETE0x000000D0UL;
997 }
998 if (!sftk_hasAttribute(object, CKA_HASH_OF_CERTIFICATE0x00000635UL)) {
999 return CKR_TEMPLATE_INCOMPLETE0x000000D0UL;
1000 }
1001 if (!sftk_hasAttribute(object, CKA_NAME_HASH_ALGORITHM0x0000008cUL)) {
1002 return CKR_TEMPLATE_INCOMPLETE0x000000D0UL;
1003 }
1004
1005 if (sftk_isTrue(object, CKA_TOKEN0x00000001UL)) {
1006 SFTKSlot *slot = session->slot;
1007 SFTKDBHandle *certHandle = sftk_getCertDB(slot);
1008 CK_RV crv;
1009
1010 if (certHandle == NULL((void*)0)) {
1011 return CKR_TOKEN_WRITE_PROTECTED0x000000E2UL;
1012 }
1013
1014 crv = sftkdb_write(certHandle, object, &object->handle);
1015 sftk_freeDB(certHandle);
1016 return crv;
1017 }
1018
1019 return CKR_OK0x00000000UL;
1020}
1021
1022/*
1023 * check the consistancy and initialize a Trust Object
1024 */
1025static CK_RV
1026sftk_handleSMimeObject(SFTKSession *session, SFTKObject *object)
1027{
1028
1029 /* we can't store any certs private */
1030 if (sftk_isTrue(object, CKA_PRIVATE0x00000002UL)) {
1031 return CKR_ATTRIBUTE_VALUE_INVALID0x00000013UL;
1032 }
1033
1034 /* certificates must have a type */
1035 if (!sftk_hasAttribute(object, CKA_SUBJECT0x00000101UL)) {
1036 return CKR_TEMPLATE_INCOMPLETE0x000000D0UL;
1037 }
1038 if (!sftk_hasAttribute(object, CKA_NSS_EMAIL((0x80000000UL | 0x4E534350) + 2))) {
1039 return CKR_TEMPLATE_INCOMPLETE0x000000D0UL;
1040 }
1041
1042 if (sftk_isTrue(object, CKA_TOKEN0x00000001UL)) {
1043 SFTKSlot *slot = session->slot;
1044 SFTKDBHandle *certHandle;
1045 CK_RV crv;
1046
1047 PORT_Assert(slot)((slot) ? ((void)0) : PR_Assert("slot", "/root/firefox-clang/security/nss/lib/softoken/pkcs11.c"
, 1047))
;
1048 if (slot == NULL((void*)0)) {
1049 return CKR_SESSION_HANDLE_INVALID0x000000B3UL;
1050 }
1051
1052 certHandle = sftk_getCertDB(slot);
1053 if (certHandle == NULL((void*)0)) {
1054 return CKR_TOKEN_WRITE_PROTECTED0x000000E2UL;
1055 }
1056
1057 crv = sftkdb_write(certHandle, object, &object->handle);
1058 sftk_freeDB(certHandle);
1059 return crv;
1060 }
1061
1062 return CKR_OK0x00000000UL;
1063}
1064
1065/*
1066 * check the consistancy and initialize a Trust Object
1067 */
1068static CK_RV
1069sftk_handleCrlObject(SFTKSession *session, SFTKObject *object)
1070{
1071
1072 /* we can't store any certs private */
1073 if (sftk_isTrue(object, CKA_PRIVATE0x00000002UL)) {
1074 return CKR_ATTRIBUTE_VALUE_INVALID0x00000013UL;
1075 }
1076
1077 /* certificates must have a type */
1078 if (!sftk_hasAttribute(object, CKA_SUBJECT0x00000101UL)) {
1079 return CKR_TEMPLATE_INCOMPLETE0x000000D0UL;
1080 }
1081 if (!sftk_hasAttribute(object, CKA_VALUE0x00000011UL)) {
1082 return CKR_TEMPLATE_INCOMPLETE0x000000D0UL;
1083 }
1084
1085 if (sftk_isTrue(object, CKA_TOKEN0x00000001UL)) {
1086 SFTKSlot *slot = session->slot;
1087 SFTKDBHandle *certHandle = sftk_getCertDB(slot);
1088 CK_RV crv;
1089
1090 if (certHandle == NULL((void*)0)) {
1091 return CKR_TOKEN_WRITE_PROTECTED0x000000E2UL;
1092 }
1093
1094 crv = sftkdb_write(certHandle, object, &object->handle);
1095 sftk_freeDB(certHandle);
1096 return crv;
1097 }
1098
1099 return CKR_OK0x00000000UL;
1100}
1101
1102/*
1103 * check the consistancy and initialize a Public Key Object
1104 */
1105static CK_RV
1106sftk_handlePublicKeyObject(SFTKSession *session, SFTKObject *object,
1107 CK_KEY_TYPE key_type)
1108{
1109 CK_BBOOL encrypt = CK_FALSE0;
1110 CK_BBOOL recover = CK_FALSE0;
1111 CK_BBOOL wrap = CK_FALSE0;
1112 CK_BBOOL derive = CK_FALSE0;
1113 CK_BBOOL verify = CK_FALSE0;
1114 CK_BBOOL encapsulate = CK_FALSE0;
1115 CK_ULONG paramSet = 0;
1116 CK_RV crv;
1117
1118 switch (key_type) {
1119 case CKK_RSA0x00000000UL:
1120 crv = sftk_ConstrainAttribute(object, CKA_MODULUS0x00000120UL,
1121 RSA_MIN_MODULUS_BITS128, 0, 0);
1122 if (crv != CKR_OK0x00000000UL) {
1123 return crv;
1124 }
1125 crv = sftk_ConstrainAttribute(object, CKA_PUBLIC_EXPONENT0x00000122UL, 2, 0, 0);
1126 if (crv != CKR_OK0x00000000UL) {
1127 return crv;
1128 }
1129 encrypt = CK_TRUE1;
1130 recover = CK_TRUE1;
1131 wrap = CK_TRUE1;
1132 verify = CK_TRUE1;
1133 break;
1134 case CKK_DSA0x00000001UL:
1135 crv = sftk_ConstrainAttribute(object, CKA_SUBPRIME0x00000131UL,
1136 DSA_MIN_Q_BITS160, DSA_MAX_Q_BITS256, 0);
1137 if (crv != CKR_OK0x00000000UL) {
1138 return crv;
1139 }
1140 crv = sftk_ConstrainAttribute(object, CKA_PRIME0x00000130UL,
1141 DSA_MIN_P_BITS512, DSA_MAX_P_BITS3072, 64);
1142 if (crv != CKR_OK0x00000000UL) {
1143 return crv;
1144 }
1145 crv = sftk_ConstrainAttribute(object, CKA_BASE0x00000132UL, 2, DSA_MAX_P_BITS3072, 0);
1146 if (crv != CKR_OK0x00000000UL) {
1147 return crv;
1148 }
1149 crv = sftk_ConstrainAttribute(object, CKA_VALUE0x00000011UL, 2, DSA_MAX_P_BITS3072, 0);
1150 if (crv != CKR_OK0x00000000UL) {
1151 return crv;
1152 }
1153 verify = CK_TRUE1;
1154 break;
1155 case CKK_DH0x00000002UL:
1156 crv = sftk_ConstrainAttribute(object, CKA_PRIME0x00000130UL,
1157 DH_MIN_P_BITS128, DH_MAX_P_BITS16384, 0);
1158 if (crv != CKR_OK0x00000000UL) {
1159 return crv;
1160 }
1161 crv = sftk_ConstrainAttribute(object, CKA_BASE0x00000132UL, 2, DH_MAX_P_BITS16384, 0);
1162 if (crv != CKR_OK0x00000000UL) {
1163 return crv;
1164 }
1165 crv = sftk_ConstrainAttribute(object, CKA_VALUE0x00000011UL, 2, DH_MAX_P_BITS16384, 0);
1166 if (crv != CKR_OK0x00000000UL) {
1167 return crv;
1168 }
1169 derive = CK_TRUE1;
1170 break;
1171 case CKK_EC_MONTGOMERY0x00000041UL:
1172 case CKK_EC_EDWARDS0x00000040UL:
1173 case CKK_EC0x00000003UL:
1174 if (!sftk_hasAttribute(object, CKA_EC_PARAMS0x00000180UL)) {
1175 return CKR_TEMPLATE_INCOMPLETE0x000000D0UL;
1176 }
1177 if (!sftk_hasAttribute(object, CKA_EC_POINT0x00000181UL)) {
1178 return CKR_TEMPLATE_INCOMPLETE0x000000D0UL;
1179 }
1180 /* for ECDSA and EDDSA. Change if the structure of any of them is modified. */
1181 derive = (key_type == CKK_EC_EDWARDS0x00000040UL) ? CK_FALSE0 : CK_TRUE1; /* CK_TRUE for ECDH */
1182 verify = (key_type == CKK_EC_MONTGOMERY0x00000041UL) ? CK_FALSE0 : CK_TRUE1; /* for ECDSA and EDDSA */
1183 break;
1184 case CKK_NSS_ML_KEM((0x80000000UL | 0x4E534350) + 6):
1185 case CKK_ML_KEM0x00000049UL:
1186 if (!sftk_hasAttribute(object, CKA_PARAMETER_SET0x0000061dUL)) {
1187 if (!sftk_hasAttribute(object, CKA_NSS_PARAMETER_SET((0x80000000UL | 0x4E534350) + 40))) {
1188 return CKR_TEMPLATE_INCOMPLETE0x000000D0UL;
1189 }
1190 }
1191 encapsulate = CK_TRUE1;
1192 break;
1193 case CKK_ML_DSA0x0000004aUL:
1194 if (!sftk_hasAttribute(object, CKA_PARAMETER_SET0x0000061dUL)) {
1195 return CKR_TEMPLATE_INCOMPLETE0x000000D0UL;
1196 }
1197 crv = sftk_GetULongAttribute(object, CKA_PARAMETER_SET0x0000061dUL,
1198 &paramSet);
1199 if (crv != CKR_OK0x00000000UL) {
1200 return crv;
1201 }
1202 if (sftk_MLDSAGetSigLen(paramSet) == 0) {
1203 return CKR_ATTRIBUTE_VALUE_INVALID0x00000013UL;
1204 }
1205 verify = CK_TRUE1;
1206 break;
1207 default:
1208 return CKR_ATTRIBUTE_VALUE_INVALID0x00000013UL;
1209 }
1210
1211 /* make sure the required fields exist */
1212 crv = sftk_defaultAttribute(object, CKA_SUBJECT0x00000101UL, NULL((void*)0), 0);
1213 if (crv != CKR_OK0x00000000UL)
1214 return crv;
1215 crv = sftk_defaultAttribute(object, CKA_ENCRYPT0x00000104UL, &encrypt, sizeof(CK_BBOOL));
1216 if (crv != CKR_OK0x00000000UL)
1217 return crv;
1218 crv = sftk_defaultAttribute(object, CKA_VERIFY0x0000010AUL, &verify, sizeof(CK_BBOOL));
1219 if (crv != CKR_OK0x00000000UL)
1220 return crv;
1221 crv = sftk_defaultAttribute(object, CKA_VERIFY_RECOVER0x0000010BUL,
1222 &recover, sizeof(CK_BBOOL));
1223 if (crv != CKR_OK0x00000000UL)
1224 return crv;
1225 crv = sftk_defaultAttribute(object, CKA_WRAP0x00000106UL, &wrap, sizeof(CK_BBOOL));
1226 if (crv != CKR_OK0x00000000UL)
1227 return crv;
1228 crv = sftk_defaultAttribute(object, CKA_DERIVE0x0000010CUL, &derive, sizeof(CK_BBOOL));
1229 if (crv != CKR_OK0x00000000UL)
1230 return crv;
1231 crv = sftk_defaultAttribute(object, CKA_ENCAPSULATE0x00000633UL, &encapsulate,
1232 sizeof(CK_BBOOL));
1233 if (crv != CKR_OK0x00000000UL)
1234 return crv;
1235 object->objectInfo = sftk_GetPubKey(object, key_type, &crv);
1236 if (object->objectInfo == NULL((void*)0)) {
1237 return crv;
1238 }
1239 object->infoFree = SFTKFree_nsslowkey_DestroyPublicKey;
1240
1241 /* Check that an imported EC key is valid */
1242 if (key_type == CKK_EC0x00000003UL || key_type == CKK_EC_EDWARDS0x00000040UL || key_type == CKK_EC_MONTGOMERY0x00000041UL) {
1243 NSSLOWKEYPublicKey *pubKey = (NSSLOWKEYPublicKey *)object->objectInfo;
1244 SECStatus rv = EC_ValidatePublicKey(&pubKey->u.ec.ecParams,
1245 &pubKey->u.ec.publicValue);
1246
1247 if (rv != SECSuccess) {
1248 return CKR_TEMPLATE_INCONSISTENT0x000000D1UL;
1249 }
1250 }
1251
1252 if (sftk_isTrue(object, CKA_TOKEN0x00000001UL)) {
1253 SFTKSlot *slot = session->slot;
1254 SFTKDBHandle *certHandle = sftk_getCertDB(slot);
1255
1256 if (certHandle == NULL((void*)0)) {
1257 return CKR_TOKEN_WRITE_PROTECTED0x000000E2UL;
1258 }
1259
1260 crv = sftkdb_write(certHandle, object, &object->handle);
1261 sftk_freeDB(certHandle);
1262 return crv;
1263 }
1264
1265 return CKR_OK0x00000000UL;
1266}
1267
1268static NSSLOWKEYPrivateKey *
1269sftk_mkPrivKey(SFTKObject *object, CK_KEY_TYPE key, CK_RV *rvp);
1270
1271static SECStatus
1272sftk_verifyRSAPrivateKey(SFTKObject *object, PRBool fillIfNeeded);
1273
1274/*
1275 * check the consistancy and initialize a Private Key Object
1276 */
1277static CK_RV
1278sftk_handlePrivateKeyObject(SFTKSession *session, SFTKObject *object, CK_KEY_TYPE key_type)
1279{
1280 CK_BBOOL cktrue = CK_TRUE1;
1281 CK_BBOOL encrypt = CK_FALSE0;
1282 CK_BBOOL sign = CK_FALSE0;
1283 CK_BBOOL recover = CK_FALSE0;
1284 CK_BBOOL wrap = CK_FALSE0;
1285 CK_BBOOL derive = CK_FALSE0;
1286 CK_BBOOL decapsulate = CK_FALSE0;
1287 CK_BBOOL ckfalse = CK_FALSE0;
1288 CK_ULONG paramSet = 0;
1289 PRBool createObjectInfo = PR_TRUE1;
1290 PRBool fillPrivateKey = PR_FALSE0;
1291 int missing_rsa_mod_component = 0;
1292 int missing_rsa_exp_component = 0;
1293 int missing_rsa_crt_component = 0;
1294
1295 SECItem mod;
1296 CK_RV crv;
1297 SECStatus rv;
1298
1299 switch (key_type) {
1300 case CKK_RSA0x00000000UL:
1301 if (!sftk_hasAttribute(object, CKA_MODULUS0x00000120UL)) {
1302 missing_rsa_mod_component++;
1303 }
1304 if (!sftk_hasAttribute(object, CKA_PUBLIC_EXPONENT0x00000122UL)) {
1305 missing_rsa_exp_component++;
1306 }
1307 if (!sftk_hasAttribute(object, CKA_PRIVATE_EXPONENT0x00000123UL)) {
1308 missing_rsa_exp_component++;
1309 }
1310 if (!sftk_hasAttribute(object, CKA_PRIME_10x00000124UL)) {
1311 missing_rsa_mod_component++;
1312 }
1313 if (!sftk_hasAttribute(object, CKA_PRIME_20x00000125UL)) {
1314 missing_rsa_mod_component++;
1315 }
1316 if (!sftk_hasAttribute(object, CKA_EXPONENT_10x00000126UL)) {
1317 missing_rsa_crt_component++;
1318 }
1319 if (!sftk_hasAttribute(object, CKA_EXPONENT_20x00000127UL)) {
1320 missing_rsa_crt_component++;
1321 }
1322 if (!sftk_hasAttribute(object, CKA_COEFFICIENT0x00000128UL)) {
1323 missing_rsa_crt_component++;
1324 }
1325 if (missing_rsa_mod_component || missing_rsa_exp_component ||
1326 missing_rsa_crt_component) {
1327 /* we are missing a component, see if we have enough to rebuild
1328 * the rest */
1329 int have_exp = 2 - missing_rsa_exp_component;
1330 int have_component = 5 -
1331 (missing_rsa_exp_component + missing_rsa_mod_component);
1332
1333 if ((have_exp == 0) || (have_component < 3)) {
1334 /* nope, not enough to reconstruct the private key */
1335 return CKR_TEMPLATE_INCOMPLETE0x000000D0UL;
1336 }
1337 fillPrivateKey = PR_TRUE1;
1338 }
1339 /*verify the parameters for consistency*/
1340 rv = sftk_verifyRSAPrivateKey(object, fillPrivateKey);
1341 if (rv != SECSuccess) {
1342 return CKR_TEMPLATE_INCOMPLETE0x000000D0UL;
1343 }
1344
1345 /* make sure Netscape DB attribute is set correctly */
1346 crv = sftk_Attribute2SSecItem(NULL((void*)0), &mod, object, CKA_MODULUS0x00000120UL);
1347 if (crv != CKR_OK0x00000000UL)
1348 return crv;
1349 crv = sftk_forceAttribute(object, CKA_NSS_DB0xD5A0DB00L,
1350 sftk_item_expand(&mod)(&mod)->data, (&mod)->len);
1351 if (mod.data)
1352 SECITEM_ZfreeItemSECITEM_ZfreeItem_Util(&mod, PR_FALSE0);
1353 if (crv != CKR_OK0x00000000UL)
1354 return crv;
1355
1356 sign = CK_TRUE1;
1357 wrap = CK_TRUE1;
1358 encrypt = CK_TRUE1;
1359 break;
1360 case CKK_DSA0x00000001UL:
1361 if (!sftk_hasAttribute(object, CKA_SUBPRIME0x00000131UL)) {
1362 return CKR_TEMPLATE_INCOMPLETE0x000000D0UL;
1363 }
1364 if (!sftk_hasAttribute(object, CKA_PRIME0x00000130UL)) {
1365 return CKR_TEMPLATE_INCOMPLETE0x000000D0UL;
1366 }
1367 if (!sftk_hasAttribute(object, CKA_BASE0x00000132UL)) {
1368 return CKR_TEMPLATE_INCOMPLETE0x000000D0UL;
1369 }
1370 if (!sftk_hasAttribute(object, CKA_VALUE0x00000011UL)) {
1371 return CKR_TEMPLATE_INCOMPLETE0x000000D0UL;
1372 }
1373 sign = CK_TRUE1;
1374 break;
1375 case CKK_DH0x00000002UL:
1376 if (!sftk_hasAttribute(object, CKA_PRIME0x00000130UL)) {
1377 return CKR_TEMPLATE_INCOMPLETE0x000000D0UL;
1378 }
1379 if (!sftk_hasAttribute(object, CKA_BASE0x00000132UL)) {
1380 return CKR_TEMPLATE_INCOMPLETE0x000000D0UL;
1381 }
1382 if (!sftk_hasAttribute(object, CKA_VALUE0x00000011UL)) {
1383 return CKR_TEMPLATE_INCOMPLETE0x000000D0UL;
1384 }
1385 /* allow subprime to be set after the fact */
1386 crv = sftk_defaultAttribute(object, CKA_SUBPRIME0x00000131UL, NULL((void*)0), 0);
1387 if (crv != CKR_OK0x00000000UL) {
1388 return crv;
1389 }
1390 derive = CK_TRUE1;
1391 break;
1392 case CKK_EC0x00000003UL:
1393 case CKK_EC_EDWARDS0x00000040UL:
1394 case CKK_EC_MONTGOMERY0x00000041UL:
1395 if (!sftk_hasAttribute(object, CKA_EC_PARAMS0x00000180UL)) {
1396 return CKR_TEMPLATE_INCOMPLETE0x000000D0UL;
1397 }
1398 if (!sftk_hasAttribute(object, CKA_VALUE0x00000011UL)) {
1399 return CKR_TEMPLATE_INCOMPLETE0x000000D0UL;
1400 }
1401 /* for ECDSA and EDDSA. Change if the structure of any of them is modified. */
1402 derive = (key_type == CKK_EC_EDWARDS0x00000040UL) ? CK_FALSE0 : CK_TRUE1; /* CK_TRUE for ECDH */
1403 sign = (key_type == CKK_EC_MONTGOMERY0x00000041UL) ? CK_FALSE0 : CK_TRUE1; /* for ECDSA and EDDSA */
1404 break;
1405 case CKK_NSS_JPAKE_ROUND1((0x80000000UL | 0x4E534350) + 2):
1406 if (!sftk_hasAttribute(object, CKA_PRIME0x00000130UL) ||
1407 !sftk_hasAttribute(object, CKA_SUBPRIME0x00000131UL) ||
1408 !sftk_hasAttribute(object, CKA_BASE0x00000132UL)) {
1409 return CKR_TEMPLATE_INCOMPLETE0x000000D0UL;
1410 }
1411 /* fall through */
1412 case CKK_NSS_JPAKE_ROUND2((0x80000000UL | 0x4E534350) + 3):
1413 /* CKA_NSS_JPAKE_SIGNERID and CKA_NSS_JPAKE_PEERID are checked in
1414 the J-PAKE code. */
1415 derive = CK_TRUE1;
1416 createObjectInfo = PR_FALSE0;
1417 break;
1418 case CKK_NSS_ML_KEM((0x80000000UL | 0x4E534350) + 6):
1419 case CKK_ML_KEM0x00000049UL:
1420 if (!sftk_hasAttribute(object, CKA_KEY_TYPE0x00000100UL)) {
1421 return CKR_TEMPLATE_INCOMPLETE0x000000D0UL;
1422 }
1423 if (!sftk_hasAttribute(object, CKA_VALUE0x00000011UL) &&
1424 !sftk_hasAttribute(object, CKA_SEED0x00000637UL)) {
1425 return CKR_TEMPLATE_INCOMPLETE0x000000D0UL;
1426 }
1427 crv = sftk_GetULongAttribute(object, CKA_PARAMETER_SET0x0000061dUL,
1428 &paramSet);
1429 if (crv != CKR_OK0x00000000UL) {
1430 crv = sftk_GetULongAttribute(object, CKA_NSS_PARAMETER_SET((0x80000000UL | 0x4E534350) + 40),
1431 &paramSet);
1432 if (crv != CKR_OK0x00000000UL) {
1433 return crv;
1434 }
1435 }
1436
1437 KyberParams kyberParams = sftk_kyber_PK11ParamToInternal(paramSet);
1438 if (kyberParams == params_kyber_invalid) {
1439 return CKR_ATTRIBUTE_VALUE_INVALID0x00000013UL;
1440 }
1441 /*
1442 * if we have a seedm deal with making sure CKA_SEED and
1443 * CKA_VALUE are consistant. We skip this step if the CKA_SEED and
1444 * CKA_VALUE were generated together by us. */
1445 if (sftk_hasAttribute(object, CKA_SEED0x00000637UL)) {
1446 PRBool seedOK = sftk_hasAttribute(object, CKA_NSS_SEED_OK((0x80000000UL | 0x4E534350) + 41));
1447 SFTKAttribute *seedAttribute = sftk_FindAttribute(object,
1448 CKA_SEED0x00000637UL);
1449 PORT_Assert(seedAttribute)((seedAttribute) ? ((void)0) : PR_Assert("seedAttribute", "/root/firefox-clang/security/nss/lib/softoken/pkcs11.c"
, 1449))
;
1450 crv = CKR_OK0x00000000UL;
1451 if (seedAttribute->attrib.ulValueLen != 0) {
1452 SFTKAttribute *valueAttribute =
1453 sftk_FindAttribute(object, CKA_VALUE0x00000011UL);
1454 CK_ULONG valueLen = valueAttribute ? valueAttribute->attrib.ulValueLen : 0;
1455 if (!seedOK || valueLen == 0) {
1456 SECItem privKey = { siBuffer, NULL((void*)0), 0 };
1457 SECItem pubKey = { siBuffer, NULL((void*)0), 0 };
1458 SECItem seed = { siBuffer, NULL((void*)0), 0 };
1459
1460 seed.data = seedAttribute->attrib.pValue;
1461 seed.len = (int)seedAttribute->attrib.ulValueLen;
1462
1463 if (!sftk_kyber_AllocPrivKeyItem(kyberParams, &privKey)) {
1464 crv = CKR_HOST_MEMORY0x00000002UL;
1465 goto kyber_loser;
1466 }
1467 if (!sftk_kyber_AllocPubKeyItem(kyberParams, &pubKey)) {
1468 crv = CKR_HOST_MEMORY0x00000002UL;
1469 goto kyber_loser;
1470 }
1471 rv = Kyber_NewKey(kyberParams, &seed, &privKey, &pubKey);
1472 if (rv != SECSuccess) {
1473 crv = CKR_ATTRIBUTE_VALUE_INVALID0x00000013UL;
1474 } else if (valueLen == 0) {
1475 crv = sftk_forceAttribute(object, CKA_VALUE0x00000011UL,
1476 privKey.data,
1477 privKey.len);
1478 } else {
1479 /* we have the value, so we must verify it */
1480 PORT_Assert(!seedOK)((!seedOK) ? ((void)0) : PR_Assert("!seedOK", "/root/firefox-clang/security/nss/lib/softoken/pkcs11.c"
, 1480))
;
1481 if ((privKey.len != valueLen) ||
1482 (PORT_Memcmpmemcmp(valueAttribute->attrib.pValue,
1483 privKey.data, valueLen) != 0)) {
1484 crv = CKR_ATTRIBUTE_VALUE_INVALID0x00000013UL;
1485 }
1486 }
1487 kyber_loser:
1488 SECITEM_ZfreeItemSECITEM_ZfreeItem_Util(&privKey, PR_FALSE0);
1489 SECITEM_ZfreeItemSECITEM_ZfreeItem_Util(&pubKey, PR_FALSE0);
1490 /* seed is cleared when we free seedAttribute */
1491 }
1492 if (valueAttribute)
1493 sftk_FreeAttribute(valueAttribute);
1494 }
1495 sftk_FreeAttribute(seedAttribute);
1496 if (crv != CKR_OK0x00000000UL) {
1497 return crv;
1498 }
1499 }
1500 sftk_DeleteAttributeType(object, CKA_NSS_SEED_OK((0x80000000UL | 0x4E534350) + 41));
1501 /* if we got this far, we should have a CKA_VALUE, either by
1502 * one given to us, or by it being generated above */
1503 if (!sftk_hasAttribute(object, CKA_VALUE0x00000011UL)) {
1504 return CKR_TEMPLATE_INCOMPLETE0x000000D0UL;
1505 }
1506 decapsulate = CK_TRUE1;
1507 break;
1508 case CKK_ML_DSA0x0000004aUL:
1509 if (!sftk_hasAttribute(object, CKA_KEY_TYPE0x00000100UL)) {
1510 return CKR_TEMPLATE_INCOMPLETE0x000000D0UL;
1511 }
1512 /* make sure we have a CKA_PARAMETER_SET */
1513 if (!sftk_hasAttribute(object, CKA_PARAMETER_SET0x0000061dUL)) {
1514 return CKR_TEMPLATE_INCOMPLETE0x000000D0UL;
1515 }
1516 /* make sure it's one we understand */
1517 crv = sftk_GetULongAttribute(object, CKA_PARAMETER_SET0x0000061dUL,
1518 &paramSet);
1519 if (crv != CKR_OK0x00000000UL) {
1520 return crv;
1521 }
1522 if (sftk_MLDSAGetSigLen(paramSet) == 0) {
1523 return CKR_ATTRIBUTE_VALUE_INVALID0x00000013UL;
1524 }
1525 /*
1526 * if we have a seed deal with making sure seed and
1527 * CKA_VALUE . We skip this step if the SEED and VALUE
1528 * was generated together by us. */
1529 if (sftk_hasAttribute(object, CKA_SEED0x00000637UL)) {
1530 PRBool seedOK = sftk_hasAttribute(object, CKA_NSS_SEED_OK((0x80000000UL | 0x4E534350) + 41));
1531 SFTKAttribute *seedAttribute = sftk_FindAttribute(object,
1532 CKA_SEED0x00000637UL);
1533 PORT_Assert(seedAttribute)((seedAttribute) ? ((void)0) : PR_Assert("seedAttribute", "/root/firefox-clang/security/nss/lib/softoken/pkcs11.c"
, 1533))
;
1534 crv = CKR_OK0x00000000UL;
1535 if (seedAttribute->attrib.ulValueLen != 0) {
1536 SFTKAttribute *valueAttribute =
1537 sftk_FindAttribute(object, CKA_VALUE0x00000011UL);
1538 CK_ULONG valueLen = valueAttribute ? valueAttribute->attrib.ulValueLen : 0;
1539 if (!seedOK || valueLen == 0) {
1540 MLDSAPrivateKey privKey;
1541 MLDSAPublicKey pubKey;
1542 SECItem seedItem;
1543
1544 seedItem.data = seedAttribute->attrib.pValue;
1545 seedItem.len = seedAttribute->attrib.ulValueLen;
1546 rv = MLDSA_NewKey(paramSet, &seedItem, &privKey,
1547 &pubKey);
1548 if (rv != SECSuccess) {
1549 crv = CKR_ATTRIBUTE_VALUE_INVALID0x00000013UL;
1550 } else if (valueLen == 0) {
1551 crv = sftk_forceAttribute(object, CKA_VALUE0x00000011UL,
1552 privKey.keyVal,
1553 privKey.keyValLen);
1554 } else {
1555 /* we have the value, so we must need to
1556 * verify it */
1557 PORT_Assert(!seedOK)((!seedOK) ? ((void)0) : PR_Assert("!seedOK", "/root/firefox-clang/security/nss/lib/softoken/pkcs11.c"
, 1557))
;
1558 if ((privKey.keyValLen != valueLen) ||
1559 (PORT_Memcmpmemcmp(valueAttribute->attrib.pValue,
1560 privKey.keyVal, valueLen) != 0)) {
1561 crv = CKR_ATTRIBUTE_VALUE_INVALID0x00000013UL;
1562 }
1563 }
1564 PORT_SafeZero(&privKey, sizeof(privKey));
1565 PORT_SafeZero(&pubKey, sizeof(pubKey));
1566 }
1567 if (valueAttribute)
1568 sftk_FreeAttribute(valueAttribute);
1569 }
1570 sftk_FreeAttribute(seedAttribute);
1571 if (crv != CKR_OK0x00000000UL) {
1572 return crv;
1573 }
1574 }
1575 sftk_DeleteAttributeType(object, CKA_NSS_SEED_OK((0x80000000UL | 0x4E534350) + 41));
1576 /* if we got this far, we should have a CKA_VALUE, either but
1577 * one given to us, or by it being generated above */
1578 if (!sftk_hasAttribute(object, CKA_VALUE0x00000011UL)) {
1579 return CKR_TEMPLATE_INCOMPLETE0x000000D0UL;
1580 }
1581 sign = CK_TRUE1;
1582 break;
1583
1584 default:
1585 return CKR_ATTRIBUTE_VALUE_INVALID0x00000013UL;
1586 }
1587 crv = sftk_defaultAttribute(object, CKA_SUBJECT0x00000101UL, NULL((void*)0), 0);
1588 if (crv != CKR_OK0x00000000UL)
1589 return crv;
1590 crv = sftk_defaultAttribute(object, CKA_SENSITIVE0x00000103UL, &cktrue, sizeof(CK_BBOOL));
1591 if (crv != CKR_OK0x00000000UL)
1592 return crv;
1593 crv = sftk_defaultAttribute(object, CKA_EXTRACTABLE0x00000162UL, &cktrue, sizeof(CK_BBOOL));
1594 if (crv != CKR_OK0x00000000UL)
1595 return crv;
1596 crv = sftk_defaultAttribute(object, CKA_DECRYPT0x00000105UL, &encrypt, sizeof(CK_BBOOL));
1597 if (crv != CKR_OK0x00000000UL)
1598 return crv;
1599 crv = sftk_defaultAttribute(object, CKA_SIGN0x00000108UL, &sign, sizeof(CK_BBOOL));
1600 if (crv != CKR_OK0x00000000UL)
1601 return crv;
1602 crv = sftk_defaultAttribute(object, CKA_SIGN_RECOVER0x00000109UL, &recover,
1603 sizeof(CK_BBOOL));
1604 if (crv != CKR_OK0x00000000UL)
1605 return crv;
1606 crv = sftk_defaultAttribute(object, CKA_UNWRAP0x00000107UL, &wrap, sizeof(CK_BBOOL));
1607 if (crv != CKR_OK0x00000000UL)
1608 return crv;
1609 crv = sftk_defaultAttribute(object, CKA_DERIVE0x0000010CUL, &derive, sizeof(CK_BBOOL));
1610 if (crv != CKR_OK0x00000000UL)
1611 return crv;
1612 crv = sftk_defaultAttribute(object, CKA_DECAPSULATE0x00000634UL, &decapsulate,
1613 sizeof(CK_BBOOL));
1614 if (crv != CKR_OK0x00000000UL)
1615 return crv;
1616 /* the next two bits get modified only in the key gen and token cases */
1617 crv = sftk_forceAttribute(object, CKA_ALWAYS_SENSITIVE0x00000165UL,
1618 &ckfalse, sizeof(CK_BBOOL));
1619 if (crv != CKR_OK0x00000000UL)
1620 return crv;
1621 crv = sftk_forceAttribute(object, CKA_NEVER_EXTRACTABLE0x00000164UL,
1622 &ckfalse, sizeof(CK_BBOOL));
1623 if (crv != CKR_OK0x00000000UL)
1624 return crv;
1625
1626 /* should we check the non-token RSA private keys? */
1627
1628 if (sftk_isTrue(object, CKA_TOKEN0x00000001UL)) {
1629 SFTKSlot *slot = session->slot;
1630 SFTKDBHandle *keyHandle = sftk_getKeyDB(slot);
1631
1632 if (keyHandle == NULL((void*)0)) {
1633 return CKR_TOKEN_WRITE_PROTECTED0x000000E2UL;
1634 }
1635
1636 crv = sftkdb_write(keyHandle, object, &object->handle);
1637 sftk_freeDB(keyHandle);
1638 return crv;
1639 } else if (createObjectInfo) {
1640 object->objectInfo = sftk_mkPrivKey(object, key_type, &crv);
1641 if (object->objectInfo == NULL((void*)0))
1642 return crv;
1643 object->infoFree = SFTKFree_nsslowkey_DestroyPrivateKey;
1644 }
1645 return CKR_OK0x00000000UL;
1646}
1647
1648/* forward declare the DES formating function for handleSecretKey */
1649void sftk_FormatDESKey(unsigned char *key, int length);
1650
1651/* Validate secret key data, and set defaults */
1652static CK_RV
1653validateSecretKey(SFTKSession *session, SFTKObject *object,
1654 CK_KEY_TYPE key_type, PRBool isFIPS)
1655{
1656 CK_RV crv;
1657 CK_BBOOL cktrue = CK_TRUE1;
1658 CK_BBOOL ckfalse = CK_FALSE0;
1659 SFTKAttribute *attribute = NULL((void*)0);
1660 unsigned long requiredLen;
1661
1662 crv = sftk_defaultAttribute(object, CKA_SENSITIVE0x00000103UL,
1663 isFIPS ? &cktrue : &ckfalse, sizeof(CK_BBOOL));
1664 if (crv != CKR_OK0x00000000UL)
1665 return crv;
1666 crv = sftk_defaultAttribute(object, CKA_EXTRACTABLE0x00000162UL,
1667 &cktrue, sizeof(CK_BBOOL));
1668 if (crv != CKR_OK0x00000000UL)
1669 return crv;
1670 crv = sftk_defaultAttribute(object, CKA_ENCRYPT0x00000104UL, &cktrue, sizeof(CK_BBOOL));
1671 if (crv != CKR_OK0x00000000UL)
1672 return crv;
1673 crv = sftk_defaultAttribute(object, CKA_DECRYPT0x00000105UL, &cktrue, sizeof(CK_BBOOL));
1674 if (crv != CKR_OK0x00000000UL)
1675 return crv;
1676 crv = sftk_defaultAttribute(object, CKA_SIGN0x00000108UL, &ckfalse, sizeof(CK_BBOOL));
1677 if (crv != CKR_OK0x00000000UL)
1678 return crv;
1679 crv = sftk_defaultAttribute(object, CKA_VERIFY0x0000010AUL, &ckfalse, sizeof(CK_BBOOL));
1680 if (crv != CKR_OK0x00000000UL)
1681 return crv;
1682 crv = sftk_defaultAttribute(object, CKA_WRAP0x00000106UL, &cktrue, sizeof(CK_BBOOL));
1683 if (crv != CKR_OK0x00000000UL)
1684 return crv;
1685 crv = sftk_defaultAttribute(object, CKA_UNWRAP0x00000107UL, &cktrue, sizeof(CK_BBOOL));
1686 if (crv != CKR_OK0x00000000UL)
1687 return crv;
1688
1689 if (!sftk_hasAttribute(object, CKA_VALUE0x00000011UL)) {
1690 return CKR_TEMPLATE_INCOMPLETE0x000000D0UL;
1691 }
1692 /* the next two bits get modified only in the key gen and token cases */
1693 crv = sftk_forceAttribute(object, CKA_ALWAYS_SENSITIVE0x00000165UL,
1694 &ckfalse, sizeof(CK_BBOOL));
1695 if (crv != CKR_OK0x00000000UL)
1696 return crv;
1697 crv = sftk_forceAttribute(object, CKA_NEVER_EXTRACTABLE0x00000164UL,
1698 &ckfalse, sizeof(CK_BBOOL));
1699 if (crv != CKR_OK0x00000000UL)
1700 return crv;
1701
1702 /* some types of keys have a value length */
1703 crv = CKR_OK0x00000000UL;
1704 switch (key_type) {
1705 /* force CKA_VALUE_LEN to be set */
1706 case CKK_GENERIC_SECRET0x00000010UL:
1707 case CKK_RC20x00000011UL:
1708 case CKK_RC40x00000012UL:
1709#if NSS_SOFTOKEN_DOES_RC5
1710 case CKK_RC50x00000019UL:
1711#endif
1712#ifdef NSS_SOFTOKEN_DOES_CAST
1713 case CKK_CAST0x00000016UL:
1714 case CKK_CAST30x00000017UL:
1715 case CKK_CAST50x00000018UL:
1716#endif
1717#if NSS_SOFTOKEN_DOES_IDEA
1718 case CKK_IDEA0x0000001AUL:
1719#endif
1720 attribute = sftk_FindAttribute(object, CKA_VALUE0x00000011UL);
1721 /* shouldn't happen */
1722 if (attribute == NULL((void*)0))
1723 return CKR_TEMPLATE_INCOMPLETE0x000000D0UL;
1724 crv = sftk_forceAttribute(object, CKA_VALUE_LEN0x00000161UL,
1725 &attribute->attrib.ulValueLen, sizeof(CK_ULONG));
1726 sftk_FreeAttribute(attribute);
1727 break;
1728 /* force the value to have the correct parity */
1729 case CKK_DES0x00000013UL:
1730 case CKK_DES20x00000014UL:
1731 case CKK_DES30x00000015UL:
1732 case CKK_CDMF0x0000001EUL:
1733 attribute = sftk_FindAttribute(object, CKA_VALUE0x00000011UL);
1734 /* shouldn't happen */
1735 if (attribute == NULL((void*)0))
1736 return CKR_TEMPLATE_INCOMPLETE0x000000D0UL;
1737 requiredLen = sftk_MapKeySize(key_type);
1738 if (attribute->attrib.ulValueLen != requiredLen) {
1739 sftk_FreeAttribute(attribute);
1740 return CKR_KEY_SIZE_RANGE0x00000062UL;
1741 }
1742 /* sftk_FindAttribute returns a private copy, so set the parity
1743 * bits on it and then write the value back to the object. */
1744 sftk_FormatDESKey((unsigned char *)attribute->attrib.pValue,
1745 attribute->attrib.ulValueLen);
1746 crv = sftk_forceAttribute(object, CKA_VALUE0x00000011UL,
1747 attribute->attrib.pValue,
1748 attribute->attrib.ulValueLen);
1749 sftk_FreeAttribute(attribute);
1750 if (crv != CKR_OK0x00000000UL)
1751 return crv;
1752 break;
1753 case CKK_AES0x0000001FUL:
1754 attribute = sftk_FindAttribute(object, CKA_VALUE0x00000011UL);
1755 /* shouldn't happen */
1756 if (attribute == NULL((void*)0))
1757 return CKR_TEMPLATE_INCOMPLETE0x000000D0UL;
1758 if (attribute->attrib.ulValueLen != 16 &&
1759 attribute->attrib.ulValueLen != 24 &&
1760 attribute->attrib.ulValueLen != 32) {
1761 sftk_FreeAttribute(attribute);
1762 return CKR_KEY_SIZE_RANGE0x00000062UL;
1763 }
1764 crv = sftk_forceAttribute(object, CKA_VALUE_LEN0x00000161UL,
1765 &attribute->attrib.ulValueLen, sizeof(CK_ULONG));
1766 sftk_FreeAttribute(attribute);
1767 break;
1768 default:
1769 break;
1770 }
1771
1772 return crv;
1773}
1774
1775/*
1776 * check the consistancy and initialize a Secret Key Object
1777 */
1778static CK_RV
1779sftk_handleSecretKeyObject(SFTKSession *session, SFTKObject *object,
1780 CK_KEY_TYPE key_type, PRBool isFIPS)
1781{
1782 CK_RV crv;
1783
1784 /* First validate and set defaults */
1785 crv = validateSecretKey(session, object, key_type, isFIPS);
1786 if (crv != CKR_OK0x00000000UL)
1787 goto loser;
1788
1789 /* If the object is a TOKEN object, store in the database */
1790 if (sftk_isTrue(object, CKA_TOKEN0x00000001UL)) {
1791 SFTKSlot *slot = session->slot;
1792 SFTKDBHandle *keyHandle = sftk_getKeyDB(slot);
1793
1794 if (keyHandle == NULL((void*)0)) {
1795 return CKR_TOKEN_WRITE_PROTECTED0x000000E2UL;
1796 }
1797
1798 crv = sftkdb_write(keyHandle, object, &object->handle);
1799 sftk_freeDB(keyHandle);
1800 return crv;
1801 }
1802
1803loser:
1804
1805 return crv;
1806}
1807
1808/*
1809 * check the consistancy and initialize a Key Object
1810 */
1811static CK_RV
1812sftk_handleKeyObject(SFTKSession *session, SFTKObject *object)
1813{
1814 SFTKAttribute *attribute;
1815 CK_KEY_TYPE key_type;
1816 CK_BBOOL ckfalse = CK_FALSE0;
1817 CK_RV crv;
1818
1819 /* verify the required fields */
1820 if (!sftk_hasAttribute(object, CKA_KEY_TYPE0x00000100UL)) {
1821 return CKR_TEMPLATE_INCOMPLETE0x000000D0UL;
1822 }
1823
1824 /* now verify the common fields */
1825 crv = sftk_defaultAttribute(object, CKA_ID0x00000102UL, NULL((void*)0), 0);
1826 if (crv != CKR_OK0x00000000UL)
1827 return crv;
1828 crv = sftk_defaultAttribute(object, CKA_START_DATE0x00000110UL, NULL((void*)0), 0);
1829 if (crv != CKR_OK0x00000000UL)
1830 return crv;
1831 crv = sftk_defaultAttribute(object, CKA_END_DATE0x00000111UL, NULL((void*)0), 0);
1832 if (crv != CKR_OK0x00000000UL)
1833 return crv;
1834 /* CKA_DERIVE is common to all keys, but it's default value is
1835 * key dependent */
1836 crv = sftk_defaultAttribute(object, CKA_LOCAL0x00000163UL, &ckfalse, sizeof(CK_BBOOL));
1837 if (crv != CKR_OK0x00000000UL)
1838 return crv;
1839
1840 /* get the key type */
1841 attribute = sftk_FindAttribute(object, CKA_KEY_TYPE0x00000100UL);
1842 if (!attribute) {
1843 return CKR_ATTRIBUTE_VALUE_INVALID0x00000013UL;
1844 }
1845 key_type = *(CK_KEY_TYPE *)attribute->attrib.pValue;
1846 sftk_FreeAttribute(attribute);
1847
1848 switch (object->objclass) {
1849 case CKO_PUBLIC_KEY0x00000002UL:
1850 return sftk_handlePublicKeyObject(session, object, key_type);
1851 case CKO_PRIVATE_KEY0x00000003UL:
1852 return sftk_handlePrivateKeyObject(session, object, key_type);
1853 case CKO_SECRET_KEY0x00000004UL:
1854 /* make sure the required fields exist */
1855 return sftk_handleSecretKeyObject(session, object, key_type,
1856 (PRBool)(sftk_isFIPS(session->slot->slotID)(((session->slot->slotID) == 3) || ((session->slot->
slotID) >= 101))
));
1857 default:
1858 break;
1859 }
1860 return CKR_ATTRIBUTE_VALUE_INVALID0x00000013UL;
1861}
1862
1863/*
1864 * check the consistancy and Verify a DSA Parameter Object
1865 */
1866static CK_RV
1867sftk_handleDSAParameterObject(SFTKSession *session, SFTKObject *object)
1868{
1869 SFTKAttribute *primeAttr = NULL((void*)0);
1870 SFTKAttribute *subPrimeAttr = NULL((void*)0);
1871 SFTKAttribute *baseAttr = NULL((void*)0);
1872 SFTKAttribute *seedAttr = NULL((void*)0);
1873 SFTKAttribute *hAttr = NULL((void*)0);
1874 SFTKAttribute *attribute;
1875 CK_RV crv = CKR_TEMPLATE_INCOMPLETE0x000000D0UL;
1876 PQGParams params;
1877 PQGVerify vfy, *verify = NULL((void*)0);
1878 SECStatus result, rv;
1879 /* This bool keeps track of whether or not we need verify parameters.
1880 * If a P, Q and G or supplied, we dont' need verify parameters, as we
1881 * have PQ and G.
1882 * - If G is not supplied, the presumption is that we want to
1883 * verify P and Q only.
1884 * - If counter is supplied, it is presumed we want to verify PQ because
1885 * the counter is only used in verification.
1886 * - If H is supplied, is is presumed we want to verify G because H is
1887 * only used to verify G.
1888 * - Any verification step must have the SEED (counter or H could be
1889 * missing depending on exactly what we want to verify). If SEED is supplied,
1890 * the code just goes ahead and runs verify (other errors are parameter
1891 * errors are detected by the PQG_VerifyParams function). If SEED is not
1892 * supplied, but we determined that we are trying to verify (because needVfy
1893 * is set, go ahead and return CKR_TEMPLATE_INCOMPLETE.
1894 */
1895 PRBool needVfy = PR_FALSE0;
1896
1897 primeAttr = sftk_FindAttribute(object, CKA_PRIME0x00000130UL);
1898 if (primeAttr == NULL((void*)0))
1899 goto loser;
1900 params.prime.data = primeAttr->attrib.pValue;
1901 params.prime.len = primeAttr->attrib.ulValueLen;
1902
1903 subPrimeAttr = sftk_FindAttribute(object, CKA_SUBPRIME0x00000131UL);
1904 if (subPrimeAttr == NULL((void*)0))
1905 goto loser;
1906 params.subPrime.data = subPrimeAttr->attrib.pValue;
1907 params.subPrime.len = subPrimeAttr->attrib.ulValueLen;
1908
1909 baseAttr = sftk_FindAttribute(object, CKA_BASE0x00000132UL);
1910 if (baseAttr != NULL((void*)0)) {
1911 params.base.data = baseAttr->attrib.pValue;
1912 params.base.len = baseAttr->attrib.ulValueLen;
1913 } else {
1914 params.base.data = NULL((void*)0);
1915 params.base.len = 0;
1916 needVfy = PR_TRUE1; /* presumably only including PQ so we can verify
1917 * them. */
1918 }
1919
1920 attribute = sftk_FindAttribute(object, CKA_NSS_PQG_COUNTER((0x80000000UL | 0x4E534350) + 20));
1921 if (attribute != NULL((void*)0)) {
1922 vfy.counter = *(CK_ULONG *)attribute->attrib.pValue;
1923 sftk_FreeAttribute(attribute);
1924 needVfy = PR_TRUE1; /* included a count so we can verify PQ */
1925 } else {
1926 vfy.counter = -1;
1927 }
1928
1929 hAttr = sftk_FindAttribute(object, CKA_NSS_PQG_H((0x80000000UL | 0x4E534350) + 22));
1930 if (hAttr != NULL((void*)0)) {
1931 vfy.h.data = hAttr->attrib.pValue;
1932 vfy.h.len = hAttr->attrib.ulValueLen;
1933 needVfy = PR_TRUE1; /* included H so we can verify G */
1934 } else {
1935 vfy.h.data = NULL((void*)0);
1936 vfy.h.len = 0;
1937 }
1938 seedAttr = sftk_FindAttribute(object, CKA_NSS_PQG_SEED((0x80000000UL | 0x4E534350) + 21));
1939 if (seedAttr != NULL((void*)0)) {
1940 vfy.seed.data = seedAttr->attrib.pValue;
1941 vfy.seed.len = seedAttr->attrib.ulValueLen;
1942
1943 verify = &vfy;
1944 } else if (needVfy) {
1945 goto loser; /* Verify always needs seed, if we need verify and not seed
1946 * then fail */
1947 }
1948
1949 crv = CKR_FUNCTION_FAILED0x00000006UL;
1950 rv = PQG_VerifyParams(&params, verify, &result);
1951 if (rv == SECSuccess) {
1952 crv = (result == SECSuccess) ? CKR_OK0x00000000UL : CKR_ATTRIBUTE_VALUE_INVALID0x00000013UL;
1953 }
1954
1955loser:
1956 if (hAttr)
1957 sftk_FreeAttribute(hAttr);
1958 if (seedAttr)
1959 sftk_FreeAttribute(seedAttr);
1960 if (baseAttr)
1961 sftk_FreeAttribute(baseAttr);
1962 if (subPrimeAttr)
1963 sftk_FreeAttribute(subPrimeAttr);
1964 if (primeAttr)
1965 sftk_FreeAttribute(primeAttr);
1966
1967 return crv;
1968}
1969
1970/*
1971 * check the consistancy and initialize a Key Parameter Object
1972 */
1973static CK_RV
1974sftk_handleKeyParameterObject(SFTKSession *session, SFTKObject *object)
1975{
1976 SFTKAttribute *attribute;
1977 CK_KEY_TYPE key_type;
1978 CK_BBOOL ckfalse = CK_FALSE0;
1979 CK_RV crv;
1980
1981 /* verify the required fields */
1982 if (!sftk_hasAttribute(object, CKA_KEY_TYPE0x00000100UL)) {
1983 return CKR_TEMPLATE_INCOMPLETE0x000000D0UL;
1984 }
1985
1986 /* now verify the common fields */
1987 crv = sftk_defaultAttribute(object, CKA_LOCAL0x00000163UL, &ckfalse, sizeof(CK_BBOOL));
1988 if (crv != CKR_OK0x00000000UL)
1989 return crv;
1990
1991 /* get the key type */
1992 attribute = sftk_FindAttribute(object, CKA_KEY_TYPE0x00000100UL);
1993 if (!attribute) {
1994 return CKR_ATTRIBUTE_VALUE_INVALID0x00000013UL;
1995 }
1996 key_type = *(CK_KEY_TYPE *)attribute->attrib.pValue;
1997 sftk_FreeAttribute(attribute);
1998
1999 switch (key_type) {
2000 case CKK_DSA0x00000001UL:
2001 return sftk_handleDSAParameterObject(session, object);
2002
2003 default:
2004 break;
2005 }
2006 return CKR_KEY_TYPE_INCONSISTENT0x00000063UL;
2007}
2008
2009/*
2010 * Handle Object does all the object consistancy checks, automatic attribute
2011 * generation, attribute defaulting, etc. If handleObject succeeds, the object
2012 * will be assigned an object handle, and the object installed in the session
2013 * or stored in the DB.
2014 */
2015CK_RV
2016sftk_handleObject(SFTKObject *object, SFTKSession *session)
2017{
2018 SFTKSlot *slot = session->slot;
2019 SFTKAttribute *attribute;
2020 CK_BBOOL ckfalse = CK_FALSE0;
2021 CK_BBOOL cktrue = CK_TRUE1;
2022 PRBool isLoggedIn, needLogin;
2023 CK_RV crv;
2024
2025 /* make sure all the base object types are defined. If not set the
2026 * defaults */
2027 crv = sftk_defaultAttribute(object, CKA_TOKEN0x00000001UL, &ckfalse, sizeof(CK_BBOOL));
2028 if (crv != CKR_OK0x00000000UL)
2029 return crv;
2030 crv = sftk_defaultAttribute(object, CKA_PRIVATE0x00000002UL, &ckfalse, sizeof(CK_BBOOL));
2031 if (crv != CKR_OK0x00000000UL)
2032 return crv;
2033 crv = sftk_defaultAttribute(object, CKA_LABEL0x00000003UL, NULL((void*)0), 0);
2034 if (crv != CKR_OK0x00000000UL)
2035 return crv;
2036 crv = sftk_defaultAttribute(object, CKA_MODIFIABLE0x00000170UL, &cktrue, sizeof(CK_BBOOL));
2037 if (crv != CKR_OK0x00000000UL)
2038 return crv;
2039
2040 PR_Lock(slot->slotLock);
2041 isLoggedIn = slot->isLoggedIn;
2042 needLogin = slot->needLogin;
2043 PR_Unlock(slot->slotLock);
2044
2045 /* don't create a private object if we aren't logged in */
2046 if (!isLoggedIn && needLogin && sftk_isTrue(object, CKA_PRIVATE0x00000002UL)) {
2047 return CKR_USER_NOT_LOGGED_IN0x00000101UL;
2048 }
2049
2050 if (((session->info.flags & CKF_RW_SESSION0x00000002UL) == 0) &&
2051 (sftk_isTrue(object, CKA_TOKEN0x00000001UL))) {
2052 return CKR_SESSION_READ_ONLY0x000000B5UL;
2053 }
2054
2055 /* Assign a unique SESSION object handle to every new object,
2056 * whether it is a session object or a token object.
2057 * At this point, all new objects are structured as session objects.
2058 * Objects with the CKA_TOKEN attribute true will be turned into
2059 * token objects and will have a token object handle assigned to
2060 * them by the handler for each object class, invoked below.
2061 *
2062 * It may be helpful to note/remember that
2063 * sftk_narrowToXxxObject uses the object's handle and type field,
2064 * but sftk_isTrue(...,CKA_TOKEN) examines the CKA_TOKEN attribute.
2065 */
2066 object->handle = sftk_getNextHandle(slot);
2067
2068 /* get the object class */
2069 attribute = sftk_FindAttribute(object, CKA_CLASS0x00000000UL);
2070 if (attribute == NULL((void*)0)) {
2071 return CKR_TEMPLATE_INCOMPLETE0x000000D0UL;
2072 }
2073 object->objclass = *(CK_OBJECT_CLASS *)attribute->attrib.pValue;
2074 sftk_FreeAttribute(attribute);
2075
2076 /* Now handle the specific object class.
2077 * At this point, all objects are session objects, and the session
2078 * number must be passed to the object class handlers.
2079 */
2080 switch (object->objclass) {
2081 case CKO_DATA0x00000000UL:
2082 crv = sftk_handleDataObject(session, object);
2083 break;
2084 case CKO_CERTIFICATE0x00000001UL:
2085 crv = sftk_handleCertObject(session, object);
2086 break;
2087 case CKO_NSS_TRUST((0x80000000UL | 0x4E534350) + 3):
2088 crv = sftk_handleNSSTrustObject(session, object);
2089 break;
2090 case CKO_TRUST0x0000000bUL:
2091 crv = sftk_handleTrustObject(session, object);
2092 break;
2093 case CKO_NSS_CRL((0x80000000UL | 0x4E534350) + 1):
2094 crv = sftk_handleCrlObject(session, object);
2095 break;
2096 case CKO_NSS_SMIME((0x80000000UL | 0x4E534350) + 2):
2097 crv = sftk_handleSMimeObject(session, object);
2098 break;
2099 case CKO_PRIVATE_KEY0x00000003UL:
2100 case CKO_PUBLIC_KEY0x00000002UL:
2101 case CKO_SECRET_KEY0x00000004UL:
2102 crv = sftk_handleKeyObject(session, object);
2103 break;
2104 case CKO_DOMAIN_PARAMETERS0x00000006UL:
2105 crv = sftk_handleKeyParameterObject(session, object);
2106 break;
2107 default:
2108 crv = CKR_ATTRIBUTE_VALUE_INVALID0x00000013UL;
2109 break;
2110 }
2111
2112 /* can't fail from here on out unless the pk_handlXXX functions have
2113 * failed the request */
2114 if (crv != CKR_OK0x00000000UL) {
2115 return crv;
2116 }
2117
2118 /* Now link the object into the slot and session structures.
2119 * If the object has a true CKA_TOKEN attribute, the above object
2120 * class handlers will have set the sign bit in the object handle,
2121 * causing the following test to be true. We still need to set the
2122 * object type.
2123 */
2124 if (sftk_isToken(object->handle)(((object->handle) & 0x80000000L) == 0x80000000L)) {
2125 object->type = SFTK_TOKEN_OBJECT_TYPE0x00000000U;
2126 return sftk_convertSessionToToken(object);
2127 }
2128
2129 object->type = SFTK_SESSION_OBJECT_TYPE0xFFFFFFFFU;
2130 object->slot = slot;
2131 sftk_AddObject(session, object);
2132 return CKR_OK0x00000000UL;
2133}
2134
2135/*
2136 * ******************** Public Key Utilities ***************************
2137 */
2138/* Generate a low public key structure from an object */
2139NSSLOWKEYPublicKey *
2140sftk_GetPubKey(SFTKObject *object, CK_KEY_TYPE key_type,
2141 CK_RV *crvp)
2142{
2143 NSSLOWKEYPublicKey *pubKey;
2144 PLArenaPool *arena;
2145 CK_ULONG paramSet;
2146 CK_RV crv;
2147
2148 if (object->objclass != CKO_PUBLIC_KEY0x00000002UL) {
2149 *crvp = CKR_KEY_TYPE_INCONSISTENT0x00000063UL;
2150 return NULL((void*)0);
2151 }
2152
2153 if (sftk_isToken(object->handle)(((object->handle) & 0x80000000L) == 0x80000000L)) {
2154 /* ferret out the token object handle */
2155 }
2156
2157 /* If we already have a key, use it */
2158 if (object->objectInfo) {
2159 *crvp = CKR_OK0x00000000UL;
2160 return (NSSLOWKEYPublicKey *)object->objectInfo;
2161 }
2162
2163 /* allocate the structure */
2164 arena = PORT_NewArenaPORT_NewArena_Util(DER_DEFAULT_CHUNKSIZE(2048));
2165 if (arena == NULL((void*)0)) {
2166 *crvp = CKR_HOST_MEMORY0x00000002UL;
2167 return NULL((void*)0);
2168 }
2169
2170 pubKey = (NSSLOWKEYPublicKey *)
2171 PORT_ArenaAllocPORT_ArenaAlloc_Util(arena, sizeof(NSSLOWKEYPublicKey));
2172 if (pubKey == NULL((void*)0)) {
2173 PORT_FreeArenaPORT_FreeArena_Util(arena, PR_FALSE0);
2174 *crvp = CKR_HOST_MEMORY0x00000002UL;
2175 return NULL((void*)0);
2176 }
2177
2178 /* fill in the structure */
2179 pubKey->arena = arena;
2180 switch (key_type) {
2181 case CKK_RSA0x00000000UL:
2182 pubKey->keyType = NSSLOWKEYRSAKey;
2183 /* if we claim the object is FIPS, then make sure it's verified
2184 * before we do an OAEP operation */
2185 pubKey->u.rsa.needVerify = sftk_hasFIPS(object);
2186 crv = sftk_Attribute2SSecItem(arena, &pubKey->u.rsa.modulus,
2187 object, CKA_MODULUS0x00000120UL);
2188 if (crv != CKR_OK0x00000000UL)
2189 break;
2190 crv = sftk_Attribute2SSecItem(arena, &pubKey->u.rsa.publicExponent,
2191 object, CKA_PUBLIC_EXPONENT0x00000122UL);
2192 break;
2193 case CKK_DSA0x00000001UL:
2194 pubKey->keyType = NSSLOWKEYDSAKey;
2195 crv = sftk_Attribute2SSecItem(arena, &pubKey->u.dsa.params.prime,
2196 object, CKA_PRIME0x00000130UL);
2197 if (crv != CKR_OK0x00000000UL)
2198 break;
2199 crv = sftk_Attribute2SSecItem(arena, &pubKey->u.dsa.params.subPrime,
2200 object, CKA_SUBPRIME0x00000131UL);
2201 if (crv != CKR_OK0x00000000UL)
2202 break;
2203 crv = sftk_Attribute2SSecItem(arena, &pubKey->u.dsa.params.base,
2204 object, CKA_BASE0x00000132UL);
2205 if (crv != CKR_OK0x00000000UL)
2206 break;
2207 crv = sftk_Attribute2SSecItem(arena, &pubKey->u.dsa.publicValue,
2208 object, CKA_VALUE0x00000011UL);
2209 break;
2210 case CKK_DH0x00000002UL:
2211 pubKey->keyType = NSSLOWKEYDHKey;
2212 crv = sftk_Attribute2SSecItem(arena, &pubKey->u.dh.prime,
2213 object, CKA_PRIME0x00000130UL);
2214 if (crv != CKR_OK0x00000000UL)
2215 break;
2216 crv = sftk_Attribute2SSecItem(arena, &pubKey->u.dh.base,
2217 object, CKA_BASE0x00000132UL);
2218 if (crv != CKR_OK0x00000000UL)
2219 break;
2220 crv = sftk_Attribute2SSecItem(arena, &pubKey->u.dh.publicValue,
2221 object, CKA_VALUE0x00000011UL);
2222 break;
2223 case CKK_EC_EDWARDS0x00000040UL:
2224 pubKey->keyType = NSSLOWKEYECEdwardsKey;
2225 goto ec_continue;
2226 case CKK_EC_MONTGOMERY0x00000041UL:
2227 pubKey->keyType = NSSLOWKEYECMontgomeryKey;
2228 goto ec_continue;
2229 case CKK_EC0x00000003UL:
2230 pubKey->keyType = NSSLOWKEYECKey;
2231 ec_continue:
2232 crv = sftk_Attribute2SSecItem(arena,
2233 &pubKey->u.ec.ecParams.DEREncoding,
2234 object, CKA_EC_PARAMS0x00000180UL);
2235 if (crv != CKR_OK0x00000000UL)
2236 break;
2237
2238 /* Fill out the rest of the ecParams structure
2239 * based on the encoded params
2240 */
2241 if (EC_FillParams(arena, &pubKey->u.ec.ecParams.DEREncoding,
2242 &pubKey->u.ec.ecParams) != SECSuccess) {
2243 crv = CKR_DOMAIN_PARAMS_INVALID0x00000130UL;
2244 break;
2245 }
2246
2247 crv = sftk_Attribute2SSecItem(arena, &pubKey->u.ec.publicValue,
2248 object, CKA_EC_POINT0x00000181UL);
2249 if (crv == CKR_OK0x00000000UL) {
2250 unsigned int keyLen = EC_GetPointSize(&pubKey->u.ec.ecParams);
2251 SECItem *point = &pubKey->u.ec.publicValue;
2252 /* ECPoint_IsBare wants one coordinate, not the whole point */
2253 unsigned int fieldLen = keyLen ? (keyLen - 1) / 2 : 0;
2254
2255 /* Handle the non-DER encoded case.
2256 * Some curves are always pressumed to be non-DER.
2257 */
2258 if (pubKey->u.ec.ecParams.type != ec_params_named) {
2259 break;
2260 }
2261
2262 /* Due to a bug in some NSS version, we may have a bare SEC#1
2263 * point in uncompressed format. If so, skip the decoding
2264 * step. */
2265 if (ECPoint_IsBare(point, fieldLen)) {
2266 if (point->data[0] != EC_POINT_FORM_UNCOMPRESSED0x04) {
2267 crv = CKR_ATTRIBUTE_VALUE_INVALID0x00000013UL;
2268 }
2269 break;
2270 }
2271
2272 /* handle the encoded case */
2273 if (point->len != 0 &&
2274 point->data[0] == SEC_ASN1_OCTET_STRING0x04) {
2275 SECItem publicValue;
2276 SECStatus rv;
2277
2278 rv = SEC_QuickDERDecodeItemSEC_QuickDERDecodeItem_Util(arena, &publicValue,
2279 SEC_ASN1_GET(SEC_OctetStringTemplate)SEC_OctetStringTemplate_Util,
2280 point);
2281 /* nope, didn't decode correctly */
2282 if (rv != SECSuccess) {
2283 crv = CKR_ATTRIBUTE_VALUE_INVALID0x00000013UL;
2284 break;
2285 }
2286
2287 if (publicValue.len == keyLen && publicValue.data[0] == EC_POINT_FORM_UNCOMPRESSED0x04) {
2288 /* Received uncompressed point */
2289 pubKey->u.ec.publicValue = publicValue;
2290 break;
2291 }
2292
2293 /* Trying to decompress */
2294 SECItem publicDecompressed = { siBuffer, NULL((void*)0), 0 };
2295 (void)SECITEM_AllocItemSECITEM_AllocItem_Util(arena, &publicDecompressed, keyLen);
2296 if (EC_DecompressPublicKey(&publicValue, &pubKey->u.ec.ecParams, &publicDecompressed) == SECFailure) {
2297 crv = CKR_ATTRIBUTE_VALUE_INVALID0x00000013UL;
2298 break;
2299 }
2300
2301 /* replace our previous public key with the decoded decompressed key */
2302 pubKey->u.ec.publicValue = publicDecompressed;
2303
2304 SECItem publicDecompressedEncoded = { siBuffer, NULL((void*)0), 0 };
2305 (void)SEC_ASN1EncodeItemSEC_ASN1EncodeItem_Util(arena, &publicDecompressedEncoded, &publicDecompressed,
2306 SEC_ASN1_GET(SEC_OctetStringTemplate)SEC_OctetStringTemplate_Util);
2307 if (CKR_OK0x00000000UL != sftk_forceAttribute(object, CKA_EC_POINT0x00000181UL, sftk_item_expand(&publicDecompressedEncoded)(&publicDecompressedEncoded)->data, (&publicDecompressedEncoded
)->len
)) {
2308 crv = CKR_ATTRIBUTE_VALUE_INVALID0x00000013UL;
2309 break;
2310 }
2311
2312 break;
2313 }
2314 crv = CKR_ATTRIBUTE_VALUE_INVALID0x00000013UL;
2315 }
2316 break;
2317 case CKK_NSS_ML_KEM((0x80000000UL | 0x4E534350) + 6):
2318 case CKK_ML_KEM0x00000049UL:
2319 pubKey->keyType = NSSLOWKEYMLKEMKey;
2320 crv = sftk_GetULongAttribute(object, CKA_PARAMETER_SET0x0000061dUL,
2321 &paramSet);
2322 if (crv != CKR_OK0x00000000UL) {
2323 crv = sftk_GetULongAttribute(object, CKA_NSS_PARAMETER_SET((0x80000000UL | 0x4E534350) + 40),
2324 &paramSet);
2325 if (crv != CKR_OK0x00000000UL) {
2326 break;
2327 }
2328 }
2329 pubKey->u.mlkem.mlkemParams = sftk_kyber_PK11ParamToInternal(paramSet);
2330 if (pubKey->u.mlkem.mlkemParams == params_kyber_invalid) {
2331 crv = CKR_PARAMETER_SET_NOT_SUPPORTED0x00000209UL;
2332 break;
2333 }
2334
2335 crv = sftk_Attribute2SSecItem(arena, &pubKey->u.mlkem.key,
2336 object, CKA_VALUE0x00000011UL);
2337 break;
2338 case CKK_ML_DSA0x0000004aUL:
2339 pubKey->keyType = NSSLOWKEYMLDSAKey;
2340 crv = sftk_ReadAttribute(object, CKA_VALUE0x00000011UL,
2341 pubKey->u.mldsa.keyVal,
2342 sizeof(pubKey->u.mldsa.keyVal),
2343 &pubKey->u.mldsa.keyValLen);
2344 if (crv != CKR_OK0x00000000UL) {
2345 break;
2346 }
2347 crv = sftk_GetULongAttribute(object, CKA_PARAMETER_SET0x0000061dUL,
2348 &pubKey->u.mldsa.paramSet);
2349
2350 break;
2351 default:
2352 crv = CKR_KEY_TYPE_INCONSISTENT0x00000063UL;
2353 break;
2354 }
2355 *crvp = crv;
2356 if (crv != CKR_OK0x00000000UL) {
2357 PORT_FreeArenaPORT_FreeArena_Util(arena, PR_TRUE1);
2358 return NULL((void*)0);
2359 }
2360
2361 object->objectInfo = pubKey;
2362 object->infoFree = SFTKFree_nsslowkey_DestroyPublicKey;
2363 return pubKey;
2364}
2365
2366/* make a private key from a verified object */
2367static NSSLOWKEYPrivateKey *
2368sftk_mkPrivKey(SFTKObject *object, CK_KEY_TYPE key_type, CK_RV *crvp)
2369{
2370 NSSLOWKEYPrivateKey *privKey;
2371 SFTKItemTemplate itemTemplate[SFTK_MAX_ITEM_TEMPLATE10];
2372 int itemTemplateCount = 0;
2373 PLArenaPool *arena;
2374 CK_RV crv = CKR_OK0x00000000UL;
2375 SECStatus rv;
2376 CK_ULONG paramSet;
2377
2378 arena = PORT_NewArenaPORT_NewArena_Util(DER_DEFAULT_CHUNKSIZE(2048));
2379 if (arena == NULL((void*)0)) {
2380 *crvp = CKR_HOST_MEMORY0x00000002UL;
2381 return NULL((void*)0);
2382 }
2383
2384 privKey = (NSSLOWKEYPrivateKey *)
2385 PORT_ArenaZAllocPORT_ArenaZAlloc_Util(arena, sizeof(NSSLOWKEYPrivateKey));
2386 if (privKey == NULL((void*)0)) {
2387 PORT_FreeArenaPORT_FreeArena_Util(arena, PR_FALSE0);
2388 *crvp = CKR_HOST_MEMORY0x00000002UL;
2389 return NULL((void*)0);
2390 }
2391
2392 /* in future this would be a switch on key_type */
2393 privKey->arena = arena;
2394 switch (key_type) {
2395 case CKK_RSA0x00000000UL:
2396 privKey->keyType = NSSLOWKEYRSAKey;
2397
2398 SFTK_SET_ITEM_TEMPLATE(itemTemplate, itemTemplateCount,itemTemplate[itemTemplateCount].type = 0x00000120UL; itemTemplate
[itemTemplateCount].item = &privKey->u.rsa.modulus
2399 &privKey->u.rsa.modulus, CKA_MODULUS)itemTemplate[itemTemplateCount].type = 0x00000120UL; itemTemplate
[itemTemplateCount].item = &privKey->u.rsa.modulus
;
2400 itemTemplateCount++;
2401 SFTK_SET_ITEM_TEMPLATE(itemTemplate, itemTemplateCount,itemTemplate[itemTemplateCount].type = 0x00000122UL; itemTemplate
[itemTemplateCount].item = &privKey->u.rsa.publicExponent
2402 &privKey->u.rsa.publicExponent, CKA_PUBLIC_EXPONENT)itemTemplate[itemTemplateCount].type = 0x00000122UL; itemTemplate
[itemTemplateCount].item = &privKey->u.rsa.publicExponent
;
2403 itemTemplateCount++;
2404 SFTK_SET_ITEM_TEMPLATE(itemTemplate, itemTemplateCount,itemTemplate[itemTemplateCount].type = 0x00000123UL; itemTemplate
[itemTemplateCount].item = &privKey->u.rsa.privateExponent
2405 &privKey->u.rsa.privateExponent, CKA_PRIVATE_EXPONENT)itemTemplate[itemTemplateCount].type = 0x00000123UL; itemTemplate
[itemTemplateCount].item = &privKey->u.rsa.privateExponent
;
2406 itemTemplateCount++;
2407 SFTK_SET_ITEM_TEMPLATE(itemTemplate, itemTemplateCount,itemTemplate[itemTemplateCount].type = 0x00000124UL; itemTemplate
[itemTemplateCount].item = &privKey->u.rsa.prime1
2408 &privKey->u.rsa.prime1, CKA_PRIME_1)itemTemplate[itemTemplateCount].type = 0x00000124UL; itemTemplate
[itemTemplateCount].item = &privKey->u.rsa.prime1
;
2409 itemTemplateCount++;
2410 SFTK_SET_ITEM_TEMPLATE(itemTemplate, itemTemplateCount,itemTemplate[itemTemplateCount].type = 0x00000125UL; itemTemplate
[itemTemplateCount].item = &privKey->u.rsa.prime2
2411 &privKey->u.rsa.prime2, CKA_PRIME_2)itemTemplate[itemTemplateCount].type = 0x00000125UL; itemTemplate
[itemTemplateCount].item = &privKey->u.rsa.prime2
;
2412 itemTemplateCount++;
2413 SFTK_SET_ITEM_TEMPLATE(itemTemplate, itemTemplateCount,itemTemplate[itemTemplateCount].type = 0x00000126UL; itemTemplate
[itemTemplateCount].item = &privKey->u.rsa.exponent1
2414 &privKey->u.rsa.exponent1, CKA_EXPONENT_1)itemTemplate[itemTemplateCount].type = 0x00000126UL; itemTemplate
[itemTemplateCount].item = &privKey->u.rsa.exponent1
;
2415 itemTemplateCount++;
2416 SFTK_SET_ITEM_TEMPLATE(itemTemplate, itemTemplateCount,itemTemplate[itemTemplateCount].type = 0x00000127UL; itemTemplate
[itemTemplateCount].item = &privKey->u.rsa.exponent2
2417 &privKey->u.rsa.exponent2, CKA_EXPONENT_2)itemTemplate[itemTemplateCount].type = 0x00000127UL; itemTemplate
[itemTemplateCount].item = &privKey->u.rsa.exponent2
;
2418 itemTemplateCount++;
2419 SFTK_SET_ITEM_TEMPLATE(itemTemplate, itemTemplateCount,itemTemplate[itemTemplateCount].type = 0x00000128UL; itemTemplate
[itemTemplateCount].item = &privKey->u.rsa.coefficient
2420 &privKey->u.rsa.coefficient, CKA_COEFFICIENT)itemTemplate[itemTemplateCount].type = 0x00000128UL; itemTemplate
[itemTemplateCount].item = &privKey->u.rsa.coefficient
;
2421 itemTemplateCount++;
2422 rv = DER_SetUInteger(privKey->arena, &privKey->u.rsa.version,
2423 NSSLOWKEY_PRIVATE_KEY_INFO_VERSION0);
2424 if (rv != SECSuccess)
2425 crv = CKR_HOST_MEMORY0x00000002UL;
2426 break;
2427
2428 case CKK_DSA0x00000001UL:
2429 privKey->keyType = NSSLOWKEYDSAKey;
2430 SFTK_SET_ITEM_TEMPLATE(itemTemplate, itemTemplateCount,itemTemplate[itemTemplateCount].type = 0x00000130UL; itemTemplate
[itemTemplateCount].item = &privKey->u.dsa.params.prime
2431 &privKey->u.dsa.params.prime, CKA_PRIME)itemTemplate[itemTemplateCount].type = 0x00000130UL; itemTemplate
[itemTemplateCount].item = &privKey->u.dsa.params.prime
;
2432 itemTemplateCount++;
2433 SFTK_SET_ITEM_TEMPLATE(itemTemplate, itemTemplateCount,itemTemplate[itemTemplateCount].type = 0x00000131UL; itemTemplate
[itemTemplateCount].item = &privKey->u.dsa.params.subPrime
2434 &privKey->u.dsa.params.subPrime, CKA_SUBPRIME)itemTemplate[itemTemplateCount].type = 0x00000131UL; itemTemplate
[itemTemplateCount].item = &privKey->u.dsa.params.subPrime
;
2435 itemTemplateCount++;
2436 SFTK_SET_ITEM_TEMPLATE(itemTemplate, itemTemplateCount,itemTemplate[itemTemplateCount].type = 0x00000132UL; itemTemplate
[itemTemplateCount].item = &privKey->u.dsa.params.base
2437 &privKey->u.dsa.params.base, CKA_BASE)itemTemplate[itemTemplateCount].type = 0x00000132UL; itemTemplate
[itemTemplateCount].item = &privKey->u.dsa.params.base
;
2438 itemTemplateCount++;
2439 SFTK_SET_ITEM_TEMPLATE(itemTemplate, itemTemplateCount,itemTemplate[itemTemplateCount].type = 0x00000011UL; itemTemplate
[itemTemplateCount].item = &privKey->u.dsa.privateValue
2440 &privKey->u.dsa.privateValue, CKA_VALUE)itemTemplate[itemTemplateCount].type = 0x00000011UL; itemTemplate
[itemTemplateCount].item = &privKey->u.dsa.privateValue
;
2441 itemTemplateCount++;
2442 /* privKey was zero'd so public value is already set to NULL, 0
2443 * if we don't set it explicitly */
2444 break;
2445
2446 case CKK_DH0x00000002UL:
2447 privKey->keyType = NSSLOWKEYDHKey;
2448 SFTK_SET_ITEM_TEMPLATE(itemTemplate, itemTemplateCount,itemTemplate[itemTemplateCount].type = 0x00000130UL; itemTemplate
[itemTemplateCount].item = &privKey->u.dh.prime
2449 &privKey->u.dh.prime, CKA_PRIME)itemTemplate[itemTemplateCount].type = 0x00000130UL; itemTemplate
[itemTemplateCount].item = &privKey->u.dh.prime
;
2450 itemTemplateCount++;
2451 SFTK_SET_ITEM_TEMPLATE(itemTemplate, itemTemplateCount,itemTemplate[itemTemplateCount].type = 0x00000132UL; itemTemplate
[itemTemplateCount].item = &privKey->u.dh.base
2452 &privKey->u.dh.base, CKA_BASE)itemTemplate[itemTemplateCount].type = 0x00000132UL; itemTemplate
[itemTemplateCount].item = &privKey->u.dh.base
;
2453 itemTemplateCount++;
2454 SFTK_SET_ITEM_TEMPLATE(itemTemplate, itemTemplateCount,itemTemplate[itemTemplateCount].type = 0x00000011UL; itemTemplate
[itemTemplateCount].item = &privKey->u.dh.privateValue
2455 &privKey->u.dh.privateValue, CKA_VALUE)itemTemplate[itemTemplateCount].type = 0x00000011UL; itemTemplate
[itemTemplateCount].item = &privKey->u.dh.privateValue
;
2456 itemTemplateCount++;
2457 /* privKey was zero'd so public value is already set to NULL, 0
2458 * if we don't set it explicitly */
2459 break;
2460 case CKK_EC_EDWARDS0x00000040UL:
2461 privKey->keyType = NSSLOWKEYECEdwardsKey;
2462 goto ec_continue;
2463 case CKK_EC_MONTGOMERY0x00000041UL:
2464 privKey->keyType = NSSLOWKEYECMontgomeryKey;
2465 goto ec_continue;
2466 case CKK_EC0x00000003UL:
2467 privKey->keyType = NSSLOWKEYECKey;
2468 ec_continue:
2469 crv = sftk_Attribute2SSecItem(arena,
2470 &privKey->u.ec.ecParams.DEREncoding,
2471 object, CKA_EC_PARAMS0x00000180UL);
2472 if (crv != CKR_OK0x00000000UL)
2473 break;
2474
2475 /* Fill out the rest of the ecParams structure
2476 * based on the encoded params
2477 */
2478 if (EC_FillParams(arena, &privKey->u.ec.ecParams.DEREncoding,
2479 &privKey->u.ec.ecParams) != SECSuccess) {
2480 crv = CKR_DOMAIN_PARAMS_INVALID0x00000130UL;
2481 break;
2482 }
2483 crv = sftk_Attribute2SSecItem(arena, &privKey->u.ec.privateValue,
2484 object, CKA_VALUE0x00000011UL);
2485 if (crv != CKR_OK0x00000000UL)
2486 break;
2487
2488 if (sftk_hasAttribute(object, CKA_NSS_DB0xD5A0DB00L)) {
2489 crv = sftk_Attribute2SSecItem(arena, &privKey->u.ec.publicValue,
2490 object, CKA_NSS_DB0xD5A0DB00L);
2491 if (crv != CKR_OK0x00000000UL) {
2492 break;
2493 }
2494 /* privKey was zero'd so public value is already set to NULL, 0
2495 * if we don't set it explicitly */
2496 } else if (key_type == CKK_EC0x00000003UL) {
2497 /* as no public key was provided during the import, we need to derive it here.
2498 See: PK11_ImportAndReturnPrivateKey*/
2499 if (SECITEM_AllocItemSECITEM_AllocItem_Util(arena, &privKey->u.ec.publicValue,
2500 EC_GetPointSize(&privKey->u.ec.ecParams)) == NULL((void*)0)) {
2501 crv = CKR_HOST_MEMORY0x00000002UL;
2502 break;
2503 }
2504 PORT_Memsetmemset(privKey->u.ec.publicValue.data, 0, privKey->u.ec.publicValue.len);
2505 rv = EC_DerivePublicKey(&privKey->u.ec.privateValue, &privKey->u.ec.ecParams, &privKey->u.ec.publicValue);
2506 if (rv != SECSuccess) {
2507 crv = CKR_ATTRIBUTE_VALUE_INVALID0x00000013UL;
2508 break;
2509 }
2510 sftk_forceAttribute(object, CKA_NSS_DB0xD5A0DB00L, privKey->u.ec.publicValue.data, privKey->u.ec.publicValue.len);
2511 }
2512
2513 rv = DER_SetUInteger(privKey->arena, &privKey->u.ec.version,
2514 NSSLOWKEY_EC_PRIVATE_KEY_VERSION1);
2515 if (rv != SECSuccess) {
2516 crv = CKR_HOST_MEMORY0x00000002UL;
2517/* The following ifdef is needed for Linux arm distros and
2518 * Android as gcc 4.6 has a bug when targeting arm (but not
2519 * thumb). The bug has been fixed in gcc 4.7.
2520 * http://gcc.gnu.org/bugzilla/show_bug.cgi?id=56561
2521 */
2522#if defined(__arm__) && !defined(__thumb__) && defined(__GNUC__4)
2523 *crvp = CKR_HOST_MEMORY0x00000002UL;
2524 break;
2525#endif
2526 }
2527 break;
2528
2529 case CKK_NSS_ML_KEM((0x80000000UL | 0x4E534350) + 6):
2530 case CKK_ML_KEM0x00000049UL:
2531 privKey->keyType = NSSLOWKEYMLKEMKey;
2532
2533 crv = sftk_GetULongAttribute(object, CKA_PARAMETER_SET0x0000061dUL,
2534 &paramSet);
2535 if (crv != CKR_OK0x00000000UL) {
2536 crv = sftk_GetULongAttribute(object, CKA_NSS_PARAMETER_SET((0x80000000UL | 0x4E534350) + 40),
2537 &paramSet);
2538 if (crv != CKR_OK0x00000000UL) {
2539 break;
2540 }
2541 }
2542 privKey->u.mlkem.mlkemParams = sftk_kyber_PK11ParamToInternal(paramSet);
2543 if (privKey->u.mlkem.mlkemParams == params_kyber_invalid) {
2544 crv = CKR_PARAMETER_SET_NOT_SUPPORTED0x00000209UL;
2545 break;
2546 }
2547
2548 crv = sftk_Attribute2SSecItem(arena, &privKey->u.mlkem.key,
2549 object, CKA_VALUE0x00000011UL);
2550 if (crv != CKR_OK0x00000000UL) {
2551 break;
2552 }
2553 crv = sftk_Attribute2SSecItem(arena, &privKey->u.mlkem.seed,
2554 object, CKA_SEED0x00000637UL);
2555 if (crv != CKR_OK0x00000000UL) {
2556 /* we don't need the seed to function, if we don't have
2557 * it, just don't include it */
2558 privKey->u.mlkem.seed.data = NULL((void*)0);
2559 privKey->u.mlkem.seed.len = 0;
2560 crv = CKR_OK0x00000000UL;
2561 }
2562 break;
2563
2564 case CKK_ML_DSA0x0000004aUL:
2565 privKey->keyType = NSSLOWKEYMLDSAKey;
2566 crv = sftk_ReadAttribute(object, CKA_VALUE0x00000011UL,
2567 privKey->u.mldsa.keyVal,
2568 sizeof(privKey->u.mldsa.keyVal),
2569 &privKey->u.mldsa.keyValLen);
2570 if (crv != CKR_OK0x00000000UL) {
2571 break;
2572 }
2573 crv = sftk_ReadAttribute(object, CKA_SEED0x00000637UL,
2574 privKey->u.mldsa.seed,
2575 sizeof(privKey->u.mldsa.seed),
2576 &privKey->u.mldsa.seedLen);
2577 if (crv != CKR_OK0x00000000UL) {
2578 /* no seed value, just set it to zero. The seed
2579 * has been lost or discarded for this key */
2580 privKey->u.mldsa.seedLen = 0;
2581 }
2582 crv = sftk_GetULongAttribute(object, CKA_PARAMETER_SET0x0000061dUL,
2583 &privKey->u.mldsa.paramSet);
2584
2585 break;
2586
2587 default:
2588 crv = CKR_KEY_TYPE_INCONSISTENT0x00000063UL;
2589 break;
2590 }
2591 if (crv == CKR_OK0x00000000UL && itemTemplateCount != 0) {
2592 PORT_Assert(itemTemplateCount > 0)((itemTemplateCount > 0) ? ((void)0) : PR_Assert("itemTemplateCount > 0"
, "/root/firefox-clang/security/nss/lib/softoken/pkcs11.c", 2592
))
;
2593 PORT_Assert(itemTemplateCount <= SFTK_MAX_ITEM_TEMPLATE)((itemTemplateCount <= 10) ? ((void)0) : PR_Assert("itemTemplateCount <= SFTK_MAX_ITEM_TEMPLATE"
, "/root/firefox-clang/security/nss/lib/softoken/pkcs11.c", 2593
))
;
2594 crv = sftk_MultipleAttribute2SecItem(arena, object, itemTemplate,
2595 itemTemplateCount);
2596 }
2597 *crvp = crv;
2598 if (crv != CKR_OK0x00000000UL) {
2599 PORT_FreeArenaPORT_FreeArena_Util(arena, PR_TRUE1);
2600 return NULL((void*)0);
2601 }
2602 return privKey;
2603}
2604
2605/*
2606 * If a partial RSA private key is present, fill in the rest if necessary,
2607 * and then verify the parameters are well-formed
2608 */
2609static SECStatus
2610sftk_verifyRSAPrivateKey(SFTKObject *object, PRBool fillIfNeeded)
2611{
2612 RSAPrivateKey tmpKey = { 0 };
2613 SFTKAttribute *modulus = NULL((void*)0);
2614 SFTKAttribute *prime1 = NULL((void*)0);
2615 SFTKAttribute *prime2 = NULL((void*)0);
2616 SFTKAttribute *privateExponent = NULL((void*)0);
2617 SFTKAttribute *publicExponent = NULL((void*)0);
2618 SFTKAttribute *exponent1 = NULL((void*)0);
2619 SFTKAttribute *exponent2 = NULL((void*)0);
2620 SFTKAttribute *coefficient = NULL((void*)0);
2621 SECStatus rv;
2622 CK_RV crv;
2623
2624 /* first fill in the components that we have. Populate only uses
2625 * the non-crt components, so only fill those in */
2626 tmpKey.arena = NULL((void*)0);
2627 modulus = sftk_FindAttribute(object, CKA_MODULUS0x00000120UL);
2628 if (modulus) {
2629 tmpKey.modulus.data = modulus->attrib.pValue;
2630 tmpKey.modulus.len = modulus->attrib.ulValueLen;
2631 }
2632 prime1 = sftk_FindAttribute(object, CKA_PRIME_10x00000124UL);
2633 if (prime1) {
2634 tmpKey.prime1.data = prime1->attrib.pValue;
2635 tmpKey.prime1.len = prime1->attrib.ulValueLen;
2636 }
2637 prime2 = sftk_FindAttribute(object, CKA_PRIME_20x00000125UL);
2638 if (prime2) {
2639 tmpKey.prime2.data = prime2->attrib.pValue;
2640 tmpKey.prime2.len = prime2->attrib.ulValueLen;
2641 }
2642 privateExponent = sftk_FindAttribute(object, CKA_PRIVATE_EXPONENT0x00000123UL);
2643 if (privateExponent) {
2644 tmpKey.privateExponent.data = privateExponent->attrib.pValue;
2645 tmpKey.privateExponent.len = privateExponent->attrib.ulValueLen;
2646 }
2647 publicExponent = sftk_FindAttribute(object, CKA_PUBLIC_EXPONENT0x00000122UL);
2648 if (publicExponent) {
2649 tmpKey.publicExponent.data = publicExponent->attrib.pValue;
2650 tmpKey.publicExponent.len = publicExponent->attrib.ulValueLen;
2651 }
2652 exponent1 = sftk_FindAttribute(object, CKA_EXPONENT_10x00000126UL);
2653 if (exponent1) {
2654 tmpKey.exponent1.data = exponent1->attrib.pValue;
2655 tmpKey.exponent1.len = exponent1->attrib.ulValueLen;
2656 }
2657 exponent2 = sftk_FindAttribute(object, CKA_EXPONENT_20x00000127UL);
2658 if (exponent2) {
2659 tmpKey.exponent2.data = exponent2->attrib.pValue;
2660 tmpKey.exponent2.len = exponent2->attrib.ulValueLen;
2661 }
2662 coefficient = sftk_FindAttribute(object, CKA_COEFFICIENT0x00000128UL);
2663 if (coefficient) {
2664 tmpKey.coefficient.data = coefficient->attrib.pValue;
2665 tmpKey.coefficient.len = coefficient->attrib.ulValueLen;
2666 }
2667
2668 if (fillIfNeeded) {
2669 /*
2670 * populate requires one exponent plus 2 other components to work.
2671 * we expected our caller to check that first. If that didn't happen,
2672 * populate will simply return an error here.
2673 */
2674 rv = RSA_PopulatePrivateKey(&tmpKey);
2675 if (rv != SECSuccess) {
2676 goto loser;
2677 }
2678 }
2679 rv = RSA_PrivateKeyCheck(&tmpKey);
2680 if (rv != SECSuccess) {
2681 goto loser;
2682 }
2683 /* now that we have a fully populated key, set all our attribute values */
2684 rv = SECFailure;
2685 if (!modulus || modulus->attrib.pValue != tmpKey.modulus.data) {
2686 crv = sftk_forceAttribute(object, CKA_MODULUS0x00000120UL,
2687 sftk_item_expand(&tmpKey.modulus)(&tmpKey.modulus)->data, (&tmpKey.modulus)->len);
2688 if (crv != CKR_OK0x00000000UL)
2689 goto loser;
2690 }
2691 if (!publicExponent ||
2692 publicExponent->attrib.pValue != tmpKey.publicExponent.data) {
2693 crv = sftk_forceAttribute(object, CKA_PUBLIC_EXPONENT0x00000122UL,
2694 sftk_item_expand(&tmpKey.publicExponent)(&tmpKey.publicExponent)->data, (&tmpKey.publicExponent
)->len
);
2695 if (crv != CKR_OK0x00000000UL)
2696 goto loser;
2697 }
2698 if (!privateExponent ||
2699 privateExponent->attrib.pValue != tmpKey.privateExponent.data) {
2700 crv = sftk_forceAttribute(object, CKA_PRIVATE_EXPONENT0x00000123UL,
2701 sftk_item_expand(&tmpKey.privateExponent)(&tmpKey.privateExponent)->data, (&tmpKey.privateExponent
)->len
);
2702 if (crv != CKR_OK0x00000000UL)
2703 goto loser;
2704 }
2705 if (!prime1 || prime1->attrib.pValue != tmpKey.prime1.data) {
2706 crv = sftk_forceAttribute(object, CKA_PRIME_10x00000124UL,
2707 sftk_item_expand(&tmpKey.prime1)(&tmpKey.prime1)->data, (&tmpKey.prime1)->len);
2708 if (crv != CKR_OK0x00000000UL)
2709 goto loser;
2710 }
2711 if (!prime2 || prime2->attrib.pValue != tmpKey.prime2.data) {
2712 crv = sftk_forceAttribute(object, CKA_PRIME_20x00000125UL,
2713 sftk_item_expand(&tmpKey.prime2)(&tmpKey.prime2)->data, (&tmpKey.prime2)->len);
2714 if (crv != CKR_OK0x00000000UL)
2715 goto loser;
2716 }
2717 if (!exponent1 || exponent1->attrib.pValue != tmpKey.exponent1.data) {
2718 crv = sftk_forceAttribute(object, CKA_EXPONENT_10x00000126UL,
2719 sftk_item_expand(&tmpKey.exponent1)(&tmpKey.exponent1)->data, (&tmpKey.exponent1)->
len
);
2720 if (crv != CKR_OK0x00000000UL)
2721 goto loser;
2722 }
2723 if (!exponent2 || exponent2->attrib.pValue != tmpKey.exponent2.data) {
2724 crv = sftk_forceAttribute(object, CKA_EXPONENT_20x00000127UL,
2725 sftk_item_expand(&tmpKey.exponent2)(&tmpKey.exponent2)->data, (&tmpKey.exponent2)->
len
);
2726 if (crv != CKR_OK0x00000000UL)
2727 goto loser;
2728 }
2729 if (!coefficient || coefficient->attrib.pValue != tmpKey.coefficient.data) {
2730 crv = sftk_forceAttribute(object, CKA_COEFFICIENT0x00000128UL,
2731 sftk_item_expand(&tmpKey.coefficient)(&tmpKey.coefficient)->data, (&tmpKey.coefficient)
->len
);
2732 if (crv != CKR_OK0x00000000UL)
2733 goto loser;
2734 }
2735 rv = SECSuccess;
2736
2737/* we're done (one way or the other), clean up all our stuff */
2738loser:
2739 if (tmpKey.arena) {
2740 PORT_FreeArenaPORT_FreeArena_Util(tmpKey.arena, PR_TRUE1);
2741 }
2742 if (modulus) {
2743 sftk_FreeAttribute(modulus);
2744 }
2745 if (prime1) {
2746 sftk_FreeAttribute(prime1);
2747 }
2748 if (prime2) {
2749 sftk_FreeAttribute(prime2);
2750 }
2751 if (privateExponent) {
2752 sftk_FreeAttribute(privateExponent);
2753 }
2754 if (publicExponent) {
2755 sftk_FreeAttribute(publicExponent);
2756 }
2757 if (exponent1) {
2758 sftk_FreeAttribute(exponent1);
2759 }
2760 if (exponent2) {
2761 sftk_FreeAttribute(exponent2);
2762 }
2763 if (coefficient) {
2764 sftk_FreeAttribute(coefficient);
2765 }
2766 return rv;
2767}
2768
2769/* Generate a low private key structure from an object */
2770NSSLOWKEYPrivateKey *
2771sftk_GetPrivKey(SFTKObject *object, CK_KEY_TYPE key_type, CK_RV *crvp)
2772{
2773 NSSLOWKEYPrivateKey *priv = NULL((void*)0);
2774
2775 if (object->objclass != CKO_PRIVATE_KEY0x00000003UL) {
2776 *crvp = CKR_KEY_TYPE_INCONSISTENT0x00000063UL;
2777 return NULL((void*)0);
2778 }
2779 if (object->objectInfo) {
2780 *crvp = CKR_OK0x00000000UL;
2781 return (NSSLOWKEYPrivateKey *)object->objectInfo;
2782 }
2783
2784 priv = sftk_mkPrivKey(object, key_type, crvp);
2785 object->objectInfo = priv;
2786 object->infoFree = SFTKFree_nsslowkey_DestroyPrivateKey;
2787 return priv;
2788}
2789
2790/* populate a public key object from a lowpublic keys structure */
2791CK_RV
2792sftk_PutPubKey(SFTKObject *publicKey, SFTKObject *privateKey, CK_KEY_TYPE keyType, NSSLOWKEYPublicKey *pubKey)
2793{
2794 CK_OBJECT_CLASS classType = CKO_PUBLIC_KEY0x00000002UL;
2795 CK_BBOOL cktrue = CK_TRUE1;
2796 CK_RV crv = CKR_OK0x00000000UL;
2797 CK_ULONG paramSet = CKP_INVALID_ID0x00000000UL;
2798 sftk_DeleteAttributeType(publicKey, CKA_CLASS0x00000000UL);
2799 sftk_DeleteAttributeType(publicKey, CKA_KEY_TYPE0x00000100UL);
2800 sftk_DeleteAttributeType(publicKey, CKA_VALUE0x00000011UL);
2801
2802 switch (keyType) {
2803 case CKK_RSA0x00000000UL:
2804 sftk_DeleteAttributeType(publicKey, CKA_MODULUS0x00000120UL);
2805 sftk_DeleteAttributeType(publicKey, CKA_PUBLIC_EXPONENT0x00000122UL);
2806 /* format the keys */
2807 /* fill in the RSA dependent paramenters in the public key */
2808 crv = sftk_AddAttributeType(publicKey, CKA_MODULUS0x00000120UL,
2809 sftk_item_expand(&pubKey->u.rsa.modulus)(&pubKey->u.rsa.modulus)->data, (&pubKey->u.
rsa.modulus)->len
);
2810 if (crv != CKR_OK0x00000000UL) {
2811 break;
2812 }
2813 crv = sftk_AddAttributeType(publicKey, CKA_PUBLIC_EXPONENT0x00000122UL,
2814 sftk_item_expand(&pubKey->u.rsa.publicExponent)(&pubKey->u.rsa.publicExponent)->data, (&pubKey
->u.rsa.publicExponent)->len
);
2815 break;
2816 case CKK_DSA0x00000001UL:
2817 sftk_DeleteAttributeType(publicKey, CKA_PRIME0x00000130UL);
2818 sftk_DeleteAttributeType(publicKey, CKA_SUBPRIME0x00000131UL);
2819 sftk_DeleteAttributeType(publicKey, CKA_BASE0x00000132UL);
2820 crv = sftk_AddAttributeType(publicKey, CKA_PRIME0x00000130UL,
2821 sftk_item_expand(&pubKey->u.dsa.params.prime)(&pubKey->u.dsa.params.prime)->data, (&pubKey->
u.dsa.params.prime)->len
);
2822 if (crv != CKR_OK0x00000000UL) {
2823 break;
2824 }
2825 crv = sftk_AddAttributeType(publicKey, CKA_SUBPRIME0x00000131UL,
2826 sftk_item_expand(&pubKey->u.dsa.params.subPrime)(&pubKey->u.dsa.params.subPrime)->data, (&pubKey
->u.dsa.params.subPrime)->len
);
2827 if (crv != CKR_OK0x00000000UL) {
2828 break;
2829 }
2830 crv = sftk_AddAttributeType(publicKey, CKA_BASE0x00000132UL,
2831 sftk_item_expand(&pubKey->u.dsa.params.base)(&pubKey->u.dsa.params.base)->data, (&pubKey->
u.dsa.params.base)->len
);
2832 if (crv != CKR_OK0x00000000UL) {
2833 break;
2834 }
2835 crv = sftk_AddAttributeType(publicKey, CKA_VALUE0x00000011UL,
2836 sftk_item_expand(&pubKey->u.dsa.publicValue)(&pubKey->u.dsa.publicValue)->data, (&pubKey->
u.dsa.publicValue)->len
);
2837 break;
2838 case CKK_NSS_ML_KEM((0x80000000UL | 0x4E534350) + 6):
2839 case CKK_ML_KEM0x00000049UL:
2840 sftk_DeleteAttributeType(publicKey, CKA_VALUE0x00000011UL);
2841 sftk_DeleteAttributeType(publicKey, CKA_PARAMETER_SET0x0000061dUL);
2842 crv = sftk_AddAttributeType(publicKey, CKA_VALUE0x00000011UL,
2843 sftk_item_expand(&pubKey->u.mlkem.key)(&pubKey->u.mlkem.key)->data, (&pubKey->u.mlkem
.key)->len
);
2844 if (crv != CKR_OK0x00000000UL) {
2845 break;
2846 }
2847 paramSet = sftk_kyber_InternalToPK11Param(pubKey->u.mlkem.mlkemParams);
2848 if (paramSet == CKP_INVALID_ID0x00000000UL) {
2849 crv = CKR_PUBLIC_KEY_INVALID0x000001B9UL;
2850 break;
2851 }
2852 crv = sftk_AddAttributeType(publicKey, CKA_PARAMETER_SET0x0000061dUL,
2853 (unsigned char *)&paramSet,
2854 sizeof(paramSet));
2855 break;
2856 case CKK_ML_DSA0x0000004aUL:
2857 sftk_DeleteAttributeType(publicKey, CKA_VALUE0x00000011UL);
2858 sftk_DeleteAttributeType(publicKey, CKA_PARAMETER_SET0x0000061dUL);
2859 crv = sftk_AddAttributeType(publicKey, CKA_VALUE0x00000011UL,
2860 pubKey->u.mldsa.keyVal,
2861 pubKey->u.mldsa.keyValLen);
2862 if (crv != CKR_OK0x00000000UL) {
2863 break;
2864 }
2865 crv = sftk_AddAttributeType(publicKey, CKA_PARAMETER_SET0x0000061dUL,
2866 (unsigned char *)&pubKey->u.mldsa.paramSet,
2867 sizeof(pubKey->u.mldsa.paramSet));
2868 break;
2869 case CKK_DH0x00000002UL:
2870 sftk_DeleteAttributeType(publicKey, CKA_PRIME0x00000130UL);
2871 sftk_DeleteAttributeType(publicKey, CKA_BASE0x00000132UL);
2872 crv = sftk_AddAttributeType(publicKey, CKA_PRIME0x00000130UL,
2873 sftk_item_expand(&pubKey->u.dh.prime)(&pubKey->u.dh.prime)->data, (&pubKey->u.dh.
prime)->len
);
2874 if (crv != CKR_OK0x00000000UL) {
2875 break;
2876 }
2877 crv = sftk_AddAttributeType(publicKey, CKA_BASE0x00000132UL,
2878 sftk_item_expand(&pubKey->u.dh.base)(&pubKey->u.dh.base)->data, (&pubKey->u.dh.base
)->len
);
2879 if (crv != CKR_OK0x00000000UL) {
2880 break;
2881 }
2882 crv = sftk_AddAttributeType(publicKey, CKA_VALUE0x00000011UL,
2883 sftk_item_expand(&pubKey->u.dh.publicValue)(&pubKey->u.dh.publicValue)->data, (&pubKey->
u.dh.publicValue)->len
);
2884 break;
2885 case CKK_EC0x00000003UL:
2886 case CKK_EC_MONTGOMERY0x00000041UL:
2887 case CKK_EC_EDWARDS0x00000040UL: {
2888 SECItem encodedPoint = { siBuffer, NULL((void*)0), 0 };
2889 const SECItem *point = &pubKey->u.ec.publicValue;
2890 sftk_DeleteAttributeType(publicKey, CKA_EC_PARAMS0x00000180UL);
2891 sftk_DeleteAttributeType(publicKey, CKA_EC_POINT0x00000181UL);
2892 crv = sftk_AddAttributeType(publicKey, CKA_EC_PARAMS0x00000180UL,
2893 sftk_item_expand(&pubKey->u.ec.ecParams.DEREncoding)(&pubKey->u.ec.ecParams.DEREncoding)->data, (&pubKey
->u.ec.ecParams.DEREncoding)->len
);
2894 if (crv != CKR_OK0x00000000UL) {
2895 break;
2896 }
2897 /* NSC_GenerateKeyPair stores a CKK_EC point DER encoded and an
2898 * Edwards or Montgomery one bare, so match that here. What
2899 * nsslowkey_ConvertToPublicKey handed us is normally the bare
2900 * point, but don't wrap it twice if it isn't. */
2901 if (keyType == CKK_EC0x00000003UL &&
2902 ECPoint_IsBare(point,
2903 (EC_GetPointSize(&pubKey->u.ec.ecParams) - 1) / 2)) {
2904 if (SEC_ASN1EncodeItemSEC_ASN1EncodeItem_Util(NULL((void*)0), &encodedPoint, point,
2905 SEC_ASN1_GET(SEC_OctetStringTemplate)SEC_OctetStringTemplate_Util) == NULL((void*)0)) {
2906 crv = CKR_HOST_MEMORY0x00000002UL;
2907 break;
2908 }
2909 point = &encodedPoint;
2910 }
2911 crv = sftk_AddAttributeType(publicKey, CKA_EC_POINT0x00000181UL,
2912 sftk_item_expand(point)(point)->data, (point)->len);
2913 SECITEM_FreeItemSECITEM_FreeItem_Util(&encodedPoint, PR_FALSE0);
2914 break;
2915 }
2916 default:
2917 return CKR_KEY_TYPE_INCONSISTENT0x00000063UL;
2918 }
2919 if (crv != CKR_OK0x00000000UL) {
2920 return crv;
2921 }
2922 crv = sftk_AddAttributeType(publicKey, CKA_CLASS0x00000000UL, &classType,
2923 sizeof(CK_OBJECT_CLASS));
2924 if (crv != CKR_OK0x00000000UL) {
2925 return crv;
2926 }
2927 crv = sftk_AddAttributeType(publicKey, CKA_KEY_TYPE0x00000100UL, &keyType,
2928 sizeof(CK_KEY_TYPE));
2929 if (crv != CKR_OK0x00000000UL) {
2930 return crv;
2931 }
2932 /* now handle the operator attributes */
2933 if (sftk_isTrue(privateKey, CKA_DECRYPT0x00000105UL)) {
2934 crv = sftk_forceAttribute(publicKey, CKA_ENCRYPT0x00000104UL, &cktrue, sizeof(CK_BBOOL));
2935 if (crv != CKR_OK0x00000000UL) {
2936 return crv;
2937 }
2938 }
2939 if (sftk_isTrue(privateKey, CKA_SIGN0x00000108UL)) {
2940 crv = sftk_forceAttribute(publicKey, CKA_VERIFY0x0000010AUL, &cktrue, sizeof(CK_BBOOL));
2941 if (crv != CKR_OK0x00000000UL) {
2942 return crv;
2943 }
2944 }
2945 if (sftk_isTrue(privateKey, CKA_SIGN_RECOVER0x00000109UL)) {
2946 crv = sftk_forceAttribute(publicKey, CKA_VERIFY_RECOVER0x0000010BUL, &cktrue, sizeof(CK_BBOOL));
2947 if (crv != CKR_OK0x00000000UL) {
2948 return crv;
2949 }
2950 }
2951 if (sftk_isTrue(privateKey, CKA_DECAPSULATE0x00000634UL)) {
2952 crv = sftk_forceAttribute(publicKey, CKA_ENCAPSULATE0x00000633UL, &cktrue, sizeof(CK_BBOOL));
2953 if (crv != CKR_OK0x00000000UL) {
2954 return crv;
2955 }
2956 }
2957 if (sftk_isTrue(privateKey, CKA_DERIVE0x0000010CUL)) {
2958 crv = sftk_forceAttribute(publicKey, CKA_DERIVE0x0000010CUL, &cktrue, sizeof(CK_BBOOL));
2959 if (crv != CKR_OK0x00000000UL) {
2960 return crv;
2961 }
2962 }
2963 return crv;
2964}
2965
2966/*
2967 **************************** Symetric Key utils ************************
2968 */
2969/*
2970 * set the DES key with parity bits correctly
2971 */
2972void
2973sftk_FormatDESKey(unsigned char *key, int length)
2974{
2975 int i;
2976
2977 /* format the des key */
2978 for (i = 0; i < length; i++) {
2979 key[i] = parityTable[key[i] >> 1];
2980 }
2981}
2982
2983/*
2984 * check a des key (des2 or des3 subkey) for weak keys.
2985 */
2986PRBool
2987sftk_CheckDESKey(unsigned char *key)
2988{
2989 int i;
2990
2991 /* format the des key with parity */
2992 sftk_FormatDESKey(key, 8);
2993
2994 for (i = 0; i < sftk_desWeakTableSize; i++) {
2995 if (PORT_Memcmpmemcmp(key, sftk_desWeakTable[i], 8) == 0) {
2996 return PR_TRUE1;
2997 }
2998 }
2999 return PR_FALSE0;
3000}
3001
3002/*
3003 * check if a des or triple des key is weak.
3004 */
3005PRBool
3006sftk_IsWeakKey(unsigned char *key, CK_KEY_TYPE key_type)
3007{
3008
3009 switch (key_type) {
3010 case CKK_DES0x00000013UL:
3011 return sftk_CheckDESKey(key);
3012 case CKM_DES2_KEY_GEN0x00000130UL:
3013 if (sftk_CheckDESKey(key))
3014 return PR_TRUE1;
3015 return sftk_CheckDESKey(&key[8]);
3016 case CKM_DES3_KEY_GEN0x00000131UL:
3017 if (sftk_CheckDESKey(key))
3018 return PR_TRUE1;
3019 if (sftk_CheckDESKey(&key[8]))
3020 return PR_TRUE1;
3021 return sftk_CheckDESKey(&key[16]);
3022 default:
3023 break;
3024 }
3025 return PR_FALSE0;
3026}
3027
3028/**********************************************************************
3029 *
3030 * Start of PKCS 11 functions
3031 *
3032 **********************************************************************/
3033
3034/* return the function list */
3035CK_RV
3036NSC_GetFunctionList(CK_FUNCTION_LIST_PTR *pFunctionList)
3037{
3038 *pFunctionList = (CK_FUNCTION_LIST_PTR)&sftk_funcList_v2;
3039 return CKR_OK0x00000000UL;
3040}
3041
3042/* return the function list */
3043CK_RV
3044C_GetFunctionList(CK_FUNCTION_LIST_PTR *pFunctionList)
3045{
3046#ifdef NSS_FIPS_DISABLED1
3047 return NSC_GetFunctionList(pFunctionList);
3048#else
3049 if (NSS_GetSystemFIPSEnabled()) {
3050 return FC_GetFunctionList(pFunctionList);
3051 } else {
3052 return NSC_GetFunctionList(pFunctionList);
3053 }
3054#endif
3055}
3056
3057CK_RV
3058NSC_GetInterfaceList(CK_INTERFACE_PTR interfaces, CK_ULONG_PTR pulCount)
3059{
3060 CK_ULONG count = *pulCount;
3061 *pulCount = NSS_INTERFACE_COUNT(sizeof(nss_interfaces) / sizeof((nss_interfaces)[0]));
3062 if (interfaces == NULL((void*)0)) {
3063 return CKR_OK0x00000000UL;
3064 }
3065 if (count < NSS_INTERFACE_COUNT(sizeof(nss_interfaces) / sizeof((nss_interfaces)[0]))) {
3066 return CKR_BUFFER_TOO_SMALL0x00000150UL;
3067 }
3068 PORT_Memcpymemcpy(interfaces, nss_interfaces, sizeof(nss_interfaces));
3069 return CKR_OK0x00000000UL;
3070}
3071
3072CK_RV
3073C_GetInterfaceList(CK_INTERFACE_PTR interfaces, CK_ULONG_PTR pulCount)
3074{
3075#ifdef NSS_FIPS_DISABLED1
3076 return NSC_GetInterfaceList(interfaces, pulCount);
3077#else
3078 if (NSS_GetSystemFIPSEnabled()) {
3079 return FC_GetInterfaceList(interfaces, pulCount);
3080 } else {
3081 return NSC_GetInterfaceList(interfaces, pulCount);
3082 }
3083#endif
3084}
3085
3086/*
3087 * Get the requested interface, use the nss_interfaces array so we can
3088 * easily add new interfaces as they occur.
3089 */
3090CK_RV
3091NSC_GetInterface(CK_UTF8CHAR_PTR pInterfaceName, CK_VERSION_PTR pVersion,
3092 CK_INTERFACE_PTR_PTR ppInterface, CK_FLAGS flags)
3093{
3094 int i;
3095 for (i = 0; i < NSS_INTERFACE_COUNT(sizeof(nss_interfaces) / sizeof((nss_interfaces)[0])); i++) {
3096 CK_INTERFACE_PTR interface = &nss_interfaces[i];
3097 if (pInterfaceName && PORT_Strcmpstrcmp((char *)pInterfaceName, (char *)interface->pInterfaceName) != 0) {
3098 continue;
3099 }
3100 if (pVersion && PORT_Memcmpmemcmp(pVersion, (CK_VERSION *)interface->pFunctionList, sizeof(CK_VERSION)) != 0) {
3101 continue;
3102 }
3103 if (flags & ((interface->flags & flags) != flags)) {
3104 continue;
3105 }
3106 *ppInterface = interface;
3107 return CKR_OK0x00000000UL;
3108 }
3109 return CKR_ARGUMENTS_BAD0x00000007UL;
3110}
3111
3112CK_RV
3113C_GetInterface(CK_UTF8CHAR_PTR pInterfaceName, CK_VERSION_PTR pVersion,
3114 CK_INTERFACE_PTR_PTR ppInterface, CK_FLAGS flags)
3115{
3116#ifdef NSS_FIPS_DISABLED1
3117 return NSC_GetInterface(pInterfaceName, pVersion, ppInterface, flags);
3118#else
3119 if (NSS_GetSystemFIPSEnabled()) {
3120 return FC_GetInterface(pInterfaceName, pVersion, ppInterface, flags);
3121 } else {
3122 return NSC_GetInterface(pInterfaceName, pVersion, ppInterface, flags);
3123 }
3124#endif
3125}
3126
3127static PLHashNumber
3128sftk_HashNumber(const void *key)
3129{
3130 return (PLHashNumber)((char *)key - (char *)NULL((void*)0));
3131}
3132
3133/*
3134 * eventually I'd like to expunge all occurances of XXX_SLOT_ID and
3135 * just go with the info in the slot. This is one place, however,
3136 * where it might be a little difficult.
3137 */
3138const char *
3139sftk_getDefTokName(CK_SLOT_ID slotID)
3140{
3141 static char buf[33];
3142
3143 switch (slotID) {
3144 case NETSCAPE_SLOT_ID1:
3145 return "NSS Generic Crypto Services ";
3146 case PRIVATE_KEY_SLOT_ID2:
3147 return "NSS Certificate DB ";
3148 case FIPS_SLOT_ID3:
3149 return "NSS FIPS 140-2 Certificate DB ";
3150 default:
3151 break;
3152 }
3153 snprintf(buf, sizeof(buf), "NSS Application Token %08x ", (unsigned int)slotID);
3154 return buf;
3155}
3156
3157const char *
3158sftk_getDefSlotName(CK_SLOT_ID slotID)
3159{
3160 static char buf[65];
3161
3162 switch (slotID) {
3163 case NETSCAPE_SLOT_ID1:
3164 return "NSS Internal Cryptographic Services ";
3165 case PRIVATE_KEY_SLOT_ID2:
3166 return "NSS User Private Key and Certificate Services ";
3167 case FIPS_SLOT_ID3:
3168 return "NSS FIPS 140-2 User Private Key Services ";
3169 default:
3170 break;
3171 }
3172 snprintf(buf, sizeof(buf),
3173 "NSS Application Slot %08x ",
3174 (unsigned int)slotID);
3175 return buf;
3176}
3177
3178static CK_ULONG nscSlotCount[2] = { 0, 0 };
3179static CK_SLOT_ID_PTR nscSlotList[2] = { NULL((void*)0), NULL((void*)0) };
3180static CK_ULONG nscSlotListSize[2] = { 0, 0 };
3181static PLHashTable *nscSlotHashTable[2] = { NULL((void*)0), NULL((void*)0) };
3182
3183static unsigned int
3184sftk_GetModuleIndex(CK_SLOT_ID slotID)
3185{
3186 if (sftk_isFIPS(slotID)(((slotID) == 3) || ((slotID) >= 101))) {
3187 return NSC_FIPS_MODULE1;
3188 }
3189 return NSC_NON_FIPS_MODULE0;
3190}
3191
3192/* look up a slot structure from the ID (used to be a macro when we only
3193 * had two slots) */
3194/* if all is true, return the slot even if it has been 'unloaded' */
3195/* if all is false, only return the slots which are present */
3196SFTKSlot *
3197sftk_SlotFromID(CK_SLOT_ID slotID, PRBool all)
3198{
3199 SFTKSlot *slot;
3200 unsigned int index = sftk_GetModuleIndex(slotID);
3201
3202 if (nscSlotHashTable[index] == NULL((void*)0))
3203 return NULL((void*)0);
3204 slot = (SFTKSlot *)PL_HashTableLookupConst(nscSlotHashTable[index],
3205 (void *)(uintptr_t)slotID);
3206 /* cleared slots shouldn't 'show up' */
3207 if (slot && !all && !slot->present)
3208 slot = NULL((void*)0);
3209 return slot;
3210}
3211
3212CK_SLOT_ID
3213sftk_SlotIDFromSessionHandle(CK_SESSION_HANDLE handle)
3214{
3215 CK_ULONG slotIDIndex = (handle >> 24) & 0x7f;
3216 CK_ULONG moduleIndex = (handle >> 31) & 1;
3217
3218 if (slotIDIndex >= nscSlotCount[moduleIndex]) {
3219 return (CK_SLOT_ID)-1;
3220 }
3221 return nscSlotList[moduleIndex][slotIDIndex];
3222}
3223
3224SFTKSlot *
3225sftk_SlotFromSessionHandle(CK_SESSION_HANDLE handle)
3226{
3227 return sftk_SlotFromID(sftk_SlotIDFromSessionHandle(handle), PR_FALSE0);
3228}
3229
3230static CK_RV
3231sftk_RegisterSlot(SFTKSlot *slot, unsigned int moduleIndex)
3232{
3233 PLHashEntry *entry;
3234 unsigned int index;
3235
3236 index = sftk_GetModuleIndex(slot->slotID);
3237
3238 /* make sure the slotID for this module is valid */
3239 if (moduleIndex != index) {
3240 return CKR_SLOT_ID_INVALID0x00000003UL;
3241 }
3242
3243 if (nscSlotList[index] == NULL((void*)0)) {
3244 nscSlotListSize[index] = NSC_SLOT_LIST_BLOCK_SIZE10;
3245 nscSlotList[index] = (CK_SLOT_ID *)
3246 PORT_ZAllocPORT_ZAlloc_Util(nscSlotListSize[index] * sizeof(CK_SLOT_ID));
3247 if (nscSlotList[index] == NULL((void*)0)) {
3248 return CKR_HOST_MEMORY0x00000002UL;
3249 }
3250 }
3251 if (nscSlotCount[index] >= nscSlotListSize[index]) {
3252 CK_SLOT_ID *oldNscSlotList = nscSlotList[index];
3253 CK_ULONG oldNscSlotListSize = nscSlotListSize[index];
3254 nscSlotListSize[index] += NSC_SLOT_LIST_BLOCK_SIZE10;
3255 nscSlotList[index] = (CK_SLOT_ID *)PORT_ReallocPORT_Realloc_Util(oldNscSlotList,
3256 nscSlotListSize[index] * sizeof(CK_SLOT_ID));
3257 if (nscSlotList[index] == NULL((void*)0)) {
3258 /* evidently coverity doesn't know realloc does not
3259 * free var if it fails ! */
3260 /* coverity [use_after_free : FALSE] */
3261 nscSlotList[index] = oldNscSlotList;
3262 nscSlotListSize[index] = oldNscSlotListSize;
3263 return CKR_HOST_MEMORY0x00000002UL;
3264 }
3265 }
3266
3267 if (nscSlotHashTable[index] == NULL((void*)0)) {
3268 nscSlotHashTable[index] = PL_NewHashTable(64, sftk_HashNumber,
3269 PL_CompareValues, PL_CompareValues, NULL((void*)0), 0);
3270 if (nscSlotHashTable[index] == NULL((void*)0)) {
3271 return CKR_HOST_MEMORY0x00000002UL;
3272 }
3273 }
3274
3275 entry = PL_HashTableAdd(nscSlotHashTable[index], (void *)(uintptr_t)slot->slotID, slot);
3276 if (entry == NULL((void*)0)) {
3277 return CKR_HOST_MEMORY0x00000002UL;
3278 }
3279 slot->index = (nscSlotCount[index] & 0x7f) | ((index << 7) & 0x80);
3280 nscSlotList[index][nscSlotCount[index]++] = slot->slotID;
3281
3282 return CKR_OK0x00000000UL;
3283}
3284
3285/*
3286 * ths function has all the common initialization that happens whenever we
3287 * create a new slot or repurpose an old slot (only valid for slotID's 4
3288 * and greater).
3289 *
3290 * things that are not reinitialized are:
3291 * slotID (can't change)
3292 * slotDescription (can't change once defined)
3293 * the locks and hash tables (difficult to change in running code, and
3294 * unnecessary. hash tables and list are cleared on shutdown, but they
3295 * are cleared in a 'friendly' way).
3296 * session and object ID counters -- so any old sessions and objects in the
3297 * application will get properly notified that the world has changed.
3298 *
3299 * things that are reinitialized:
3300 * database (otherwise what would the point be;).
3301 * state variables related to databases.
3302 * session count stat info.
3303 * tokenDescription.
3304 *
3305 * NOTE: slotID's 4 and greater show up as removable devices.
3306 *
3307 */
3308CK_RV
3309SFTK_SlotReInit(SFTKSlot *slot, char *configdir, char *updatedir,
3310 char *updateID, sftk_token_parameters *params,
3311 unsigned int moduleIndex)
3312{
3313 PRBool needLogin = !params->noKeyDB;
3314 CK_RV crv;
3315
3316 slot->hasTokens = PR_FALSE0;
3317 slot->sessionIDConflict = 0;
3318 slot->sessionCount = 0;
3319 slot->rwSessionCount = 0;
3320 slot->needLogin = PR_FALSE0;
3321 slot->isLoggedIn = PR_FALSE0;
3322 slot->ssoLoggedIn = PR_FALSE0;
3323 slot->DB_loaded = PR_FALSE0;
3324 slot->certDB = NULL((void*)0);
3325 slot->keyDB = NULL((void*)0);
3326 slot->minimumPinLen = 0;
3327 slot->readOnly = params->readOnly;
3328 sftk_setStringName(params->tokdes ? params->tokdes : sftk_getDefTokName(slot->slotID), slot->tokDescription,
3329 sizeof(slot->tokDescription), PR_TRUE1);
3330 sftk_setStringName(params->updtokdes ? params->updtokdes : " ",
3331 slot->updateTokDescription,
3332 sizeof(slot->updateTokDescription), PR_TRUE1);
3333
3334 if ((!params->noCertDB) || (!params->noKeyDB)) {
3335 SFTKDBHandle *certHandle = NULL((void*)0);
3336 SFTKDBHandle *keyHandle = NULL((void*)0);
3337 crv = sftk_DBInit(params->configdir ? params->configdir : configdir,
3338 params->certPrefix, params->keyPrefix,
3339 params->updatedir ? params->updatedir : updatedir,
3340 params->updCertPrefix, params->updKeyPrefix,
3341 params->updateID ? params->updateID : updateID,
3342 params->readOnly, params->noCertDB, params->noKeyDB,
3343 params->forceOpen,
3344 moduleIndex == NSC_FIPS_MODULE1,
3345 &certHandle, &keyHandle);
3346 if (crv != CKR_OK0x00000000UL) {
3347 goto loser;
3348 }
3349
3350 slot->certDB = certHandle;
3351 slot->keyDB = keyHandle;
3352 }
3353 if (needLogin) {
3354 /* if the data base is initialized with a null password,remember that */
3355 slot->needLogin =
3356 (PRBool)!sftk_hasNullPassword(slot, slot->keyDB);
3357 if ((params->minPW >= 0) && (params->minPW <= SFTK_MAX_PIN500)) {
3358 slot->minimumPinLen = params->minPW;
3359 }
3360 if ((slot->minimumPinLen == 0) && (params->pwRequired)) {
3361 slot->minimumPinLen = 1;
3362 }
3363 /* Make sure the pin len is set to the Minimum allowed value for fips
3364 * when in FIPS mode. NOTE: we don't set it if the database has not
3365 * been initialized yet so that we can init into level1 mode if needed
3366 */
3367 if ((sftkdb_HasPasswordSet(slot->keyDB) == SECSuccess) &&
3368 (moduleIndex == NSC_FIPS_MODULE1) &&
3369 (slot->minimumPinLen < FIPS_MIN_PIN7)) {
3370 slot->minimumPinLen = FIPS_MIN_PIN7;
3371 }
3372 }
3373
3374 slot->present = PR_TRUE1;
3375 return CKR_OK0x00000000UL;
3376
3377loser:
3378 SFTK_ShutdownSlot(slot);
3379 return crv;
3380}
3381
3382/*
3383 * initialize one of the slot structures. figure out which by the ID
3384 */
3385CK_RV
3386SFTK_SlotInit(char *configdir, char *updatedir, char *updateID,
3387 sftk_token_parameters *params, unsigned int moduleIndex)
3388{
3389 unsigned int i;
3390 CK_SLOT_ID slotID = params->slotID;
3391 SFTKSlot *slot;
3392 CK_RV crv = CKR_HOST_MEMORY0x00000002UL;
3393
3394 /*
3395 * first we initialize everything that is 'permanent' with this slot.
3396 * that is everything we aren't going to shutdown if we close this slot
3397 * and open it up again with different databases */
3398
3399 slot = PORT_ZNew(SFTKSlot)(SFTKSlot *)PORT_ZAlloc_Util(sizeof(SFTKSlot));
3400
3401 if (slot == NULL((void*)0)) {
3402 return CKR_HOST_MEMORY0x00000002UL;
3403 }
3404
3405 slot->optimizeSpace = params->optimizeSpace;
3406 if (slot->optimizeSpace) {
3407 slot->sessObjHashSize = SPACE_SESSION_OBJECT_HASH_SIZE32;
3408 slot->sessHashSize = SPACE_SESSION_HASH_SIZE32;
3409 slot->numSessionLocks = 1;
3410 } else {
3411 slot->sessObjHashSize = TIME_SESSION_OBJECT_HASH_SIZE1024;
3412 slot->sessHashSize = TIME_SESSION_HASH_SIZE1024;
3413 slot->numSessionLocks = slot->sessHashSize / BUCKETS_PER_SESSION_LOCK(1 << (0));
3414 }
3415 slot->sessionLockMask = slot->numSessionLocks - 1;
3416
3417 slot->slotLock = PR_NewLock();
3418 if (slot->slotLock == NULL((void*)0))
3419 goto mem_loser;
3420 slot->sessionLock = PORT_ZNewArray(PRLock *, slot->numSessionLocks)(PRLock * *)PORT_ZAlloc_Util(sizeof(PRLock *) * (slot->numSessionLocks
))
;
3421 if (slot->sessionLock == NULL((void*)0))
3422 goto mem_loser;
3423 for (i = 0; i < slot->numSessionLocks; i++) {
3424 slot->sessionLock[i] = PR_NewLock();
3425 if (slot->sessionLock[i] == NULL((void*)0))
3426 goto mem_loser;
3427 }
3428 slot->objectLock = PR_NewLock();
3429 if (slot->objectLock == NULL((void*)0))
3430 goto mem_loser;
3431 slot->pwCheckLock = PR_NewLock();
3432 if (slot->pwCheckLock == NULL((void*)0))
3433 goto mem_loser;
3434 slot->head = PORT_ZNewArray(SFTKSession *, slot->sessHashSize)(SFTKSession * *)PORT_ZAlloc_Util(sizeof(SFTKSession *) * (slot
->sessHashSize))
;
3435 if (slot->head == NULL((void*)0))
3436 goto mem_loser;
3437 slot->sessObjHashTable = PORT_ZNewArray(SFTKObject *, slot->sessObjHashSize)(SFTKObject * *)PORT_ZAlloc_Util(sizeof(SFTKObject *) * (slot
->sessObjHashSize))
;
3438 if (slot->sessObjHashTable == NULL((void*)0))
3439 goto mem_loser;
3440 slot->tokObjHashTable = PL_NewHashTable(64, sftk_HashNumber, PL_CompareValues,
3441 SECITEM_HashCompare, NULL((void*)0), 0);
3442 if (slot->tokObjHashTable == NULL((void*)0))
3443 goto mem_loser;
3444
3445 slot->sessionIDCount = 0;
3446 slot->sessionObjectHandleCount = NSC_MIN_SESSION_OBJECT_HANDLE1U;
3447 slot->slotID = slotID;
3448 sftk_setStringName(params->slotdes ? params->slotdes : sftk_getDefSlotName(slotID), slot->slotDescription,
3449 sizeof(slot->slotDescription), PR_TRUE1);
3450 crv = sftk_InitSession(&slot->moduleObjects, slot, slotID, NULL((void*)0), NULL((void*)0),
3451 CKF_SERIAL_SESSION0x00000004UL);
3452 if (crv != CKR_OK0x00000000UL) {
3453 goto loser;
3454 }
3455
3456 /* call the reinit code to set everything that changes between token
3457 * init calls */
3458 crv = SFTK_SlotReInit(slot, configdir, updatedir, updateID,
3459 params, moduleIndex);
3460 if (crv != CKR_OK0x00000000UL) {
3461 goto loser;
3462 }
3463 if (sftk_isFIPS(slotID)(((slotID) == 3) || ((slotID) >= 101))) {
3464 crv = sftk_CreateValidationObjects(slot);
3465 if (crv != CKR_OK0x00000000UL) {
3466 goto loser;
3467 }
3468 }
3469 crv = sftk_RegisterSlot(slot, moduleIndex);
3470 if (crv != CKR_OK0x00000000UL) {
3471 goto loser;
3472 }
3473 return CKR_OK0x00000000UL;
3474
3475mem_loser:
3476 crv = CKR_HOST_MEMORY0x00000002UL;
3477loser:
3478 SFTK_DestroySlotData(slot);
3479 return crv;
3480}
3481
3482#ifdef DEBUG1
3483/*
3484 * Count session objects that are still live in a slot and belong to an
3485 * application session. The module's own objects (the FIPS validation
3486 * indicators hung off slot->moduleObjects) are excluded because softoken owns
3487 * them and tears them down separately in SFTK_DestroySlotData.
3488 *
3489 * When all of a slot's sessions are torn down (logout case below) a
3490 * well-behaved application has already destroyed every object it created, so
3491 * this count should be zero. A leftover object means the application created a
3492 * session object and relied on session/slot teardown to reap it instead of
3493 * destroying it -- harmless at exit, but an unbounded leak while the process
3494 * runs, since these accumulate on NSS's long-lived internal session.
3495 */
3496static unsigned int
3497sftk_countLiveSessionObjects(SFTKSlot *slot)
3498{
3499 unsigned int i;
3500 unsigned int count = 0;
3501
3502 PR_Lock(slot->objectLock);
3503 for (i = 0; i < slot->sessObjHashSize; i++) {
3504 SFTKObject *object;
3505 for (object = slot->sessObjHashTable[i]; object;
3506 object = object->next) {
3507 SFTKSessionObject *so = sftk_narrowToSessionObject(object);
3508 if (so && so->session != &slot->moduleObjects) {
3509 count++;
3510 }
3511 }
3512 }
3513 PR_Unlock(slot->objectLock);
3514 return count;
3515}
3516#endif /* DEBUG */
3517
3518CK_RV
3519sftk_CloseAllSessions(SFTKSlot *slot, PRBool logout)
3520{
3521 SFTKSession *session;
3522 unsigned int i;
3523 SFTKDBHandle *handle;
3524
3525#ifdef DEBUG1
3526 /* When NSS_STRICT_SHUTDOWN is set, assert that the application has
3527 * destroyed all of its session objects before every session on the slot is
3528 * torn down (which happens via NSC_CloseAllSessions at slot teardown /
3529 * C_Finalize, and via SFTK_ShutdownSlot). The fork child inherits the
3530 * parent's objects and cannot clean them up, so skip the check there. */
3531 if (logout && !parentForkedAfterC_Initialize &&
3532 PR_GetEnvSecure("NSS_STRICT_SHUTDOWN")) {
3533 unsigned int live = sftk_countLiveSessionObjects(slot);
3534 if (live != 0) {
3535 PR_fprintf(PR_STDERRPR_GetSpecialFD(PR_StandardError),
3536 "NSS_STRICT_SHUTDOWN: slot %lu still has %u live "
3537 "session object(s) when closing all sessions\n",
3538 (unsigned long)slot->slotID, live);
3539 }
3540 PORT_Assert(live == 0)((live == 0) ? ((void)0) : PR_Assert("live == 0", "/root/firefox-clang/security/nss/lib/softoken/pkcs11.c"
, 3540))
;
3541 }
3542#endif
3543
3544 /* first log out the card */
3545 /* special case - if we are in a middle of upgrade, we want to close the
3546 * sessions to fake a token removal to tell the upper level code we have
3547 * switched from one database to another, but we don't want to
3548 * explicity logout in case we can continue the upgrade with the
3549 * existing password if possible.
3550 */
3551 if (logout) {
3552 handle = sftk_getKeyDB(slot);
3553 SKIP_AFTER_FORK(PR_Lock(slot->slotLock))PR_Lock(slot->slotLock);
3554 slot->isLoggedIn = PR_FALSE0;
3555 if (slot->needLogin && handle) {
3556 sftkdb_ClearPassword(handle);
3557 }
3558 SKIP_AFTER_FORK(PR_Unlock(slot->slotLock))PR_Unlock(slot->slotLock);
3559 if (handle) {
3560 sftk_freeDB(handle);
3561 }
3562 }
3563
3564 /* now close all the current sessions */
3565 /* NOTE: If you try to open new sessions before NSC_CloseAllSessions
3566 * completes, some of those new sessions may or may not be closed by
3567 * NSC_CloseAllSessions... but any session running when this code starts
3568 * will guarrenteed be close, and no session will be partially closed */
3569 for (i = 0; i < slot->sessHashSize; i++) {
3570 PRLock *lock = SFTK_HEAD_BUCKET_LOCK(slot, i)((slot)->sessionLock[((i) >> 0) & (slot)->sessionLockMask
])
;
3571 do {
3572 SKIP_AFTER_FORK(PR_Lock(lock))PR_Lock(lock);
3573 session = slot->head[i];
3574 /* hand deque */
3575 /* this duplicates function of NSC_close session functions, but
3576 * because we know that we are freeing all the sessions, we can
3577 * do more efficient processing */
3578 if (session) {
3579 slot->head[i] = session->next;
3580 if (session->next)
3581 session->next->prev = NULL((void*)0);
3582 session->next = session->prev = NULL((void*)0);
3583 SKIP_AFTER_FORK(PR_Unlock(lock))PR_Unlock(lock);
3584 SKIP_AFTER_FORK(PR_Lock(slot->slotLock))PR_Lock(slot->slotLock);
3585 --slot->sessionCount;
3586 if (session->info.flags & CKF_RW_SESSION0x00000002UL) {
3587 --slot->rwSessionCount;
3588 }
3589 SKIP_AFTER_FORK(PR_Unlock(slot->slotLock))PR_Unlock(slot->slotLock);
3590 } else {
3591 SKIP_AFTER_FORK(PR_Unlock(lock))PR_Unlock(lock);
3592 }
3593 if (session) {
3594 sftk_FreeSession(session);
3595 }
3596 } while (session != NULL((void*)0));
3597 }
3598 return CKR_OK0x00000000UL;
3599}
3600
3601/*
3602 * shut down the databases.
3603 * we get the slot lock (which also protects slot->certDB and slot->keyDB)
3604 * and clear the values so the new users will not find the databases.
3605 * once things are clear, we can release our references to the databases.
3606 * The databases will close when the last reference is released.
3607 *
3608 * We use reference counts so that we don't crash if someone shuts down
3609 * a token that another thread is actively using.
3610 */
3611static void
3612sftk_DBShutdown(SFTKSlot *slot)
3613{
3614 SFTKDBHandle *certHandle;
3615 SFTKDBHandle *keyHandle;
3616 SKIP_AFTER_FORK(PR_Lock(slot->slotLock))PR_Lock(slot->slotLock);
3617 certHandle = slot->certDB;
3618 slot->certDB = NULL((void*)0);
3619 keyHandle = slot->keyDB;
3620 slot->keyDB = NULL((void*)0);
3621 SKIP_AFTER_FORK(PR_Unlock(slot->slotLock))PR_Unlock(slot->slotLock);
3622 if (certHandle) {
3623 sftk_freeDB(certHandle);
3624 }
3625 if (keyHandle) {
3626 sftk_freeDB(keyHandle);
3627 }
3628}
3629
3630CK_RV
3631SFTK_ShutdownSlot(SFTKSlot *slot)
3632{
3633 /* make sure no new PK11 calls work except C_GetSlotInfo */
3634 slot->present = PR_FALSE0;
3635
3636 /* close all outstanding sessions
3637 * the sessHashSize variable guarentees we have all the session
3638 * mechanism set up */
3639 if (slot->head) {
3640 sftk_CloseAllSessions(slot, PR_TRUE1);
3641 }
3642
3643 /* clear all objects.. session objects are cleared as a result of
3644 * closing all the sessions. We just need to clear the token object
3645 * cache. slot->tokObjHashTable guarentees we have the token
3646 * infrastructure set up. */
3647 if (slot->tokObjHashTable) {
3648 SFTK_ClearTokenKeyHashTable(slot);
3649 }
3650
3651 /* clear the slot description for the next guy */
3652 PORT_Memsetmemset(slot->tokDescription, 0, sizeof(slot->tokDescription));
3653
3654 /* now shut down the databases. */
3655 sftk_DBShutdown(slot);
3656 return CKR_OK0x00000000UL;
3657}
3658
3659/*
3660 * initialize one of the slot structures. figure out which by the ID
3661 */
3662CK_RV
3663SFTK_DestroySlotData(SFTKSlot *slot)
3664{
3665 unsigned int i;
3666
3667 SFTK_ShutdownSlot(slot);
3668
3669 sftk_ClearSession(&slot->moduleObjects);
3670
3671 if (slot->tokObjHashTable) {
3672 PL_HashTableDestroy(slot->tokObjHashTable);
3673 slot->tokObjHashTable = NULL((void*)0);
3674 }
3675
3676 if (slot->sessObjHashTable) {
3677 PORT_FreePORT_Free_Util(slot->sessObjHashTable);
3678 slot->sessObjHashTable = NULL((void*)0);
3679 }
3680 slot->sessObjHashSize = 0;
3681
3682 if (slot->head) {
3683 PORT_FreePORT_Free_Util(slot->head);
3684 slot->head = NULL((void*)0);
3685 }
3686 slot->sessHashSize = 0;
3687
3688 /* OK everything has been disassembled, now we can finally get rid
3689 * of the locks */
3690 SKIP_AFTER_FORK(PR_DestroyLock(slot->slotLock))PR_DestroyLock(slot->slotLock);
3691 slot->slotLock = NULL((void*)0);
3692 if (slot->sessionLock) {
3693 for (i = 0; i < slot->numSessionLocks; i++) {
3694 if (slot->sessionLock[i]) {
3695 SKIP_AFTER_FORK(PR_DestroyLock(slot->sessionLock[i]))PR_DestroyLock(slot->sessionLock[i]);
3696 slot->sessionLock[i] = NULL((void*)0);
3697 }
3698 }
3699 PORT_FreePORT_Free_Util(slot->sessionLock);
3700 slot->sessionLock = NULL((void*)0);
3701 }
3702 if (slot->objectLock) {
3703 SKIP_AFTER_FORK(PR_DestroyLock(slot->objectLock))PR_DestroyLock(slot->objectLock);
3704 slot->objectLock = NULL((void*)0);
3705 }
3706 if (slot->pwCheckLock) {
3707 SKIP_AFTER_FORK(PR_DestroyLock(slot->pwCheckLock))PR_DestroyLock(slot->pwCheckLock);
3708 slot->pwCheckLock = NULL((void*)0);
3709 }
3710 PORT_FreePORT_Free_Util(slot);
3711 return CKR_OK0x00000000UL;
3712}
3713
3714/*
3715 * handle the SECMOD.db
3716 */
3717char **
3718NSC_ModuleDBFunc(unsigned long function, char *parameters, void *args)
3719{
3720#ifdef NSS_DISABLE_DBM1
3721 return NSSUTIL_DoModuleDBFunction(function, parameters, args);
3722#else
3723 char *secmod = NULL((void*)0);
3724 char *appName = NULL((void*)0);
3725 char *filename = NULL((void*)0);
3726 NSSDBType dbType = NSS_DB_TYPE_NONE;
3727 PRBool rw;
3728 static char *success = "Success";
3729 char **rvstr = NULL((void*)0);
3730
3731 rvstr = NSSUTIL_DoModuleDBFunction(function, parameters, args);
3732 if (rvstr != NULL((void*)0)) {
3733 return rvstr;
3734 }
3735
3736 if (PORT_GetErrorPORT_GetError_Util() != SEC_ERROR_LEGACY_DATABASE) {
3737 return NULL((void*)0);
3738 }
3739
3740 /* The legacy database uses the old dbm, which is only linked with the
3741 * legacy DB handler, which is only callable from softoken */
3742
3743 secmod = _NSSUTIL_GetSecmodName(parameters, &dbType, &appName,
3744 &filename, &rw);
3745
3746 switch (function) {
3747 case SECMOD_MODULE_DB_FUNCTION_FIND0:
3748 if (secmod == NULL((void*)0)) {
3749 PORT_SetErrorPORT_SetError_Util(SEC_ERROR_INVALID_ARGS);
3750 goto loser;
3751 }
3752 if (rw && (dbType != NSS_DB_TYPE_LEGACY) &&
3753 (dbType != NSS_DB_TYPE_MULTIACCESS)) {
3754 /* if we get here, we are trying to update the local database */
3755 /* force data from the legacy DB */
3756 char *oldSecmod = NULL((void*)0);
3757 char *oldAppName = NULL((void*)0);
3758 char *oldFilename = NULL((void*)0);
3759 PRBool oldrw;
3760 char **strings = NULL((void*)0);
3761 int i;
3762
3763 dbType = NSS_DB_TYPE_LEGACY;
3764 oldSecmod = _NSSUTIL_GetSecmodName(parameters, &dbType, &oldAppName,
3765 &oldFilename, &oldrw);
3766 strings = sftkdbCall_ReadSecmodDB(appName, oldFilename, oldSecmod,
3767 (char *)parameters, oldrw);
3768 if (strings) {
3769 /* write out the strings */
3770 for (i = 0; strings[i]; i++) {
3771 NSSUTIL_DoModuleDBFunction(SECMOD_MODULE_DB_FUNCTION_ADD1,
3772 parameters, strings[i]);
3773 }
3774 sftkdbCall_ReleaseSecmodDBData(oldAppName, oldFilename, oldSecmod,
3775 (char **)strings, oldrw);
3776 } else {
3777 /* write out a dummy record */
3778 NSSUTIL_DoModuleDBFunction(SECMOD_MODULE_DB_FUNCTION_ADD1,
3779 parameters, " ");
3780 }
3781 if (oldSecmod) {
3782 PR_smprintf_free(oldSecmod);
3783 }
3784 if (oldAppName) {
3785 PORT_FreePORT_Free_Util(oldAppName);
3786 }
3787 if (oldFilename) {
3788 PORT_FreePORT_Free_Util(oldFilename);
3789 }
3790 rvstr = NSSUTIL_DoModuleDBFunction(function, parameters, args);
3791 break;
3792 }
3793 rvstr = sftkdbCall_ReadSecmodDB(appName, filename, secmod,
3794 (char *)parameters, rw);
3795 break;
3796 case SECMOD_MODULE_DB_FUNCTION_ADD1:
3797 if (secmod == NULL((void*)0)) {
3798 PORT_SetErrorPORT_SetError_Util(SEC_ERROR_INVALID_ARGS);
3799 goto loser;
3800 }
3801 rvstr = (sftkdbCall_AddSecmodDB(appName, filename, secmod,
3802 (char *)args, rw) == SECSuccess)
3803 ? &success
3804 : NULL((void*)0);
3805 break;
3806 case SECMOD_MODULE_DB_FUNCTION_DEL2:
3807 if (secmod == NULL((void*)0)) {
3808 PORT_SetErrorPORT_SetError_Util(SEC_ERROR_INVALID_ARGS);
3809 goto loser;
3810 }
3811 rvstr = (sftkdbCall_DeleteSecmodDB(appName, filename, secmod,
3812 (char *)args, rw) == SECSuccess)
3813 ? &success
3814 : NULL((void*)0);
3815 break;
3816 case SECMOD_MODULE_DB_FUNCTION_RELEASE3:
3817 rvstr = (sftkdbCall_ReleaseSecmodDBData(appName, filename, secmod,
3818 (char **)args, rw) == SECSuccess)
3819 ? &success
3820 : NULL((void*)0);
3821 break;
3822 }
3823
3824loser:
3825 if (secmod)
3826 PR_smprintf_free(secmod);
3827 if (appName)
3828 PORT_FreePORT_Free_Util(appName);
3829 if (filename)
3830 PORT_FreePORT_Free_Util(filename);
3831 return rvstr;
3832#endif /* NSS_DISABLE_DBM */
3833}
3834
3835static void
3836nscFreeAllSlots(unsigned int moduleIndex)
3837{
3838 /* free all the slots */
3839 SFTKSlot *slot = NULL((void*)0);
3840 CK_SLOT_ID slotID;
3841 int i;
3842
3843 if (nscSlotList[moduleIndex]) {
3844 CK_ULONG tmpSlotCount = nscSlotCount[moduleIndex];
3845 CK_SLOT_ID_PTR tmpSlotList = nscSlotList[moduleIndex];
3846 PLHashTable *tmpSlotHashTable = nscSlotHashTable[moduleIndex];
3847
3848 /* first close all the session */
3849 for (i = 0; i < (int)tmpSlotCount; i++) {
3850 slotID = tmpSlotList[i];
3851 (void)NSC_CloseAllSessions(slotID);
3852 }
3853
3854 /* now clear out the statics */
3855 nscSlotList[moduleIndex] = NULL((void*)0);
3856 nscSlotCount[moduleIndex] = 0;
3857 nscSlotHashTable[moduleIndex] = NULL((void*)0);
3858 nscSlotListSize[moduleIndex] = 0;
3859
3860 for (i = 0; i < (int)tmpSlotCount; i++) {
3861 slotID = tmpSlotList[i];
3862 slot = (SFTKSlot *)
3863 PL_HashTableLookup(tmpSlotHashTable, (void *)(uintptr_t)slotID);
3864 PORT_Assert(slot)((slot) ? ((void)0) : PR_Assert("slot", "/root/firefox-clang/security/nss/lib/softoken/pkcs11.c"
, 3864))
;
3865 if (!slot)
3866 continue;
3867 SFTK_DestroySlotData(slot);
3868 PL_HashTableRemove(tmpSlotHashTable, (void *)(uintptr_t)slotID);
3869 }
3870 PORT_FreePORT_Free_Util(tmpSlotList);
3871 PL_HashTableDestroy(tmpSlotHashTable);
3872 }
3873}
3874
3875static void
3876sftk_closePeer(PRBool isFIPS)
3877{
3878 CK_SLOT_ID slotID = isFIPS ? PRIVATE_KEY_SLOT_ID2 : FIPS_SLOT_ID3;
3879 SFTKSlot *slot;
3880 unsigned int moduleIndex = isFIPS ? NSC_NON_FIPS_MODULE0 : NSC_FIPS_MODULE1;
3881 PLHashTable *tmpSlotHashTable = nscSlotHashTable[moduleIndex];
3882
3883 slot = (SFTKSlot *)PL_HashTableLookup(tmpSlotHashTable, (void *)(uintptr_t)slotID);
3884 if (slot == NULL((void*)0)) {
3885 return;
3886 }
3887 sftk_DBShutdown(slot);
3888 return;
3889}
3890
3891extern void sftk_PBELockInit(void);
3892extern void sftk_PBELockShutdown(void);
3893
3894/* Parse the library parameters from the first occurance in the following src.:
3895 * 1. C_INITIALIZED_ARGS - lib params are included in LibraryParameters field
3896 * 2. NSS_LIB_PARAMS - env. var. containing the lib. params.
3897 * 3. NSS_LIB_PARAMS_FILE - env. var. pointion to a file with lib. params.
3898 * 4. /etc/nss/params.config - default lib. param. file location [Linux only]
3899 * 5. LIB_PARAM_DEFAULT - string ensureing the pressence at all times
3900 * "configdir='' certPrefix='' keyPrefix='' secmod='' flags=noCertDB,noModDB"
3901 */
3902static CK_RV
3903sftk_getParameters(CK_C_INITIALIZE_ARGS *init_args, PRBool isFIPS,
3904 sftk_parameters *paramStrings)
3905{
3906 CK_RV crv;
3907 char *libParams;
3908 const char *filename;
3909 PRFileDesc *file_dc;
3910 PRBool free_mem = PR_FALSE0;
3911
3912 if (!init_args || !init_args->LibraryParameters) {
3913 /* Library parameters were not provided via C_Initialize_args*/
3914
3915 /* Enviromental value has precedence to configuration filename */
3916 libParams = PR_GetEnvSecure("NSS_LIB_PARAMS");
3917
3918 if (!libParams) {
3919 /* Load from config filename or use default */
3920 filename = PR_GetEnvSecure("NSS_LIB_PARAMS_FILE");
3921#ifdef XP_UNIX1
3922 /* Use default configuration file for Linux only */
3923 if (!filename)
3924 filename = LIB_PARAM_DEFAULT_FILE_LOCATION"/etc/nss/params.config";
3925#endif
3926 if (filename) {
3927 file_dc = PR_OpenFile(filename, PR_RDONLY0x01, 444);
3928 if (file_dc) {
3929 /* file opened */
3930 PRInt32 len = PR_Available(file_dc);
3931 libParams = PORT_NewArray(char, len + 1)(char *)PORT_Alloc_Util(sizeof(char) * (len + 1));
3932 if (libParams) {
3933 /* memory allocated */
3934 if (PR_Read(file_dc, libParams, len) == -1) {
3935 PORT_FreePORT_Free_Util(libParams);
3936 libParams = NULL((void*)0);
3937 } else {
3938 free_mem = PR_TRUE1;
3939 libParams[len] = '\0';
3940 }
3941 }
3942
3943 PR_Close(file_dc);
3944 }
3945 }
3946 }
3947
3948 if (libParams == NULL((void*)0))
3949 libParams = LIB_PARAM_DEFAULT" configdir='' certPrefix='' keyPrefix='' secmod='' flags=noCertDB,noModDB ";
3950
3951 } else {
3952 /* Use parameters provided with C_Initialize_args */
3953 libParams = (char *)init_args->LibraryParameters;
3954 }
3955
3956 crv = sftk_parseParameters(libParams, paramStrings, isFIPS);
3957 if (crv != CKR_OK0x00000000UL) {
3958 crv = CKR_ARGUMENTS_BAD0x00000007UL;
3959 goto loser;
3960 }
3961
3962 crv = CKR_OK0x00000000UL;
3963loser:
3964 if (free_mem)
3965 PORT_FreePORT_Free_Util(libParams);
3966
3967 return crv;
3968}
3969
3970/* NSC_Initialize initializes the Cryptoki library. */
3971CK_RV
3972nsc_CommonInitialize(CK_VOID_PTR pReserved, PRBool isFIPS)
3973{
3974 CK_RV crv = CKR_OK0x00000000UL;
3975 SECStatus rv;
3976 CK_C_INITIALIZE_ARGS *init_args = (CK_C_INITIALIZE_ARGS *)pReserved;
3977 PRBool destroy_freelist_on_error = PR_TRUE1;
3978 int i;
3979 unsigned int moduleIndex = isFIPS ? NSC_FIPS_MODULE1 : NSC_NON_FIPS_MODULE0;
3980
3981 if (isFIPS) {
3982 loginWaitTime = PR_SecondsToInterval(1);
3983 }
3984
3985 ENABLE_FORK_CHECK();
3986
3987 sftk_PBELockInit();
3988
3989 rv = SECOID_Init();
3990 if (rv != SECSuccess) {
3991 crv = CKR_DEVICE_ERROR0x00000030UL;
3992 return crv;
3993 }
3994
3995 rv = BL_Init(); /* initialize freebl engine */
3996 if (rv != SECSuccess) {
3997 crv = CKR_DEVICE_ERROR0x00000030UL;
3998 return crv;
3999 }
4000
4001 rv = RNG_RNGInit(); /* initialize random number generator */
4002 if (rv != SECSuccess) {
4003 crv = CKR_DEVICE_ERROR0x00000030UL;
4004 return crv;
4005 }
4006
4007 /* NOTE:
4008 * we should be getting out mutexes from this list, not statically binding
4009 * them from NSPR. This should happen before we allow the internal to split
4010 * off from the rest on NSS.
4011 */
4012
4013 /* initialize the key and cert db's */
4014 if (init_args && (!(init_args->flags & CKF_OS_LOCKING_OK0x00000002UL))) {
4015 if (init_args->CreateMutex && init_args->DestroyMutex &&
4016 init_args->LockMutex && init_args->UnlockMutex) {
4017 /* softoken always uses NSPR (ie. OS locking), and doesn't know how
4018 * to use the lock functions provided by the application.
4019 */
4020 crv = CKR_CANT_LOCK0x0000000AUL;
4021 return crv;
4022 }
4023 if (init_args->CreateMutex || init_args->DestroyMutex ||
4024 init_args->LockMutex || init_args->UnlockMutex) {
4025 /* only some of the lock functions were provided by the
4026 * application. This is invalid per PKCS#11 spec.
4027 */
4028 crv = CKR_ARGUMENTS_BAD0x00000007UL;
4029 return crv;
4030 }
4031 }
4032
4033 sftk_parameters paramStrings;
4034
4035 /* load and parse the library parameters */
4036 crv = sftk_getParameters(init_args, isFIPS, &paramStrings);
4037 if (crv != CKR_OK0x00000000UL) {
4038 goto loser;
4039 }
4040
4041 crv = sftk_configure(paramStrings.man, paramStrings.libdes);
4042 if (crv != CKR_OK0x00000000UL) {
4043 goto loser;
4044 }
4045
4046 /* if we have a peer already open, have him close his DB's so we
4047 * don't clobber each other. */
4048 if ((isFIPS && nsc_init) || (!isFIPS && nsf_init)) {
4049 sftk_closePeer(isFIPS);
4050 if (sftk_audit_enabled) {
4051 if (isFIPS && nsc_init) {
4052 sftk_LogAuditMessage(NSS_AUDIT_INFO, NSS_AUDIT_FIPS_STATE,
4053 "enabled FIPS mode");
4054 } else {
4055 sftk_LogAuditMessage(NSS_AUDIT_INFO, NSS_AUDIT_FIPS_STATE,
4056 "disabled FIPS mode");
4057 }
4058 }
4059 /* if we have a peer open, we don't want to destroy the freelist
4060 * from under the peer if we fail, the free list will be
4061 * destroyed in that case when the C_Finalize is called for
4062 * the peer */
4063 destroy_freelist_on_error = PR_FALSE0;
4064 }
4065 /* allow us to create objects in SFTK_SlotInit */
4066 sftk_InitFreeLists();
4067
4068 for (i = 0; i < paramStrings.token_count; i++) {
4069 crv = SFTK_SlotInit(paramStrings.configdir,
4070 paramStrings.updatedir, paramStrings.updateID,
4071 &paramStrings.tokens[i], moduleIndex);
4072 if (crv != CKR_OK0x00000000UL) {
4073 nscFreeAllSlots(moduleIndex);
4074 break;
4075 }
4076 }
4077
4078loser:
4079
4080 sftk_freeParams(&paramStrings);
4081
4082 if (destroy_freelist_on_error && (CKR_OK0x00000000UL != crv)) {
4083 /* idempotent. If the list are already freed, this is a noop */
4084 sftk_CleanupFreeLists();
4085 }
4086
4087#ifndef NO_FORK_CHECK
4088 if (CKR_OK0x00000000UL == crv) {
4089#if defined(CHECK_FORK_MIXED)
4090 /* Before Solaris 10, fork handlers are not unregistered at dlclose()
4091 * time. So, we only use pthread_atfork on Solaris 10 and later. For
4092 * earlier versions, we use PID checks.
4093 */
4094 char buf[200];
4095 int major = 0, minor = 0;
4096
4097 long rv = sysinfo(SI_RELEASE, buf, sizeof(buf));
4098 if (rv > 0 && rv < sizeof(buf)) {
4099 if (2 == sscanf(buf, "%d.%d", &major, &minor)) {
4100 /* Are we on Solaris 10 or greater ? */
4101 if (major > 5 || (5 == major && minor >= 10)) {
4102 /* we are safe to use pthread_atfork */
4103 usePthread_atfork = PR_TRUE1;
4104 }
4105 }
4106 }
4107 if (usePthread_atfork) {
4108 pthread_atfork(NULL((void*)0), NULL((void*)0), ForkedChild);
4109 } else {
4110 myPid = getpid();
4111 }
4112
4113#elif defined(CHECK_FORK_PTHREAD)
4114 pthread_atfork(NULL((void*)0), NULL((void*)0), ForkedChild);
4115#elif defined(CHECK_FORK_GETPID)
4116 myPid = getpid();
4117#else
4118#error Incorrect fork check method.
4119#endif
4120 }
4121#endif
4122 return crv;
4123}
4124
4125CK_RV
4126NSC_Initialize(CK_VOID_PTR pReserved)
4127{
4128 CK_RV crv;
4129
4130 sftk_ForkReset(pReserved, &crv);
4131
4132 if (nsc_init) {
4133 return CKR_CRYPTOKI_ALREADY_INITIALIZED0x00000191UL;
4134 }
4135 crv = nsc_CommonInitialize(pReserved, PR_FALSE0);
4136 nsc_init = (PRBool)(crv == CKR_OK0x00000000UL);
4137 return crv;
4138}
4139
4140/* NSC_Finalize indicates that an application is done with the
4141 * Cryptoki library.*/
4142CK_RV
4143nsc_CommonFinalize(CK_VOID_PTR pReserved, PRBool isFIPS)
4144{
4145 /* propagate the fork status to freebl and util */
4146 BL_SetForkState(parentForkedAfterC_Initialize);
4147 UTIL_SetForkState(parentForkedAfterC_Initialize);
4148
4149 nscFreeAllSlots(isFIPS ? NSC_FIPS_MODULE1 : NSC_NON_FIPS_MODULE0);
4150
4151 /* don't muck with the globals if our peer is still initialized */
4152 if (isFIPS && nsc_init) {
4153 return CKR_OK0x00000000UL;
4154 }
4155 if (!isFIPS && nsf_init) {
4156 return CKR_OK0x00000000UL;
4157 }
4158
4159 sftk_CleanupFreeLists();
4160 sftkdb_Shutdown();
4161
4162 /* This function does not discard all our previously aquired entropy. */
4163 RNG_RNGShutdown();
4164
4165 /* tell freeBL to clean up after itself */
4166 BL_Cleanup();
4167
4168 /* reset fork status in freebl. We must do this before BL_Unload so that
4169 * this call doesn't force freebl to be reloaded. */
4170 BL_SetForkState(PR_FALSE0);
4171
4172#ifndef NSS_STATIC_SOFTOKEN
4173 /* unload freeBL shared library from memory. This may only decrement the
4174 * OS refcount if it's been loaded multiple times, eg. by libssl */
4175 BL_Unload();
4176#endif
4177
4178 /* clean up the default OID table */
4179 SECOID_Shutdown();
4180
4181 sftk_PBELockShutdown();
4182
4183 /* reset fork status in util */
4184 UTIL_SetForkState(PR_FALSE0);
4185
4186 nsc_init = PR_FALSE0;
4187
4188#ifndef NO_FORK_CHECK
4189#ifdef CHECK_FORK_MIXED
4190 if (!usePthread_atfork) {
4191 myPid = 0; /* allow CHECK_FORK in the next softoken initialization to
4192 * succeed */
4193 } else {
4194 forked = PR_FALSE0; /* allow reinitialization */
4195 }
4196#elif defined(CHECK_FORK_GETPID)
4197 myPid = 0; /* allow reinitialization */
4198#elif defined(CHECK_FORK_PTHREAD)
4199 forked = PR_FALSE0; /* allow reinitialization */
4200#endif
4201#endif
4202 return CKR_OK0x00000000UL;
4203}
4204
4205/* Hard-reset the entire softoken PKCS#11 module if the parent process forked
4206 * while it was initialized. */
4207PRBool
4208sftk_ForkReset(CK_VOID_PTR pReserved, CK_RV *crv)
4209{
4210#ifndef NO_FORK_CHECK
4211 if (PARENT_FORKED()) {
4212 parentForkedAfterC_Initialize = PR_TRUE1;
4213 if (nsc_init) {
4214 /* finalize non-FIPS token */
4215 *crv = nsc_CommonFinalize(pReserved, PR_FALSE0);
4216 PORT_Assert(CKR_OK == *crv)((0x00000000UL == *crv) ? ((void)0) : PR_Assert("CKR_OK == *crv"
, "/root/firefox-clang/security/nss/lib/softoken/pkcs11.c", 4216
))
;
4217 nsc_init = (PRBool) !(*crv == CKR_OK0x00000000UL);
4218 }
4219 if (nsf_init) {
4220 /* finalize FIPS token */
4221 *crv = nsc_CommonFinalize(pReserved, PR_TRUE1);
4222 PORT_Assert(CKR_OK == *crv)((0x00000000UL == *crv) ? ((void)0) : PR_Assert("CKR_OK == *crv"
, "/root/firefox-clang/security/nss/lib/softoken/pkcs11.c", 4222
))
;
4223 nsf_init = (PRBool) !(*crv == CKR_OK0x00000000UL);
4224 }
4225 parentForkedAfterC_Initialize = PR_FALSE0;
4226 return PR_TRUE1;
4227 }
4228#endif
4229 return PR_FALSE0;
4230}
4231
4232/* NSC_Finalize indicates that an application is done with the
4233 * Cryptoki library.*/
4234CK_RV
4235NSC_Finalize(CK_VOID_PTR pReserved)
4236{
4237 CK_RV crv;
4238
4239 /* reset entire PKCS#11 module upon fork */
4240 if (sftk_ForkReset(pReserved, &crv)) {
4241 return crv;
4242 }
4243
4244 if (!nsc_init) {
4245 return CKR_OK0x00000000UL;
4246 }
4247
4248 crv = nsc_CommonFinalize(pReserved, PR_FALSE0);
4249
4250 nsc_init = (PRBool) !(crv == CKR_OK0x00000000UL);
4251
4252 return crv;
4253}
4254
4255extern const char __nss_softokn_version[];
4256
4257/* NSC_GetInfo returns general information about Cryptoki. */
4258CK_RV
4259NSC_GetInfo(CK_INFO_PTR pInfo)
4260{
4261#define NSS_VERSION_VARIABLE __nss_softokn_version
4262#include "verref.h"
4263
4264 CHECK_FORK();
4265
4266 pInfo->cryptokiVersion.major = CRYPTOKI_VERSION_MAJOR3;
4267 pInfo->cryptokiVersion.minor = CRYPTOKI_VERSION_MINOR2;
4268 PORT_Memcpymemcpy(pInfo->manufacturerID, manufacturerID, 32);
4269 pInfo->libraryVersion.major = SOFTOKEN_VMAJOR3;
4270 pInfo->libraryVersion.minor = SOFTOKEN_VMINOR132;
4271 PORT_Memcpymemcpy(pInfo->libraryDescription, libraryDescription, 32);
4272 pInfo->flags = 0;
4273 return CKR_OK0x00000000UL;
4274}
4275
4276/* NSC_GetInfo returns general information about Cryptoki. */
4277CK_RV
4278NSC_GetInfoV2(CK_INFO_PTR pInfo)
4279{
4280 CHECK_FORK();
4281
4282 pInfo->cryptokiVersion.major = 2;
4283 pInfo->cryptokiVersion.minor = 40;
4284 PORT_Memcpymemcpy(pInfo->manufacturerID, manufacturerID, 32);
4285 pInfo->libraryVersion.major = SOFTOKEN_VMAJOR3;
4286 pInfo->libraryVersion.minor = SOFTOKEN_VMINOR132;
4287 PORT_Memcpymemcpy(pInfo->libraryDescription, libraryDescription, 32);
4288 pInfo->flags = 0;
4289 return CKR_OK0x00000000UL;
4290}
4291
4292/* NSC_GetSlotList obtains a list of slots in the system. */
4293CK_RV
4294nsc_CommonGetSlotList(CK_BBOOL tokenPresent,
4295 CK_SLOT_ID_PTR pSlotList, CK_ULONG_PTR pulCount,
4296 unsigned int moduleIndex)
4297{
4298 *pulCount = nscSlotCount[moduleIndex];
4299 if (pSlotList != NULL((void*)0)) {
4300 PORT_Memcpymemcpy(pSlotList, nscSlotList[moduleIndex],
4301 nscSlotCount[moduleIndex] * sizeof(CK_SLOT_ID));
4302 }
4303 return CKR_OK0x00000000UL;
4304}
4305
4306/* NSC_GetSlotList obtains a list of slots in the system. */
4307CK_RV
4308NSC_GetSlotList(CK_BBOOL tokenPresent,
4309 CK_SLOT_ID_PTR pSlotList, CK_ULONG_PTR pulCount)
4310{
4311 CHECK_FORK();
4312 return nsc_CommonGetSlotList(tokenPresent, pSlotList, pulCount,
4313 NSC_NON_FIPS_MODULE0);
4314}
4315
4316/* NSC_GetSlotInfo obtains information about a particular slot in the system. */
4317CK_RV
4318NSC_GetSlotInfo(CK_SLOT_ID slotID, CK_SLOT_INFO_PTR pInfo)
4319{
4320 SFTKSlot *slot = sftk_SlotFromID(slotID, PR_TRUE1);
4321
4322 CHECK_FORK();
4323
4324 if (slot == NULL((void*)0))
4325 return CKR_SLOT_ID_INVALID0x00000003UL;
4326
4327 PORT_Memcpymemcpy(pInfo->manufacturerID, manufacturerID,
4328 sizeof(pInfo->manufacturerID));
4329 PORT_Memcpymemcpy(pInfo->slotDescription, slot->slotDescription,
4330 sizeof(pInfo->slotDescription));
4331 pInfo->flags = (slot->present) ? CKF_TOKEN_PRESENT0x00000001UL : 0;
4332
4333 /* all user defined slots are defined as removable */
4334 if (slotID >= SFTK_MIN_USER_SLOT_ID4) {
4335 pInfo->flags |= CKF_REMOVABLE_DEVICE0x00000002UL;
4336 } else {
4337 /* In the case where we are doing a merge update, we need
4338 * the DB slot to be removable so the token name can change
4339 * appropriately. */
4340 SFTKDBHandle *handle = sftk_getKeyDB(slot);
4341 if (handle) {
4342 if (sftkdb_InUpdateMerge(handle)) {
4343 pInfo->flags |= CKF_REMOVABLE_DEVICE0x00000002UL;
4344 }
4345 sftk_freeDB(handle);
4346 }
4347 }
4348
4349 /* If there is no key database, this is for example the case when NSS was
4350 * initialized with NSS_NoDbInit(), then there won't be any point in
4351 * requesting a PIN. Set the CKF_USER_PIN_INITIALIZED bit so that
4352 * PK11_NeedUserInit() doesn't indicate that a PIN is needed.
4353 */
4354 if (slot->keyDB == NULL((void*)0)) {
4355 pInfo->flags |= CKF_USER_PIN_INITIALIZED0x00000008UL;
4356 }
4357
4358 /* ok we really should read it out of the keydb file. */
4359 /* pInfo->hardwareVersion.major = NSSLOWKEY_DB_FILE_VERSION; */
4360 pInfo->hardwareVersion.major = SOFTOKEN_VMAJOR3;
4361 pInfo->hardwareVersion.minor = SOFTOKEN_VMINOR132;
4362 pInfo->firmwareVersion.major = SOFTOKEN_VPATCH0;
4363 pInfo->firmwareVersion.minor = SOFTOKEN_VBUILD0;
4364 return CKR_OK0x00000000UL;
4365}
4366
4367/*
4368 * check the current state of the 'needLogin' flag in case the database has
4369 * been changed underneath us.
4370 */
4371static PRBool
4372sftk_checkNeedLogin(SFTKSlot *slot, SFTKDBHandle *keyHandle)
4373{
4374 PRBool needLogin;
4375 if (sftkdb_PWCached(keyHandle) == SECSuccess) {
4376 PR_Lock(slot->slotLock);
4377 needLogin = slot->needLogin;
4378 PR_Unlock(slot->slotLock);
4379 } else {
4380 needLogin = (PRBool)!sftk_hasNullPassword(slot, keyHandle);
4381 PR_Lock(slot->slotLock);
4382 slot->needLogin = needLogin;
4383 PR_Unlock(slot->slotLock);
4384 }
4385 return needLogin;
4386}
4387
4388static PRBool
4389sftk_isBlank(const char *s, int len)
4390{
4391 int i;
4392 for (i = 0; i < len; i++) {
4393 if (s[i] != ' ') {
4394 return PR_FALSE0;
4395 }
4396 }
4397 return PR_TRUE1;
4398}
4399
4400/* NSC_GetTokenInfo obtains information about a particular token in
4401 * the system. */
4402CK_RV
4403NSC_GetTokenInfo(CK_SLOT_ID slotID, CK_TOKEN_INFO_PTR pInfo)
4404{
4405 SFTKSlot *slot;
4406 SFTKDBHandle *handle;
4407
4408 CHECK_FORK();
4409
4410 if (!nsc_init && !nsf_init)
4411 return CKR_CRYPTOKI_NOT_INITIALIZED0x00000190UL;
4412 slot = sftk_SlotFromID(slotID, PR_FALSE0);
4413 if (slot == NULL((void*)0))
4414 return CKR_SLOT_ID_INVALID0x00000003UL;
4415
4416 PORT_Memcpymemcpy(pInfo->manufacturerID, manufacturerID, 32);
4417 PORT_Memcpymemcpy(pInfo->model, "NSS 3 ", 16);
4418 PORT_Memcpymemcpy(pInfo->serialNumber, "0000000000000000", 16);
4419 PORT_Memcpymemcpy(pInfo->utcTime, "0000000000000000", 16);
4420 pInfo->ulMaxSessionCount = CK_EFFECTIVELY_INFINITE0;
4421 pInfo->ulMaxRwSessionCount = CK_EFFECTIVELY_INFINITE0;
4422 PR_Lock(slot->slotLock); /* Protect sessionCount / rwSessioncount */
4423 pInfo->ulSessionCount = slot->sessionCount;
4424 pInfo->ulRwSessionCount = slot->rwSessionCount;
4425 PR_Unlock(slot->slotLock); /* Unlock before sftk_getKeyDB */
4426 pInfo->firmwareVersion.major = 0;
4427 pInfo->firmwareVersion.minor = 0;
4428 PORT_Memcpymemcpy(pInfo->label, slot->tokDescription, sizeof(pInfo->label));
4429 handle = sftk_getKeyDB(slot);
4430 pInfo->flags = CKF_RNG0x00000001UL | CKF_DUAL_CRYPTO_OPERATIONS0x00000200UL;
4431 if (handle == NULL((void*)0)) {
4432 pInfo->flags |= CKF_WRITE_PROTECTED0x00000002UL;
4433 pInfo->ulMaxPinLen = 0;
4434 pInfo->ulMinPinLen = 0;
4435 pInfo->ulTotalPublicMemory = 0;
4436 pInfo->ulFreePublicMemory = 0;
4437 pInfo->ulTotalPrivateMemory = 0;
4438 pInfo->ulFreePrivateMemory = 0;
4439 pInfo->hardwareVersion.major = 4;
4440 pInfo->hardwareVersion.minor = 0;
4441 } else {
4442 /*
4443 * we have three possible states which we may be in:
4444 * (1) No DB password has been initialized. This also means we
4445 * have no keys in the key db.
4446 * (2) Password initialized to NULL. This means we have keys, but
4447 * the user has chosen not use a password.
4448 * (3) Finally we have an initialized password whicn is not NULL, and
4449 * we will need to prompt for it.
4450 */
4451 if (sftkdb_HasPasswordSet(handle) == SECFailure) {
4452 pInfo->flags |= CKF_LOGIN_REQUIRED0x00000004UL;
4453 } else if (!sftk_checkNeedLogin(slot, handle)) {
4454 pInfo->flags |= CKF_USER_PIN_INITIALIZED0x00000008UL;
4455 } else {
4456 pInfo->flags |= CKF_LOGIN_REQUIRED0x00000004UL | CKF_USER_PIN_INITIALIZED0x00000008UL;
4457 /*
4458 * if we are doing a merge style update, and we need to get the password
4459 * of our source database (the database we are updating from), make sure we
4460 * return a token name that will match the database we are prompting for.
4461 */
4462 if (sftkdb_NeedUpdateDBPassword(handle)) {
4463 /* if we have an update tok description, use it. otherwise
4464 * use the updateID for this database */
4465 if (!sftk_isBlank(slot->updateTokDescription,
4466 sizeof(pInfo->label))) {
4467 PORT_Memcpymemcpy(pInfo->label, slot->updateTokDescription,
4468 sizeof(pInfo->label));
4469 } else {
4470 /* build from updateID */
4471 const char *updateID = sftkdb_GetUpdateID(handle);
4472 if (updateID) {
4473 sftk_setStringName(updateID, (char *)pInfo->label,
4474 sizeof(pInfo->label), PR_FALSE0);
4475 }
4476 }
4477 }
4478 }
4479 pInfo->ulMaxPinLen = SFTK_MAX_PIN500;
4480 pInfo->ulMinPinLen = (CK_ULONG)slot->minimumPinLen;
4481 pInfo->ulTotalPublicMemory = 1;
4482 pInfo->ulFreePublicMemory = 1;
4483 pInfo->ulTotalPrivateMemory = 1;
4484 pInfo->ulFreePrivateMemory = 1;
4485#ifdef SHDB_FIXME
4486 pInfo->hardwareVersion.major = CERT_DB_FILE_VERSION;
4487 pInfo->hardwareVersion.minor = handle->version;
4488#else
4489 pInfo->hardwareVersion.major = 0;
4490 pInfo->hardwareVersion.minor = 0;
4491#endif
4492 sftk_freeDB(handle);
4493 }
4494 /*
4495 * CKF_LOGIN_REQUIRED CKF_USER_PIN_INITIALIZED how CKF_TOKEN_INITIALIZED
4496 * should be set
4497 * 0 0 1
4498 * 1 0 0
4499 * 0 1 1
4500 * 1 1 1
4501 */
4502 if (!(pInfo->flags & CKF_LOGIN_REQUIRED0x00000004UL) ||
4503 (pInfo->flags & CKF_USER_PIN_INITIALIZED0x00000008UL)) {
4504 pInfo->flags |= CKF_TOKEN_INITIALIZED0x00000400UL;
4505 }
4506 return CKR_OK0x00000000UL;
4507}
4508
4509/* NSC_GetMechanismList obtains a list of mechanism types
4510 * supported by a token. */
4511CK_RV
4512NSC_GetMechanismList(CK_SLOT_ID slotID,
4513 CK_MECHANISM_TYPE_PTR pMechanismList, CK_ULONG_PTR pulCount)
4514{
4515 CK_ULONG i;
4516
4517 CHECK_FORK();
4518
4519 switch (slotID) {
4520 /* default: */
4521 case NETSCAPE_SLOT_ID1:
4522 *pulCount = mechanismCount;
4523 if (pMechanismList != NULL((void*)0)) {
4524 for (i = 0; i < mechanismCount; i++) {
4525 pMechanismList[i] = mechanisms[i].type;
4526 }
4527 }
4528 break;
4529 default:
4530 *pulCount = 0;
4531 for (i = 0; i < mechanismCount; i++) {
4532 if (mechanisms[i].privkey) {
4533 (*pulCount)++;
4534 if (pMechanismList != NULL((void*)0)) {
4535 *pMechanismList++ = mechanisms[i].type;
4536 }
4537 }
4538 }
4539 break;
4540 }
4541 return CKR_OK0x00000000UL;
4542}
4543
4544/* NSC_GetMechanismInfo obtains information about a particular mechanism
4545 * possibly supported by a token. */
4546CK_RV
4547NSC_GetMechanismInfo(CK_SLOT_ID slotID, CK_MECHANISM_TYPE type,
4548 CK_MECHANISM_INFO_PTR pInfo)
4549{
4550 PRBool isPrivateKey;
4551 CK_ULONG i;
4552
4553 CHECK_FORK();
4554
4555 switch (slotID) {
4556 case NETSCAPE_SLOT_ID1:
4557 isPrivateKey = PR_FALSE0;
4558 break;
4559 default:
4560 isPrivateKey = PR_TRUE1;
4561 break;
4562 }
4563 for (i = 0; i < mechanismCount; i++) {
4564 if (type == mechanisms[i].type) {
4565 if (isPrivateKey && !mechanisms[i].privkey) {
4566 return CKR_MECHANISM_INVALID0x00000070UL;
4567 }
4568 PORT_Memcpymemcpy(pInfo, &mechanisms[i].info, sizeof(CK_MECHANISM_INFO));
4569 return CKR_OK0x00000000UL;
4570 }
4571 }
4572 return CKR_MECHANISM_INVALID0x00000070UL;
4573}
4574
4575/*
4576 * If we are using the V2 interface, strip out the message flags
4577 */
4578#define SFTK_MESSAGE_FLAGS(0x00000002UL | 0x00000004UL | 0x00000008UL | 0x00000010UL) (CKF_MESSAGE_ENCRYPT0x00000002UL | CKF_MESSAGE_DECRYPT0x00000004UL | CKF_MESSAGE_SIGN0x00000008UL | CKF_MESSAGE_VERIFY0x00000010UL)
4579CK_RV
4580NSC_GetMechanismInfoV2(CK_SLOT_ID slotID, CK_MECHANISM_TYPE type,
4581 CK_MECHANISM_INFO_PTR pInfo)
4582{
4583 CK_RV crv;
4584 crv = NSC_GetMechanismInfo(slotID, type, pInfo);
4585 if (crv == CKR_OK0x00000000UL) {
4586 pInfo->flags = pInfo->flags & ~SFTK_MESSAGE_FLAGS(0x00000002UL | 0x00000004UL | 0x00000008UL | 0x00000010UL);
4587 }
4588 return crv;
4589}
4590
4591CK_RV
4592sftk_MechAllowsOperation(CK_MECHANISM_TYPE type, CK_ATTRIBUTE_TYPE op)
4593{
4594 CK_ULONG i;
4595 CK_FLAGS flags = sftk_AttributeToFlags(op);
4596
4597 if (flags == 0) {
4598 return CKR_ARGUMENTS_BAD0x00000007UL;
4599 }
4600 for (i = 0; i < mechanismCount; i++) {
4601 if (type == mechanisms[i].type) {
4602 return (flags & mechanisms[i].info.flags) ? CKR_OK0x00000000UL
4603 : CKR_MECHANISM_INVALID0x00000070UL;
4604 }
4605 }
4606 return CKR_MECHANISM_INVALID0x00000070UL;
4607}
4608
4609/* NSC_InitToken initializes a token. */
4610CK_RV
4611NSC_InitToken(CK_SLOT_ID slotID, CK_CHAR_PTR pPin,
4612 CK_ULONG ulPinLen, CK_CHAR_PTR pLabel)
4613{
4614 SFTKSlot *slot = sftk_SlotFromID(slotID, PR_FALSE0);
4615 SFTKDBHandle *handle;
4616 SECStatus rv;
4617 unsigned int i;
4618 SFTKObject *object;
4619
4620 CHECK_FORK();
4621
4622 if (slot == NULL((void*)0))
4623 return CKR_SLOT_ID_INVALID0x00000003UL;
4624
4625 /* don't initialize the database if we aren't talking to a token
4626 * that uses the key database.
4627 */
4628 if (slotID == NETSCAPE_SLOT_ID1) {
4629 return CKR_TOKEN_WRITE_PROTECTED0x000000E2UL;
4630 }
4631
4632 /* first, delete all our loaded key and cert objects from our
4633 * internal list. */
4634 PR_Lock(slot->objectLock);
4635 for (i = 0; i < slot->sessObjHashSize; i++) {
4636 do {
4637 object = slot->sessObjHashTable[i];
4638 /* hand deque */
4639 /* this duplicates function of NSC_close session functions, but
4640 * because we know that we are freeing all the sessions, we can
4641 * do more efficient processing */
4642 if (object) {
4643 SFTKSessionObject *so = sftk_narrowToSessionObject(object);
4644
4645 slot->sessObjHashTable[i] = object->next;
4646
4647 if (object->next)
4648 object->next->prev = NULL((void*)0);
4649 object->next = object->prev = NULL((void*)0);
4650 /* Keep the two queues in agreement: sftk_ClearSession()
4651 * relies on an object being on a session's list only while it
4652 * is still in the slot hash. Leaving it linked would let that
4653 * walk touch this object after we drop the queues' reference
4654 * below. */
4655 if (so && so->session) {
4656 PORT_Assert(sftkqueue_is_queued(&so->sessionList, 0,(((((&so->sessionList)->next) || ((&so->sessionList
)->prev) || ((so->session->objects)[((PRUint32)((0) *
1791398085) & (0 - 1))] == (&so->sessionList)))) ?
((void)0) : PR_Assert("sftkqueue_is_queued(&so->sessionList, 0, so->session->objects, 0)"
, "/root/firefox-clang/security/nss/lib/softoken/pkcs11.c", 4657
))
4657 so->session->objects, 0))(((((&so->sessionList)->next) || ((&so->sessionList
)->prev) || ((so->session->objects)[((PRUint32)((0) *
1791398085) & (0 - 1))] == (&so->sessionList)))) ?
((void)0) : PR_Assert("sftkqueue_is_queued(&so->sessionList, 0, so->session->objects, 0)"
, "/root/firefox-clang/security/nss/lib/softoken/pkcs11.c", 4657
))
;
4658 sftkqueue_delete(&so->sessionList, 0,if ((&so->sessionList)->next) (&so->sessionList
)->next->prev = (&so->sessionList)->prev; if (
(&so->sessionList)->prev) (&so->sessionList)
->prev->next = (&so->sessionList)->next; else
(so->session->objects)[((PRUint32)((0) * 1791398085) &
(0 - 1))] = ((&so->sessionList)->next); (&so->
sessionList)->next = ((void*)0); (&so->sessionList)
->prev = ((void*)0);
4659 so->session->objects, 0)if ((&so->sessionList)->next) (&so->sessionList
)->next->prev = (&so->sessionList)->prev; if (
(&so->sessionList)->prev) (&so->sessionList)
->prev->next = (&so->sessionList)->next; else
(so->session->objects)[((PRUint32)((0) * 1791398085) &
(0 - 1))] = ((&so->sessionList)->next); (&so->
sessionList)->next = ((void*)0); (&so->sessionList)
->prev = ((void*)0);
;
4660 }
4661 }
4662 if (object)
4663 sftk_FreeObject(object);
4664 } while (object != NULL((void*)0));
4665 }
4666 slot->DB_loaded = PR_FALSE0;
4667 PR_Unlock(slot->objectLock);
4668
4669 /* then clear out the key database */
4670 handle = sftk_getKeyDB(slot);
4671 if (handle == NULL((void*)0)) {
4672 return CKR_TOKEN_WRITE_PROTECTED0x000000E2UL;
4673 }
4674
4675 rv = sftkdb_ResetKeyDB(handle);
4676 /* clear the password */
4677 sftkdb_ClearPassword(handle);
4678 /* update slot->needLogin (should be true now since no password is set) */
4679 sftk_checkNeedLogin(slot, handle);
4680 sftk_freeDB(handle);
4681 if (rv != SECSuccess) {
4682 return CKR_DEVICE_ERROR0x00000030UL;
4683 }
4684
4685 return CKR_OK0x00000000UL;
4686}
4687
4688/* NSC_InitPIN initializes the normal user's PIN. */
4689CK_RV
4690NSC_InitPIN(CK_SESSION_HANDLE hSession,
4691 CK_CHAR_PTR pPin, CK_ULONG ulPinLen)
4692{
4693 SFTKSession *sp = NULL((void*)0);
4694 SFTKSlot *slot;
4695 SFTKDBHandle *handle = NULL((void*)0);
4696 char newPinStr[SFTK_MAX_PIN500 + 1];
4697 SECStatus rv;
4698 CK_RV crv = CKR_SESSION_HANDLE_INVALID0x000000B3UL;
4699 PRBool tokenRemoved = PR_FALSE0;
4700
4701 CHECK_FORK();
4702
4703 sp = sftk_SessionFromHandle(hSession);
4704 if (sp == NULL((void*)0)) {
4705 goto loser;
4706 }
4707
4708 slot = sftk_SlotFromSession(sp)((sp)->slot);
4709 if (slot == NULL((void*)0)) {
4710 goto loser;
4711 }
4712
4713 handle = sftk_getKeyDB(slot);
4714 if (handle == NULL((void*)0)) {
4715 crv = CKR_PIN_LEN_RANGE0x000000A2UL;
4716 goto loser;
4717 }
4718
4719 if (sp->info.state != CKS_RW_SO_FUNCTIONS4) {
4720 crv = CKR_USER_NOT_LOGGED_IN0x00000101UL;
4721 goto loser;
4722 }
4723
4724 sftk_FreeSession(sp);
4725 sp = NULL((void*)0);
4726
4727 /* make sure the pins aren't too long */
4728 if (ulPinLen > SFTK_MAX_PIN500) {
4729 crv = CKR_PIN_LEN_RANGE0x000000A2UL;
4730 goto loser;
4731 }
4732 if (ulPinLen < (CK_ULONG)slot->minimumPinLen) {
4733 crv = CKR_PIN_LEN_RANGE0x000000A2UL;
4734 goto loser;
4735 }
4736
4737 if (sftkdb_HasPasswordSet(handle) != SECFailure) {
4738 crv = CKR_DEVICE_ERROR0x00000030UL;
4739 goto loser;
4740 }
4741
4742 /* convert to null terminated string */
4743 PORT_Memcpymemcpy(newPinStr, pPin, ulPinLen);
4744 newPinStr[ulPinLen] = 0;
4745
4746 /* build the hashed pins which we pass around */
4747
4748 /* change the data base */
4749 rv = sftkdb_ChangePassword(handle, NULL((void*)0), newPinStr, &tokenRemoved);
4750 if (tokenRemoved) {
4751 sftk_CloseAllSessions(slot, PR_FALSE0);
4752 }
4753 PORT_Memsetmemset(newPinStr, 0, ulPinLen);
4754 sftk_freeDB(handle);
4755 handle = NULL((void*)0);
4756
4757 /* Now update our local copy of the pin */
4758 if (rv == SECSuccess) {
4759 if (ulPinLen == 0) {
4760 PR_Lock(slot->slotLock);
4761 slot->needLogin = PR_FALSE0;
4762 PR_Unlock(slot->slotLock);
4763 }
4764 /* database has been initialized, now force min password in FIPS
4765 * mode. NOTE: if we are in level1, we may not have a password, but
4766 * forcing it now will prevent an insufficient password from being set.
4767 */
4768 if ((sftk_GetModuleIndex(slot->slotID) == NSC_FIPS_MODULE1) &&
4769 (slot->minimumPinLen < FIPS_MIN_PIN7)) {
4770 slot->minimumPinLen = FIPS_MIN_PIN7;
4771 }
4772 return CKR_OK0x00000000UL;
4773 }
4774 crv = CKR_PIN_INCORRECT0x000000A0UL;
4775
4776loser:
4777 if (sp) {
4778 sftk_FreeSession(sp);
4779 }
4780 if (handle) {
4781 sftk_freeDB(handle);
4782 }
4783 return crv;
4784}
4785
4786/* NSC_SetPIN modifies the PIN of user that is currently logged in. */
4787/* NOTE: This is only valid for the PRIVATE_KEY_SLOT */
4788CK_RV
4789NSC_SetPIN(CK_SESSION_HANDLE hSession, CK_CHAR_PTR pOldPin,
4790 CK_ULONG ulOldLen, CK_CHAR_PTR pNewPin, CK_ULONG ulNewLen)
4791{
4792 SFTKSession *sp = NULL((void*)0);
4793 SFTKSlot *slot;
4794 SFTKDBHandle *handle = NULL((void*)0);
4795 char newPinStr[SFTK_MAX_PIN500 + 1], oldPinStr[SFTK_MAX_PIN500 + 1];
4796 SECStatus rv;
4797 CK_RV crv = CKR_SESSION_HANDLE_INVALID0x000000B3UL;
4798 PRBool needLogin;
4799 PRBool tokenRemoved = PR_FALSE0;
4800
4801 CHECK_FORK();
4802
4803 sp = sftk_SessionFromHandle(hSession);
4804 if (sp == NULL((void*)0)) {
4805 goto loser;
4806 }
4807
4808 slot = sftk_SlotFromSession(sp)((sp)->slot);
4809 if (!slot) {
4810 goto loser;
4811 }
4812
4813 handle = sftk_getKeyDB(slot);
4814 if (handle == NULL((void*)0)) {
4815 sftk_FreeSession(sp);
4816 return CKR_PIN_LEN_RANGE0x000000A2UL; /* XXX FIXME wrong return value */
4817 }
4818
4819 PR_Lock(slot->slotLock);
4820 needLogin = slot->needLogin;
4821 PR_Unlock(slot->slotLock);
4822 if (needLogin && sp->info.state != CKS_RW_USER_FUNCTIONS3) {
4823 crv = CKR_USER_NOT_LOGGED_IN0x00000101UL;
4824 goto loser;
4825 }
4826
4827 sftk_FreeSession(sp);
4828 sp = NULL((void*)0);
4829
4830 /* make sure the pins aren't too long */
4831 if ((ulNewLen > SFTK_MAX_PIN500) || (ulOldLen > SFTK_MAX_PIN500)) {
4832 crv = CKR_PIN_LEN_RANGE0x000000A2UL;
4833 goto loser;
4834 }
4835 /* check the length of new pin, unless both old and new passwords
4836 * are empty */
4837 if ((ulNewLen != 0 || ulOldLen != 0) &&
4838 ulNewLen < (CK_ULONG)slot->minimumPinLen) {
4839 crv = CKR_PIN_LEN_RANGE0x000000A2UL;
4840 goto loser;
4841 }
4842
4843 /* convert to null terminated string */
4844 PORT_Memcpymemcpy(newPinStr, pNewPin, ulNewLen);
4845 newPinStr[ulNewLen] = 0;
4846 PORT_Memcpymemcpy(oldPinStr, pOldPin, ulOldLen);
4847 oldPinStr[ulOldLen] = 0;
4848
4849 /* change the data base password */
4850 PR_Lock(slot->pwCheckLock);
4851 rv = sftkdb_ChangePassword(handle, oldPinStr, newPinStr, &tokenRemoved);
4852 PORT_Memsetmemset(newPinStr, 0, ulNewLen);
4853 PORT_Memsetmemset(oldPinStr, 0, ulOldLen);
4854 if (tokenRemoved) {
4855 sftk_CloseAllSessions(slot, PR_FALSE0);
4856 }
4857 if ((rv != SECSuccess) && (sftk_isFIPS(slot->slotID)(((slot->slotID) == 3) || ((slot->slotID) >= 101)))) {
4858 PR_Sleep(loginWaitTime);
4859 }
4860 PR_Unlock(slot->pwCheckLock);
4861
4862 /* Now update our local copy of the pin */
4863 if (rv == SECSuccess) {
4864 PR_Lock(slot->slotLock);
4865 slot->needLogin = (PRBool)(ulNewLen != 0);
4866 slot->isLoggedIn = (PRBool)(sftkdb_PWCached(handle) == SECSuccess);
4867 PR_Unlock(slot->slotLock);
4868 /* Reset login flags. */
4869 if (ulNewLen == 0) {
4870 PR_Lock(slot->slotLock);
4871 slot->isLoggedIn = PR_FALSE0;
4872 slot->ssoLoggedIn = PR_FALSE0;
4873 PR_Unlock(slot->slotLock);
4874
4875 tokenRemoved = PR_FALSE0;
4876 rv = sftkdb_CheckPasswordNull(handle, &tokenRemoved);
Value stored to 'rv' is never read
4877 if (tokenRemoved) {
4878 sftk_CloseAllSessions(slot, PR_FALSE0);
4879 }
4880 }
4881 sftk_update_all_states(slot);
4882 sftk_freeDB(handle);
4883 return CKR_OK0x00000000UL;
4884 }
4885 crv = CKR_PIN_INCORRECT0x000000A0UL;
4886loser:
4887 if (sp) {
4888 sftk_FreeSession(sp);
4889 }
4890 if (handle) {
4891 sftk_freeDB(handle);
4892 }
4893 return crv;
4894}
4895
4896/* NSC_OpenSession opens a session between an application and a token. */
4897CK_RV
4898NSC_OpenSession(CK_SLOT_ID slotID, CK_FLAGS flags,
4899 CK_VOID_PTR pApplication, CK_NOTIFY Notify, CK_SESSION_HANDLE_PTR phSession)
4900{
4901 SFTKSlot *slot;
4902 CK_SESSION_HANDLE sessionID;
4903 SFTKSession *session;
4904 SFTKSession *sameID;
4905
4906 CHECK_FORK();
4907
4908 slot = sftk_SlotFromID(slotID, PR_FALSE0);
4909 if (slot == NULL((void*)0))
4910 return CKR_SLOT_ID_INVALID0x00000003UL;
4911
4912 /* new session (we only have serial sessions) */
4913 session = sftk_NewSession(slotID, Notify, pApplication,
4914 flags | CKF_SERIAL_SESSION0x00000004UL);
4915 if (session == NULL((void*)0))
4916 return CKR_HOST_MEMORY0x00000002UL;
4917
4918 if (slot->readOnly && (flags & CKF_RW_SESSION0x00000002UL)) {
4919 /* NETSCAPE_SLOT_ID is Read ONLY */
4920 session->info.flags &= ~CKF_RW_SESSION0x00000002UL;
4921 }
4922 PR_Lock(slot->slotLock);
4923 ++slot->sessionCount;
4924 if (session->info.flags & CKF_RW_SESSION0x00000002UL) {
4925 ++slot->rwSessionCount;
4926 }
4927 PR_Unlock(slot->slotLock);
4928
4929 do {
4930 PRLock *lock;
4931 do {
4932 sessionID = (PR_ATOMIC_INCREMENT(&slot->sessionIDCount)__sync_add_and_fetch(&slot->sessionIDCount, 1) & 0xffffff) | (slot->index << 24);
4933 } while (sessionID == CK_INVALID_HANDLE0);
4934 lock = SFTK_SESSION_LOCK(slot, sessionID)((slot)->sessionLock[((((PRUint32)(((sessionID)) * 1791398085
) & ((slot)->sessHashSize - 1))) >> 0) & (slot
)->sessionLockMask])
;
4935 PR_Lock(lock);
4936 sftkqueue_find(sameID, sessionID, slot->head, slot->sessHashSize)for ((sameID) = (slot->head)[((PRUint32)((sessionID) * 1791398085
) & (slot->sessHashSize - 1))]; (sameID) != ((void*)0)
; (sameID) = (sameID)->next) { if ((sameID)->handle == (
sessionID)) { break; } }
;
4937 if (sameID == NULL((void*)0)) {
4938 session->handle = sessionID;
4939 sftk_update_state(slot, session);
4940 sftkqueue_add(session, sessionID, slot->head, slot->sessHashSize){ int tmp = ((PRUint32)((sessionID) * 1791398085) & (slot
->sessHashSize - 1)); (session)->next = (slot->head)
[tmp]; (session)->prev = ((void*)0); if ((slot->head)[tmp
]) (slot->head)[tmp]->prev = (session); (slot->head)
[tmp] = (session); }
;
4941 } else {
4942 slot->sessionIDConflict++; /* for debugging */
4943 }
4944 PR_Unlock(lock);
4945 } while (sameID != NULL((void*)0));
4946
4947 *phSession = sessionID;
4948 return CKR_OK0x00000000UL;
4949}
4950
4951/* NSC_CloseSession closes a session between an application and a token. */
4952CK_RV
4953NSC_CloseSession(CK_SESSION_HANDLE hSession)
4954{
4955 SFTKSlot *slot;
4956 SFTKSession *session;
4957 PRBool sessionFound;
4958 PRLock *lock;
4959
4960 CHECK_FORK();
4961
4962 session = sftk_SessionFromHandle(hSession);
4963 if (session == NULL((void*)0))
4964 return CKR_SESSION_HANDLE_INVALID0x000000B3UL;
4965 slot = sftk_SlotFromSession(session)((session)->slot);
4966 sessionFound = PR_FALSE0;
4967
4968 /* lock */
4969 lock = SFTK_SESSION_LOCK(slot, hSession)((slot)->sessionLock[((((PRUint32)(((hSession)) * 1791398085
) & ((slot)->sessHashSize - 1))) >> 0) & (slot
)->sessionLockMask])
;
4970 PR_Lock(lock);
4971 if (sftkqueue_is_queued(session, hSession, slot->head, slot->sessHashSize)(((session)->next) || ((session)->prev) || ((slot->head
)[((PRUint32)((hSession) * 1791398085) & (slot->sessHashSize
- 1))] == (session)))
) {
4972 sessionFound = PR_TRUE1;
4973 sftkqueue_delete(session, hSession, slot->head, slot->sessHashSize)if ((session)->next) (session)->next->prev = (session
)->prev; if ((session)->prev) (session)->prev->next
= (session)->next; else (slot->head)[((PRUint32)((hSession
) * 1791398085) & (slot->sessHashSize - 1))] = ((session
)->next); (session)->next = ((void*)0); (session)->prev
= ((void*)0);
;
4974 /* Drop the bucket's reference. We still hold the reference taken
4975 * by sftk_SessionFromHandle, so refCount cannot reach 0 here. */
4976 PORT_ReleaseAssert(session->refCount > 1)((session->refCount > 1) ? ((void)0) : PR_Assert("session->refCount > 1"
, "/root/firefox-clang/security/nss/lib/softoken/pkcs11.c", 4976
))
;
4977 session->refCount--;
4978 }
4979 PR_Unlock(lock);
4980
4981 if (sessionFound) {
4982 SFTKDBHandle *handle;
4983 handle = sftk_getKeyDB(slot);
4984 PR_Lock(slot->slotLock);
4985 if (--slot->sessionCount == 0) {
4986 slot->isLoggedIn = PR_FALSE0;
4987 if (slot->needLogin && handle) {
4988 sftkdb_ClearPassword(handle);
4989 }
4990 }
4991 if (session->info.flags & CKF_RW_SESSION0x00000002UL) {
4992 --slot->rwSessionCount;
4993 }
4994 PR_Unlock(slot->slotLock);
4995 if (handle) {
4996 sftk_freeDB(handle);
4997 }
4998 }
4999
5000 /* Drop the lookup reference. Whichever caller drives refCount to 0
5001 * destroys the session. */
5002 sftk_FreeSession(session);
5003 return CKR_OK0x00000000UL;
5004}
5005
5006/* NSC_CloseAllSessions closes all sessions with a token. */
5007CK_RV
5008NSC_CloseAllSessions(CK_SLOT_ID slotID)
5009{
5010 SFTKSlot *slot;
5011
5012#ifndef NO_FORK_CHECK
5013 /* skip fork check if we are being called from C_Initialize or C_Finalize */
5014 if (!parentForkedAfterC_Initialize) {
5015 CHECK_FORK();
5016 }
5017#endif
5018
5019 slot = sftk_SlotFromID(slotID, PR_FALSE0);
5020 if (slot == NULL((void*)0))
5021 return CKR_SLOT_ID_INVALID0x00000003UL;
5022
5023 return sftk_CloseAllSessions(slot, PR_TRUE1);
5024}
5025
5026/* NSC_GetSessionInfo obtains information about the session. */
5027CK_RV
5028NSC_GetSessionInfo(CK_SESSION_HANDLE hSession,
5029 CK_SESSION_INFO_PTR pInfo)
5030{
5031 SFTKSession *session;
5032
5033 CHECK_FORK();
5034
5035 session = sftk_SessionFromHandle(hSession);
5036 if (session == NULL((void*)0))
5037 return CKR_SESSION_HANDLE_INVALID0x000000B3UL;
5038
5039 PORT_Memcpymemcpy(pInfo, &session->info, sizeof(CK_SESSION_INFO));
5040 sftk_FreeSession(session);
5041 return CKR_OK0x00000000UL;
5042}
5043
5044/* NSC_Login logs a user into a token. */
5045CK_RV
5046NSC_Login(CK_SESSION_HANDLE hSession, CK_USER_TYPE userType,
5047 CK_CHAR_PTR pPin, CK_ULONG ulPinLen)
5048{
5049 SFTKSlot *slot;
5050 SFTKSession *session;
5051 SFTKDBHandle *handle;
5052 CK_FLAGS sessionFlags;
5053 SECStatus rv;
5054 CK_RV crv;
5055 char pinStr[SFTK_MAX_PIN500 + 1];
5056 PRBool tokenRemoved = PR_FALSE0;
5057 PRBool isLoggedIn;
5058 PRBool needLogin;
5059
5060 CHECK_FORK();
5061
5062 /* get the slot */
5063 slot = sftk_SlotFromSessionHandle(hSession);
5064 if (slot == NULL((void*)0)) {
5065 return CKR_SESSION_HANDLE_INVALID0x000000B3UL;
5066 }
5067
5068 /* make sure the session is valid */
5069 session = sftk_SessionFromHandle(hSession);
5070 if (session == NULL((void*)0)) {
5071 return CKR_SESSION_HANDLE_INVALID0x000000B3UL;
5072 }
5073 sessionFlags = session->info.flags;
5074 sftk_FreeSession(session);
5075 session = NULL((void*)0);
5076
5077 /* can't log into the Netscape Slot */
5078 if (slot->slotID == NETSCAPE_SLOT_ID1) {
5079 return CKR_USER_TYPE_INVALID0x00000103UL;
5080 }
5081
5082 PR_Lock(slot->slotLock);
5083 isLoggedIn = slot->isLoggedIn;
5084 needLogin = slot->needLogin;
5085 PR_Unlock(slot->slotLock);
5086
5087 if (isLoggedIn)
5088 return CKR_USER_ALREADY_LOGGED_IN0x00000100UL;
5089 if (!needLogin) {
5090 return ulPinLen ? CKR_PIN_INCORRECT0x000000A0UL : CKR_OK0x00000000UL;
5091 }
5092
5093 PR_Lock(slot->slotLock);
5094 slot->ssoLoggedIn = PR_FALSE0;
5095 PR_Unlock(slot->slotLock);
5096
5097 if (ulPinLen > SFTK_MAX_PIN500)
5098 return CKR_PIN_LEN_RANGE0x000000A2UL;
5099
5100 /* convert to null terminated string */
5101 if (ulPinLen) {
5102 PORT_Memcpymemcpy(pinStr, pPin, ulPinLen);
5103 }
5104 pinStr[ulPinLen] = 0;
5105
5106 handle = sftk_getKeyDB(slot);
5107 if (handle == NULL((void*)0)) {
5108 PORT_Memsetmemset(pinStr, 0, ulPinLen);
5109 return CKR_USER_TYPE_INVALID0x00000103UL;
5110 }
5111
5112 /*
5113 * Deal with bootstrap. We allow the SSO to login in with a NULL
5114 * password if and only if we haven't initialized the KEY DB yet.
5115 * We only allow this on a RW session.
5116 */
5117 rv = sftkdb_HasPasswordSet(handle);
5118 if (rv == SECFailure) {
5119 /* allow SSO's to log in only if there is not password on the
5120 * key database */
5121 if (((userType == CKU_SO0) && (sessionFlags & CKF_RW_SESSION0x00000002UL))
5122 /* fips always needs to authenticate, even if there isn't a db */
5123 || (sftk_isFIPS(slot->slotID)(((slot->slotID) == 3) || ((slot->slotID) >= 101)))) {
5124 /* should this be a fixed password? */
5125 if (ulPinLen == 0) {
5126 sftkdb_ClearPassword(handle);
5127 PR_Lock(slot->slotLock);
5128 slot->isLoggedIn = PR_TRUE1;
5129 slot->ssoLoggedIn = (PRBool)(userType == CKU_SO0);
5130 PR_Unlock(slot->slotLock);
5131 sftk_update_all_states(slot);
5132 crv = CKR_OK0x00000000UL;
5133 goto done;
5134 }
5135 crv = CKR_PIN_INCORRECT0x000000A0UL;
5136 goto done;
5137 }
5138 crv = CKR_USER_TYPE_INVALID0x00000103UL;
5139 goto done;
5140 }
5141
5142 /* don't allow the SSO to log in if the user is already initialized */
5143 if (userType != CKU_USER1) {
5144 crv = CKR_USER_TYPE_INVALID0x00000103UL;
5145 goto done;
5146 }
5147
5148 /* build the hashed pins which we pass around */
5149 PR_Lock(slot->pwCheckLock);
5150 rv = sftkdb_CheckPassword(handle, pinStr, &tokenRemoved);
5151 if (tokenRemoved) {
5152 sftk_CloseAllSessions(slot, PR_FALSE0);
5153 }
5154 if ((rv != SECSuccess) && (sftk_isFIPS(slot->slotID)(((slot->slotID) == 3) || ((slot->slotID) >= 101)))) {
5155 PR_Sleep(loginWaitTime);
5156 }
5157 PR_Unlock(slot->pwCheckLock);
5158 if (rv == SECSuccess) {
5159 PR_Lock(slot->slotLock);
5160 /* make sure the login state matches the underlying
5161 * database state */
5162 slot->isLoggedIn = sftkdb_PWCached(handle) == SECSuccess ? PR_TRUE1 : PR_FALSE0;
5163 PR_Unlock(slot->slotLock);
5164
5165 sftk_freeDB(handle);
5166 handle = NULL((void*)0);
5167
5168 /* update all sessions */
5169 sftk_update_all_states(slot);
5170 return CKR_OK0x00000000UL;
5171 }
5172
5173 crv = CKR_PIN_INCORRECT0x000000A0UL;
5174done:
5175 PORT_Memsetmemset(pinStr, 0, ulPinLen);
5176 if (handle) {
5177 sftk_freeDB(handle);
5178 }
5179 return crv;
5180}
5181
5182CK_RV
5183NSC_LoginUser(CK_SESSION_HANDLE hSession, CK_USER_TYPE userType,
5184 CK_CHAR_PTR pPin, CK_ULONG ulPinLen, CK_UTF8CHAR_PTR pUsername,
5185 CK_ULONG ulUsernameLen)
5186{
5187 /* softoken currently does not support additional users */
5188 return CKR_OPERATION_NOT_INITIALIZED0x00000091UL;
5189}
5190
5191/* NSC_Logout logs a user out from a token. */
5192CK_RV
5193NSC_Logout(CK_SESSION_HANDLE hSession)
5194{
5195 SFTKSlot *slot = sftk_SlotFromSessionHandle(hSession);
5196 SFTKSession *session;
5197 SFTKDBHandle *handle;
5198
5199 CHECK_FORK();
5200
5201 if (slot == NULL((void*)0)) {
5202 return CKR_SESSION_HANDLE_INVALID0x000000B3UL;
5203 }
5204 session = sftk_SessionFromHandle(hSession);
5205 if (session == NULL((void*)0))
5206 return CKR_SESSION_HANDLE_INVALID0x000000B3UL;
5207 sftk_FreeSession(session);
5208 session = NULL((void*)0);
5209
5210 PR_Lock(slot->slotLock);
5211 PRBool isLoggedIn = slot->isLoggedIn;
5212 PR_Unlock(slot->slotLock);
5213 if (!isLoggedIn) {
5214 return CKR_USER_NOT_LOGGED_IN0x00000101UL;
5215 }
5216
5217 handle = sftk_getKeyDB(slot);
5218 PR_Lock(slot->slotLock);
5219 slot->isLoggedIn = PR_FALSE0;
5220 slot->ssoLoggedIn = PR_FALSE0;
5221 if (slot->needLogin && handle) {
5222 sftkdb_ClearPassword(handle);
5223 }
5224 PR_Unlock(slot->slotLock);
5225 if (handle) {
5226 sftk_freeDB(handle);
5227 }
5228
5229 sftk_update_all_states(slot);
5230 return CKR_OK0x00000000UL;
5231}
5232
5233/*
5234 * Create or remove a new slot on the fly.
5235 * When creating a slot, "slot" is the slot that the request came from. The
5236 * resulting slot will live in the same module as "slot".
5237 * When removing a slot, "slot" is the slot to be removed.
5238 * "object" is the creation object that specifies the module spec for the slot
5239 * to add or remove.
5240 */
5241static CK_RV
5242sftk_CreateNewSlot(SFTKSlot *slot, CK_OBJECT_CLASS class,
5243 SFTKObject *object)
5244{
5245 PRBool isValidUserSlot = PR_FALSE0;
5246 PRBool isValidFIPSUserSlot = PR_FALSE0;
5247 PRBool isValidSlot = PR_FALSE0;
5248 PRBool isFIPS = PR_FALSE0;
5249 unsigned int moduleIndex = NSC_NON_FIPS_MODULE0;
5250 SFTKAttribute *attribute;
5251 sftk_parameters paramStrings;
5252 CK_SLOT_ID slotID = 0;
5253 SFTKSlot *newSlot = NULL((void*)0);
5254 CK_RV crv = CKR_OK0x00000000UL;
5255
5256 if (class != CKO_NSS_DELSLOT((0x80000000UL | 0x4E534350) + 6) && class != CKO_NSS_NEWSLOT((0x80000000UL | 0x4E534350) + 5)) {
5257 return CKR_ATTRIBUTE_VALUE_INVALID0x00000013UL;
5258 }
5259 if (class == CKO_NSS_NEWSLOT((0x80000000UL | 0x4E534350) + 5) && slot->slotID == FIPS_SLOT_ID3) {
5260 isFIPS = PR_TRUE1;
5261 }
5262 attribute = sftk_FindAttribute(object, CKA_NSS_MODULE_SPEC((0x80000000UL | 0x4E534350) + 24));
5263 if (attribute == NULL((void*)0)) {
5264 return CKR_TEMPLATE_INCOMPLETE0x000000D0UL;
5265 }
5266 /* sftk_parseParameters scans attribute->attrib.pValue as a C string;
5267 * reject if it isn't NUL-terminated within ulValueLen. */
5268 if (attribute->attrib.ulValueLen == 0 ||
5269 memchr(attribute->attrib.pValue, '\0',
5270 attribute->attrib.ulValueLen) == NULL((void*)0)) {
5271 crv = CKR_ATTRIBUTE_VALUE_INVALID0x00000013UL;
5272 goto loser;
5273 }
5274 crv = sftk_parseParameters((char *)attribute->attrib.pValue,
5275 &paramStrings, isFIPS);
5276 if (crv != CKR_OK0x00000000UL) {
5277 goto loser;
5278 }
5279
5280 /* enforce only one at a time */
5281 if (paramStrings.token_count != 1) {
5282 crv = CKR_ATTRIBUTE_VALUE_INVALID0x00000013UL;
5283 goto loser;
5284 }
5285
5286 slotID = paramStrings.tokens[0].slotID;
5287
5288 /* stay within the valid ID space */
5289 isValidUserSlot = (slotID >= SFTK_MIN_USER_SLOT_ID4 &&
5290 slotID <= SFTK_MAX_USER_SLOT_ID100);
5291 isValidFIPSUserSlot = (slotID >= SFTK_MIN_FIPS_USER_SLOT_ID101 &&
5292 slotID <= SFTK_MAX_FIPS_USER_SLOT_ID127);
5293
5294 if (class == CKO_NSS_DELSLOT((0x80000000UL | 0x4E534350) + 6)) {
5295 if (slot->slotID == slotID) {
5296 isValidSlot = isValidUserSlot || isValidFIPSUserSlot;
5297 }
5298 } else {
5299 /* only the crypto or FIPS slots can create new slot objects */
5300 if (slot->slotID == NETSCAPE_SLOT_ID1) {
5301 isValidSlot = isValidUserSlot;
5302 moduleIndex = NSC_NON_FIPS_MODULE0;
5303 } else if (slot->slotID == FIPS_SLOT_ID3) {
5304 isValidSlot = isValidFIPSUserSlot;
5305 moduleIndex = NSC_FIPS_MODULE1;
5306 }
5307 }
5308
5309 if (!isValidSlot) {
5310 crv = CKR_ATTRIBUTE_VALUE_INVALID0x00000013UL;
5311 goto loser;
5312 }
5313
5314 /* unload any existing slot at this id */
5315 newSlot = sftk_SlotFromID(slotID, PR_TRUE1);
5316 if (newSlot && newSlot->present) {
5317 crv = SFTK_ShutdownSlot(newSlot);
5318 if (crv != CKR_OK0x00000000UL) {
5319 goto loser;
5320 }
5321 }
5322
5323 /* if we were just planning on deleting the slot, then do so now */
5324 if (class == CKO_NSS_DELSLOT((0x80000000UL | 0x4E534350) + 6)) {
5325 /* sort of a unconventional use of this error code, be we are
5326 * overusing CKR_ATTRIBUTE_VALUE_INVALID, and it does apply */
5327 crv = newSlot ? CKR_OK0x00000000UL : CKR_SLOT_ID_INVALID0x00000003UL;
5328 goto loser; /* really exit */
5329 }
5330
5331 if (newSlot) {
5332 crv = SFTK_SlotReInit(newSlot, paramStrings.configdir,
5333 paramStrings.updatedir, paramStrings.updateID,
5334 &paramStrings.tokens[0], moduleIndex);
5335 } else {
5336 crv = SFTK_SlotInit(paramStrings.configdir,
5337 paramStrings.updatedir, paramStrings.updateID,
5338 &paramStrings.tokens[0], moduleIndex);
5339 }
5340
5341loser:
5342 sftk_freeParams(&paramStrings);
5343 sftk_FreeAttribute(attribute);
5344
5345 return crv;
5346}
5347
5348/* NSC_CreateObject creates a new object. */
5349CK_RV
5350NSC_CreateObject(CK_SESSION_HANDLE hSession,
5351 CK_ATTRIBUTE_PTR pTemplate, CK_ULONG ulCount,
5352 CK_OBJECT_HANDLE_PTR phObject)
5353{
5354 SFTKSlot *slot = sftk_SlotFromSessionHandle(hSession);
5355 SFTKSession *session;
5356 SFTKObject *object;
5357 /* make sure class isn't randomly CKO_NSS_NEWSLOT or
5358 * CKO_NETSCPE_DELSLOT. */
5359 CK_OBJECT_CLASS class = CKO_VENDOR_DEFINED0x80000000UL;
5360 CK_RV crv;
5361 int i;
5362
5363 CHECK_FORK();
5364
5365 *phObject = CK_INVALID_HANDLE0;
5366
5367 if (slot == NULL((void*)0)) {
5368 return CKR_SESSION_HANDLE_INVALID0x000000B3UL;
5369 }
5370 /*
5371 * now lets create an object to hang the attributes off of
5372 */
5373 object = sftk_NewObject(slot); /* fill in the handle later */
5374 if (object == NULL((void*)0)) {
5375 return CKR_HOST_MEMORY0x00000002UL;
5376 }
5377
5378 /*
5379 * sftk_NewObject will set object->validation_value to
5380 * SFTK_VALIDATION_FIPS_FLAG if the slot is FIPS.
5381 * We don't need to worry about that here, as FC_CreateObject will always
5382 * disallow the import of secret and private keys, regardless of isFIPS
5383 * approval status. Therefore, at this point we know that the key is a
5384 * public key, which is acceptable to be imported in plaintext.
5385 */
5386
5387 /*
5388 * load the template values into the object
5389 */
5390 for (i = 0; i < (int)ulCount; i++) {
5391 crv = sftk_AddAttributeType(object, sftk_attr_expand(&pTemplate[i])(&pTemplate[i])->type, (&pTemplate[i])->pValue,
(&pTemplate[i])->ulValueLen
);
5392 if (crv != CKR_OK0x00000000UL) {
5393 sftk_FreeObject(object);
5394 return crv;
5395 }
5396 if ((pTemplate[i].type == CKA_CLASS0x00000000UL) && pTemplate[i].pValue) {
5397 class = *(CK_OBJECT_CLASS *)pTemplate[i].pValue;
5398 }
5399 }
5400
5401 /* get the session */
5402 session = sftk_SessionFromHandle(hSession);
5403 if (session == NULL((void*)0)) {
5404 sftk_FreeObject(object);
5405 return CKR_SESSION_HANDLE_INVALID0x000000B3UL;
5406 }
5407
5408 /*
5409 * handle pseudo objects (CKO_NEWSLOT)
5410 */
5411 if ((class == CKO_NSS_NEWSLOT((0x80000000UL | 0x4E534350) + 5)) || (class == CKO_NSS_DELSLOT((0x80000000UL | 0x4E534350) + 6))) {
5412 crv = sftk_CreateNewSlot(slot, class, object);
5413 goto done;
5414 }
5415
5416 /*
5417 * handle the base object stuff
5418 */
5419 crv = sftk_handleObject(object, session);
5420 *phObject = object->handle;
5421done:
5422 sftk_FreeSession(session);
5423 sftk_FreeObject(object);
5424
5425 return crv;
5426}
5427
5428/* NSC_CopyObject copies an object, creating a new object for the copy. */
5429CK_RV
5430NSC_CopyObject(CK_SESSION_HANDLE hSession,
5431 CK_OBJECT_HANDLE hObject, CK_ATTRIBUTE_PTR pTemplate, CK_ULONG ulCount,
5432 CK_OBJECT_HANDLE_PTR phNewObject)
5433{
5434 SFTKObject *destObject, *srcObject;
5435 SFTKSession *session;
5436 CK_RV crv = CKR_OK0x00000000UL;
5437 SFTKSlot *slot = sftk_SlotFromSessionHandle(hSession);
5438 int i;
5439
5440 CHECK_FORK();
5441
5442 if (slot == NULL((void*)0)) {
5443 return CKR_SESSION_HANDLE_INVALID0x000000B3UL;
5444 }
5445 /* Get srcObject so we can find the class */
5446 session = sftk_SessionFromHandle(hSession);
5447 if (session == NULL((void*)0)) {
5448 return CKR_SESSION_HANDLE_INVALID0x000000B3UL;
5449 }
5450 srcObject = sftk_ObjectFromHandle(hObject, session);
5451 if (srcObject == NULL((void*)0)) {
5452 sftk_FreeSession(session);
5453 return CKR_OBJECT_HANDLE_INVALID0x00000082UL;
5454 }
5455 /*
5456 * create an object to hang the attributes off of
5457 */
5458 destObject = sftk_NewObject(slot); /* fill in the handle later */
5459 if (destObject == NULL((void*)0)) {
5460 sftk_FreeSession(session);
5461 sftk_FreeObject(srcObject);
5462 return CKR_HOST_MEMORY0x00000002UL;
5463 }
5464
5465 /*
5466 * load the template values into the object
5467 */
5468 for (i = 0; i < (int)ulCount; i++) {
5469 if (sftk_modifyType(pTemplate[i].type, srcObject->objclass) == SFTK_NEVER) {
5470 crv = CKR_ATTRIBUTE_READ_ONLY0x00000010UL;
5471 break;
5472 }
5473 crv = sftk_AddAttributeType(destObject, sftk_attr_expand(&pTemplate[i])(&pTemplate[i])->type, (&pTemplate[i])->pValue,
(&pTemplate[i])->ulValueLen
);
5474 if (crv != CKR_OK0x00000000UL) {
5475 break;
5476 }
5477 }
5478 if (crv != CKR_OK0x00000000UL) {
5479 sftk_FreeSession(session);
5480 sftk_FreeObject(srcObject);
5481 sftk_FreeObject(destObject);
5482 return crv;
5483 }
5484
5485 /* sensitive can only be changed to CK_TRUE */
5486 if (sftk_hasAttribute(destObject, CKA_SENSITIVE0x00000103UL)) {
5487 if (!sftk_isTrue(destObject, CKA_SENSITIVE0x00000103UL)) {
5488 sftk_FreeSession(session);
5489 sftk_FreeObject(srcObject);
5490 sftk_FreeObject(destObject);
5491 return CKR_ATTRIBUTE_READ_ONLY0x00000010UL;
5492 }
5493 }
5494
5495 /*
5496 * now copy the old attributes from the new attributes
5497 */
5498 /* don't create a token object if we aren't in a rw session */
5499 /* we need to hold the lock to copy a consistant version of
5500 * the object. */
5501 crv = sftk_CopyObject(destObject, srcObject);
5502
5503 destObject->objclass = srcObject->objclass;
5504 sftk_FreeObject(srcObject);
5505 if (crv != CKR_OK0x00000000UL) {
5506 sftk_FreeObject(destObject);
5507 sftk_FreeSession(session);
5508 return crv;
5509 }
5510
5511 crv = sftk_handleObject(destObject, session);
5512 *phNewObject = destObject->handle;
5513 sftk_FreeSession(session);
5514 sftk_FreeObject(destObject);
5515
5516 return crv;
5517}
5518
5519/* NSC_GetObjectSize gets the size of an object in bytes. */
5520CK_RV
5521NSC_GetObjectSize(CK_SESSION_HANDLE hSession,
5522 CK_OBJECT_HANDLE hObject, CK_ULONG_PTR pulSize)
5523{
5524 CHECK_FORK();
5525
5526 *pulSize = 0;
5527 return CKR_OK0x00000000UL;
5528}
5529
5530static CK_RV
5531nsc_GetTokenAttributeValue(SFTKSession *session, CK_OBJECT_HANDLE hObject,
5532 CK_ATTRIBUTE_PTR pTemplate, CK_ULONG ulCount)
5533{
5534 SFTKSlot *slot = sftk_SlotFromSession(session)((session)->slot);
5535 SFTKDBHandle *dbHandle = sftk_getDBForTokenObject(slot, hObject);
5536 SFTKDBHandle *keydb = NULL((void*)0);
5537 CK_RV crv;
5538
5539 if (dbHandle == NULL((void*)0)) {
5540 return CKR_OBJECT_HANDLE_INVALID0x00000082UL;
5541 }
5542
5543 crv = sftkdb_GetAttributeValue(dbHandle, hObject, pTemplate, ulCount);
5544
5545 /* make sure we don't export any sensitive information */
5546 keydb = sftk_getKeyDB(slot);
5547 if (dbHandle == keydb) {
5548 CK_ULONG i;
5549 for (i = 0; i < ulCount; i++) {
5550 if (sftk_isSensitive(pTemplate[i].type, CKO_PRIVATE_KEY0x00000003UL)) {
5551 crv = CKR_ATTRIBUTE_SENSITIVE0x00000011UL;
5552 if (pTemplate[i].pValue && (pTemplate[i].ulValueLen != -1)) {
5553 PORT_Memsetmemset(pTemplate[i].pValue, 0,
5554 pTemplate[i].ulValueLen);
5555 }
5556 pTemplate[i].ulValueLen = -1;
5557 }
5558 }
5559 }
5560
5561 sftk_freeDB(dbHandle);
5562 if (keydb) {
5563 sftk_freeDB(keydb);
5564 }
5565 return crv;
5566}
5567
5568PRBool
5569sftk_template_hasAttribute(CK_ATTRIBUTE_TYPE type, CK_ATTRIBUTE *pTemplate,
5570 CK_ULONG ulCount)
5571{
5572 for (int i = 0; i < ulCount; i++) {
5573 if (pTemplate[i].type == type) {
5574 return PR_TRUE1;
5575 }
5576 }
5577 return PR_FALSE0;
5578}
5579
5580/* NSC_GetAttributeValue obtains the value of one or more object attributes. */
5581CK_RV
5582NSC_GetAttributeValue(CK_SESSION_HANDLE hSession,
5583 CK_OBJECT_HANDLE hObject, CK_ATTRIBUTE_PTR pTemplate, CK_ULONG ulCount)
5584{
5585 SFTKSlot *slot = sftk_SlotFromSessionHandle(hSession);
5586 SFTKSession *session;
5587 SFTKObject *object;
5588 SFTKAttribute *attribute;
5589 PRBool sensitive, isLoggedIn, needLogin;
5590 CK_RV crv;
5591 int i;
5592
5593 CHECK_FORK();
5594
5595 if (slot == NULL((void*)0)) {
5596 return CKR_SESSION_HANDLE_INVALID0x000000B3UL;
5597 }
5598 /*
5599 * make sure we're allowed
5600 */
5601 session = sftk_SessionFromHandle(hSession);
5602 if (session == NULL((void*)0)) {
5603 return CKR_SESSION_HANDLE_INVALID0x000000B3UL;
5604 }
5605
5606 /* short circuit everything for token objects */
5607 if (sftk_isToken(hObject)(((hObject) & 0x80000000L) == 0x80000000L) &&
5608 /* we we have a CKA_OBJECT_VALIDATION_FLAG, don't do the
5609 * short circuit. the SDB code doesn't know how to process
5610 * this attribute */
5611 !sftk_template_hasAttribute(CKA_OBJECT_VALIDATION_FLAGS0x0000061eUL, pTemplate,
5612 ulCount)) {
5613 crv = nsc_GetTokenAttributeValue(session, hObject, pTemplate, ulCount);
5614 sftk_FreeSession(session);
5615 return crv;
5616 }
5617
5618 /* handle the session object */
5619 object = sftk_ObjectFromHandle(hObject, session);
5620 sftk_FreeSession(session);
5621 if (object == NULL((void*)0)) {
5622 return CKR_OBJECT_HANDLE_INVALID0x00000082UL;
5623 }
5624
5625 PR_Lock(slot->slotLock);
5626 isLoggedIn = slot->isLoggedIn;
5627 needLogin = slot->needLogin;
5628 PR_Unlock(slot->slotLock);
5629
5630 /* don't read a private object if we aren't logged in */
5631 if (!isLoggedIn && needLogin && sftk_isTrue(object, CKA_PRIVATE0x00000002UL)) {
5632 sftk_FreeObject(object);
5633 return CKR_USER_NOT_LOGGED_IN0x00000101UL;
5634 }
5635
5636 crv = CKR_OK0x00000000UL;
5637 sensitive = sftk_isTrue(object, CKA_SENSITIVE0x00000103UL);
5638 for (i = 0; i < (int)ulCount; i++) {
5639 /* Make sure that this attribute is retrievable */
5640 if (sensitive && sftk_isSensitive(pTemplate[i].type, object->objclass)) {
5641 crv = CKR_ATTRIBUTE_SENSITIVE0x00000011UL;
5642 pTemplate[i].ulValueLen = -1;
5643 continue;
5644 }
5645 attribute = sftk_FindAttribute(object, pTemplate[i].type);
5646 if (attribute == NULL((void*)0)) {
5647 crv = CKR_ATTRIBUTE_TYPE_INVALID0x00000012UL;
5648 pTemplate[i].ulValueLen = -1;
5649 continue;
5650 }
5651 if (pTemplate[i].pValue != NULL((void*)0)) {
5652 PORT_Memcpymemcpy(pTemplate[i].pValue, attribute->attrib.pValue,
5653 attribute->attrib.ulValueLen);
5654 }
5655 pTemplate[i].ulValueLen = attribute->attrib.ulValueLen;
5656 sftk_FreeAttribute(attribute);
5657 }
5658
5659 sftk_FreeObject(object);
5660 return crv;
5661}
5662
5663/* NSC_SetAttributeValue modifies the value of one or more object attributes */
5664CK_RV
5665NSC_SetAttributeValue(CK_SESSION_HANDLE hSession,
5666 CK_OBJECT_HANDLE hObject, CK_ATTRIBUTE_PTR pTemplate, CK_ULONG ulCount)
5667{
5668 SFTKSlot *slot = sftk_SlotFromSessionHandle(hSession);
5669 SFTKSession *session;
5670 SFTKAttribute *attribute;
5671 SFTKObject *object;
5672 PRBool isToken, isLoggedIn, needLogin;
5673 CK_RV crv = CKR_OK0x00000000UL;
5674 CK_BBOOL legal;
5675 int i;
5676
5677 CHECK_FORK();
5678
5679 if (slot == NULL((void*)0)) {
5680 return CKR_SESSION_HANDLE_INVALID0x000000B3UL;
5681 }
5682 /*
5683 * make sure we're allowed
5684 */
5685 session = sftk_SessionFromHandle(hSession);
5686 if (session == NULL((void*)0)) {
5687 return CKR_SESSION_HANDLE_INVALID0x000000B3UL;
5688 }
5689
5690 object = sftk_ObjectFromHandle(hObject, session);
5691 if (object == NULL((void*)0)) {
5692 sftk_FreeSession(session);
5693 return CKR_OBJECT_HANDLE_INVALID0x00000082UL;
5694 }
5695
5696 PR_Lock(slot->slotLock);
5697 isLoggedIn = slot->isLoggedIn;
5698 needLogin = slot->needLogin;
5699 PR_Unlock(slot->slotLock);
5700
5701 /* don't modify a private object if we aren't logged in */
5702 if (!isLoggedIn && needLogin && sftk_isTrue(object, CKA_PRIVATE0x00000002UL)) {
5703 sftk_FreeSession(session);
5704 sftk_FreeObject(object);
5705 return CKR_USER_NOT_LOGGED_IN0x00000101UL;
5706 }
5707
5708 /* don't modify a token object if we aren't in a rw session */
5709 isToken = sftk_isTrue(object, CKA_TOKEN0x00000001UL);
5710 if (((session->info.flags & CKF_RW_SESSION0x00000002UL) == 0) && isToken) {
5711 sftk_FreeSession(session);
5712 sftk_FreeObject(object);
5713 return CKR_SESSION_READ_ONLY0x000000B5UL;
5714 }
5715 sftk_FreeSession(session);
5716
5717 /* only change modifiable objects */
5718 if (!sftk_isTrue(object, CKA_MODIFIABLE0x00000170UL)) {
5719 sftk_FreeObject(object);
5720 return CKR_ATTRIBUTE_READ_ONLY0x00000010UL;
5721 }
5722
5723 for (i = 0; i < (int)ulCount; i++) {
5724 /* Make sure that this attribute is changeable */
5725 switch (sftk_modifyType(pTemplate[i].type, object->objclass)) {
5726 case SFTK_NEVER:
5727 case SFTK_ONCOPY:
5728 default:
5729 crv = CKR_ATTRIBUTE_READ_ONLY0x00000010UL;
5730 break;
5731
5732 case SFTK_SENSITIVE:
5733 legal = (pTemplate[i].type == CKA_EXTRACTABLE0x00000162UL) ? CK_FALSE0 : CK_TRUE1;
5734 if ((*(CK_BBOOL *)pTemplate[i].pValue) != legal) {
5735 crv = CKR_ATTRIBUTE_READ_ONLY0x00000010UL;
5736 }
5737 break;
5738 case SFTK_ALWAYS:
5739 break;
5740 }
5741 if (crv != CKR_OK0x00000000UL)
5742 break;
5743
5744 /* find the old attribute */
5745 attribute = sftk_FindAttribute(object, pTemplate[i].type);
5746 if (attribute == NULL((void*)0)) {
5747 crv = CKR_ATTRIBUTE_TYPE_INVALID0x00000012UL;
5748 break;
5749 }
5750 sftk_FreeAttribute(attribute);
5751 crv = sftk_forceAttribute(object, sftk_attr_expand(&pTemplate[i])(&pTemplate[i])->type, (&pTemplate[i])->pValue,
(&pTemplate[i])->ulValueLen
);
5752 if (crv != CKR_OK0x00000000UL)
5753 break;
5754 }
5755
5756 sftk_FreeObject(object);
5757 return crv;
5758}
5759
5760static CK_RV
5761sftk_expandSearchList(SFTKSearchResults *search, int count)
5762{
5763 search->array_size += count;
5764 search->handles = (CK_OBJECT_HANDLE *)PORT_ReallocPORT_Realloc_Util(search->handles,
5765 sizeof(CK_OBJECT_HANDLE) * search->array_size);
5766 return search->handles ? CKR_OK0x00000000UL : CKR_HOST_MEMORY0x00000002UL;
5767}
5768
5769static CK_RV
5770sftk_searchDatabase(SFTKDBHandle *handle, SFTKSearchResults *search,
5771 const CK_ATTRIBUTE *pTemplate, CK_ULONG ulCount)
5772{
5773 CK_RV crv;
5774 int objectListSize = search->array_size - search->size;
5775 CK_OBJECT_HANDLE *array = &search->handles[search->size];
5776 SDBFind *find = NULL((void*)0);
5777 CK_ULONG count;
5778
5779 crv = sftkdb_FindObjectsInit(handle, pTemplate, ulCount, &find);
5780 if (crv != CKR_OK0x00000000UL)
5781 return crv;
5782 do {
5783 crv = sftkdb_FindObjects(handle, find, array, objectListSize, &count);
5784 if ((crv != CKR_OK0x00000000UL) || (count == 0))
5785 break;
5786 search->size += count;
5787 objectListSize -= count;
5788 if (objectListSize > 0)
5789 break;
5790 crv = sftk_expandSearchList(search, NSC_SEARCH_BLOCK_SIZE5);
5791 objectListSize = NSC_SEARCH_BLOCK_SIZE5;
5792 array = &search->handles[search->size];
5793 } while (crv == CKR_OK0x00000000UL);
5794 sftkdb_FindObjectsFinal(handle, find);
5795
5796 return crv;
5797}
5798
5799/* softoken used to search the SMimeEntries automatically instead of
5800 * doing this in pk11wrap. This code should really be up in
5801 * pk11wrap so that it will work with other tokens other than softoken.
5802 */
5803CK_RV
5804sftk_emailhack(SFTKSlot *slot, SFTKDBHandle *handle,
5805 SFTKSearchResults *search, CK_ATTRIBUTE *pTemplate, CK_ULONG ulCount)
5806{
5807 PRBool isCert = PR_FALSE0;
5808 int emailIndex = -1;
5809 unsigned int i;
5810 SFTKSearchResults smime_search;
5811 CK_ATTRIBUTE smime_template[2];
5812 CK_OBJECT_CLASS smime_class = CKO_NSS_SMIME((0x80000000UL | 0x4E534350) + 2);
5813 SFTKAttribute *attribute = NULL((void*)0);
5814 SFTKObject *object = NULL((void*)0);
5815 CK_RV crv = CKR_OK0x00000000UL;
5816
5817 smime_search.handles = NULL((void*)0); /* paranoia, some one is bound to add a goto
5818 * loser before this gets initialized */
5819
5820 /* see if we are looking for email certs */
5821 for (i = 0; i < ulCount; i++) {
5822 if (pTemplate[i].type == CKA_CLASS0x00000000UL) {
5823 if ((pTemplate[i].ulValueLen != sizeof(CK_OBJECT_CLASS) ||
5824 (*(CK_OBJECT_CLASS *)pTemplate[i].pValue) != CKO_CERTIFICATE0x00000001UL)) {
5825 /* not a cert, skip out */
5826 break;
5827 }
5828 isCert = PR_TRUE1;
5829 } else if (pTemplate[i].type == CKA_NSS_EMAIL((0x80000000UL | 0x4E534350) + 2)) {
5830 emailIndex = i;
5831 }
5832 if (isCert && (emailIndex != -1))
5833 break;
5834 }
5835
5836 if (!isCert || (emailIndex == -1)) {
5837 return CKR_OK0x00000000UL;
5838 }
5839
5840 /* we are doing a cert and email search, find the SMimeEntry */
5841 smime_template[0].type = CKA_CLASS0x00000000UL;
5842 smime_template[0].pValue = &smime_class;
5843 smime_template[0].ulValueLen = sizeof(smime_class);
5844 smime_template[1] = pTemplate[emailIndex];
5845
5846 smime_search.handles = (CK_OBJECT_HANDLE *)
5847 PORT_AllocPORT_Alloc_Util(sizeof(CK_OBJECT_HANDLE) * NSC_SEARCH_BLOCK_SIZE5);
5848 if (smime_search.handles == NULL((void*)0)) {
5849 crv = CKR_HOST_MEMORY0x00000002UL;
5850 goto loser;
5851 }
5852 smime_search.index = 0;
5853 smime_search.size = 0;
5854 smime_search.array_size = NSC_SEARCH_BLOCK_SIZE5;
5855
5856 crv = sftk_searchDatabase(handle, &smime_search, smime_template, 2);
5857 if (crv != CKR_OK0x00000000UL || smime_search.size == 0) {
5858 goto loser;
5859 }
5860
5861 /* get the SMime subject */
5862 object = sftk_NewTokenObject(slot, NULL((void*)0), smime_search.handles[0]);
5863 if (object == NULL((void*)0)) {
5864 crv = CKR_HOST_MEMORY0x00000002UL; /* is there any other reason for this failure? */
5865 goto loser;
5866 }
5867 attribute = sftk_FindAttribute(object, CKA_SUBJECT0x00000101UL);
5868 if (attribute == NULL((void*)0)) {
5869 crv = CKR_ATTRIBUTE_TYPE_INVALID0x00000012UL;
5870 goto loser;
5871 }
5872
5873 /* now find the certs with that subject */
5874 pTemplate[emailIndex] = attribute->attrib;
5875 /* now add the appropriate certs to the search list */
5876 crv = sftk_searchDatabase(handle, search, pTemplate, ulCount);
5877 pTemplate[emailIndex] = smime_template[1]; /* restore the user's template*/
5878
5879loser:
5880 if (attribute) {
5881 sftk_FreeAttribute(attribute);
5882 }
5883 if (object) {
5884 sftk_FreeObject(object);
5885 }
5886 if (smime_search.handles) {
5887 PORT_FreePORT_Free_Util(smime_search.handles);
5888 }
5889
5890 return crv;
5891}
5892
5893static void
5894sftk_pruneSearch(CK_ATTRIBUTE *pTemplate, CK_ULONG ulCount,
5895 PRBool *searchCertDB, PRBool *searchKeyDB)
5896{
5897 CK_ULONG i;
5898
5899 *searchCertDB = PR_TRUE1;
5900 *searchKeyDB = PR_TRUE1;
5901 for (i = 0; i < ulCount; i++) {
5902 if (pTemplate[i].type == CKA_CLASS0x00000000UL && pTemplate[i].pValue != NULL((void*)0)) {
5903 CK_OBJECT_CLASS class = *((CK_OBJECT_CLASS *)pTemplate[i].pValue);
5904 if (class == CKO_PRIVATE_KEY0x00000003UL || class == CKO_SECRET_KEY0x00000004UL) {
5905 *searchCertDB = PR_FALSE0;
5906 } else {
5907 *searchKeyDB = PR_FALSE0;
5908 }
5909 break;
5910 }
5911 }
5912}
5913
5914static CK_RV
5915sftk_searchTokenList(SFTKSlot *slot, SFTKSearchResults *search,
5916 CK_ATTRIBUTE *pTemplate, CK_ULONG ulCount,
5917 PRBool isLoggedIn)
5918{
5919 CK_RV crv = CKR_OK0x00000000UL;
5920 CK_RV crv2;
5921 PRBool searchCertDB;
5922 PRBool searchKeyDB;
5923
5924 sftk_pruneSearch(pTemplate, ulCount, &searchCertDB, &searchKeyDB);
5925
5926 if (searchCertDB) {
5927 SFTKDBHandle *certHandle = sftk_getCertDB(slot);
5928 crv = sftk_searchDatabase(certHandle, search, pTemplate, ulCount);
5929 crv2 = sftk_emailhack(slot, certHandle, search, pTemplate, ulCount);
5930 if (crv == CKR_OK0x00000000UL)
5931 crv = crv2;
5932 sftk_freeDB(certHandle);
5933 }
5934
5935 if (crv == CKR_OK0x00000000UL && isLoggedIn && searchKeyDB) {
5936 SFTKDBHandle *keyHandle = sftk_getKeyDB(slot);
5937 crv = sftk_searchDatabase(keyHandle, search, pTemplate, ulCount);
5938 sftk_freeDB(keyHandle);
5939 }
5940 return crv;
5941}
5942
5943/* Atomically install a search, returning whatever was previously installed
5944 * (which the caller must free). Without the lock, two concurrent
5945 * FindObjectsInit calls each read NULL, then one's later assignment
5946 * overwrites the other's, leaking the loser. The lock also protects the
5947 * "take" side, so a concurrent FindObjects/FindObjectsFinal can't observe
5948 * a torn replacement. */
5949static SFTKSearchResults *
5950sftk_SwapSearch(SFTKSession *session, SFTKSearchResults *new_search)
5951{
5952 SFTKSlot *slot = sftk_SlotFromSession(session)((session)->slot);
5953 PRLock *lock = SFTK_SESSION_LOCK(slot, session->handle)((slot)->sessionLock[((((PRUint32)(((session->handle)) *
1791398085) & ((slot)->sessHashSize - 1))) >> 0
) & (slot)->sessionLockMask])
;
5954 SFTKSearchResults *prev;
5955 PR_Lock(lock);
5956 prev = session->search;
5957 session->search = new_search;
5958 PR_Unlock(lock);
5959 return prev;
5960}
5961
5962/* Try to reinstall a search after a partial consume. If a concurrent
5963 * FindObjectsInit replaced the search while we worked, our search is
5964 * stale; signal the caller to free it. */
5965static PRBool
5966sftk_RestoreSearch(SFTKSession *session, SFTKSearchResults *search)
5967{
5968 SFTKSlot *slot = sftk_SlotFromSession(session)((session)->slot);
5969 PRLock *lock = SFTK_SESSION_LOCK(slot, session->handle)((slot)->sessionLock[((((PRUint32)(((session->handle)) *
1791398085) & ((slot)->sessHashSize - 1))) >> 0
) & (slot)->sessionLockMask])
;
5970 PRBool restored;
5971 PR_Lock(lock);
5972 if (session->search == NULL((void*)0)) {
5973 session->search = search;
5974 restored = PR_TRUE1;
5975 } else {
5976 restored = PR_FALSE0;
5977 }
5978 PR_Unlock(lock);
5979 return restored;
5980}
5981
5982/* NSC_FindObjectsInit initializes a search for token and session objects
5983 * that match a template. */
5984CK_RV
5985NSC_FindObjectsInit(CK_SESSION_HANDLE hSession,
5986 CK_ATTRIBUTE_PTR pTemplate, CK_ULONG ulCount)
5987{
5988 SFTKSearchResults *search = NULL((void*)0), *freeSearch = NULL((void*)0);
5989 SFTKSession *session = NULL((void*)0);
5990 SFTKSlot *slot = sftk_SlotFromSessionHandle(hSession);
5991 CK_RV crv = CKR_OK0x00000000UL;
5992 PRBool isLoggedIn;
5993
5994 CHECK_FORK();
5995
5996 if (slot == NULL((void*)0)) {
5997 return CKR_SESSION_HANDLE_INVALID0x000000B3UL;
5998 }
5999 session = sftk_SessionFromHandle(hSession);
6000 if (session == NULL((void*)0)) {
6001 crv = CKR_SESSION_HANDLE_INVALID0x000000B3UL;
6002 goto loser;
6003 }
6004
6005 search = (SFTKSearchResults *)PORT_AllocPORT_Alloc_Util(sizeof(SFTKSearchResults));
6006 if (search == NULL((void*)0)) {
6007 crv = CKR_HOST_MEMORY0x00000002UL;
6008 goto loser;
6009 }
6010 search->handles = (CK_OBJECT_HANDLE *)
6011 PORT_AllocPORT_Alloc_Util(sizeof(CK_OBJECT_HANDLE) * NSC_SEARCH_BLOCK_SIZE5);
6012 if (search->handles == NULL((void*)0)) {
6013 crv = CKR_HOST_MEMORY0x00000002UL;
6014 goto loser;
6015 }
6016 search->index = 0;
6017 search->size = 0;
6018 search->array_size = NSC_SEARCH_BLOCK_SIZE5;
6019
6020 PR_Lock(slot->slotLock);
6021 isLoggedIn = (PRBool)((!slot->needLogin) || slot->isLoggedIn);
6022 PR_Unlock(slot->slotLock);
6023
6024 PRBool validTokenAttribute = PR_FALSE0;
6025 PRBool tokenAttributeValue = PR_FALSE0;
6026 for (CK_ULONG i = 0; i < ulCount; i++) {
6027 CK_ATTRIBUTE_PTR attr = &pTemplate[i];
6028 if (attr->type == CKA_TOKEN0x00000001UL && attr->pValue && attr->ulValueLen == sizeof(CK_BBOOL)) {
6029 if (*(CK_BBOOL *)attr->pValue == CK_TRUE1) {
6030 validTokenAttribute = PR_TRUE1;
6031 tokenAttributeValue = PR_TRUE1;
6032 } else if (*(CK_BBOOL *)attr->pValue == CK_FALSE0) {
6033 validTokenAttribute = PR_TRUE1;
6034 tokenAttributeValue = PR_FALSE0;
6035 }
6036 break;
6037 }
6038 }
6039
6040 // Search over the token object list if the template's CKA_TOKEN attribute is set to
6041 // CK_TRUE or if it is not set.
6042 if (validTokenAttribute == PR_FALSE0 || tokenAttributeValue == PR_TRUE1) {
6043 crv = sftk_searchTokenList(slot, search, pTemplate, ulCount, isLoggedIn);
6044 if (crv != CKR_OK0x00000000UL) {
6045 goto loser;
6046 }
6047 }
6048
6049 // Search over the session object list if the template's CKA_TOKEN attribute is set to
6050 // CK_FALSE or if it is not set.
6051 if (validTokenAttribute == PR_FALSE0 || tokenAttributeValue == PR_FALSE0) {
6052 crv = sftk_searchObjectList(search, slot->sessObjHashTable,
6053 slot->sessObjHashSize, slot->objectLock,
6054 pTemplate, ulCount, isLoggedIn);
6055 if (crv != CKR_OK0x00000000UL) {
6056 goto loser;
6057 }
6058 }
6059
6060 freeSearch = sftk_SwapSearch(session, search);
6061 if (freeSearch != NULL((void*)0)) {
6062 sftk_FreeSearch(freeSearch);
6063 }
6064 sftk_FreeSession(session);
6065 return CKR_OK0x00000000UL;
6066
6067loser:
6068 if (search) {
6069 sftk_FreeSearch(search);
6070 }
6071 if (session) {
6072 sftk_FreeSession(session);
6073 }
6074 return crv;
6075}
6076
6077/* NSC_FindObjects continues a search for token and session objects
6078 * that match a template, obtaining additional object handles. */
6079CK_RV
6080NSC_FindObjects(CK_SESSION_HANDLE hSession,
6081 CK_OBJECT_HANDLE_PTR phObject, CK_ULONG ulMaxObjectCount,
6082 CK_ULONG_PTR pulObjectCount)
6083{
6084 SFTKSession *session;
6085 SFTKSearchResults *search;
6086 int transfer;
6087 int left;
6088
6089 CHECK_FORK();
6090
6091 *pulObjectCount = 0;
6092 session = sftk_SessionFromHandle(hSession);
6093 if (session == NULL((void*)0))
6094 return CKR_SESSION_HANDLE_INVALID0x000000B3UL;
6095 /* Take ownership of the search for the duration of the iteration so
6096 * a concurrent FindObjectsInit/FindObjectsFinal can't free it under
6097 * us. If another thread takes it first, we behave as if the search
6098 * had been exhausted: zero results, no error. */
6099 search = sftk_SwapSearch(session, NULL((void*)0));
6100 if (search == NULL((void*)0)) {
6101 sftk_FreeSession(session);
6102 return CKR_OK0x00000000UL;
6103 }
6104 left = search->size - search->index;
6105 transfer = ((int)ulMaxObjectCount > left) ? left : ulMaxObjectCount;
6106 if (transfer > 0) {
6107 PORT_Memcpymemcpy(phObject, &search->handles[search->index],
6108 transfer * sizeof(CK_OBJECT_HANDLE));
6109 } else {
6110 *phObject = CK_INVALID_HANDLE0;
6111 }
6112
6113 search->index += transfer;
6114 if (search->index == search->size) {
6115 /* fully consumed */
6116 sftk_FreeSearch(search);
6117 } else if (!sftk_RestoreSearch(session, search)) {
6118 /* A concurrent FindObjectsInit installed a newer search while
6119 * we were iterating; ours is now stale, drop it. */
6120 sftk_FreeSearch(search);
6121 }
6122 *pulObjectCount = transfer;
6123 sftk_FreeSession(session);
6124 return CKR_OK0x00000000UL;
6125}
6126
6127/* NSC_FindObjectsFinal finishes a search for token and session objects. */
6128CK_RV
6129NSC_FindObjectsFinal(CK_SESSION_HANDLE hSession)
6130{
6131 SFTKSession *session;
6132 SFTKSearchResults *search;
6133
6134 CHECK_FORK();
6135
6136 session = sftk_SessionFromHandle(hSession);
6137 if (session == NULL((void*)0))
6138 return CKR_SESSION_HANDLE_INVALID0x000000B3UL;
6139 search = sftk_SwapSearch(session, NULL((void*)0));
6140 sftk_FreeSession(session);
6141 if (search != NULL((void*)0)) {
6142 sftk_FreeSearch(search);
6143 }
6144 return CKR_OK0x00000000UL;
6145}
6146
6147CK_RV
6148NSC_WaitForSlotEvent(CK_FLAGS flags, CK_SLOT_ID_PTR pSlot,
6149 CK_VOID_PTR pReserved)
6150{
6151 CHECK_FORK();
6152
6153 return CKR_FUNCTION_NOT_SUPPORTED0x00000054UL;
6154}
6155
6156CK_RV
6157NSC_AsyncComplete(CK_SESSION_HANDLE hSession, CK_UTF8CHAR_PTR pFunctionName,
6158 CK_ASYNC_DATA_PTR pResult)
6159{
6160 CHECK_FORK();
6161
6162 return CKR_FUNCTION_NOT_SUPPORTED0x00000054UL;
6163}
6164
6165CK_RV
6166NSC_AsyncGetID(CK_SESSION_HANDLE hSession, CK_UTF8CHAR_PTR pFunctionName,
6167 CK_ULONG_PTR pulID)
6168{
6169 CHECK_FORK();
6170
6171 return CKR_FUNCTION_NOT_SUPPORTED0x00000054UL;
6172}
6173
6174CK_RV
6175NSC_AsyncJoin(CK_SESSION_HANDLE hSession, CK_UTF8CHAR_PTR pFunctionName,
6176 CK_ULONG ulID, CK_BYTE_PTR pData, CK_ULONG ulData)
6177{
6178 CHECK_FORK();
6179
6180 return CKR_FUNCTION_NOT_SUPPORTED0x00000054UL;
6181}
6182
6183static CK_RV
6184nsc_NSSGetFIPSStatus(CK_SESSION_HANDLE hSession,
6185 CK_OBJECT_HANDLE hObject,
6186 CK_ULONG ulOperationType,
6187 CK_ULONG *pulFIPSStatus)
6188{
6189 CK_ULONG sessionState = CKS_NSS_UNINITIALIZED0xffffffffUL;
6190 CK_ULONG objectState = CKS_NSS_UNINITIALIZED0xffffffffUL;
6191 PRBool needSession = PR_FALSE0;
6192 PRBool needObject = PR_FALSE0;
6193 SFTKSession *session;
6194 SFTKObject *object;
6195
6196 *pulFIPSStatus = CKS_NSS_FIPS_NOT_OK0UL;
6197
6198 /* first determine what we need to look up */
6199 switch (ulOperationType) {
6200 case CKT_NSS_SESSION_CHECK1UL:
6201 case CKT_NSS_SESSION_LAST_CHECK4UL:
6202 needSession = PR_TRUE1;
6203 needObject = PR_FALSE0;
6204 break;
6205 case CKT_NSS_OBJECT_CHECK2UL:
6206 needSession = PR_FALSE0;
6207 needObject = PR_TRUE1;
6208 break;
6209 case CKT_NSS_BOTH_CHECK3UL:
6210 needSession = PR_TRUE1;
6211 needObject = PR_TRUE1;
6212 break;
6213 default:
6214 return CKR_ARGUMENTS_BAD0x00000007UL;
6215 }
6216
6217 /* we always need the session handle, the object handle is only
6218 * meaningful if there is a session */
6219 session = sftk_SessionFromHandle(hSession);
6220 if (!session) {
6221 return CKR_SESSION_HANDLE_INVALID0x000000B3UL;
6222 }
6223 if (needSession) {
6224 if (CKT_NSS_SESSION_LAST_CHECK4UL == ulOperationType) {
6225 sessionState = session->lastOpWasFIPS ? CKS_NSS_FIPS_OK1UL : CKS_NSS_FIPS_NOT_OK0UL;
6226 } else {
6227 if (session->enc_context) {
6228 sessionState = session->enc_context->isFIPS ? CKS_NSS_FIPS_OK1UL : CKS_NSS_FIPS_NOT_OK0UL;
6229 }
6230 if (sessionState != CKS_NSS_FIPS_NOT_OK0UL && session->hash_context) {
6231 sessionState = session->hash_context->isFIPS ? CKS_NSS_FIPS_OK1UL : CKS_NSS_FIPS_NOT_OK0UL;
6232 }
6233 /* sessionState is set to CKS_NSS_UNINITIALIZED if neither
6234 * context exists */
6235 }
6236 }
6237
6238 if (needObject) {
6239 object = sftk_ObjectFromHandle(hObject, session);
6240 if (!object) {
6241 sftk_FreeSession(session);
6242 return CKR_OBJECT_HANDLE_INVALID0x00000082UL;
6243 }
6244 objectState = sftk_hasFIPS(object) ? CKS_NSS_FIPS_OK1UL : CKS_NSS_FIPS_NOT_OK0UL;
6245 sftk_FreeObject(object);
6246 }
6247
6248 sftk_FreeSession(session);
6249
6250 /* If we didn't fetch the state, then it is uninitialized.
6251 * The session state can also be uninitialized if there are no active
6252 * crypto operations on the session. Turns out the rules for combining
6253 * the states are the same whether or not the state was uninitialzed
6254 * because we didn't fetch it or because there wasn't a state to fetch.
6255 */
6256
6257 /* if the object State is uninitialized, return the state of the session. */
6258 if (objectState == CKS_NSS_UNINITIALIZED0xffffffffUL) {
6259 /* if they are both uninitalized, return CKS_FIPS_NOT_OK */
6260 if (sessionState == CKS_NSS_UNINITIALIZED0xffffffffUL) {
6261 /* *pulFIPSStatus already set to CKS_FIPS_NOT_OK */
6262 return CKR_OK0x00000000UL;
6263 }
6264 *pulFIPSStatus = sessionState;
6265 return CKR_OK0x00000000UL;
6266 }
6267 /* objectState is initialized, if sessionState is uninitialized, we can
6268 * just return objectState */
6269 if (sessionState == CKS_NSS_UNINITIALIZED0xffffffffUL) {
6270 *pulFIPSStatus = objectState;
6271 return CKR_OK0x00000000UL;
6272 }
6273
6274 /* they are are not equal, one must be CKS_FIPS_NOT_OK, so we return that
6275 * value CKS_FIPS_NOT_OK */
6276 if (objectState != sessionState) {
6277 /* *pulFIPSStatus already set to CKS_FIPS_NOT_OK */
6278 return CKR_OK0x00000000UL;
6279 }
6280
6281 /* objectState and sessionState or the same, so we can return either */
6282 *pulFIPSStatus = sessionState;
6283 return CKR_OK0x00000000UL;
6284}
6285
6286CK_RV
6287NSC_GetSessionValidationFlags(CK_SESSION_HANDLE hSession,
6288 CK_SESSION_VALIDATION_FLAGS_TYPE type,
6289 CK_FLAGS_PTR pFlags)
6290{
6291 CK_RV crv;
6292 CK_ULONG status = CKS_NSS_UNINITIALIZED0xffffffffUL;
6293 CK_ULONG status1 = CKS_NSS_UNINITIALIZED0xffffffffUL;
6294
6295 *pFlags = 0;
6296
6297 crv = nsc_NSSGetFIPSStatus(hSession, CK_INVALID_HANDLE0,
6298 CKT_NSS_SESSION_LAST_CHECK4UL, &status);
6299 if (crv != CKR_OK0x00000000UL) {
6300 return crv;
6301 }
6302 crv = nsc_NSSGetFIPSStatus(hSession, CK_INVALID_HANDLE0,
6303 CKT_NSS_SESSION_CHECK1UL, &status1);
6304 if (crv != CKR_OK0x00000000UL) {
6305 return crv;
6306 }
6307 /* PCKS #11 only defines the last operation. For us this includes
6308 * things in the CKT_NSS_SESSION_CHECK */
6309 if ((status == CKS_NSS_FIPS_OK1UL) || (status1 == CKS_NSS_FIPS_OK1UL)) {
6310 *pFlags = SFTK_VALIDATION_FIPS_FLAG0x00000001L;
6311 }
6312 return CKR_OK0x00000000UL;
6313}