cmsasn1.c
上传用户:lyxiangda
上传日期:2007-01-12
资源大小:3042k
文件大小:19k
源码类别:

CA认证

开发平台:

WINDOWS

  1. /*
  2.  * The contents of this file are subject to the Mozilla Public
  3.  * License Version 1.1 (the "License"); you may not use this file
  4.  * except in compliance with the License. You may obtain a copy of
  5.  * the License at http://www.mozilla.org/MPL/
  6.  * 
  7.  * Software distributed under the License is distributed on an "AS
  8.  * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
  9.  * implied. See the License for the specific language governing
  10.  * rights and limitations under the License.
  11.  * 
  12.  * The Original Code is the Netscape security libraries.
  13.  * 
  14.  * The Initial Developer of the Original Code is Netscape
  15.  * Communications Corporation.  Portions created by Netscape are 
  16.  * Copyright (C) 1994-2000 Netscape Communications Corporation.  All
  17.  * Rights Reserved.
  18.  * 
  19.  * Contributor(s):
  20.  * 
  21.  * Alternatively, the contents of this file may be used under the
  22.  * terms of the GNU General Public License Version 2 or later (the
  23.  * "GPL"), in which case the provisions of the GPL are applicable 
  24.  * instead of those above.  If you wish to allow use of your 
  25.  * version of this file only under the terms of the GPL and not to
  26.  * allow others to use your version of this file under the MPL,
  27.  * indicate your decision by deleting the provisions above and
  28.  * replace them with the notice and other provisions required by
  29.  * the GPL.  If you do not delete the provisions above, a recipient
  30.  * may use your version of this file under either the MPL or the
  31.  * GPL.
  32.  */
  33. /*
  34.  * CMS ASN.1 templates
  35.  *
  36.  * $Id: cmsasn1.c,v 1.4 2000/06/20 16:28:57 chrisk%netscape.com Exp $
  37.  */
  38. #include "cmslocal.h"
  39. #include "cert.h"
  40. #include "key.h"
  41. #include "secasn1.h"
  42. #include "secitem.h"
  43. #include "secoid.h"
  44. #include "prtime.h"
  45. #include "secerr.h"
  46. extern const SEC_ASN1Template nss_cms_set_of_attribute_template[];
  47. /* -----------------------------------------------------------------------------
  48.  * MESSAGE
  49.  * (uses NSSCMSContentInfo)
  50.  */
  51. /* forward declaration */
  52. static const SEC_ASN1Template *
  53. nss_cms_choose_content_template(void *src_or_dest, PRBool encoding);
  54. static SEC_ChooseASN1TemplateFunc nss_cms_chooser
  55. = nss_cms_choose_content_template;
  56. const SEC_ASN1Template NSSCMSMessageTemplate[] = {
  57.     { SEC_ASN1_SEQUENCE | SEC_ASN1_MAY_STREAM,
  58.   0, NULL, sizeof(NSSCMSMessage) },
  59.     { SEC_ASN1_OBJECT_ID,
  60.   offsetof(NSSCMSMessage,contentInfo.contentType) },
  61.     { SEC_ASN1_OPTIONAL | SEC_ASN1_DYNAMIC | SEC_ASN1_MAY_STREAM
  62.      | SEC_ASN1_EXPLICIT | SEC_ASN1_CONSTRUCTED | SEC_ASN1_CONTEXT_SPECIFIC | 0,
  63.   offsetof(NSSCMSMessage,contentInfo.content),
  64.   &nss_cms_chooser },
  65.     { 0 }
  66. };
  67. static const SEC_ASN1Template NSS_PointerToCMSMessageTemplate[] = {
  68.     { SEC_ASN1_POINTER, 0, NSSCMSMessageTemplate }
  69. };
  70. /* -----------------------------------------------------------------------------
  71.  * ENCAPSULATED & ENCRYPTED CONTENTINFO
  72.  * (both use a NSSCMSContentInfo)
  73.  */
  74. static const SEC_ASN1Template NSSCMSEncapsulatedContentInfoTemplate[] = {
  75.     { SEC_ASN1_SEQUENCE | SEC_ASN1_MAY_STREAM,
  76.   0, NULL, sizeof(NSSCMSContentInfo) },
  77.     { SEC_ASN1_OBJECT_ID,
  78.   offsetof(NSSCMSContentInfo,contentType) },
  79.     { SEC_ASN1_OPTIONAL | SEC_ASN1_EXPLICIT | SEC_ASN1_MAY_STREAM |
  80. SEC_ASN1_CONSTRUCTED | SEC_ASN1_CONTEXT_SPECIFIC | 0,
  81.   offsetof(NSSCMSContentInfo,rawContent),
  82.   SEC_PointerToOctetStringTemplate },
  83.     { 0 }
  84. };
  85. static const SEC_ASN1Template NSSCMSEncryptedContentInfoTemplate[] = {
  86.     { SEC_ASN1_SEQUENCE | SEC_ASN1_MAY_STREAM,
  87.   0, NULL, sizeof(NSSCMSContentInfo) },
  88.     { SEC_ASN1_OBJECT_ID,
  89.   offsetof(NSSCMSContentInfo,contentType) },
  90.     { SEC_ASN1_INLINE,
  91.   offsetof(NSSCMSContentInfo,contentEncAlg),
  92.   SECOID_AlgorithmIDTemplate },
  93.     { SEC_ASN1_OPTIONAL | SEC_ASN1_POINTER | SEC_ASN1_MAY_STREAM | SEC_ASN1_CONTEXT_SPECIFIC | 0,
  94.   offsetof(NSSCMSContentInfo,rawContent),
  95.   SEC_OctetStringTemplate },
  96.     { 0 }
  97. };
  98. /* -----------------------------------------------------------------------------
  99.  * SIGNED DATA
  100.  */
  101. const SEC_ASN1Template NSSCMSSignerInfoTemplate[];
  102. const SEC_ASN1Template NSSCMSSignedDataTemplate[] = {
  103.     { SEC_ASN1_SEQUENCE | SEC_ASN1_MAY_STREAM,
  104.   0, NULL, sizeof(NSSCMSSignedData) },
  105.     { SEC_ASN1_INTEGER,
  106.   offsetof(NSSCMSSignedData,version) },
  107.     { SEC_ASN1_SET_OF,
  108.   offsetof(NSSCMSSignedData,digestAlgorithms),
  109.   SECOID_AlgorithmIDTemplate },
  110.     { SEC_ASN1_INLINE,
  111.   offsetof(NSSCMSSignedData,contentInfo),
  112.   NSSCMSEncapsulatedContentInfoTemplate },
  113.     { SEC_ASN1_OPTIONAL | SEC_ASN1_CONSTRUCTED | SEC_ASN1_CONTEXT_SPECIFIC | 0,
  114.   offsetof(NSSCMSSignedData,rawCerts),
  115.   SEC_SetOfAnyTemplate },
  116.     { SEC_ASN1_OPTIONAL | SEC_ASN1_CONSTRUCTED | SEC_ASN1_CONTEXT_SPECIFIC | 1,
  117.   offsetof(NSSCMSSignedData,crls),
  118.   CERT_SetOfSignedCrlTemplate },
  119.     { SEC_ASN1_SET_OF,
  120.   offsetof(NSSCMSSignedData,signerInfos),
  121.   NSSCMSSignerInfoTemplate },
  122.     { 0 }
  123. };
  124. const SEC_ASN1Template NSS_PointerToCMSSignedDataTemplate[] = {
  125.     { SEC_ASN1_POINTER, 0, NSSCMSSignedDataTemplate }
  126. };
  127. /* -----------------------------------------------------------------------------
  128.  * signeridentifier
  129.  */
  130. static const SEC_ASN1Template NSSCMSSignerIdentifierTemplate[] = {
  131.     { SEC_ASN1_CHOICE,
  132.   offsetof(NSSCMSSignerIdentifier,identifierType), NULL,
  133.   sizeof(NSSCMSSignerIdentifier) },
  134.     { SEC_ASN1_POINTER | SEC_ASN1_CONTEXT_SPECIFIC | 0,
  135.   offsetof(NSSCMSSignerIdentifier,id.subjectKeyID),
  136.   SEC_OctetStringTemplate,
  137.   NSSCMSRecipientID_SubjectKeyID },
  138.     { SEC_ASN1_POINTER,
  139.   offsetof(NSSCMSSignerIdentifier,id.issuerAndSN),
  140.   CERT_IssuerAndSNTemplate,
  141.   NSSCMSRecipientID_IssuerSN },
  142.     { 0 }
  143. };
  144. /* -----------------------------------------------------------------------------
  145.  * signerinfo
  146.  */
  147. const SEC_ASN1Template NSSCMSSignerInfoTemplate[] = {
  148.     { SEC_ASN1_SEQUENCE,
  149.   0, NULL, sizeof(NSSCMSSignerInfo) },
  150.     { SEC_ASN1_INTEGER,
  151.   offsetof(NSSCMSSignerInfo,version) },
  152.     { SEC_ASN1_INLINE,
  153.   offsetof(NSSCMSSignerInfo,signerIdentifier),
  154.   NSSCMSSignerIdentifierTemplate },
  155.     { SEC_ASN1_INLINE,
  156.   offsetof(NSSCMSSignerInfo,digestAlg),
  157.   SECOID_AlgorithmIDTemplate },
  158.     { SEC_ASN1_OPTIONAL | SEC_ASN1_CONSTRUCTED | SEC_ASN1_CONTEXT_SPECIFIC | 0,
  159.   offsetof(NSSCMSSignerInfo,authAttr),
  160.   nss_cms_set_of_attribute_template },
  161.     { SEC_ASN1_INLINE,
  162.   offsetof(NSSCMSSignerInfo,digestEncAlg),
  163.   SECOID_AlgorithmIDTemplate },
  164.     { SEC_ASN1_OCTET_STRING,
  165.   offsetof(NSSCMSSignerInfo,encDigest) },
  166.     { SEC_ASN1_OPTIONAL | SEC_ASN1_CONSTRUCTED | SEC_ASN1_CONTEXT_SPECIFIC | 1,
  167.   offsetof(NSSCMSSignerInfo,unAuthAttr),
  168.   nss_cms_set_of_attribute_template },
  169.     { 0 }
  170. };
  171. /* -----------------------------------------------------------------------------
  172.  * ENVELOPED DATA
  173.  */
  174. static const SEC_ASN1Template NSSCMSOriginatorInfoTemplate[] = {
  175.     { SEC_ASN1_SEQUENCE,
  176.   0, NULL, sizeof(NSSCMSOriginatorInfo) },
  177.     { SEC_ASN1_OPTIONAL | SEC_ASN1_CONSTRUCTED | SEC_ASN1_CONTEXT_SPECIFIC | 0,
  178.   offsetof(NSSCMSOriginatorInfo,rawCerts),
  179.   SEC_SetOfAnyTemplate },
  180.     { SEC_ASN1_OPTIONAL | SEC_ASN1_CONSTRUCTED | SEC_ASN1_CONTEXT_SPECIFIC | 1,
  181.   offsetof(NSSCMSOriginatorInfo,crls),
  182.   CERT_SetOfSignedCrlTemplate },
  183.     { 0 }
  184. };
  185. const SEC_ASN1Template NSSCMSRecipientInfoTemplate[];
  186. const SEC_ASN1Template NSSCMSEnvelopedDataTemplate[] = {
  187.     { SEC_ASN1_SEQUENCE | SEC_ASN1_MAY_STREAM,
  188.   0, NULL, sizeof(NSSCMSEnvelopedData) },
  189.     { SEC_ASN1_INTEGER,
  190.   offsetof(NSSCMSEnvelopedData,version) },
  191.     { SEC_ASN1_OPTIONAL | SEC_ASN1_POINTER | SEC_ASN1_CONSTRUCTED | SEC_ASN1_CONTEXT_SPECIFIC | 0,
  192.   offsetof(NSSCMSEnvelopedData,originatorInfo),
  193.   NSSCMSOriginatorInfoTemplate },
  194.     { SEC_ASN1_SET_OF,
  195.   offsetof(NSSCMSEnvelopedData,recipientInfos),
  196.   NSSCMSRecipientInfoTemplate },
  197.     { SEC_ASN1_INLINE,
  198.   offsetof(NSSCMSEnvelopedData,contentInfo),
  199.   NSSCMSEncryptedContentInfoTemplate },
  200.     { SEC_ASN1_OPTIONAL | SEC_ASN1_CONSTRUCTED | SEC_ASN1_CONTEXT_SPECIFIC | 1,
  201.   offsetof(NSSCMSEnvelopedData,unprotectedAttr),
  202.   nss_cms_set_of_attribute_template },
  203.     { 0 }
  204. };
  205. const SEC_ASN1Template NSS_PointerToCMSEnvelopedDataTemplate[] = {
  206.     { SEC_ASN1_POINTER, 0, NSSCMSEnvelopedDataTemplate }
  207. };
  208. /* here come the 15 gazillion templates for all the v3 varieties of RecipientInfo */
  209. /* -----------------------------------------------------------------------------
  210.  * key transport recipient info
  211.  */
  212. static const SEC_ASN1Template NSSCMSRecipientIdentifierTemplate[] = {
  213.     { SEC_ASN1_CHOICE,
  214.   offsetof(NSSCMSRecipientIdentifier,identifierType), NULL,
  215.   sizeof(NSSCMSRecipientIdentifier) },
  216.     { SEC_ASN1_EXPLICIT | SEC_ASN1_CONSTRUCTED | SEC_ASN1_CONTEXT_SPECIFIC | 0,
  217.   offsetof(NSSCMSRecipientIdentifier,id.subjectKeyID),
  218.   SEC_PointerToOctetStringTemplate,
  219.   NSSCMSRecipientID_SubjectKeyID },
  220.     { SEC_ASN1_POINTER,
  221.   offsetof(NSSCMSRecipientIdentifier,id.issuerAndSN),
  222.   CERT_IssuerAndSNTemplate,
  223.   NSSCMSRecipientID_IssuerSN },
  224.     { 0 }
  225. };
  226. static const SEC_ASN1Template NSSCMSKeyTransRecipientInfoTemplate[] = {
  227.     { SEC_ASN1_SEQUENCE,
  228.   0, NULL, sizeof(NSSCMSKeyTransRecipientInfo) },
  229.     { SEC_ASN1_INTEGER,
  230.   offsetof(NSSCMSKeyTransRecipientInfo,version) },
  231.     { SEC_ASN1_INLINE,
  232.   offsetof(NSSCMSKeyTransRecipientInfo,recipientIdentifier),
  233.   NSSCMSRecipientIdentifierTemplate },
  234.     { SEC_ASN1_INLINE,
  235.   offsetof(NSSCMSKeyTransRecipientInfo,keyEncAlg),
  236.   SECOID_AlgorithmIDTemplate },
  237.     { SEC_ASN1_OCTET_STRING,
  238.   offsetof(NSSCMSKeyTransRecipientInfo,encKey) },
  239.     { 0 }
  240. };
  241. /* -----------------------------------------------------------------------------
  242.  * key agreement recipient info
  243.  */
  244. static const SEC_ASN1Template NSSCMSOriginatorPublicKeyTemplate[] = {
  245.     { SEC_ASN1_SEQUENCE,
  246.   0, NULL, sizeof(NSSCMSOriginatorPublicKey) },
  247.     { SEC_ASN1_INLINE,
  248.   offsetof(NSSCMSOriginatorPublicKey,algorithmIdentifier),
  249.   SECOID_AlgorithmIDTemplate },
  250.     { SEC_ASN1_INLINE,
  251.   offsetof(NSSCMSOriginatorPublicKey,publicKey),
  252.   SEC_BitStringTemplate },
  253.     { 0 }
  254. };
  255. static const SEC_ASN1Template NSSCMSOriginatorIdentifierOrKeyTemplate[] = {
  256.     { SEC_ASN1_CHOICE,
  257.   offsetof(NSSCMSOriginatorIdentifierOrKey,identifierType), NULL,
  258.   sizeof(NSSCMSOriginatorIdentifierOrKey) },
  259.     { SEC_ASN1_POINTER,
  260.   offsetof(NSSCMSOriginatorIdentifierOrKey,id.issuerAndSN),
  261.   CERT_IssuerAndSNTemplate,
  262.   NSSCMSOriginatorIDOrKey_IssuerSN },
  263.     { SEC_ASN1_EXPLICIT | SEC_ASN1_CONSTRUCTED | SEC_ASN1_CONTEXT_SPECIFIC | 1,
  264.   offsetof(NSSCMSOriginatorIdentifierOrKey,id.subjectKeyID),
  265.   SEC_PointerToOctetStringTemplate,
  266.   NSSCMSOriginatorIDOrKey_SubjectKeyID },
  267.     { SEC_ASN1_EXPLICIT | SEC_ASN1_CONSTRUCTED | SEC_ASN1_CONTEXT_SPECIFIC | 2,
  268.   offsetof(NSSCMSOriginatorIdentifierOrKey,id.originatorPublicKey),
  269.   NSSCMSOriginatorPublicKeyTemplate,
  270.   NSSCMSOriginatorIDOrKey_OriginatorPublicKey },
  271.     { 0 }
  272. };
  273. const SEC_ASN1Template NSSCMSRecipientKeyIdentifierTemplate[] = {
  274.     { SEC_ASN1_SEQUENCE,
  275.   0, NULL, sizeof(NSSCMSRecipientKeyIdentifier) },
  276.     { SEC_ASN1_OCTET_STRING,
  277.   offsetof(NSSCMSRecipientKeyIdentifier,subjectKeyIdentifier) },
  278.     { SEC_ASN1_OPTIONAL | SEC_ASN1_OCTET_STRING,
  279.   offsetof(NSSCMSRecipientKeyIdentifier,date) },
  280.     { SEC_ASN1_OPTIONAL | SEC_ASN1_OCTET_STRING,
  281.   offsetof(NSSCMSRecipientKeyIdentifier,other) },
  282.     { 0 }
  283. };
  284. static const SEC_ASN1Template NSSCMSKeyAgreeRecipientIdentifierTemplate[] = {
  285.     { SEC_ASN1_CHOICE,
  286.   offsetof(NSSCMSKeyAgreeRecipientIdentifier,identifierType), NULL,
  287.   sizeof(NSSCMSKeyAgreeRecipientIdentifier) },
  288.     { SEC_ASN1_POINTER,
  289.   offsetof(NSSCMSKeyAgreeRecipientIdentifier,id.issuerAndSN),
  290.   CERT_IssuerAndSNTemplate,
  291.   NSSCMSKeyAgreeRecipientID_IssuerSN },
  292.     { SEC_ASN1_CONSTRUCTED | SEC_ASN1_CONTEXT_SPECIFIC | 0,
  293.   offsetof(NSSCMSKeyAgreeRecipientIdentifier,id.recipientKeyIdentifier),
  294.   NSSCMSRecipientKeyIdentifierTemplate,
  295.   NSSCMSKeyAgreeRecipientID_RKeyID },
  296.     { 0 }
  297. };
  298. static const SEC_ASN1Template NSSCMSRecipientEncryptedKeyTemplate[] = {
  299.     { SEC_ASN1_SEQUENCE,
  300.   0, NULL, sizeof(NSSCMSRecipientEncryptedKey) },
  301.     { SEC_ASN1_INLINE,
  302.   offsetof(NSSCMSRecipientEncryptedKey,recipientIdentifier),
  303.   NSSCMSKeyAgreeRecipientIdentifierTemplate },
  304.     { SEC_ASN1_INLINE,
  305.   offsetof(NSSCMSRecipientEncryptedKey,encKey),
  306.   SEC_BitStringTemplate },
  307.     { 0 }
  308. };
  309. static const SEC_ASN1Template NSSCMSKeyAgreeRecipientInfoTemplate[] = {
  310.     { SEC_ASN1_SEQUENCE,
  311.   0, NULL, sizeof(NSSCMSKeyAgreeRecipientInfo) },
  312.     { SEC_ASN1_INTEGER,
  313.   offsetof(NSSCMSKeyAgreeRecipientInfo,version) },
  314.     { SEC_ASN1_EXPLICIT | SEC_ASN1_CONSTRUCTED | SEC_ASN1_CONTEXT_SPECIFIC | 0,
  315.   offsetof(NSSCMSKeyAgreeRecipientInfo,originatorIdentifierOrKey),
  316.   NSSCMSOriginatorIdentifierOrKeyTemplate },
  317.     { SEC_ASN1_OPTIONAL | SEC_ASN1_CONSTRUCTED | SEC_ASN1_EXPLICIT |
  318.     SEC_ASN1_CONTEXT_SPECIFIC | 1,
  319.   offsetof(NSSCMSKeyAgreeRecipientInfo,ukm),
  320.   SEC_OctetStringTemplate },
  321.     { SEC_ASN1_INLINE,
  322.   offsetof(NSSCMSKeyAgreeRecipientInfo,keyEncAlg),
  323.   SECOID_AlgorithmIDTemplate },
  324.     { SEC_ASN1_SEQUENCE_OF,
  325.   offsetof(NSSCMSKeyAgreeRecipientInfo,recipientEncryptedKeys),
  326.   NSSCMSRecipientEncryptedKeyTemplate },
  327.     { 0 }
  328. };
  329. /* -----------------------------------------------------------------------------
  330.  * KEK recipient info
  331.  */
  332. static const SEC_ASN1Template NSSCMSKEKIdentifierTemplate[] = {
  333.     { SEC_ASN1_SEQUENCE,
  334.   0, NULL, sizeof(NSSCMSKEKIdentifier) },
  335.     { SEC_ASN1_OCTET_STRING,
  336.   offsetof(NSSCMSKEKIdentifier,keyIdentifier) },
  337.     { SEC_ASN1_OPTIONAL | SEC_ASN1_OCTET_STRING,
  338.   offsetof(NSSCMSKEKIdentifier,date) },
  339.     { SEC_ASN1_OPTIONAL | SEC_ASN1_OCTET_STRING,
  340.   offsetof(NSSCMSKEKIdentifier,other) },
  341.     { 0 }
  342. };
  343. static const SEC_ASN1Template NSSCMSKEKRecipientInfoTemplate[] = {
  344.     { SEC_ASN1_SEQUENCE,
  345.   0, NULL, sizeof(NSSCMSKEKRecipientInfo) },
  346.     { SEC_ASN1_INTEGER,
  347.   offsetof(NSSCMSKEKRecipientInfo,version) },
  348.     { SEC_ASN1_INLINE,
  349.   offsetof(NSSCMSKEKRecipientInfo,kekIdentifier),
  350.   NSSCMSKEKIdentifierTemplate },
  351.     { SEC_ASN1_INLINE,
  352.   offsetof(NSSCMSKEKRecipientInfo,keyEncAlg),
  353.   SECOID_AlgorithmIDTemplate },
  354.     { SEC_ASN1_OCTET_STRING,
  355.   offsetof(NSSCMSKEKRecipientInfo,encKey) },
  356.     { 0 }
  357. };
  358. /* -----------------------------------------------------------------------------
  359.  * recipient info
  360.  */
  361. const SEC_ASN1Template NSSCMSRecipientInfoTemplate[] = {
  362.     { SEC_ASN1_CHOICE,
  363.   offsetof(NSSCMSRecipientInfo,recipientInfoType), NULL,
  364.   sizeof(NSSCMSRecipientInfo) },
  365.     { SEC_ASN1_EXPLICIT | SEC_ASN1_CONSTRUCTED | SEC_ASN1_CONTEXT_SPECIFIC | 1,
  366.   offsetof(NSSCMSRecipientInfo,ri.keyAgreeRecipientInfo),
  367.   NSSCMSKeyAgreeRecipientInfoTemplate,
  368.   NSSCMSRecipientInfoID_KeyAgree },
  369.     { SEC_ASN1_EXPLICIT | SEC_ASN1_CONSTRUCTED | SEC_ASN1_CONTEXT_SPECIFIC | 2,
  370.   offsetof(NSSCMSRecipientInfo,ri.kekRecipientInfo),
  371.   NSSCMSKEKRecipientInfoTemplate,
  372.   NSSCMSRecipientInfoID_KEK },
  373.     { SEC_ASN1_INLINE,
  374.   offsetof(NSSCMSRecipientInfo,ri.keyTransRecipientInfo),
  375.   NSSCMSKeyTransRecipientInfoTemplate,
  376.   NSSCMSRecipientInfoID_KeyTrans },
  377.     { 0 }
  378. };
  379. /* -----------------------------------------------------------------------------
  380.  *
  381.  */
  382. const SEC_ASN1Template NSSCMSDigestedDataTemplate[] = {
  383.     { SEC_ASN1_SEQUENCE | SEC_ASN1_MAY_STREAM,
  384.   0, NULL, sizeof(NSSCMSDigestedData) },
  385.     { SEC_ASN1_INTEGER,
  386.   offsetof(NSSCMSDigestedData,version) },
  387.     { SEC_ASN1_INLINE,
  388.   offsetof(NSSCMSDigestedData,digestAlg),
  389.   SECOID_AlgorithmIDTemplate },
  390.     { SEC_ASN1_INLINE,
  391.   offsetof(NSSCMSDigestedData,contentInfo),
  392.   NSSCMSEncapsulatedContentInfoTemplate },
  393.     { SEC_ASN1_OCTET_STRING,
  394.   offsetof(NSSCMSDigestedData,digest) },
  395.     { 0 }
  396. };
  397. const SEC_ASN1Template NSS_PointerToCMSDigestedDataTemplate[] = {
  398.     { SEC_ASN1_POINTER, 0, NSSCMSDigestedDataTemplate }
  399. };
  400. const SEC_ASN1Template NSSCMSEncryptedDataTemplate[] = {
  401.     { SEC_ASN1_SEQUENCE | SEC_ASN1_MAY_STREAM,
  402.   0, NULL, sizeof(NSSCMSEncryptedData) },
  403.     { SEC_ASN1_INTEGER,
  404.   offsetof(NSSCMSEncryptedData,version) },
  405.     { SEC_ASN1_INLINE,
  406.   offsetof(NSSCMSEncryptedData,contentInfo),
  407.   NSSCMSEncryptedContentInfoTemplate },
  408.     { SEC_ASN1_OPTIONAL | SEC_ASN1_CONSTRUCTED | SEC_ASN1_CONTEXT_SPECIFIC | 1,
  409.   offsetof(NSSCMSEncryptedData,unprotectedAttr),
  410.   nss_cms_set_of_attribute_template },
  411.     { 0 }
  412. };
  413. const SEC_ASN1Template NSS_PointerToCMSEncryptedDataTemplate[] = {
  414.     { SEC_ASN1_POINTER, 0, NSSCMSEncryptedDataTemplate }
  415. };
  416. /* -----------------------------------------------------------------------------
  417.  * FORTEZZA KEA
  418.  */
  419. const SEC_ASN1Template NSS_SMIMEKEAParamTemplateSkipjack[] = {
  420. { SEC_ASN1_SEQUENCE,
  421.   0, NULL, sizeof(NSSCMSSMIMEKEAParameters) },
  422. { SEC_ASN1_OCTET_STRING /* | SEC_ASN1_OPTIONAL */,
  423.   offsetof(NSSCMSSMIMEKEAParameters,originatorKEAKey) },
  424. { SEC_ASN1_OCTET_STRING,
  425.   offsetof(NSSCMSSMIMEKEAParameters,originatorRA) },
  426. { 0 }
  427. };
  428. const SEC_ASN1Template NSS_SMIMEKEAParamTemplateNoSkipjack[] = {
  429. { SEC_ASN1_SEQUENCE,
  430.   0, NULL, sizeof(NSSCMSSMIMEKEAParameters) },
  431. { SEC_ASN1_OCTET_STRING /* | SEC_ASN1_OPTIONAL */,
  432.   offsetof(NSSCMSSMIMEKEAParameters,originatorKEAKey) },
  433. { SEC_ASN1_OCTET_STRING,
  434.   offsetof(NSSCMSSMIMEKEAParameters,originatorRA) },
  435. { SEC_ASN1_OCTET_STRING  | SEC_ASN1_OPTIONAL ,
  436.   offsetof(NSSCMSSMIMEKEAParameters,nonSkipjackIV) },
  437. { 0 }
  438. };
  439. const SEC_ASN1Template NSS_SMIMEKEAParamTemplateAllParams[] = {
  440. { SEC_ASN1_SEQUENCE,
  441.   0, NULL, sizeof(NSSCMSSMIMEKEAParameters) },
  442. { SEC_ASN1_OCTET_STRING /* | SEC_ASN1_OPTIONAL */,
  443.   offsetof(NSSCMSSMIMEKEAParameters,originatorKEAKey) },
  444. { SEC_ASN1_OCTET_STRING,
  445.   offsetof(NSSCMSSMIMEKEAParameters,originatorRA) },
  446. { SEC_ASN1_OCTET_STRING  | SEC_ASN1_OPTIONAL ,
  447.   offsetof(NSSCMSSMIMEKEAParameters,nonSkipjackIV) },
  448. { SEC_ASN1_OCTET_STRING  | SEC_ASN1_OPTIONAL ,
  449.   offsetof(NSSCMSSMIMEKEAParameters,bulkKeySize) },
  450. { 0 }
  451. };
  452. const SEC_ASN1Template *
  453. nss_cms_get_kea_template(NSSCMSKEATemplateSelector whichTemplate)
  454. {
  455. const SEC_ASN1Template *returnVal = NULL;
  456. switch(whichTemplate)
  457. {
  458. case NSSCMSKEAUsesNonSkipjack:
  459. returnVal = NSS_SMIMEKEAParamTemplateNoSkipjack;
  460. break;
  461. case NSSCMSKEAUsesSkipjack:
  462. returnVal = NSS_SMIMEKEAParamTemplateSkipjack;
  463. break;
  464. case NSSCMSKEAUsesNonSkipjackWithPaddedEncKey:
  465. default:
  466. returnVal = NSS_SMIMEKEAParamTemplateAllParams;
  467. break;
  468. }
  469. return returnVal;
  470. }
  471. /* -----------------------------------------------------------------------------
  472.  *
  473.  */
  474. static const SEC_ASN1Template *
  475. nss_cms_choose_content_template(void *src_or_dest, PRBool encoding)
  476. {
  477.     const SEC_ASN1Template *theTemplate;
  478.     NSSCMSContentInfo *cinfo;
  479.     PORT_Assert (src_or_dest != NULL);
  480.     if (src_or_dest == NULL)
  481. return NULL;
  482.     cinfo = (NSSCMSContentInfo *)src_or_dest;
  483.     switch (NSS_CMSContentInfo_GetContentTypeTag(cinfo)) {
  484.     default:
  485. theTemplate = SEC_PointerToAnyTemplate;
  486. break;
  487.     case SEC_OID_PKCS7_DATA:
  488. theTemplate = SEC_PointerToOctetStringTemplate;
  489. break;
  490.     case SEC_OID_PKCS7_SIGNED_DATA:
  491. theTemplate = NSS_PointerToCMSSignedDataTemplate;
  492. break;
  493.     case SEC_OID_PKCS7_ENVELOPED_DATA:
  494. theTemplate = NSS_PointerToCMSEnvelopedDataTemplate;
  495. break;
  496.     case SEC_OID_PKCS7_DIGESTED_DATA:
  497. theTemplate = NSS_PointerToCMSDigestedDataTemplate;
  498. break;
  499.     case SEC_OID_PKCS7_ENCRYPTED_DATA:
  500. theTemplate = NSS_PointerToCMSEncryptedDataTemplate;
  501. break;
  502.     }
  503.     return theTemplate;
  504. }