2 * Copyright (c) 2005-2007,2011,2014 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@
26 * DER_Cert.c - support for decoding RSA keys
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>
36 #ifndef DER_DECODE_ENABLE
37 #error Please define DER_DECODE_ENABLE.
42 * DERItemSpecs for decoding RSA keys.
45 /* Algorithm Identifier */
46 const DERItemSpec DERAlgorithmIdItemSpecs
[] =
48 { DER_OFFSET(DERAlgorithmId
, oid
),
51 { DER_OFFSET(DERAlgorithmId
, params
),
53 DER_DEC_ASN_ANY
| DER_DEC_OPTIONAL
| DER_DEC_SAVE_DER
}
55 const DERSize DERNumAlgorithmIdItemSpecs
=
56 sizeof(DERAlgorithmIdItemSpecs
) / sizeof(DERItemSpec
);
58 /* X509 SubjectPublicKeyInfo */
59 const DERItemSpec DERSubjPubKeyInfoItemSpecs
[] =
61 { DER_OFFSET(DERSubjPubKeyInfo
, algId
),
64 { DER_OFFSET(DERSubjPubKeyInfo
, pubKey
),
69 const DERSize DERNumSubjPubKeyInfoItemSpecs
=
70 sizeof(DERSubjPubKeyInfoItemSpecs
) / sizeof(DERItemSpec
);
73 * RSA private key in CRT format
75 const DERItemSpec DERRSAPrivKeyCRTItemSpecs
[] =
77 /* version, n, e, d - skip */
90 { DER_OFFSET(DERRSAPrivKeyCRT
, p
),
93 { DER_OFFSET(DERRSAPrivKeyCRT
, q
),
96 { DER_OFFSET(DERRSAPrivKeyCRT
, dp
),
99 { DER_OFFSET(DERRSAPrivKeyCRT
, dq
),
102 { DER_OFFSET(DERRSAPrivKeyCRT
, qInv
),
105 /* ignore the (optional) rest */
107 const DERSize DERNumRSAPrivKeyCRTItemSpecs
=
108 sizeof(DERRSAPrivKeyCRTItemSpecs
) / sizeof(DERItemSpec
);
110 #endif /* DER_DECODE_ENABLE */
112 #if DER_DECODE_ENABLE || DER_ENCODE_ENABLE
114 /* RSA public key in PKCS1 format - encode and decode */
115 const DERItemSpec DERRSAPubKeyPKCS1ItemSpecs
[] =
117 { DER_OFFSET(DERRSAPubKeyPKCS1
, modulus
),
119 DER_DEC_NO_OPTS
| DER_ENC_SIGNED_INT
},
120 { DER_OFFSET(DERRSAPubKeyPKCS1
, pubExponent
),
122 DER_DEC_NO_OPTS
| DER_ENC_SIGNED_INT
},
124 const DERSize DERNumRSAPubKeyPKCS1ItemSpecs
=
125 sizeof(DERRSAPubKeyPKCS1ItemSpecs
) / sizeof(DERItemSpec
);
127 /* RSA public key in Apple custome format with reciprocal - encode and decode */
128 const DERItemSpec DERRSAPubKeyAppleItemSpecs
[] =
130 { DER_OFFSET(DERRSAPubKeyApple
, modulus
),
132 DER_DEC_NO_OPTS
| DER_ENC_SIGNED_INT
},
133 { DER_OFFSET(DERRSAPubKeyApple
, reciprocal
),
135 DER_DEC_NO_OPTS
| DER_ENC_SIGNED_INT
},
136 { DER_OFFSET(DERRSAPubKeyApple
, pubExponent
),
138 DER_DEC_NO_OPTS
| DER_ENC_SIGNED_INT
},
140 const DERSize DERNumRSAPubKeyAppleItemSpecs
=
141 sizeof(DERRSAPubKeyAppleItemSpecs
) / sizeof(DERItemSpec
);
144 #endif /* DER_DECODE_ENABLE || DER_ENCODE_ENABLE */
146 #ifndef DER_ENCODE_ENABLE
147 #error Please define DER_ENCODE_ENABLE.
150 #if DER_ENCODE_ENABLE
152 /* RSA Key Pair, encode only */
153 const DERItemSpec DERRSAKeyPairItemSpecs
[] =
155 { DER_OFFSET(DERRSAKeyPair
, version
),
157 DER_ENC_SIGNED_INT
},
158 { DER_OFFSET(DERRSAKeyPair
, n
),
160 DER_ENC_SIGNED_INT
},
161 { DER_OFFSET(DERRSAKeyPair
, e
),
163 DER_ENC_SIGNED_INT
},
164 { DER_OFFSET(DERRSAKeyPair
, d
),
166 DER_ENC_SIGNED_INT
},
167 { DER_OFFSET(DERRSAKeyPair
, p
),
169 DER_ENC_SIGNED_INT
},
170 { DER_OFFSET(DERRSAKeyPair
, q
),
172 DER_ENC_SIGNED_INT
},
173 { DER_OFFSET(DERRSAKeyPair
, dp
),
175 DER_ENC_SIGNED_INT
},
176 { DER_OFFSET(DERRSAKeyPair
, dq
),
178 DER_ENC_SIGNED_INT
},
179 { DER_OFFSET(DERRSAKeyPair
, qInv
),
181 DER_ENC_SIGNED_INT
},
184 const DERSize DERNumRSAKeyPairItemSpecs
=
185 sizeof(DERRSAKeyPairItemSpecs
) / sizeof(DERItemSpec
);
187 #endif /* DER_ENCODE_ENABLE */