2 * Copyright (c) 2003,2005 Apple Computer, Inc. All Rights Reserved.
4 * The contents of this file constitute Original Code as defined in and are
5 * subject to the Apple Public Source License Version 1.2 (the 'License').
6 * You may not use this file except in compliance with the License. Please
7 * obtain a copy of the License at http://www.apple.com/publicsource and
8 * read it before using this file.
10 * This Original Code and all software distributed under the License are
11 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
12 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
13 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
14 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
15 * Please see the License for the specific language governing rights and
16 * limitations under the License.
20 * pkcs12Utils.cpp - standalone copies of utility functions from libsecurity_pkcs12
23 #include "pkcs12Utils.h"
25 #include <Security/oidsalg.h>
26 #include <security_asn1/nssUtils.h>
28 /* CSSM_DATA --> uint32. Returns true if OK. */
30 const CSSM_DATA
&cdata
,
33 if((cdata
.Length
== 0) || (cdata
.Data
== NULL
)) {
34 /* default/not present */
38 uint32 len
= cdata
.Length
;
39 if(len
> sizeof(uint32
)) {
44 uint8
*cp
= cdata
.Data
;
45 for(uint32 i
=0; i
<len
; i
++) {
46 rtn
= (rtn
<< 8) | *cp
++;
53 * OIDS for P12 and PKCS5 v1.5 (PBES1) encrypt and decrypt map to the following
58 CSSM_ALGORITHMS keyAlg
; // e.g., CSSM_ALGID_DES
59 CSSM_ALGORITHMS encrAlg
; // e.g., CSSM_ALGID_3DES_3KEY_EDE
60 CSSM_ALGORITHMS pbeHashAlg
; // SHA1 or MD5
62 uint32 blockSizeInBytes
; // for IV, optional
63 CSSM_PADDING padding
; // CSSM_PADDING_PKCS7, etc.
64 CSSM_ENCRYPT_MODE mode
; // CSSM_ALGMODE_CBCPadIV8, etc.
65 PKCS_Which pkcs
; // PW_PKCS12 (for this module) or PW_PKCS5_v1_5
68 static const PKCSOidInfo pkcsOidInfos
[] = {
69 /* PKCS12 first, the ones this module uses */
71 &CSSMOID_PKCS12_pbeWithSHAAnd128BitRC4
,
76 0, // RC4 is a stream cipher
82 &CSSMOID_PKCS12_pbeWithSHAAnd40BitRC4
,
87 0, // RC4 is a stream cipher
93 &CSSMOID_PKCS12_pbeWithSHAAnd3Key3DESCBC
,
95 CSSM_ALGID_3DES_3KEY_EDE
,
100 CSSM_ALGMODE_CBCPadIV8
,
104 &CSSMOID_PKCS12_pbeWithSHAAnd2Key3DESCBC
,
105 CSSM_ALGID_3DES_2KEY
,
106 CSSM_ALGID_3DES_2KEY_EDE
,
111 CSSM_ALGMODE_CBCPadIV8
,
115 &CSSMOID_PKCS12_pbeWithSHAAnd128BitRC2CBC
,
122 CSSM_ALGMODE_CBCPadIV8
,
126 &CSSMOID_PKCS12_pbewithSHAAnd40BitRC2CBC
,
133 CSSM_ALGMODE_CBCPadIV8
,
137 /* PKCS5 v1.5, used for SecImportExport module */
139 &CSSMOID_PKCS5_pbeWithMD2AndDES
,
146 CSSM_ALGMODE_CBCPadIV8
,
150 &CSSMOID_PKCS5_pbeWithMD2AndRC2
,
157 CSSM_ALGMODE_CBCPadIV8
,
161 &CSSMOID_PKCS5_pbeWithMD5AndDES
,
168 CSSM_ALGMODE_CBCPadIV8
,
172 &CSSMOID_PKCS5_pbeWithMD5AndRC2
,
179 CSSM_ALGMODE_CBCPadIV8
,
183 &CSSMOID_PKCS5_pbeWithSHA1AndDES
,
190 CSSM_ALGMODE_CBCPadIV8
,
194 &CSSMOID_PKCS5_pbeWithSHA1AndRC2
,
201 CSSM_ALGMODE_CBCPadIV8
,
205 /* finally one for PKCS5 v2.0, which has its own means of
206 * cooking up all the parameters */
208 &CSSMOID_PKCS5_PBES2
,
217 #define NUM_PKCS_OID_INFOS (sizeof(pkcsOidInfos) / sizeof(pkcsOidInfos[1]))
219 /* map an OID to the components */
220 /* returns false if OID not found */
223 * NOTE: as of March 8 2004 this is also used by the SecImportExport
224 * module...not just PKCS12!
226 bool pkcsOidToParams(
228 CSSM_ALGORITHMS
&keyAlg
, // e.g., CSSM_ALGID_DES
229 CSSM_ALGORITHMS
&encrAlg
, // e.g., CSSM_ALGID_3DES_3KEY_EDE
230 CSSM_ALGORITHMS
&pbeHashAlg
, // SHA1 or MD5
231 uint32
&keySizeInBits
,
232 uint32
&blockSizeInBytes
, // for IV, optional
233 CSSM_PADDING
&padding
, // CSSM_PADDING_PKCS7, etc.
234 CSSM_ENCRYPT_MODE
&mode
, // CSSM_ALGMODE_CBCPadIV8, etc.
235 PKCS_Which
&pkcs
) // PW_PKCS5_v1_5 or PW_PKCS12
237 const PKCSOidInfo
*info
= pkcsOidInfos
;
240 for(unsigned dex
=0; dex
<NUM_PKCS_OID_INFOS
; dex
++) {
241 if(nssCompareCssmData(oid
, info
->oid
)) {
242 keyAlg
= info
->keyAlg
;
243 encrAlg
= info
->encrAlg
;
244 pbeHashAlg
= info
->pbeHashAlg
;
245 keySizeInBits
= info
->keySizeInBits
;
246 blockSizeInBytes
= info
->blockSizeInBytes
;
247 padding
= info
->padding
;
258 * Enum to string mappper.
262 * Each type of attribute has a name/value pair in a table of these:
269 /* declare one entry in a table of p12NameValuePair */
270 #define NVP(attr) {attr, #attr}
272 /* the NULL entry which terminates all p12NameValuePair tables */
273 #define NVP_END {0, NULL}
275 static const p12NameValuePair p7CITypeNames
[] =
281 NVP(CT_SignedEnvData
),
283 NVP(CT_EncryptedData
),
287 static const p12NameValuePair p12BagTypeNames
[] =
291 NVP(BT_ShroudedKeyBag
),
295 NVP(BT_SafeContentsBag
),
299 static const char *typeToStr(
301 const p12NameValuePair
*table
)
304 if(table
->value
== type
) {
312 const char *p12BagTypeStr(
313 NSS_P12_SB_Type type
)
315 return typeToStr(type
, p12BagTypeNames
);
318 const char *p7ContentInfoTypeStr(
321 return typeToStr(type
, p7CITypeNames
);