@@ -126,51 +126,58 @@ int rsa_decode_parameters(const ltc_asn1_list *parameters, ltc_rsa_parameters *r
126126 }
127127 }
128128
129-
130129 rsa_params -> pss_oaep = 1 ;
131130
132131 return CRYPT_OK ;
133132}
134133
135- static int s_rsa_import_pss (const unsigned char * in , unsigned long inlen , rsa_key * key )
134+ LTC_INLINE static int s_rsa_1_5_import_spki (const unsigned char * in , unsigned long inlen , rsa_key * key )
135+ {
136+ return x509_process_public_key_from_spki (in , inlen ,
137+ LTC_OID_RSA ,
138+ LTC_ASN1_NULL , NULL , NULL ,
139+ (public_key_decode_cb )s_rsa_decode , key );
140+ }
141+
142+ LTC_INLINE static int s_rsa_pss_import_spki (const unsigned char * in , unsigned long inlen , rsa_key * key )
136143{
137144 rsa_pss_parameters_data d ;
138- ltc_asn1_list * decoded_list ;
139- const ltc_asn1_list * spki ;
140- int err ;
141145 unsigned long n_params = LTC_ARRAY_SIZE (d .params );
142146
143- if ((err = x509_process_public_key_from_spki (in , inlen ,
144- LTC_OID_RSA_PSS ,
145- LTC_ASN1_NULL , NULL , NULL ,
146- (public_key_decode_cb )s_rsa_decode , key )) != CRYPT_OK ) {
147- if ((err = x509_decode_spki (in , inlen , & decoded_list , & spki )) != CRYPT_OK ) {
148- return err ;
149- }
150- if ((err = x509_process_public_key_from_spki (spki -> data , spki -> size ,
151- LTC_OID_RSA_PSS ,
152- LTC_ASN1_NULL , NULL , NULL ,
153- (public_key_decode_cb )s_rsa_decode , key )) != CRYPT_OK ) {
154- s_rsa_pss_parameters_data_setup (& d );
155- err = x509_process_public_key_from_spki (spki -> data , spki -> size ,
156- LTC_OID_RSA_PSS ,
157- LTC_ASN1_SEQUENCE , d .params , & n_params ,
158- (public_key_decode_cb )s_rsa_decode , key );
159- }
147+ if (x509_process_public_key_from_spki (in , inlen ,
148+ LTC_OID_RSA_PSS ,
149+ LTC_ASN1_NULL , NULL , NULL ,
150+ (public_key_decode_cb )s_rsa_decode , key ) == CRYPT_OK ) {
151+ return CRYPT_OK ;
160152 }
153+ s_rsa_pss_parameters_data_setup (& d );
154+ return x509_process_public_key_from_spki (in , inlen ,
155+ LTC_OID_RSA_PSS ,
156+ LTC_ASN1_SEQUENCE , d .params , & n_params ,
157+ (public_key_decode_cb )s_rsa_decode , key );
158+ }
161159
162- der_free_sequence_flexi (decoded_list );
160+ LTC_INLINE static int s_rsa_import_spki (const unsigned char * in , unsigned long inlen , rsa_key * key )
161+ {
162+ int err ;
163+ if (s_rsa_1_5_import_spki (in , inlen , key ) == CRYPT_OK ) {
164+ return CRYPT_OK ;
165+ }
166+
167+ if ((err = s_rsa_pss_import_spki (in , inlen , key )) == CRYPT_OK ) {
168+ return CRYPT_OK ;
169+ }
163170 return err ;
164171}
165172
166173/**
167- Import an RSA key from a X.509 certificate
174+ Import an RSA key from SubjectPublicKeyInfo
168175 @param in The packet to import from
169176 @param inlen It's length (octets)
170177 @param key [out] Destination for newly imported key
171178 @return CRYPT_OK if successful, upon error allocated memory is freed
172179*/
173- int rsa_import_x509 (const unsigned char * in , unsigned long inlen , rsa_key * key )
180+ int rsa_import_spki (const unsigned char * in , unsigned long inlen , rsa_key * key )
174181{
175182 int err ;
176183
@@ -183,22 +190,58 @@ int rsa_import_x509(const unsigned char *in, unsigned long inlen, rsa_key *key)
183190 return err ;
184191 }
185192
186- if ((err = x509_decode_public_key_from_certificate (in , inlen ,
187- LTC_OID_RSA ,
188- LTC_ASN1_NULL , NULL , NULL ,
189- (public_key_decode_cb )s_rsa_decode , key )) == CRYPT_OK ) {
193+ if ((err = s_rsa_import_spki (in , inlen , key )) == CRYPT_OK ) {
190194 key -> type = PK_PUBLIC ;
191195 return CRYPT_OK ;
192196 }
193197
194- if ((err = s_rsa_import_pss (in , inlen , key )) == CRYPT_OK ) {
198+ rsa_free (key );
199+
200+ return err ;
201+ }
202+
203+ /**
204+ Import an RSA key from a X.509 certificate
205+ @param in The packet to import from
206+ @param inlen It's length (octets)
207+ @param key [out] Destination for newly imported key
208+ @return CRYPT_OK if successful, upon error allocated memory is freed
209+ */
210+ int rsa_import_x509 (const unsigned char * in , unsigned long inlen , rsa_key * key )
211+ {
212+ ltc_asn1_list * decoded_list ;
213+ const ltc_asn1_list * spki ;
214+ int err ;
215+
216+ LTC_ARGCHK (in != NULL );
217+ LTC_ARGCHK (key != NULL );
218+ LTC_ARGCHK (ltc_mp .name != NULL );
219+
220+ /* init key */
221+ if ((err = rsa_init (key )) != CRYPT_OK ) {
222+ return err ;
223+ }
224+
225+ /* First try to decode as SubjectPublicKeyInfo */
226+ if (s_rsa_import_spki (in , inlen , key ) == CRYPT_OK ) {
195227 key -> type = PK_PUBLIC ;
196228 return CRYPT_OK ;
197229 }
198230
199- rsa_free (key );
231+ /* Now try to extract the SubjectPublicKeyInfo from the Certificate */
232+ if ((err = x509_decode_spki (in , inlen , & decoded_list , & spki )) != CRYPT_OK ) {
233+ rsa_free (key );
234+ return err ;
235+ }
236+ err = s_rsa_import_spki (spki -> data , spki -> size , key );
200237
201- return err ;
238+ der_free_sequence_flexi (decoded_list );
239+ if (err != CRYPT_OK ) {
240+ rsa_free (key );
241+ return err ;
242+ }
243+ key -> type = PK_PUBLIC ;
244+ return CRYPT_OK ;
202245}
203246
204247#endif /* LTC_MRSA */
0 commit comments