]> git.saurik.com Git - apple/security.git/blob - OSX/libsecurity_asn1/lib/keyTemplates.c
Security-57336.10.29.tar.gz
[apple/security.git] / OSX / libsecurity_asn1 / lib / keyTemplates.c
1 /*
2 * Copyright (c) 2003-2006,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 * keyTemplate.cpp - ASN1 templates for asymmetric keys and related
24 * structs.
25 */
26
27 #include "SecAsn1Templates.h"
28 #include <stddef.h>
29 #include "keyTemplates.h"
30
31 /* AlgorithmIdentifier : SecAsn1AlgId */
32 const SecAsn1Template kSecAsn1AlgorithmIDTemplate[] = {
33 { SEC_ASN1_SEQUENCE,
34 0, NULL, sizeof(SecAsn1AlgId) },
35 { SEC_ASN1_OBJECT_ID,
36 offsetof(SecAsn1AlgId,algorithm), },
37 { SEC_ASN1_OPTIONAL | SEC_ASN1_ANY,
38 offsetof(SecAsn1AlgId,parameters), },
39 { 0, }
40 };
41
42 /* SubjectPublicKeyInfo : SecAsn1PubKeyInfo */
43 const SecAsn1Template kSecAsn1SubjectPublicKeyInfoTemplate[] = {
44 { SEC_ASN1_SEQUENCE,
45 0, NULL, sizeof(SecAsn1PubKeyInfo) },
46 { SEC_ASN1_INLINE,
47 offsetof(SecAsn1PubKeyInfo,algorithm),
48 kSecAsn1AlgorithmIDTemplate },
49 { SEC_ASN1_BIT_STRING,
50 offsetof(SecAsn1PubKeyInfo,subjectPublicKey), },
51 { 0, }
52 };
53
54 /* Attribute : NSS_Attribute */
55 const SecAsn1Template kSecAsn1AttributeTemplate[] = {
56 { SEC_ASN1_SEQUENCE,
57 0, NULL, sizeof(NSS_Attribute) },
58 { SEC_ASN1_OBJECT_ID, offsetof(NSS_Attribute, attrType) },
59 { SEC_ASN1_SET_OF, offsetof(NSS_Attribute, attrValue),
60 kSecAsn1AnyTemplate },
61 { 0 }
62 };
63
64 const SecAsn1Template kSecAsn1SetOfAttributeTemplate[] = {
65 { SEC_ASN1_SET_OF, 0, kSecAsn1AttributeTemplate },
66 };
67
68 /* PKCS8 PrivateKeyInfo : NSS_PrivateKeyInfo */
69 const SecAsn1Template kSecAsn1PrivateKeyInfoTemplate[] = {
70 { SEC_ASN1_SEQUENCE, 0, NULL, sizeof(NSS_PrivateKeyInfo) },
71 { SEC_ASN1_INTEGER, offsetof(NSS_PrivateKeyInfo,version) },
72 { SEC_ASN1_INLINE, offsetof(NSS_PrivateKeyInfo,algorithm),
73 kSecAsn1AlgorithmIDTemplate },
74 { SEC_ASN1_OCTET_STRING, offsetof(NSS_PrivateKeyInfo,privateKey) },
75 { SEC_ASN1_OPTIONAL | SEC_ASN1_CONSTRUCTED |
76 SEC_ASN1_CONTEXT_SPECIFIC | 0,
77 offsetof(NSS_PrivateKeyInfo,attributes),
78 kSecAsn1SetOfAttributeTemplate },
79 { 0 }
80 };
81
82 /* NSS_EncryptedPrivateKeyInfo */
83 const SecAsn1Template kSecAsn1EncryptedPrivateKeyInfoTemplate[] = {
84 { SEC_ASN1_SEQUENCE, 0, NULL, sizeof(NSS_EncryptedPrivateKeyInfo) },
85 { SEC_ASN1_INLINE,
86 offsetof(NSS_EncryptedPrivateKeyInfo,algorithm),
87 kSecAsn1AlgorithmIDTemplate },
88 { SEC_ASN1_OCTET_STRING,
89 offsetof(NSS_EncryptedPrivateKeyInfo,encryptedData) },
90 { 0 }
91 };
92
93 /* DigestInfo: NSS_DigestInfo */
94 const SecAsn1Template kSecAsn1DigestInfoTemplate[] = {
95 { SEC_ASN1_SEQUENCE, 0, NULL, sizeof(NSS_DigestInfo) },
96 { SEC_ASN1_INLINE, offsetof(NSS_DigestInfo,digestAlgorithm),
97 kSecAsn1AlgorithmIDTemplate },
98 { SEC_ASN1_OCTET_STRING, offsetof(NSS_DigestInfo,digest) },
99 { 0 }
100 };
101
102 // MARK: -
103 // MARK: *** RSA ***
104
105 /*** RSA public key, PKCS1 format : NSS_RSAPublicKeyPKCS1 ***/
106 const SecAsn1Template kSecAsn1RSAPublicKeyPKCS1Template[] = {
107 { SEC_ASN1_SEQUENCE, 0, NULL, sizeof(NSS_RSAPublicKeyPKCS1) },
108 { SEC_ASN1_INTEGER, offsetof(NSS_RSAPublicKeyPKCS1,modulus) },
109 { SEC_ASN1_INTEGER, offsetof(NSS_RSAPublicKeyPKCS1,publicExponent) },
110 { 0, }
111 };
112
113 /*** RSA private key key, PKCS1 format : NSS_RSAPrivateKeyPKCS1 ***/
114 const SecAsn1Template kSecAsn1RSAPrivateKeyPKCS1Template[] = {
115 { SEC_ASN1_SEQUENCE, 0, NULL, sizeof(NSS_RSAPrivateKeyPKCS1) },
116 { SEC_ASN1_INTEGER, offsetof(NSS_RSAPrivateKeyPKCS1,version) },
117 { SEC_ASN1_INTEGER, offsetof(NSS_RSAPrivateKeyPKCS1,modulus) },
118 { SEC_ASN1_INTEGER, offsetof(NSS_RSAPrivateKeyPKCS1,publicExponent) },
119 { SEC_ASN1_INTEGER, offsetof(NSS_RSAPrivateKeyPKCS1,privateExponent) },
120 { SEC_ASN1_INTEGER, offsetof(NSS_RSAPrivateKeyPKCS1,prime1) },
121 { SEC_ASN1_INTEGER, offsetof(NSS_RSAPrivateKeyPKCS1,prime2) },
122 { SEC_ASN1_INTEGER, offsetof(NSS_RSAPrivateKeyPKCS1,exponent1) },
123 { SEC_ASN1_INTEGER, offsetof(NSS_RSAPrivateKeyPKCS1,exponent2) },
124 { SEC_ASN1_INTEGER, offsetof(NSS_RSAPrivateKeyPKCS1,coefficient) },
125 { 0, }
126 };
127
128 // MARK: -
129 // MARK: *** Diffie-Hellman ***
130
131 /****
132 **** Diffie-Hellman, from PKCS3.
133 ****/
134 const SecAsn1Template kSecAsn1DHParameterTemplate[] = {
135 { SEC_ASN1_SEQUENCE, 0, NULL, sizeof(NSS_DHParameter) },
136 { SEC_ASN1_INTEGER, offsetof(NSS_DHParameter,prime) },
137 { SEC_ASN1_INTEGER, offsetof(NSS_DHParameter,base) },
138 { SEC_ASN1_INTEGER | SEC_ASN1_OPTIONAL, offsetof(NSS_DHParameter,privateValueLength) },
139 { 0, }
140 };
141
142 const SecAsn1Template kSecAsn1DHParameterBlockTemplate[] = {
143 { SEC_ASN1_SEQUENCE, 0, NULL, sizeof(NSS_DHParameterBlock) },
144 { SEC_ASN1_OBJECT_ID, offsetof(NSS_DHParameterBlock, oid) },
145 { SEC_ASN1_INLINE,
146 offsetof(NSS_DHParameterBlock, params),
147 kSecAsn1DHParameterTemplate },
148 { 0, }
149 };
150
151 const SecAsn1Template kSecAsn1DHPrivateKeyTemplate[] = {
152 { SEC_ASN1_SEQUENCE, 0, NULL, sizeof(NSS_DHPrivateKey) },
153 { SEC_ASN1_OBJECT_ID, offsetof(NSS_DHPrivateKey, dhOid) },
154 { SEC_ASN1_INLINE,
155 offsetof(NSS_DHPrivateKey, params),
156 kSecAsn1DHParameterTemplate },
157 { SEC_ASN1_INTEGER, offsetof(NSS_DHPrivateKey,secretPart) },
158 { 0, }
159 };
160
161 /*
162 * Diffie-Hellman, X9.42 style.
163 */
164 const SecAsn1Template kSecAsn1DHValidationParamsTemplate[] = {
165 { SEC_ASN1_SEQUENCE, 0, NULL, sizeof(NSS_DHValidationParams) },
166 { SEC_ASN1_BIT_STRING, offsetof(NSS_DHValidationParams, seed) },
167 { SEC_ASN1_INTEGER, offsetof(NSS_DHValidationParams, pGenCounter) },
168 { 0, }
169 };
170
171 const SecAsn1Template kSecAsn1DHDomainParamsX942Template[] = {
172 { SEC_ASN1_SEQUENCE, 0, NULL, sizeof(NSS_DHDomainParamsX942) },
173 { SEC_ASN1_INTEGER, offsetof(NSS_DHDomainParamsX942, p) },
174 { SEC_ASN1_INTEGER, offsetof(NSS_DHDomainParamsX942, g) },
175 { SEC_ASN1_INTEGER, offsetof(NSS_DHDomainParamsX942, q) },
176 { SEC_ASN1_INTEGER | SEC_ASN1_OPTIONAL,
177 offsetof(NSS_DHDomainParamsX942, j) },
178 { SEC_ASN1_POINTER | SEC_ASN1_OPTIONAL,
179 offsetof(NSS_DHDomainParamsX942, valParams),
180 kSecAsn1DHValidationParamsTemplate },
181 { 0, }
182 };
183
184 const SecAsn1Template kSecAsn1DHAlgorithmIdentifierX942Template[] = {
185 { SEC_ASN1_SEQUENCE, 0, NULL, sizeof(NSS_DHAlgorithmIdentifierX942) },
186 { SEC_ASN1_OBJECT_ID, offsetof(NSS_DHAlgorithmIdentifierX942, oid) },
187 { SEC_ASN1_INLINE,
188 offsetof(NSS_DHAlgorithmIdentifierX942, params),
189 kSecAsn1DHDomainParamsX942Template },
190 { 0, }
191 };
192
193 const SecAsn1Template kSecAsn1DHPrivateKeyPKCS8Template[] = {
194 { SEC_ASN1_SEQUENCE, 0, NULL, sizeof(NSS_DHPrivateKeyPKCS8) },
195 { SEC_ASN1_INTEGER, offsetof(NSS_DHPrivateKeyPKCS8,version) },
196 { SEC_ASN1_INLINE, offsetof(NSS_DHPrivateKeyPKCS8,algorithm),
197 kSecAsn1DHAlgorithmIdentifierX942Template },
198 { SEC_ASN1_OCTET_STRING, offsetof(NSS_DHPrivateKeyPKCS8,privateKey) },
199 { SEC_ASN1_OPTIONAL | SEC_ASN1_CONSTRUCTED |
200 SEC_ASN1_CONTEXT_SPECIFIC | 0,
201 offsetof(NSS_DHPrivateKeyPKCS8,attributes),
202 kSecAsn1SetOfAttributeTemplate },
203 { 0 }
204 };
205
206 const SecAsn1Template kSecAsn1DHPublicKeyX509Template[] = {
207 { SEC_ASN1_SEQUENCE, 0, NULL, sizeof(NSS_DHPublicKeyX509) },
208 { SEC_ASN1_INLINE, offsetof(NSS_DHPublicKeyX509,algorithm),
209 kSecAsn1DHAlgorithmIdentifierX942Template },
210 { SEC_ASN1_BIT_STRING, offsetof(NSS_DHPublicKeyX509, publicKey) },
211 { 0 }
212 };
213
214 /* ECDSA Private key */
215 const SecAsn1Template kSecAsn1ECDSAPrivateKeyInfoTemplate[] = {
216 { SEC_ASN1_SEQUENCE, 0, NULL, sizeof(NSS_ECDSA_PrivateKey) },
217 { SEC_ASN1_INTEGER, offsetof(NSS_ECDSA_PrivateKey,version) },
218 { SEC_ASN1_OCTET_STRING, offsetof(NSS_ECDSA_PrivateKey,privateKey) },
219 { SEC_ASN1_OPTIONAL | SEC_ASN1_CONSTRUCTED | SEC_ASN1_EXPLICIT | SEC_ASN1_CONTEXT_SPECIFIC | 0,
220 offsetof(NSS_ECDSA_PrivateKey,params), kSecAsn1AnyTemplate},
221 { SEC_ASN1_OPTIONAL | SEC_ASN1_CONSTRUCTED | SEC_ASN1_EXPLICIT | SEC_ASN1_CONTEXT_SPECIFIC | 1,
222 offsetof(NSS_ECDSA_PrivateKey,pubKey), kSecAsn1BitStringTemplate },
223 { 0, }
224 };
225