]> git.saurik.com Git - apple/security.git/blame - OSX/libsecurity_pkcs12/lib/pkcs7Templates.cpp
Security-57740.31.2.tar.gz
[apple/security.git] / OSX / libsecurity_pkcs12 / lib / pkcs7Templates.cpp
CommitLineData
b1ab9ed8 1/*
d8f41ccd 2 * Copyright (c) 2003-2004,2011,2014 Apple Inc. All Rights Reserved.
b1ab9ed8
A
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 "pkcs7Templates.h"
28#include <Security/keyTemplates.h> /* NSS_AlgorithmIDTemplate */
29#include <Security/SecAsn1Templates.h>
30#include <security_asn1/nssUtils.h>
31#include "pkcs12Utils.h"
32#include <Security/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,
822b670c
A
100 const char *buf, // on decode, tag byte and length
101 size_t len,
b1ab9ed8
A
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(nssCompareCssmData(&dci->contentType,
110 &CSSMOID_PKCS7_Data)) {
111 templ = kSecAsn1PointerToOctetStringTemplate;
112 type = CT_Data;
113 }
114 else if(nssCompareCssmData(&dci->contentType,
115 &CSSMOID_PKCS7_EncryptedData)) {
116 templ = NSS_P7_PtrToEncryptedDataTemplate;
117 type = CT_EncryptedData;
118 }
119 else if(nssCompareCssmData(&dci->contentType,
120 &CSSMOID_PKCS7_SignedData)) {
121 templ = NSS_P7_PtrToSignedDataTemplate;
122 type = CT_SignedData;
123 }
124 else if(nssCompareCssmData(&dci->contentType,
125 &CSSMOID_PKCS7_EnvelopedData)) {
126 templ = NSS_P7_PtrToEnvelDataTemplate;
127 type = CT_EnvData;
128 }
129 else if(nssCompareCssmData(&dci->contentType,
130 &CSSMOID_PKCS7_SignedAndEnvelopedData)) {
131 templ = NSS_P7_PtrToSignEnvelDataTemplate;
132 type = CT_SignedEnvData;
133 }
134 else if(nssCompareCssmData(&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
149static const SecAsn1TemplateChooserPtr NSS_P7_ContentInfoChooserPtr =
150 NSS_P7_ContentInfoChooser;
151
152const 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};