michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #include "plarena.h" michael@0: #include "secitem.h" michael@0: #include "secoid.h" michael@0: #include "seccomon.h" michael@0: #include "secport.h" michael@0: #include "cert.h" michael@0: #include "secpkcs7.h" michael@0: #include "secasn1.h" michael@0: #include "p12t.h" michael@0: michael@0: SEC_ASN1_MKSUB(SEC_AnyTemplate) michael@0: SEC_ASN1_MKSUB(sgn_DigestInfoTemplate) michael@0: michael@0: static const SEC_ASN1Template * michael@0: sec_pkcs12_choose_safe_bag_type(void *src_or_dest, PRBool encoding) michael@0: { michael@0: const SEC_ASN1Template *theTemplate; michael@0: sec_PKCS12SafeBag *safeBag; michael@0: SECOidData *oiddata; michael@0: michael@0: if (src_or_dest == NULL) { michael@0: return NULL; michael@0: } michael@0: michael@0: safeBag = (sec_PKCS12SafeBag*)src_or_dest; michael@0: michael@0: oiddata = SECOID_FindOID(&safeBag->safeBagType); michael@0: if(oiddata == NULL) { michael@0: return SEC_ASN1_GET(SEC_AnyTemplate); michael@0: } michael@0: michael@0: switch (oiddata->offset) { michael@0: default: michael@0: theTemplate = SEC_ASN1_GET(SEC_AnyTemplate); michael@0: break; michael@0: case SEC_OID_PKCS12_V1_KEY_BAG_ID: michael@0: theTemplate = SEC_ASN1_GET(SECKEY_PointerToPrivateKeyInfoTemplate); michael@0: break; michael@0: case SEC_OID_PKCS12_V1_CERT_BAG_ID: michael@0: theTemplate = sec_PKCS12PointerToCertBagTemplate; michael@0: break; michael@0: case SEC_OID_PKCS12_V1_CRL_BAG_ID: michael@0: theTemplate = sec_PKCS12PointerToCRLBagTemplate; michael@0: break; michael@0: case SEC_OID_PKCS12_V1_SECRET_BAG_ID: michael@0: theTemplate = sec_PKCS12PointerToSecretBagTemplate; michael@0: break; michael@0: case SEC_OID_PKCS12_V1_PKCS8_SHROUDED_KEY_BAG_ID: michael@0: theTemplate = michael@0: SEC_ASN1_GET(SECKEY_PointerToEncryptedPrivateKeyInfoTemplate); michael@0: break; michael@0: case SEC_OID_PKCS12_V1_SAFE_CONTENTS_BAG_ID: michael@0: if(encoding) { michael@0: theTemplate = sec_PKCS12PointerToSafeContentsTemplate; michael@0: } else { michael@0: theTemplate = SEC_ASN1_GET(SEC_PointerToAnyTemplate); michael@0: } michael@0: break; michael@0: } michael@0: return theTemplate; michael@0: } michael@0: michael@0: static const SEC_ASN1Template * michael@0: sec_pkcs12_choose_crl_bag_type(void *src_or_dest, PRBool encoding) michael@0: { michael@0: const SEC_ASN1Template *theTemplate; michael@0: sec_PKCS12CRLBag *crlbag; michael@0: SECOidData *oiddata; michael@0: michael@0: if (src_or_dest == NULL) { michael@0: return NULL; michael@0: } michael@0: michael@0: crlbag = (sec_PKCS12CRLBag*)src_or_dest; michael@0: michael@0: oiddata = SECOID_FindOID(&crlbag->bagID); michael@0: if(oiddata == NULL) { michael@0: return SEC_ASN1_GET(SEC_AnyTemplate); michael@0: } michael@0: michael@0: switch (oiddata->offset) { michael@0: default: michael@0: theTemplate = SEC_ASN1_GET(SEC_AnyTemplate); michael@0: break; michael@0: case SEC_OID_PKCS9_X509_CRL: michael@0: theTemplate = SEC_ASN1_GET(SEC_OctetStringTemplate); michael@0: break; michael@0: } michael@0: return theTemplate; michael@0: } michael@0: michael@0: static const SEC_ASN1Template * michael@0: sec_pkcs12_choose_cert_bag_type(void *src_or_dest, PRBool encoding) michael@0: { michael@0: const SEC_ASN1Template *theTemplate; michael@0: sec_PKCS12CertBag *certbag; michael@0: SECOidData *oiddata; michael@0: michael@0: if (src_or_dest == NULL) { michael@0: return NULL; michael@0: } michael@0: michael@0: certbag = (sec_PKCS12CertBag*)src_or_dest; michael@0: michael@0: oiddata = SECOID_FindOID(&certbag->bagID); michael@0: if(oiddata == NULL) { michael@0: return SEC_ASN1_GET(SEC_AnyTemplate); michael@0: } michael@0: michael@0: switch (oiddata->offset) { michael@0: default: michael@0: theTemplate = SEC_ASN1_GET(SEC_AnyTemplate); michael@0: break; michael@0: case SEC_OID_PKCS9_X509_CERT: michael@0: theTemplate = SEC_ASN1_GET(SEC_OctetStringTemplate); michael@0: break; michael@0: case SEC_OID_PKCS9_SDSI_CERT: michael@0: theTemplate = SEC_ASN1_GET(SEC_IA5StringTemplate); michael@0: break; michael@0: } michael@0: return theTemplate; michael@0: } michael@0: michael@0: static const SEC_ASN1Template * michael@0: sec_pkcs12_choose_attr_type(void *src_or_dest, PRBool encoding) michael@0: { michael@0: const SEC_ASN1Template *theTemplate; michael@0: sec_PKCS12Attribute *attr; michael@0: SECOidData *oiddata; michael@0: michael@0: if (src_or_dest == NULL) { michael@0: return NULL; michael@0: } michael@0: michael@0: attr = (sec_PKCS12Attribute*)src_or_dest; michael@0: michael@0: oiddata = SECOID_FindOID(&attr->attrType); michael@0: if(oiddata == NULL) { michael@0: return SEC_ASN1_GET(SEC_AnyTemplate); michael@0: } michael@0: michael@0: switch (oiddata->offset) { michael@0: default: michael@0: theTemplate = SEC_ASN1_GET(SEC_AnyTemplate); michael@0: break; michael@0: case SEC_OID_PKCS9_FRIENDLY_NAME: michael@0: theTemplate = SEC_ASN1_GET(SEC_BMPStringTemplate); michael@0: break; michael@0: case SEC_OID_PKCS9_LOCAL_KEY_ID: michael@0: theTemplate = SEC_ASN1_GET(SEC_OctetStringTemplate); michael@0: break; michael@0: case SEC_OID_PKCS12_KEY_USAGE: michael@0: theTemplate = SEC_ASN1_GET(SEC_BitStringTemplate); michael@0: break; michael@0: } michael@0: michael@0: return theTemplate; michael@0: } michael@0: michael@0: michael@0: const SEC_ASN1Template sec_PKCS12PointerToContentInfoTemplate[] = { michael@0: { SEC_ASN1_POINTER | SEC_ASN1_MAY_STREAM, 0, sec_PKCS7ContentInfoTemplate } michael@0: }; michael@0: michael@0: static const SEC_ASN1TemplateChooserPtr sec_pkcs12_crl_bag_chooser = michael@0: sec_pkcs12_choose_crl_bag_type; michael@0: michael@0: static const SEC_ASN1TemplateChooserPtr sec_pkcs12_cert_bag_chooser = michael@0: sec_pkcs12_choose_cert_bag_type; michael@0: michael@0: static const SEC_ASN1TemplateChooserPtr sec_pkcs12_safe_bag_chooser = michael@0: sec_pkcs12_choose_safe_bag_type; michael@0: michael@0: static const SEC_ASN1TemplateChooserPtr sec_pkcs12_attr_chooser = michael@0: sec_pkcs12_choose_attr_type; michael@0: michael@0: const SEC_ASN1Template sec_PKCS12PointerToCertBagTemplate[] = { michael@0: { SEC_ASN1_POINTER, 0, sec_PKCS12CertBagTemplate } michael@0: }; michael@0: michael@0: const SEC_ASN1Template sec_PKCS12PointerToCRLBagTemplate[] = { michael@0: { SEC_ASN1_POINTER, 0, sec_PKCS12CRLBagTemplate } michael@0: }; michael@0: michael@0: const SEC_ASN1Template sec_PKCS12PointerToSecretBagTemplate[] = { michael@0: { SEC_ASN1_POINTER, 0, sec_PKCS12SecretBagTemplate } michael@0: }; michael@0: michael@0: const SEC_ASN1Template sec_PKCS12PointerToSafeContentsTemplate[] = { michael@0: { SEC_ASN1_POINTER, 0, sec_PKCS12SafeContentsTemplate } michael@0: }; michael@0: michael@0: const SEC_ASN1Template sec_PKCS12PFXItemTemplate[] = { michael@0: { SEC_ASN1_SEQUENCE | SEC_ASN1_MAY_STREAM, 0, NULL, michael@0: sizeof(sec_PKCS12PFXItem) }, michael@0: { SEC_ASN1_OPTIONAL | SEC_ASN1_INTEGER, michael@0: offsetof(sec_PKCS12PFXItem, version) }, michael@0: { SEC_ASN1_ANY | SEC_ASN1_MAY_STREAM, michael@0: offsetof(sec_PKCS12PFXItem, encodedAuthSafe) }, michael@0: { SEC_ASN1_ANY | SEC_ASN1_MAY_STREAM, michael@0: offsetof(sec_PKCS12PFXItem, encodedMacData) }, michael@0: { 0 } michael@0: }; michael@0: michael@0: const SEC_ASN1Template sec_PKCS12MacDataTemplate[] = { michael@0: { SEC_ASN1_SEQUENCE, 0, NULL, sizeof(sec_PKCS12MacData) }, michael@0: { SEC_ASN1_INLINE | SEC_ASN1_XTRN , offsetof(sec_PKCS12MacData, safeMac), michael@0: SEC_ASN1_SUB(sgn_DigestInfoTemplate) }, michael@0: { SEC_ASN1_OCTET_STRING, offsetof(sec_PKCS12MacData, macSalt) }, michael@0: { SEC_ASN1_OPTIONAL | SEC_ASN1_INTEGER, offsetof(sec_PKCS12MacData, iter) }, michael@0: { 0 } michael@0: }; michael@0: michael@0: const SEC_ASN1Template sec_PKCS12AuthenticatedSafeTemplate[] = { michael@0: { SEC_ASN1_SEQUENCE_OF | SEC_ASN1_MAY_STREAM | SEC_ASN1_XTRN , michael@0: offsetof(sec_PKCS12AuthenticatedSafe, encodedSafes), michael@0: SEC_ASN1_SUB(SEC_AnyTemplate) } michael@0: }; michael@0: michael@0: const SEC_ASN1Template sec_PKCS12SafeBagTemplate[] = { michael@0: { SEC_ASN1_SEQUENCE | SEC_ASN1_MAY_STREAM, 0, NULL, michael@0: sizeof(sec_PKCS12SafeBag) }, michael@0: { SEC_ASN1_OBJECT_ID, offsetof(sec_PKCS12SafeBag, safeBagType) }, michael@0: { SEC_ASN1_EXPLICIT | SEC_ASN1_DYNAMIC | SEC_ASN1_CONSTRUCTED | michael@0: SEC_ASN1_MAY_STREAM | SEC_ASN1_CONTEXT_SPECIFIC | 0, michael@0: offsetof(sec_PKCS12SafeBag, safeBagContent), michael@0: &sec_pkcs12_safe_bag_chooser }, michael@0: { SEC_ASN1_SET_OF | SEC_ASN1_OPTIONAL, offsetof(sec_PKCS12SafeBag, attribs), michael@0: sec_PKCS12AttributeTemplate }, michael@0: { 0 } michael@0: }; michael@0: michael@0: const SEC_ASN1Template sec_PKCS12SafeContentsTemplate[] = { michael@0: { SEC_ASN1_SEQUENCE_OF | SEC_ASN1_MAY_STREAM, michael@0: offsetof(sec_PKCS12SafeContents, safeBags), michael@0: sec_PKCS12SafeBagTemplate } michael@0: }; michael@0: michael@0: const SEC_ASN1Template sec_PKCS12SequenceOfAnyTemplate[] = { michael@0: { SEC_ASN1_SEQUENCE_OF | SEC_ASN1_MAY_STREAM | SEC_ASN1_XTRN , 0, michael@0: SEC_ASN1_SUB(SEC_AnyTemplate) } michael@0: }; michael@0: michael@0: const SEC_ASN1Template sec_PKCS12NestedSafeContentsDecodeTemplate[] = { michael@0: { SEC_ASN1_EXPLICIT | SEC_ASN1_CONTEXT_SPECIFIC | SEC_ASN1_CONSTRUCTED | 0, michael@0: offsetof(sec_PKCS12SafeContents, encodedSafeBags), michael@0: sec_PKCS12SequenceOfAnyTemplate } michael@0: }; michael@0: michael@0: const SEC_ASN1Template sec_PKCS12SafeContentsDecodeTemplate[] = { michael@0: { SEC_ASN1_SEQUENCE_OF | SEC_ASN1_MAY_STREAM | SEC_ASN1_XTRN , michael@0: offsetof(sec_PKCS12SafeContents, encodedSafeBags), michael@0: SEC_ASN1_SUB(SEC_AnyTemplate) } michael@0: }; michael@0: michael@0: const SEC_ASN1Template sec_PKCS12CRLBagTemplate[] = { michael@0: { SEC_ASN1_SEQUENCE, 0, NULL, sizeof(sec_PKCS12CRLBag) }, michael@0: { SEC_ASN1_OBJECT_ID, offsetof(sec_PKCS12CRLBag, bagID) }, michael@0: { SEC_ASN1_DYNAMIC | SEC_ASN1_POINTER, michael@0: offsetof(sec_PKCS12CRLBag, value), &sec_pkcs12_crl_bag_chooser }, michael@0: { 0 } michael@0: }; michael@0: michael@0: const SEC_ASN1Template sec_PKCS12CertBagTemplate[] = { michael@0: { SEC_ASN1_SEQUENCE, 0, NULL, sizeof(sec_PKCS12CertBag) }, michael@0: { SEC_ASN1_OBJECT_ID, offsetof(sec_PKCS12CertBag, bagID) }, michael@0: { SEC_ASN1_DYNAMIC | SEC_ASN1_EXPLICIT | SEC_ASN1_CONSTRUCTED | michael@0: SEC_ASN1_CONTEXT_SPECIFIC | 0, michael@0: offsetof(sec_PKCS12CertBag, value), &sec_pkcs12_cert_bag_chooser }, michael@0: { 0 } michael@0: }; michael@0: michael@0: const SEC_ASN1Template sec_PKCS12SecretBagTemplate[] = { michael@0: { SEC_ASN1_SEQUENCE, 0, NULL, sizeof(sec_PKCS12SecretBag) }, michael@0: { SEC_ASN1_OBJECT_ID, offsetof(sec_PKCS12SecretBag, secretType) }, michael@0: { SEC_ASN1_ANY, offsetof(sec_PKCS12SecretBag, secretContent) }, michael@0: { 0 } michael@0: }; michael@0: michael@0: const SEC_ASN1Template sec_PKCS12AttributeTemplate[] = { michael@0: { SEC_ASN1_SEQUENCE, 0, NULL, sizeof(sec_PKCS12Attribute) }, michael@0: { SEC_ASN1_OBJECT_ID, offsetof(sec_PKCS12Attribute, attrType) }, michael@0: { SEC_ASN1_SET_OF | SEC_ASN1_DYNAMIC, michael@0: offsetof(sec_PKCS12Attribute, attrValue), michael@0: &sec_pkcs12_attr_chooser }, michael@0: { 0 } michael@0: };