/src/nss/lib/pk11wrap/pk11cxt.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* This Source Code Form is subject to the terms of the Mozilla Public |
2 | | * License, v. 2.0. If a copy of the MPL was not distributed with this |
3 | | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
4 | | /* |
5 | | * This file PK11Contexts which are used in multipart hashing, |
6 | | * encryption/decryption, and signing/verication operations. |
7 | | */ |
8 | | |
9 | | #include "seccomon.h" |
10 | | #include "secmod.h" |
11 | | #include "nssilock.h" |
12 | | #include "secmodi.h" |
13 | | #include "secmodti.h" |
14 | | #include "pkcs11.h" |
15 | | #include "pk11func.h" |
16 | | #include "secitem.h" |
17 | | #include "secoid.h" |
18 | | #include "sechash.h" |
19 | | #include "secerr.h" |
20 | | #include "blapit.h" |
21 | | #include "secport.h" |
22 | | |
23 | | static const SECItem pk11_null_params = { 0 }; |
24 | | |
25 | | /********************************************************************** |
26 | | * |
27 | | * Now Deal with Crypto Contexts |
28 | | * |
29 | | **********************************************************************/ |
30 | | |
31 | | /* |
32 | | * the monitors... |
33 | | */ |
34 | | void |
35 | | PK11_EnterContextMonitor(PK11Context *cx) |
36 | 0 | { |
37 | | /* if we own the session and our slot is ThreadSafe, only monitor |
38 | | * the Context */ |
39 | 0 | if ((cx->ownSession) && (cx->slot->isThreadSafe)) { |
40 | | /* Should this use monitors instead? */ |
41 | 0 | PZ_Lock(cx->sessionLock); |
42 | 0 | } else { |
43 | 0 | PK11_EnterSlotMonitor(cx->slot); |
44 | 0 | } |
45 | 0 | } |
46 | | |
47 | | void |
48 | | PK11_ExitContextMonitor(PK11Context *cx) |
49 | 0 | { |
50 | | /* if we own the session and our slot is ThreadSafe, only monitor |
51 | | * the Context */ |
52 | 0 | if ((cx->ownSession) && (cx->slot->isThreadSafe)) { |
53 | | /* Should this use monitors instead? */ |
54 | 0 | PZ_Unlock(cx->sessionLock); |
55 | 0 | } else { |
56 | 0 | PK11_ExitSlotMonitor(cx->slot); |
57 | 0 | } |
58 | 0 | } |
59 | | |
60 | | /* |
61 | | * Free up a Cipher Context |
62 | | */ |
63 | | void |
64 | | PK11_DestroyContext(PK11Context *context, PRBool freeit) |
65 | 0 | { |
66 | 0 | pk11_CloseSession(context->slot, context->session, context->ownSession); |
67 | | /* initialize the critical fields of the context */ |
68 | 0 | if (context->savedData != NULL) |
69 | 0 | PORT_Free(context->savedData); |
70 | 0 | if (context->key) |
71 | 0 | PK11_FreeSymKey(context->key); |
72 | 0 | if (context->param && context->param != &pk11_null_params) |
73 | 0 | SECITEM_FreeItem(context->param, PR_TRUE); |
74 | 0 | if (context->sessionLock) |
75 | 0 | PZ_DestroyLock(context->sessionLock); |
76 | 0 | PK11_FreeSlot(context->slot); |
77 | 0 | if (freeit) |
78 | 0 | PORT_Free(context); |
79 | 0 | } |
80 | | |
81 | | /* |
82 | | * save the current context. Allocate Space if necessary. |
83 | | */ |
84 | | static unsigned char * |
85 | | pk11_saveContextHelper(PK11Context *context, unsigned char *buffer, |
86 | | unsigned long *savedLength) |
87 | 0 | { |
88 | 0 | CK_RV crv; |
89 | | |
90 | | /* If buffer is NULL, this will get the length */ |
91 | 0 | crv = PK11_GETTAB(context->slot)->C_GetOperationState(context->session, (CK_BYTE_PTR)buffer, savedLength); |
92 | 0 | if (!buffer || (crv == CKR_BUFFER_TOO_SMALL)) { |
93 | | /* the given buffer wasn't big enough (or was NULL), but we |
94 | | * have the length, so try again with a new buffer and the |
95 | | * correct length |
96 | | */ |
97 | 0 | unsigned long bufLen = *savedLength; |
98 | 0 | buffer = PORT_Alloc(bufLen); |
99 | 0 | if (buffer == NULL) { |
100 | 0 | return (unsigned char *)NULL; |
101 | 0 | } |
102 | 0 | crv = PK11_GETTAB(context->slot)->C_GetOperationState(context->session, (CK_BYTE_PTR)buffer, savedLength); |
103 | 0 | if (crv != CKR_OK) { |
104 | 0 | PORT_ZFree(buffer, bufLen); |
105 | 0 | } |
106 | 0 | } |
107 | 0 | if (crv != CKR_OK) { |
108 | 0 | PORT_SetError(PK11_MapError(crv)); |
109 | 0 | return (unsigned char *)NULL; |
110 | 0 | } |
111 | 0 | return buffer; |
112 | 0 | } |
113 | | |
114 | | void * |
115 | | pk11_saveContext(PK11Context *context, void *space, unsigned long *savedLength) |
116 | 0 | { |
117 | 0 | return pk11_saveContextHelper(context, |
118 | 0 | (unsigned char *)space, savedLength); |
119 | 0 | } |
120 | | |
121 | | /* |
122 | | * restore the current context |
123 | | */ |
124 | | SECStatus |
125 | | pk11_restoreContext(PK11Context *context, void *space, unsigned long savedLength) |
126 | 0 | { |
127 | 0 | CK_RV crv; |
128 | 0 | CK_OBJECT_HANDLE objectID = context->objectID; |
129 | |
|
130 | 0 | PORT_Assert(space != NULL); |
131 | 0 | if (space == NULL) { |
132 | 0 | PORT_SetError(SEC_ERROR_LIBRARY_FAILURE); |
133 | 0 | return SECFailure; |
134 | 0 | } |
135 | 0 | crv = PK11_GETTAB(context->slot)->C_SetOperationState(context->session, (CK_BYTE_PTR)space, savedLength, objectID, 0); |
136 | 0 | if (crv != CKR_OK) { |
137 | 0 | PORT_SetError(PK11_MapError(crv)); |
138 | 0 | return SECFailure; |
139 | 0 | } |
140 | 0 | return SECSuccess; |
141 | 0 | } |
142 | | |
143 | | SECStatus pk11_Finalize(PK11Context *context); |
144 | | |
145 | | /* |
146 | | * Initialize a Message function. Particular function is passed in as a |
147 | | * function pointer. Since all C_Message*Init funcitons have the same |
148 | | * prototype, we just pick one of the the prototypes to declare our init |
149 | | * function. |
150 | | */ |
151 | | static CK_RV |
152 | | pk11_contextInitMessage(PK11Context *context, CK_MECHANISM_PTR mech, |
153 | | CK_C_MessageEncryptInit initFunc, |
154 | | CK_FLAGS flags, CK_RV scrv) |
155 | 0 | { |
156 | 0 | PK11SlotInfo *slot = context->slot; |
157 | 0 | CK_VERSION version = slot->module->cryptokiVersion; |
158 | 0 | CK_RV crv = CKR_OK; |
159 | |
|
160 | 0 | context->ivCounter = 0; |
161 | 0 | context->ivMaxCount = 0; |
162 | 0 | context->ivFixedBits = 0; |
163 | 0 | context->ivLen = 0; |
164 | 0 | context->ivGen = CKG_NO_GENERATE; |
165 | 0 | context->simulate_mechanism = (mech)->mechanism; |
166 | 0 | context->simulate_message = PR_FALSE; |
167 | | /* check that we can do the Message interface. We need to check |
168 | | * for either 1) are we using a PKCS #11 v3 interface and 2) is the |
169 | | * Message flag set on the mechanism. If either is false we simulate |
170 | | * the message interface for the Encrypt and Decrypt cases using the |
171 | | * PKCS #11 V2 interface. |
172 | | * Sign and verify do not have V2 interfaces, so we go ahead and fail |
173 | | * if those cases */ |
174 | 0 | if ((version.major >= 3) && |
175 | 0 | PK11_DoesMechanismFlag(slot, (mech)->mechanism, flags)) { |
176 | 0 | PK11_EnterContextMonitor(context); |
177 | 0 | crv = (*initFunc)((context)->session, (mech), (context)->objectID); |
178 | 0 | PK11_ExitContextMonitor(context); |
179 | 0 | if ((crv == CKR_FUNCTION_NOT_SUPPORTED) || |
180 | 0 | (crv == CKR_MECHANISM_INVALID)) { |
181 | | /* we have a 3.0 interface, and the flag was set (or ignored) |
182 | | * but the implementation was not there, use the V2 interface */ |
183 | 0 | crv = (scrv); |
184 | 0 | context->simulate_message = PR_TRUE; |
185 | 0 | } |
186 | 0 | } else { |
187 | 0 | crv = (scrv); |
188 | 0 | context->simulate_message = PR_TRUE; |
189 | 0 | } |
190 | 0 | return crv; |
191 | 0 | } |
192 | | |
193 | | /* |
194 | | * Context initialization. Used by all flavors of CreateContext |
195 | | */ |
196 | | static SECStatus |
197 | | pk11_context_init(PK11Context *context, CK_MECHANISM *mech_info) |
198 | 0 | { |
199 | 0 | CK_RV crv; |
200 | 0 | SECStatus rv = SECSuccess; |
201 | |
|
202 | 0 | context->simulate_message = PR_FALSE; |
203 | 0 | switch (context->operation) { |
204 | 0 | case CKA_ENCRYPT: |
205 | 0 | PK11_EnterContextMonitor(context); |
206 | 0 | crv = PK11_GETTAB(context->slot)->C_EncryptInit(context->session, mech_info, context->objectID); |
207 | 0 | PK11_ExitContextMonitor(context); |
208 | 0 | break; |
209 | 0 | case CKA_DECRYPT: |
210 | 0 | PK11_EnterContextMonitor(context); |
211 | 0 | if (context->fortezzaHack) { |
212 | 0 | CK_ULONG count = 0; |
213 | | /* generate the IV for fortezza */ |
214 | 0 | crv = PK11_GETTAB(context->slot)->C_EncryptInit(context->session, mech_info, context->objectID); |
215 | 0 | if (crv != CKR_OK) { |
216 | 0 | PK11_ExitContextMonitor(context); |
217 | 0 | break; |
218 | 0 | } |
219 | 0 | PK11_GETTAB(context->slot) |
220 | 0 | ->C_EncryptFinal(context->session, |
221 | 0 | NULL, &count); |
222 | 0 | } |
223 | 0 | crv = PK11_GETTAB(context->slot)->C_DecryptInit(context->session, mech_info, context->objectID); |
224 | 0 | PK11_ExitContextMonitor(context); |
225 | 0 | break; |
226 | 0 | case CKA_SIGN: |
227 | 0 | PK11_EnterContextMonitor(context); |
228 | 0 | crv = PK11_GETTAB(context->slot)->C_SignInit(context->session, mech_info, context->objectID); |
229 | 0 | PK11_ExitContextMonitor(context); |
230 | 0 | break; |
231 | 0 | case CKA_VERIFY: |
232 | | /* NOTE: we previously has this set to C_SignInit for Macing. |
233 | | * It turns out now one could possibly use it that way, though, |
234 | | * because PK11_HashOp() always called C_VerifyUpdate on CKA_VERIFY, |
235 | | * which would have failed. So everyone just calls us with CKA_SIGN |
236 | | * when Macing even when they are verifying, no need to 'do it |
237 | | * for them'. It needs to be VerifyInit now so that we can do |
238 | | * PKCS #11 hash/Verify combo operations. */ |
239 | 0 | PK11_EnterContextMonitor(context); |
240 | 0 | crv = PK11_GETTAB(context->slot)->C_VerifyInit(context->session, mech_info, context->objectID); |
241 | 0 | PK11_ExitContextMonitor(context); |
242 | 0 | break; |
243 | 0 | case CKA_DIGEST: |
244 | 0 | PK11_EnterContextMonitor(context); |
245 | 0 | crv = PK11_GETTAB(context->slot)->C_DigestInit(context->session, mech_info); |
246 | 0 | PK11_ExitContextMonitor(context); |
247 | 0 | break; |
248 | | |
249 | 0 | case CKA_NSS_MESSAGE | CKA_ENCRYPT: |
250 | 0 | crv = pk11_contextInitMessage(context, mech_info, |
251 | 0 | PK11_GETTAB(context->slot)->C_MessageEncryptInit, |
252 | 0 | CKF_MESSAGE_ENCRYPT, CKR_OK); |
253 | 0 | break; |
254 | 0 | case CKA_NSS_MESSAGE | CKA_DECRYPT: |
255 | 0 | crv = pk11_contextInitMessage(context, mech_info, |
256 | 0 | PK11_GETTAB(context->slot)->C_MessageDecryptInit, |
257 | 0 | CKF_MESSAGE_DECRYPT, CKR_OK); |
258 | 0 | break; |
259 | 0 | case CKA_NSS_MESSAGE | CKA_SIGN: |
260 | 0 | crv = pk11_contextInitMessage(context, mech_info, |
261 | 0 | PK11_GETTAB(context->slot)->C_MessageSignInit, |
262 | 0 | CKF_MESSAGE_SIGN, CKR_FUNCTION_NOT_SUPPORTED); |
263 | 0 | break; |
264 | 0 | case CKA_NSS_MESSAGE | CKA_VERIFY: |
265 | 0 | crv = pk11_contextInitMessage(context, mech_info, |
266 | 0 | PK11_GETTAB(context->slot)->C_MessageVerifyInit, |
267 | 0 | CKF_MESSAGE_VERIFY, CKR_FUNCTION_NOT_SUPPORTED); |
268 | 0 | break; |
269 | 0 | default: |
270 | 0 | crv = CKR_OPERATION_NOT_INITIALIZED; |
271 | 0 | break; |
272 | 0 | } |
273 | | |
274 | 0 | if (crv != CKR_OK) { |
275 | 0 | PORT_SetError(PK11_MapError(crv)); |
276 | 0 | return SECFailure; |
277 | 0 | } |
278 | | |
279 | | /* handle the case where the token is using the old NSS mechanism */ |
280 | 0 | if (context->simulate_message && |
281 | 0 | !PK11_DoesMechanism(context->slot, context->simulate_mechanism)) { |
282 | 0 | if ((context->simulate_mechanism == CKM_CHACHA20_POLY1305) && |
283 | 0 | PK11_DoesMechanism(context->slot, CKM_NSS_CHACHA20_POLY1305)) { |
284 | 0 | context->simulate_mechanism = CKM_NSS_CHACHA20_POLY1305; |
285 | 0 | } else { |
286 | 0 | PORT_SetError(PK11_MapError(CKR_MECHANISM_INVALID)); |
287 | 0 | return SECFailure; |
288 | 0 | } |
289 | 0 | } |
290 | | |
291 | | /* |
292 | | * handle session starvation case.. use our last session to multiplex |
293 | | */ |
294 | 0 | if (!context->ownSession) { |
295 | 0 | PK11_EnterContextMonitor(context); |
296 | 0 | context->savedData = pk11_saveContext(context, context->savedData, |
297 | 0 | &context->savedLength); |
298 | 0 | if (context->savedData == NULL) |
299 | 0 | rv = SECFailure; |
300 | | /* clear out out session for others to use */ |
301 | 0 | pk11_Finalize(context); |
302 | 0 | PK11_ExitContextMonitor(context); |
303 | 0 | } |
304 | 0 | return rv; |
305 | 0 | } |
306 | | |
307 | | /* |
308 | | * Testing interfaces, not for general use. This function forces |
309 | | * an AEAD context into simulation mode even though the target token |
310 | | * can already do PKCS #11 v3.0 Message (i.e. softoken). |
311 | | */ |
312 | | SECStatus |
313 | | _PK11_ContextSetAEADSimulation(PK11Context *context) |
314 | 0 | { |
315 | 0 | CK_RV crv; |
316 | | /* only message encrypt and message decrypt contexts can be simulated */ |
317 | 0 | if ((context->operation != (CKA_NSS_MESSAGE | CKA_ENCRYPT)) && |
318 | 0 | (context->operation != (CKA_NSS_MESSAGE | CKA_DECRYPT))) { |
319 | 0 | PORT_SetError(SEC_ERROR_INVALID_ARGS); |
320 | 0 | return SECFailure; |
321 | 0 | } |
322 | | /* if we are already simulating, return */ |
323 | 0 | if (context->simulate_message) { |
324 | 0 | return SECSuccess; |
325 | 0 | } |
326 | | /* we need to shutdown the existing AEAD operation */ |
327 | 0 | switch (context->operation) { |
328 | 0 | case CKA_NSS_MESSAGE | CKA_ENCRYPT: |
329 | 0 | crv = PK11_GETTAB(context->slot)->C_MessageEncryptFinal(context->session); |
330 | 0 | break; |
331 | 0 | case CKA_NSS_MESSAGE | CKA_DECRYPT: |
332 | 0 | crv = PK11_GETTAB(context->slot)->C_MessageDecryptFinal(context->session); |
333 | 0 | break; |
334 | 0 | default: |
335 | 0 | PORT_SetError(SEC_ERROR_NOT_INITIALIZED); |
336 | 0 | return SECFailure; |
337 | 0 | } |
338 | 0 | if (crv != CKR_OK) { |
339 | 0 | PORT_SetError(PK11_MapError(crv)); |
340 | 0 | return SECFailure; |
341 | 0 | } |
342 | 0 | context->simulate_message = PR_TRUE; |
343 | 0 | return SECSuccess; |
344 | 0 | } |
345 | | |
346 | | PRBool |
347 | | _PK11_ContextGetAEADSimulation(PK11Context *context) |
348 | 0 | { |
349 | 0 | return context->simulate_message; |
350 | 0 | } |
351 | | |
352 | | /* |
353 | | * Common Helper Function do come up with a new context. |
354 | | */ |
355 | | static PK11Context * |
356 | | pk11_CreateNewContextInSlot(CK_MECHANISM_TYPE type, |
357 | | PK11SlotInfo *slot, CK_ATTRIBUTE_TYPE operation, |
358 | | PK11SymKey *symKey, CK_OBJECT_HANDLE objectID, |
359 | | const SECItem *param, void *pwArg) |
360 | 0 | { |
361 | 0 | CK_MECHANISM mech_info; |
362 | 0 | PK11Context *context; |
363 | 0 | SECStatus rv; |
364 | |
|
365 | 0 | PORT_Assert(slot != NULL); |
366 | 0 | if (!slot || ((objectID == CK_INVALID_HANDLE) && ((operation != CKA_DIGEST) || |
367 | 0 | (type == CKM_SKIPJACK_CBC64)))) { |
368 | 0 | PORT_SetError(SEC_ERROR_INVALID_ARGS); |
369 | 0 | return NULL; |
370 | 0 | } |
371 | 0 | context = (PK11Context *)PORT_Alloc(sizeof(PK11Context)); |
372 | 0 | if (context == NULL) { |
373 | 0 | return NULL; |
374 | 0 | } |
375 | | |
376 | | /* now deal with the fortezza hack... the fortezza hack is an attempt |
377 | | * to get around the issue of the card not allowing you to do a FORTEZZA |
378 | | * LoadIV/Encrypt, which was added because such a combination could be |
379 | | * use to circumvent the key escrow system. Unfortunately SSL needs to |
380 | | * do this kind of operation, so in SSL we do a loadIV (to verify it), |
381 | | * Then GenerateIV, and through away the first 8 bytes on either side |
382 | | * of the connection.*/ |
383 | 0 | context->fortezzaHack = PR_FALSE; |
384 | 0 | if (type == CKM_SKIPJACK_CBC64) { |
385 | 0 | if (symKey && (symKey->origin == PK11_OriginFortezzaHack)) { |
386 | 0 | context->fortezzaHack = PR_TRUE; |
387 | 0 | } |
388 | 0 | } |
389 | | |
390 | | /* initialize the critical fields of the context */ |
391 | 0 | context->operation = operation; |
392 | | /* If we were given a symKey, keep our own reference to it so |
393 | | * that the key doesn't disappear in the middle of the operation |
394 | | * if the caller frees it. Public and Private keys are not reference |
395 | | * counted, so the caller just has to keep his copies around until |
396 | | * the operation completes */ |
397 | 0 | context->key = symKey ? PK11_ReferenceSymKey(symKey) : NULL; |
398 | 0 | context->objectID = objectID; |
399 | 0 | context->slot = PK11_ReferenceSlot(slot); |
400 | 0 | context->session = pk11_GetNewSession(slot, &context->ownSession); |
401 | 0 | context->pwArg = pwArg; |
402 | | /* get our session */ |
403 | 0 | context->savedData = NULL; |
404 | | |
405 | | /* save the parameters so that some digesting stuff can do multiple |
406 | | * begins on a single context */ |
407 | 0 | context->type = type; |
408 | 0 | if (param) { |
409 | 0 | if (param->len > 0) { |
410 | 0 | context->param = SECITEM_DupItem(param); |
411 | 0 | } else { |
412 | 0 | context->param = (SECItem *)&pk11_null_params; |
413 | 0 | } |
414 | 0 | } else { |
415 | 0 | PORT_SetError(SEC_ERROR_INVALID_ARGS); |
416 | 0 | context->param = NULL; |
417 | 0 | } |
418 | 0 | context->init = PR_FALSE; |
419 | 0 | context->sessionLock = PZ_NewLock(nssILockPK11cxt); |
420 | 0 | if ((context->param == NULL) || (context->sessionLock == NULL)) { |
421 | 0 | PK11_DestroyContext(context, PR_TRUE); |
422 | 0 | return NULL; |
423 | 0 | } |
424 | | |
425 | 0 | mech_info.mechanism = type; |
426 | 0 | mech_info.pParameter = param->data; |
427 | 0 | mech_info.ulParameterLen = param->len; |
428 | 0 | rv = pk11_context_init(context, &mech_info); |
429 | |
|
430 | 0 | if (rv != SECSuccess) { |
431 | 0 | PK11_DestroyContext(context, PR_TRUE); |
432 | 0 | return NULL; |
433 | 0 | } |
434 | 0 | context->init = PR_TRUE; |
435 | 0 | return context; |
436 | 0 | } |
437 | | |
438 | | /* |
439 | | * put together the various PK11_Create_Context calls used by different |
440 | | * parts of libsec. |
441 | | */ |
442 | | PK11Context * |
443 | | __PK11_CreateContextByRawKey(PK11SlotInfo *slot, CK_MECHANISM_TYPE type, |
444 | | PK11Origin origin, CK_ATTRIBUTE_TYPE operation, SECItem *key, |
445 | | SECItem *param, void *wincx) |
446 | 0 | { |
447 | 0 | PK11SymKey *symKey = NULL; |
448 | 0 | PK11Context *context = NULL; |
449 | | |
450 | | /* first get a slot */ |
451 | 0 | if (slot == NULL) { |
452 | 0 | slot = PK11_GetBestSlot(type, wincx); |
453 | 0 | if (slot == NULL) { |
454 | 0 | PORT_SetError(SEC_ERROR_NO_MODULE); |
455 | 0 | goto loser; |
456 | 0 | } |
457 | 0 | } else { |
458 | 0 | PK11_ReferenceSlot(slot); |
459 | 0 | } |
460 | | |
461 | | /* now import the key */ |
462 | 0 | symKey = PK11_ImportSymKey(slot, type, origin, operation, key, wincx); |
463 | 0 | if (symKey == NULL) |
464 | 0 | goto loser; |
465 | | |
466 | 0 | context = PK11_CreateContextBySymKey(type, operation, symKey, param); |
467 | |
|
468 | 0 | loser: |
469 | 0 | if (symKey) { |
470 | 0 | PK11_FreeSymKey(symKey); |
471 | 0 | } |
472 | 0 | if (slot) { |
473 | 0 | PK11_FreeSlot(slot); |
474 | 0 | } |
475 | |
|
476 | 0 | return context; |
477 | 0 | } |
478 | | |
479 | | PK11Context * |
480 | | PK11_CreateContextByRawKey(PK11SlotInfo *slot, CK_MECHANISM_TYPE type, |
481 | | PK11Origin origin, CK_ATTRIBUTE_TYPE operation, SECItem *key, |
482 | | SECItem *param, void *wincx) |
483 | 0 | { |
484 | 0 | return __PK11_CreateContextByRawKey(slot, type, origin, operation, |
485 | 0 | key, param, wincx); |
486 | 0 | } |
487 | | |
488 | | /* |
489 | | * Create a context from a key. We really should make sure we aren't using |
490 | | * the same key in multiple sessions! |
491 | | */ |
492 | | PK11Context * |
493 | | PK11_CreateContextBySymKey(CK_MECHANISM_TYPE type, CK_ATTRIBUTE_TYPE operation, |
494 | | PK11SymKey *symKey, const SECItem *param) |
495 | 0 | { |
496 | 0 | PK11SymKey *newKey; |
497 | 0 | PK11Context *context; |
498 | | |
499 | | /* if this slot doesn't support the mechanism, go to a slot that does */ |
500 | 0 | newKey = pk11_ForceSlot(symKey, type, operation); |
501 | 0 | if (newKey == NULL) { |
502 | 0 | PK11_ReferenceSymKey(symKey); |
503 | 0 | } else { |
504 | 0 | symKey = newKey; |
505 | 0 | } |
506 | | |
507 | | /* Context keeps its reference to the symKey, so it's safe to |
508 | | * free our reference we we are through, even though we may have |
509 | | * created the key using pk11_ForceSlot. */ |
510 | 0 | context = pk11_CreateNewContextInSlot(type, symKey->slot, operation, symKey, |
511 | 0 | symKey->objectID, param, symKey->cx); |
512 | 0 | PK11_FreeSymKey(symKey); |
513 | 0 | return context; |
514 | 0 | } |
515 | | |
516 | | /* To support multipart public key operations (like hash/verify operations), |
517 | | * we need to create contexts with public keys. */ |
518 | | PK11Context * |
519 | | PK11_CreateContextByPubKey(CK_MECHANISM_TYPE type, CK_ATTRIBUTE_TYPE operation, |
520 | | SECKEYPublicKey *pubKey, const SECItem *param, |
521 | | void *pwArg) |
522 | 0 | { |
523 | 0 | PK11SlotInfo *slot = pubKey->pkcs11Slot; |
524 | 0 | SECItem nullparam = { 0, 0, 0 }; |
525 | | |
526 | | /* if this slot doesn't support the mechanism, go to a slot that does */ |
527 | | /* public keys have all their data in the public key data structure, |
528 | | * so there's no need to export the old key, just import this one. The |
529 | | * import manages consistancy of the public key data structure */ |
530 | 0 | if (slot == NULL || !PK11_DoesMechanism(slot, type)) { |
531 | 0 | CK_OBJECT_HANDLE objectID; |
532 | 0 | slot = PK11_GetBestSlot(type, NULL); |
533 | 0 | if (slot == NULL) { |
534 | 0 | return NULL; |
535 | 0 | } |
536 | 0 | objectID = PK11_ImportPublicKey(slot, pubKey, PR_FALSE); |
537 | 0 | PK11_FreeSlot(slot); |
538 | 0 | if (objectID == CK_INVALID_HANDLE) { |
539 | 0 | return NULL; |
540 | 0 | } |
541 | 0 | } |
542 | | |
543 | | /* unlike symkeys, we accept a NULL parameter. map a null parameter |
544 | | * to the empty parameter. This matches the semantics of |
545 | | * PK11_VerifyWithMechanism */ |
546 | 0 | return pk11_CreateNewContextInSlot(type, pubKey->pkcs11Slot, operation, |
547 | 0 | NULL, pubKey->pkcs11ID, |
548 | 0 | param ? param : &nullparam, pwArg); |
549 | 0 | } |
550 | | |
551 | | /* To support multipart private key operations (like hash/sign operations), |
552 | | * we need to create contexts with private keys. */ |
553 | | PK11Context * |
554 | | PK11_CreateContextByPrivKey(CK_MECHANISM_TYPE type, CK_ATTRIBUTE_TYPE operation, |
555 | | SECKEYPrivateKey *privKey, const SECItem *param) |
556 | 0 | { |
557 | 0 | SECItem nullparam = { 0, 0, 0 }; |
558 | | /* Private keys are generally not movable. If the token the |
559 | | * private key lives on can't do the operation, generally we are |
560 | | * stuck anyway. So no need to try to manipulate the key into |
561 | | * another token */ |
562 | | |
563 | | /* if this slot doesn't support the mechanism, go to a slot that does */ |
564 | | /* unlike symkeys, we accept a NULL parameter. map a null parameter |
565 | | * to the empty parameter. This matches the semantics of |
566 | | * PK11_SignWithMechanism */ |
567 | 0 | return pk11_CreateNewContextInSlot(type, privKey->pkcs11Slot, operation, |
568 | 0 | NULL, privKey->pkcs11ID, |
569 | 0 | param ? param : &nullparam, |
570 | 0 | privKey->wincx); |
571 | 0 | } |
572 | | |
573 | | /* |
574 | | * Digest contexts don't need keys, but the do need to find a slot. |
575 | | * Macing should use PK11_CreateContextBySymKey. |
576 | | */ |
577 | | PK11Context * |
578 | | PK11_CreateDigestContext(SECOidTag hashAlg) |
579 | 0 | { |
580 | | /* digesting has to work without authentication to the slot */ |
581 | 0 | CK_MECHANISM_TYPE type; |
582 | 0 | PK11SlotInfo *slot; |
583 | 0 | PK11Context *context; |
584 | 0 | SECItem param; |
585 | |
|
586 | 0 | type = PK11_AlgtagToMechanism(hashAlg); |
587 | 0 | slot = PK11_GetBestSlot(type, NULL); |
588 | 0 | if (slot == NULL) { |
589 | 0 | PORT_SetError(SEC_ERROR_NO_MODULE); |
590 | 0 | return NULL; |
591 | 0 | } |
592 | | |
593 | | /* maybe should really be PK11_GenerateNewParam?? */ |
594 | 0 | param.data = NULL; |
595 | 0 | param.len = 0; |
596 | 0 | param.type = 0; |
597 | |
|
598 | 0 | context = pk11_CreateNewContextInSlot(type, slot, CKA_DIGEST, NULL, |
599 | 0 | CK_INVALID_HANDLE, ¶m, NULL); |
600 | 0 | PK11_FreeSlot(slot); |
601 | 0 | return context; |
602 | 0 | } |
603 | | |
604 | | /* |
605 | | * create a new context which is the clone of the state of old context. |
606 | | */ |
607 | | PK11Context * |
608 | | PK11_CloneContext(PK11Context *old) |
609 | 0 | { |
610 | 0 | PK11Context *newcx; |
611 | 0 | PRBool needFree = PR_FALSE; |
612 | 0 | SECStatus rv = SECSuccess; |
613 | 0 | void *data; |
614 | 0 | unsigned long len; |
615 | |
|
616 | 0 | newcx = pk11_CreateNewContextInSlot(old->type, old->slot, old->operation, |
617 | 0 | old->key, old->objectID, old->param, |
618 | 0 | old->pwArg); |
619 | 0 | if (newcx == NULL) |
620 | 0 | return NULL; |
621 | | |
622 | | /* now clone the save state. First we need to find the save state |
623 | | * of the old session. If the old context owns it's session, |
624 | | * the state needs to be saved, otherwise the state is in saveData. */ |
625 | 0 | if (old->ownSession) { |
626 | 0 | PK11_EnterContextMonitor(old); |
627 | 0 | data = pk11_saveContext(old, NULL, &len); |
628 | 0 | PK11_ExitContextMonitor(old); |
629 | 0 | needFree = PR_TRUE; |
630 | 0 | } else { |
631 | 0 | data = old->savedData; |
632 | 0 | len = old->savedLength; |
633 | 0 | } |
634 | |
|
635 | 0 | if (data == NULL) { |
636 | 0 | PK11_DestroyContext(newcx, PR_TRUE); |
637 | 0 | return NULL; |
638 | 0 | } |
639 | | |
640 | | /* now copy that state into our new context. Again we have different |
641 | | * work if the new context owns it's own session. If it does, we |
642 | | * restore the state gathered above. If it doesn't, we copy the |
643 | | * saveData pointer... */ |
644 | 0 | if (newcx->ownSession) { |
645 | 0 | PK11_EnterContextMonitor(newcx); |
646 | 0 | rv = pk11_restoreContext(newcx, data, len); |
647 | 0 | PK11_ExitContextMonitor(newcx); |
648 | 0 | } else { |
649 | 0 | PORT_Assert(newcx->savedData != NULL); |
650 | 0 | if ((newcx->savedData == NULL) || (newcx->savedLength < len)) { |
651 | 0 | PORT_SetError(SEC_ERROR_LIBRARY_FAILURE); |
652 | 0 | rv = SECFailure; |
653 | 0 | } else { |
654 | 0 | PORT_Memcpy(newcx->savedData, data, len); |
655 | 0 | newcx->savedLength = len; |
656 | 0 | } |
657 | 0 | } |
658 | |
|
659 | 0 | if (needFree) |
660 | 0 | PORT_Free(data); |
661 | |
|
662 | 0 | if (rv != SECSuccess) { |
663 | 0 | PK11_DestroyContext(newcx, PR_TRUE); |
664 | 0 | return NULL; |
665 | 0 | } |
666 | 0 | return newcx; |
667 | 0 | } |
668 | | |
669 | | /* |
670 | | * save the current context state into a variable. Required to make FORTEZZA |
671 | | * work. |
672 | | */ |
673 | | SECStatus |
674 | | PK11_SaveContext(PK11Context *cx, unsigned char *save, int *len, int saveLength) |
675 | 0 | { |
676 | 0 | unsigned char *data = NULL; |
677 | 0 | CK_ULONG length = saveLength; |
678 | |
|
679 | 0 | if (cx->ownSession) { |
680 | 0 | PK11_EnterContextMonitor(cx); |
681 | 0 | data = pk11_saveContextHelper(cx, save, &length); |
682 | 0 | PK11_ExitContextMonitor(cx); |
683 | 0 | if (data) |
684 | 0 | *len = length; |
685 | 0 | } else if ((unsigned)saveLength >= cx->savedLength) { |
686 | 0 | data = (unsigned char *)cx->savedData; |
687 | 0 | if (cx->savedData) { |
688 | 0 | PORT_Memcpy(save, cx->savedData, cx->savedLength); |
689 | 0 | } |
690 | 0 | *len = cx->savedLength; |
691 | 0 | } |
692 | 0 | if (data != NULL) { |
693 | 0 | if (cx->ownSession) { |
694 | 0 | PORT_ZFree(data, length); |
695 | 0 | } |
696 | 0 | return SECSuccess; |
697 | 0 | } else { |
698 | 0 | return SECFailure; |
699 | 0 | } |
700 | 0 | } |
701 | | |
702 | | /* same as above, but may allocate the return buffer. */ |
703 | | unsigned char * |
704 | | PK11_SaveContextAlloc(PK11Context *cx, |
705 | | unsigned char *preAllocBuf, unsigned int pabLen, |
706 | | unsigned int *stateLen) |
707 | 0 | { |
708 | 0 | unsigned char *stateBuf = NULL; |
709 | 0 | unsigned long length = (unsigned long)pabLen; |
710 | |
|
711 | 0 | if (cx->ownSession) { |
712 | 0 | PK11_EnterContextMonitor(cx); |
713 | 0 | stateBuf = pk11_saveContextHelper(cx, preAllocBuf, &length); |
714 | 0 | PK11_ExitContextMonitor(cx); |
715 | 0 | *stateLen = (stateBuf != NULL) ? length : 0; |
716 | 0 | } else { |
717 | 0 | if (pabLen < cx->savedLength) { |
718 | 0 | stateBuf = (unsigned char *)PORT_Alloc(cx->savedLength); |
719 | 0 | if (!stateBuf) { |
720 | 0 | return (unsigned char *)NULL; |
721 | 0 | } |
722 | 0 | } else { |
723 | 0 | stateBuf = preAllocBuf; |
724 | 0 | } |
725 | 0 | if (cx->savedData) { |
726 | 0 | PORT_Memcpy(stateBuf, cx->savedData, cx->savedLength); |
727 | 0 | } |
728 | 0 | *stateLen = cx->savedLength; |
729 | 0 | } |
730 | 0 | return stateBuf; |
731 | 0 | } |
732 | | |
733 | | /* |
734 | | * restore the context state into a new running context. Also required for |
735 | | * FORTEZZA . |
736 | | */ |
737 | | SECStatus |
738 | | PK11_RestoreContext(PK11Context *cx, unsigned char *save, int len) |
739 | 0 | { |
740 | 0 | SECStatus rv = SECSuccess; |
741 | 0 | if (cx->ownSession) { |
742 | 0 | PK11_EnterContextMonitor(cx); |
743 | 0 | pk11_Finalize(cx); |
744 | 0 | rv = pk11_restoreContext(cx, save, len); |
745 | 0 | PK11_ExitContextMonitor(cx); |
746 | 0 | } else { |
747 | 0 | PORT_Assert(cx->savedData != NULL); |
748 | 0 | if ((cx->savedData == NULL) || (cx->savedLength < (unsigned)len)) { |
749 | 0 | PORT_SetError(SEC_ERROR_LIBRARY_FAILURE); |
750 | 0 | rv = SECFailure; |
751 | 0 | } else { |
752 | 0 | PORT_Memcpy(cx->savedData, save, len); |
753 | 0 | cx->savedLength = len; |
754 | 0 | } |
755 | 0 | } |
756 | 0 | return rv; |
757 | 0 | } |
758 | | |
759 | | /* |
760 | | * This is to get FIPS compliance until we can convert |
761 | | * libjar to use PK11_ hashing functions. It returns PR_FALSE |
762 | | * if we can't get a PK11 Context. |
763 | | */ |
764 | | PRBool |
765 | | PK11_HashOK(SECOidTag algID) |
766 | 0 | { |
767 | 0 | PK11Context *cx; |
768 | |
|
769 | 0 | cx = PK11_CreateDigestContext(algID); |
770 | 0 | if (cx == NULL) |
771 | 0 | return PR_FALSE; |
772 | 0 | PK11_DestroyContext(cx, PR_TRUE); |
773 | 0 | return PR_TRUE; |
774 | 0 | } |
775 | | |
776 | | /* |
777 | | * start a new digesting or Mac'ing operation on this context |
778 | | */ |
779 | | SECStatus |
780 | | PK11_DigestBegin(PK11Context *cx) |
781 | 0 | { |
782 | 0 | CK_MECHANISM mech_info; |
783 | 0 | SECStatus rv; |
784 | |
|
785 | 0 | if (cx->init == PR_TRUE) { |
786 | 0 | return SECSuccess; |
787 | 0 | } |
788 | | |
789 | | /* |
790 | | * make sure the old context is clear first |
791 | | */ |
792 | 0 | PK11_EnterContextMonitor(cx); |
793 | 0 | pk11_Finalize(cx); |
794 | 0 | PK11_ExitContextMonitor(cx); |
795 | |
|
796 | 0 | mech_info.mechanism = cx->type; |
797 | 0 | mech_info.pParameter = cx->param->data; |
798 | 0 | mech_info.ulParameterLen = cx->param->len; |
799 | 0 | rv = pk11_context_init(cx, &mech_info); |
800 | |
|
801 | 0 | if (rv != SECSuccess) { |
802 | 0 | return SECFailure; |
803 | 0 | } |
804 | 0 | cx->init = PR_TRUE; |
805 | 0 | return SECSuccess; |
806 | 0 | } |
807 | | |
808 | | SECStatus |
809 | | PK11_HashBuf(SECOidTag hashAlg, unsigned char *out, const unsigned char *in, |
810 | | PRInt32 len) |
811 | 0 | { |
812 | 0 | PK11Context *context; |
813 | 0 | unsigned int max_length; |
814 | 0 | unsigned int out_length; |
815 | 0 | SECStatus rv; |
816 | | |
817 | | /* len will be passed to PK11_DigestOp as unsigned. */ |
818 | 0 | if (len < 0) { |
819 | 0 | PORT_SetError(SEC_ERROR_INVALID_ARGS); |
820 | 0 | return SECFailure; |
821 | 0 | } |
822 | | |
823 | 0 | context = PK11_CreateDigestContext(hashAlg); |
824 | 0 | if (context == NULL) |
825 | 0 | return SECFailure; |
826 | | |
827 | 0 | rv = PK11_DigestBegin(context); |
828 | 0 | if (rv != SECSuccess) { |
829 | 0 | PK11_DestroyContext(context, PR_TRUE); |
830 | 0 | return rv; |
831 | 0 | } |
832 | | |
833 | 0 | rv = PK11_DigestOp(context, in, len); |
834 | 0 | if (rv != SECSuccess) { |
835 | 0 | PK11_DestroyContext(context, PR_TRUE); |
836 | 0 | return rv; |
837 | 0 | } |
838 | | |
839 | | /* XXX This really should have been an argument to this function! */ |
840 | 0 | max_length = HASH_ResultLenByOidTag(hashAlg); |
841 | 0 | PORT_Assert(max_length); |
842 | 0 | if (!max_length) |
843 | 0 | max_length = HASH_LENGTH_MAX; |
844 | |
|
845 | 0 | rv = PK11_DigestFinal(context, out, &out_length, max_length); |
846 | 0 | PK11_DestroyContext(context, PR_TRUE); |
847 | 0 | return rv; |
848 | 0 | } |
849 | | |
850 | | /* |
851 | | * execute a bulk encryption operation |
852 | | */ |
853 | | SECStatus |
854 | | PK11_CipherOp(PK11Context *context, unsigned char *out, int *outlen, |
855 | | int maxout, const unsigned char *in, int inlen) |
856 | 0 | { |
857 | 0 | CK_RV crv = CKR_OK; |
858 | 0 | CK_ULONG length = maxout; |
859 | 0 | CK_ULONG offset = 0; |
860 | 0 | SECStatus rv = SECSuccess; |
861 | 0 | unsigned char *saveOut = out; |
862 | 0 | unsigned char *allocOut = NULL; |
863 | | |
864 | | /* if we ran out of session, we need to restore our previously stored |
865 | | * state. |
866 | | */ |
867 | 0 | PK11_EnterContextMonitor(context); |
868 | 0 | if (!context->ownSession) { |
869 | 0 | rv = pk11_restoreContext(context, context->savedData, |
870 | 0 | context->savedLength); |
871 | 0 | if (rv != SECSuccess) { |
872 | 0 | PK11_ExitContextMonitor(context); |
873 | 0 | return rv; |
874 | 0 | } |
875 | 0 | } |
876 | | |
877 | | /* |
878 | | * The fortezza hack is to send 8 extra bytes on the first encrypted and |
879 | | * lose them on the first decrypt. |
880 | | */ |
881 | 0 | if (context->fortezzaHack) { |
882 | 0 | unsigned char random[8]; |
883 | 0 | if (context->operation == CKA_ENCRYPT) { |
884 | 0 | PK11_ExitContextMonitor(context); |
885 | 0 | rv = PK11_GenerateRandom(random, sizeof(random)); |
886 | 0 | PK11_EnterContextMonitor(context); |
887 | | |
888 | | /* since we are offseting the output, we can't encrypt back into |
889 | | * the same buffer... allocate a temporary buffer just for this |
890 | | * call. */ |
891 | 0 | allocOut = out = (unsigned char *)PORT_Alloc(maxout); |
892 | 0 | if (out == NULL) { |
893 | 0 | PK11_ExitContextMonitor(context); |
894 | 0 | return SECFailure; |
895 | 0 | } |
896 | 0 | crv = PK11_GETTAB(context->slot)->C_EncryptUpdate(context->session, random, sizeof(random), out, &length); |
897 | |
|
898 | 0 | out += length; |
899 | 0 | maxout -= length; |
900 | 0 | offset = length; |
901 | 0 | } else if (context->operation == CKA_DECRYPT) { |
902 | 0 | length = sizeof(random); |
903 | 0 | crv = PK11_GETTAB(context->slot)->C_DecryptUpdate(context->session, (CK_BYTE_PTR)in, sizeof(random), random, &length); |
904 | 0 | inlen -= length; |
905 | 0 | in += length; |
906 | 0 | context->fortezzaHack = PR_FALSE; |
907 | 0 | } |
908 | 0 | } |
909 | | |
910 | 0 | switch (context->operation) { |
911 | 0 | case CKA_ENCRYPT: |
912 | 0 | length = maxout; |
913 | 0 | crv = PK11_GETTAB(context->slot)->C_EncryptUpdate(context->session, (CK_BYTE_PTR)in, inlen, out, &length); |
914 | 0 | length += offset; |
915 | 0 | break; |
916 | 0 | case CKA_DECRYPT: |
917 | 0 | length = maxout; |
918 | 0 | crv = PK11_GETTAB(context->slot)->C_DecryptUpdate(context->session, (CK_BYTE_PTR)in, inlen, out, &length); |
919 | 0 | break; |
920 | 0 | default: |
921 | 0 | crv = CKR_OPERATION_NOT_INITIALIZED; |
922 | 0 | break; |
923 | 0 | } |
924 | | |
925 | 0 | if (crv != CKR_OK) { |
926 | 0 | PORT_SetError(PK11_MapError(crv)); |
927 | 0 | *outlen = 0; |
928 | 0 | rv = SECFailure; |
929 | 0 | } else { |
930 | 0 | *outlen = length; |
931 | 0 | } |
932 | |
|
933 | 0 | if (context->fortezzaHack) { |
934 | 0 | if (context->operation == CKA_ENCRYPT) { |
935 | 0 | PORT_Assert(allocOut); |
936 | 0 | PORT_Memcpy(saveOut, allocOut, length); |
937 | 0 | PORT_Free(allocOut); |
938 | 0 | } |
939 | 0 | context->fortezzaHack = PR_FALSE; |
940 | 0 | } |
941 | | |
942 | | /* |
943 | | * handle session starvation case.. use our last session to multiplex |
944 | | */ |
945 | 0 | if (!context->ownSession) { |
946 | 0 | context->savedData = pk11_saveContext(context, context->savedData, |
947 | 0 | &context->savedLength); |
948 | 0 | if (context->savedData == NULL) |
949 | 0 | rv = SECFailure; |
950 | | |
951 | | /* clear out out session for others to use */ |
952 | 0 | pk11_Finalize(context); |
953 | 0 | } |
954 | 0 | PK11_ExitContextMonitor(context); |
955 | 0 | return rv; |
956 | 0 | } |
957 | | |
958 | | /* |
959 | | * Simulate the IV generation that normally would happen in the token. |
960 | | * |
961 | | * This is a modifed copy of what is in freebl/gcm.c. We can't use the |
962 | | * version in freebl because of layering, since freebl is inside the token |
963 | | * boundary. These issues are traditionally handled by moving them to util, |
964 | | * but we also have two different Random functions we have two switch between. |
965 | | * Since this is primarily here for tokens that don't support the PKCS #11 |
966 | | * Message Interface, it's OK if they diverge a bit. Slight semantic |
967 | | * differences from the freebl/gcm.c version shouldn't be much more than the |
968 | | * sematic differences between freebl and other tokens which do implement the |
969 | | * Message Interface. */ |
970 | | static SECStatus |
971 | | pk11_GenerateIV(PK11Context *context, CK_GENERATOR_FUNCTION ivgen, |
972 | | int fixedBits, unsigned char *iv, int ivLen) |
973 | 0 | { |
974 | 0 | unsigned int i; |
975 | 0 | unsigned int flexBits; |
976 | 0 | unsigned int ivOffset; |
977 | 0 | unsigned int ivNewCount; |
978 | 0 | unsigned char ivMask; |
979 | 0 | unsigned char ivSave; |
980 | 0 | SECStatus rv; |
981 | |
|
982 | 0 | if (context->ivCounter != 0) { |
983 | | /* If we've already generated a message, make sure all subsequent |
984 | | * messages are using the same generator */ |
985 | 0 | if ((context->ivGen != ivgen) || |
986 | 0 | (context->ivFixedBits != fixedBits) || |
987 | 0 | (context->ivLen != ivLen)) { |
988 | 0 | PORT_SetError(SEC_ERROR_INVALID_ARGS); |
989 | 0 | return SECFailure; |
990 | 0 | } |
991 | 0 | } else { |
992 | | /* remember these values */ |
993 | 0 | context->ivGen = ivgen; |
994 | 0 | context->ivFixedBits = fixedBits; |
995 | 0 | context->ivLen = ivLen; |
996 | | /* now calculate how may bits of IV we have to supply */ |
997 | 0 | flexBits = ivLen * PR_BITS_PER_BYTE; |
998 | | /* first make sure we aren't going to overflow */ |
999 | 0 | if (flexBits < fixedBits) { |
1000 | 0 | PORT_SetError(SEC_ERROR_INVALID_ARGS); |
1001 | 0 | return SECFailure; |
1002 | 0 | } |
1003 | 0 | flexBits -= fixedBits; |
1004 | | /* if we are generating a random number reduce the acceptable bits to |
1005 | | * avoid birthday attacks */ |
1006 | 0 | if (ivgen == CKG_GENERATE_RANDOM) { |
1007 | 0 | if (flexBits <= GCMIV_RANDOM_BIRTHDAY_BITS) { |
1008 | 0 | PORT_SetError(SEC_ERROR_INVALID_ARGS); |
1009 | 0 | return SECFailure; |
1010 | 0 | } |
1011 | | /* see freebl/blapit.h for how GCMIV_RANDOM_BIRTHDAY_BITS is |
1012 | | * calculated. */ |
1013 | 0 | flexBits -= GCMIV_RANDOM_BIRTHDAY_BITS; |
1014 | 0 | flexBits = flexBits >> 1; |
1015 | 0 | } |
1016 | 0 | if (flexBits == 0) { |
1017 | 0 | PORT_SetError(SEC_ERROR_INVALID_ARGS); |
1018 | 0 | return SECFailure; |
1019 | 0 | } |
1020 | | /* Turn those bits into the number of IV's we can safely return */ |
1021 | 0 | if (flexBits >= sizeof(context->ivMaxCount) * PR_BITS_PER_BYTE) { |
1022 | 0 | context->ivMaxCount = PR_UINT64(0xffffffffffffffff); |
1023 | 0 | } else { |
1024 | 0 | context->ivMaxCount = (PR_UINT64(1) << flexBits); |
1025 | 0 | } |
1026 | 0 | } |
1027 | | |
1028 | | /* no generate, accept the IV from the source */ |
1029 | 0 | if (ivgen == CKG_NO_GENERATE) { |
1030 | 0 | context->ivCounter = 1; |
1031 | 0 | return SECSuccess; |
1032 | 0 | } |
1033 | | |
1034 | | /* make sure we haven't exceeded the number of IVs we can return |
1035 | | * for this key, generator, and IV size */ |
1036 | 0 | if (context->ivCounter >= context->ivMaxCount) { |
1037 | | /* use a unique error from just bad user input */ |
1038 | 0 | PORT_SetError(SEC_ERROR_EXTRA_INPUT); |
1039 | 0 | return SECFailure; |
1040 | 0 | } |
1041 | | |
1042 | | /* build to mask to handle the first byte of the IV */ |
1043 | 0 | ivOffset = fixedBits / PR_BITS_PER_BYTE; |
1044 | 0 | ivMask = 0xff >> ((PR_BITS_PER_BYTE - (fixedBits & 7)) & 7); |
1045 | 0 | ivNewCount = ivLen - ivOffset; |
1046 | | |
1047 | | /* finally generate the IV */ |
1048 | 0 | switch (ivgen) { |
1049 | 0 | case CKG_GENERATE: /* default to counter */ |
1050 | 0 | case CKG_GENERATE_COUNTER: |
1051 | 0 | iv[ivOffset] = (iv[ivOffset] & ~ivMask) | |
1052 | 0 | (PORT_GET_BYTE_BE(context->ivCounter, 0, ivNewCount) & ivMask); |
1053 | 0 | for (i = 1; i < ivNewCount; i++) { |
1054 | 0 | iv[ivOffset + i] = |
1055 | 0 | PORT_GET_BYTE_BE(context->ivCounter, i, ivNewCount); |
1056 | 0 | } |
1057 | 0 | break; |
1058 | 0 | case CKG_GENERATE_COUNTER_XOR: |
1059 | 0 | iv[ivOffset] ^= |
1060 | 0 | (PORT_GET_BYTE_BE(context->ivCounter, 0, ivNewCount) & ivMask); |
1061 | 0 | for (i = 1; i < ivNewCount; i++) { |
1062 | 0 | iv[ivOffset + i] ^= |
1063 | 0 | PORT_GET_BYTE_BE(context->ivCounter, i, ivNewCount); |
1064 | 0 | } |
1065 | 0 | break; |
1066 | 0 | case CKG_GENERATE_RANDOM: |
1067 | 0 | ivSave = iv[ivOffset] & ~ivMask; |
1068 | 0 | rv = PK11_GenerateRandom(iv + ivOffset, ivNewCount); |
1069 | 0 | iv[ivOffset] = ivSave | (iv[ivOffset] & ivMask); |
1070 | 0 | if (rv != SECSuccess) { |
1071 | 0 | return rv; |
1072 | 0 | } |
1073 | 0 | break; |
1074 | 0 | } |
1075 | 0 | context->ivCounter++; |
1076 | 0 | return SECSuccess; |
1077 | 0 | } |
1078 | | |
1079 | | /* |
1080 | | * PKCS #11 v2.40 did not have a message interface. If our module can't |
1081 | | * do the message interface use the old method of doing AEAD */ |
1082 | | static SECStatus |
1083 | | pk11_AEADSimulateOp(PK11Context *context, void *params, int paramslen, |
1084 | | const unsigned char *aad, int aadlen, |
1085 | | unsigned char *out, int *outlen, |
1086 | | int maxout, const unsigned char *in, int inlen) |
1087 | 0 | { |
1088 | 0 | unsigned int length = maxout; |
1089 | 0 | SECStatus rv = SECSuccess; |
1090 | 0 | unsigned char *saveOut = out; |
1091 | 0 | unsigned char *allocOut = NULL; |
1092 | | |
1093 | | /* |
1094 | | * first we need to convert the single shot (v2.40) parameters into |
1095 | | * the message version of the parameters. This usually involves |
1096 | | * copying the Nonce or IV, setting the AAD from our parameter list |
1097 | | * and handling the tag differences */ |
1098 | 0 | CK_GCM_PARAMS_V3 gcm; |
1099 | 0 | CK_GCM_MESSAGE_PARAMS *gcm_message; |
1100 | 0 | CK_CCM_PARAMS ccm; |
1101 | 0 | CK_CCM_MESSAGE_PARAMS *ccm_message; |
1102 | 0 | CK_SALSA20_CHACHA20_POLY1305_PARAMS chacha_poly; |
1103 | 0 | CK_SALSA20_CHACHA20_POLY1305_MSG_PARAMS *chacha_poly_message; |
1104 | 0 | CK_NSS_AEAD_PARAMS nss_chacha_poly; |
1105 | 0 | CK_MECHANISM_TYPE mechanism = context->simulate_mechanism; |
1106 | 0 | SECItem sim_params = { 0, NULL, 0 }; |
1107 | 0 | unsigned char *tag = NULL; |
1108 | 0 | unsigned int taglen; |
1109 | 0 | PRBool encrypt; |
1110 | |
|
1111 | 0 | *outlen = 0; |
1112 | | /* figure out if we are encrypting or decrypting, as tags are |
1113 | | * handled differently in both */ |
1114 | 0 | switch (context->operation) { |
1115 | 0 | case CKA_NSS_MESSAGE | CKA_ENCRYPT: |
1116 | 0 | encrypt = PR_TRUE; |
1117 | 0 | break; |
1118 | 0 | case CKA_NSS_MESSAGE | CKA_DECRYPT: |
1119 | 0 | encrypt = PR_FALSE; |
1120 | 0 | break; |
1121 | 0 | default: |
1122 | 0 | PORT_SetError(SEC_ERROR_INVALID_ARGS); |
1123 | 0 | return SECFailure; |
1124 | 0 | } |
1125 | | |
1126 | 0 | switch (mechanism) { |
1127 | 0 | case CKM_CHACHA20_POLY1305: |
1128 | 0 | case CKM_SALSA20_POLY1305: |
1129 | 0 | if (paramslen != sizeof(CK_SALSA20_CHACHA20_POLY1305_MSG_PARAMS)) { |
1130 | 0 | PORT_SetError(SEC_ERROR_INVALID_ARGS); |
1131 | 0 | return SECFailure; |
1132 | 0 | } |
1133 | 0 | chacha_poly_message = |
1134 | 0 | (CK_SALSA20_CHACHA20_POLY1305_MSG_PARAMS *)params; |
1135 | 0 | chacha_poly.pNonce = chacha_poly_message->pNonce; |
1136 | 0 | chacha_poly.ulNonceLen = chacha_poly_message->ulNonceLen; |
1137 | 0 | chacha_poly.pAAD = (CK_BYTE_PTR)aad; |
1138 | 0 | chacha_poly.ulAADLen = aadlen; |
1139 | 0 | tag = chacha_poly_message->pTag; |
1140 | 0 | taglen = 16; |
1141 | 0 | sim_params.data = (unsigned char *)&chacha_poly; |
1142 | 0 | sim_params.len = sizeof(chacha_poly); |
1143 | | /* SALSA20_POLY1305 and CHACHA20_POLY1305 do not generate the iv |
1144 | | * internally, don't simulate it either */ |
1145 | 0 | break; |
1146 | 0 | case CKM_NSS_CHACHA20_POLY1305: |
1147 | 0 | if (paramslen != sizeof(CK_SALSA20_CHACHA20_POLY1305_MSG_PARAMS)) { |
1148 | 0 | PORT_SetError(SEC_ERROR_INVALID_ARGS); |
1149 | 0 | return SECFailure; |
1150 | 0 | } |
1151 | 0 | chacha_poly_message = |
1152 | 0 | (CK_SALSA20_CHACHA20_POLY1305_MSG_PARAMS *)params; |
1153 | 0 | tag = chacha_poly_message->pTag; |
1154 | 0 | taglen = 16; |
1155 | 0 | nss_chacha_poly.pNonce = chacha_poly_message->pNonce; |
1156 | 0 | nss_chacha_poly.ulNonceLen = chacha_poly_message->ulNonceLen; |
1157 | 0 | nss_chacha_poly.pAAD = (CK_BYTE_PTR)aad; |
1158 | 0 | nss_chacha_poly.ulAADLen = aadlen; |
1159 | 0 | nss_chacha_poly.ulTagLen = taglen; |
1160 | 0 | sim_params.data = (unsigned char *)&nss_chacha_poly; |
1161 | 0 | sim_params.len = sizeof(nss_chacha_poly); |
1162 | | /* CKM_NSS_CHACHA20_POLY1305 does not generate the iv |
1163 | | * internally, don't simulate it either */ |
1164 | 0 | break; |
1165 | 0 | case CKM_AES_CCM: |
1166 | 0 | if (paramslen != sizeof(CK_CCM_MESSAGE_PARAMS)) { |
1167 | 0 | PORT_SetError(SEC_ERROR_INVALID_ARGS); |
1168 | 0 | return SECFailure; |
1169 | 0 | } |
1170 | 0 | ccm_message = (CK_CCM_MESSAGE_PARAMS *)params; |
1171 | 0 | ccm.ulDataLen = ccm_message->ulDataLen; |
1172 | 0 | ccm.pNonce = ccm_message->pNonce; |
1173 | 0 | ccm.ulNonceLen = ccm_message->ulNonceLen; |
1174 | 0 | ccm.pAAD = (CK_BYTE_PTR)aad; |
1175 | 0 | ccm.ulAADLen = aadlen; |
1176 | 0 | ccm.ulMACLen = ccm_message->ulMACLen; |
1177 | 0 | tag = ccm_message->pMAC; |
1178 | 0 | taglen = ccm_message->ulMACLen; |
1179 | 0 | sim_params.data = (unsigned char *)&ccm; |
1180 | 0 | sim_params.len = sizeof(ccm); |
1181 | 0 | if (encrypt) { |
1182 | | /* simulate generating the IV */ |
1183 | 0 | rv = pk11_GenerateIV(context, ccm_message->nonceGenerator, |
1184 | 0 | ccm_message->ulNonceFixedBits, |
1185 | 0 | ccm_message->pNonce, |
1186 | 0 | ccm_message->ulNonceLen); |
1187 | 0 | if (rv != SECSuccess) { |
1188 | 0 | return rv; |
1189 | 0 | } |
1190 | 0 | } |
1191 | 0 | break; |
1192 | 0 | case CKM_AES_GCM: |
1193 | 0 | if (paramslen != sizeof(CK_GCM_MESSAGE_PARAMS)) { |
1194 | 0 | PORT_SetError(SEC_ERROR_INVALID_ARGS); |
1195 | 0 | return SECFailure; |
1196 | 0 | } |
1197 | 0 | gcm_message = (CK_GCM_MESSAGE_PARAMS *)params; |
1198 | 0 | gcm.pIv = gcm_message->pIv; |
1199 | 0 | gcm.ulIvLen = gcm_message->ulIvLen; |
1200 | 0 | gcm.ulIvBits = gcm.ulIvLen * PR_BITS_PER_BYTE; |
1201 | 0 | gcm.pAAD = (CK_BYTE_PTR)aad; |
1202 | 0 | gcm.ulAADLen = aadlen; |
1203 | 0 | gcm.ulTagBits = gcm_message->ulTagBits; |
1204 | 0 | tag = gcm_message->pTag; |
1205 | 0 | taglen = (gcm_message->ulTagBits + (PR_BITS_PER_BYTE - 1)) / PR_BITS_PER_BYTE; |
1206 | 0 | sim_params.data = (unsigned char *)&gcm; |
1207 | 0 | sim_params.len = sizeof(gcm); |
1208 | 0 | if (encrypt) { |
1209 | | /* simulate generating the IV */ |
1210 | 0 | rv = pk11_GenerateIV(context, gcm_message->ivGenerator, |
1211 | 0 | gcm_message->ulIvFixedBits, |
1212 | 0 | gcm_message->pIv, gcm_message->ulIvLen); |
1213 | 0 | if (rv != SECSuccess) { |
1214 | 0 | return rv; |
1215 | 0 | } |
1216 | 0 | } |
1217 | 0 | break; |
1218 | 0 | default: |
1219 | 0 | PORT_SetError(SEC_ERROR_INVALID_ALGORITHM); |
1220 | 0 | return SECFailure; |
1221 | 0 | } |
1222 | | /* now handle the tag. The message interface separates the tag from |
1223 | | * the data, while the single shot gets and puts the tag at the end of |
1224 | | * the encrypted data. */ |
1225 | 0 | if (!encrypt) { |
1226 | | /* In the decrypt case, if the tag is already at the end of the |
1227 | | * input buffer we are golden, otherwise we'll need a new input |
1228 | | * buffer and copy the tag at the end of it */ |
1229 | 0 | if (tag != in + inlen) { |
1230 | 0 | allocOut = PORT_Alloc(inlen + taglen); |
1231 | 0 | if (allocOut == NULL) { |
1232 | 0 | return SECFailure; |
1233 | 0 | } |
1234 | 0 | PORT_Memcpy(allocOut, in, inlen); |
1235 | 0 | PORT_Memcpy(allocOut + inlen, tag, taglen); |
1236 | 0 | in = allocOut; |
1237 | 0 | } |
1238 | 0 | inlen = inlen + taglen; |
1239 | 0 | } else { |
1240 | | /* if we end up allocating, we don't want to overrun this buffer, |
1241 | | * so we fail early here */ |
1242 | 0 | if (maxout < inlen) { |
1243 | 0 | PORT_SetError(SEC_ERROR_INVALID_ARGS); |
1244 | 0 | return SECFailure; |
1245 | 0 | } |
1246 | | /* in the encrypt case, we are fine if maxout is big enough to hold |
1247 | | * the tag. We'll copy the tag after the operation */ |
1248 | 0 | if (maxout < inlen + taglen) { |
1249 | 0 | allocOut = PORT_Alloc(inlen + taglen); |
1250 | 0 | if (allocOut == NULL) { |
1251 | 0 | return SECFailure; |
1252 | 0 | } |
1253 | 0 | out = allocOut; |
1254 | 0 | length = maxout = inlen + taglen; |
1255 | 0 | } |
1256 | 0 | } |
1257 | | /* now do the operation */ |
1258 | 0 | if (encrypt) { |
1259 | 0 | rv = PK11_Encrypt(context->key, mechanism, &sim_params, out, &length, |
1260 | 0 | maxout, in, inlen); |
1261 | 0 | } else { |
1262 | 0 | rv = PK11_Decrypt(context->key, mechanism, &sim_params, out, &length, |
1263 | 0 | maxout, in, inlen); |
1264 | 0 | } |
1265 | 0 | if (rv != SECSuccess) { |
1266 | | /* If the mechanism was CKM_AES_GCM, the module may have been |
1267 | | * following the same error as old versions of NSS. Retry with |
1268 | | * the CK_NSS_GCM_PARAMS */ |
1269 | 0 | if ((mechanism == CKM_AES_GCM) && |
1270 | 0 | (PORT_GetError() == SEC_ERROR_BAD_DATA)) { |
1271 | 0 | CK_NSS_GCM_PARAMS gcm_nss; |
1272 | 0 | gcm_message = (CK_GCM_MESSAGE_PARAMS *)params; |
1273 | 0 | gcm_nss.pIv = gcm_message->pIv; |
1274 | 0 | gcm_nss.ulIvLen = gcm_message->ulIvLen; |
1275 | 0 | gcm_nss.pAAD = (CK_BYTE_PTR)aad; |
1276 | 0 | gcm_nss.ulAADLen = aadlen; |
1277 | 0 | gcm_nss.ulTagBits = gcm_message->ulTagBits; |
1278 | 0 | sim_params.data = (unsigned char *)&gcm_nss; |
1279 | 0 | sim_params.len = sizeof(gcm_nss); |
1280 | 0 | if (encrypt) { |
1281 | 0 | rv = PK11_Encrypt(context->key, mechanism, &sim_params, out, |
1282 | 0 | &length, maxout, in, inlen); |
1283 | 0 | } else { |
1284 | 0 | rv = PK11_Decrypt(context->key, mechanism, &sim_params, out, |
1285 | 0 | &length, maxout, in, inlen); |
1286 | 0 | } |
1287 | 0 | if (rv != SECSuccess) { |
1288 | 0 | goto fail; |
1289 | 0 | } |
1290 | 0 | } else { |
1291 | 0 | goto fail; |
1292 | 0 | } |
1293 | 0 | } |
1294 | | |
1295 | | /* on encrypt, separate the output buffer from the tag */ |
1296 | 0 | if (encrypt) { |
1297 | 0 | if ((length < taglen) || (length > inlen + taglen)) { |
1298 | | /* PKCS #11 module should not return a length smaller than |
1299 | | * taglen, or bigger than inlen+taglen */ |
1300 | 0 | PORT_SetError(SEC_ERROR_LIBRARY_FAILURE); |
1301 | 0 | rv = SECFailure; |
1302 | 0 | goto fail; |
1303 | 0 | } |
1304 | 0 | length = length - taglen; |
1305 | 0 | if (allocOut) { |
1306 | | /* |
1307 | | * If we used a temporary buffer, copy it out to the original |
1308 | | * buffer. |
1309 | | */ |
1310 | 0 | PORT_Memcpy(saveOut, allocOut, length); |
1311 | 0 | } |
1312 | | /* if the tag isn't in the right place, copy it out */ |
1313 | 0 | if (tag != out + length) { |
1314 | 0 | PORT_Memcpy(tag, out + length, taglen); |
1315 | 0 | } |
1316 | 0 | } |
1317 | 0 | *outlen = length; |
1318 | 0 | rv = SECSuccess; |
1319 | 0 | fail: |
1320 | 0 | if (allocOut) { |
1321 | 0 | PORT_Free(allocOut); |
1322 | 0 | } |
1323 | 0 | return rv; |
1324 | 0 | } |
1325 | | |
1326 | | /* |
1327 | | * Do an AEAD operation. This function optionally returns |
1328 | | * and IV on Encrypt for all mechanism. NSS knows which mechanisms |
1329 | | * generate IV's in the token and which don't. This allows the |
1330 | | * applications to make a single call without special handling for |
1331 | | * each AEAD mechanism (the special handling is all contained here. |
1332 | | */ |
1333 | | SECStatus |
1334 | | PK11_AEADOp(PK11Context *context, CK_GENERATOR_FUNCTION ivgen, |
1335 | | int fixedbits, unsigned char *iv, int ivlen, |
1336 | | const unsigned char *aad, int aadlen, |
1337 | | unsigned char *out, int *outlen, |
1338 | | int maxout, unsigned char *tag, int taglen, |
1339 | | const unsigned char *in, int inlen) |
1340 | 0 | { |
1341 | 0 | CK_GCM_MESSAGE_PARAMS gcm_message; |
1342 | 0 | CK_CCM_MESSAGE_PARAMS ccm_message; |
1343 | 0 | CK_SALSA20_CHACHA20_POLY1305_MSG_PARAMS chacha_poly_message; |
1344 | 0 | void *params; |
1345 | 0 | int paramslen; |
1346 | 0 | SECStatus rv; |
1347 | |
|
1348 | 0 | switch (context->simulate_mechanism) { |
1349 | 0 | case CKM_CHACHA20_POLY1305: |
1350 | 0 | case CKM_SALSA20_POLY1305: |
1351 | 0 | case CKM_NSS_CHACHA20_POLY1305: |
1352 | 0 | chacha_poly_message.pNonce = iv; |
1353 | 0 | chacha_poly_message.ulNonceLen = ivlen; |
1354 | 0 | chacha_poly_message.pTag = tag; |
1355 | 0 | params = &chacha_poly_message; |
1356 | 0 | paramslen = sizeof(CK_SALSA20_CHACHA20_POLY1305_MSG_PARAMS); |
1357 | | /* SALSA20_POLY1305 and CHACHA20_POLY1305 do not generate the iv |
1358 | | * internally, Do it here. */ |
1359 | 0 | if (context->operation == (CKA_NSS_MESSAGE | CKA_ENCRYPT)) { |
1360 | | /* simulate generating the IV */ |
1361 | 0 | rv = pk11_GenerateIV(context, ivgen, fixedbits, iv, ivlen); |
1362 | 0 | if (rv != SECSuccess) { |
1363 | 0 | return rv; |
1364 | 0 | } |
1365 | 0 | } |
1366 | 0 | break; |
1367 | 0 | case CKM_AES_GCM: |
1368 | 0 | gcm_message.pIv = iv; |
1369 | 0 | gcm_message.ulIvLen = ivlen; |
1370 | 0 | gcm_message.ivGenerator = ivgen; |
1371 | 0 | gcm_message.ulIvFixedBits = fixedbits; |
1372 | 0 | gcm_message.pTag = tag; |
1373 | 0 | gcm_message.ulTagBits = taglen * 8; |
1374 | 0 | params = &gcm_message; |
1375 | 0 | paramslen = sizeof(CK_GCM_MESSAGE_PARAMS); |
1376 | | /* GCM generates IV internally */ |
1377 | 0 | break; |
1378 | 0 | case CKM_AES_CCM: |
1379 | 0 | ccm_message.ulDataLen = inlen; |
1380 | 0 | ccm_message.pNonce = iv; |
1381 | 0 | ccm_message.ulNonceLen = ivlen; |
1382 | 0 | ccm_message.nonceGenerator = ivgen; |
1383 | 0 | ccm_message.ulNonceFixedBits = fixedbits; |
1384 | 0 | ccm_message.pMAC = tag; |
1385 | 0 | ccm_message.ulMACLen = taglen; |
1386 | 0 | params = &ccm_message; |
1387 | 0 | paramslen = sizeof(CK_GCM_MESSAGE_PARAMS); |
1388 | | /* CCM generates IV internally */ |
1389 | 0 | break; |
1390 | | |
1391 | 0 | default: |
1392 | 0 | PORT_SetError(SEC_ERROR_INVALID_ALGORITHM); |
1393 | 0 | return SECFailure; |
1394 | 0 | } |
1395 | 0 | return PK11_AEADRawOp(context, params, paramslen, aad, aadlen, out, outlen, |
1396 | 0 | maxout, in, inlen); |
1397 | 0 | } |
1398 | | |
1399 | | /* Do and AED operation. The application builds the params on it's own |
1400 | | * and passes them in. This allows applications direct access to the params |
1401 | | * so they can use mechanisms not yet understood by, NSS, or get semantics |
1402 | | * not suppied by PK11_AEAD. */ |
1403 | | SECStatus |
1404 | | PK11_AEADRawOp(PK11Context *context, void *params, int paramslen, |
1405 | | const unsigned char *aad, int aadlen, |
1406 | | unsigned char *out, int *outlen, |
1407 | | int maxout, const unsigned char *in, int inlen) |
1408 | 0 | { |
1409 | 0 | CK_RV crv = CKR_OK; |
1410 | 0 | CK_ULONG length = maxout; |
1411 | 0 | SECStatus rv = SECSuccess; |
1412 | |
|
1413 | 0 | PORT_Assert(outlen != NULL); |
1414 | 0 | *outlen = 0; |
1415 | 0 | if (((context->operation) & CKA_NSS_MESSAGE_MASK) != CKA_NSS_MESSAGE) { |
1416 | 0 | PORT_SetError(SEC_ERROR_INVALID_ARGS); |
1417 | 0 | return SECFailure; |
1418 | 0 | } |
1419 | | |
1420 | | /* |
1421 | | * The PKCS 11 module does not support the message interface, fall |
1422 | | * back to using single shot operation */ |
1423 | 0 | if (context->simulate_message) { |
1424 | 0 | return pk11_AEADSimulateOp(context, params, paramslen, aad, aadlen, |
1425 | 0 | out, outlen, maxout, in, inlen); |
1426 | 0 | } |
1427 | | |
1428 | | /* if we ran out of session, we need to restore our previously stored |
1429 | | * state. |
1430 | | */ |
1431 | 0 | PK11_EnterContextMonitor(context); |
1432 | 0 | if (!context->ownSession) { |
1433 | 0 | rv = pk11_restoreContext(context, context->savedData, |
1434 | 0 | context->savedLength); |
1435 | 0 | if (rv != SECSuccess) { |
1436 | 0 | PK11_ExitContextMonitor(context); |
1437 | 0 | return rv; |
1438 | 0 | } |
1439 | 0 | } |
1440 | | |
1441 | 0 | switch (context->operation) { |
1442 | 0 | case CKA_NSS_MESSAGE | CKA_ENCRYPT: |
1443 | 0 | length = maxout; |
1444 | 0 | crv = PK11_GETTAB(context->slot)->C_EncryptMessage(context->session, params, paramslen, (CK_BYTE_PTR)aad, aadlen, (CK_BYTE_PTR)in, inlen, out, &length); |
1445 | 0 | break; |
1446 | 0 | case CKA_NSS_MESSAGE | CKA_DECRYPT: |
1447 | 0 | length = maxout; |
1448 | 0 | crv = PK11_GETTAB(context->slot)->C_DecryptMessage(context->session, params, paramslen, (CK_BYTE_PTR)aad, aadlen, (CK_BYTE_PTR)in, inlen, out, &length); |
1449 | 0 | break; |
1450 | 0 | case CKA_NSS_MESSAGE | CKA_SIGN: |
1451 | 0 | length = maxout; |
1452 | 0 | crv = PK11_GETTAB(context->slot)->C_SignMessage(context->session, params, paramslen, (CK_BYTE_PTR)in, inlen, out, &length); |
1453 | 0 | break; |
1454 | 0 | case CKA_NSS_MESSAGE | CKA_VERIFY: |
1455 | 0 | length = maxout; /* sig length */ |
1456 | 0 | crv = PK11_GETTAB(context->slot)->C_VerifyMessage(context->session, params, paramslen, (CK_BYTE_PTR)in, inlen, out /* sig */, length); |
1457 | 0 | break; |
1458 | 0 | default: |
1459 | 0 | crv = CKR_OPERATION_NOT_INITIALIZED; |
1460 | 0 | break; |
1461 | 0 | } |
1462 | | |
1463 | 0 | if (crv != CKR_OK) { |
1464 | 0 | PORT_SetError(PK11_MapError(crv)); |
1465 | 0 | rv = SECFailure; |
1466 | 0 | } else { |
1467 | 0 | *outlen = length; |
1468 | 0 | } |
1469 | | |
1470 | | /* |
1471 | | * handle session starvation case.. use our last session to multiplex |
1472 | | */ |
1473 | 0 | if (!context->ownSession) { |
1474 | 0 | context->savedData = pk11_saveContext(context, context->savedData, |
1475 | 0 | &context->savedLength); |
1476 | 0 | if (context->savedData == NULL) |
1477 | 0 | rv = SECFailure; |
1478 | | |
1479 | | /* clear out out session for others to use */ |
1480 | 0 | pk11_Finalize(context); |
1481 | 0 | } |
1482 | 0 | PK11_ExitContextMonitor(context); |
1483 | 0 | return rv; |
1484 | 0 | } |
1485 | | |
1486 | | /* |
1487 | | * execute a digest/signature operation |
1488 | | */ |
1489 | | SECStatus |
1490 | | PK11_DigestOp(PK11Context *context, const unsigned char *in, unsigned inLen) |
1491 | 0 | { |
1492 | 0 | CK_RV crv = CKR_OK; |
1493 | 0 | SECStatus rv = SECSuccess; |
1494 | |
|
1495 | 0 | if (inLen == 0) { |
1496 | 0 | return SECSuccess; |
1497 | 0 | } |
1498 | 0 | if (!in) { |
1499 | 0 | PORT_SetError(SEC_ERROR_INVALID_ARGS); |
1500 | 0 | return SECFailure; |
1501 | 0 | } |
1502 | | |
1503 | | /* if we ran out of session, we need to restore our previously stored |
1504 | | * state. |
1505 | | */ |
1506 | 0 | context->init = PR_FALSE; |
1507 | 0 | PK11_EnterContextMonitor(context); |
1508 | 0 | if (!context->ownSession) { |
1509 | 0 | rv = pk11_restoreContext(context, context->savedData, |
1510 | 0 | context->savedLength); |
1511 | 0 | if (rv != SECSuccess) { |
1512 | 0 | PK11_ExitContextMonitor(context); |
1513 | 0 | return rv; |
1514 | 0 | } |
1515 | 0 | } |
1516 | | |
1517 | 0 | switch (context->operation) { |
1518 | | /* also for MAC'ing */ |
1519 | 0 | case CKA_SIGN: |
1520 | 0 | crv = PK11_GETTAB(context->slot)->C_SignUpdate(context->session, (unsigned char *)in, inLen); |
1521 | 0 | break; |
1522 | 0 | case CKA_VERIFY: |
1523 | 0 | crv = PK11_GETTAB(context->slot)->C_VerifyUpdate(context->session, (unsigned char *)in, inLen); |
1524 | 0 | break; |
1525 | 0 | case CKA_DIGEST: |
1526 | 0 | crv = PK11_GETTAB(context->slot)->C_DigestUpdate(context->session, (unsigned char *)in, inLen); |
1527 | 0 | break; |
1528 | 0 | default: |
1529 | 0 | crv = CKR_OPERATION_NOT_INITIALIZED; |
1530 | 0 | break; |
1531 | 0 | } |
1532 | | |
1533 | 0 | if (crv != CKR_OK) { |
1534 | 0 | PORT_SetError(PK11_MapError(crv)); |
1535 | 0 | rv = SECFailure; |
1536 | 0 | } |
1537 | | |
1538 | | /* |
1539 | | * handle session starvation case.. use our last session to multiplex |
1540 | | */ |
1541 | 0 | if (!context->ownSession) { |
1542 | 0 | context->savedData = pk11_saveContext(context, context->savedData, |
1543 | 0 | &context->savedLength); |
1544 | 0 | if (context->savedData == NULL) |
1545 | 0 | rv = SECFailure; |
1546 | | |
1547 | | /* clear out out session for others to use */ |
1548 | 0 | pk11_Finalize(context); |
1549 | 0 | } |
1550 | 0 | PK11_ExitContextMonitor(context); |
1551 | 0 | return rv; |
1552 | 0 | } |
1553 | | |
1554 | | /* |
1555 | | * Digest a key if possible./ |
1556 | | */ |
1557 | | SECStatus |
1558 | | PK11_DigestKey(PK11Context *context, PK11SymKey *key) |
1559 | 0 | { |
1560 | 0 | CK_RV crv = CKR_OK; |
1561 | 0 | SECStatus rv = SECSuccess; |
1562 | 0 | PK11SymKey *newKey = NULL; |
1563 | |
|
1564 | 0 | if (!context || !key) { |
1565 | 0 | PORT_SetError(SEC_ERROR_INVALID_ARGS); |
1566 | 0 | return SECFailure; |
1567 | 0 | } |
1568 | | |
1569 | | /* if we ran out of session, we need to restore our previously stored |
1570 | | * state. |
1571 | | */ |
1572 | 0 | if (context->slot != key->slot) { |
1573 | 0 | newKey = pk11_CopyToSlot(context->slot, CKM_SSL3_SHA1_MAC, CKA_SIGN, key); |
1574 | 0 | } else { |
1575 | 0 | newKey = PK11_ReferenceSymKey(key); |
1576 | 0 | } |
1577 | |
|
1578 | 0 | context->init = PR_FALSE; |
1579 | 0 | PK11_EnterContextMonitor(context); |
1580 | 0 | if (!context->ownSession) { |
1581 | 0 | rv = pk11_restoreContext(context, context->savedData, |
1582 | 0 | context->savedLength); |
1583 | 0 | if (rv != SECSuccess) { |
1584 | 0 | PK11_ExitContextMonitor(context); |
1585 | 0 | PK11_FreeSymKey(newKey); |
1586 | 0 | return rv; |
1587 | 0 | } |
1588 | 0 | } |
1589 | | |
1590 | 0 | if (newKey == NULL) { |
1591 | 0 | crv = CKR_KEY_TYPE_INCONSISTENT; |
1592 | 0 | if (key->data.data) { |
1593 | 0 | crv = PK11_GETTAB(context->slot)->C_DigestUpdate(context->session, key->data.data, key->data.len); |
1594 | 0 | } |
1595 | 0 | } else { |
1596 | 0 | crv = PK11_GETTAB(context->slot)->C_DigestKey(context->session, newKey->objectID); |
1597 | 0 | } |
1598 | |
|
1599 | 0 | if (crv != CKR_OK) { |
1600 | 0 | PORT_SetError(PK11_MapError(crv)); |
1601 | 0 | rv = SECFailure; |
1602 | 0 | } |
1603 | | |
1604 | | /* |
1605 | | * handle session starvation case.. use our last session to multiplex |
1606 | | */ |
1607 | 0 | if (!context->ownSession) { |
1608 | 0 | context->savedData = pk11_saveContext(context, context->savedData, |
1609 | 0 | &context->savedLength); |
1610 | 0 | if (context->savedData == NULL) |
1611 | 0 | rv = SECFailure; |
1612 | | |
1613 | | /* clear out out session for others to use */ |
1614 | 0 | pk11_Finalize(context); |
1615 | 0 | } |
1616 | 0 | PK11_ExitContextMonitor(context); |
1617 | 0 | if (newKey) |
1618 | 0 | PK11_FreeSymKey(newKey); |
1619 | 0 | return rv; |
1620 | 0 | } |
1621 | | |
1622 | | /* |
1623 | | * externally callable version of the lowercase pk11_finalize(). |
1624 | | */ |
1625 | | SECStatus |
1626 | | PK11_Finalize(PK11Context *context) |
1627 | 0 | { |
1628 | 0 | SECStatus rv; |
1629 | |
|
1630 | 0 | PK11_EnterContextMonitor(context); |
1631 | 0 | rv = pk11_Finalize(context); |
1632 | 0 | PK11_ExitContextMonitor(context); |
1633 | 0 | return rv; |
1634 | 0 | } |
1635 | | |
1636 | | /* |
1637 | | * clean up a cipher operation, so the session can be used by |
1638 | | * someone new. |
1639 | | */ |
1640 | | SECStatus |
1641 | | pk11_Finalize(PK11Context *context) |
1642 | 0 | { |
1643 | 0 | CK_ULONG count = 0; |
1644 | 0 | CK_RV crv; |
1645 | 0 | unsigned char stackBuf[256]; |
1646 | 0 | unsigned char *buffer = NULL; |
1647 | |
|
1648 | 0 | if (!context->ownSession) { |
1649 | 0 | return SECSuccess; |
1650 | 0 | } |
1651 | | |
1652 | 0 | finalize: |
1653 | 0 | switch (context->operation) { |
1654 | 0 | case CKA_ENCRYPT: |
1655 | 0 | crv = PK11_GETTAB(context->slot)->C_EncryptFinal(context->session, buffer, &count); |
1656 | 0 | break; |
1657 | 0 | case CKA_DECRYPT: |
1658 | 0 | crv = PK11_GETTAB(context->slot)->C_DecryptFinal(context->session, buffer, &count); |
1659 | 0 | break; |
1660 | 0 | case CKA_SIGN: |
1661 | 0 | crv = PK11_GETTAB(context->slot)->C_SignFinal(context->session, buffer, &count); |
1662 | 0 | break; |
1663 | 0 | case CKA_VERIFY: |
1664 | 0 | crv = PK11_GETTAB(context->slot)->C_VerifyFinal(context->session, buffer, count); |
1665 | 0 | break; |
1666 | 0 | case CKA_DIGEST: |
1667 | 0 | crv = PK11_GETTAB(context->slot)->C_DigestFinal(context->session, buffer, &count); |
1668 | 0 | break; |
1669 | 0 | case CKA_NSS_MESSAGE | CKA_ENCRYPT: |
1670 | 0 | crv = PK11_GETTAB(context->slot)->C_MessageEncryptFinal(context->session); |
1671 | 0 | break; |
1672 | 0 | case CKA_NSS_MESSAGE | CKA_DECRYPT: |
1673 | 0 | crv = PK11_GETTAB(context->slot)->C_MessageDecryptFinal(context->session); |
1674 | 0 | break; |
1675 | 0 | case CKA_NSS_MESSAGE | CKA_SIGN: |
1676 | 0 | crv = PK11_GETTAB(context->slot)->C_MessageSignFinal(context->session); |
1677 | 0 | break; |
1678 | 0 | case CKA_NSS_MESSAGE | CKA_VERIFY: |
1679 | 0 | crv = PK11_GETTAB(context->slot)->C_MessageVerifyFinal(context->session); |
1680 | 0 | break; |
1681 | 0 | default: |
1682 | 0 | crv = CKR_OPERATION_NOT_INITIALIZED; |
1683 | 0 | break; |
1684 | 0 | } |
1685 | | |
1686 | 0 | if (crv != CKR_OK) { |
1687 | 0 | if (buffer != stackBuf) { |
1688 | 0 | PORT_Free(buffer); |
1689 | 0 | } |
1690 | 0 | if (crv == CKR_OPERATION_NOT_INITIALIZED) { |
1691 | | /* if there's no operation, it is finalized */ |
1692 | 0 | return SECSuccess; |
1693 | 0 | } |
1694 | 0 | PORT_SetError(PK11_MapError(crv)); |
1695 | 0 | return SECFailure; |
1696 | 0 | } |
1697 | | |
1698 | | /* Message interface does not need to allocate a final buffer */ |
1699 | 0 | if (((context->operation) & CKA_NSS_MESSAGE_MASK) == CKA_NSS_MESSAGE) { |
1700 | 0 | return SECSuccess; |
1701 | 0 | } |
1702 | | |
1703 | | /* try to finalize the session with a buffer */ |
1704 | 0 | if (buffer == NULL) { |
1705 | 0 | if (count <= sizeof stackBuf) { |
1706 | 0 | buffer = stackBuf; |
1707 | 0 | } else { |
1708 | 0 | buffer = PORT_Alloc(count); |
1709 | 0 | if (buffer == NULL) { |
1710 | 0 | return SECFailure; |
1711 | 0 | } |
1712 | 0 | } |
1713 | 0 | goto finalize; |
1714 | 0 | } |
1715 | 0 | if (buffer != stackBuf) { |
1716 | 0 | PORT_Free(buffer); |
1717 | 0 | } |
1718 | 0 | return SECSuccess; |
1719 | 0 | } |
1720 | | |
1721 | | /* |
1722 | | * Return the final digested or signed data... |
1723 | | * this routine can either take pre initialized data, or allocate data |
1724 | | * either out of an arena or out of the standard heap. |
1725 | | */ |
1726 | | SECStatus |
1727 | | PK11_DigestFinal(PK11Context *context, unsigned char *data, |
1728 | | unsigned int *outLen, unsigned int length) |
1729 | 0 | { |
1730 | 0 | CK_ULONG len; |
1731 | 0 | CK_RV crv; |
1732 | 0 | SECStatus rv; |
1733 | | |
1734 | | /* message interface returns no data on Final, Should not use DigestFinal |
1735 | | * in this case */ |
1736 | 0 | if (((context->operation) & CKA_NSS_MESSAGE_MASK) == CKA_NSS_MESSAGE) { |
1737 | 0 | PORT_SetError(SEC_ERROR_INVALID_ARGS); |
1738 | 0 | return SECFailure; |
1739 | 0 | } |
1740 | | |
1741 | | /* if we ran out of session, we need to restore our previously stored |
1742 | | * state. |
1743 | | */ |
1744 | 0 | PK11_EnterContextMonitor(context); |
1745 | 0 | if (!context->ownSession) { |
1746 | 0 | rv = pk11_restoreContext(context, context->savedData, |
1747 | 0 | context->savedLength); |
1748 | 0 | if (rv != SECSuccess) { |
1749 | 0 | PK11_ExitContextMonitor(context); |
1750 | 0 | return rv; |
1751 | 0 | } |
1752 | 0 | } |
1753 | | |
1754 | 0 | len = length; |
1755 | 0 | switch (context->operation) { |
1756 | 0 | case CKA_SIGN: |
1757 | 0 | crv = PK11_GETTAB(context->slot)->C_SignFinal(context->session, data, &len); |
1758 | 0 | break; |
1759 | 0 | case CKA_VERIFY: |
1760 | 0 | crv = PK11_GETTAB(context->slot)->C_VerifyFinal(context->session, data, len); |
1761 | 0 | break; |
1762 | 0 | case CKA_DIGEST: |
1763 | 0 | crv = PK11_GETTAB(context->slot)->C_DigestFinal(context->session, data, &len); |
1764 | 0 | break; |
1765 | 0 | case CKA_ENCRYPT: |
1766 | 0 | crv = PK11_GETTAB(context->slot)->C_EncryptFinal(context->session, data, &len); |
1767 | 0 | break; |
1768 | 0 | case CKA_DECRYPT: |
1769 | 0 | crv = PK11_GETTAB(context->slot)->C_DecryptFinal(context->session, data, &len); |
1770 | 0 | break; |
1771 | 0 | default: |
1772 | 0 | crv = CKR_OPERATION_NOT_INITIALIZED; |
1773 | 0 | break; |
1774 | 0 | } |
1775 | 0 | PK11_ExitContextMonitor(context); |
1776 | |
|
1777 | 0 | context->init = PR_FALSE; /* allow Begin to start up again */ |
1778 | |
|
1779 | 0 | if (crv != CKR_OK) { |
1780 | 0 | PORT_SetError(PK11_MapError(crv)); |
1781 | 0 | return SECFailure; |
1782 | 0 | } |
1783 | 0 | *outLen = (unsigned int)len; |
1784 | 0 | return SECSuccess; |
1785 | 0 | } |
1786 | | |
1787 | | PRBool |
1788 | | PK11_ContextGetFIPSStatus(PK11Context *context) |
1789 | 0 | { |
1790 | 0 | if (context->slot == NULL) { |
1791 | 0 | return PR_FALSE; |
1792 | 0 | } |
1793 | 0 | return pk11slot_GetFIPSStatus(context->slot, context->session, |
1794 | 0 | CK_INVALID_HANDLE, context->init ? CKT_NSS_SESSION_CHECK : CKT_NSS_SESSION_LAST_CHECK); |
1795 | 0 | } |