]> git.saurik.com Git - apple/security.git/blob - OSX/libsecurity_asn1/lib/pkcs7Templates.c
Security-59754.41.1.tar.gz
[apple/security.git] / OSX / libsecurity_asn1 / lib / pkcs7Templates.c
1 /*
2 * Copyright (c) 2003-2004,2008,2010,2012 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
34 const 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 */
49 const 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 */
64 const 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
78 const 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
89 const SecAsn1Template NSS_P7_PtrToEncryptedDataTemplate[] = {
90 { SEC_ASN1_POINTER, 0, NSS_P7_EncryptedDataTemplate }
91 };
92
93 /*
94 * Decoded ContentInfo via SEC_ASN1_DYNAMIC
95 */
96
97 static const SecAsn1Template * NSS_P7_ContentInfoChooser(
98 void *arg, // --> NSS_P7_DecodedContentInfo
99 Boolean enc,
100 const char *buf, // on decode, tag byte and length
101 size_t len,
102 void *dest) // --> NSS_P7_DecodedContentInfo.content
103 {
104 NSS_P7_DecodedContentInfo *dci =
105 (NSS_P7_DecodedContentInfo *)arg;
106 const SecAsn1Template *templ = NULL;
107 NSS_P7_CI_Type type = CT_None;
108
109 if(nssCompareSecAsn1Items(&dci->contentType,
110 &CSSMOID_PKCS7_Data)) {
111 templ = kSecAsn1PointerToOctetStringTemplate;
112 type = CT_Data;
113 }
114 else if(nssCompareSecAsn1Items(&dci->contentType,
115 &CSSMOID_PKCS7_EncryptedData)) {
116 templ = NSS_P7_PtrToEncryptedDataTemplate;
117 type = CT_EncryptedData;
118 }
119 else if(nssCompareSecAsn1Items(&dci->contentType,
120 &CSSMOID_PKCS7_SignedData)) {
121 templ = NSS_P7_PtrToSignedDataTemplate;
122 type = CT_SignedData;
123 }
124 else if(nssCompareSecAsn1Items(&dci->contentType,
125 &CSSMOID_PKCS7_EnvelopedData)) {
126 templ = NSS_P7_PtrToEnvelDataTemplate;
127 type = CT_EnvData;
128 }
129 else if(nssCompareSecAsn1Items(&dci->contentType,
130 &CSSMOID_PKCS7_SignedAndEnvelopedData)) {
131 templ = NSS_P7_PtrToSignEnvelDataTemplate;
132 type = CT_SignedEnvData;
133 }
134 else if(nssCompareSecAsn1Items(&dci->contentType,
135 &CSSMOID_PKCS7_DigestedData)) {
136 templ = NSS_P7_PtrToDigestedDataTemplate;
137 type = CT_DigestData;
138 }
139 /* add more here when we implement them */
140 else {
141 return kSecAsn1PointerToAnyTemplate;
142 }
143 if(!enc) {
144 dci->type = type;
145 }
146 return templ;
147 }
148
149 static const SecAsn1TemplateChooserPtr NSS_P7_ContentInfoChooserPtr =
150 NSS_P7_ContentInfoChooser;
151
152 const SecAsn1Template NSS_P7_DecodedContentInfoTemplate[] = {
153 { SEC_ASN1_SEQUENCE,
154 0, NULL, sizeof(NSS_P7_DecodedContentInfo) },
155 { SEC_ASN1_OBJECT_ID,
156 offsetof(NSS_P7_DecodedContentInfo,contentType) },
157 { SEC_ASN1_OPTIONAL | SEC_ASN1_DYNAMIC |
158 SEC_ASN1_EXPLICIT | SEC_ASN1_CONSTRUCTED |
159 SEC_ASN1_CONTEXT_SPECIFIC | 0,
160 offsetof(NSS_P7_DecodedContentInfo,content),
161 &NSS_P7_ContentInfoChooserPtr },
162 { 0 }
163 };