2 * Copyright (c) 2003-2004,2011,2013-2014 Apple 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 #include "pkcs12Templates.h"
28 #include "pkcs12Utils.h"
29 #include <security_asn1/nssUtils.h>
30 #include <Security/SecAsn1Templates.h>
31 #include <Security/oidsattr.h>
33 #pragma clang diagnostic push
34 #pragma clang diagnostic ignored "-Wunused-const-variable"
37 const SecAsn1Template NSS_P12_MacDataTemplate
[] = {
39 0, NULL
, sizeof(NSS_P12_MacData
) },
41 offsetof(NSS_P12_MacData
,mac
),
42 NSS_P7_DigestInfoTemplate
},
43 { SEC_ASN1_OCTET_STRING
,
44 offsetof(NSS_P12_MacData
,macSalt
) },
45 /* iterations is unsigned - right? */
46 { SEC_ASN1_INTEGER
| SEC_ASN1_OPTIONAL
,
47 offsetof(NSS_P12_MacData
,iterations
) },
51 const SecAsn1Template pointerToMacDataTemplate
[] = {
52 { SEC_ASN1_POINTER
, 0, NSS_P12_MacDataTemplate
}
55 /* raw PFX with unprocessed authSafe */
56 const SecAsn1Template NSS_P12_RawPFXTemplate
[] = {
58 0, NULL
, sizeof(NSS_P12_RawPFX
) },
60 offsetof(NSS_P12_RawPFX
,version
) },
62 offsetof(NSS_P12_RawPFX
, authSafe
),
63 NSS_P7_RawContentInfoTemplate
},
64 { SEC_ASN1_POINTER
| SEC_ASN1_OPTIONAL
,
65 offsetof(NSS_P12_RawPFX
, macData
),
66 NSS_P12_MacDataTemplate
},
70 /* PFX with decoded authSafe */
71 extern const SecAsn1Template NSS_P12_DecodedPFXTemplate
[] = {
73 0, NULL
, sizeof(NSS_P12_DecodedPFX
) },
75 offsetof(NSS_P12_DecodedPFX
,version
) },
77 offsetof(NSS_P12_DecodedPFX
, authSafe
),
78 NSS_P7_DecodedContentInfoTemplate
},
79 { SEC_ASN1_POINTER
| SEC_ASN1_OPTIONAL
,
80 offsetof(NSS_P12_DecodedPFX
, macData
),
81 NSS_P12_MacDataTemplate
},
85 /* AuthenticatedSafe */
86 const SecAsn1Template NSS_P12_AuthenticatedSafeTemplate
[] = {
87 { SEC_ASN1_SEQUENCE_OF
,
88 offsetof(NSS_P12_AuthenticatedSafe
, info
),
89 NSS_P7_DecodedContentInfoTemplate
,
90 sizeof(NSS_P12_AuthenticatedSafe
) }
94 * Individual SafeBag type-specific templates here when we write 'em
96 const SecAsn1Template NSS_P12_PtrToShroudedKeyBagTemplate
[] = {
97 { SEC_ASN1_POINTER
, 0, kSecAsn1EncryptedPrivateKeyInfoTemplate
}
101 * CertBag via SEC_ASN1_DYNAMIC
103 static const SecAsn1Template
* NSS_P12_CertBagChooser(
104 void *arg
, // --> NSS_P12_CertBag
108 void *dest
) // --> NSS_P12_CertBag.bagValue
110 NSS_P12_CertBag
*bag
= (NSS_P12_CertBag
*)arg
;
111 const SecAsn1Template
*templ
= NULL
;
112 NSS_P12_CertBagType type
= CT_Unknown
;
113 CSSM_OID
*oid
= &bag
->bagType
;
115 if(nssCompareCssmData(oid
, &CSSMOID_PKCS9_X509Certificate
)) {
116 templ
= kSecAsn1OctetStringTemplate
;
119 else if(nssCompareCssmData(oid
, &CSSMOID_PKCS9_SdsiCertificate
)) {
120 templ
= kSecAsn1IA5StringTemplate
;
125 templ
= kSecAsn1AnyTemplate
;
133 static const SecAsn1TemplateChooserPtr NSS_P12_CertBagChooserPtr
=
134 NSS_P12_CertBagChooser
;
136 const SecAsn1Template NSS_P12_CertBagTemplate
[] = {
138 0, NULL
, sizeof(NSS_P12_CertBag
) },
139 { SEC_ASN1_OBJECT_ID
,
140 offsetof(NSS_P12_CertBag
,bagType
) },
141 /* these come in with a tag of 0xA0, context/constructed,
142 * though I don't know why they are flagged as constructed */
143 { SEC_ASN1_DYNAMIC
| SEC_ASN1_CONTEXT_SPECIFIC
|
144 SEC_ASN1_EXPLICIT
| SEC_ASN1_CONSTRUCTED
| 0,
145 offsetof(NSS_P12_CertBag
, certValue
),
146 &NSS_P12_CertBagChooserPtr
},
150 const SecAsn1Template NSS_P12_PtrToCertBagTemplate
[] = {
151 { SEC_ASN1_POINTER
, 0, NSS_P12_CertBagTemplate
}
155 * CrlBag via SEC_ASN1_DYNAMIC
157 static const SecAsn1Template
* NSS_P12_CrlBagChooser(
158 void *arg
, // --> NSS_P12_CrlBag
160 const char *buf
, // on decode, tag byte and length
162 void *dest
) // --> NSS_P12_CertBag.bagValue
164 NSS_P12_CrlBag
*bag
= (NSS_P12_CrlBag
*)arg
;
165 const SecAsn1Template
*templ
= NULL
;
166 NSS_P12_CrlBagType type
= CRT_Unknown
;
167 CSSM_OID
*oid
= &bag
->bagType
;
169 if(nssCompareCssmData(oid
, &CSSMOID_PKCS9_X509Crl
)) {
170 templ
= kSecAsn1OctetStringTemplate
;
175 templ
= kSecAsn1AnyTemplate
;
183 static const SecAsn1TemplateChooserPtr NSS_P12_CrlBagChooserPtr
=
184 NSS_P12_CrlBagChooser
;
186 const SecAsn1Template NSS_P12_CrlBagTemplate
[] = {
188 0, NULL
, sizeof(NSS_P12_CrlBag
) },
189 { SEC_ASN1_OBJECT_ID
,
190 offsetof(NSS_P12_CrlBag
,bagType
) },
191 /* these come in with a tag of 0xA0, context/constructed,
192 * though I don't know why they are flagged as constructed */
193 { SEC_ASN1_DYNAMIC
| SEC_ASN1_CONTEXT_SPECIFIC
|
194 SEC_ASN1_EXPLICIT
| SEC_ASN1_CONSTRUCTED
| 0,
195 offsetof(NSS_P12_CrlBag
, crlValue
),
196 &NSS_P12_CrlBagChooserPtr
},
200 const SecAsn1Template NSS_P12_PtrToCrlBagTemplate
[] = {
201 { SEC_ASN1_POINTER
, 0, NSS_P12_CrlBagTemplate
}
205 /* the stub templates for unimplemented BagTypes */
206 #define NSS_P12_PtrToKeyBagTemplate kSecAsn1PointerToAnyTemplate
207 #define NSS_P12_PtrToSecretBagTemplate kSecAsn1PointerToAnyTemplate
208 #define NSS_P12_PtrToSafeContentsBagTemplate kSecAsn1PointerToAnyTemplate
212 * SafeBag via SEC_ASN1_DYNAMIC
214 static const SecAsn1Template
* NSS_P12_SafeBagChooser(
215 void *arg
, // --> NSS_P12_SafeBag
217 const char *buf
, // on decode, tag byte and length
219 void *dest
) // --> NSS_P12_SafeBag.bagValue
221 NSS_P12_SafeBag
*bag
= (NSS_P12_SafeBag
*)arg
;
222 const SecAsn1Template
*templ
= NULL
;
223 NSS_P12_SB_Type type
= BT_None
;
224 CSSM_OID
*oid
= &bag
->bagId
;
226 if(nssCompareCssmData(oid
, &CSSMOID_PKCS12_keyBag
)) {
227 templ
= NSS_P12_PtrToKeyBagTemplate
;
230 else if(nssCompareCssmData(oid
, &CSSMOID_PKCS12_shroudedKeyBag
)) {
231 templ
= NSS_P12_PtrToShroudedKeyBagTemplate
;
232 type
= BT_ShroudedKeyBag
;
234 else if(nssCompareCssmData(oid
, &CSSMOID_PKCS12_certBag
)) {
235 templ
= NSS_P12_PtrToCertBagTemplate
;
238 else if(nssCompareCssmData(oid
, &CSSMOID_PKCS12_crlBag
)) {
239 templ
= NSS_P12_PtrToCrlBagTemplate
;
242 else if(nssCompareCssmData(oid
, &CSSMOID_PKCS12_secretBag
)) {
243 templ
= NSS_P12_PtrToSecretBagTemplate
;
246 else if(nssCompareCssmData(oid
, &CSSMOID_PKCS12_safeContentsBag
)) {
247 templ
= NSS_P12_PtrToSafeContentsBagTemplate
;
248 type
= BT_SafeContentsBag
;
250 /* add more here when we implement them */
252 templ
= kSecAsn1PointerToAnyTemplate
;
260 static const SecAsn1TemplateChooserPtr NSS_P12_SafeBagChooserPtr
=
261 NSS_P12_SafeBagChooser
;
263 const SecAsn1Template NSS_P12_SafeBagTemplate
[] = {
265 0, NULL
, sizeof(NSS_P12_SafeBag
) },
266 { SEC_ASN1_OBJECT_ID
,
267 offsetof(NSS_P12_SafeBag
,bagId
) },
268 { SEC_ASN1_DYNAMIC
| SEC_ASN1_CONSTRUCTED
|
269 SEC_ASN1_EXPLICIT
| SEC_ASN1_CONTEXT_SPECIFIC
| 0,
270 offsetof(NSS_P12_SafeBag
,bagValue
),
271 &NSS_P12_SafeBagChooserPtr
},
272 { SEC_ASN1_OPTIONAL
| SEC_ASN1_SET_OF
,
273 offsetof(NSS_P12_SafeBag
,bagAttrs
),
274 kSecAsn1AttributeTemplate
},
278 const SecAsn1Template NSS_P12_SafeContentsTemplate
[] = {
279 { SEC_ASN1_SEQUENCE_OF
,
280 offsetof(NSS_P12_SafeContents
, bags
),
281 NSS_P12_SafeBagTemplate
,
282 sizeof(NSS_P12_SafeContents
) }
285 const SecAsn1Template NSS_P12_PBE_ParamsTemplate
[] = {
287 0, NULL
, sizeof(NSS_P12_PBE_Params
) },
288 { SEC_ASN1_OCTET_STRING
,
289 offsetof(NSS_P12_PBE_Params
,salt
) },
290 /* iterations is unsigned - right? */
292 offsetof(NSS_P12_PBE_Params
,iterations
) },
296 #pragma clang diagnostic pop