Skip to content

Commit e9b2277

Browse files
authored
Merge pull request #483 from libtom/improve/x509
Improve/x509
2 parents c28a849 + f629999 commit e9b2277

File tree

10 files changed

+167
-111
lines changed

10 files changed

+167
-111
lines changed

libtomcrypt_VS2008.vcproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2193,6 +2193,10 @@
21932193
<Filter
21942194
Name="x509"
21952195
>
2196+
<File
2197+
RelativePath="src\pk\asn1\x509\x509_decode_public_key_from_certificate.c"
2198+
>
2199+
</File>
21962200
<File
21972201
RelativePath="src\pk\asn1\x509\x509_decode_subject_public_key_info.c"
21982202
>

makefile.mingw

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,8 @@ src/pk/asn1/der/utctime/der_decode_utctime.o src/pk/asn1/der/utctime/der_encode_
170170
src/pk/asn1/der/utctime/der_length_utctime.o src/pk/asn1/der/utf8/der_decode_utf8_string.o \
171171
src/pk/asn1/der/utf8/der_encode_utf8_string.o src/pk/asn1/der/utf8/der_length_utf8_string.o \
172172
src/pk/asn1/oid/pk_get_oid.o src/pk/asn1/oid/pk_oid_cmp.o src/pk/asn1/oid/pk_oid_str.o \
173-
src/pk/asn1/pkcs8/pkcs8_decode_flexi.o src/pk/asn1/x509/x509_decode_subject_public_key_info.o \
173+
src/pk/asn1/pkcs8/pkcs8_decode_flexi.o src/pk/asn1/x509/x509_decode_public_key_from_certificate.o \
174+
src/pk/asn1/x509/x509_decode_subject_public_key_info.o \
174175
src/pk/asn1/x509/x509_encode_subject_public_key_info.o src/pk/dh/dh.o src/pk/dh/dh_check_pubkey.o \
175176
src/pk/dh/dh_export.o src/pk/dh/dh_export_key.o src/pk/dh/dh_free.o src/pk/dh/dh_generate_key.o \
176177
src/pk/dh/dh_import.o src/pk/dh/dh_set.o src/pk/dh/dh_set_pg_dhparam.o src/pk/dh/dh_shared_secret.o \

makefile.msvc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,8 @@ src/pk/asn1/der/utctime/der_decode_utctime.obj src/pk/asn1/der/utctime/der_encod
163163
src/pk/asn1/der/utctime/der_length_utctime.obj src/pk/asn1/der/utf8/der_decode_utf8_string.obj \
164164
src/pk/asn1/der/utf8/der_encode_utf8_string.obj src/pk/asn1/der/utf8/der_length_utf8_string.obj \
165165
src/pk/asn1/oid/pk_get_oid.obj src/pk/asn1/oid/pk_oid_cmp.obj src/pk/asn1/oid/pk_oid_str.obj \
166-
src/pk/asn1/pkcs8/pkcs8_decode_flexi.obj src/pk/asn1/x509/x509_decode_subject_public_key_info.obj \
166+
src/pk/asn1/pkcs8/pkcs8_decode_flexi.obj src/pk/asn1/x509/x509_decode_public_key_from_certificate.obj \
167+
src/pk/asn1/x509/x509_decode_subject_public_key_info.obj \
167168
src/pk/asn1/x509/x509_encode_subject_public_key_info.obj src/pk/dh/dh.obj src/pk/dh/dh_check_pubkey.obj \
168169
src/pk/dh/dh_export.obj src/pk/dh/dh_export_key.obj src/pk/dh/dh_free.obj src/pk/dh/dh_generate_key.obj \
169170
src/pk/dh/dh_import.obj src/pk/dh/dh_set.obj src/pk/dh/dh_set_pg_dhparam.obj src/pk/dh/dh_shared_secret.obj \

