1/*
2 * Copyright (c) 2005-2007,2011,2014 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
25/*
26 * DER_Cert.c - support for decoding RSA keys
27 *
28 */
29
30#include <libDER/DER_Decode.h>
31#include <libDER/DER_Encode.h>
32#include <libDER/DER_Keys.h>
33#include <libDER/asn1Types.h>
34#include <libDER/libDER_config.h>
35
36#ifndef	DER_DECODE_ENABLE
37#error Please define DER_DECODE_ENABLE.
38#endif
39#if		DER_DECODE_ENABLE
40
41/*
42 * DERItemSpecs for decoding RSA keys.
43 */
44
45/* Algorithm Identifier */
46const DERItemSpec DERAlgorithmIdItemSpecs[] =
47{
48	{ DER_OFFSET(DERAlgorithmId, oid),
49			ASN1_OBJECT_ID,
50			DER_DEC_NO_OPTS },
51	{ DER_OFFSET(DERAlgorithmId, params),
52			0,				/* no tag - any */
53			DER_DEC_ASN_ANY | DER_DEC_OPTIONAL | DER_DEC_SAVE_DER }
54};
55const DERSize DERNumAlgorithmIdItemSpecs =
56	sizeof(DERAlgorithmIdItemSpecs) / sizeof(DERItemSpec);
57
58/* X509 SubjectPublicKeyInfo */
59const DERItemSpec DERSubjPubKeyInfoItemSpecs[] =
60{
61	{ DER_OFFSET(DERSubjPubKeyInfo, algId),
62			ASN1_CONSTR_SEQUENCE,
63			DER_DEC_NO_OPTS },
64	{ DER_OFFSET(DERSubjPubKeyInfo, pubKey),
65			ASN1_BIT_STRING,
66			DER_DEC_NO_OPTS },
67
68};
69const DERSize DERNumSubjPubKeyInfoItemSpecs =
70	sizeof(DERSubjPubKeyInfoItemSpecs) / sizeof(DERItemSpec);
71
72/*
73 * RSA private key in CRT format
74 */
75const DERItemSpec DERRSAPrivKeyCRTItemSpecs[] =
76{
77	/* version, n, e, d - skip */
78	{ 0,
79			ASN1_INTEGER,
80			DER_DEC_SKIP },
81	{ 0,
82			ASN1_INTEGER,
83			DER_DEC_SKIP },
84	{ 0,
85			ASN1_INTEGER,
86			DER_DEC_SKIP },
87	{ 0,
88			ASN1_INTEGER,
89			DER_DEC_SKIP },
90	{ DER_OFFSET(DERRSAPrivKeyCRT, p),
91			ASN1_INTEGER,
92			DER_DEC_NO_OPTS },
93	{ DER_OFFSET(DERRSAPrivKeyCRT, q),
94			ASN1_INTEGER,
95			DER_DEC_NO_OPTS },
96	{ DER_OFFSET(DERRSAPrivKeyCRT, dp),
97			ASN1_INTEGER,
98			DER_DEC_NO_OPTS },
99	{ DER_OFFSET(DERRSAPrivKeyCRT, dq),
100			ASN1_INTEGER,
101			DER_DEC_NO_OPTS },
102	{ DER_OFFSET(DERRSAPrivKeyCRT, qInv),
103			ASN1_INTEGER,
104			DER_DEC_NO_OPTS },
105	/* ignore the (optional) rest */
106};
107const DERSize DERNumRSAPrivKeyCRTItemSpecs =
108	sizeof(DERRSAPrivKeyCRTItemSpecs) / sizeof(DERItemSpec);
109
110#endif	/* DER_DECODE_ENABLE */
111
112#if		DER_DECODE_ENABLE || DER_ENCODE_ENABLE
113
114/* RSA public key in PKCS1 format - encode and decode */
115const DERItemSpec DERRSAPubKeyPKCS1ItemSpecs[] =
116{
117	{ DER_OFFSET(DERRSAPubKeyPKCS1, modulus),
118			ASN1_INTEGER,
119			DER_DEC_NO_OPTS | DER_ENC_SIGNED_INT },
120	{ DER_OFFSET(DERRSAPubKeyPKCS1, pubExponent),
121			ASN1_INTEGER,
122			DER_DEC_NO_OPTS | DER_ENC_SIGNED_INT },
123};
124const DERSize DERNumRSAPubKeyPKCS1ItemSpecs =
125	sizeof(DERRSAPubKeyPKCS1ItemSpecs) / sizeof(DERItemSpec);
126
127/* RSA public key in Apple custome format with reciprocal - encode and decode */
128const DERItemSpec DERRSAPubKeyAppleItemSpecs[] =
129{
130	{ DER_OFFSET(DERRSAPubKeyApple, modulus),
131			ASN1_INTEGER,
132			DER_DEC_NO_OPTS | DER_ENC_SIGNED_INT },
133	{ DER_OFFSET(DERRSAPubKeyApple, reciprocal),
134			ASN1_INTEGER,
135			DER_DEC_NO_OPTS | DER_ENC_SIGNED_INT },
136	{ DER_OFFSET(DERRSAPubKeyApple, pubExponent),
137			ASN1_INTEGER,
138			DER_DEC_NO_OPTS | DER_ENC_SIGNED_INT },
139};
140const DERSize DERNumRSAPubKeyAppleItemSpecs =
141	sizeof(DERRSAPubKeyAppleItemSpecs) / sizeof(DERItemSpec);
142
143
144#endif		/* DER_DECODE_ENABLE || DER_ENCODE_ENABLE */
145
146#ifndef	DER_ENCODE_ENABLE
147#error Please define DER_ENCODE_ENABLE.
148#endif
149
150#if		DER_ENCODE_ENABLE
151
152/* RSA Key Pair, encode only */
153const DERItemSpec DERRSAKeyPairItemSpecs[] =
154{
155	{ DER_OFFSET(DERRSAKeyPair, version),
156			ASN1_INTEGER,
157			DER_ENC_SIGNED_INT },
158	{ DER_OFFSET(DERRSAKeyPair, n),
159			ASN1_INTEGER,
160			DER_ENC_SIGNED_INT },
161	{ DER_OFFSET(DERRSAKeyPair, e),
162			ASN1_INTEGER,
163			DER_ENC_SIGNED_INT },
164	{ DER_OFFSET(DERRSAKeyPair, d),
165			ASN1_INTEGER,
166			DER_ENC_SIGNED_INT },
167	{ DER_OFFSET(DERRSAKeyPair, p),
168			ASN1_INTEGER,
169			DER_ENC_SIGNED_INT },
170	{ DER_OFFSET(DERRSAKeyPair, q),
171			ASN1_INTEGER,
172			DER_ENC_SIGNED_INT },
173	{ DER_OFFSET(DERRSAKeyPair, dp),
174			ASN1_INTEGER,
175			DER_ENC_SIGNED_INT },
176	{ DER_OFFSET(DERRSAKeyPair, dq),
177			ASN1_INTEGER,
178			DER_ENC_SIGNED_INT },
179	{ DER_OFFSET(DERRSAKeyPair, qInv),
180			ASN1_INTEGER,
181			DER_ENC_SIGNED_INT },
182};
183
184const DERSize DERNumRSAKeyPairItemSpecs =
185	sizeof(DERRSAKeyPairItemSpecs) / sizeof(DERItemSpec);
186
187#endif	/* DER_ENCODE_ENABLE */
188
189