1 /* LibTomCrypt, modular cryptographic library -- Tom St Denis
3 * LibTomCrypt is a library that provides various cryptographic
4 * algorithms in a highly modular and flexible manner.
6 * The library is free for all purposes without any express
13 Import an RSA key from a X.509 certificate, Steffen Jaeckel
19 Import an RSA key from a X.509 certificate
20 @param in The packet to import from
21 @param inlen It's length (octets)
22 @param key [out] Destination for newly imported key
23 @return CRYPT_OK if successful, upon error allocated memory is freed
25 int rsa_import_x509(const unsigned char *in, unsigned long inlen, rsa_key *key)
28 unsigned char *tmpbuf;
29 unsigned long tmpbuf_len, tmp_inlen;
30 ltc_asn1_list *decoded_list = NULL, *l;
32 LTC_ARGCHK(in != NULL);
33 LTC_ARGCHK(key != NULL);
34 LTC_ARGCHK(ltc_mp.name != NULL);
37 if ((err = mp_init_multi(&key->e, &key->d, &key->N, &key->dQ,
38 &key->dP, &key->qP, &key->p, &key->q, NULL)) != CRYPT_OK) {
43 tmpbuf = XCALLOC(1, tmpbuf_len);
50 if ((err = der_decode_sequence_flexi(in, &tmp_inlen, &decoded_list)) == CRYPT_OK) {
52 /* Move 2 levels up in the tree
57 if (l->type == LTC_ASN1_SEQUENCE && l->child) {
59 if (l->type == LTC_ASN1_SEQUENCE && l->child) {
64 /* Move forward in the tree until we find this combination
68 OBJECT IDENTIFIER 1.2.840.113549.1.1.1
73 /* The additional check for l->data is there to make sure
74 * we won't try to decode a list that has been 'shrunk'
76 if (l->type == LTC_ASN1_SEQUENCE && l->data && l->child &&
77 l->child->type == LTC_ASN1_SEQUENCE && l->child->child &&
78 l->child->child->type == LTC_ASN1_OBJECT_IDENTIFIER && l->child->next &&
79 l->child->next->type == LTC_ASN1_BIT_STRING) {
80 err = der_decode_subject_public_key_info(l->data, l->size,
81 PKA_RSA, tmpbuf, &tmpbuf_len,
82 LTC_ASN1_NULL, NULL, 0);
83 if (err == CRYPT_OK) {
84 /* now it should be SEQUENCE { INTEGER, INTEGER } */
85 if ((err = der_decode_sequence_multi(tmpbuf, tmpbuf_len,
86 LTC_ASN1_INTEGER, 1UL, key->N,
87 LTC_ASN1_INTEGER, 1UL, key->e,
88 LTC_ASN1_EOL, 0UL, NULL)) != CRYPT_OK) {
91 key->type = PK_PUBLIC;
107 if (decoded_list) der_free_sequence_flexi(decoded_list);
108 if (tmpbuf != NULL) XFREE(tmpbuf);
113 #endif /* LTC_MRSA */
116 /* ref: $Format:%D$ */
117 /* git commit: $Format:%H$ */
118 /* commit time: $Format:%ai$ */