]> git.saurik.com Git - apple/security.git/blob - libsecurity_keychain/libDER/libDER/DER_Keys.c
Security-55163.44.tar.gz
[apple/security.git] / libsecurity_keychain / libDER / libDER / DER_Keys.c
1 /* Copyright (c) 2005-2007 Apple Inc. All Rights Reserved. */
2
3 /*
4 * DER_Cert.c - support for decoding RSA keys
5 *
6 * Created Nov. 8 2005 by Doug Mitchell.
7 */
8
9 #include <libDER/DER_Decode.h>
10 #include <libDER/DER_Encode.h>
11 #include <libDER/DER_Keys.h>
12 #include <libDER/asn1Types.h>
13 #include <libDER/libDER_config.h>
14
15 #ifndef DER_DECODE_ENABLE
16 #error Please define DER_DECODE_ENABLE.
17 #endif
18 #if DER_DECODE_ENABLE
19
20 /*
21 * DERItemSpecs for decoding RSA keys.
22 */
23
24 /* Algorithm Identifier */
25 const DERItemSpec DERAlgorithmIdItemSpecs[] =
26 {
27 { DER_OFFSET(DERAlgorithmId, oid),
28 ASN1_OBJECT_ID,
29 DER_DEC_NO_OPTS },
30 { DER_OFFSET(DERAlgorithmId, params),
31 0, /* no tag - any */
32 DER_DEC_ASN_ANY | DER_DEC_OPTIONAL | DER_DEC_SAVE_DER }
33 };
34 const DERSize DERNumAlgorithmIdItemSpecs =
35 sizeof(DERAlgorithmIdItemSpecs) / sizeof(DERItemSpec);
36
37 /* X509 SubjectPublicKeyInfo */
38 const DERItemSpec DERSubjPubKeyInfoItemSpecs[] =
39 {
40 { DER_OFFSET(DERSubjPubKeyInfo, algId),
41 ASN1_CONSTR_SEQUENCE,
42 DER_DEC_NO_OPTS },
43 { DER_OFFSET(DERSubjPubKeyInfo, pubKey),
44 ASN1_BIT_STRING,
45 DER_DEC_NO_OPTS },
46
47 };
48 const DERSize DERNumSubjPubKeyInfoItemSpecs =
49 sizeof(DERSubjPubKeyInfoItemSpecs) / sizeof(DERItemSpec);
50
51 /*
52 * RSA private key in CRT format
53 */
54 const DERItemSpec DERRSAPrivKeyCRTItemSpecs[] =
55 {
56 /* version, n, e, d - skip */
57 { 0,
58 ASN1_INTEGER,
59 DER_DEC_SKIP },
60 { 0,
61 ASN1_INTEGER,
62 DER_DEC_SKIP },
63 { 0,
64 ASN1_INTEGER,
65 DER_DEC_SKIP },
66 { 0,
67 ASN1_INTEGER,
68 DER_DEC_SKIP },
69 { DER_OFFSET(DERRSAPrivKeyCRT, p),
70 ASN1_INTEGER,
71 DER_DEC_NO_OPTS },
72 { DER_OFFSET(DERRSAPrivKeyCRT, q),
73 ASN1_INTEGER,
74 DER_DEC_NO_OPTS },
75 { DER_OFFSET(DERRSAPrivKeyCRT, dp),
76 ASN1_INTEGER,
77 DER_DEC_NO_OPTS },
78 { DER_OFFSET(DERRSAPrivKeyCRT, dq),
79 ASN1_INTEGER,
80 DER_DEC_NO_OPTS },
81 { DER_OFFSET(DERRSAPrivKeyCRT, qInv),
82 ASN1_INTEGER,
83 DER_DEC_NO_OPTS },
84 /* ignore the (optional) rest */
85 };
86 const DERSize DERNumRSAPrivKeyCRTItemSpecs =
87 sizeof(DERRSAPrivKeyCRTItemSpecs) / sizeof(DERItemSpec);
88
89 #endif /* DER_DECODE_ENABLE */
90
91 #if DER_DECODE_ENABLE || DER_ENCODE_ENABLE
92
93 /* RSA public key in PKCS1 format - encode and decode */
94 const DERItemSpec DERRSAPubKeyPKCS1ItemSpecs[] =
95 {
96 { DER_OFFSET(DERRSAPubKeyPKCS1, modulus),
97 ASN1_INTEGER,
98 DER_DEC_NO_OPTS | DER_ENC_SIGNED_INT },
99 { DER_OFFSET(DERRSAPubKeyPKCS1, pubExponent),
100 ASN1_INTEGER,
101 DER_DEC_NO_OPTS | DER_ENC_SIGNED_INT },
102 };
103 const DERSize DERNumRSAPubKeyPKCS1ItemSpecs =
104 sizeof(DERRSAPubKeyPKCS1ItemSpecs) / sizeof(DERItemSpec);
105
106 /* RSA public key in Apple custome format with reciprocal - encode and decode */
107 const DERItemSpec DERRSAPubKeyAppleItemSpecs[] =
108 {
109 { DER_OFFSET(DERRSAPubKeyApple, modulus),
110 ASN1_INTEGER,
111 DER_DEC_NO_OPTS | DER_ENC_SIGNED_INT },
112 { DER_OFFSET(DERRSAPubKeyApple, reciprocal),
113 ASN1_INTEGER,
114 DER_DEC_NO_OPTS | DER_ENC_SIGNED_INT },
115 { DER_OFFSET(DERRSAPubKeyApple, pubExponent),
116 ASN1_INTEGER,
117 DER_DEC_NO_OPTS | DER_ENC_SIGNED_INT },
118 };
119 const DERSize DERNumRSAPubKeyAppleItemSpecs =
120 sizeof(DERRSAPubKeyAppleItemSpecs) / sizeof(DERItemSpec);
121
122
123 #endif /* DER_DECODE_ENABLE || DER_ENCODE_ENABLE */
124
125 #ifndef DER_ENCODE_ENABLE
126 #error Please define DER_ENCODE_ENABLE.
127 #endif
128
129 #if DER_ENCODE_ENABLE
130
131 /* RSA Key Pair, encode only */
132 const DERItemSpec DERRSAKeyPairItemSpecs[] =
133 {
134 { DER_OFFSET(DERRSAKeyPair, version),
135 ASN1_INTEGER,
136 DER_ENC_SIGNED_INT },
137 { DER_OFFSET(DERRSAKeyPair, n),
138 ASN1_INTEGER,
139 DER_ENC_SIGNED_INT },
140 { DER_OFFSET(DERRSAKeyPair, e),
141 ASN1_INTEGER,
142 DER_ENC_SIGNED_INT },
143 { DER_OFFSET(DERRSAKeyPair, d),
144 ASN1_INTEGER,
145 DER_ENC_SIGNED_INT },
146 { DER_OFFSET(DERRSAKeyPair, p),
147 ASN1_INTEGER,
148 DER_ENC_SIGNED_INT },
149 { DER_OFFSET(DERRSAKeyPair, q),
150 ASN1_INTEGER,
151 DER_ENC_SIGNED_INT },
152 { DER_OFFSET(DERRSAKeyPair, dp),
153 ASN1_INTEGER,
154 DER_ENC_SIGNED_INT },
155 { DER_OFFSET(DERRSAKeyPair, dq),
156 ASN1_INTEGER,
157 DER_ENC_SIGNED_INT },
158 { DER_OFFSET(DERRSAKeyPair, qInv),
159 ASN1_INTEGER,
160 DER_ENC_SIGNED_INT },
161 };
162
163 const DERSize DERNumRSAKeyPairItemSpecs =
164 sizeof(DERRSAKeyPairItemSpecs) / sizeof(DERItemSpec);
165
166 #endif /* DER_ENCODE_ENABLE */
167