1/*
2 * Copyright (c) 2003-2004,2008,2010 Apple Inc. All Rights Reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
11 * file.
12 *
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
20 *
21 * @APPLE_LICENSE_HEADER_END@
22 */
23/*
24 * pkcs7Templates.cpp
25 */
26
27#include <stddef.h>
28#include "pkcs7Templates.h"
29#include "keyTemplates.h"	/* NSS_AlgorithmIDTemplate */
30#include "SecAsn1Templates.h"
31#include "nssUtils.h"
32#include "oidsattr.h"
33
34const SecAsn1Template NSS_P7_DigestInfoTemplate[] = {
35    { SEC_ASN1_SEQUENCE,
36      0, NULL, sizeof(NSS_P7_DigestInfo) },
37    { SEC_ASN1_INLINE,
38	  offsetof(NSS_P7_DigestInfo,digestAlgorithm),
39	  kSecAsn1AlgorithmIDTemplate },
40    { SEC_ASN1_OCTET_STRING,
41	  offsetof(NSS_P7_DigestInfo,digest) },
42    { 0 }
43};
44
45/*
46 * Uninterpreted ContentInfo, with content stripped from its
47 * EXPLICIT CONTEXT_SPECIFIC wrapper
48 */
49const SecAsn1Template NSS_P7_RawContentInfoTemplate[] = {
50    { SEC_ASN1_SEQUENCE,
51      0, NULL, sizeof(NSS_P7_RawContentInfo) },
52    { SEC_ASN1_OBJECT_ID,
53	  offsetof(NSS_P7_RawContentInfo,contentType) },
54	{ SEC_ASN1_CONTEXT_SPECIFIC | SEC_ASN1_EXPLICIT |
55	  SEC_ASN1_CONSTRUCTED | SEC_ASN1_OPTIONAL | 0,
56	  offsetof(NSS_P7_RawContentInfo,content),
57	  kSecAsn1AnyTemplate },
58    { 0 }
59};
60
61/*
62 * Individual ContentInfo.content templates
63 */
64const SecAsn1Template NSS_P7_EncrContentInfoTemplate[] = {
65    { SEC_ASN1_SEQUENCE,
66      0, NULL, sizeof(NSS_P7_EncrContentInfo) },
67    { SEC_ASN1_OBJECT_ID,
68	  offsetof(NSS_P7_EncrContentInfo,contentType) },
69    { SEC_ASN1_INLINE,
70	  offsetof(NSS_P7_EncrContentInfo,encrAlg),
71	  kSecAsn1AlgorithmIDTemplate },
72	{ SEC_ASN1_CONTEXT_SPECIFIC | SEC_ASN1_OPTIONAL | 0,
73	  offsetof(NSS_P7_EncrContentInfo,encrContent),
74	  kSecAsn1OctetStringTemplate },
75    { 0 }
76};
77
78const SecAsn1Template NSS_P7_EncryptedDataTemplate[] = {
79    { SEC_ASN1_SEQUENCE,
80      0, NULL, sizeof(NSS_P7_EncryptedData) },
81	{ SEC_ASN1_INTEGER,
82	  offsetof(NSS_P7_EncryptedData,version) },
83	{ SEC_ASN1_INLINE,
84	  offsetof(NSS_P7_EncryptedData,contentInfo),
85	  NSS_P7_EncrContentInfoTemplate },
86   { 0 }
87};
88
89const SecAsn1Template NSS_P7_PtrToEncryptedDataTemplate[] = {
90    { SEC_ASN1_POINTER, 0, NSS_P7_EncryptedDataTemplate }
91};
92
93/*
94 * Decoded ContentInfo via SEC_ASN1_DYNAMIC
95 */
96
97static const SecAsn1Template * NSS_P7_ContentInfoChooser(
98	void *arg, 			// --> NSS_P7_DecodedContentInfo
99	Boolean enc,
100	const char *buf,	// on decode, tag byte
101	void *dest)			// --> NSS_P7_DecodedContentInfo.content
102{
103	NSS_P7_DecodedContentInfo *dci =
104		(NSS_P7_DecodedContentInfo *)arg;
105	const SecAsn1Template *templ = NULL;
106	NSS_P7_CI_Type type = CT_None;
107
108	if(nssCompareSecAsn1Items(&dci->contentType,
109			&CSSMOID_PKCS7_Data)) {
110		templ = kSecAsn1PointerToOctetStringTemplate;
111		type = CT_Data;
112	}
113	else if(nssCompareSecAsn1Items(&dci->contentType,
114			&CSSMOID_PKCS7_EncryptedData)) {
115		templ = NSS_P7_PtrToEncryptedDataTemplate;
116		type = CT_EncryptedData;
117	}
118	else if(nssCompareSecAsn1Items(&dci->contentType,
119			&CSSMOID_PKCS7_SignedData)) {
120		templ = NSS_P7_PtrToSignedDataTemplate;
121		type = CT_SignedData;
122	}
123	else if(nssCompareSecAsn1Items(&dci->contentType,
124			&CSSMOID_PKCS7_EnvelopedData)) {
125		templ = NSS_P7_PtrToEnvelDataTemplate;
126		type = CT_EnvData;
127	}
128	else if(nssCompareSecAsn1Items(&dci->contentType,
129			&CSSMOID_PKCS7_SignedAndEnvelopedData)) {
130		templ = NSS_P7_PtrToSignEnvelDataTemplate;
131		type = CT_SignedEnvData;
132	}
133	else if(nssCompareSecAsn1Items(&dci->contentType,
134			&CSSMOID_PKCS7_DigestedData)) {
135		templ = NSS_P7_PtrToDigestedDataTemplate;
136		type = CT_DigestData;
137	}
138	/* add more here when we implement them */
139	else {
140		return kSecAsn1PointerToAnyTemplate;
141	}
142	if(!enc) {
143		dci->type = type;
144	}
145	return templ;
146}
147
148static const SecAsn1TemplateChooserPtr NSS_P7_ContentInfoChooserPtr =
149	NSS_P7_ContentInfoChooser;
150
151const SecAsn1Template NSS_P7_DecodedContentInfoTemplate[] = {
152    { SEC_ASN1_SEQUENCE,
153      0, NULL, sizeof(NSS_P7_DecodedContentInfo) },
154    { SEC_ASN1_OBJECT_ID,
155	  offsetof(NSS_P7_DecodedContentInfo,contentType) },
156    { SEC_ASN1_OPTIONAL | SEC_ASN1_DYNAMIC |
157			SEC_ASN1_EXPLICIT | SEC_ASN1_CONSTRUCTED |
158			SEC_ASN1_CONTEXT_SPECIFIC | 0,
159	  offsetof(NSS_P7_DecodedContentInfo,content),
160	  &NSS_P7_ContentInfoChooserPtr },
161   { 0 }
162};
163