Import from 1.9a8 tarball
[mozilla-nss.git] / security / nss / lib / pkcs12 / p12tmpl.c
blob9b0f3ddba0edb4405733c9ea2547a17e354c03ee
1 /* ***** BEGIN LICENSE BLOCK *****
2 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
4 * The contents of this file are subject to the Mozilla Public License Version
5 * 1.1 (the "License"); you may not use this file except in compliance with
6 * the License. You may obtain a copy of the License at
7 * http://www.mozilla.org/MPL/
9 * Software distributed under the License is distributed on an "AS IS" basis,
10 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
11 * for the specific language governing rights and limitations under the
12 * License.
14 * The Original Code is the Netscape security libraries.
16 * The Initial Developer of the Original Code is
17 * Netscape Communications Corporation.
18 * Portions created by the Initial Developer are Copyright (C) 1994-2000
19 * the Initial Developer. All Rights Reserved.
21 * Contributor(s):
23 * Alternatively, the contents of this file may be used under the terms of
24 * either the GNU General Public License Version 2 or later (the "GPL"), or
25 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
26 * in which case the provisions of the GPL or the LGPL are applicable instead
27 * of those above. If you wish to allow use of your version of this file only
28 * under the terms of either the GPL or the LGPL, and not to allow others to
29 * use your version of this file under the terms of the MPL, indicate your
30 * decision by deleting the provisions above and replace them with the notice
31 * and other provisions required by the GPL or the LGPL. If you do not delete
32 * the provisions above, a recipient may use your version of this file under
33 * the terms of any one of the MPL, the GPL or the LGPL.
35 * ***** END LICENSE BLOCK ***** */
37 #include "plarena.h"
38 #include "secitem.h"
39 #include "secoid.h"
40 #include "seccomon.h"
41 #include "secport.h"
42 #include "cert.h"
43 #include "secpkcs7.h"
44 #include "secasn1.h"
45 #include "p12t.h"
47 SEC_ASN1_MKSUB(SEC_AnyTemplate)
48 SEC_ASN1_MKSUB(sgn_DigestInfoTemplate)
50 static const SEC_ASN1Template *
51 sec_pkcs12_choose_safe_bag_type(void *src_or_dest, PRBool encoding)
53 const SEC_ASN1Template *theTemplate;
54 sec_PKCS12SafeBag *safeBag;
55 SECOidData *oiddata;
57 if (src_or_dest == NULL) {
58 return NULL;
61 safeBag = (sec_PKCS12SafeBag*)src_or_dest;
63 oiddata = SECOID_FindOID(&safeBag->safeBagType);
64 if(oiddata == NULL) {
65 return SEC_ASN1_GET(SEC_AnyTemplate);
68 switch (oiddata->offset) {
69 default:
70 theTemplate = SEC_ASN1_GET(SEC_AnyTemplate);
71 break;
72 case SEC_OID_PKCS12_V1_KEY_BAG_ID:
73 theTemplate = SEC_ASN1_GET(SECKEY_PointerToPrivateKeyInfoTemplate);
74 break;
75 case SEC_OID_PKCS12_V1_CERT_BAG_ID:
76 theTemplate = sec_PKCS12PointerToCertBagTemplate;
77 break;
78 case SEC_OID_PKCS12_V1_CRL_BAG_ID:
79 theTemplate = sec_PKCS12PointerToCRLBagTemplate;
80 break;
81 case SEC_OID_PKCS12_V1_SECRET_BAG_ID:
82 theTemplate = sec_PKCS12PointerToSecretBagTemplate;
83 break;
84 case SEC_OID_PKCS12_V1_PKCS8_SHROUDED_KEY_BAG_ID:
85 theTemplate =
86 SEC_ASN1_GET(SECKEY_PointerToEncryptedPrivateKeyInfoTemplate);
87 break;
88 case SEC_OID_PKCS12_V1_SAFE_CONTENTS_BAG_ID:
89 if(encoding) {
90 theTemplate = sec_PKCS12PointerToSafeContentsTemplate;
91 } else {
92 theTemplate = SEC_ASN1_GET(SEC_PointerToAnyTemplate);
94 break;
96 return theTemplate;
99 static const SEC_ASN1Template *
100 sec_pkcs12_choose_crl_bag_type(void *src_or_dest, PRBool encoding)
102 const SEC_ASN1Template *theTemplate;
103 sec_PKCS12CRLBag *crlbag;
104 SECOidData *oiddata;
106 if (src_or_dest == NULL) {
107 return NULL;
110 crlbag = (sec_PKCS12CRLBag*)src_or_dest;
112 oiddata = SECOID_FindOID(&crlbag->bagID);
113 if(oiddata == NULL) {
114 return SEC_ASN1_GET(SEC_AnyTemplate);
117 switch (oiddata->offset) {
118 default:
119 theTemplate = SEC_ASN1_GET(SEC_AnyTemplate);
120 break;
121 case SEC_OID_PKCS9_X509_CRL:
122 theTemplate = SEC_ASN1_GET(SEC_OctetStringTemplate);
123 break;
125 return theTemplate;
128 static const SEC_ASN1Template *
129 sec_pkcs12_choose_cert_bag_type(void *src_or_dest, PRBool encoding)
131 const SEC_ASN1Template *theTemplate;
132 sec_PKCS12CertBag *certbag;
133 SECOidData *oiddata;
135 if (src_or_dest == NULL) {
136 return NULL;
139 certbag = (sec_PKCS12CertBag*)src_or_dest;
141 oiddata = SECOID_FindOID(&certbag->bagID);
142 if(oiddata == NULL) {
143 return SEC_ASN1_GET(SEC_AnyTemplate);
146 switch (oiddata->offset) {
147 default:
148 theTemplate = SEC_ASN1_GET(SEC_AnyTemplate);
149 break;
150 case SEC_OID_PKCS9_X509_CERT:
151 theTemplate = SEC_ASN1_GET(SEC_OctetStringTemplate);
152 break;
153 case SEC_OID_PKCS9_SDSI_CERT:
154 theTemplate = SEC_ASN1_GET(SEC_IA5StringTemplate);
155 break;
157 return theTemplate;
160 static const SEC_ASN1Template *
161 sec_pkcs12_choose_attr_type(void *src_or_dest, PRBool encoding)
163 const SEC_ASN1Template *theTemplate;
164 sec_PKCS12Attribute *attr;
165 SECOidData *oiddata;
167 if (src_or_dest == NULL) {
168 return NULL;
171 attr = (sec_PKCS12Attribute*)src_or_dest;
173 oiddata = SECOID_FindOID(&attr->attrType);
174 if(oiddata == NULL) {
175 return SEC_ASN1_GET(SEC_AnyTemplate);
178 switch (oiddata->offset) {
179 default:
180 theTemplate = SEC_ASN1_GET(SEC_AnyTemplate);
181 break;
182 case SEC_OID_PKCS9_FRIENDLY_NAME:
183 theTemplate = SEC_ASN1_GET(SEC_BMPStringTemplate);
184 break;
185 case SEC_OID_PKCS9_LOCAL_KEY_ID:
186 theTemplate = SEC_ASN1_GET(SEC_OctetStringTemplate);
187 break;
188 case SEC_OID_PKCS12_KEY_USAGE:
189 theTemplate = SEC_ASN1_GET(SEC_BitStringTemplate);
190 break;
193 return theTemplate;
197 const SEC_ASN1Template sec_PKCS12PointerToContentInfoTemplate[] = {
198 { SEC_ASN1_POINTER | SEC_ASN1_MAY_STREAM, 0, sec_PKCS7ContentInfoTemplate }
201 static const SEC_ASN1TemplateChooserPtr sec_pkcs12_crl_bag_chooser =
202 sec_pkcs12_choose_crl_bag_type;
204 static const SEC_ASN1TemplateChooserPtr sec_pkcs12_cert_bag_chooser =
205 sec_pkcs12_choose_cert_bag_type;
207 static const SEC_ASN1TemplateChooserPtr sec_pkcs12_safe_bag_chooser =
208 sec_pkcs12_choose_safe_bag_type;
210 static const SEC_ASN1TemplateChooserPtr sec_pkcs12_attr_chooser =
211 sec_pkcs12_choose_attr_type;
213 const SEC_ASN1Template sec_PKCS12PointerToCertBagTemplate[] = {
214 { SEC_ASN1_POINTER, 0, sec_PKCS12CertBagTemplate }
217 const SEC_ASN1Template sec_PKCS12PointerToCRLBagTemplate[] = {
218 { SEC_ASN1_POINTER, 0, sec_PKCS12CRLBagTemplate }
221 const SEC_ASN1Template sec_PKCS12PointerToSecretBagTemplate[] = {
222 { SEC_ASN1_POINTER, 0, sec_PKCS12SecretBagTemplate }
225 const SEC_ASN1Template sec_PKCS12PointerToSafeContentsTemplate[] = {
226 { SEC_ASN1_POINTER, 0, sec_PKCS12SafeContentsTemplate }
229 const SEC_ASN1Template sec_PKCS12PFXItemTemplate[] = {
230 { SEC_ASN1_SEQUENCE | SEC_ASN1_MAY_STREAM, 0, NULL,
231 sizeof(sec_PKCS12PFXItem) },
232 { SEC_ASN1_OPTIONAL | SEC_ASN1_INTEGER,
233 offsetof(sec_PKCS12PFXItem, version) },
234 { SEC_ASN1_ANY | SEC_ASN1_MAY_STREAM,
235 offsetof(sec_PKCS12PFXItem, encodedAuthSafe) },
236 { SEC_ASN1_ANY | SEC_ASN1_MAY_STREAM,
237 offsetof(sec_PKCS12PFXItem, encodedMacData) },
238 { 0 }
241 const SEC_ASN1Template sec_PKCS12MacDataTemplate[] = {
242 { SEC_ASN1_SEQUENCE, 0, NULL, sizeof(sec_PKCS12MacData) },
243 { SEC_ASN1_INLINE | SEC_ASN1_XTRN , offsetof(sec_PKCS12MacData, safeMac),
244 SEC_ASN1_SUB(sgn_DigestInfoTemplate) },
245 { SEC_ASN1_OCTET_STRING, offsetof(sec_PKCS12MacData, macSalt) },
246 { SEC_ASN1_OPTIONAL | SEC_ASN1_INTEGER, offsetof(sec_PKCS12MacData, iter) },
247 { 0 }
250 const SEC_ASN1Template sec_PKCS12AuthenticatedSafeTemplate[] = {
251 { SEC_ASN1_SEQUENCE_OF | SEC_ASN1_MAY_STREAM | SEC_ASN1_XTRN ,
252 offsetof(sec_PKCS12AuthenticatedSafe, encodedSafes),
253 SEC_ASN1_SUB(SEC_AnyTemplate) }
256 const SEC_ASN1Template sec_PKCS12SafeBagTemplate[] = {
257 { SEC_ASN1_SEQUENCE | SEC_ASN1_MAY_STREAM, 0, NULL,
258 sizeof(sec_PKCS12SafeBag) },
259 { SEC_ASN1_OBJECT_ID, offsetof(sec_PKCS12SafeBag, safeBagType) },
260 { SEC_ASN1_EXPLICIT | SEC_ASN1_DYNAMIC | SEC_ASN1_CONSTRUCTED |
261 SEC_ASN1_MAY_STREAM | SEC_ASN1_CONTEXT_SPECIFIC | 0,
262 offsetof(sec_PKCS12SafeBag, safeBagContent),
263 &sec_pkcs12_safe_bag_chooser },
264 { SEC_ASN1_SET_OF | SEC_ASN1_OPTIONAL, offsetof(sec_PKCS12SafeBag, attribs),
265 sec_PKCS12AttributeTemplate },
266 { 0 }
269 const SEC_ASN1Template sec_PKCS12SafeContentsTemplate[] = {
270 { SEC_ASN1_SEQUENCE_OF | SEC_ASN1_MAY_STREAM,
271 offsetof(sec_PKCS12SafeContents, safeBags),
272 sec_PKCS12SafeBagTemplate }
275 const SEC_ASN1Template sec_PKCS12SequenceOfAnyTemplate[] = {
276 { SEC_ASN1_SEQUENCE_OF | SEC_ASN1_MAY_STREAM | SEC_ASN1_XTRN , 0,
277 SEC_ASN1_SUB(SEC_AnyTemplate) }
280 const SEC_ASN1Template sec_PKCS12NestedSafeContentsDecodeTemplate[] = {
281 { SEC_ASN1_EXPLICIT | SEC_ASN1_CONTEXT_SPECIFIC | SEC_ASN1_CONSTRUCTED | 0,
282 offsetof(sec_PKCS12SafeContents, encodedSafeBags),
283 sec_PKCS12SequenceOfAnyTemplate }
286 const SEC_ASN1Template sec_PKCS12SafeContentsDecodeTemplate[] = {
287 { SEC_ASN1_SEQUENCE_OF | SEC_ASN1_MAY_STREAM | SEC_ASN1_XTRN ,
288 offsetof(sec_PKCS12SafeContents, encodedSafeBags),
289 SEC_ASN1_SUB(SEC_AnyTemplate) }
292 const SEC_ASN1Template sec_PKCS12CRLBagTemplate[] = {
293 { SEC_ASN1_SEQUENCE, 0, NULL, sizeof(sec_PKCS12CRLBag) },
294 { SEC_ASN1_OBJECT_ID, offsetof(sec_PKCS12CRLBag, bagID) },
295 { SEC_ASN1_DYNAMIC | SEC_ASN1_POINTER,
296 offsetof(sec_PKCS12CRLBag, value), &sec_pkcs12_crl_bag_chooser },
297 { 0 }
300 const SEC_ASN1Template sec_PKCS12CertBagTemplate[] = {
301 { SEC_ASN1_SEQUENCE, 0, NULL, sizeof(sec_PKCS12CertBag) },
302 { SEC_ASN1_OBJECT_ID, offsetof(sec_PKCS12CertBag, bagID) },
303 { SEC_ASN1_DYNAMIC | SEC_ASN1_EXPLICIT | SEC_ASN1_CONSTRUCTED |
304 SEC_ASN1_CONTEXT_SPECIFIC | 0,
305 offsetof(sec_PKCS12CertBag, value), &sec_pkcs12_cert_bag_chooser },
306 { 0 }
309 const SEC_ASN1Template sec_PKCS12SecretBagTemplate[] = {
310 { SEC_ASN1_SEQUENCE, 0, NULL, sizeof(sec_PKCS12SecretBag) },
311 { SEC_ASN1_OBJECT_ID, offsetof(sec_PKCS12SecretBag, secretType) },
312 { SEC_ASN1_ANY, offsetof(sec_PKCS12SecretBag, secretContent) },
313 { 0 }
316 const SEC_ASN1Template sec_PKCS12AttributeTemplate[] = {
317 { SEC_ASN1_SEQUENCE, 0, NULL, sizeof(sec_PKCS12Attribute) },
318 { SEC_ASN1_OBJECT_ID, offsetof(sec_PKCS12Attribute, attrType) },
319 { SEC_ASN1_SET_OF | SEC_ASN1_DYNAMIC,
320 offsetof(sec_PKCS12Attribute, attrValue),
321 &sec_pkcs12_attr_chooser },
322 { 0 }