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