1 /* Copyright (c) 2005-2007 Apple Inc. All Rights Reserved. */
4 * DER_Cert.c - support for decoding RSA keys
6 * Created Nov. 8 2005 by Doug Mitchell.
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>
15 #ifndef DER_DECODE_ENABLE
16 #error Please define DER_DECODE_ENABLE.
21 * DERItemSpecs for decoding RSA keys.
24 /* Algorithm Identifier */
25 const DERItemSpec DERAlgorithmIdItemSpecs
[] =
27 { DER_OFFSET(DERAlgorithmId
, oid
),
30 { DER_OFFSET(DERAlgorithmId
, params
),
32 DER_DEC_ASN_ANY
| DER_DEC_OPTIONAL
| DER_DEC_SAVE_DER
}
34 const DERSize DERNumAlgorithmIdItemSpecs
=
35 sizeof(DERAlgorithmIdItemSpecs
) / sizeof(DERItemSpec
);
37 /* X509 SubjectPublicKeyInfo */
38 const DERItemSpec DERSubjPubKeyInfoItemSpecs
[] =
40 { DER_OFFSET(DERSubjPubKeyInfo
, algId
),
43 { DER_OFFSET(DERSubjPubKeyInfo
, pubKey
),
48 const DERSize DERNumSubjPubKeyInfoItemSpecs
=
49 sizeof(DERSubjPubKeyInfoItemSpecs
) / sizeof(DERItemSpec
);
52 * RSA private key in CRT format
54 const DERItemSpec DERRSAPrivKeyCRTItemSpecs
[] =
56 /* version, n, e, d - skip */
69 { DER_OFFSET(DERRSAPrivKeyCRT
, p
),
72 { DER_OFFSET(DERRSAPrivKeyCRT
, q
),
75 { DER_OFFSET(DERRSAPrivKeyCRT
, dp
),
78 { DER_OFFSET(DERRSAPrivKeyCRT
, dq
),
81 { DER_OFFSET(DERRSAPrivKeyCRT
, qInv
),
84 /* ignore the (optional) rest */
86 const DERSize DERNumRSAPrivKeyCRTItemSpecs
=
87 sizeof(DERRSAPrivKeyCRTItemSpecs
) / sizeof(DERItemSpec
);
89 #endif /* DER_DECODE_ENABLE */
91 #if DER_DECODE_ENABLE || DER_ENCODE_ENABLE
93 /* RSA public key in PKCS1 format - encode and decode */
94 const DERItemSpec DERRSAPubKeyPKCS1ItemSpecs
[] =
96 { DER_OFFSET(DERRSAPubKeyPKCS1
, modulus
),
98 DER_DEC_NO_OPTS
| DER_ENC_SIGNED_INT
},
99 { DER_OFFSET(DERRSAPubKeyPKCS1
, pubExponent
),
101 DER_DEC_NO_OPTS
| DER_ENC_SIGNED_INT
},
103 const DERSize DERNumRSAPubKeyPKCS1ItemSpecs
=
104 sizeof(DERRSAPubKeyPKCS1ItemSpecs
) / sizeof(DERItemSpec
);
106 /* RSA public key in Apple custome format with reciprocal - encode and decode */
107 const DERItemSpec DERRSAPubKeyAppleItemSpecs
[] =
109 { DER_OFFSET(DERRSAPubKeyApple
, modulus
),
111 DER_DEC_NO_OPTS
| DER_ENC_SIGNED_INT
},
112 { DER_OFFSET(DERRSAPubKeyApple
, reciprocal
),
114 DER_DEC_NO_OPTS
| DER_ENC_SIGNED_INT
},
115 { DER_OFFSET(DERRSAPubKeyApple
, pubExponent
),
117 DER_DEC_NO_OPTS
| DER_ENC_SIGNED_INT
},
119 const DERSize DERNumRSAPubKeyAppleItemSpecs
=
120 sizeof(DERRSAPubKeyAppleItemSpecs
) / sizeof(DERItemSpec
);
123 #endif /* DER_DECODE_ENABLE || DER_ENCODE_ENABLE */
125 #ifndef DER_ENCODE_ENABLE
126 #error Please define DER_ENCODE_ENABLE.
129 #if DER_ENCODE_ENABLE
131 /* RSA Key Pair, encode only */
132 const DERItemSpec DERRSAKeyPairItemSpecs
[] =
134 { DER_OFFSET(DERRSAKeyPair
, version
),
136 DER_ENC_SIGNED_INT
},
137 { DER_OFFSET(DERRSAKeyPair
, n
),
139 DER_ENC_SIGNED_INT
},
140 { DER_OFFSET(DERRSAKeyPair
, e
),
142 DER_ENC_SIGNED_INT
},
143 { DER_OFFSET(DERRSAKeyPair
, d
),
145 DER_ENC_SIGNED_INT
},
146 { DER_OFFSET(DERRSAKeyPair
, p
),
148 DER_ENC_SIGNED_INT
},
149 { DER_OFFSET(DERRSAKeyPair
, q
),
151 DER_ENC_SIGNED_INT
},
152 { DER_OFFSET(DERRSAKeyPair
, dp
),
154 DER_ENC_SIGNED_INT
},
155 { DER_OFFSET(DERRSAKeyPair
, dq
),
157 DER_ENC_SIGNED_INT
},
158 { DER_OFFSET(DERRSAKeyPair
, qInv
),
160 DER_ENC_SIGNED_INT
},
163 const DERSize DERNumRSAKeyPairItemSpecs
=
164 sizeof(DERRSAKeyPairItemSpecs
) / sizeof(DERItemSpec
);
166 #endif /* DER_ENCODE_ENABLE */