makefile.unix

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,8 @@ src/pk/asn1/der/utctime/der_decode_utctime.o src/pk/asn1/der/utctime/der_encode_
180180
src/pk/asn1/der/utctime/der_length_utctime.o src/pk/asn1/der/utf8/der_decode_utf8_string.o \
181181
src/pk/asn1/der/utf8/der_encode_utf8_string.o src/pk/asn1/der/utf8/der_length_utf8_string.o \
182182
src/pk/asn1/oid/pk_get_oid.o src/pk/asn1/oid/pk_oid_cmp.o src/pk/asn1/oid/pk_oid_str.o \
183-
src/pk/asn1/pkcs8/pkcs8_decode_flexi.o src/pk/asn1/x509/x509_decode_subject_public_key_info.o \
183+
src/pk/asn1/pkcs8/pkcs8_decode_flexi.o src/pk/asn1/x509/x509_decode_public_key_from_certificate.o \
184+
src/pk/asn1/x509/x509_decode_subject_public_key_info.o \
184185
src/pk/asn1/x509/x509_encode_subject_public_key_info.o src/pk/dh/dh.o src/pk/dh/dh_check_pubkey.o \
185186
src/pk/dh/dh_export.o src/pk/dh/dh_export_key.o src/pk/dh/dh_free.o src/pk/dh/dh_generate_key.o \
186187
src/pk/dh/dh_import.o src/pk/dh/dh_set.o src/pk/dh/dh_set_pg_dhparam.o src/pk/dh/dh_shared_secret.o \

