2 * Copyright (c) 2003-2004 Apple Computer, Inc. All Rights Reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
21 * @APPLE_LICENSE_HEADER_END@
27 #ifndef _PKCS12_UTILS_H_
28 #define _PKCS12_UTILS_H_
30 #include <Security/cssmtype.h>
31 #include <security_asn1/SecNssCoder.h>
32 #include <security_pkcs12/pkcs7Templates.h>
33 #include <security_pkcs12/pkcs12Templates.h>
34 #include <Security/cssmerr.h>
35 #include <CoreFoundation/CoreFoundation.h>
36 #include <CoreServices/../Frameworks/CarbonCore.framework/Headers/MacErrors.h>
42 /* malloc a NULL-ed array of pointers of size num+1 */
43 void **p12NssNullArray(
47 /* CSSM_DATA --> uint32. Returns true if OK. */
49 const CSSM_DATA
&cdata
,
52 /* uint32 --> CSSM_DATA */
58 /* CFDataRef <--> CSSM_DATA */
59 CFDataRef
p12CssmDataToCf(
66 CSSM_DATA_PTR
p12StringToUtf8(
70 const char *p12BagTypeStr(
71 NSS_P12_SB_Type type
);
72 const char *p7ContentInfoTypeStr(
75 /* map an OID to the components */
78 PW_None
, /* not comprehended */
79 PW_PKCS5_v1_5
, /* PKCS5 v1.5 */
80 PW_PKCS5_v2
, /* PKCS5 v2.0, not used by this module but parsed here */
81 PW_PKCS12
/* PKCS12 */
84 /* returns false if OID not found */
87 CSSM_ALGORITHMS
&keyAlg
, // e.g., CSSM_ALGID_DES
88 CSSM_ALGORITHMS
&encrAlg
, // e.g., CSSM_ALGID_3DES_3KEY_EDE
89 CSSM_ALGORITHMS
&pbeHashAlg
, // SHA1 or MD5
90 uint32
&keySizeInBits
,
91 uint32
&blockSizeInBytes
, // for IV, optional
92 CSSM_PADDING
&padding
, // CSSM_PADDING_PKCS7, etc.
93 CSSM_ENCRYPT_MODE
&mode
, // CSSM_ALGMODE_CBCPadIV8, etc.
94 PKCS_Which
&pkcs
); // PW_PKCS5_v1_5 or PW_PKCS12
96 CSSM_RETURN
p12VerifyMac(
97 const NSS_P12_DecodedPFX
&pfx
,
98 CSSM_CSP_HANDLE cspHand
,
99 const CSSM_DATA
*pwd
, // unicode, double null terminated
100 const CSSM_KEY
*passKey
,
101 SecNssCoder
&coder
);// for temp mallocs
111 void p12NullAlgParams(
112 CSSM_X509_ALGORITHM_IDENTIFIER
&algId
);
115 * Free memory via specified plugin's app-level allocator
122 * Though it pains me to do this, I must. We "happen to know" the
123 * names (in string form) of two of a key's attributes. These
124 * have not been published anywhere, they are hard-coded into
125 * the script (KeySchema.m4) which generates the KeySchema
130 * This one is initially the same as the "label" argument passed
131 * in to the CSP when creating or importing keys; it eventually
132 * gets munged into the hash of the associated public key (
133 * in our case, by p12SetPubKeyHash()).
135 #define P12_KEY_ATTR_LABEL_AND_HASH "Label"
138 * This one is the user-friendly name.
140 #define P12_KEY_ATTR_PRINT_NAME "PrintName"
143 * Find private key by label, modify its Label attr to be the
144 * hash of the associated public key.
146 CSSM_RETURN
p12SetPubKeyHash(
147 CSSM_CSP_HANDLE cspHand
, // where the key lives
148 CSSM_DL_DB_HANDLE dlDbHand
, // ditto
149 CSSM_DATA
&keyLabel
, // for DB lookup
150 CSSM_DATA_PTR newPrintName
, // optional
151 SecNssCoder
&coder
, // for mallocing newLabel
152 CSSM_DATA
&newLabel
, // RETURNED with label as hash
153 CSSM_KEY_PTR
&foundKey
); // RETURNED on dup key detect
155 CSSM_RETURN
p12AddContextAttribute(CSSM_CC_HANDLE CCHandle
,
156 uint32 AttributeType
,
157 uint32 AttributeLength
,
158 const void *AttributePtr
);
161 * Find private key by specified label, delete it.
163 CSSM_RETURN
p12DeleteKey(
164 CSSM_DL_DB_HANDLE dlDbHand
,
165 const CSSM_DATA
&keyLabel
);
167 /* convert App passphrase to array of chars used in P12 PBE */
168 void p12ImportPassPhrase(
169 CFStringRef inPhrase
,
171 CSSM_DATA
&outPhrase
);
174 * Standard error throwMes.
175 * P12_ENCODE_ERR only occurs on DER-encode which should never fail.
177 #define P12_DECODE_ERR errSecUnknownFormat
178 #define P12_ENCODE_ERR internalComponentErr
179 #define P12_THROW_DECODE MacOSError::throwMe(P12_DECODE_ERR)
180 #define P12_THROW_ENCODE MacOSError::throwMe(P12_ENCODE_ERR)
186 #endif /* _PKCS12_UTILS_H_ */