2 * bsafeUtils.c - common routines for CDSA/BSAFE compatibility testing
10 //#include <security_bsafe/bsafe.h>
11 //#include <security_bsafe/aglobal.h>
12 #include "bsafeUtils.h"
13 #include <Security/cssmerr.h>
17 * Convert between BSAFE ITEM and CSSM_DATA
20 static inline void buItemToCssmData(
24 cdata
->Data
= item
->data
;
25 cdata
->Length
= item
->len
;
28 static inline void buCssmDataToItem(
29 const CSSM_DATA
*cdata
,
32 item
->data
= cdata
->Data
;
33 item
->len
= cdata
->Length
;
37 * BSafe's Chooser table - all we'll ever need.
39 /*static*/ B_ALGORITHM_METHOD
*BSAFE_ALGORITHM_CHOOSER
[] = {
74 &AM_RSA_STRONG_KEY_GEN
,
76 &AM_RSA_CRT_ENCRYPT_BLIND
,
77 &AM_RSA_CRT_DECRYPT_BLIND
,
89 // random number generation
94 (B_ALGORITHM_METHOD
*)NULL_PTR
98 * Convert a BSAFE return to a CSSM error and optionally print the error msg with
99 * the op in which the error occurred.
101 static CSSM_RETURN
buBsafeErrToCssm(
105 const char *errStr
= NULL
;
112 crtn
= CSSMERR_CSSM_MEMORY_ERROR
;
116 crtn
= CSSMERR_CSP_VERIFY_FAILED
;
117 errStr
= "BE_SIGNATURE";
120 crtn
= CSSMERR_CSP_OUTPUT_LENGTH_ERROR
;
121 errStr
= "BE_OUTPUT_LEN";
124 crtn
= CSSMERR_CSP_INPUT_LENGTH_ERROR
;
125 errStr
= "BE_INPUT_LEN";
127 case BE_EXPONENT_EVEN
:
128 crtn
= CSSMERR_CSP_INVALID_KEY
;
129 errStr
= "BE_EXPONENT_EVEN";
131 case BE_EXPONENT_LEN
:
132 crtn
= CSSMERR_CSP_INVALID_KEY
;
133 errStr
= "BE_EXPONENT_LEN";
135 case BE_EXPONENT_ONE
:
136 crtn
= CSSMERR_CSP_INVALID_KEY
;
137 errStr
= "BE_EXPONENT_ONE";
140 crtn
= CSSMERR_CSP_INVALID_DATA
;
144 crtn
= CSSMERR_CSP_INVALID_DATA
;
145 errStr
= "BE_INPUT_DATA";
147 case BE_WRONG_KEY_INFO
:
148 crtn
= CSSMERR_CSP_INVALID_KEY
;
149 errStr
= "BE_WRONG_KEY_INFO";
152 //@@@ translate BSafe errors intelligently
153 crtn
= CSSM_ERRCODE_INTERNAL_ERROR
;
154 errStr
= "Other BSAFE error";
158 printf("%s: BSAFE error %d (%s)\n", op
, brtn
, errStr
);
164 * Non-thread-safe global random B_ALGORITHM_OBJ and a reusable init for it.
166 static B_ALGORITHM_OBJ bsafeRng
= NULL
;
167 #define BSAFE_RANDSIZE 64
169 static B_ALGORITHM_OBJ
buGetRng()
172 uint8 seed
[BSAFE_RANDSIZE
];
174 if(bsafeRng
!= NULL
) {
177 brtn
= B_CreateAlgorithmObject(&bsafeRng
);
179 buBsafeErrToCssm(brtn
, "B_CreateAlgorithmObject(&bsafeRng)");
182 brtn
= B_SetAlgorithmInfo(bsafeRng
, AI_X962Random_V0
, NULL_PTR
);
184 buBsafeErrToCssm(brtn
, "B_SetAlgorithmInfo(bsafeRng)");
187 brtn
= B_RandomInit(bsafeRng
, BSAFE_ALGORITHM_CHOOSER
, NULL
);
189 buBsafeErrToCssm(brtn
, "B_SetAlgorithmInfo(bsafeRng)");
192 appGetRandomBytes(seed
, BSAFE_RANDSIZE
);
193 brtn
= B_RandomUpdate(bsafeRng
, seed
, BSAFE_RANDSIZE
, NULL
);
195 buBsafeErrToCssm(brtn
, "B_RandomUpdate");
203 * Create a symmetric key.
205 CSSM_RETURN
buGenSymKey(
206 uint32 keySizeInBits
,
207 const CSSM_DATA
*keyData
,
208 BU_KEY
*key
) // RETURNED
212 B_KEY_OBJ bkey
= NULL
;
214 unsigned keyBytes
= (keySizeInBits
+ 7) / 8;
216 if(keyBytes
> keyData
->Length
) {
217 /* note it's OK to give us too much key data */
218 printf("***buGenSymKey: Insufficient keyData\n");
219 return CSSM_ERRCODE_INTERNAL_ERROR
;
222 /* create a BSAFE key */
223 brtn
= B_CreateKeyObject(&bkey
);
225 return buBsafeErrToCssm(brtn
, "B_CreateKeyObject");
228 /* assign data to the key */
229 item
.data
= keyData
->Data
;
231 brtn
= B_SetKeyInfo(bkey
, KI_Item
, (POINTER
)&item
);
233 return buBsafeErrToCssm(brtn
, "B_SetKeyInfo");
244 * Create asymmetric key pair.
245 * FIXME - additional params (e.g. DSA params, RSA exponent)?
247 CSSM_RETURN
buGenKeyPair(
248 uint32 keySizeInBits
,
249 CSSM_ALGORITHMS keyAlg
, // CSSM_ALGID_{RSA,DSA}
250 BU_KEY
*pubKey
, // RETURNED
251 BU_KEY
*privKey
) // RETURNED
253 #if 0 // NO MORE BSAFE
255 B_KEY_OBJ bPubkey
= NULL
;
256 B_KEY_OBJ bPrivkey
= NULL
;
257 B_ALGORITHM_OBJ keypairGen
= NULL
;
258 const char *op
= NULL
;
259 A_RSA_KEY_GEN_PARAMS params
;
260 unsigned char exp
[1] = { 3 };
261 B_ALGORITHM_OBJ genDsaAlg
= NULL
;
262 B_ALGORITHM_OBJ dsaResult
= NULL
;
263 B_DSA_PARAM_GEN_PARAMS dsaParams
;
264 A_DSA_PARAMS
*kParams
= NULL
;
266 /* create algorithm object */
267 brtn
= B_CreateAlgorithmObject(&keypairGen
);
269 return CSSMERR_CSSM_MEMORY_ERROR
;
272 /* create two BSAFE keys */
273 brtn
= B_CreateKeyObject(&bPubkey
);
275 op
="B_CreateKeyObject";
278 brtn
= B_CreateKeyObject(&bPrivkey
);
280 op
="B_CreateKeyObject";
286 /* set RSA-specific params */
287 params
.modulusBits
= keySizeInBits
;
288 /* hack - parameterize? */
289 params
.publicExponent
.data
= exp
;
290 params
.publicExponent
.len
= 1;
291 brtn
= B_SetAlgorithmInfo(keypairGen
, AI_RSAKeyGen
,
294 op
="B_SetAlgorithmInfo(AI_RSAKeyGen)";
300 /* jump through hoops generating parameters */
301 brtn
= B_CreateAlgorithmObject(&genDsaAlg
);
303 op
="B_CreateAlgorithmObject";
306 dsaParams
.primeBits
= keySizeInBits
;
307 brtn
= B_SetAlgorithmInfo(genDsaAlg
, AI_DSAParamGen
, (POINTER
)&dsaParams
);
309 op
= "B_SetAlgorithmInfo(AI_DSAParamGen)";
312 brtn
= B_GenerateInit(genDsaAlg
, BSAFE_ALGORITHM_CHOOSER
, NULL
);
314 op
= "B_GenerateInit(AI_DSAParamGen)";
317 brtn
= B_CreateAlgorithmObject(&dsaResult
);
319 op
= "B_CreateAlgorithmObject";
322 brtn
= B_GenerateParameters(genDsaAlg
, dsaResult
, buGetRng(), NULL
);
324 op
= "B_GenerateParameters";
328 /* dsaResult now has the parameters, which we must extract and then
329 * apply to the keypairGen object. Cool, huh? */
330 brtn
= B_GetAlgorithmInfo((POINTER
*)&kParams
, dsaResult
, AI_DSAKeyGen
);
332 op
= "B_GetAlgorithmInfo(AI_DSAKeyGen)";
335 brtn
= B_SetAlgorithmInfo(keypairGen
, AI_DSAKeyGen
, (POINTER
)kParams
);
337 op
="B_SetAlgorithmInfo(AI_DSAKeyGen)";
342 printf("buGenKeyPair: algorithm not supported\n");
343 return CSSMERR_CSSM_FUNCTION_NOT_IMPLEMENTED
;
349 /* keypairGen all set to go. */
350 brtn
= B_GenerateInit(keypairGen
,
351 BSAFE_ALGORITHM_CHOOSER
,
352 (A_SURRENDER_CTX
*)NULL
);
354 op
= "B_GenerateInit";
357 brtn
= B_GenerateKeypair(keypairGen
,
363 op
= "B_GenerateInit";
366 B_DestroyAlgorithmObject(&keypairGen
);
367 B_DestroyAlgorithmObject(&genDsaAlg
);
368 B_DestroyAlgorithmObject(&dsaResult
);
370 B_DestroyKeyObject(&bPubkey
);
371 B_DestroyKeyObject(&bPrivkey
);
372 return buBsafeErrToCssm(brtn
, op
);
384 * Free a key created in buGenSymKey or buGenKeyPair
386 CSSM_RETURN
buFreeKey(
389 #if 0 // NO MORE BSAFE
390 B_KEY_OBJ bkey
= (B_KEY_OBJ
)key
;
391 B_DestroyKeyObject(&bkey
);
399 CSSM_RETURN
buEncryptDecrypt(
401 CSSM_BOOL forEncrypt
,
402 CSSM_ALGORITHMS encrAlg
,
403 CSSM_ENCRYPT_MODE mode
, // CSSM_ALGMODE_CBC, etc.
404 const CSSM_DATA
*iv
, //Êoptional per mode
405 uint32 effectiveKeyBits
, // optional per key alg (actually just RC2)
406 // for RSA, key size in bits
407 uint32 rounds
, // optional, RC5 only
408 const CSSM_DATA
*inData
,
409 CSSM_DATA_PTR outData
) // mallocd and RETURNED
411 #if 0 // NO MORE BSAFE
415 uint32 blockSize
= 0;
421 // these variables are used in the switch below and need to
422 // live until after setAlgorithm()
424 B_BLK_CIPHER_W_FEEDBACK_PARAMS spec
;
425 A_RC5_PARAMS rc5Params
;
426 A_RC2_PARAMS rc2Params
;
428 brtn
= B_CreateAlgorithmObject(&alg
);
430 return buBsafeErrToCssm(brtn
, "B_CreateAlgorithmObject");
437 brtn
= B_SetAlgorithmInfo(alg
, AI_RC4
, NULL
);
439 crtn
= buBsafeErrToCssm(brtn
, "B_SetAlgorithmInfo");
446 /* assume encrypt via publicm decrypt via private */
448 brtn
= B_SetAlgorithmInfo(alg
, AI_PKCS_RSAPublic
, NULL
);
451 brtn
= B_SetAlgorithmInfo(alg
, AI_PKCS_RSAPrivate
, NULL
);
454 crtn
= buBsafeErrToCssm(brtn
, "B_SetAlgorithmInfo(RSA)");
457 blockSize
= (effectiveKeyBits
+ 7) / 8;
461 /* common code using AI_FeebackCipher */
463 spec
.encryptionMethodName
= (POINTER
)"des";
466 case CSSM_ALGID_DESX
:
467 spec
.encryptionMethodName
= (POINTER
)"desx";
470 case CSSM_ALGID_3DES_3KEY_EDE
:
471 spec
.encryptionMethodName
= (POINTER
)"des_ede";
475 spec
.encryptionMethodName
= (POINTER
)"rc5";
476 spec
.encryptionParams
= (POINTER
)&rc5Params
;
477 rc5Params
.version
= 0x10;
478 rc5Params
.rounds
= rounds
;
479 rc5Params
.wordSizeInBits
= 32;
483 spec
.encryptionMethodName
= (POINTER
)"rc2";
484 spec
.encryptionParams
= (POINTER
)&rc2Params
;
485 rc2Params
.effectiveKeyBits
= effectiveKeyBits
;
488 /* add other non-AI_FeebackCipher algorithms here */
490 printf("buEncryptDecrypt: unknown algorithm\n");
491 return CSSM_ERRCODE_INTERNAL_ERROR
;
494 useIv
= 1; // default, except for ECB
496 case CSSM_ALGMODE_CBCPadIV8
:
497 spec
.feedbackMethodName
= (POINTER
)"cbc";
498 spec
.paddingMethodName
= (POINTER
)"pad";
500 case CSSM_ALGMODE_CBC_IV8
:
501 spec
.feedbackMethodName
= (POINTER
)"cbc";
502 spec
.paddingMethodName
= (POINTER
)"nopad";
504 case CSSM_ALGMODE_OFB_IV8
:
505 spec
.feedbackMethodName
= (POINTER
)"cbc";
506 spec
.paddingMethodName
= (POINTER
)"nopad";
508 case CSSM_ALGMODE_ECB
:
509 /* this does not seem to work yet - need info from
510 * RSA. Specify block size as the feedbackParams (per manual)
511 * and get a memmove error trying to copy from address 8; specify
512 * an IV and get BSAFE error 524 (BE_INPUT_DATA) error on the
515 spec
.feedbackMethodName
= (POINTER
)"ecb";
516 spec
.paddingMethodName
= (POINTER
)"nopad";
518 //spec.feedbackParams = (POINTER)8;
521 printf("buEncryptDecrypt: unknown mode\n");
522 return CSSM_ERRCODE_INTERNAL_ERROR
;
524 if(useIv
&& (iv
!= NULL
)) {
525 buCssmDataToItem(iv
, &bsIv
);
526 spec
.feedbackParams
= (POINTER
)&bsIv
;
529 brtn
= B_SetAlgorithmInfo(alg
, AI_FeedbackCipher
, (POINTER
)&spec
);
531 crtn
= buBsafeErrToCssm(brtn
, "B_SetAlgorithmInfo");
537 * OK, one way or another we have an algorithm object. Set up
541 outBufLen
= inData
->Length
+ blockSize
;
544 outBufLen
= inData
->Length
;
547 outData
->Data
= NULL
;
548 crtn
= appSetupCssmData(outData
, outBufLen
);
553 brtn
= B_EncryptInit(alg
,
555 BSAFE_ALGORITHM_CHOOSER
,
556 (A_SURRENDER_CTX
*)NULL
);
558 crtn
= buBsafeErrToCssm(brtn
, "B_EncryptInit");
561 brtn
= B_EncryptUpdate(alg
,
567 buGetRng(), // randAlg
570 crtn
= buBsafeErrToCssm(brtn
, "B_EncryptInit");
573 outData
->Length
= bytesMoved
;
574 brtn
= B_EncryptFinal(alg
,
575 outData
->Data
+ bytesMoved
,
577 outBufLen
- outData
->Length
,
578 buGetRng(), // randAlg
581 crtn
= buBsafeErrToCssm(brtn
, "B_EncryptFinal");
584 outData
->Length
+= bytesMoved
;
588 brtn
= B_DecryptInit(alg
,
590 BSAFE_ALGORITHM_CHOOSER
,
591 (A_SURRENDER_CTX
*)NULL
);
593 crtn
= buBsafeErrToCssm(brtn
, "B_DecryptInit");
596 brtn
= B_DecryptUpdate(alg
,
605 crtn
= buBsafeErrToCssm(brtn
, "B_DecryptUpdate");
608 outData
->Length
= bytesMoved
;
609 brtn
= B_DecryptFinal(alg
,
610 outData
->Data
+ bytesMoved
,
612 outBufLen
- outData
->Length
,
616 crtn
= buBsafeErrToCssm(brtn
, "B_DecryptFinal");
619 outData
->Length
+= bytesMoved
;
623 B_DestroyAlgorithmObject(&alg
);
629 /* CSSM sig alg --> B_INFO_TYPE */
630 static CSSM_RETURN
cssmSigAlgToInfoType(
631 CSSM_ALGORITHMS cssmAlg
,
632 B_INFO_TYPE
*infoType
)
635 case CSSM_ALGID_SHA1WithRSA
:
636 *infoType
= AI_SHA1WithRSAEncryption
;
638 case CSSM_ALGID_MD5WithRSA
:
639 *infoType
= AI_MD5WithRSAEncryption
;
641 case CSSM_ALGID_SHA1WithDSA
:
642 *infoType
= AI_DSAWithSHA1
;
645 printf("cssmSigAlgToInfoType: unknown algorithm\n");
646 return CSSMERR_CSSM_FUNCTION_NOT_IMPLEMENTED
;
657 CSSM_ALGORITHMS sigAlg
,
658 const CSSM_DATA
*ptext
,
659 uint32 keySizeInBits
, // to set up sig
660 CSSM_DATA_PTR sig
) // mallocd and RETURNED
662 #if 0 // NO MORE BSAFE
663 B_ALGORITHM_OBJ alg
= NULL
;
665 B_INFO_TYPE infoType
;
669 brtn
= B_CreateAlgorithmObject(&alg
);
671 return buBsafeErrToCssm(brtn
, "B_CreateAlgorithmObject");
673 crtn
= cssmSigAlgToInfoType(sigAlg
, &infoType
);
677 brtn
= B_SetAlgorithmInfo(alg
, infoType
, NULL
);
679 crtn
= buBsafeErrToCssm(brtn
, "B_SetAlgorithmInfo");
682 brtn
= B_SignInit(alg
, (B_KEY_OBJ
)key
, BSAFE_ALGORITHM_CHOOSER
, NULL
);
684 crtn
= buBsafeErrToCssm(brtn
, "B_SignInit");
687 brtn
= B_SignUpdate(alg
, ptext
->Data
, ptext
->Length
, NULL
);
689 crtn
= buBsafeErrToCssm(brtn
, "B_SignUpdate");
693 /* prepare for sig, size of key */
694 sigBytes
= (keySizeInBits
+ 7) / 8;
695 sig
->Data
= (uint8
*)CSSM_MALLOC(sigBytes
);
696 sig
->Length
= sigBytes
;
698 brtn
= B_SignFinal(alg
, sig
->Data
, &sigBytes
, sigBytes
, buGetRng(), NULL
);
700 crtn
= buBsafeErrToCssm(brtn
, "B_SignFinal");
703 sig
->Length
= sigBytes
;
706 B_DestroyAlgorithmObject(&alg
);
711 CSSM_RETURN
buVerify(
713 CSSM_ALGORITHMS sigAlg
,
714 const CSSM_DATA
*ptext
,
715 const CSSM_DATA
*sig
) // mallocd and RETURNED
717 #if 0 // NO MORE BSAFE
718 B_ALGORITHM_OBJ alg
= NULL
;
720 B_INFO_TYPE infoType
;
723 brtn
= B_CreateAlgorithmObject(&alg
);
725 return buBsafeErrToCssm(brtn
, "B_CreateAlgorithmObject");
727 crtn
= cssmSigAlgToInfoType(sigAlg
, &infoType
);
731 brtn
= B_SetAlgorithmInfo(alg
, infoType
, NULL
);
733 crtn
= buBsafeErrToCssm(brtn
, "B_SetAlgorithmInfo");
736 brtn
= B_VerifyInit(alg
, (B_KEY_OBJ
)key
, BSAFE_ALGORITHM_CHOOSER
, NULL
);
738 crtn
= buBsafeErrToCssm(brtn
, "B_VerifyInit");
741 brtn
= B_VerifyUpdate(alg
, ptext
->Data
, ptext
->Length
, NULL
);
743 crtn
= buBsafeErrToCssm(brtn
, "B_VerifyUpdate");
746 brtn
= B_VerifyFinal(alg
, sig
->Data
, sig
->Length
, buGetRng(), NULL
);
748 crtn
= buBsafeErrToCssm(brtn
, "B_VerifyFinal");
753 B_DestroyAlgorithmObject(&alg
);
760 * generate MAC either one update (updateSizes == NULL) or
761 * specified set of update sizes.
763 #define MAX_MAC_SIZE 20
765 CSSM_RETURN
buGenMac(
766 BU_KEY key
, // any key, any size
767 CSSM_ALGORITHMS macAlg
, // only CSSM_ALGID_SHA1HMAC for now
768 const CSSM_DATA
*ptext
,
769 unsigned *updateSizes
, // NULL --> random updates
770 // else null-terminated list of sizes
771 CSSM_DATA_PTR mac
) // mallocd and RETURNED
773 #if 0 // NO MORE BSAFE
774 B_ALGORITHM_OBJ alg
= NULL
;
777 B_DIGEST_SPECIFIER digestInfo
;
778 B_INFO_TYPE infoType
;
781 brtn
= B_CreateAlgorithmObject(&alg
);
783 return buBsafeErrToCssm(brtn
, "B_CreateAlgorithmObject");
786 case CSSM_ALGID_SHA1HMAC
:
787 case CSSM_ALGID_SHA1HMAC_LEGACY
:
788 digestInfo
.digestInfoType
= AI_SHA1
;
792 printf("buGenMac: alg not supported\n");
793 return CSSMERR_CSSM_FUNCTION_NOT_IMPLEMENTED
;
795 digestInfo
.digestInfoParams
= NULL
;
796 brtn
= B_SetAlgorithmInfo(alg
, infoType
, (POINTER
)&digestInfo
);
798 crtn
= buBsafeErrToCssm(brtn
, "B_SetAlgorithmInfo");
801 brtn
= B_DigestInit(alg
, (B_KEY_OBJ
)key
, BSAFE_ALGORITHM_CHOOSER
, NULL
);
803 crtn
= buBsafeErrToCssm(brtn
, "B_DigestInit");
807 uint8
*currData
= ptext
->Data
;
808 while(*updateSizes
) {
809 brtn
= B_DigestUpdate(alg
, currData
, *updateSizes
, NULL
);
811 crtn
= buBsafeErrToCssm(brtn
, "B_DigestUpdate");
814 currData
+= *updateSizes
;
820 brtn
= B_DigestUpdate(alg
, ptext
->Data
, ptext
->Length
, NULL
);
822 crtn
= buBsafeErrToCssm(brtn
, "B_DigestUpdate");
826 /* prepare for mac, magically gleaned max size */
827 macBytes
= MAX_MAC_SIZE
;
828 mac
->Data
= (uint8
*)CSSM_MALLOC(macBytes
);
829 mac
->Length
= macBytes
;
831 brtn
= B_DigestFinal(alg
, mac
->Data
, &macBytes
, macBytes
, NULL
);
833 crtn
= buBsafeErrToCssm(brtn
, "B_DigestFinal");
836 mac
->Length
= macBytes
;
839 B_DestroyAlgorithmObject(&alg
);
845 /* generate digest */
846 #define MAX_DIGEST_SIZE 20
848 CSSM_RETURN
buGenDigest(
849 CSSM_ALGORITHMS macAlg
, // CSSM_ALGID_SHA1, etc. */
850 const CSSM_DATA
*ptext
,
851 CSSM_DATA_PTR digest
) // mallocd and RETURNED
853 #if 0 // NO MORE BSAFE
854 B_ALGORITHM_OBJ alg
= NULL
;
857 B_INFO_TYPE infoType
;
860 brtn
= B_CreateAlgorithmObject(&alg
);
862 return buBsafeErrToCssm(brtn
, "B_CreateAlgorithmObject");
865 case CSSM_ALGID_SHA1
:
875 printf("buGenDigest: alg not supported\n");
876 return CSSMERR_CSSM_FUNCTION_NOT_IMPLEMENTED
;
878 brtn
= B_SetAlgorithmInfo(alg
, infoType
, NULL
);
880 crtn
= buBsafeErrToCssm(brtn
, "B_SetAlgorithmInfo");
883 brtn
= B_DigestInit(alg
, NULL
, BSAFE_ALGORITHM_CHOOSER
, NULL
);
885 crtn
= buBsafeErrToCssm(brtn
, "B_DigestInit");
888 brtn
= B_DigestUpdate(alg
, ptext
->Data
, ptext
->Length
, NULL
);
890 crtn
= buBsafeErrToCssm(brtn
, "B_DigestUpdate");
894 /* prepare for digest, magically gleaned max size */
895 hashBytes
= MAX_DIGEST_SIZE
;
896 digest
->Data
= (uint8
*)CSSM_MALLOC(hashBytes
);
897 digest
->Length
= hashBytes
;
899 brtn
= B_DigestFinal(alg
, digest
->Data
, &hashBytes
, hashBytes
, NULL
);
901 crtn
= buBsafeErrToCssm(brtn
, "B_DigestFinal");
904 digest
->Length
= hashBytes
;
907 B_DestroyAlgorithmObject(&alg
);
915 * Convert between BSAFE and CDSA private keys
917 CSSM_RETURN
buBsafePrivKeyToCdsa(
918 CSSM_ALGORITHMS keyAlg
,
919 uint32 keySizeInBits
,
921 CSSM_KEY_PTR cdsaPrivKey
)
923 #if 0 // NO MORE BSAFE
924 B_INFO_TYPE infoType
;
927 CSSM_KEYBLOB_FORMAT format
;
928 CSSM_KEYHEADER_PTR hdr
= &cdsaPrivKey
->KeyHeader
;
930 /* what kind of info? */
933 infoType
= KI_PKCS_RSAPrivateBER
;
934 format
= CSSM_KEYBLOB_RAW_FORMAT_PKCS8
;
937 infoType
= KI_DSAPrivateBER
;
938 format
= CSSM_KEYBLOB_RAW_FORMAT_FIPS186
;
941 printf("***buBsafePrivKeyToCdsa: bogus keyAlg\n");
942 return CSSMERR_CSSM_FUNCTION_NOT_IMPLEMENTED
;
946 brtn
= B_GetKeyInfo((POINTER
*)&keyBlob
,
947 (B_KEY_OBJ
)bsafePrivKey
,
950 return buBsafeErrToCssm(brtn
, "B_GetKeyInfo");
953 /* copy blob to CDSA key */
954 cdsaPrivKey
->KeyData
.Data
= (uint8
*)CSSM_MALLOC(keyBlob
->len
);
955 cdsaPrivKey
->KeyData
.Length
= keyBlob
->len
;
956 memmove(cdsaPrivKey
->KeyData
.Data
, keyBlob
->data
, keyBlob
->len
);
958 /* set up CSSM key header */
959 memset(hdr
, 0, sizeof(CSSM_KEYHEADER
));
960 hdr
->HeaderVersion
= CSSM_KEYHEADER_VERSION
;
961 hdr
->BlobType
= CSSM_KEYBLOB_RAW
;
962 hdr
->Format
= format
;
963 hdr
->AlgorithmId
= keyAlg
;
964 hdr
->KeyClass
= CSSM_KEYCLASS_PRIVATE_KEY
;
965 hdr
->LogicalKeySizeInBits
= keySizeInBits
;
966 hdr
->KeyAttr
= CSSM_KEYATTR_EXTRACTABLE
;
967 hdr
->KeyUsage
= CSSM_KEYUSE_ANY
;
972 CSSM_RETURN
buCdsaPrivKeyToBsafe(
973 CSSM_KEY_PTR cdsaPrivKey
,
974 BU_KEY
*bsafePrivKey
)
976 #if 0 // NO MORE BSAFE
978 B_KEY_OBJ privKey
= NULL
;
980 B_INFO_TYPE infoType
;
982 /* what kind of info? */
983 switch(cdsaPrivKey
->KeyHeader
.AlgorithmId
) {
985 infoType
= KI_PKCS_RSAPrivateBER
;
988 infoType
= KI_DSAPrivateBER
;
991 printf("***buCdsaPrivKeyToCssm: bogus keyAlg\n");
992 return CSSMERR_CSSM_FUNCTION_NOT_IMPLEMENTED
;
995 /* create caller's key, assign blob to it */
996 brtn
= B_CreateKeyObject(&privKey
);
998 return buBsafeErrToCssm(brtn
, "B_CreateKeyObject");
1000 buCssmDataToItem(&cdsaPrivKey
->KeyData
, &keyBlob
);
1001 brtn
= B_SetKeyInfo(privKey
, infoType
, (POINTER
)&keyBlob
);
1003 return buBsafeErrToCssm(brtn
, "B_SetKeyInfo");
1005 *bsafePrivKey
= privKey
;