makefile_include.mk

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -340,7 +340,8 @@ src/pk/asn1/der/utctime/der_decode_utctime.o src/pk/asn1/der/utctime/der_encode_
340340
src/pk/asn1/der/utctime/der_length_utctime.o src/pk/asn1/der/utf8/der_decode_utf8_string.o \
341341
src/pk/asn1/der/utf8/der_encode_utf8_string.o src/pk/asn1/der/utf8/der_length_utf8_string.o \
342342
src/pk/asn1/oid/pk_get_oid.o src/pk/asn1/oid/pk_oid_cmp.o src/pk/asn1/oid/pk_oid_str.o \
343-
src/pk/asn1/pkcs8/pkcs8_decode_flexi.o src/pk/asn1/x509/x509_decode_subject_public_key_info.o \
343+
src/pk/asn1/pkcs8/pkcs8_decode_flexi.o src/pk/asn1/x509/x509_decode_public_key_from_certificate.o \
344+
src/pk/asn1/x509/x509_decode_subject_public_key_info.o \
344345
src/pk/asn1/x509/x509_encode_subject_public_key_info.o src/pk/dh/dh.o src/pk/dh/dh_check_pubkey.o \
345346
src/pk/dh/dh_export.o src/pk/dh/dh_export_key.o src/pk/dh/dh_free.o src/pk/dh/dh_generate_key.o \
346347
src/pk/dh/dh_import.o src/pk/dh/dh_set.o src/pk/dh/dh_set_pg_dhparam.o src/pk/dh/dh_shared_secret.o \

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: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
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$ */

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) {

src/pk/ecc/ecc_import_x509.c

Lines changed: 1 addition & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -112,36 +112,7 @@ int ecc_import_subject_public_key_info(const unsigned char *in, unsigned long in
112112
*/
113113
int ecc_import_x509(const unsigned char *in, unsigned long inlen, ecc_key *key)
114114
{
115-
int err;
116-
unsigned long len;
117-
ltc_asn1_list *decoded_list = NULL, *l;
118-
119-
LTC_ARGCHK(in != NULL);
120-
LTC_ARGCHK(key != NULL);
121-
122-
len = inlen;
123-
if ((err = der_decode_sequence_flexi(in, &len, &decoded_list)) == CRYPT_OK) {
124-
err = CRYPT_ERROR;
125-
l = decoded_list;
126-
if (l->type == LTC_ASN1_SEQUENCE &&
127-
l->child && l->child->type == LTC_ASN1_SEQUENCE) {
128-
l = l->child->child;
129-
while (l) {
130-
if (l->type == LTC_ASN1_SEQUENCE && l->data &&
131-
l->child && l->child->type == LTC_ASN1_SEQUENCE &&
132-
l->child->child && l->child->child->type == LTC_ASN1_OBJECT_IDENTIFIER &&
133-
l->child->next && l->child->next->type == LTC_ASN1_BIT_STRING) {
134-
err = ecc_import_subject_public_key_info(l->data, l->size, key);
135-
goto LBL_DONE;
136-
}
137-
l = l->next;
138-
}
139-
}
140-
}
141-
142-
LBL_DONE:
143-
if (decoded_list) der_free_sequence_flexi(decoded_list);
144-
return err;
115+
return x509_decode_public_key_from_certificate(in, inlen, PKA_EC, LTC_ASN1_EOL, NULL, NULL, NULL, key);
145116
}
146117

147118
#endif /* LTC_MECC */

src/pk/rsa/rsa_import_x509.c

Lines changed: 16 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,15 @@
1515

1616
#ifdef LTC_MRSA
1717

18+
static int _rsa_decode(const unsigned char *in, unsigned long inlen, rsa_key *key)
19+
{
20+
/* now it should be SEQUENCE { INTEGER, INTEGER } */
21+
return der_decode_sequence_multi(in, inlen,
22+
LTC_ASN1_INTEGER, 1UL, key->N,
23+
LTC_ASN1_INTEGER, 1UL, key->e,
24+
LTC_ASN1_EOL, 0UL, NULL);
25+
}
26+
1827
/**
1928
Import an RSA key from a X.509 certificate
2029
@param in The packet to import from
@@ -25,9 +34,6 @@
2534
int rsa_import_x509(const unsigned char *in, unsigned long inlen, rsa_key *key)
2635
{
2736
int err;
28-
unsigned char *tmpbuf;
29-
unsigned long tmpbuf_len, tmp_inlen, len;
30-
ltc_asn1_list *decoded_list = NULL, *l;
3137

3238
LTC_ARGCHK(in != NULL);
3339
LTC_ARGCHK(key != NULL);
@@ -39,75 +45,15 @@ int rsa_import_x509(const unsigned char *in, unsigned long inlen, rsa_key *key)
3945
return err;
4046
}
4147

42-
tmpbuf_len = inlen;
43-
tmpbuf = XCALLOC(1, tmpbuf_len);
44-
if (tmpbuf == NULL) {
45-
err = CRYPT_MEM;
46-
goto LBL_ERR;
47-
}
48-
49-
tmp_inlen = inlen;
50-
if ((err = der_decode_sequence_flexi(in, &tmp_inlen, &decoded_list)) == CRYPT_OK) {
51-
l = decoded_list;
52-
/* Move 2 levels up in the tree
53-
SEQUENCE
54-
SEQUENCE
55-
...
56-
*/
57-
if (l->type == LTC_ASN1_SEQUENCE && l->child) {
58-
l = l->child;
59-
if (l->type == LTC_ASN1_SEQUENCE && l->child) {
60-
l = l->child;
61-
62-
err = CRYPT_ERROR;
63-
64-
/* Move forward in the tree until we find this combination
65-
...
66-
SEQUENCE
67-
SEQUENCE
68-
OBJECT IDENTIFIER 1.2.840.113549.1.1.1
69-
NULL
70-
BIT STRING
71-
*/
72-
do {
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'
75-
*/
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-
len = 0;
81-
err = x509_decode_subject_public_key_info(l->data, l->size,
82-
PKA_RSA, tmpbuf, &tmpbuf_len,
83-
LTC_ASN1_NULL, NULL, &len);
84-
if (err == CRYPT_OK) {
85-
/* now it should be SEQUENCE { INTEGER, INTEGER } */
86-
if ((err = der_decode_sequence_multi(tmpbuf, tmpbuf_len,
87-
LTC_ASN1_INTEGER, 1UL, key->N,
88-
LTC_ASN1_INTEGER, 1UL, key->e,
89-
LTC_ASN1_EOL, 0UL, NULL)) != CRYPT_OK) {
90-
goto LBL_ERR;
91-
}
92-
key->type = PK_PUBLIC;
93-
err = CRYPT_OK;
94-
goto LBL_FREE;
95-
}
96-
}
97-
l = l->next;
98-
} while(l);
99-
}
100-
}
48+
if ((err = x509_decode_public_key_from_certificate(in, inlen,
49+
PKA_RSA, LTC_ASN1_NULL,
50+
NULL, NULL,
51+
(public_key_decode_cb)_rsa_decode, key)) != CRYPT_OK) {
52+
rsa_free(key);
53+
} else {
54+
key->type = PK_PUBLIC;
10155
}
10256

103-
104-
LBL_ERR:
105-
rsa_free(key);
106-
107-
LBL_FREE:
108-
if (decoded_list) der_free_sequence_flexi(decoded_list);
109-
if (tmpbuf != NULL) XFREE(tmpbuf);
110-
11157
return err;
11258
}
11359

0 commit comments

Comments
 (0)