Skip to content

Commit 5119e71

Browse files
committed
add x509_decode_public_key_from_certificate()
1 parent c28a849 commit 5119e71

File tree

3 files changed

+134
-7
lines changed

3 files changed

+134
-7
lines changed

src/headers/tomcrypt_private.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -330,6 +330,13 @@ int der_teletex_value_decode(int v);
330330

331331
int der_utf8_valid_char(const wchar_t c);
332332

333+
typedef int (*public_key_decode_cb)(const unsigned char *in, unsigned long inlen, void *ctx);
334+
335+
int x509_decode_public_key_from_certificate(const unsigned char *in, unsigned long inlen,
336+
enum ltc_oid_id algorithm, ltc_asn1_type param_type,
337+
ltc_asn1_list* parameters, unsigned long *parameters_len,
338+
public_key_decode_cb callback, void *ctx);
339+
333340
/* SUBJECT PUBLIC KEY INFO */
334341
int x509_encode_subject_public_key_info(unsigned char *out, unsigned long *outlen,
335342
unsigned int algorithm, const void* public_key, unsigned long public_key_len,
Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
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
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_INVALID_ARG;
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+
err = x509_decode_subject_public_key_info(l->data, l->size,
90+
algorithm, tmpbuf, &tmpbuf_len,
91+
param_type, parameters, parameters_len);
92+
if (err == CRYPT_OK) {
93+
err = callback(tmpbuf, tmpbuf_len, ctx);
94+
goto LBL_OUT;
95+
}
96+
}
97+
l = l->next;
98+
} while(l);
99+
}
100+
}
101+
}
102+
103+
LBL_OUT:
104+
if (decoded_list) der_free_sequence_flexi(decoded_list);
105+
if (tmpbuf != NULL) XFREE(tmpbuf);
106+
107+
return err;
108+
}
109+
110+
#endif
111+
112+
/* ref: $Format:%D$ */
113+
/* git commit: $Format:%H$ */
114+
/* commit time: $Format:%ai$ */

src/pk/asn1/x509/x509_decode_subject_public_key_info.c

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -34,26 +34,33 @@
3434
@param public_key_len [in/out] The length of the public key buffer and the written length
3535
@param parameters_type The parameters' type out of the enum ltc_asn1_type
3636
@param parameters The parameters to include
37-
@param parameters_len [in/out]The number of parameters to include
37+
@param parameters_len [in/out] The number of parameters to include
3838
@return CRYPT_OK on success
3939
*/
4040
int x509_decode_subject_public_key_info(const unsigned char *in, unsigned long inlen,
4141
unsigned int algorithm, void* public_key, unsigned long* public_key_len,
4242
ltc_asn1_type parameters_type, ltc_asn1_list* parameters, unsigned long *parameters_len)
4343
{
4444
int err;
45-
unsigned long len, alg_id_num;
45+
unsigned long len, alg_id_num, tmplen;
4646
const char* oid;
4747
unsigned char *tmpbuf;
4848
unsigned long tmpoid[16];
49+
unsigned long *_parameters_len;
4950
ltc_asn1_list alg_id[2];
5051
ltc_asn1_list subject_pubkey[2];
5152

5253
LTC_ARGCHK(in != NULL);
5354
LTC_ARGCHK(inlen != 0);
5455
LTC_ARGCHK(public_key_len != NULL);
56+
5557
if (parameters_type != LTC_ASN1_EOL) {
56-
LTC_ARGCHK(parameters_len != NULL);
58+
if ((parameters == NULL) || (parameters_len == NULL)) {
59+
tmplen = 0;
60+
_parameters_len = &tmplen;
61+
} else {
62+
_parameters_len = parameters_len;
63+
}
5764
}
5865

5966
err = pk_get_oid(algorithm, &oid);
@@ -72,9 +79,8 @@ int x509_decode_subject_public_key_info(const unsigned char *in, unsigned long i
7279
LTC_SET_ASN1(alg_id, 0, LTC_ASN1_OBJECT_IDENTIFIER, tmpoid, sizeof(tmpoid)/sizeof(tmpoid[0]));
7380
if (parameters_type == LTC_ASN1_EOL) {
7481
alg_id_num = 1;
75-
}
76-
else {
77-
LTC_SET_ASN1(alg_id, 1, parameters_type, parameters, *parameters_len);
82+
} else {
83+
LTC_SET_ASN1(alg_id, 1, parameters_type, parameters, *_parameters_len);
7884
alg_id_num = 2;
7985
}
8086

@@ -89,7 +95,7 @@ int x509_decode_subject_public_key_info(const unsigned char *in, unsigned long i
8995
goto LBL_ERR;
9096
}
9197
if (parameters_type != LTC_ASN1_EOL) {
92-
*parameters_len = alg_id[1].size;
98+
*_parameters_len = alg_id[1].size;
9399
}
94100

95101
if ((err = pk_oid_cmp_with_asn1(oid, &alg_id[0])) != CRYPT_OK) {

0 commit comments

Comments
 (0)