Skip to content

Commit 25b26c6

Browse files
committed
Export static function as x509_import_spki()
Signed-off-by: Steffen Jaeckel <s@jaeckel.eu>
1 parent 4247ead commit 25b26c6

File tree

3 files changed

+58
-40
lines changed

3 files changed

+58
-40
lines changed

src/headers/tomcrypt_private.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -645,6 +645,7 @@ int x509_decode_subject_public_key_info(const unsigned char *in, unsigned long i
645645
ltc_asn1_type parameters_type, ltc_asn1_list* parameters, unsigned long *parameters_len);
646646

647647
int x509_get_pka(ltc_asn1_list *pub, enum ltc_pka_id *pka);
648+
int x509_import_spki(const unsigned char *asn1_cert, unsigned long asn1_len, ltc_pka_key *k, ltc_asn1_list **root);
648649

649650
int pk_oid_cmp_with_asn1(const char *o1, const ltc_asn1_list *o2);
650651

src/misc/pem/pem_pkcs.c

Lines changed: 1 addition & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -41,45 +41,6 @@ static int s_decrypt_pem(unsigned char *asn1_cert, unsigned long *asn1_len, cons
4141
return err;
4242
}
4343

44-
typedef int (*import_fn)(const unsigned char *, unsigned long, void*);
45-
46-
static const import_fn s_import_x509_fns[LTC_PKA_NUM] = {
47-
#ifdef LTC_MRSA
48-
[LTC_PKA_RSA] = (import_fn)rsa_import_x509,
49-
#endif
50-
#ifdef LTC_MECC
51-
[LTC_PKA_EC] = (import_fn)ecc_import_x509,
52-
#endif
53-
#ifdef LTC_CURVE25519
54-
[LTC_PKA_X25519] = (import_fn)x25519_import_x509,
55-
[LTC_PKA_ED25519] = (import_fn)ed25519_import_x509,
56-
#endif
57-
};
58-
59-
static int s_import_x509(unsigned char *asn1_cert, unsigned long asn1_len, ltc_pka_key *k)
60-
{
61-
enum ltc_pka_id pka = LTC_PKA_UNDEF;
62-
ltc_asn1_list *d, *spki;
63-
int err;
64-
if ((err = x509_decode_spki(asn1_cert, asn1_len, &d, &spki)) != CRYPT_OK) {
65-
return err;
66-
}
67-
err = x509_get_pka(spki, &pka);
68-
der_free_sequence_flexi(d);
69-
if (err != CRYPT_OK) {
70-
return err;
71-
}
72-
if (pka < 0
73-
|| pka > LTC_ARRAY_SIZE(s_import_x509_fns)
74-
|| s_import_x509_fns[pka] == NULL) {
75-
return CRYPT_PK_INVALID_TYPE;
76-
}
77-
if ((err = s_import_x509_fns[pka](asn1_cert, asn1_len, &k->u)) == CRYPT_OK) {
78-
k->id = pka;
79-
}
80-
return err;
81-
}
82-
8344
static int s_import_pkcs8(unsigned char *asn1_cert, unsigned long asn1_len, ltc_pka_key *k, const password_ctx *pw_ctx)
8445
{
8546
int err;
@@ -198,7 +159,7 @@ static int s_decode(struct get_char *g, ltc_pka_key *k, const password_ctx *pw_c
198159
err = s_import_pkcs8(asn1_cert, asn1_len, k, pw_ctx);
199160
goto cleanup;
200161
} else if (hdr.id->flags == pf_x509) {
201-
err = s_import_x509(asn1_cert, asn1_len, k);
162+
err = x509_import_spki(asn1_cert, asn1_len, k, NULL);
202163
goto cleanup;
203164
} else if ((hdr.id->flags & pf_public) && hdr.id->pka == LTC_PKA_UNDEF) {
204165
if ((err = s_extract_pka(asn1_cert, asn1_len, &pka)) != CRYPT_OK) {
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/* LibTomCrypt, modular cryptographic library -- Tom St Denis */
2+
/* SPDX-License-Identifier: Unlicense */
3+
#include "tomcrypt_private.h"
4+
5+
/**
6+
@file x509_import_spki.c
7+
Import the SubjectPublicKeyInfo of an X.509 cert, Steffen Jaeckel
8+
*/
9+
10+
#ifdef LTC_DER
11+
12+
typedef int (*import_fn)(const unsigned char *, unsigned long, void*);
13+
14+
static const import_fn s_import_x509_fns[LTC_PKA_NUM] = {
15+
#ifdef LTC_MRSA
16+
[LTC_PKA_RSA] = (import_fn)rsa_import_x509,
17+
#endif
18+
#ifdef LTC_MECC
19+
[LTC_PKA_EC] = (import_fn)ecc_import_x509,
20+
#endif
21+
#ifdef LTC_CURVE25519
22+
[LTC_PKA_X25519] = (import_fn)x25519_import_x509,
23+
[LTC_PKA_ED25519] = (import_fn)ed25519_import_x509,
24+
#endif
25+
};
26+
27+
int x509_import_spki(const unsigned char *asn1_cert, unsigned long asn1_len, ltc_pka_key *k, ltc_asn1_list **root)
28+
{
29+
enum ltc_pka_id pka = LTC_PKA_UNDEF;
30+
ltc_asn1_list *d, *spki;
31+
int err;
32+
if ((err = x509_decode_spki(asn1_cert, asn1_len, &d, &spki)) != CRYPT_OK) {
33+
return err;
34+
}
35+
if ((err = x509_get_pka(spki, &pka)) != CRYPT_OK) {
36+
goto err_out;
37+
}
38+
if (pka < 0
39+
|| pka > LTC_ARRAY_SIZE(s_import_x509_fns)
40+
|| s_import_x509_fns[pka] == NULL) {
41+
err = CRYPT_PK_INVALID_TYPE;
42+
goto err_out;
43+
}
44+
if ((err = s_import_x509_fns[pka](asn1_cert, asn1_len, &k->u)) == CRYPT_OK) {
45+
k->id = pka;
46+
}
47+
err_out:
48+
if (err == CRYPT_OK && root) {
49+
*root = d;
50+
d = NULL;
51+
}
52+
der_free_sequence_flexi(d);
53+
return err;
54+
}
55+
56+
#endif /* LTC_DER */

0 commit comments

Comments
 (0)