Back to index

lightning-sunbird  0.9+nobinonly
p12tmpl.c
Go to the documentation of this file.
00001 /* ***** BEGIN LICENSE BLOCK *****
00002  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00003  *
00004  * The contents of this file are subject to the Mozilla Public License Version
00005  * 1.1 (the "License"); you may not use this file except in compliance with
00006  * the License. You may obtain a copy of the License at
00007  * http://www.mozilla.org/MPL/
00008  *
00009  * Software distributed under the License is distributed on an "AS IS" basis,
00010  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00011  * for the specific language governing rights and limitations under the
00012  * License.
00013  *
00014  * The Original Code is the Netscape security libraries.
00015  *
00016  * The Initial Developer of the Original Code is
00017  * Netscape Communications Corporation.
00018  * Portions created by the Initial Developer are Copyright (C) 1994-2000
00019  * the Initial Developer. All Rights Reserved.
00020  *
00021  * Contributor(s):
00022  *
00023  * Alternatively, the contents of this file may be used under the terms of
00024  * either the GNU General Public License Version 2 or later (the "GPL"), or
00025  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00026  * in which case the provisions of the GPL or the LGPL are applicable instead
00027  * of those above. If you wish to allow use of your version of this file only
00028  * under the terms of either the GPL or the LGPL, and not to allow others to
00029  * use your version of this file under the terms of the MPL, indicate your
00030  * decision by deleting the provisions above and replace them with the notice
00031  * and other provisions required by the GPL or the LGPL. If you do not delete
00032  * the provisions above, a recipient may use your version of this file under
00033  * the terms of any one of the MPL, the GPL or the LGPL.
00034  *
00035  * ***** END LICENSE BLOCK ***** */
00036 
00037 #include "plarena.h"
00038 #include "secitem.h"
00039 #include "secoid.h"
00040 #include "seccomon.h"
00041 #include "secport.h"
00042 #include "cert.h"
00043 #include "secpkcs7.h"
00044 #include "secasn1.h"
00045 #include "p12t.h"
00046 
00047 SEC_ASN1_MKSUB(SEC_AnyTemplate)
00048 SEC_ASN1_MKSUB(sgn_DigestInfoTemplate)
00049 
00050 static const SEC_ASN1Template *
00051 sec_pkcs12_choose_safe_bag_type(void *src_or_dest, PRBool encoding)
00052 {
00053     const SEC_ASN1Template *theTemplate;
00054     sec_PKCS12SafeBag *safeBag;
00055     SECOidData *oiddata;
00056 
00057     if (src_or_dest == NULL) {
00058        return NULL;
00059     }
00060 
00061     safeBag = (sec_PKCS12SafeBag*)src_or_dest;
00062 
00063     oiddata = SECOID_FindOID(&safeBag->safeBagType);
00064     if(oiddata == NULL) {
00065        return SEC_ASN1_GET(SEC_AnyTemplate);
00066     }
00067 
00068     switch (oiddata->offset) {
00069        default:
00070            theTemplate = SEC_ASN1_GET(SEC_AnyTemplate);
00071            break;
00072        case SEC_OID_PKCS12_V1_KEY_BAG_ID:
00073            theTemplate = SEC_ASN1_GET(SECKEY_PointerToPrivateKeyInfoTemplate);
00074            break;
00075        case SEC_OID_PKCS12_V1_CERT_BAG_ID:
00076            theTemplate = sec_PKCS12PointerToCertBagTemplate;
00077            break;
00078        case SEC_OID_PKCS12_V1_CRL_BAG_ID:
00079            theTemplate = sec_PKCS12PointerToCRLBagTemplate;
00080            break;
00081         case SEC_OID_PKCS12_V1_SECRET_BAG_ID:
00082            theTemplate = sec_PKCS12PointerToSecretBagTemplate;
00083            break;
00084        case SEC_OID_PKCS12_V1_PKCS8_SHROUDED_KEY_BAG_ID:
00085            theTemplate = 
00086                SEC_ASN1_GET(SECKEY_PointerToEncryptedPrivateKeyInfoTemplate);
00087            break;
00088        case SEC_OID_PKCS12_V1_SAFE_CONTENTS_BAG_ID:
00089            if(encoding) {
00090               theTemplate = sec_PKCS12PointerToSafeContentsTemplate;
00091            } else {
00092               theTemplate = SEC_ASN1_GET(SEC_PointerToAnyTemplate);
00093            }
00094            break;
00095     }
00096     return theTemplate;
00097 }
00098 
00099 static const SEC_ASN1Template *
00100 sec_pkcs12_choose_crl_bag_type(void *src_or_dest, PRBool encoding)
00101 {
00102     const SEC_ASN1Template *theTemplate;
00103     sec_PKCS12CRLBag *crlbag;
00104     SECOidData *oiddata;
00105 
00106     if (src_or_dest == NULL) {
00107        return NULL;
00108     }
00109 
00110     crlbag = (sec_PKCS12CRLBag*)src_or_dest;
00111 
00112     oiddata = SECOID_FindOID(&crlbag->bagID);
00113     if(oiddata == NULL) {
00114        return SEC_ASN1_GET(SEC_AnyTemplate);
00115     }
00116 
00117     switch (oiddata->offset) {
00118        default:
00119            theTemplate = SEC_ASN1_GET(SEC_AnyTemplate);
00120            break;
00121        case SEC_OID_PKCS9_X509_CRL:
00122            theTemplate = SEC_ASN1_GET(SEC_OctetStringTemplate);
00123            break;
00124     }
00125     return theTemplate;
00126 }
00127 
00128 static const SEC_ASN1Template *
00129 sec_pkcs12_choose_cert_bag_type(void *src_or_dest, PRBool encoding)
00130 {
00131     const SEC_ASN1Template *theTemplate;
00132     sec_PKCS12CertBag *certbag;
00133     SECOidData *oiddata;
00134 
00135     if (src_or_dest == NULL) {
00136        return NULL;
00137     }
00138 
00139     certbag = (sec_PKCS12CertBag*)src_or_dest;
00140 
00141     oiddata = SECOID_FindOID(&certbag->bagID);
00142     if(oiddata == NULL) {
00143        return SEC_ASN1_GET(SEC_AnyTemplate);
00144     }
00145 
00146     switch (oiddata->offset) {
00147        default:
00148            theTemplate = SEC_ASN1_GET(SEC_AnyTemplate);
00149            break;
00150        case SEC_OID_PKCS9_X509_CERT:
00151            theTemplate = SEC_ASN1_GET(SEC_OctetStringTemplate);
00152            break;
00153        case SEC_OID_PKCS9_SDSI_CERT:
00154            theTemplate = SEC_ASN1_GET(SEC_IA5StringTemplate);
00155            break;
00156     }
00157     return theTemplate;
00158 }
00159 
00160 static const SEC_ASN1Template *
00161 sec_pkcs12_choose_attr_type(void *src_or_dest, PRBool encoding)
00162 {
00163     const SEC_ASN1Template *theTemplate;
00164     sec_PKCS12Attribute *attr;
00165     SECOidData *oiddata;
00166 
00167     if (src_or_dest == NULL) {
00168        return NULL;
00169     }
00170 
00171     attr = (sec_PKCS12Attribute*)src_or_dest;
00172 
00173     oiddata = SECOID_FindOID(&attr->attrType);
00174     if(oiddata == NULL) {
00175        return SEC_ASN1_GET(SEC_AnyTemplate);
00176     }
00177 
00178     switch (oiddata->offset) {
00179        default:
00180            theTemplate = SEC_ASN1_GET(SEC_AnyTemplate);
00181            break;
00182        case SEC_OID_PKCS9_FRIENDLY_NAME:
00183            theTemplate = SEC_ASN1_GET(SEC_BMPStringTemplate);
00184            break;
00185        case SEC_OID_PKCS9_LOCAL_KEY_ID:
00186            theTemplate = SEC_ASN1_GET(SEC_OctetStringTemplate);
00187            break;
00188        case SEC_OID_PKCS12_KEY_USAGE:
00189            theTemplate = SEC_ASN1_GET(SEC_BitStringTemplate);
00190            break;
00191     }
00192 
00193     return theTemplate;
00194 }
00195 
00196 
00197 const SEC_ASN1Template sec_PKCS12PointerToContentInfoTemplate[] = {
00198     { SEC_ASN1_POINTER | SEC_ASN1_MAY_STREAM, 0, sec_PKCS7ContentInfoTemplate }
00199 };
00200 
00201 static const SEC_ASN1TemplateChooserPtr sec_pkcs12_crl_bag_chooser =
00202     sec_pkcs12_choose_crl_bag_type;
00203 
00204 static const SEC_ASN1TemplateChooserPtr sec_pkcs12_cert_bag_chooser =
00205     sec_pkcs12_choose_cert_bag_type;
00206 
00207 static const SEC_ASN1TemplateChooserPtr sec_pkcs12_safe_bag_chooser =
00208     sec_pkcs12_choose_safe_bag_type;
00209 
00210 static const SEC_ASN1TemplateChooserPtr sec_pkcs12_attr_chooser =
00211     sec_pkcs12_choose_attr_type;
00212 
00213 const SEC_ASN1Template sec_PKCS12PointerToCertBagTemplate[] = {
00214     { SEC_ASN1_POINTER, 0, sec_PKCS12CertBagTemplate }
00215 };
00216 
00217 const SEC_ASN1Template sec_PKCS12PointerToCRLBagTemplate[] = {
00218     { SEC_ASN1_POINTER, 0, sec_PKCS12CRLBagTemplate }
00219 };
00220 
00221 const SEC_ASN1Template sec_PKCS12PointerToSecretBagTemplate[] = {
00222     { SEC_ASN1_POINTER, 0, sec_PKCS12SecretBagTemplate }
00223 };
00224 
00225 const SEC_ASN1Template sec_PKCS12PointerToSafeContentsTemplate[] = {
00226     { SEC_ASN1_POINTER, 0, sec_PKCS12SafeContentsTemplate }
00227 };
00228 
00229 const SEC_ASN1Template sec_PKCS12PFXItemTemplate[] = {
00230     { SEC_ASN1_SEQUENCE | SEC_ASN1_MAY_STREAM, 0, NULL, 
00231        sizeof(sec_PKCS12PFXItem) },
00232     { SEC_ASN1_OPTIONAL | SEC_ASN1_INTEGER, 
00233        offsetof(sec_PKCS12PFXItem, version) },
00234     { SEC_ASN1_ANY | SEC_ASN1_MAY_STREAM, 
00235        offsetof(sec_PKCS12PFXItem, encodedAuthSafe) },
00236     { SEC_ASN1_ANY | SEC_ASN1_MAY_STREAM,
00237        offsetof(sec_PKCS12PFXItem, encodedMacData) },
00238     { 0 }
00239 };
00240 
00241 const SEC_ASN1Template sec_PKCS12MacDataTemplate[] = {
00242     { SEC_ASN1_SEQUENCE, 0, NULL, sizeof(sec_PKCS12MacData) },
00243     { SEC_ASN1_INLINE | SEC_ASN1_XTRN , offsetof(sec_PKCS12MacData, safeMac),
00244        SEC_ASN1_SUB(sgn_DigestInfoTemplate) },
00245     { SEC_ASN1_OCTET_STRING, offsetof(sec_PKCS12MacData, macSalt) },
00246     { SEC_ASN1_OPTIONAL | SEC_ASN1_INTEGER, offsetof(sec_PKCS12MacData, iter) },
00247     { 0 }
00248 };
00249 
00250 const SEC_ASN1Template sec_PKCS12AuthenticatedSafeTemplate[] = {
00251     { SEC_ASN1_SEQUENCE_OF | SEC_ASN1_MAY_STREAM | SEC_ASN1_XTRN , 
00252        offsetof(sec_PKCS12AuthenticatedSafe, encodedSafes), 
00253        SEC_ASN1_SUB(SEC_AnyTemplate) }
00254 };
00255 
00256 const SEC_ASN1Template sec_PKCS12SafeBagTemplate[] = {
00257     { SEC_ASN1_SEQUENCE | SEC_ASN1_MAY_STREAM, 0, NULL, 
00258        sizeof(sec_PKCS12SafeBag) },
00259     { SEC_ASN1_OBJECT_ID, offsetof(sec_PKCS12SafeBag, safeBagType) },
00260     { SEC_ASN1_EXPLICIT | SEC_ASN1_DYNAMIC | SEC_ASN1_CONSTRUCTED |
00261        SEC_ASN1_MAY_STREAM | SEC_ASN1_CONTEXT_SPECIFIC | 0, 
00262        offsetof(sec_PKCS12SafeBag, safeBagContent), 
00263        &sec_pkcs12_safe_bag_chooser },
00264     { SEC_ASN1_SET_OF | SEC_ASN1_OPTIONAL, offsetof(sec_PKCS12SafeBag, attribs),
00265        sec_PKCS12AttributeTemplate },
00266     { 0 }
00267 };
00268 
00269 const SEC_ASN1Template sec_PKCS12SafeContentsTemplate[] = {
00270     { SEC_ASN1_SEQUENCE_OF | SEC_ASN1_MAY_STREAM, 
00271        offsetof(sec_PKCS12SafeContents, safeBags),
00272        sec_PKCS12SafeBagTemplate }
00273 };
00274 
00275 const SEC_ASN1Template sec_PKCS12SequenceOfAnyTemplate[] = {
00276     { SEC_ASN1_SEQUENCE_OF | SEC_ASN1_MAY_STREAM | SEC_ASN1_XTRN , 0,
00277        SEC_ASN1_SUB(SEC_AnyTemplate) }
00278 };
00279 
00280 const SEC_ASN1Template sec_PKCS12NestedSafeContentsDecodeTemplate[] = {
00281     { SEC_ASN1_EXPLICIT | SEC_ASN1_CONTEXT_SPECIFIC | SEC_ASN1_CONSTRUCTED | 0,
00282        offsetof(sec_PKCS12SafeContents, encodedSafeBags),
00283        sec_PKCS12SequenceOfAnyTemplate }
00284 };
00285 
00286 const SEC_ASN1Template sec_PKCS12SafeContentsDecodeTemplate[] = {
00287     { SEC_ASN1_SEQUENCE_OF | SEC_ASN1_MAY_STREAM | SEC_ASN1_XTRN , 
00288        offsetof(sec_PKCS12SafeContents, encodedSafeBags),
00289        SEC_ASN1_SUB(SEC_AnyTemplate) }
00290 };
00291 
00292 const SEC_ASN1Template sec_PKCS12CRLBagTemplate[] = {
00293     { SEC_ASN1_SEQUENCE, 0, NULL, sizeof(sec_PKCS12CRLBag) },
00294     { SEC_ASN1_OBJECT_ID, offsetof(sec_PKCS12CRLBag, bagID) },
00295     { SEC_ASN1_DYNAMIC | SEC_ASN1_POINTER, 
00296        offsetof(sec_PKCS12CRLBag, value), &sec_pkcs12_crl_bag_chooser },
00297     { 0 }
00298 };
00299 
00300 const SEC_ASN1Template sec_PKCS12CertBagTemplate[] = {
00301     { SEC_ASN1_SEQUENCE, 0, NULL, sizeof(sec_PKCS12CertBag) },
00302     { SEC_ASN1_OBJECT_ID, offsetof(sec_PKCS12CertBag, bagID) },
00303     { SEC_ASN1_DYNAMIC | SEC_ASN1_EXPLICIT | SEC_ASN1_CONSTRUCTED |
00304        SEC_ASN1_CONTEXT_SPECIFIC | 0,
00305        offsetof(sec_PKCS12CertBag, value), &sec_pkcs12_cert_bag_chooser },
00306     { 0 }
00307 };
00308 
00309 const SEC_ASN1Template sec_PKCS12SecretBagTemplate[] = {
00310     { SEC_ASN1_SEQUENCE, 0, NULL, sizeof(sec_PKCS12SecretBag) },
00311     { SEC_ASN1_OBJECT_ID, offsetof(sec_PKCS12SecretBag, secretType) },
00312     { SEC_ASN1_ANY, offsetof(sec_PKCS12SecretBag, secretContent) },
00313     { 0 }
00314 };
00315 
00316 const SEC_ASN1Template sec_PKCS12AttributeTemplate[] = {
00317     { SEC_ASN1_SEQUENCE, 0, NULL, sizeof(sec_PKCS12Attribute) },
00318     { SEC_ASN1_OBJECT_ID, offsetof(sec_PKCS12Attribute, attrType) },
00319     { SEC_ASN1_SET_OF | SEC_ASN1_DYNAMIC, 
00320        offsetof(sec_PKCS12Attribute, attrValue),
00321        &sec_pkcs12_attr_chooser },
00322     { 0 }
00323 };