Skip to content

Commit 4247ead

Browse files
committed
Extract static function as x509_get_pka()
Signed-off-by: Steffen Jaeckel <s@jaeckel.eu>
1 parent 028775e commit 4247ead

File tree

3 files changed

+42
-28
lines changed

3 files changed

+42
-28
lines changed

src/headers/tomcrypt_private.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -644,6 +644,8 @@ int x509_decode_subject_public_key_info(const unsigned char *in, unsigned long i
644644
enum ltc_oid_id algorithm, void *public_key, unsigned long *public_key_len,
645645
ltc_asn1_type parameters_type, ltc_asn1_list* parameters, unsigned long *parameters_len);
646646

647+
int x509_get_pka(ltc_asn1_list *pub, enum ltc_pka_id *pka);
648+
647649
int pk_oid_cmp_with_asn1(const char *o1, const ltc_asn1_list *o2);
648650

649651
#endif /* LTC_DER */

src/misc/pem/pem_pkcs.c

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

44-
static int s_get_pka(ltc_asn1_list *pub, enum ltc_pka_id *pka)
45-
{
46-
der_flexi_check flexi_should[4];
47-
ltc_asn1_list *seqid, *id;
48-
enum ltc_oid_id oid_id;
49-
int err;
50-
unsigned long n = 0;
51-
LTC_SET_DER_FLEXI_CHECK(flexi_should, n++, LTC_ASN1_SEQUENCE, &seqid);
52-
LTC_SET_DER_FLEXI_CHECK(flexi_should, n++, LTC_ASN1_BIT_STRING, NULL);
53-
LTC_SET_DER_FLEXI_CHECK(flexi_should, n, LTC_ASN1_EOL, NULL);
54-
if ((err = der_flexi_sequence_cmp(pub, flexi_should)) != CRYPT_OK) {
55-
return err;
56-
}
57-
n = 0;
58-
LTC_SET_DER_FLEXI_CHECK(flexi_should, n++, LTC_ASN1_OBJECT_IDENTIFIER, &id);
59-
LTC_SET_DER_FLEXI_CHECK(flexi_should, n, LTC_ASN1_EOL, NULL);
60-
err = der_flexi_sequence_cmp(seqid, flexi_should);
61-
if (err != CRYPT_OK && err != CRYPT_INPUT_TOO_LONG) {
62-
return err;
63-
}
64-
if ((err = pk_get_oid_from_asn1(id, &oid_id)) != CRYPT_OK) {
65-
return err;
66-
}
67-
return pk_get_pka_id(oid_id, pka);
68-
}
69-
7044
typedef int (*import_fn)(const unsigned char *, unsigned long, void*);
7145

7246
static const import_fn s_import_x509_fns[LTC_PKA_NUM] = {
@@ -90,7 +64,7 @@ static int s_import_x509(unsigned char *asn1_cert, unsigned long asn1_len, ltc_p
9064
if ((err = x509_decode_spki(asn1_cert, asn1_len, &d, &spki)) != CRYPT_OK) {
9165
return err;
9266
}
93-
err = s_get_pka(spki, &pka);
67+
err = x509_get_pka(spki, &pka);
9468
der_free_sequence_flexi(d);
9569
if (err != CRYPT_OK) {
9670
return err;
@@ -171,7 +145,7 @@ static int s_extract_pka(unsigned char *asn1_cert, unsigned long asn1_len, enum
171145
if ((err = der_decode_sequence_flexi(asn1_cert, &asn1_len, &pub)) != CRYPT_OK) {
172146
return err;
173147
}
174-
err = s_get_pka(pub, pka);
148+
err = x509_get_pka(pub, pka);
175149
der_sequence_free(pub);
176150
return err;
177151
}

src/pk/asn1/x509/x509_get_pka.c

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/* LibTomCrypt, modular cryptographic library -- Tom St Denis */
2+
/* SPDX-License-Identifier: Unlicense */
3+
#include "tomcrypt_private.h"
4+
5+
/**
6+
@file x509_get_pka.c
7+
Extract the PKA from an X.509 cert, Steffen Jaeckel
8+
*/
9+
10+
#ifdef LTC_DER
11+
12+
int x509_get_pka(ltc_asn1_list *pub, enum ltc_pka_id *pka)
13+
{
14+
der_flexi_check flexi_should[4];
15+
ltc_asn1_list *seqid, *id;
16+
enum ltc_oid_id oid_id;
17+
int err;
18+
unsigned long n = 0;
19+
LTC_SET_DER_FLEXI_CHECK(flexi_should, n++, LTC_ASN1_SEQUENCE, &seqid);
20+
LTC_SET_DER_FLEXI_CHECK(flexi_should, n++, LTC_ASN1_BIT_STRING, NULL);
21+
LTC_SET_DER_FLEXI_CHECK(flexi_should, n, LTC_ASN1_EOL, NULL);
22+
if ((err = der_flexi_sequence_cmp(pub, flexi_should)) != CRYPT_OK) {
23+
return err;
24+
}
25+
n = 0;
26+
LTC_SET_DER_FLEXI_CHECK(flexi_should, n++, LTC_ASN1_OBJECT_IDENTIFIER, &id);
27+
LTC_SET_DER_FLEXI_CHECK(flexi_should, n, LTC_ASN1_EOL, NULL);
28+
err = der_flexi_sequence_cmp(seqid, flexi_should);
29+
if (err != CRYPT_OK && err != CRYPT_INPUT_TOO_LONG) {
30+
return err;
31+
}
32+
if ((err = pk_get_oid_from_asn1(id, &oid_id)) != CRYPT_OK) {
33+
return err;
34+
}
35+
return pk_get_pka_id(oid_id, pka);
36+
}
37+
38+
#endif /* LTC_DER */

0 commit comments

Comments
 (0)