|
| 1 | +/* LibTomCrypt, modular cryptographic library -- Tom St Denis |
| 2 | + * |
| 3 | + * LibTomCrypt is a library that provides various cryptographic |
| 4 | + * algorithms in a highly modular and flexible manner. |
| 5 | + * |
| 6 | + * The library is free for all purposes without any express |
| 7 | + * guarantee it works. |
| 8 | + */ |
| 9 | +#include "tomcrypt_private.h" |
| 10 | + |
| 11 | +/** |
| 12 | + @file x509_decode_public_key_from_certificate.c |
| 13 | + ASN.1 DER/X.509, decode a certificate |
| 14 | +*/ |
| 15 | + |
| 16 | +#ifdef LTC_DER |
| 17 | + |
| 18 | +/* Check if it looks like a SubjectPublicKeyInfo */ |
| 19 | +#define LOOKS_LIKE_SPKI(l) ((l) != NULL) \ |
| 20 | +&& ((l)->type == LTC_ASN1_SEQUENCE) \ |
| 21 | +&& ((l)->child != NULL) \ |
| 22 | +&& ((l)->child->type == LTC_ASN1_OBJECT_IDENTIFIER) \ |
| 23 | +&& ((l)->next != NULL) \ |
| 24 | +&& ((l)->next->type == LTC_ASN1_BIT_STRING) |
| 25 | + |
| 26 | +/** |
| 27 | + Try to decode the public key from a X.509 certificate |
| 28 | + @param in The input buffer |
| 29 | + @param inlen The length of the input buffer |
| 30 | + @param algorithm One out of the enum #public_key_algorithms |
| 31 | + @param param_type The parameters' type out of the enum ltc_asn1_type |
| 32 | + @param parameters The parameters to include |
| 33 | + @param parameters_len [in/out] The number of parameters to include |
| 34 | + @param callback The callback |
| 35 | + @param ctx The context passed to the callback |
| 36 | + @return CRYPT_OK on success, CRYPT_NOP if no SubjectPublicKeyInfo was found |
| 37 | +*/ |
| 38 | +int x509_decode_public_key_from_certificate(const unsigned char *in, unsigned long inlen, |
| 39 | + enum ltc_oid_id algorithm, ltc_asn1_type param_type, |
| 40 | + ltc_asn1_list* parameters, unsigned long *parameters_len, |
| 41 | + public_key_decode_cb callback, void *ctx) |
| 42 | +{ |
| 43 | + int err; |
| 44 | + unsigned char *tmpbuf; |
| 45 | + unsigned long tmpbuf_len, tmp_inlen; |
| 46 | + ltc_asn1_list *decoded_list = NULL, *l; |
| 47 | + |
| 48 | + LTC_ARGCHK(in != NULL); |
| 49 | + LTC_ARGCHK(inlen != 0); |
| 50 | + |
| 51 | + tmpbuf_len = inlen; |
| 52 | + tmpbuf = XCALLOC(1, tmpbuf_len); |
| 53 | + if (tmpbuf == NULL) { |
| 54 | + err = CRYPT_MEM; |
| 55 | + goto LBL_OUT; |
| 56 | + } |
| 57 | + |
| 58 | + tmp_inlen = inlen; |
| 59 | + if ((err = der_decode_sequence_flexi(in, &tmp_inlen, &decoded_list)) == CRYPT_OK) { |
| 60 | + l = decoded_list; |
| 61 | + |
| 62 | + err = CRYPT_NOP; |
| 63 | + |
| 64 | + /* Move 2 levels up in the tree |
| 65 | + SEQUENCE |
| 66 | + SEQUENCE |
| 67 | + ... |
| 68 | + */ |
| 69 | + if ((l->type == LTC_ASN1_SEQUENCE) && (l->child != NULL)) { |
| 70 | + l = l->child; |
| 71 | + if ((l->type == LTC_ASN1_SEQUENCE) && (l->child != NULL)) { |
| 72 | + l = l->child; |
| 73 | + |
| 74 | + /* Move forward in the tree until we find this combination |
| 75 | + ... |
| 76 | + SEQUENCE |
| 77 | + SEQUENCE |
| 78 | + OBJECT IDENTIFIER <some PKA OID, e.g. 1.2.840.113549.1.1.1> |
| 79 | + NULL |
| 80 | + BIT STRING |
| 81 | + */ |
| 82 | + do { |
| 83 | + /* The additional check for l->data is there to make sure |
| 84 | + * we won't try to decode a list that has been 'shrunk' |
| 85 | + */ |
| 86 | + if ((l->type == LTC_ASN1_SEQUENCE) |
| 87 | + && (l->data != NULL) |
| 88 | + && LOOKS_LIKE_SPKI(l->child)) { |
| 89 | + if (algorithm == PKA_EC) { |
| 90 | + err = ecc_import_subject_public_key_info(l->data, l->size, ctx); |
| 91 | + } else { |
| 92 | + err = x509_decode_subject_public_key_info(l->data, l->size, |
| 93 | + algorithm, tmpbuf, &tmpbuf_len, |
| 94 | + param_type, parameters, parameters_len); |
| 95 | + if (err == CRYPT_OK) { |
| 96 | + err = callback(tmpbuf, tmpbuf_len, ctx); |
| 97 | + goto LBL_OUT; |
| 98 | + } |
| 99 | + } |
| 100 | + } |
| 101 | + l = l->next; |
| 102 | + } while(l); |
| 103 | + } |
| 104 | + } |
| 105 | + } |
| 106 | + |
| 107 | +LBL_OUT: |
| 108 | + if (decoded_list) der_free_sequence_flexi(decoded_list); |
| 109 | + if (tmpbuf != NULL) XFREE(tmpbuf); |
| 110 | + |
| 111 | + return err; |
| 112 | +} |
| 113 | + |
| 114 | +#endif |
| 115 | + |
| 116 | +/* ref: $Format:%D$ */ |
| 117 | +/* git commit: $Format:%H$ */ |
| 118 | +/* commit time: $Format:%ai$ */ |
0 commit comments