Bug 470455 - test_database_sync_embed_visits.js leaks, r=sdwilsh
[wine-gecko.git] / security / nss / lib / ckfw / mechanism.c
blobad426642788ed6e024350239a609b28adca559a8
1 /* ***** BEGIN LICENSE BLOCK *****
2 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
4 * The contents of this file are subject to the Mozilla Public License Version
5 * 1.1 (the "License"); you may not use this file except in compliance with
6 * the License. You may obtain a copy of the License at
7 * http://www.mozilla.org/MPL/
9 * Software distributed under the License is distributed on an "AS IS" basis,
10 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
11 * for the specific language governing rights and limitations under the
12 * License.
14 * The Original Code is the Netscape security libraries.
16 * The Initial Developer of the Original Code is
17 * Netscape Communications Corporation.
18 * Portions created by the Initial Developer are Copyright (C) 1994-2000
19 * the Initial Developer. All Rights Reserved.
21 * Contributor(s):
23 * Alternatively, the contents of this file may be used under the terms of
24 * either the GNU General Public License Version 2 or later (the "GPL"), or
25 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
26 * in which case the provisions of the GPL or the LGPL are applicable instead
27 * of those above. If you wish to allow use of your version of this file only
28 * under the terms of either the GPL or the LGPL, and not to allow others to
29 * use your version of this file under the terms of the MPL, indicate your
30 * decision by deleting the provisions above and replace them with the notice
31 * and other provisions required by the GPL or the LGPL. If you do not delete
32 * the provisions above, a recipient may use your version of this file under
33 * the terms of any one of the MPL, the GPL or the LGPL.
35 * ***** END LICENSE BLOCK ***** */
37 #ifdef DEBUG
38 static const char CVS_ID[] = "@(#) $RCSfile: mechanism.c,v $ $Revision: 1.5 $ $Date: 2007/12/12 00:50:58 $";
39 #endif /* DEBUG */
42 * mechanism.c
44 * This file implements the NSSCKFWMechanism type and methods.
47 #ifndef CK_T
48 #include "ck.h"
49 #endif /* CK_T */
52 * NSSCKFWMechanism
54 * -- create/destroy --
55 * nssCKFWMechanism_Create
56 * nssCKFWMechanism_Destroy
58 * -- implement public accessors --
59 * nssCKFWMechanism_GetMDMechanism
60 * nssCKFWMechanism_GetParameter
62 * -- private accessors --
64 * -- module fronts --
65 * nssCKFWMechanism_GetMinKeySize
66 * nssCKFWMechanism_GetMaxKeySize
67 * nssCKFWMechanism_GetInHardware
68 * nssCKFWMechanism_GetCanEncrypt
69 * nssCKFWMechanism_GetCanDecrypt
70 * nssCKFWMechanism_GetCanDigest
71 * nssCKFWMechanism_GetCanSign
72 * nssCKFWMechanism_GetCanSignRecover
73 * nssCKFWMechanism_GetCanVerify
74 * nssCKFWMechanism_GetCanGenerate
75 * nssCKFWMechanism_GetCanGenerateKeyPair
76 * nssCKFWMechanism_GetCanUnwrap
77 * nssCKFWMechanism_GetCanWrap
78 * nssCKFWMechanism_GetCanDerive
79 * nssCKFWMechanism_EncryptInit
80 * nssCKFWMechanism_DecryptInit
81 * nssCKFWMechanism_DigestInit
82 * nssCKFWMechanism_SignInit
83 * nssCKFWMechanism_VerifyInit
84 * nssCKFWMechanism_SignRecoverInit
85 * nssCKFWMechanism_VerifyRecoverInit
86 * nssCKFWMechanism_GenerateKey
87 * nssCKFWMechanism_GenerateKeyPair
88 * nssCKFWMechanism_GetWrapKeyLength
89 * nssCKFWMechanism_WrapKey
90 * nssCKFWMechanism_UnwrapKey
91 * nssCKFWMechanism_DeriveKey
95 struct NSSCKFWMechanismStr {
96 NSSCKMDMechanism *mdMechanism;
97 NSSCKMDToken *mdToken;
98 NSSCKFWToken *fwToken;
99 NSSCKMDInstance *mdInstance;
100 NSSCKFWInstance *fwInstance;
104 * nssCKFWMechanism_Create
107 NSS_IMPLEMENT NSSCKFWMechanism *
108 nssCKFWMechanism_Create
110 NSSCKMDMechanism *mdMechanism,
111 NSSCKMDToken *mdToken,
112 NSSCKFWToken *fwToken,
113 NSSCKMDInstance *mdInstance,
114 NSSCKFWInstance *fwInstance
117 NSSCKFWMechanism *fwMechanism;
120 fwMechanism = nss_ZNEW(NULL, NSSCKFWMechanism);
121 if ((NSSCKFWMechanism *)NULL == fwMechanism) {
122 return (NSSCKFWMechanism *)NULL;
124 fwMechanism->mdMechanism = mdMechanism;
125 fwMechanism->mdToken = mdToken;
126 fwMechanism->fwToken = fwToken;
127 fwMechanism->mdInstance = mdInstance;
128 fwMechanism->fwInstance = fwInstance;
129 return fwMechanism;
133 * nssCKFWMechanism_Destroy
136 NSS_IMPLEMENT void
137 nssCKFWMechanism_Destroy
139 NSSCKFWMechanism *fwMechanism
142 /* destroy any fw resources held by nssCKFWMechanism (currently none) */
144 if ((void *)NULL == (void *)fwMechanism->mdMechanism->Destroy) {
145 /* destroys it's parent as well */
146 fwMechanism->mdMechanism->Destroy(
147 fwMechanism->mdMechanism,
148 fwMechanism,
149 fwMechanism->mdInstance,
150 fwMechanism->fwInstance);
152 /* if the Destroy function wasn't supplied, then the mechanism is 'static',
153 * and there is nothing to destroy */
154 return;
158 * nssCKFWMechanism_GetMDMechanism
161 NSS_IMPLEMENT NSSCKMDMechanism *
162 nssCKFWMechanism_GetMDMechanism
164 NSSCKFWMechanism *fwMechanism
167 return fwMechanism->mdMechanism;
171 * nssCKFWMechanism_GetMinKeySize
174 NSS_IMPLEMENT CK_ULONG
175 nssCKFWMechanism_GetMinKeySize
177 NSSCKFWMechanism *fwMechanism,
178 CK_RV *pError
181 if ( (void *)NULL == (void *)fwMechanism->mdMechanism->GetMinKeySize) {
182 return 0;
185 return fwMechanism->mdMechanism->GetMinKeySize(fwMechanism->mdMechanism,
186 fwMechanism, fwMechanism->mdToken, fwMechanism->fwToken,
187 fwMechanism->mdInstance, fwMechanism->fwInstance, pError);
191 * nssCKFWMechanism_GetMaxKeySize
194 NSS_IMPLEMENT CK_ULONG
195 nssCKFWMechanism_GetMaxKeySize
197 NSSCKFWMechanism *fwMechanism,
198 CK_RV *pError
201 if ( (void *)NULL == (void *)fwMechanism->mdMechanism->GetMaxKeySize) {
202 return 0;
205 return fwMechanism->mdMechanism->GetMaxKeySize(fwMechanism->mdMechanism,
206 fwMechanism, fwMechanism->mdToken, fwMechanism->fwToken,
207 fwMechanism->mdInstance, fwMechanism->fwInstance, pError);
211 * nssCKFWMechanism_GetInHardware
214 NSS_IMPLEMENT CK_BBOOL
215 nssCKFWMechanism_GetInHardware
217 NSSCKFWMechanism *fwMechanism,
218 CK_RV *pError
221 if ( (void *)NULL == (void *)fwMechanism->mdMechanism->GetInHardware) {
222 return CK_FALSE;
225 return fwMechanism->mdMechanism->GetInHardware(fwMechanism->mdMechanism,
226 fwMechanism, fwMechanism->mdToken, fwMechanism->fwToken,
227 fwMechanism->mdInstance, fwMechanism->fwInstance, pError);
232 * the following are determined automatically by which of the cryptographic
233 * functions are defined for this mechanism.
236 * nssCKFWMechanism_GetCanEncrypt
239 NSS_EXTERN CK_BBOOL
240 nssCKFWMechanism_GetCanEncrypt
242 NSSCKFWMechanism *fwMechanism,
243 CK_RV *pError
246 if ( (void *)NULL == (void *)fwMechanism->mdMechanism->EncryptInit) {
247 return CK_FALSE;
249 return CK_TRUE;
253 * nssCKFWMechanism_GetCanDecrypt
256 NSS_EXTERN CK_BBOOL
257 nssCKFWMechanism_GetCanDecrypt
259 NSSCKFWMechanism *fwMechanism,
260 CK_RV *pError
263 if ( (void *)NULL == (void *)fwMechanism->mdMechanism->DecryptInit) {
264 return CK_FALSE;
266 return CK_TRUE;
270 * nssCKFWMechanism_GetCanDigest
273 NSS_EXTERN CK_BBOOL
274 nssCKFWMechanism_GetCanDigest
276 NSSCKFWMechanism *fwMechanism,
277 CK_RV *pError
280 if ( (void *)NULL == (void *)fwMechanism->mdMechanism->DigestInit) {
281 return CK_FALSE;
283 return CK_TRUE;
287 * nssCKFWMechanism_GetCanSign
290 NSS_EXTERN CK_BBOOL
291 nssCKFWMechanism_GetCanSign
293 NSSCKFWMechanism *fwMechanism,
294 CK_RV *pError
297 if ( (void *)NULL == (void *)fwMechanism->mdMechanism->SignInit) {
298 return CK_FALSE;
300 return CK_TRUE;
304 * nssCKFWMechanism_GetCanSignRecover
307 NSS_EXTERN CK_BBOOL
308 nssCKFWMechanism_GetCanSignRecover
310 NSSCKFWMechanism *fwMechanism,
311 CK_RV *pError
314 if ( (void *)NULL == (void *)fwMechanism->mdMechanism->SignRecoverInit) {
315 return CK_FALSE;
317 return CK_TRUE;
321 * nssCKFWMechanism_GetCanVerify
324 NSS_EXTERN CK_BBOOL
325 nssCKFWMechanism_GetCanVerify
327 NSSCKFWMechanism *fwMechanism,
328 CK_RV *pError
331 if ( (void *)NULL == (void *)fwMechanism->mdMechanism->VerifyInit) {
332 return CK_FALSE;
334 return CK_TRUE;
338 * nssCKFWMechanism_GetCanVerifyRecover
341 NSS_EXTERN CK_BBOOL
342 nssCKFWMechanism_GetCanVerifyRecover
344 NSSCKFWMechanism *fwMechanism,
345 CK_RV *pError
348 if ( (void *)NULL == (void *)fwMechanism->mdMechanism->VerifyRecoverInit) {
349 return CK_FALSE;
351 return CK_TRUE;
355 * nssCKFWMechanism_GetCanGenerate
358 NSS_EXTERN CK_BBOOL
359 nssCKFWMechanism_GetCanGenerate
361 NSSCKFWMechanism *fwMechanism,
362 CK_RV *pError
365 if ( (void *)NULL == (void *)fwMechanism->mdMechanism->GenerateKey) {
366 return CK_FALSE;
368 return CK_TRUE;
372 * nssCKFWMechanism_GetCanGenerateKeyPair
375 NSS_EXTERN CK_BBOOL
376 nssCKFWMechanism_GetCanGenerateKeyPair
378 NSSCKFWMechanism *fwMechanism,
379 CK_RV *pError
382 if ( (void *)NULL == (void *)fwMechanism->mdMechanism->GenerateKeyPair) {
383 return CK_FALSE;
385 return CK_TRUE;
389 * nssCKFWMechanism_GetCanUnwrap
392 NSS_EXTERN CK_BBOOL
393 nssCKFWMechanism_GetCanUnwrap
395 NSSCKFWMechanism *fwMechanism,
396 CK_RV *pError
399 if ( (void *)NULL == (void *)fwMechanism->mdMechanism->UnwrapKey) {
400 return CK_FALSE;
402 return CK_TRUE;
406 * nssCKFWMechanism_GetCanWrap
409 NSS_EXTERN CK_BBOOL
410 nssCKFWMechanism_GetCanWrap
412 NSSCKFWMechanism *fwMechanism,
413 CK_RV *pError
416 if ( (void *)NULL == (void *)fwMechanism->mdMechanism->WrapKey) {
417 return CK_FALSE;
419 return CK_TRUE;
423 * nssCKFWMechanism_GetCanDerive
426 NSS_EXTERN CK_BBOOL
427 nssCKFWMechanism_GetCanDerive
429 NSSCKFWMechanism *fwMechanism,
430 CK_RV *pError
433 if ( (void *)NULL == (void *)fwMechanism->mdMechanism->DeriveKey) {
434 return CK_FALSE;
436 return CK_TRUE;
440 * These are the actual crypto operations
444 * nssCKFWMechanism_EncryptInit
445 * Start an encryption session.
447 NSS_EXTERN CK_RV
448 nssCKFWMechanism_EncryptInit
450 NSSCKFWMechanism *fwMechanism,
451 CK_MECHANISM *pMechanism,
452 NSSCKFWSession *fwSession,
453 NSSCKFWObject *fwObject
456 NSSCKFWCryptoOperation *fwOperation;
457 NSSCKMDCryptoOperation *mdOperation;
458 NSSCKMDSession *mdSession;
459 NSSCKMDObject *mdObject;
460 CK_RV error = CKR_OK;
463 fwOperation = nssCKFWSession_GetCurrentCryptoOperation(fwSession,
464 NSSCKFWCryptoOperationState_EncryptDecrypt);
465 if ((NSSCKFWCryptoOperation *)NULL != fwOperation) {
466 return CKR_OPERATION_ACTIVE;
469 if ( (void *)NULL == (void *)fwMechanism->mdMechanism->EncryptInit) {
470 return CKR_FUNCTION_FAILED;
473 mdSession = nssCKFWSession_GetMDSession(fwSession);
474 mdObject = nssCKFWObject_GetMDObject(fwObject);
475 mdOperation = fwMechanism->mdMechanism->EncryptInit(
476 fwMechanism->mdMechanism,
477 fwMechanism,
478 pMechanism,
479 mdSession,
480 fwSession,
481 fwMechanism->mdToken,
482 fwMechanism->fwToken,
483 fwMechanism->mdInstance,
484 fwMechanism->fwInstance,
485 mdObject,
486 fwObject,
487 &error
489 if ((NSSCKMDCryptoOperation *)NULL == mdOperation) {
490 goto loser;
493 fwOperation = nssCKFWCryptoOperation_Create(mdOperation,
494 mdSession, fwSession, fwMechanism->mdToken, fwMechanism->fwToken,
495 fwMechanism->mdInstance, fwMechanism->fwInstance,
496 NSSCKFWCryptoOperationType_Encrypt, &error);
497 if ((NSSCKFWCryptoOperation *)NULL != fwOperation) {
498 nssCKFWSession_SetCurrentCryptoOperation(fwSession, fwOperation,
499 NSSCKFWCryptoOperationState_EncryptDecrypt);
502 loser:
503 return error;
507 * nssCKFWMechanism_DecryptInit
508 * Start an encryption session.
510 NSS_EXTERN CK_RV
511 nssCKFWMechanism_DecryptInit
513 NSSCKFWMechanism *fwMechanism,
514 CK_MECHANISM *pMechanism,
515 NSSCKFWSession *fwSession,
516 NSSCKFWObject *fwObject
519 NSSCKFWCryptoOperation *fwOperation;
520 NSSCKMDCryptoOperation *mdOperation;
521 NSSCKMDSession *mdSession;
522 NSSCKMDObject *mdObject;
523 CK_RV error = CKR_OK;
526 fwOperation = nssCKFWSession_GetCurrentCryptoOperation(fwSession,
527 NSSCKFWCryptoOperationState_EncryptDecrypt);
528 if ((NSSCKFWCryptoOperation *)NULL != fwOperation) {
529 return CKR_OPERATION_ACTIVE;
532 if ( (void *)NULL == (void *)fwMechanism->mdMechanism->DecryptInit) {
533 return CKR_FUNCTION_FAILED;
536 mdSession = nssCKFWSession_GetMDSession(fwSession);
537 mdObject = nssCKFWObject_GetMDObject(fwObject);
538 mdOperation = fwMechanism->mdMechanism->DecryptInit(
539 fwMechanism->mdMechanism,
540 fwMechanism,
541 pMechanism,
542 mdSession,
543 fwSession,
544 fwMechanism->mdToken,
545 fwMechanism->fwToken,
546 fwMechanism->mdInstance,
547 fwMechanism->fwInstance,
548 mdObject,
549 fwObject,
550 &error
552 if ((NSSCKMDCryptoOperation *)NULL == mdOperation) {
553 goto loser;
556 fwOperation = nssCKFWCryptoOperation_Create(mdOperation,
557 mdSession, fwSession, fwMechanism->mdToken, fwMechanism->fwToken,
558 fwMechanism->mdInstance, fwMechanism->fwInstance,
559 NSSCKFWCryptoOperationType_Decrypt, &error);
560 if ((NSSCKFWCryptoOperation *)NULL != fwOperation) {
561 nssCKFWSession_SetCurrentCryptoOperation(fwSession, fwOperation,
562 NSSCKFWCryptoOperationState_EncryptDecrypt);
565 loser:
566 return error;
570 * nssCKFWMechanism_DigestInit
571 * Start an encryption session.
573 NSS_EXTERN CK_RV
574 nssCKFWMechanism_DigestInit
576 NSSCKFWMechanism *fwMechanism,
577 CK_MECHANISM *pMechanism,
578 NSSCKFWSession *fwSession
581 NSSCKFWCryptoOperation *fwOperation;
582 NSSCKMDCryptoOperation *mdOperation;
583 NSSCKMDSession *mdSession;
584 CK_RV error = CKR_OK;
587 fwOperation = nssCKFWSession_GetCurrentCryptoOperation(fwSession,
588 NSSCKFWCryptoOperationState_Digest);
589 if ((NSSCKFWCryptoOperation *)NULL != fwOperation) {
590 return CKR_OPERATION_ACTIVE;
593 if ( (void *)NULL == (void *)fwMechanism->mdMechanism->DigestInit) {
594 return CKR_FUNCTION_FAILED;
597 mdSession = nssCKFWSession_GetMDSession(fwSession);
598 mdOperation = fwMechanism->mdMechanism->DigestInit(
599 fwMechanism->mdMechanism,
600 fwMechanism,
601 pMechanism,
602 mdSession,
603 fwSession,
604 fwMechanism->mdToken,
605 fwMechanism->fwToken,
606 fwMechanism->mdInstance,
607 fwMechanism->fwInstance,
608 &error
610 if ((NSSCKMDCryptoOperation *)NULL == mdOperation) {
611 goto loser;
614 fwOperation = nssCKFWCryptoOperation_Create(mdOperation,
615 mdSession, fwSession, fwMechanism->mdToken, fwMechanism->fwToken,
616 fwMechanism->mdInstance, fwMechanism->fwInstance,
617 NSSCKFWCryptoOperationType_Digest, &error);
618 if ((NSSCKFWCryptoOperation *)NULL != fwOperation) {
619 nssCKFWSession_SetCurrentCryptoOperation(fwSession, fwOperation,
620 NSSCKFWCryptoOperationState_Digest);
623 loser:
624 return error;
628 * nssCKFWMechanism_SignInit
629 * Start an encryption session.
631 NSS_EXTERN CK_RV
632 nssCKFWMechanism_SignInit
634 NSSCKFWMechanism *fwMechanism,
635 CK_MECHANISM *pMechanism,
636 NSSCKFWSession *fwSession,
637 NSSCKFWObject *fwObject
640 NSSCKFWCryptoOperation *fwOperation;
641 NSSCKMDCryptoOperation *mdOperation;
642 NSSCKMDSession *mdSession;
643 NSSCKMDObject *mdObject;
644 CK_RV error = CKR_OK;
647 fwOperation = nssCKFWSession_GetCurrentCryptoOperation(fwSession,
648 NSSCKFWCryptoOperationState_SignVerify);
649 if ((NSSCKFWCryptoOperation *)NULL != fwOperation) {
650 return CKR_OPERATION_ACTIVE;
653 if ( (void *)NULL == (void *)fwMechanism->mdMechanism->SignInit) {
654 return CKR_FUNCTION_FAILED;
657 mdSession = nssCKFWSession_GetMDSession(fwSession);
658 mdObject = nssCKFWObject_GetMDObject(fwObject);
659 mdOperation = fwMechanism->mdMechanism->SignInit(
660 fwMechanism->mdMechanism,
661 fwMechanism,
662 pMechanism,
663 mdSession,
664 fwSession,
665 fwMechanism->mdToken,
666 fwMechanism->fwToken,
667 fwMechanism->mdInstance,
668 fwMechanism->fwInstance,
669 mdObject,
670 fwObject,
671 &error
673 if ((NSSCKMDCryptoOperation *)NULL == mdOperation) {
674 goto loser;
677 fwOperation = nssCKFWCryptoOperation_Create(mdOperation,
678 mdSession, fwSession, fwMechanism->mdToken, fwMechanism->fwToken,
679 fwMechanism->mdInstance, fwMechanism->fwInstance,
680 NSSCKFWCryptoOperationType_Sign, &error);
681 if ((NSSCKFWCryptoOperation *)NULL != fwOperation) {
682 nssCKFWSession_SetCurrentCryptoOperation(fwSession, fwOperation,
683 NSSCKFWCryptoOperationState_SignVerify);
686 loser:
687 return error;
691 * nssCKFWMechanism_VerifyInit
692 * Start an encryption session.
694 NSS_EXTERN CK_RV
695 nssCKFWMechanism_VerifyInit
697 NSSCKFWMechanism *fwMechanism,
698 CK_MECHANISM *pMechanism,
699 NSSCKFWSession *fwSession,
700 NSSCKFWObject *fwObject
703 NSSCKFWCryptoOperation *fwOperation;
704 NSSCKMDCryptoOperation *mdOperation;
705 NSSCKMDSession *mdSession;
706 NSSCKMDObject *mdObject;
707 CK_RV error = CKR_OK;
710 fwOperation = nssCKFWSession_GetCurrentCryptoOperation(fwSession,
711 NSSCKFWCryptoOperationState_SignVerify);
712 if ((NSSCKFWCryptoOperation *)NULL != fwOperation) {
713 return CKR_OPERATION_ACTIVE;
716 if ( (void *)NULL == (void *)fwMechanism->mdMechanism->VerifyInit) {
717 return CKR_FUNCTION_FAILED;
720 mdSession = nssCKFWSession_GetMDSession(fwSession);
721 mdObject = nssCKFWObject_GetMDObject(fwObject);
722 mdOperation = fwMechanism->mdMechanism->VerifyInit(
723 fwMechanism->mdMechanism,
724 fwMechanism,
725 pMechanism,
726 mdSession,
727 fwSession,
728 fwMechanism->mdToken,
729 fwMechanism->fwToken,
730 fwMechanism->mdInstance,
731 fwMechanism->fwInstance,
732 mdObject,
733 fwObject,
734 &error
736 if ((NSSCKMDCryptoOperation *)NULL == mdOperation) {
737 goto loser;
740 fwOperation = nssCKFWCryptoOperation_Create(mdOperation,
741 mdSession, fwSession, fwMechanism->mdToken, fwMechanism->fwToken,
742 fwMechanism->mdInstance, fwMechanism->fwInstance,
743 NSSCKFWCryptoOperationType_Verify, &error);
744 if ((NSSCKFWCryptoOperation *)NULL != fwOperation) {
745 nssCKFWSession_SetCurrentCryptoOperation(fwSession, fwOperation,
746 NSSCKFWCryptoOperationState_SignVerify);
749 loser:
750 return error;
754 * nssCKFWMechanism_SignRecoverInit
755 * Start an encryption session.
757 NSS_EXTERN CK_RV
758 nssCKFWMechanism_SignRecoverInit
760 NSSCKFWMechanism *fwMechanism,
761 CK_MECHANISM *pMechanism,
762 NSSCKFWSession *fwSession,
763 NSSCKFWObject *fwObject
766 NSSCKFWCryptoOperation *fwOperation;
767 NSSCKMDCryptoOperation *mdOperation;
768 NSSCKMDSession *mdSession;
769 NSSCKMDObject *mdObject;
770 CK_RV error = CKR_OK;
773 fwOperation = nssCKFWSession_GetCurrentCryptoOperation(fwSession,
774 NSSCKFWCryptoOperationState_SignVerify);
775 if ((NSSCKFWCryptoOperation *)NULL != fwOperation) {
776 return CKR_OPERATION_ACTIVE;
779 if ( (void *)NULL == (void *)fwMechanism->mdMechanism->SignRecoverInit) {
780 return CKR_FUNCTION_FAILED;
783 mdSession = nssCKFWSession_GetMDSession(fwSession);
784 mdObject = nssCKFWObject_GetMDObject(fwObject);
785 mdOperation = fwMechanism->mdMechanism->SignRecoverInit(
786 fwMechanism->mdMechanism,
787 fwMechanism,
788 pMechanism,
789 mdSession,
790 fwSession,
791 fwMechanism->mdToken,
792 fwMechanism->fwToken,
793 fwMechanism->mdInstance,
794 fwMechanism->fwInstance,
795 mdObject,
796 fwObject,
797 &error
799 if ((NSSCKMDCryptoOperation *)NULL == mdOperation) {
800 goto loser;
803 fwOperation = nssCKFWCryptoOperation_Create(mdOperation,
804 mdSession, fwSession, fwMechanism->mdToken, fwMechanism->fwToken,
805 fwMechanism->mdInstance, fwMechanism->fwInstance,
806 NSSCKFWCryptoOperationType_SignRecover, &error);
807 if ((NSSCKFWCryptoOperation *)NULL != fwOperation) {
808 nssCKFWSession_SetCurrentCryptoOperation(fwSession, fwOperation,
809 NSSCKFWCryptoOperationState_SignVerify);
812 loser:
813 return error;
817 * nssCKFWMechanism_VerifyRecoverInit
818 * Start an encryption session.
820 NSS_EXTERN CK_RV
821 nssCKFWMechanism_VerifyRecoverInit
823 NSSCKFWMechanism *fwMechanism,
824 CK_MECHANISM *pMechanism,
825 NSSCKFWSession *fwSession,
826 NSSCKFWObject *fwObject
829 NSSCKFWCryptoOperation *fwOperation;
830 NSSCKMDCryptoOperation *mdOperation;
831 NSSCKMDSession *mdSession;
832 NSSCKMDObject *mdObject;
833 CK_RV error = CKR_OK;
836 fwOperation = nssCKFWSession_GetCurrentCryptoOperation(fwSession,
837 NSSCKFWCryptoOperationState_SignVerify);
838 if ((NSSCKFWCryptoOperation *)NULL != fwOperation) {
839 return CKR_OPERATION_ACTIVE;
842 if ( (void *)NULL == (void *)fwMechanism->mdMechanism->VerifyRecoverInit) {
843 return CKR_FUNCTION_FAILED;
846 mdSession = nssCKFWSession_GetMDSession(fwSession);
847 mdObject = nssCKFWObject_GetMDObject(fwObject);
848 mdOperation = fwMechanism->mdMechanism->VerifyRecoverInit(
849 fwMechanism->mdMechanism,
850 fwMechanism,
851 pMechanism,
852 mdSession,
853 fwSession,
854 fwMechanism->mdToken,
855 fwMechanism->fwToken,
856 fwMechanism->mdInstance,
857 fwMechanism->fwInstance,
858 mdObject,
859 fwObject,
860 &error
862 if ((NSSCKMDCryptoOperation *)NULL == mdOperation) {
863 goto loser;
866 fwOperation = nssCKFWCryptoOperation_Create(mdOperation,
867 mdSession, fwSession, fwMechanism->mdToken, fwMechanism->fwToken,
868 fwMechanism->mdInstance, fwMechanism->fwInstance,
869 NSSCKFWCryptoOperationType_VerifyRecover, &error);
870 if ((NSSCKFWCryptoOperation *)NULL != fwOperation) {
871 nssCKFWSession_SetCurrentCryptoOperation(fwSession, fwOperation,
872 NSSCKFWCryptoOperationState_SignVerify);
875 loser:
876 return error;
880 * nssCKFWMechanism_GenerateKey
882 NSS_EXTERN NSSCKFWObject *
883 nssCKFWMechanism_GenerateKey
885 NSSCKFWMechanism *fwMechanism,
886 CK_MECHANISM_PTR pMechanism,
887 NSSCKFWSession *fwSession,
888 CK_ATTRIBUTE_PTR pTemplate,
889 CK_ULONG ulAttributeCount,
890 CK_RV *pError
893 NSSCKMDSession *mdSession;
894 NSSCKMDObject *mdObject;
895 NSSCKFWObject *fwObject = NULL;
896 NSSArena *arena;
898 if ( (void *)NULL == (void *)fwMechanism->mdMechanism->GenerateKey) {
899 *pError = CKR_FUNCTION_FAILED;
900 return (NSSCKFWObject *)NULL;
903 arena = nssCKFWToken_GetArena(fwMechanism->fwToken, pError);
904 if ((NSSArena *)NULL == arena) {
905 if (CKR_OK == *pError) {
906 *pError = CKR_GENERAL_ERROR;
908 return (NSSCKFWObject *)NULL;
911 mdSession = nssCKFWSession_GetMDSession(fwSession);
912 mdObject = fwMechanism->mdMechanism->GenerateKey(
913 fwMechanism->mdMechanism,
914 fwMechanism,
915 pMechanism,
916 mdSession,
917 fwSession,
918 fwMechanism->mdToken,
919 fwMechanism->fwToken,
920 fwMechanism->mdInstance,
921 fwMechanism->fwInstance,
922 pTemplate,
923 ulAttributeCount,
924 pError);
926 if ((NSSCKMDObject *)NULL == mdObject) {
927 return (NSSCKFWObject *)NULL;
930 fwObject = nssCKFWObject_Create(arena, mdObject,
931 fwSession, fwMechanism->fwToken, fwMechanism->fwInstance, pError);
933 return fwObject;
937 * nssCKFWMechanism_GenerateKeyPair
939 NSS_EXTERN CK_RV
940 nssCKFWMechanism_GenerateKeyPair
942 NSSCKFWMechanism *fwMechanism,
943 CK_MECHANISM_PTR pMechanism,
944 NSSCKFWSession *fwSession,
945 CK_ATTRIBUTE_PTR pPublicKeyTemplate,
946 CK_ULONG ulPublicKeyAttributeCount,
947 CK_ATTRIBUTE_PTR pPrivateKeyTemplate,
948 CK_ULONG ulPrivateKeyAttributeCount,
949 NSSCKFWObject **fwPublicKeyObject,
950 NSSCKFWObject **fwPrivateKeyObject
953 NSSCKMDSession *mdSession;
954 NSSCKMDObject *mdPublicKeyObject;
955 NSSCKMDObject *mdPrivateKeyObject;
956 NSSArena *arena;
957 CK_RV error = CKR_OK;
959 if ( (void *)NULL == (void *)fwMechanism->mdMechanism->GenerateKey) {
960 return CKR_FUNCTION_FAILED;
963 arena = nssCKFWToken_GetArena(fwMechanism->fwToken, &error);
964 if ((NSSArena *)NULL == arena) {
965 if (CKR_OK == error) {
966 error = CKR_GENERAL_ERROR;
968 return error;
971 mdSession = nssCKFWSession_GetMDSession(fwSession);
972 error = fwMechanism->mdMechanism->GenerateKeyPair(
973 fwMechanism->mdMechanism,
974 fwMechanism,
975 pMechanism,
976 mdSession,
977 fwSession,
978 fwMechanism->mdToken,
979 fwMechanism->fwToken,
980 fwMechanism->mdInstance,
981 fwMechanism->fwInstance,
982 pPublicKeyTemplate,
983 ulPublicKeyAttributeCount,
984 pPrivateKeyTemplate,
985 ulPrivateKeyAttributeCount,
986 &mdPublicKeyObject,
987 &mdPrivateKeyObject);
989 if (CKR_OK != error) {
990 return error;
993 *fwPublicKeyObject = nssCKFWObject_Create(arena, mdPublicKeyObject,
994 fwSession, fwMechanism->fwToken, fwMechanism->fwInstance, &error);
995 if ((NSSCKFWObject *)NULL == *fwPublicKeyObject) {
996 return error;
998 *fwPrivateKeyObject = nssCKFWObject_Create(arena, mdPrivateKeyObject,
999 fwSession, fwMechanism->fwToken, fwMechanism->fwInstance, &error);
1001 return error;
1005 * nssCKFWMechanism_GetWrapKeyLength
1007 NSS_EXTERN CK_ULONG
1008 nssCKFWMechanism_GetWrapKeyLength
1010 NSSCKFWMechanism *fwMechanism,
1011 CK_MECHANISM_PTR pMechanism,
1012 NSSCKFWSession *fwSession,
1013 NSSCKFWObject *fwWrappingKeyObject,
1014 NSSCKFWObject *fwKeyObject,
1015 CK_RV *pError
1018 NSSCKMDSession *mdSession;
1019 NSSCKMDObject *mdWrappingKeyObject;
1020 NSSCKMDObject *mdKeyObject;
1022 if ( (void *)NULL == (void *)fwMechanism->mdMechanism->WrapKey) {
1023 *pError = CKR_FUNCTION_FAILED;
1024 return (CK_ULONG) 0;
1027 mdSession = nssCKFWSession_GetMDSession(fwSession);
1028 mdWrappingKeyObject = nssCKFWObject_GetMDObject(fwWrappingKeyObject);
1029 mdKeyObject = nssCKFWObject_GetMDObject(fwKeyObject);
1030 return fwMechanism->mdMechanism->GetWrapKeyLength(
1031 fwMechanism->mdMechanism,
1032 fwMechanism,
1033 pMechanism,
1034 mdSession,
1035 fwSession,
1036 fwMechanism->mdToken,
1037 fwMechanism->fwToken,
1038 fwMechanism->mdInstance,
1039 fwMechanism->fwInstance,
1040 mdWrappingKeyObject,
1041 fwWrappingKeyObject,
1042 mdKeyObject,
1043 fwKeyObject,
1044 pError);
1048 * nssCKFWMechanism_WrapKey
1050 NSS_EXTERN CK_RV
1051 nssCKFWMechanism_WrapKey
1053 NSSCKFWMechanism *fwMechanism,
1054 CK_MECHANISM_PTR pMechanism,
1055 NSSCKFWSession *fwSession,
1056 NSSCKFWObject *fwWrappingKeyObject,
1057 NSSCKFWObject *fwKeyObject,
1058 NSSItem *wrappedKey
1061 NSSCKMDSession *mdSession;
1062 NSSCKMDObject *mdWrappingKeyObject;
1063 NSSCKMDObject *mdKeyObject;
1065 if ( (void *)NULL == (void *)fwMechanism->mdMechanism->WrapKey) {
1066 return CKR_FUNCTION_FAILED;
1069 mdSession = nssCKFWSession_GetMDSession(fwSession);
1070 mdWrappingKeyObject = nssCKFWObject_GetMDObject(fwWrappingKeyObject);
1071 mdKeyObject = nssCKFWObject_GetMDObject(fwKeyObject);
1072 return fwMechanism->mdMechanism->WrapKey(
1073 fwMechanism->mdMechanism,
1074 fwMechanism,
1075 pMechanism,
1076 mdSession,
1077 fwSession,
1078 fwMechanism->mdToken,
1079 fwMechanism->fwToken,
1080 fwMechanism->mdInstance,
1081 fwMechanism->fwInstance,
1082 mdWrappingKeyObject,
1083 fwWrappingKeyObject,
1084 mdKeyObject,
1085 fwKeyObject,
1086 wrappedKey);
1090 * nssCKFWMechanism_UnwrapKey
1092 NSS_EXTERN NSSCKFWObject *
1093 nssCKFWMechanism_UnwrapKey
1095 NSSCKFWMechanism *fwMechanism,
1096 CK_MECHANISM_PTR pMechanism,
1097 NSSCKFWSession *fwSession,
1098 NSSCKFWObject *fwWrappingKeyObject,
1099 NSSItem *wrappedKey,
1100 CK_ATTRIBUTE_PTR pTemplate,
1101 CK_ULONG ulAttributeCount,
1102 CK_RV *pError
1105 NSSCKMDSession *mdSession;
1106 NSSCKMDObject *mdObject;
1107 NSSCKMDObject *mdWrappingKeyObject;
1108 NSSCKFWObject *fwObject = NULL;
1109 NSSArena *arena;
1111 if ( (void *)NULL == (void *)fwMechanism->mdMechanism->UnwrapKey) {
1112 /* we could simulate UnwrapKey using Decrypt and Create object, but
1113 * 1) it's not clear that would work well, and 2) the low level token
1114 * may want to restrict unwrap key for a reason, so just fail it it
1115 * can't be done */
1116 *pError = CKR_FUNCTION_FAILED;
1117 return (NSSCKFWObject *)NULL;
1120 arena = nssCKFWToken_GetArena(fwMechanism->fwToken, pError);
1121 if ((NSSArena *)NULL == arena) {
1122 if (CKR_OK == *pError) {
1123 *pError = CKR_GENERAL_ERROR;
1125 return (NSSCKFWObject *)NULL;
1128 mdSession = nssCKFWSession_GetMDSession(fwSession);
1129 mdWrappingKeyObject = nssCKFWObject_GetMDObject(fwWrappingKeyObject);
1130 mdObject = fwMechanism->mdMechanism->UnwrapKey(
1131 fwMechanism->mdMechanism,
1132 fwMechanism,
1133 pMechanism,
1134 mdSession,
1135 fwSession,
1136 fwMechanism->mdToken,
1137 fwMechanism->fwToken,
1138 fwMechanism->mdInstance,
1139 fwMechanism->fwInstance,
1140 mdWrappingKeyObject,
1141 fwWrappingKeyObject,
1142 wrappedKey,
1143 pTemplate,
1144 ulAttributeCount,
1145 pError);
1147 if ((NSSCKMDObject *)NULL == mdObject) {
1148 return (NSSCKFWObject *)NULL;
1151 fwObject = nssCKFWObject_Create(arena, mdObject,
1152 fwSession, fwMechanism->fwToken, fwMechanism->fwInstance, pError);
1154 return fwObject;
1158 * nssCKFWMechanism_DeriveKey
1160 NSS_EXTERN NSSCKFWObject *
1161 nssCKFWMechanism_DeriveKey
1163 NSSCKFWMechanism *fwMechanism,
1164 CK_MECHANISM_PTR pMechanism,
1165 NSSCKFWSession *fwSession,
1166 NSSCKFWObject *fwBaseKeyObject,
1167 CK_ATTRIBUTE_PTR pTemplate,
1168 CK_ULONG ulAttributeCount,
1169 CK_RV *pError
1172 NSSCKMDSession *mdSession;
1173 NSSCKMDObject *mdObject;
1174 NSSCKMDObject *mdBaseKeyObject;
1175 NSSCKFWObject *fwObject = NULL;
1176 NSSArena *arena;
1178 if ( (void *)NULL == (void *)fwMechanism->mdMechanism->DeriveKey) {
1179 *pError = CKR_FUNCTION_FAILED;
1180 return (NSSCKFWObject *)NULL;
1183 arena = nssCKFWToken_GetArena(fwMechanism->fwToken, pError);
1184 if ((NSSArena *)NULL == arena) {
1185 if (CKR_OK == *pError) {
1186 *pError = CKR_GENERAL_ERROR;
1188 return (NSSCKFWObject *)NULL;
1191 mdSession = nssCKFWSession_GetMDSession(fwSession);
1192 mdBaseKeyObject = nssCKFWObject_GetMDObject(fwBaseKeyObject);
1193 mdObject = fwMechanism->mdMechanism->DeriveKey(
1194 fwMechanism->mdMechanism,
1195 fwMechanism,
1196 pMechanism,
1197 mdSession,
1198 fwSession,
1199 fwMechanism->mdToken,
1200 fwMechanism->fwToken,
1201 fwMechanism->mdInstance,
1202 fwMechanism->fwInstance,
1203 mdBaseKeyObject,
1204 fwBaseKeyObject,
1205 pTemplate,
1206 ulAttributeCount,
1207 pError);
1209 if ((NSSCKMDObject *)NULL == mdObject) {
1210 return (NSSCKFWObject *)NULL;
1213 fwObject = nssCKFWObject_Create(arena, mdObject,
1214 fwSession, fwMechanism->fwToken, fwMechanism->fwInstance, pError);
1216 return fwObject;