PageRenderTime 29ms CodeModel.GetById 9ms app.highlight 17ms RepoModel.GetById 1ms app.codeStats 0ms

/security/nss/lib/pkcs12/p12tmpl.c

http://github.com/zpao/v8monkey
C | 323 lines | 243 code | 45 blank | 35 comment | 22 complexity | dc9836abc084a911e03ee6857f419279 MD5 | raw file
  1/* ***** BEGIN LICENSE BLOCK *****
  2 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
  3 *
  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/
  8 *
  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.
 13 *
 14 * The Original Code is the Netscape security libraries.
 15 *
 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.
 20 *
 21 * Contributor(s):
 22 *
 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.
 34 *
 35 * ***** END LICENSE BLOCK ***** */
 36
 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"
 46
 47SEC_ASN1_MKSUB(SEC_AnyTemplate)
 48SEC_ASN1_MKSUB(sgn_DigestInfoTemplate)
 49
 50static const SEC_ASN1Template *
 51sec_pkcs12_choose_safe_bag_type(void *src_or_dest, PRBool encoding)
 52{
 53    const SEC_ASN1Template *theTemplate;
 54    sec_PKCS12SafeBag *safeBag;
 55    SECOidData *oiddata;
 56
 57    if (src_or_dest == NULL) {
 58	return NULL;
 59    }
 60
 61    safeBag = (sec_PKCS12SafeBag*)src_or_dest;
 62
 63    oiddata = SECOID_FindOID(&safeBag->safeBagType);
 64    if(oiddata == NULL) {
 65	return SEC_ASN1_GET(SEC_AnyTemplate);
 66    }
 67
 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);
 93	    }
 94	    break;
 95    }
 96    return theTemplate;
 97}
 98
 99static const SEC_ASN1Template *
100sec_pkcs12_choose_crl_bag_type(void *src_or_dest, PRBool encoding)
101{
102    const SEC_ASN1Template *theTemplate;
103    sec_PKCS12CRLBag *crlbag;
104    SECOidData *oiddata;
105
106    if (src_or_dest == NULL) {
107	return NULL;
108    }
109
110    crlbag = (sec_PKCS12CRLBag*)src_or_dest;
111
112    oiddata = SECOID_FindOID(&crlbag->bagID);
113    if(oiddata == NULL) {
114	return SEC_ASN1_GET(SEC_AnyTemplate);
115    }
116
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;
124    }
125    return theTemplate;
126}
127
128static const SEC_ASN1Template *
129sec_pkcs12_choose_cert_bag_type(void *src_or_dest, PRBool encoding)
130{
131    const SEC_ASN1Template *theTemplate;
132    sec_PKCS12CertBag *certbag;
133    SECOidData *oiddata;
134
135    if (src_or_dest == NULL) {
136	return NULL;
137    }
138
139    certbag = (sec_PKCS12CertBag*)src_or_dest;
140
141    oiddata = SECOID_FindOID(&certbag->bagID);
142    if(oiddata == NULL) {
143	return SEC_ASN1_GET(SEC_AnyTemplate);
144    }
145
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;
156    }
157    return theTemplate;
158}
159
160static const SEC_ASN1Template *
161sec_pkcs12_choose_attr_type(void *src_or_dest, PRBool encoding)
162{
163    const SEC_ASN1Template *theTemplate;
164    sec_PKCS12Attribute *attr;
165    SECOidData *oiddata;
166
167    if (src_or_dest == NULL) {
168	return NULL;
169    }
170
171    attr = (sec_PKCS12Attribute*)src_or_dest;
172
173    oiddata = SECOID_FindOID(&attr->attrType);
174    if(oiddata == NULL) {
175	return SEC_ASN1_GET(SEC_AnyTemplate);
176    }
177
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;
191    }
192
193    return theTemplate;
194}
195
196
197const SEC_ASN1Template sec_PKCS12PointerToContentInfoTemplate[] = {
198    { SEC_ASN1_POINTER | SEC_ASN1_MAY_STREAM, 0, sec_PKCS7ContentInfoTemplate }
199};
200
201static const SEC_ASN1TemplateChooserPtr sec_pkcs12_crl_bag_chooser =
202    sec_pkcs12_choose_crl_bag_type;
203
204static const SEC_ASN1TemplateChooserPtr sec_pkcs12_cert_bag_chooser =
205    sec_pkcs12_choose_cert_bag_type;
206
207static const SEC_ASN1TemplateChooserPtr sec_pkcs12_safe_bag_chooser =
208    sec_pkcs12_choose_safe_bag_type;
209
210static const SEC_ASN1TemplateChooserPtr sec_pkcs12_attr_chooser =
211    sec_pkcs12_choose_attr_type;
212
213const SEC_ASN1Template sec_PKCS12PointerToCertBagTemplate[] = {
214    { SEC_ASN1_POINTER, 0, sec_PKCS12CertBagTemplate }
215};
216
217const SEC_ASN1Template sec_PKCS12PointerToCRLBagTemplate[] = {
218    { SEC_ASN1_POINTER, 0, sec_PKCS12CRLBagTemplate }
219};
220
221const SEC_ASN1Template sec_PKCS12PointerToSecretBagTemplate[] = {
222    { SEC_ASN1_POINTER, 0, sec_PKCS12SecretBagTemplate }
223};
224
225const SEC_ASN1Template sec_PKCS12PointerToSafeContentsTemplate[] = {
226    { SEC_ASN1_POINTER, 0, sec_PKCS12SafeContentsTemplate }
227};
228
229const 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 }
239};
240
241const 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 }
248};
249
250const 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) }
254};
255
256const 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 }
267};
268
269const SEC_ASN1Template sec_PKCS12SafeContentsTemplate[] = {
270    { SEC_ASN1_SEQUENCE_OF | SEC_ASN1_MAY_STREAM, 
271	offsetof(sec_PKCS12SafeContents, safeBags),
272	sec_PKCS12SafeBagTemplate }
273};
274
275const SEC_ASN1Template sec_PKCS12SequenceOfAnyTemplate[] = {
276    { SEC_ASN1_SEQUENCE_OF | SEC_ASN1_MAY_STREAM | SEC_ASN1_XTRN , 0,
277	SEC_ASN1_SUB(SEC_AnyTemplate) }
278};
279
280const SEC_ASN1Template sec_PKCS12NestedSafeContentsDecodeTemplate[] = {
281    { SEC_ASN1_EXPLICIT | SEC_ASN1_CONTEXT_SPECIFIC | SEC_ASN1_CONSTRUCTED | 0,
282	offsetof(sec_PKCS12SafeContents, encodedSafeBags),
283	sec_PKCS12SequenceOfAnyTemplate }
284};
285
286const 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) }
290};
291
292const 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 }
298};
299
300const 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 }
307};
308
309const 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 }
314};
315
316const 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 }
323};