Wed, 31 Dec 2014 06:55:50 +0100
Added tag UPSTREAM_283F7C6 for changeset ca08bd8f51b2
michael@0 | 1 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 2 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 3 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 4 | |
michael@0 | 5 | #include "plarena.h" |
michael@0 | 6 | #include "secitem.h" |
michael@0 | 7 | #include "secoid.h" |
michael@0 | 8 | #include "seccomon.h" |
michael@0 | 9 | #include "secport.h" |
michael@0 | 10 | #include "cert.h" |
michael@0 | 11 | #include "secpkcs7.h" |
michael@0 | 12 | #include "secasn1.h" |
michael@0 | 13 | #include "p12t.h" |
michael@0 | 14 | |
michael@0 | 15 | SEC_ASN1_MKSUB(SEC_AnyTemplate) |
michael@0 | 16 | SEC_ASN1_MKSUB(sgn_DigestInfoTemplate) |
michael@0 | 17 | |
michael@0 | 18 | static const SEC_ASN1Template * |
michael@0 | 19 | sec_pkcs12_choose_safe_bag_type(void *src_or_dest, PRBool encoding) |
michael@0 | 20 | { |
michael@0 | 21 | const SEC_ASN1Template *theTemplate; |
michael@0 | 22 | sec_PKCS12SafeBag *safeBag; |
michael@0 | 23 | SECOidData *oiddata; |
michael@0 | 24 | |
michael@0 | 25 | if (src_or_dest == NULL) { |
michael@0 | 26 | return NULL; |
michael@0 | 27 | } |
michael@0 | 28 | |
michael@0 | 29 | safeBag = (sec_PKCS12SafeBag*)src_or_dest; |
michael@0 | 30 | |
michael@0 | 31 | oiddata = SECOID_FindOID(&safeBag->safeBagType); |
michael@0 | 32 | if(oiddata == NULL) { |
michael@0 | 33 | return SEC_ASN1_GET(SEC_AnyTemplate); |
michael@0 | 34 | } |
michael@0 | 35 | |
michael@0 | 36 | switch (oiddata->offset) { |
michael@0 | 37 | default: |
michael@0 | 38 | theTemplate = SEC_ASN1_GET(SEC_AnyTemplate); |
michael@0 | 39 | break; |
michael@0 | 40 | case SEC_OID_PKCS12_V1_KEY_BAG_ID: |
michael@0 | 41 | theTemplate = SEC_ASN1_GET(SECKEY_PointerToPrivateKeyInfoTemplate); |
michael@0 | 42 | break; |
michael@0 | 43 | case SEC_OID_PKCS12_V1_CERT_BAG_ID: |
michael@0 | 44 | theTemplate = sec_PKCS12PointerToCertBagTemplate; |
michael@0 | 45 | break; |
michael@0 | 46 | case SEC_OID_PKCS12_V1_CRL_BAG_ID: |
michael@0 | 47 | theTemplate = sec_PKCS12PointerToCRLBagTemplate; |
michael@0 | 48 | break; |
michael@0 | 49 | case SEC_OID_PKCS12_V1_SECRET_BAG_ID: |
michael@0 | 50 | theTemplate = sec_PKCS12PointerToSecretBagTemplate; |
michael@0 | 51 | break; |
michael@0 | 52 | case SEC_OID_PKCS12_V1_PKCS8_SHROUDED_KEY_BAG_ID: |
michael@0 | 53 | theTemplate = |
michael@0 | 54 | SEC_ASN1_GET(SECKEY_PointerToEncryptedPrivateKeyInfoTemplate); |
michael@0 | 55 | break; |
michael@0 | 56 | case SEC_OID_PKCS12_V1_SAFE_CONTENTS_BAG_ID: |
michael@0 | 57 | if(encoding) { |
michael@0 | 58 | theTemplate = sec_PKCS12PointerToSafeContentsTemplate; |
michael@0 | 59 | } else { |
michael@0 | 60 | theTemplate = SEC_ASN1_GET(SEC_PointerToAnyTemplate); |
michael@0 | 61 | } |
michael@0 | 62 | break; |
michael@0 | 63 | } |
michael@0 | 64 | return theTemplate; |
michael@0 | 65 | } |
michael@0 | 66 | |
michael@0 | 67 | static const SEC_ASN1Template * |
michael@0 | 68 | sec_pkcs12_choose_crl_bag_type(void *src_or_dest, PRBool encoding) |
michael@0 | 69 | { |
michael@0 | 70 | const SEC_ASN1Template *theTemplate; |
michael@0 | 71 | sec_PKCS12CRLBag *crlbag; |
michael@0 | 72 | SECOidData *oiddata; |
michael@0 | 73 | |
michael@0 | 74 | if (src_or_dest == NULL) { |
michael@0 | 75 | return NULL; |
michael@0 | 76 | } |
michael@0 | 77 | |
michael@0 | 78 | crlbag = (sec_PKCS12CRLBag*)src_or_dest; |
michael@0 | 79 | |
michael@0 | 80 | oiddata = SECOID_FindOID(&crlbag->bagID); |
michael@0 | 81 | if(oiddata == NULL) { |
michael@0 | 82 | return SEC_ASN1_GET(SEC_AnyTemplate); |
michael@0 | 83 | } |
michael@0 | 84 | |
michael@0 | 85 | switch (oiddata->offset) { |
michael@0 | 86 | default: |
michael@0 | 87 | theTemplate = SEC_ASN1_GET(SEC_AnyTemplate); |
michael@0 | 88 | break; |
michael@0 | 89 | case SEC_OID_PKCS9_X509_CRL: |
michael@0 | 90 | theTemplate = SEC_ASN1_GET(SEC_OctetStringTemplate); |
michael@0 | 91 | break; |
michael@0 | 92 | } |
michael@0 | 93 | return theTemplate; |
michael@0 | 94 | } |
michael@0 | 95 | |
michael@0 | 96 | static const SEC_ASN1Template * |
michael@0 | 97 | sec_pkcs12_choose_cert_bag_type(void *src_or_dest, PRBool encoding) |
michael@0 | 98 | { |
michael@0 | 99 | const SEC_ASN1Template *theTemplate; |
michael@0 | 100 | sec_PKCS12CertBag *certbag; |
michael@0 | 101 | SECOidData *oiddata; |
michael@0 | 102 | |
michael@0 | 103 | if (src_or_dest == NULL) { |
michael@0 | 104 | return NULL; |
michael@0 | 105 | } |
michael@0 | 106 | |
michael@0 | 107 | certbag = (sec_PKCS12CertBag*)src_or_dest; |
michael@0 | 108 | |
michael@0 | 109 | oiddata = SECOID_FindOID(&certbag->bagID); |
michael@0 | 110 | if(oiddata == NULL) { |
michael@0 | 111 | return SEC_ASN1_GET(SEC_AnyTemplate); |
michael@0 | 112 | } |
michael@0 | 113 | |
michael@0 | 114 | switch (oiddata->offset) { |
michael@0 | 115 | default: |
michael@0 | 116 | theTemplate = SEC_ASN1_GET(SEC_AnyTemplate); |
michael@0 | 117 | break; |
michael@0 | 118 | case SEC_OID_PKCS9_X509_CERT: |
michael@0 | 119 | theTemplate = SEC_ASN1_GET(SEC_OctetStringTemplate); |
michael@0 | 120 | break; |
michael@0 | 121 | case SEC_OID_PKCS9_SDSI_CERT: |
michael@0 | 122 | theTemplate = SEC_ASN1_GET(SEC_IA5StringTemplate); |
michael@0 | 123 | break; |
michael@0 | 124 | } |
michael@0 | 125 | return theTemplate; |
michael@0 | 126 | } |
michael@0 | 127 | |
michael@0 | 128 | static const SEC_ASN1Template * |
michael@0 | 129 | sec_pkcs12_choose_attr_type(void *src_or_dest, PRBool encoding) |
michael@0 | 130 | { |
michael@0 | 131 | const SEC_ASN1Template *theTemplate; |
michael@0 | 132 | sec_PKCS12Attribute *attr; |
michael@0 | 133 | SECOidData *oiddata; |
michael@0 | 134 | |
michael@0 | 135 | if (src_or_dest == NULL) { |
michael@0 | 136 | return NULL; |
michael@0 | 137 | } |
michael@0 | 138 | |
michael@0 | 139 | attr = (sec_PKCS12Attribute*)src_or_dest; |
michael@0 | 140 | |
michael@0 | 141 | oiddata = SECOID_FindOID(&attr->attrType); |
michael@0 | 142 | if(oiddata == NULL) { |
michael@0 | 143 | return SEC_ASN1_GET(SEC_AnyTemplate); |
michael@0 | 144 | } |
michael@0 | 145 | |
michael@0 | 146 | switch (oiddata->offset) { |
michael@0 | 147 | default: |
michael@0 | 148 | theTemplate = SEC_ASN1_GET(SEC_AnyTemplate); |
michael@0 | 149 | break; |
michael@0 | 150 | case SEC_OID_PKCS9_FRIENDLY_NAME: |
michael@0 | 151 | theTemplate = SEC_ASN1_GET(SEC_BMPStringTemplate); |
michael@0 | 152 | break; |
michael@0 | 153 | case SEC_OID_PKCS9_LOCAL_KEY_ID: |
michael@0 | 154 | theTemplate = SEC_ASN1_GET(SEC_OctetStringTemplate); |
michael@0 | 155 | break; |
michael@0 | 156 | case SEC_OID_PKCS12_KEY_USAGE: |
michael@0 | 157 | theTemplate = SEC_ASN1_GET(SEC_BitStringTemplate); |
michael@0 | 158 | break; |
michael@0 | 159 | } |
michael@0 | 160 | |
michael@0 | 161 | return theTemplate; |
michael@0 | 162 | } |
michael@0 | 163 | |
michael@0 | 164 | |
michael@0 | 165 | const SEC_ASN1Template sec_PKCS12PointerToContentInfoTemplate[] = { |
michael@0 | 166 | { SEC_ASN1_POINTER | SEC_ASN1_MAY_STREAM, 0, sec_PKCS7ContentInfoTemplate } |
michael@0 | 167 | }; |
michael@0 | 168 | |
michael@0 | 169 | static const SEC_ASN1TemplateChooserPtr sec_pkcs12_crl_bag_chooser = |
michael@0 | 170 | sec_pkcs12_choose_crl_bag_type; |
michael@0 | 171 | |
michael@0 | 172 | static const SEC_ASN1TemplateChooserPtr sec_pkcs12_cert_bag_chooser = |
michael@0 | 173 | sec_pkcs12_choose_cert_bag_type; |
michael@0 | 174 | |
michael@0 | 175 | static const SEC_ASN1TemplateChooserPtr sec_pkcs12_safe_bag_chooser = |
michael@0 | 176 | sec_pkcs12_choose_safe_bag_type; |
michael@0 | 177 | |
michael@0 | 178 | static const SEC_ASN1TemplateChooserPtr sec_pkcs12_attr_chooser = |
michael@0 | 179 | sec_pkcs12_choose_attr_type; |
michael@0 | 180 | |
michael@0 | 181 | const SEC_ASN1Template sec_PKCS12PointerToCertBagTemplate[] = { |
michael@0 | 182 | { SEC_ASN1_POINTER, 0, sec_PKCS12CertBagTemplate } |
michael@0 | 183 | }; |
michael@0 | 184 | |
michael@0 | 185 | const SEC_ASN1Template sec_PKCS12PointerToCRLBagTemplate[] = { |
michael@0 | 186 | { SEC_ASN1_POINTER, 0, sec_PKCS12CRLBagTemplate } |
michael@0 | 187 | }; |
michael@0 | 188 | |
michael@0 | 189 | const SEC_ASN1Template sec_PKCS12PointerToSecretBagTemplate[] = { |
michael@0 | 190 | { SEC_ASN1_POINTER, 0, sec_PKCS12SecretBagTemplate } |
michael@0 | 191 | }; |
michael@0 | 192 | |
michael@0 | 193 | const SEC_ASN1Template sec_PKCS12PointerToSafeContentsTemplate[] = { |
michael@0 | 194 | { SEC_ASN1_POINTER, 0, sec_PKCS12SafeContentsTemplate } |
michael@0 | 195 | }; |
michael@0 | 196 | |
michael@0 | 197 | const SEC_ASN1Template sec_PKCS12PFXItemTemplate[] = { |
michael@0 | 198 | { SEC_ASN1_SEQUENCE | SEC_ASN1_MAY_STREAM, 0, NULL, |
michael@0 | 199 | sizeof(sec_PKCS12PFXItem) }, |
michael@0 | 200 | { SEC_ASN1_OPTIONAL | SEC_ASN1_INTEGER, |
michael@0 | 201 | offsetof(sec_PKCS12PFXItem, version) }, |
michael@0 | 202 | { SEC_ASN1_ANY | SEC_ASN1_MAY_STREAM, |
michael@0 | 203 | offsetof(sec_PKCS12PFXItem, encodedAuthSafe) }, |
michael@0 | 204 | { SEC_ASN1_ANY | SEC_ASN1_MAY_STREAM, |
michael@0 | 205 | offsetof(sec_PKCS12PFXItem, encodedMacData) }, |
michael@0 | 206 | { 0 } |
michael@0 | 207 | }; |
michael@0 | 208 | |
michael@0 | 209 | const SEC_ASN1Template sec_PKCS12MacDataTemplate[] = { |
michael@0 | 210 | { SEC_ASN1_SEQUENCE, 0, NULL, sizeof(sec_PKCS12MacData) }, |
michael@0 | 211 | { SEC_ASN1_INLINE | SEC_ASN1_XTRN , offsetof(sec_PKCS12MacData, safeMac), |
michael@0 | 212 | SEC_ASN1_SUB(sgn_DigestInfoTemplate) }, |
michael@0 | 213 | { SEC_ASN1_OCTET_STRING, offsetof(sec_PKCS12MacData, macSalt) }, |
michael@0 | 214 | { SEC_ASN1_OPTIONAL | SEC_ASN1_INTEGER, offsetof(sec_PKCS12MacData, iter) }, |
michael@0 | 215 | { 0 } |
michael@0 | 216 | }; |
michael@0 | 217 | |
michael@0 | 218 | const SEC_ASN1Template sec_PKCS12AuthenticatedSafeTemplate[] = { |
michael@0 | 219 | { SEC_ASN1_SEQUENCE_OF | SEC_ASN1_MAY_STREAM | SEC_ASN1_XTRN , |
michael@0 | 220 | offsetof(sec_PKCS12AuthenticatedSafe, encodedSafes), |
michael@0 | 221 | SEC_ASN1_SUB(SEC_AnyTemplate) } |
michael@0 | 222 | }; |
michael@0 | 223 | |
michael@0 | 224 | const SEC_ASN1Template sec_PKCS12SafeBagTemplate[] = { |
michael@0 | 225 | { SEC_ASN1_SEQUENCE | SEC_ASN1_MAY_STREAM, 0, NULL, |
michael@0 | 226 | sizeof(sec_PKCS12SafeBag) }, |
michael@0 | 227 | { SEC_ASN1_OBJECT_ID, offsetof(sec_PKCS12SafeBag, safeBagType) }, |
michael@0 | 228 | { SEC_ASN1_EXPLICIT | SEC_ASN1_DYNAMIC | SEC_ASN1_CONSTRUCTED | |
michael@0 | 229 | SEC_ASN1_MAY_STREAM | SEC_ASN1_CONTEXT_SPECIFIC | 0, |
michael@0 | 230 | offsetof(sec_PKCS12SafeBag, safeBagContent), |
michael@0 | 231 | &sec_pkcs12_safe_bag_chooser }, |
michael@0 | 232 | { SEC_ASN1_SET_OF | SEC_ASN1_OPTIONAL, offsetof(sec_PKCS12SafeBag, attribs), |
michael@0 | 233 | sec_PKCS12AttributeTemplate }, |
michael@0 | 234 | { 0 } |
michael@0 | 235 | }; |
michael@0 | 236 | |
michael@0 | 237 | const SEC_ASN1Template sec_PKCS12SafeContentsTemplate[] = { |
michael@0 | 238 | { SEC_ASN1_SEQUENCE_OF | SEC_ASN1_MAY_STREAM, |
michael@0 | 239 | offsetof(sec_PKCS12SafeContents, safeBags), |
michael@0 | 240 | sec_PKCS12SafeBagTemplate } |
michael@0 | 241 | }; |
michael@0 | 242 | |
michael@0 | 243 | const SEC_ASN1Template sec_PKCS12SequenceOfAnyTemplate[] = { |
michael@0 | 244 | { SEC_ASN1_SEQUENCE_OF | SEC_ASN1_MAY_STREAM | SEC_ASN1_XTRN , 0, |
michael@0 | 245 | SEC_ASN1_SUB(SEC_AnyTemplate) } |
michael@0 | 246 | }; |
michael@0 | 247 | |
michael@0 | 248 | const SEC_ASN1Template sec_PKCS12NestedSafeContentsDecodeTemplate[] = { |
michael@0 | 249 | { SEC_ASN1_EXPLICIT | SEC_ASN1_CONTEXT_SPECIFIC | SEC_ASN1_CONSTRUCTED | 0, |
michael@0 | 250 | offsetof(sec_PKCS12SafeContents, encodedSafeBags), |
michael@0 | 251 | sec_PKCS12SequenceOfAnyTemplate } |
michael@0 | 252 | }; |
michael@0 | 253 | |
michael@0 | 254 | const SEC_ASN1Template sec_PKCS12SafeContentsDecodeTemplate[] = { |
michael@0 | 255 | { SEC_ASN1_SEQUENCE_OF | SEC_ASN1_MAY_STREAM | SEC_ASN1_XTRN , |
michael@0 | 256 | offsetof(sec_PKCS12SafeContents, encodedSafeBags), |
michael@0 | 257 | SEC_ASN1_SUB(SEC_AnyTemplate) } |
michael@0 | 258 | }; |
michael@0 | 259 | |
michael@0 | 260 | const SEC_ASN1Template sec_PKCS12CRLBagTemplate[] = { |
michael@0 | 261 | { SEC_ASN1_SEQUENCE, 0, NULL, sizeof(sec_PKCS12CRLBag) }, |
michael@0 | 262 | { SEC_ASN1_OBJECT_ID, offsetof(sec_PKCS12CRLBag, bagID) }, |
michael@0 | 263 | { SEC_ASN1_DYNAMIC | SEC_ASN1_POINTER, |
michael@0 | 264 | offsetof(sec_PKCS12CRLBag, value), &sec_pkcs12_crl_bag_chooser }, |
michael@0 | 265 | { 0 } |
michael@0 | 266 | }; |
michael@0 | 267 | |
michael@0 | 268 | const SEC_ASN1Template sec_PKCS12CertBagTemplate[] = { |
michael@0 | 269 | { SEC_ASN1_SEQUENCE, 0, NULL, sizeof(sec_PKCS12CertBag) }, |
michael@0 | 270 | { SEC_ASN1_OBJECT_ID, offsetof(sec_PKCS12CertBag, bagID) }, |
michael@0 | 271 | { SEC_ASN1_DYNAMIC | SEC_ASN1_EXPLICIT | SEC_ASN1_CONSTRUCTED | |
michael@0 | 272 | SEC_ASN1_CONTEXT_SPECIFIC | 0, |
michael@0 | 273 | offsetof(sec_PKCS12CertBag, value), &sec_pkcs12_cert_bag_chooser }, |
michael@0 | 274 | { 0 } |
michael@0 | 275 | }; |
michael@0 | 276 | |
michael@0 | 277 | const SEC_ASN1Template sec_PKCS12SecretBagTemplate[] = { |
michael@0 | 278 | { SEC_ASN1_SEQUENCE, 0, NULL, sizeof(sec_PKCS12SecretBag) }, |
michael@0 | 279 | { SEC_ASN1_OBJECT_ID, offsetof(sec_PKCS12SecretBag, secretType) }, |
michael@0 | 280 | { SEC_ASN1_ANY, offsetof(sec_PKCS12SecretBag, secretContent) }, |
michael@0 | 281 | { 0 } |
michael@0 | 282 | }; |
michael@0 | 283 | |
michael@0 | 284 | const SEC_ASN1Template sec_PKCS12AttributeTemplate[] = { |
michael@0 | 285 | { SEC_ASN1_SEQUENCE, 0, NULL, sizeof(sec_PKCS12Attribute) }, |
michael@0 | 286 | { SEC_ASN1_OBJECT_ID, offsetof(sec_PKCS12Attribute, attrType) }, |
michael@0 | 287 | { SEC_ASN1_SET_OF | SEC_ASN1_DYNAMIC, |
michael@0 | 288 | offsetof(sec_PKCS12Attribute, attrValue), |
michael@0 | 289 | &sec_pkcs12_attr_chooser }, |
michael@0 | 290 | { 0 } |
michael@0 | 291 | }; |