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 x509_decode_subject_public_key_info.c
14   ASN.1 DER/X.509, encode a SubjectPublicKeyInfo structure --nmav
15 */
16 
17 #ifdef LTC_DER
18 
19 /* AlgorithmIdentifier := SEQUENCE {
20  *    algorithm OBJECT IDENTIFIER,
21  *    parameters ANY DEFINED BY algorithm
22  * }
23  *
24  * SubjectPublicKeyInfo := SEQUENCE {
25  *    algorithm AlgorithmIdentifier,
26  *    subjectPublicKey BIT STRING
27  * }
28  */
29 /**
30   Decode a SubjectPublicKeyInfo
31    @param in      The input buffer
32    @param inlen   The length of the input buffer
33    @param algorithm             One out of the enum #public_key_algorithms
34    @param public_key            The buffer for the public key
35    @param public_key_len        [in/out] The length of the public key buffer and the written length
36    @param parameters_type       The parameters' type out of the enum ltc_asn1_type
37    @param parameters            The parameters to include
38    @param parameters_len        [in/out] The number of parameters to include
39    @return CRYPT_OK on success
40 */
x509_decode_subject_public_key_info(const unsigned char * in,unsigned long inlen,unsigned int algorithm,void * public_key,unsigned long * public_key_len,ltc_asn1_type parameters_type,ltc_asn1_list * parameters,unsigned long * parameters_len)41 int x509_decode_subject_public_key_info(const unsigned char *in, unsigned long inlen,
42         unsigned int algorithm, void* public_key, unsigned long* public_key_len,
43         ltc_asn1_type parameters_type, ltc_asn1_list* parameters, unsigned long *parameters_len)
44 {
45    int err;
46    unsigned long len, alg_id_num, tmplen;
47    const char* oid;
48    unsigned char *tmpbuf;
49    unsigned long  tmpoid[16];
50    unsigned long *_parameters_len;
51    ltc_asn1_list alg_id[2];
52    ltc_asn1_list subject_pubkey[2];
53 
54    LTC_ARGCHK(in    != NULL);
55    LTC_ARGCHK(inlen != 0);
56    LTC_ARGCHK(public_key_len != NULL);
57 
58    if (parameters_type != LTC_ASN1_EOL) {
59       if ((parameters == NULL) || (parameters_len == NULL)) {
60          tmplen = 0;
61          _parameters_len = &tmplen;
62       } else {
63          _parameters_len = parameters_len;
64       }
65    }
66 
67    err = pk_get_oid(algorithm, &oid);
68    if (err != CRYPT_OK) {
69         return err;
70    }
71 
72    /* see if the OpenSSL DER format RSA public key will work */
73    tmpbuf = XCALLOC(1, inlen);
74    if (tmpbuf == NULL) {
75        err = CRYPT_MEM;
76        goto LBL_ERR;
77    }
78 
79    /* this includes the internal hash ID and optional params (NULL in this case) */
80    LTC_SET_ASN1(alg_id, 0, LTC_ASN1_OBJECT_IDENTIFIER, tmpoid, sizeof(tmpoid)/sizeof(tmpoid[0]));
81    if (parameters_type == LTC_ASN1_EOL) {
82       alg_id_num = 1;
83    } else {
84       LTC_SET_ASN1(alg_id, 1, parameters_type, parameters, *_parameters_len);
85       alg_id_num = 2;
86    }
87 
88    /* the actual format of the SSL DER key is odd, it stores a RSAPublicKey
89     * in a **BIT** string ... so we have to extract it then proceed to convert bit to octet
90     */
91    LTC_SET_ASN1(subject_pubkey, 0, LTC_ASN1_SEQUENCE, alg_id, alg_id_num);
92    LTC_SET_ASN1(subject_pubkey, 1, LTC_ASN1_RAW_BIT_STRING, tmpbuf, inlen*8U);
93 
94    err=der_decode_sequence(in, inlen, subject_pubkey, 2UL);
95    if (err != CRYPT_OK) {
96            goto LBL_ERR;
97    }
98    if (parameters_type != LTC_ASN1_EOL) {
99       *_parameters_len = alg_id[1].size;
100    }
101 
102    if ((err = pk_oid_cmp_with_asn1(oid, &alg_id[0])) != CRYPT_OK) {
103       /* OID mismatch */
104       goto LBL_ERR;
105    }
106 
107    len = subject_pubkey[1].size/8;
108    if (*public_key_len >= len) {
109        XMEMCPY(public_key, subject_pubkey[1].data, len);
110        *public_key_len = len;
111     } else {
112         *public_key_len = len;
113         err = CRYPT_BUFFER_OVERFLOW;
114         goto LBL_ERR;
115     }
116 
117     err = CRYPT_OK;
118 
119 LBL_ERR:
120 
121     XFREE(tmpbuf);
122 
123     return err;
124 }
125 
126 #endif
127 
128 /* ref:         $Format:%D$ */
129 /* git commit:  $Format:%H$ */
130 /* commit time: $Format:%ai$ */
131