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
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.
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 ***** */
38 static const char CVS_ID
[] = "@(#) $RCSfile: mechanism.c,v $ $Revision: 1.5 $ $Date: 2007/12/12 00:50:58 $";
44 * This file implements the NSSCKFWMechanism type and methods.
54 * -- create/destroy --
55 * nssCKFWMechanism_Create
56 * nssCKFWMechanism_Destroy
58 * -- implement public accessors --
59 * nssCKFWMechanism_GetMDMechanism
60 * nssCKFWMechanism_GetParameter
62 * -- private accessors --
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
;
133 * nssCKFWMechanism_Destroy
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
,
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 */
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
,
181 if ( (void *)NULL
== (void *)fwMechanism
->mdMechanism
->GetMinKeySize
) {
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
,
201 if ( (void *)NULL
== (void *)fwMechanism
->mdMechanism
->GetMaxKeySize
) {
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
,
221 if ( (void *)NULL
== (void *)fwMechanism
->mdMechanism
->GetInHardware
) {
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
240 nssCKFWMechanism_GetCanEncrypt
242 NSSCKFWMechanism
*fwMechanism
,
246 if ( (void *)NULL
== (void *)fwMechanism
->mdMechanism
->EncryptInit
) {
253 * nssCKFWMechanism_GetCanDecrypt
257 nssCKFWMechanism_GetCanDecrypt
259 NSSCKFWMechanism
*fwMechanism
,
263 if ( (void *)NULL
== (void *)fwMechanism
->mdMechanism
->DecryptInit
) {
270 * nssCKFWMechanism_GetCanDigest
274 nssCKFWMechanism_GetCanDigest
276 NSSCKFWMechanism
*fwMechanism
,
280 if ( (void *)NULL
== (void *)fwMechanism
->mdMechanism
->DigestInit
) {
287 * nssCKFWMechanism_GetCanSign
291 nssCKFWMechanism_GetCanSign
293 NSSCKFWMechanism
*fwMechanism
,
297 if ( (void *)NULL
== (void *)fwMechanism
->mdMechanism
->SignInit
) {
304 * nssCKFWMechanism_GetCanSignRecover
308 nssCKFWMechanism_GetCanSignRecover
310 NSSCKFWMechanism
*fwMechanism
,
314 if ( (void *)NULL
== (void *)fwMechanism
->mdMechanism
->SignRecoverInit
) {
321 * nssCKFWMechanism_GetCanVerify
325 nssCKFWMechanism_GetCanVerify
327 NSSCKFWMechanism
*fwMechanism
,
331 if ( (void *)NULL
== (void *)fwMechanism
->mdMechanism
->VerifyInit
) {
338 * nssCKFWMechanism_GetCanVerifyRecover
342 nssCKFWMechanism_GetCanVerifyRecover
344 NSSCKFWMechanism
*fwMechanism
,
348 if ( (void *)NULL
== (void *)fwMechanism
->mdMechanism
->VerifyRecoverInit
) {
355 * nssCKFWMechanism_GetCanGenerate
359 nssCKFWMechanism_GetCanGenerate
361 NSSCKFWMechanism
*fwMechanism
,
365 if ( (void *)NULL
== (void *)fwMechanism
->mdMechanism
->GenerateKey
) {
372 * nssCKFWMechanism_GetCanGenerateKeyPair
376 nssCKFWMechanism_GetCanGenerateKeyPair
378 NSSCKFWMechanism
*fwMechanism
,
382 if ( (void *)NULL
== (void *)fwMechanism
->mdMechanism
->GenerateKeyPair
) {
389 * nssCKFWMechanism_GetCanUnwrap
393 nssCKFWMechanism_GetCanUnwrap
395 NSSCKFWMechanism
*fwMechanism
,
399 if ( (void *)NULL
== (void *)fwMechanism
->mdMechanism
->UnwrapKey
) {
406 * nssCKFWMechanism_GetCanWrap
410 nssCKFWMechanism_GetCanWrap
412 NSSCKFWMechanism
*fwMechanism
,
416 if ( (void *)NULL
== (void *)fwMechanism
->mdMechanism
->WrapKey
) {
423 * nssCKFWMechanism_GetCanDerive
427 nssCKFWMechanism_GetCanDerive
429 NSSCKFWMechanism
*fwMechanism
,
433 if ( (void *)NULL
== (void *)fwMechanism
->mdMechanism
->DeriveKey
) {
440 * These are the actual crypto operations
444 * nssCKFWMechanism_EncryptInit
445 * Start an encryption session.
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
,
481 fwMechanism
->mdToken
,
482 fwMechanism
->fwToken
,
483 fwMechanism
->mdInstance
,
484 fwMechanism
->fwInstance
,
489 if ((NSSCKMDCryptoOperation
*)NULL
== mdOperation
) {
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
);
507 * nssCKFWMechanism_DecryptInit
508 * Start an encryption session.
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
,
544 fwMechanism
->mdToken
,
545 fwMechanism
->fwToken
,
546 fwMechanism
->mdInstance
,
547 fwMechanism
->fwInstance
,
552 if ((NSSCKMDCryptoOperation
*)NULL
== mdOperation
) {
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
);
570 * nssCKFWMechanism_DigestInit
571 * Start an encryption session.
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
,
604 fwMechanism
->mdToken
,
605 fwMechanism
->fwToken
,
606 fwMechanism
->mdInstance
,
607 fwMechanism
->fwInstance
,
610 if ((NSSCKMDCryptoOperation
*)NULL
== mdOperation
) {
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
);
628 * nssCKFWMechanism_SignInit
629 * Start an encryption session.
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
,
665 fwMechanism
->mdToken
,
666 fwMechanism
->fwToken
,
667 fwMechanism
->mdInstance
,
668 fwMechanism
->fwInstance
,
673 if ((NSSCKMDCryptoOperation
*)NULL
== mdOperation
) {
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
);
691 * nssCKFWMechanism_VerifyInit
692 * Start an encryption session.
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
,
728 fwMechanism
->mdToken
,
729 fwMechanism
->fwToken
,
730 fwMechanism
->mdInstance
,
731 fwMechanism
->fwInstance
,
736 if ((NSSCKMDCryptoOperation
*)NULL
== mdOperation
) {
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
);
754 * nssCKFWMechanism_SignRecoverInit
755 * Start an encryption session.
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
,
791 fwMechanism
->mdToken
,
792 fwMechanism
->fwToken
,
793 fwMechanism
->mdInstance
,
794 fwMechanism
->fwInstance
,
799 if ((NSSCKMDCryptoOperation
*)NULL
== mdOperation
) {
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
);
817 * nssCKFWMechanism_VerifyRecoverInit
818 * Start an encryption session.
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
,
854 fwMechanism
->mdToken
,
855 fwMechanism
->fwToken
,
856 fwMechanism
->mdInstance
,
857 fwMechanism
->fwInstance
,
862 if ((NSSCKMDCryptoOperation
*)NULL
== mdOperation
) {
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
);
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
,
893 NSSCKMDSession
*mdSession
;
894 NSSCKMDObject
*mdObject
;
895 NSSCKFWObject
*fwObject
= NULL
;
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
,
918 fwMechanism
->mdToken
,
919 fwMechanism
->fwToken
,
920 fwMechanism
->mdInstance
,
921 fwMechanism
->fwInstance
,
926 if ((NSSCKMDObject
*)NULL
== mdObject
) {
927 return (NSSCKFWObject
*)NULL
;
930 fwObject
= nssCKFWObject_Create(arena
, mdObject
,
931 fwSession
, fwMechanism
->fwToken
, fwMechanism
->fwInstance
, pError
);
937 * nssCKFWMechanism_GenerateKeyPair
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
;
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
;
971 mdSession
= nssCKFWSession_GetMDSession(fwSession
);
972 error
= fwMechanism
->mdMechanism
->GenerateKeyPair(
973 fwMechanism
->mdMechanism
,
978 fwMechanism
->mdToken
,
979 fwMechanism
->fwToken
,
980 fwMechanism
->mdInstance
,
981 fwMechanism
->fwInstance
,
983 ulPublicKeyAttributeCount
,
985 ulPrivateKeyAttributeCount
,
987 &mdPrivateKeyObject
);
989 if (CKR_OK
!= error
) {
993 *fwPublicKeyObject
= nssCKFWObject_Create(arena
, mdPublicKeyObject
,
994 fwSession
, fwMechanism
->fwToken
, fwMechanism
->fwInstance
, &error
);
995 if ((NSSCKFWObject
*)NULL
== *fwPublicKeyObject
) {
998 *fwPrivateKeyObject
= nssCKFWObject_Create(arena
, mdPrivateKeyObject
,
999 fwSession
, fwMechanism
->fwToken
, fwMechanism
->fwInstance
, &error
);
1005 * nssCKFWMechanism_GetWrapKeyLength
1008 nssCKFWMechanism_GetWrapKeyLength
1010 NSSCKFWMechanism
*fwMechanism
,
1011 CK_MECHANISM_PTR pMechanism
,
1012 NSSCKFWSession
*fwSession
,
1013 NSSCKFWObject
*fwWrappingKeyObject
,
1014 NSSCKFWObject
*fwKeyObject
,
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
,
1036 fwMechanism
->mdToken
,
1037 fwMechanism
->fwToken
,
1038 fwMechanism
->mdInstance
,
1039 fwMechanism
->fwInstance
,
1040 mdWrappingKeyObject
,
1041 fwWrappingKeyObject
,
1048 * nssCKFWMechanism_WrapKey
1051 nssCKFWMechanism_WrapKey
1053 NSSCKFWMechanism
*fwMechanism
,
1054 CK_MECHANISM_PTR pMechanism
,
1055 NSSCKFWSession
*fwSession
,
1056 NSSCKFWObject
*fwWrappingKeyObject
,
1057 NSSCKFWObject
*fwKeyObject
,
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
,
1078 fwMechanism
->mdToken
,
1079 fwMechanism
->fwToken
,
1080 fwMechanism
->mdInstance
,
1081 fwMechanism
->fwInstance
,
1082 mdWrappingKeyObject
,
1083 fwWrappingKeyObject
,
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
,
1105 NSSCKMDSession
*mdSession
;
1106 NSSCKMDObject
*mdObject
;
1107 NSSCKMDObject
*mdWrappingKeyObject
;
1108 NSSCKFWObject
*fwObject
= NULL
;
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
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
,
1136 fwMechanism
->mdToken
,
1137 fwMechanism
->fwToken
,
1138 fwMechanism
->mdInstance
,
1139 fwMechanism
->fwInstance
,
1140 mdWrappingKeyObject
,
1141 fwWrappingKeyObject
,
1147 if ((NSSCKMDObject
*)NULL
== mdObject
) {
1148 return (NSSCKFWObject
*)NULL
;
1151 fwObject
= nssCKFWObject_Create(arena
, mdObject
,
1152 fwSession
, fwMechanism
->fwToken
, fwMechanism
->fwInstance
, pError
);
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
,
1172 NSSCKMDSession
*mdSession
;
1173 NSSCKMDObject
*mdObject
;
1174 NSSCKMDObject
*mdBaseKeyObject
;
1175 NSSCKFWObject
*fwObject
= NULL
;
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
,
1199 fwMechanism
->mdToken
,
1200 fwMechanism
->fwToken
,
1201 fwMechanism
->mdInstance
,
1202 fwMechanism
->fwInstance
,
1209 if ((NSSCKMDObject
*)NULL
== mdObject
) {
1210 return (NSSCKFWObject
*)NULL
;
1213 fwObject
= nssCKFWObject_Create(arena
, mdObject
,
1214 fwSession
, fwMechanism
->fwToken
, fwMechanism
->fwInstance
, pError
);