1 // SPDX-License-Identifier: BSD-2-Clause
2 /* LibTomCrypt, modular cryptographic library -- Tom St Denis
3 *
4 * LibTomCrypt is a library that provides various cryptographic
5 * algorithms in a highly modular and flexible manner.
6 *
7 * The library is free for all purposes without any express
8 * guarantee it works.
9 */
10 #include "tomcrypt_private.h"
11
12 /**
13 @file rsa_import.c
14 Import an RSA key from a X.509 certificate, Steffen Jaeckel
15 */
16
17 #ifdef LTC_MRSA
18
_rsa_decode(const unsigned char * in,unsigned long inlen,rsa_key * key)19 static int _rsa_decode(const unsigned char *in, unsigned long inlen, rsa_key *key)
20 {
21 /* now it should be SEQUENCE { INTEGER, INTEGER } */
22 return der_decode_sequence_multi(in, inlen,
23 LTC_ASN1_INTEGER, 1UL, key->N,
24 LTC_ASN1_INTEGER, 1UL, key->e,
25 LTC_ASN1_EOL, 0UL, NULL);
26 }
27
28 /**
29 Import an RSA key from a X.509 certificate
30 @param in The packet to import from
31 @param inlen It's length (octets)
32 @param key [out] Destination for newly imported key
33 @return CRYPT_OK if successful, upon error allocated memory is freed
34 */
rsa_import_x509(const unsigned char * in,unsigned long inlen,rsa_key * key)35 int rsa_import_x509(const unsigned char *in, unsigned long inlen, rsa_key *key)
36 {
37 int err;
38
39 LTC_ARGCHK(in != NULL);
40 LTC_ARGCHK(key != NULL);
41 LTC_ARGCHK(ltc_mp.name != NULL);
42
43 /* init key */
44 if ((err = mp_init_multi(&key->e, &key->d, &key->N, &key->dQ,
45 &key->dP, &key->qP, &key->p, &key->q, NULL)) != CRYPT_OK) {
46 return err;
47 }
48
49 if ((err = x509_decode_public_key_from_certificate(in, inlen,
50 PKA_RSA, LTC_ASN1_NULL,
51 NULL, NULL,
52 (public_key_decode_cb)_rsa_decode, key)) != CRYPT_OK) {
53 rsa_free(key);
54 } else {
55 key->type = PK_PUBLIC;
56 }
57
58 return err;
59 }
60
61 #endif /* LTC_MRSA */
62
63
64 /* ref: $Format:%D$ */
65 /* git commit: $Format:%H$ */
66 /* commit time: $Format:%ai$ */
67