2 * Copyright (c) 2003-2006,2008,2010-2012 Apple Inc. All Rights Reserved.
4 * @APPLE_LICENSE_HEADER_START@
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
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.
21 * @APPLE_LICENSE_HEADER_END@
23 * keyTemplate.cpp - ASN1 templates for asymmetric keys and related
27 #include "SecAsn1Templates.h"
29 #include "keyTemplates.h"
31 /* AlgorithmIdentifier : SecAsn1AlgId */
32 const SecAsn1Template kSecAsn1AlgorithmIDTemplate
[] = {
34 0, NULL
, sizeof(SecAsn1AlgId
) },
36 offsetof(SecAsn1AlgId
,algorithm
), },
37 { SEC_ASN1_OPTIONAL
| SEC_ASN1_ANY
,
38 offsetof(SecAsn1AlgId
,parameters
), },
42 /* SubjectPublicKeyInfo : SecAsn1PubKeyInfo */
43 const SecAsn1Template kSecAsn1SubjectPublicKeyInfoTemplate
[] = {
45 0, NULL
, sizeof(SecAsn1PubKeyInfo
) },
47 offsetof(SecAsn1PubKeyInfo
,algorithm
),
48 kSecAsn1AlgorithmIDTemplate
},
49 { SEC_ASN1_BIT_STRING
,
50 offsetof(SecAsn1PubKeyInfo
,subjectPublicKey
), },
54 /* Attribute : NSS_Attribute */
55 const SecAsn1Template kSecAsn1AttributeTemplate
[] = {
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
},
64 const SecAsn1Template kSecAsn1SetOfAttributeTemplate
[] = {
65 { SEC_ASN1_SET_OF
, 0, kSecAsn1AttributeTemplate
},
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
},
82 /* NSS_EncryptedPrivateKeyInfo */
83 const SecAsn1Template kSecAsn1EncryptedPrivateKeyInfoTemplate
[] = {
84 { SEC_ASN1_SEQUENCE
, 0, NULL
, sizeof(NSS_EncryptedPrivateKeyInfo
) },
86 offsetof(NSS_EncryptedPrivateKeyInfo
,algorithm
),
87 kSecAsn1AlgorithmIDTemplate
},
88 { SEC_ASN1_OCTET_STRING
,
89 offsetof(NSS_EncryptedPrivateKeyInfo
,encryptedData
) },
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
) },
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
) },
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
) },
129 // MARK: *** Diffie-Hellman ***
132 **** Diffie-Hellman, from PKCS3.
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
) },
142 const SecAsn1Template kSecAsn1DHParameterBlockTemplate
[] = {
143 { SEC_ASN1_SEQUENCE
, 0, NULL
, sizeof(NSS_DHParameterBlock
) },
144 { SEC_ASN1_OBJECT_ID
, offsetof(NSS_DHParameterBlock
, oid
) },
146 offsetof(NSS_DHParameterBlock
, params
),
147 kSecAsn1DHParameterTemplate
},
151 const SecAsn1Template kSecAsn1DHPrivateKeyTemplate
[] = {
152 { SEC_ASN1_SEQUENCE
, 0, NULL
, sizeof(NSS_DHPrivateKey
) },
153 { SEC_ASN1_OBJECT_ID
, offsetof(NSS_DHPrivateKey
, dhOid
) },
155 offsetof(NSS_DHPrivateKey
, params
),
156 kSecAsn1DHParameterTemplate
},
157 { SEC_ASN1_INTEGER
, offsetof(NSS_DHPrivateKey
,secretPart
) },
162 * Diffie-Hellman, X9.42 style.
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
) },
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
},
184 const SecAsn1Template kSecAsn1DHAlgorithmIdentifierX942Template
[] = {
185 { SEC_ASN1_SEQUENCE
, 0, NULL
, sizeof(NSS_DHAlgorithmIdentifierX942
) },
186 { SEC_ASN1_OBJECT_ID
, offsetof(NSS_DHAlgorithmIdentifierX942
, oid
) },
188 offsetof(NSS_DHAlgorithmIdentifierX942
, params
),
189 kSecAsn1DHDomainParamsX942Template
},
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
},
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
) },
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
},