security/nss/lib/smime/cmsasn1.c

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/security/nss/lib/smime/cmsasn1.c	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,499 @@
     1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.6 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.7 +
     1.8 +/*
     1.9 + * CMS ASN.1 templates
    1.10 + */
    1.11 +
    1.12 +#include "cmslocal.h"
    1.13 +
    1.14 +#include "cert.h"
    1.15 +#include "key.h"
    1.16 +#include "secasn1.h"
    1.17 +#include "secitem.h"
    1.18 +#include "secoid.h"
    1.19 +#include "prtime.h"
    1.20 +#include "secerr.h"
    1.21 +
    1.22 +
    1.23 +extern const SEC_ASN1Template nss_cms_set_of_attribute_template[];
    1.24 +
    1.25 +SEC_ASN1_MKSUB(CERT_IssuerAndSNTemplate)
    1.26 +SEC_ASN1_MKSUB(CERT_SetOfSignedCrlTemplate)
    1.27 +SEC_ASN1_MKSUB(SECOID_AlgorithmIDTemplate)
    1.28 +SEC_ASN1_MKSUB(SEC_BitStringTemplate)
    1.29 +SEC_ASN1_MKSUB(SEC_OctetStringTemplate)
    1.30 +SEC_ASN1_MKSUB(SEC_PointerToOctetStringTemplate)
    1.31 +SEC_ASN1_MKSUB(SEC_SetOfAnyTemplate)
    1.32 +
    1.33 +/* -----------------------------------------------------------------------------
    1.34 + * MESSAGE
    1.35 + * (uses NSSCMSContentInfo)
    1.36 + */
    1.37 +
    1.38 +/* forward declaration */
    1.39 +static const SEC_ASN1Template *
    1.40 +nss_cms_choose_content_template(void *src_or_dest, PRBool encoding);
    1.41 +
    1.42 +static const SEC_ASN1TemplateChooserPtr nss_cms_chooser
    1.43 +	= nss_cms_choose_content_template;
    1.44 +
    1.45 +const SEC_ASN1Template NSSCMSMessageTemplate[] = {
    1.46 +    { SEC_ASN1_SEQUENCE | SEC_ASN1_MAY_STREAM,
    1.47 +	  0, NULL, sizeof(NSSCMSMessage) },
    1.48 +    { SEC_ASN1_OBJECT_ID,
    1.49 +	  offsetof(NSSCMSMessage,contentInfo.contentType) },
    1.50 +    { SEC_ASN1_OPTIONAL | SEC_ASN1_DYNAMIC | SEC_ASN1_MAY_STREAM
    1.51 +     | SEC_ASN1_EXPLICIT | SEC_ASN1_CONSTRUCTED | SEC_ASN1_CONTEXT_SPECIFIC | 0,
    1.52 +	  offsetof(NSSCMSMessage,contentInfo.content),
    1.53 +	  &nss_cms_chooser },
    1.54 +    { 0 }
    1.55 +};
    1.56 +
    1.57 +static const SEC_ASN1Template NSS_PointerToCMSMessageTemplate[] = {
    1.58 +    { SEC_ASN1_POINTER, 0, NSSCMSMessageTemplate }
    1.59 +};
    1.60 +
    1.61 +/* -----------------------------------------------------------------------------
    1.62 + * ENCAPSULATED & ENCRYPTED CONTENTINFO
    1.63 + * (both use a NSSCMSContentInfo)
    1.64 + */
    1.65 +static const SEC_ASN1Template NSSCMSEncapsulatedContentInfoTemplate[] = {
    1.66 +    { SEC_ASN1_SEQUENCE | SEC_ASN1_MAY_STREAM,
    1.67 +	  0, NULL, sizeof(NSSCMSContentInfo) },
    1.68 +    { SEC_ASN1_OBJECT_ID,
    1.69 +	  offsetof(NSSCMSContentInfo,contentType) },
    1.70 +    { SEC_ASN1_OPTIONAL | SEC_ASN1_EXPLICIT | SEC_ASN1_MAY_STREAM |
    1.71 +	SEC_ASN1_CONSTRUCTED | SEC_ASN1_CONTEXT_SPECIFIC | SEC_ASN1_XTRN | 0,
    1.72 +	  offsetof(NSSCMSContentInfo,rawContent),
    1.73 +	  SEC_ASN1_SUB(SEC_PointerToOctetStringTemplate) },
    1.74 +    { 0 }
    1.75 +};
    1.76 +
    1.77 +static const SEC_ASN1Template NSSCMSEncryptedContentInfoTemplate[] = {
    1.78 +    { SEC_ASN1_SEQUENCE | SEC_ASN1_MAY_STREAM,
    1.79 +	  0, NULL, sizeof(NSSCMSContentInfo) },
    1.80 +    { SEC_ASN1_OBJECT_ID,
    1.81 +	  offsetof(NSSCMSContentInfo,contentType) },
    1.82 +    { SEC_ASN1_INLINE | SEC_ASN1_XTRN,
    1.83 +	  offsetof(NSSCMSContentInfo,contentEncAlg),
    1.84 +	  SEC_ASN1_SUB(SECOID_AlgorithmIDTemplate) },
    1.85 +    { SEC_ASN1_OPTIONAL | SEC_ASN1_POINTER | SEC_ASN1_MAY_STREAM | 
    1.86 +      SEC_ASN1_CONTEXT_SPECIFIC | SEC_ASN1_XTRN | 0,
    1.87 +	  offsetof(NSSCMSContentInfo,rawContent),
    1.88 +	  SEC_ASN1_SUB(SEC_OctetStringTemplate) },
    1.89 +    { 0 }
    1.90 +};
    1.91 +
    1.92 +/* -----------------------------------------------------------------------------
    1.93 + * SIGNED DATA
    1.94 + */
    1.95 +
    1.96 +const SEC_ASN1Template NSSCMSSignerInfoTemplate[];
    1.97 +
    1.98 +const SEC_ASN1Template NSSCMSSignedDataTemplate[] = {
    1.99 +    { SEC_ASN1_SEQUENCE | SEC_ASN1_MAY_STREAM,
   1.100 +	  0, NULL, sizeof(NSSCMSSignedData) },
   1.101 +    { SEC_ASN1_INTEGER,
   1.102 +	  offsetof(NSSCMSSignedData,version) },
   1.103 +    { SEC_ASN1_SET_OF | SEC_ASN1_XTRN,
   1.104 +	  offsetof(NSSCMSSignedData,digestAlgorithms),
   1.105 +	  SEC_ASN1_SUB(SECOID_AlgorithmIDTemplate) },
   1.106 +    { SEC_ASN1_INLINE,
   1.107 +	  offsetof(NSSCMSSignedData,contentInfo),
   1.108 +	  NSSCMSEncapsulatedContentInfoTemplate },
   1.109 +    { SEC_ASN1_OPTIONAL | SEC_ASN1_CONSTRUCTED | SEC_ASN1_CONTEXT_SPECIFIC |
   1.110 +      SEC_ASN1_XTRN | 0,
   1.111 +	  offsetof(NSSCMSSignedData,rawCerts),
   1.112 +	  SEC_ASN1_SUB(SEC_SetOfAnyTemplate) },
   1.113 +    { SEC_ASN1_OPTIONAL | SEC_ASN1_CONSTRUCTED | SEC_ASN1_CONTEXT_SPECIFIC |
   1.114 +      SEC_ASN1_XTRN | 1,
   1.115 +	  offsetof(NSSCMSSignedData,crls),
   1.116 +	  SEC_ASN1_SUB(CERT_SetOfSignedCrlTemplate) },
   1.117 +    { SEC_ASN1_SET_OF,
   1.118 +	  offsetof(NSSCMSSignedData,signerInfos),
   1.119 +	  NSSCMSSignerInfoTemplate },
   1.120 +    { 0 }
   1.121 +};
   1.122 +
   1.123 +const SEC_ASN1Template NSS_PointerToCMSSignedDataTemplate[] = {
   1.124 +    { SEC_ASN1_POINTER, 0, NSSCMSSignedDataTemplate }
   1.125 +};
   1.126 +
   1.127 +/* -----------------------------------------------------------------------------
   1.128 + * signeridentifier
   1.129 + */
   1.130 +
   1.131 +static const SEC_ASN1Template NSSCMSSignerIdentifierTemplate[] = {
   1.132 +    { SEC_ASN1_CHOICE,
   1.133 +	  offsetof(NSSCMSSignerIdentifier,identifierType), NULL,
   1.134 +	  sizeof(NSSCMSSignerIdentifier) },
   1.135 +    { SEC_ASN1_POINTER | SEC_ASN1_CONTEXT_SPECIFIC | SEC_ASN1_XTRN | 0,
   1.136 +	  offsetof(NSSCMSSignerIdentifier,id.subjectKeyID),
   1.137 +	  SEC_ASN1_SUB(SEC_OctetStringTemplate) ,
   1.138 +	  NSSCMSRecipientID_SubjectKeyID },
   1.139 +    { SEC_ASN1_POINTER | SEC_ASN1_XTRN,
   1.140 +	  offsetof(NSSCMSSignerIdentifier,id.issuerAndSN),
   1.141 +	  SEC_ASN1_SUB(CERT_IssuerAndSNTemplate),
   1.142 +	  NSSCMSRecipientID_IssuerSN },
   1.143 +    { 0 }
   1.144 +};
   1.145 +
   1.146 +/* -----------------------------------------------------------------------------
   1.147 + * signerinfo
   1.148 + */
   1.149 +
   1.150 +const SEC_ASN1Template NSSCMSSignerInfoTemplate[] = {
   1.151 +    { SEC_ASN1_SEQUENCE,
   1.152 +	  0, NULL, sizeof(NSSCMSSignerInfo) },
   1.153 +    { SEC_ASN1_INTEGER,
   1.154 +	  offsetof(NSSCMSSignerInfo,version) },
   1.155 +    { SEC_ASN1_INLINE,
   1.156 +	  offsetof(NSSCMSSignerInfo,signerIdentifier),
   1.157 +	  NSSCMSSignerIdentifierTemplate },
   1.158 +    { SEC_ASN1_INLINE | SEC_ASN1_XTRN,
   1.159 +	  offsetof(NSSCMSSignerInfo,digestAlg),
   1.160 +	  SEC_ASN1_SUB(SECOID_AlgorithmIDTemplate) },
   1.161 +    { SEC_ASN1_OPTIONAL | SEC_ASN1_CONSTRUCTED | SEC_ASN1_CONTEXT_SPECIFIC | 0,
   1.162 +	  offsetof(NSSCMSSignerInfo,authAttr),
   1.163 +	  nss_cms_set_of_attribute_template },
   1.164 +    { SEC_ASN1_INLINE | SEC_ASN1_XTRN,
   1.165 +	  offsetof(NSSCMSSignerInfo,digestEncAlg),
   1.166 +	  SEC_ASN1_SUB(SECOID_AlgorithmIDTemplate) },
   1.167 +    { SEC_ASN1_OCTET_STRING,
   1.168 +	  offsetof(NSSCMSSignerInfo,encDigest) },
   1.169 +    { SEC_ASN1_OPTIONAL | SEC_ASN1_CONSTRUCTED | SEC_ASN1_CONTEXT_SPECIFIC | 1,
   1.170 +	  offsetof(NSSCMSSignerInfo,unAuthAttr),
   1.171 +	  nss_cms_set_of_attribute_template },
   1.172 +    { 0 }
   1.173 +};
   1.174 +
   1.175 +/* -----------------------------------------------------------------------------
   1.176 + * ENVELOPED DATA
   1.177 + */
   1.178 +
   1.179 +static const SEC_ASN1Template NSSCMSOriginatorInfoTemplate[] = {
   1.180 +    { SEC_ASN1_SEQUENCE,
   1.181 +	  0, NULL, sizeof(NSSCMSOriginatorInfo) },
   1.182 +    { SEC_ASN1_OPTIONAL | SEC_ASN1_CONSTRUCTED | SEC_ASN1_CONTEXT_SPECIFIC |
   1.183 +      SEC_ASN1_XTRN | 0,
   1.184 +	  offsetof(NSSCMSOriginatorInfo,rawCerts),
   1.185 +	  SEC_ASN1_SUB(SEC_SetOfAnyTemplate) },
   1.186 +    { SEC_ASN1_OPTIONAL | SEC_ASN1_CONSTRUCTED | SEC_ASN1_CONTEXT_SPECIFIC |
   1.187 +      SEC_ASN1_XTRN | 1,
   1.188 +	  offsetof(NSSCMSOriginatorInfo,crls),
   1.189 +	  SEC_ASN1_SUB(CERT_SetOfSignedCrlTemplate) },
   1.190 +    { 0 }
   1.191 +};
   1.192 +
   1.193 +const SEC_ASN1Template NSSCMSRecipientInfoTemplate[];
   1.194 +
   1.195 +const SEC_ASN1Template NSSCMSEnvelopedDataTemplate[] = {
   1.196 +    { SEC_ASN1_SEQUENCE | SEC_ASN1_MAY_STREAM,
   1.197 +	  0, NULL, sizeof(NSSCMSEnvelopedData) },
   1.198 +    { SEC_ASN1_INTEGER,
   1.199 +	  offsetof(NSSCMSEnvelopedData,version) },
   1.200 +    { SEC_ASN1_OPTIONAL | SEC_ASN1_POINTER | SEC_ASN1_CONSTRUCTED | SEC_ASN1_CONTEXT_SPECIFIC | 0,
   1.201 +	  offsetof(NSSCMSEnvelopedData,originatorInfo),
   1.202 +	  NSSCMSOriginatorInfoTemplate },
   1.203 +    { SEC_ASN1_SET_OF,
   1.204 +	  offsetof(NSSCMSEnvelopedData,recipientInfos),
   1.205 +	  NSSCMSRecipientInfoTemplate },
   1.206 +    { SEC_ASN1_INLINE,
   1.207 +	  offsetof(NSSCMSEnvelopedData,contentInfo),
   1.208 +	  NSSCMSEncryptedContentInfoTemplate },
   1.209 +    { SEC_ASN1_OPTIONAL | SEC_ASN1_CONSTRUCTED | SEC_ASN1_CONTEXT_SPECIFIC | 1,
   1.210 +	  offsetof(NSSCMSEnvelopedData,unprotectedAttr),
   1.211 +	  nss_cms_set_of_attribute_template },
   1.212 +    { 0 }
   1.213 +};
   1.214 +
   1.215 +const SEC_ASN1Template NSS_PointerToCMSEnvelopedDataTemplate[] = {
   1.216 +    { SEC_ASN1_POINTER, 0, NSSCMSEnvelopedDataTemplate }
   1.217 +};
   1.218 +
   1.219 +/* here come the 15 gazillion templates for all the v3 varieties of RecipientInfo */
   1.220 +
   1.221 +/* -----------------------------------------------------------------------------
   1.222 + * key transport recipient info
   1.223 + */
   1.224 +
   1.225 +static const SEC_ASN1Template NSSCMSRecipientIdentifierTemplate[] = {
   1.226 +    { SEC_ASN1_CHOICE,
   1.227 +	  offsetof(NSSCMSRecipientIdentifier,identifierType), NULL,
   1.228 +	  sizeof(NSSCMSRecipientIdentifier) },
   1.229 +    { SEC_ASN1_POINTER | SEC_ASN1_CONTEXT_SPECIFIC | SEC_ASN1_XTRN | 0,
   1.230 +	  offsetof(NSSCMSRecipientIdentifier,id.subjectKeyID),
   1.231 +	  SEC_ASN1_SUB(SEC_OctetStringTemplate) ,
   1.232 +	  NSSCMSRecipientID_SubjectKeyID },
   1.233 +    { SEC_ASN1_POINTER | SEC_ASN1_XTRN,
   1.234 +	  offsetof(NSSCMSRecipientIdentifier,id.issuerAndSN),
   1.235 +	  SEC_ASN1_SUB(CERT_IssuerAndSNTemplate),
   1.236 +	  NSSCMSRecipientID_IssuerSN },
   1.237 +    { 0 }
   1.238 +};
   1.239 +
   1.240 +
   1.241 +static const SEC_ASN1Template NSSCMSKeyTransRecipientInfoTemplate[] = {
   1.242 +    { SEC_ASN1_SEQUENCE,
   1.243 +	  0, NULL, sizeof(NSSCMSKeyTransRecipientInfo) },
   1.244 +    { SEC_ASN1_INTEGER,
   1.245 +	  offsetof(NSSCMSKeyTransRecipientInfo,version) },
   1.246 +    { SEC_ASN1_INLINE,
   1.247 +	  offsetof(NSSCMSKeyTransRecipientInfo,recipientIdentifier),
   1.248 +	  NSSCMSRecipientIdentifierTemplate },
   1.249 +    { SEC_ASN1_INLINE | SEC_ASN1_XTRN,
   1.250 +	  offsetof(NSSCMSKeyTransRecipientInfo,keyEncAlg),
   1.251 +	  SEC_ASN1_SUB(SECOID_AlgorithmIDTemplate) },
   1.252 +    { SEC_ASN1_OCTET_STRING,
   1.253 +	  offsetof(NSSCMSKeyTransRecipientInfo,encKey) },
   1.254 +    { 0 }
   1.255 +};
   1.256 +
   1.257 +/* -----------------------------------------------------------------------------
   1.258 + * key agreement recipient info
   1.259 + */
   1.260 +
   1.261 +static const SEC_ASN1Template NSSCMSOriginatorPublicKeyTemplate[] = {
   1.262 +    { SEC_ASN1_SEQUENCE,
   1.263 +	  0, NULL, sizeof(NSSCMSOriginatorPublicKey) },
   1.264 +    { SEC_ASN1_INLINE | SEC_ASN1_XTRN,
   1.265 +	  offsetof(NSSCMSOriginatorPublicKey,algorithmIdentifier),
   1.266 +	  SEC_ASN1_SUB(SECOID_AlgorithmIDTemplate) },
   1.267 +    { SEC_ASN1_INLINE | SEC_ASN1_XTRN,
   1.268 +	  offsetof(NSSCMSOriginatorPublicKey,publicKey),
   1.269 +	  SEC_ASN1_SUB(SEC_BitStringTemplate) },
   1.270 +    { 0 }
   1.271 +};
   1.272 +
   1.273 +
   1.274 +static const SEC_ASN1Template NSSCMSOriginatorIdentifierOrKeyTemplate[] = {
   1.275 +    { SEC_ASN1_CHOICE,
   1.276 +	  offsetof(NSSCMSOriginatorIdentifierOrKey,identifierType), NULL,
   1.277 +	  sizeof(NSSCMSOriginatorIdentifierOrKey) },
   1.278 +    { SEC_ASN1_POINTER | SEC_ASN1_XTRN,
   1.279 +	  offsetof(NSSCMSOriginatorIdentifierOrKey,id.issuerAndSN),
   1.280 +	  SEC_ASN1_SUB(CERT_IssuerAndSNTemplate),
   1.281 +	  NSSCMSOriginatorIDOrKey_IssuerSN },
   1.282 +    { SEC_ASN1_EXPLICIT | SEC_ASN1_CONSTRUCTED | SEC_ASN1_CONTEXT_SPECIFIC |
   1.283 +      SEC_ASN1_XTRN | 1,
   1.284 +	  offsetof(NSSCMSOriginatorIdentifierOrKey,id.subjectKeyID),
   1.285 +	  SEC_ASN1_SUB(SEC_PointerToOctetStringTemplate) ,
   1.286 +	  NSSCMSOriginatorIDOrKey_SubjectKeyID },
   1.287 +    { SEC_ASN1_EXPLICIT | SEC_ASN1_CONSTRUCTED | SEC_ASN1_CONTEXT_SPECIFIC | 2,
   1.288 +	  offsetof(NSSCMSOriginatorIdentifierOrKey,id.originatorPublicKey),
   1.289 +	  NSSCMSOriginatorPublicKeyTemplate,
   1.290 +	  NSSCMSOriginatorIDOrKey_OriginatorPublicKey },
   1.291 +    { 0 }
   1.292 +};
   1.293 +
   1.294 +const SEC_ASN1Template NSSCMSRecipientKeyIdentifierTemplate[] = {
   1.295 +    { SEC_ASN1_SEQUENCE,
   1.296 +	  0, NULL, sizeof(NSSCMSRecipientKeyIdentifier) },
   1.297 +    { SEC_ASN1_OCTET_STRING,
   1.298 +	  offsetof(NSSCMSRecipientKeyIdentifier,subjectKeyIdentifier) },
   1.299 +    { SEC_ASN1_OPTIONAL | SEC_ASN1_OCTET_STRING,
   1.300 +	  offsetof(NSSCMSRecipientKeyIdentifier,date) },
   1.301 +    { SEC_ASN1_OPTIONAL | SEC_ASN1_OCTET_STRING,
   1.302 +	  offsetof(NSSCMSRecipientKeyIdentifier,other) },
   1.303 +    { 0 }
   1.304 +};
   1.305 +
   1.306 +
   1.307 +static const SEC_ASN1Template NSSCMSKeyAgreeRecipientIdentifierTemplate[] = {
   1.308 +    { SEC_ASN1_CHOICE,
   1.309 +	  offsetof(NSSCMSKeyAgreeRecipientIdentifier,identifierType), NULL,
   1.310 +	  sizeof(NSSCMSKeyAgreeRecipientIdentifier) },
   1.311 +    { SEC_ASN1_POINTER | SEC_ASN1_XTRN,
   1.312 +	  offsetof(NSSCMSKeyAgreeRecipientIdentifier,id.issuerAndSN),
   1.313 +	  SEC_ASN1_SUB(CERT_IssuerAndSNTemplate),
   1.314 +	  NSSCMSKeyAgreeRecipientID_IssuerSN },
   1.315 +    { SEC_ASN1_CONSTRUCTED | SEC_ASN1_CONTEXT_SPECIFIC | 0,
   1.316 +	  offsetof(NSSCMSKeyAgreeRecipientIdentifier,id.recipientKeyIdentifier),
   1.317 +	  NSSCMSRecipientKeyIdentifierTemplate,
   1.318 +	  NSSCMSKeyAgreeRecipientID_RKeyID },
   1.319 +    { 0 }
   1.320 +};
   1.321 +
   1.322 +static const SEC_ASN1Template NSSCMSRecipientEncryptedKeyTemplate[] = {
   1.323 +    { SEC_ASN1_SEQUENCE,
   1.324 +	  0, NULL, sizeof(NSSCMSRecipientEncryptedKey) },
   1.325 +    { SEC_ASN1_INLINE,
   1.326 +	  offsetof(NSSCMSRecipientEncryptedKey,recipientIdentifier),
   1.327 +	  NSSCMSKeyAgreeRecipientIdentifierTemplate },
   1.328 +    { SEC_ASN1_INLINE | SEC_ASN1_XTRN,
   1.329 +	  offsetof(NSSCMSRecipientEncryptedKey,encKey),
   1.330 +	  SEC_ASN1_SUB(SEC_BitStringTemplate) },
   1.331 +    { 0 }
   1.332 +};
   1.333 +
   1.334 +static const SEC_ASN1Template NSSCMSKeyAgreeRecipientInfoTemplate[] = {
   1.335 +    { SEC_ASN1_SEQUENCE,
   1.336 +	  0, NULL, sizeof(NSSCMSKeyAgreeRecipientInfo) },
   1.337 +    { SEC_ASN1_INTEGER,
   1.338 +	  offsetof(NSSCMSKeyAgreeRecipientInfo,version) },
   1.339 +    { SEC_ASN1_EXPLICIT | SEC_ASN1_CONSTRUCTED | SEC_ASN1_CONTEXT_SPECIFIC | 0,
   1.340 +	  offsetof(NSSCMSKeyAgreeRecipientInfo,originatorIdentifierOrKey),
   1.341 +	  NSSCMSOriginatorIdentifierOrKeyTemplate },
   1.342 +    { SEC_ASN1_OPTIONAL | SEC_ASN1_CONSTRUCTED | SEC_ASN1_EXPLICIT |
   1.343 +      SEC_ASN1_CONTEXT_SPECIFIC | SEC_ASN1_XTRN | 1,
   1.344 +	  offsetof(NSSCMSKeyAgreeRecipientInfo,ukm),
   1.345 +	  SEC_ASN1_SUB(SEC_OctetStringTemplate) },
   1.346 +    { SEC_ASN1_INLINE | SEC_ASN1_XTRN,
   1.347 +	  offsetof(NSSCMSKeyAgreeRecipientInfo,keyEncAlg),
   1.348 +	  SEC_ASN1_SUB(SECOID_AlgorithmIDTemplate) },
   1.349 +    { SEC_ASN1_SEQUENCE_OF,
   1.350 +	  offsetof(NSSCMSKeyAgreeRecipientInfo,recipientEncryptedKeys),
   1.351 +	  NSSCMSRecipientEncryptedKeyTemplate },
   1.352 +    { 0 }
   1.353 +};
   1.354 +
   1.355 +/* -----------------------------------------------------------------------------
   1.356 + * KEK recipient info
   1.357 + */
   1.358 +
   1.359 +static const SEC_ASN1Template NSSCMSKEKIdentifierTemplate[] = {
   1.360 +    { SEC_ASN1_SEQUENCE,
   1.361 +	  0, NULL, sizeof(NSSCMSKEKIdentifier) },
   1.362 +    { SEC_ASN1_OCTET_STRING,
   1.363 +	  offsetof(NSSCMSKEKIdentifier,keyIdentifier) },
   1.364 +    { SEC_ASN1_OPTIONAL | SEC_ASN1_OCTET_STRING,
   1.365 +	  offsetof(NSSCMSKEKIdentifier,date) },
   1.366 +    { SEC_ASN1_OPTIONAL | SEC_ASN1_OCTET_STRING,
   1.367 +	  offsetof(NSSCMSKEKIdentifier,other) },
   1.368 +    { 0 }
   1.369 +};
   1.370 +
   1.371 +static const SEC_ASN1Template NSSCMSKEKRecipientInfoTemplate[] = {
   1.372 +    { SEC_ASN1_SEQUENCE,
   1.373 +	  0, NULL, sizeof(NSSCMSKEKRecipientInfo) },
   1.374 +    { SEC_ASN1_INTEGER,
   1.375 +	  offsetof(NSSCMSKEKRecipientInfo,version) },
   1.376 +    { SEC_ASN1_INLINE,
   1.377 +	  offsetof(NSSCMSKEKRecipientInfo,kekIdentifier),
   1.378 +	  NSSCMSKEKIdentifierTemplate },
   1.379 +    { SEC_ASN1_INLINE | SEC_ASN1_XTRN,
   1.380 +	  offsetof(NSSCMSKEKRecipientInfo,keyEncAlg),
   1.381 +	  SEC_ASN1_SUB(SECOID_AlgorithmIDTemplate) },
   1.382 +    { SEC_ASN1_OCTET_STRING,
   1.383 +	  offsetof(NSSCMSKEKRecipientInfo,encKey) },
   1.384 +    { 0 }
   1.385 +};
   1.386 +
   1.387 +/* -----------------------------------------------------------------------------
   1.388 + * recipient info
   1.389 + */
   1.390 +const SEC_ASN1Template NSSCMSRecipientInfoTemplate[] = {
   1.391 +    { SEC_ASN1_CHOICE,
   1.392 +	  offsetof(NSSCMSRecipientInfo,recipientInfoType), NULL,
   1.393 +	  sizeof(NSSCMSRecipientInfo) },
   1.394 +    { SEC_ASN1_EXPLICIT | SEC_ASN1_CONSTRUCTED | SEC_ASN1_CONTEXT_SPECIFIC | 1,
   1.395 +	  offsetof(NSSCMSRecipientInfo,ri.keyAgreeRecipientInfo),
   1.396 +	  NSSCMSKeyAgreeRecipientInfoTemplate,
   1.397 +	  NSSCMSRecipientInfoID_KeyAgree },
   1.398 +    { SEC_ASN1_EXPLICIT | SEC_ASN1_CONSTRUCTED | SEC_ASN1_CONTEXT_SPECIFIC | 2,
   1.399 +	  offsetof(NSSCMSRecipientInfo,ri.kekRecipientInfo),
   1.400 +	  NSSCMSKEKRecipientInfoTemplate,
   1.401 +	  NSSCMSRecipientInfoID_KEK },
   1.402 +    { SEC_ASN1_INLINE,
   1.403 +	  offsetof(NSSCMSRecipientInfo,ri.keyTransRecipientInfo),
   1.404 +	  NSSCMSKeyTransRecipientInfoTemplate,
   1.405 +	  NSSCMSRecipientInfoID_KeyTrans },
   1.406 +    { 0 }
   1.407 +};
   1.408 +
   1.409 +/* -----------------------------------------------------------------------------
   1.410 + *
   1.411 + */
   1.412 +
   1.413 +const SEC_ASN1Template NSSCMSDigestedDataTemplate[] = {
   1.414 +    { SEC_ASN1_SEQUENCE | SEC_ASN1_MAY_STREAM,
   1.415 +	  0, NULL, sizeof(NSSCMSDigestedData) },
   1.416 +    { SEC_ASN1_INTEGER,
   1.417 +	  offsetof(NSSCMSDigestedData,version) },
   1.418 +    { SEC_ASN1_INLINE | SEC_ASN1_XTRN,
   1.419 +	  offsetof(NSSCMSDigestedData,digestAlg),
   1.420 +	  SEC_ASN1_SUB(SECOID_AlgorithmIDTemplate) },
   1.421 +    { SEC_ASN1_INLINE,
   1.422 +	  offsetof(NSSCMSDigestedData,contentInfo),
   1.423 +	  NSSCMSEncapsulatedContentInfoTemplate },
   1.424 +    { SEC_ASN1_OCTET_STRING,
   1.425 +	  offsetof(NSSCMSDigestedData,digest) },
   1.426 +    { 0 }
   1.427 +};
   1.428 +
   1.429 +const SEC_ASN1Template NSS_PointerToCMSDigestedDataTemplate[] = {
   1.430 +    { SEC_ASN1_POINTER, 0, NSSCMSDigestedDataTemplate }
   1.431 +};
   1.432 +
   1.433 +const SEC_ASN1Template NSSCMSEncryptedDataTemplate[] = {
   1.434 +    { SEC_ASN1_SEQUENCE | SEC_ASN1_MAY_STREAM,
   1.435 +	  0, NULL, sizeof(NSSCMSEncryptedData) },
   1.436 +    { SEC_ASN1_INTEGER,
   1.437 +	  offsetof(NSSCMSEncryptedData,version) },
   1.438 +    { SEC_ASN1_INLINE,
   1.439 +	  offsetof(NSSCMSEncryptedData,contentInfo),
   1.440 +	  NSSCMSEncryptedContentInfoTemplate },
   1.441 +    { SEC_ASN1_OPTIONAL | SEC_ASN1_CONSTRUCTED | SEC_ASN1_CONTEXT_SPECIFIC | 1,
   1.442 +	  offsetof(NSSCMSEncryptedData,unprotectedAttr),
   1.443 +	  nss_cms_set_of_attribute_template },
   1.444 +    { 0 }
   1.445 +};
   1.446 +
   1.447 +const SEC_ASN1Template NSS_PointerToCMSEncryptedDataTemplate[] = {
   1.448 +    { SEC_ASN1_POINTER, 0, NSSCMSEncryptedDataTemplate }
   1.449 +};
   1.450 +
   1.451 +const SEC_ASN1Template NSSCMSGenericWrapperDataTemplate[] = {
   1.452 +    { SEC_ASN1_INLINE,
   1.453 +	  offsetof(NSSCMSGenericWrapperData,contentInfo),
   1.454 +	  NSSCMSEncapsulatedContentInfoTemplate },
   1.455 +};
   1.456 +
   1.457 +SEC_ASN1_CHOOSER_IMPLEMENT(NSSCMSGenericWrapperDataTemplate)
   1.458 +
   1.459 +const SEC_ASN1Template NSS_PointerToCMSGenericWrapperDataTemplate[] = {
   1.460 +    { SEC_ASN1_POINTER, 0, NSSCMSGenericWrapperDataTemplate }
   1.461 +};
   1.462 +
   1.463 +SEC_ASN1_CHOOSER_IMPLEMENT(NSS_PointerToCMSGenericWrapperDataTemplate)
   1.464 +
   1.465 +/* -----------------------------------------------------------------------------
   1.466 + *
   1.467 + */
   1.468 +static const SEC_ASN1Template *
   1.469 +nss_cms_choose_content_template(void *src_or_dest, PRBool encoding)
   1.470 +{
   1.471 +    const SEC_ASN1Template *theTemplate;
   1.472 +    NSSCMSContentInfo *cinfo;
   1.473 +    SECOidTag type;
   1.474 +
   1.475 +    PORT_Assert (src_or_dest != NULL);
   1.476 +    if (src_or_dest == NULL)
   1.477 +	return NULL;
   1.478 +
   1.479 +    cinfo = (NSSCMSContentInfo *)src_or_dest;
   1.480 +    type = NSS_CMSContentInfo_GetContentTypeTag(cinfo);
   1.481 +    switch (type) {
   1.482 +    default:
   1.483 +	theTemplate = NSS_CMSType_GetTemplate(type);
   1.484 +	break;
   1.485 +    case SEC_OID_PKCS7_DATA:
   1.486 +	theTemplate = SEC_ASN1_GET(SEC_PointerToOctetStringTemplate);
   1.487 +	break;
   1.488 +    case SEC_OID_PKCS7_SIGNED_DATA:
   1.489 +	theTemplate = NSS_PointerToCMSSignedDataTemplate;
   1.490 +	break;
   1.491 +    case SEC_OID_PKCS7_ENVELOPED_DATA:
   1.492 +	theTemplate = NSS_PointerToCMSEnvelopedDataTemplate;
   1.493 +	break;
   1.494 +    case SEC_OID_PKCS7_DIGESTED_DATA:
   1.495 +	theTemplate = NSS_PointerToCMSDigestedDataTemplate;
   1.496 +	break;
   1.497 +    case SEC_OID_PKCS7_ENCRYPTED_DATA:
   1.498 +	theTemplate = NSS_PointerToCMSEncryptedDataTemplate;
   1.499 +	break;
   1.500 +    }
   1.501 +    return theTemplate;
   1.502 +}

mercurial