2 * p12GetPassKey.h - get a CSSM_ALGID_SECURE_PASSPHRASE key for encode/decode
5 #include <CoreFoundation/CoreFoundation.h>
6 #include <Security/Security.h>
7 #include "p12GetPassKey.h"
8 #include <CoreServices.framework/Frameworks/CarbonCore.framework/Headers/MacErrors.h>
9 #include <Security/cssmapple.h>
10 #include <utilLib/cspwrap.h>
12 /* when true, simulate secure passphrase in CSPDL */
13 #define SIMULATE_PASSPHRASE 1
17 * -- guaranteed no buffer overflow
18 * -- guaranteed NULL-terminated string
19 * -- handles empty string (i.e., response is just CR) properly
29 for(dex
=0; dex
<bufSize
-1; dex
++) {
50 OSStatus
p12GetPassKey(
51 CSSM_CSP_HANDLE cspHand
,
54 CSSM_KEY
*passKey
) // RETURNED
56 if(isRawCsp
|| SIMULATE_PASSPHRASE
) {
59 if(gpkType
== GPK_Decode
) {
60 printf("Enter passphrase for PKCS12 Decode: ");
63 printf("Enter passphrase for PKCS12 Encode: ");
65 getString(passphrase
, 512);
67 /* cook up a raw key with passphrase as data */
68 unsigned phraseLen
= strlen(passphrase
);
70 memset(&rawKey
, 0, sizeof(CSSM_KEY
));
71 CSSM_KEYHEADER
&hdr
= rawKey
.KeyHeader
;
72 hdr
.HeaderVersion
= CSSM_KEYHEADER_VERSION
;
73 hdr
.BlobType
= CSSM_KEYBLOB_RAW
;
74 hdr
.Format
= CSSM_KEYBLOB_RAW_FORMAT_OCTET_STRING
;
75 hdr
.AlgorithmId
= CSSM_ALGID_SECURE_PASSPHRASE
;
76 hdr
.KeyClass
= CSSM_KEYCLASS_SESSION_KEY
;
77 hdr
.LogicalKeySizeInBits
= phraseLen
* 2 * 8;
78 hdr
.KeyAttr
= CSSM_KEYATTR_MODIFIABLE
| CSSM_KEYATTR_EXTRACTABLE
;
79 hdr
.KeyUsage
= CSSM_KEYUSE_DERIVE
;
82 /* data = Unicode version of C string passphrase, bigendian */
83 rawKey
.KeyData
.Length
= phraseLen
* 2;
84 rawKey
.KeyData
.Data
= (uint8
*)malloc(phraseLen
* 2);
85 const char *cpIn
= passphrase
;
86 char *cpOut
= (char *)rawKey
.KeyData
.Data
;
88 for(unsigned dex
=0; dex
<phraseLen
; dex
++) {
94 /* data = external representation of CFString */
95 CFStringRef cfStr
= CFStringCreateWithCString(NULL
, passphrase
,
96 kCFStringEncodingASCII
);
97 CFDataRef cfData
= CFStringCreateExternalRepresentation(NULL
,
98 cfStr
, kCFStringEncodingUnicode
, 0);
99 unsigned keyLen
= CFDataGetLength(cfData
);
100 rawKey
.KeyData
.Length
= keyLen
;
101 rawKey
.KeyData
.Data
= (uint8
*)malloc(keyLen
);
102 memmove(rawKey
.KeyData
.Data
, CFDataGetBytePtr(cfData
), keyLen
);
105 hdr
.LogicalKeySizeInBits
= keyLen
* 8;
107 CSSM_DATA descrData
= {0, NULL
};
109 /* NULL unwrap to make a ref key */
110 CSSM_RETURN crtn
= cspUnwrapKey(cspHand
,
114 0, 0, 0, // mode, pad, vector
120 printf("***Error doing NULL wrap of passKey.\n");
126 printf("SS does not support secure passphrase yet.");
128 * TBD: do a DeriveKey