Bug Summary

File:root/firefox-clang/security/nss/lib/softoken/pkcs11.c
Warning:line 3134, column 39
Subtraction of a probably non-null pointer and a null pointer may result in undefined behavior

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