Skip to content

Commit 9f1db6a

Browse files
committed
add pkcs8_get_children()
Signed-off-by: Steffen Jaeckel <s@jaeckel.eu>
1 parent 133de65 commit 9f1db6a

File tree

2 files changed

+51
-0
lines changed

2 files changed

+51
-0
lines changed

src/headers/tomcrypt_private.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -485,6 +485,9 @@ int pkcs8_decode_flexi(const unsigned char *in, unsigned long inlen,
485485
const void *pwd, unsigned long pwdlen,
486486
ltc_asn1_list **decoded_list);
487487

488+
int pkcs8_get_children(const ltc_asn1_list *decoded_list, enum ltc_oid_id *pka,
489+
ltc_asn1_list **seq, ltc_asn1_list **priv_key);
490+
488491
#endif /* LTC_PKCS_8 */
489492

490493

src/pk/asn1/pkcs8/pkcs8_get.c

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/* LibTomCrypt, modular cryptographic library -- Tom St Denis */
2+
/* SPDX-License-Identifier: Unlicense */
3+
#include "tomcrypt_private.h"
4+
5+
/**
6+
@file pkcs8_get.c
7+
PKCS#8 utility functions
8+
*/
9+
10+
#ifdef LTC_PKCS_8
11+
12+
int pkcs8_get_children(const ltc_asn1_list *decoded_list, enum ltc_oid_id *pka, ltc_asn1_list **alg_id, ltc_asn1_list **priv_key)
13+
{
14+
int err;
15+
unsigned long n;
16+
der_flexi_check flexi_should[4];
17+
ltc_asn1_list *seq_l, *version;
18+
19+
LTC_ARGCHK(ltc_mp.name != NULL);
20+
21+
if (alg_id == NULL) alg_id = &seq_l;
22+
23+
/* Setup for basic structure */
24+
n=0;
25+
LTC_SET_DER_FLEXI_CHECK(flexi_should, n++, LTC_ASN1_INTEGER, &version);
26+
LTC_SET_DER_FLEXI_CHECK(flexi_should, n++, LTC_ASN1_SEQUENCE, alg_id);
27+
LTC_SET_DER_FLEXI_CHECK(flexi_should, n++, LTC_ASN1_OCTET_STRING, priv_key);
28+
LTC_SET_DER_FLEXI_CHECK(flexi_should, n, LTC_ASN1_EOL, NULL);
29+
30+
err = der_flexi_sequence_cmp(decoded_list, flexi_should);
31+
switch (err) {
32+
case CRYPT_OK:
33+
case CRYPT_INPUT_TOO_LONG:
34+
/* If there are attributes added after the private_key it is tagged with version 1 and
35+
* we get an 'input too long' error but the rest is already decoded and can be
36+
* handled the same as for version 0
37+
*/
38+
if (mp_cmp_d(version->data, 0) != LTC_MP_EQ && mp_cmp_d(version->data, 1) != LTC_MP_EQ) {
39+
return CRYPT_INVALID_PACKET;
40+
}
41+
break;
42+
default:
43+
return err;
44+
}
45+
return pk_get_oid_from_asn1((*alg_id)->child, pka);
46+
}
47+
48+
#endif /* LTC_PKCS_8 */

0 commit comments

Comments
 (0)