diff --git a/src/misc/pem/pem_pkcs.c b/src/misc/pem/pem_pkcs.c index 6a6d4e45..5f016e0b 100644 --- a/src/misc/pem/pem_pkcs.c +++ b/src/misc/pem/pem_pkcs.c @@ -73,7 +73,7 @@ static int s_import_pkcs8(unsigned char *asn1_cert, unsigned long asn1_len, ltc_ { int err; enum ltc_oid_id oid_id; - ltc_asn1_list *alg_id, *priv_key; + ltc_asn1_list *alg_id = NULL, *priv_key = NULL; ltc_asn1_list *p8_asn1 = NULL; if ((err = pkcs8_decode_flexi(asn1_cert, asn1_len, pw_ctx, &p8_asn1)) != CRYPT_OK) { goto cleanup; diff --git a/src/pk/asn1/pkcs8/pkcs8_get.c b/src/pk/asn1/pkcs8/pkcs8_get.c index 015d1e7f..9dc34ab3 100644 --- a/src/pk/asn1/pkcs8/pkcs8_get.c +++ b/src/pk/asn1/pkcs8/pkcs8_get.c @@ -14,11 +14,16 @@ int pkcs8_get_children(const ltc_asn1_list *decoded_list, enum ltc_oid_id *pka, int err; unsigned long n; der_flexi_check flexi_should[4]; - ltc_asn1_list *seq_l, *version; + ltc_asn1_list *seq_l = NULL, *priv_l = NULL, *version = NULL; LTC_ARGCHK(ltc_mp.name != NULL); if (alg_id == NULL) alg_id = &seq_l; + if (priv_key == NULL) priv_key = &priv_l; + + /* der_flexi_sequence_cmp() writes only matched outputs, so unmatched ones stay NULL */ + *alg_id = NULL; + *priv_key = NULL; /* Setup for basic structure */ n=0; @@ -35,6 +40,9 @@ int pkcs8_get_children(const ltc_asn1_list *decoded_list, enum ltc_oid_id *pka, * we get an 'input too long' error but the rest is already decoded and can be * handled the same as for version 0 */ + if (version == NULL) { + return CRYPT_INVALID_PACKET; + } if (ltc_mp_cmp_d(version->data, 0) != LTC_MP_EQ && ltc_mp_cmp_d(version->data, 1) != LTC_MP_EQ) { return CRYPT_INVALID_PACKET; } @@ -42,6 +50,9 @@ int pkcs8_get_children(const ltc_asn1_list *decoded_list, enum ltc_oid_id *pka, default: return err; } + if ((*alg_id == NULL) || ((*alg_id)->child == NULL) || (*priv_key == NULL)) { + return CRYPT_INVALID_PACKET; + } return pk_get_oid_from_asn1((*alg_id)->child, pka); } diff --git a/src/pk/dh/dh_import_pkcs8.c b/src/pk/dh/dh_import_pkcs8.c index d1fd792b..2962bb1d 100644 --- a/src/pk/dh/dh_import_pkcs8.c +++ b/src/pk/dh/dh_import_pkcs8.c @@ -61,7 +61,7 @@ int dh_import_pkcs8(const unsigned char *in, unsigned long inlen, { int err; ltc_asn1_list *l = NULL; - ltc_asn1_list *alg_id, *priv_key; + ltc_asn1_list *alg_id = NULL, *priv_key = NULL; enum ltc_oid_id pka; LTC_ARGCHK(in != NULL); diff --git a/src/pk/dsa/dsa_import_pkcs8.c b/src/pk/dsa/dsa_import_pkcs8.c index b3bc9c95..974631ea 100644 --- a/src/pk/dsa/dsa_import_pkcs8.c +++ b/src/pk/dsa/dsa_import_pkcs8.c @@ -62,7 +62,7 @@ int dsa_import_pkcs8(const unsigned char *in, unsigned long inlen, { int err; ltc_asn1_list *l = NULL; - ltc_asn1_list *alg_id, *priv_key; + ltc_asn1_list *alg_id = NULL, *priv_key = NULL; enum ltc_oid_id pka; LTC_ARGCHK(in != NULL); diff --git a/src/pk/ec25519/ec25519_import_pkcs8.c b/src/pk/ec25519/ec25519_import_pkcs8.c index 4a7f9fb9..9f9a6bcb 100644 --- a/src/pk/ec25519/ec25519_import_pkcs8.c +++ b/src/pk/ec25519/ec25519_import_pkcs8.c @@ -60,7 +60,7 @@ int ec25519_import_pkcs8(const unsigned char *in, unsigned long inlen, { int err; ltc_asn1_list *l = NULL; - ltc_asn1_list *alg_id, *priv_key; + ltc_asn1_list *alg_id = NULL, *priv_key = NULL; enum ltc_oid_id pka; LTC_ARGCHK(in != NULL); diff --git a/src/pk/ec448/ec448_import_pkcs8.c b/src/pk/ec448/ec448_import_pkcs8.c index 0de08347..37ffba3b 100644 --- a/src/pk/ec448/ec448_import_pkcs8.c +++ b/src/pk/ec448/ec448_import_pkcs8.c @@ -60,7 +60,7 @@ int ec448_import_pkcs8(const unsigned char *in, unsigned long inlen, { int err; ltc_asn1_list *l = NULL; - ltc_asn1_list *alg_id, *priv_key; + ltc_asn1_list *alg_id = NULL, *priv_key = NULL; enum ltc_oid_id pka; LTC_ARGCHK(in != NULL); diff --git a/src/pk/ecc/ecc_import_pkcs8.c b/src/pk/ecc/ecc_import_pkcs8.c index 9df67890..ad32fa07 100644 --- a/src/pk/ecc/ecc_import_pkcs8.c +++ b/src/pk/ecc/ecc_import_pkcs8.c @@ -140,7 +140,7 @@ int ecc_import_pkcs8(const unsigned char *in, unsigned long inlen, { int err; ltc_asn1_list *l = NULL; - ltc_asn1_list *alg_id, *priv_key; + ltc_asn1_list *alg_id = NULL, *priv_key = NULL; enum ltc_oid_id pka; LTC_ARGCHK(key != NULL); diff --git a/src/pk/rsa/rsa_import_pkcs8.c b/src/pk/rsa/rsa_import_pkcs8.c index 0a4570c0..406772e0 100644 --- a/src/pk/rsa/rsa_import_pkcs8.c +++ b/src/pk/rsa/rsa_import_pkcs8.c @@ -44,7 +44,7 @@ int rsa_import_pkcs8(const unsigned char *in, unsigned long inlen, { int err; ltc_asn1_list *l = NULL; - ltc_asn1_list *alg_id, *priv_key; + ltc_asn1_list *alg_id = NULL, *priv_key = NULL; enum ltc_oid_id pka; LTC_ARGCHK(in != NULL);