Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/headers/tomcrypt_private.h
Original file line number Diff line number Diff line change
Expand Up @@ -951,7 +951,7 @@ int x509_encode_subject_public_key_info(unsigned char *out, unsigned long *outle
ltc_asn1_type parameters_type, ltc_asn1_list* parameters, unsigned long parameters_len);

int x509_decode_subject_public_key_info(const unsigned char *in, unsigned long inlen,
enum ltc_oid_id algorithm, void *public_key, unsigned long *public_key_len,
enum ltc_oid_id algorithm, void *public_key, unsigned long *public_key_len, unsigned long public_key_len_should,
ltc_asn1_type parameters_type, ltc_asn1_list* parameters, unsigned long *parameters_len);

int x509_get_pka(const ltc_asn1_list *pub, enum ltc_pka_id *pka);
Expand Down
2 changes: 1 addition & 1 deletion src/pk/asn1/x509/x509_decode_public_key_from_certificate.c
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ int x509_process_public_key_from_spki(const unsigned char *in, unsigned long inl
}

err = x509_decode_subject_public_key_info(in, inlen,
algorithm, tmpbuf, &tmpbuf_len,
algorithm, tmpbuf, &tmpbuf_len, 0,
param_type, parameters, parameters_len);
if (err == CRYPT_OK) {
err = callback(tmpbuf, tmpbuf_len, key);
Expand Down
7 changes: 6 additions & 1 deletion src/pk/asn1/x509/x509_decode_subject_public_key_info.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,14 @@
@param algorithm One out of the enum #public_key_algorithms
@param public_key The buffer for the public key
@param public_key_len [in/out] The length of the public key buffer and the written length
@param public_key_len_should The expected length of the public key, 0 means don't check
@param parameters_type The parameters' type out of the enum ltc_asn1_type
@param parameters The parameters to include
@param parameters_len [in/out] The number of parameters to include
@return CRYPT_OK on success
*/
int x509_decode_subject_public_key_info(const unsigned char *in, unsigned long inlen,
enum ltc_oid_id algorithm, void *public_key, unsigned long *public_key_len,
enum ltc_oid_id algorithm, void *public_key, unsigned long *public_key_len, unsigned long public_key_len_should,
ltc_asn1_type parameters_type, ltc_asn1_list* parameters, unsigned long *parameters_len)
{
int err;
Expand Down Expand Up @@ -100,6 +101,10 @@ int x509_decode_subject_public_key_info(const unsigned char *in, unsigned long i
}

len = subject_pubkey[1].size/8;
if (public_key_len_should != 0 && len != public_key_len_should) {
err = CRYPT_PK_INVALID_SIZE;
goto LBL_ERR;
}
if (*public_key_len >= len) {
XMEMCPY(public_key, subject_pubkey[1].data, len);
*public_key_len = len;
Expand Down
2 changes: 1 addition & 1 deletion src/pk/dsa/dsa_import.c
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ static int s_dsa_import_spki(const unsigned char *in, unsigned long inlen, dsa_k
}

err = x509_decode_subject_public_key_info(in, inlen,
LTC_OID_DSA, tmpbuf, &tmpbuf_len,
LTC_OID_DSA, tmpbuf, &tmpbuf_len, 0,
LTC_ASN1_SEQUENCE, params, &len);
if (err != CRYPT_OK) {
goto LBL_ERR;
Expand Down
14 changes: 10 additions & 4 deletions src/pk/ec25519/ec25519_import_pkcs8.c
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,17 @@ int ec25519_import_pkcs8_asn1(ltc_asn1_list *alg_id, ltc_asn1_list *priv_key,
}

key_len = sizeof(key->priv);
if ((err = der_decode_octet_string(priv_key->data, priv_key->size, key->priv, &key_len)) == CRYPT_OK) {
fp(key->pub, key->priv);
key->type = PK_PRIVATE;
err = pk_get_pka_id(id, &key->pka);
if ((err = der_decode_octet_string(priv_key->data, priv_key->size, key->priv, &key_len)) != CRYPT_OK) {
goto LBL_ERR;
}
if (key_len != sizeof(key->priv)) {
err = CRYPT_PK_INVALID_SIZE;
goto LBL_ERR;
}
fp(key->pub, key->priv);
key->type = PK_PRIVATE;
err = pk_get_pka_id(id, &key->pka);
LBL_ERR:
return err;
}

Expand Down
18 changes: 13 additions & 5 deletions src/pk/ec448/ec448_import_pkcs8.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ int ec448_import_pkcs8_asn1(ltc_asn1_list *alg_id, ltc_asn1_list *priv_key,
curve448_key *key)
{
int err;
unsigned long key_len;
unsigned long key_len, priv_len;
sk_to_pk fp;

LTC_ARGCHK(key != NULL);
Expand All @@ -27,20 +27,28 @@ int ec448_import_pkcs8_asn1(ltc_asn1_list *alg_id, ltc_asn1_list *priv_key,
switch (id) {
case LTC_OID_X448:
fp = ec448_scalarmult_base_internal;
priv_len = 56uL;
break;
case LTC_OID_ED448:
fp = ec448_sk_to_pk_internal;
priv_len = 57uL;
break;
default:
return CRYPT_PK_INVALID_TYPE;
}

key_len = sizeof(key->priv);
if ((err = der_decode_octet_string(priv_key->data, priv_key->size, key->priv, &key_len)) == CRYPT_OK) {
fp(key->pub, key->priv);
key->type = PK_PRIVATE;
err = pk_get_pka_id(id, &key->pka);
if ((err = der_decode_octet_string(priv_key->data, priv_key->size, key->priv, &key_len)) != CRYPT_OK) {
goto LBL_ERR;
}
if (key_len != priv_len) {
err = CRYPT_PK_INVALID_SIZE;
goto LBL_ERR;
}
fp(key->pub, key->priv);
key->type = PK_PRIVATE;
err = pk_get_pka_id(id, &key->pka);
LBL_ERR:
return err;
}

Expand Down
2 changes: 1 addition & 1 deletion src/pk/ecc/ecc_import_openssl.c
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ int ecc_import_with_curve(const unsigned char *in, unsigned long inlen, int type
/* try to load public key */
len_xy = sizeof(bin_xy);
len = 6;
err = x509_decode_subject_public_key_info(in, inlen, LTC_OID_EC, bin_xy, &len_xy, LTC_ASN1_SEQUENCE, seq_ecparams, &len);
err = x509_decode_subject_public_key_info(in, inlen, LTC_OID_EC, bin_xy, &len_xy, 0, LTC_ASN1_SEQUENCE, seq_ecparams, &len);
} else {
err = CRYPT_PK_INVALID_TYPE;
}
Expand Down
2 changes: 1 addition & 1 deletion src/pk/ecc/ecc_import_x509.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ static int s_ecc_import_x509_with_oid(const unsigned char *in, unsigned long inl

len_xy = sizeof(bin_xy);
len_oid = 16;
err = x509_decode_subject_public_key_info(in, inlen, LTC_OID_EC, bin_xy, &len_xy,
err = x509_decode_subject_public_key_info(in, inlen, LTC_OID_EC, bin_xy, &len_xy, 0,
LTC_ASN1_OBJECT_IDENTIFIER, (void *)curveoid, &len_oid);
if (err != CRYPT_OK) { goto error; }
err = ecc_import_with_oid(bin_xy, len_xy, curveoid, len_oid, PK_PUBLIC, key);
Expand Down
2 changes: 1 addition & 1 deletion src/pk/ed25519/ed25519_import.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ int ed25519_import(const unsigned char *in, unsigned long inlen, curve25519_key
LTC_ARGCHK(key != NULL);

key_len = sizeof(key->pub);
if ((err = x509_decode_subject_public_key_info(in, inlen, LTC_OID_ED25519, key->pub, &key_len, LTC_ASN1_EOL, NULL, 0uL)) == CRYPT_OK) {
if ((err = x509_decode_subject_public_key_info(in, inlen, LTC_OID_ED25519, key->pub, &key_len, sizeof(key->pub), LTC_ASN1_EOL, NULL, 0uL)) == CRYPT_OK) {
key->type = PK_PUBLIC;
key->pka = LTC_PKA_ED25519;
}
Expand Down
2 changes: 1 addition & 1 deletion src/pk/ed448/ed448_import.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ int ed448_import(const unsigned char *in, unsigned long inlen, curve448_key *key
LTC_ARGCHK(key != NULL);

key_len = sizeof(key->pub);
if ((err = x509_decode_subject_public_key_info(in, inlen, LTC_OID_ED448, key->pub, &key_len, LTC_ASN1_EOL, NULL, 0uL)) == CRYPT_OK) {
if ((err = x509_decode_subject_public_key_info(in, inlen, LTC_OID_ED448, key->pub, &key_len, sizeof(key->pub), LTC_ASN1_EOL, NULL, 0uL)) == CRYPT_OK) {
key->type = PK_PUBLIC;
key->pka = LTC_PKA_ED448;
}
Expand Down
2 changes: 1 addition & 1 deletion src/pk/x25519/x25519_import.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ int x25519_import(const unsigned char *in, unsigned long inlen, curve25519_key *
LTC_ARGCHK(key != NULL);

key_len = sizeof(key->pub);
if ((err = x509_decode_subject_public_key_info(in, inlen, LTC_OID_X25519, key->pub, &key_len, LTC_ASN1_EOL, NULL, 0uL)) == CRYPT_OK) {
if ((err = x509_decode_subject_public_key_info(in, inlen, LTC_OID_X25519, key->pub, &key_len, sizeof(key->pub), LTC_ASN1_EOL, NULL, 0uL)) == CRYPT_OK) {
key->type = PK_PUBLIC;
key->pka = LTC_PKA_X25519;
}
Expand Down
2 changes: 1 addition & 1 deletion src/pk/x448/x448_import.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ int x448_import(const unsigned char *in, unsigned long inlen, curve448_key *key)
LTC_ARGCHK(key != NULL);

key_len = 56uL;
if ((err = x509_decode_subject_public_key_info(in, inlen, LTC_OID_X448, key->pub, &key_len, LTC_ASN1_EOL, NULL, 0uL)) == CRYPT_OK) {
if ((err = x509_decode_subject_public_key_info(in, inlen, LTC_OID_X448, key->pub, &key_len, 56uL, LTC_ASN1_EOL, NULL, 0uL)) == CRYPT_OK) {
key->type = PK_PUBLIC;
key->pka = LTC_PKA_X448;
}
Expand Down
14 changes: 14 additions & 0 deletions tests/ed25519_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -469,9 +469,23 @@ int ed25519_test(void)
return ret;
}

static int s_ed25519_short_key_test(void)
{
/* PrivateKeyInfo and SubjectPublicKeyInfo with a 1 byte key */
const unsigned char p8[] = { 0x30, 0x0f, 0x02, 0x01, 0x00, 0x30, 0x05, 0x06, 0x03, 0x2b, 0x65, 0x70,
0x04, 0x03, 0x04, 0x01, 0x01 };
const unsigned char spki[] = { 0x30, 0x0b, 0x30, 0x05, 0x06, 0x03, 0x2b, 0x65, 0x70, 0x03, 0x02, 0x00, 0x01 };
curve25519_key key;

SHOULD_FAIL_WITH(ed25519_import_pkcs8(p8, sizeof(p8), NULL, &key), CRYPT_PK_INVALID_SIZE);
SHOULD_FAIL_WITH(ed25519_import(spki, sizeof(spki), &key), CRYPT_PK_INVALID_SIZE);
return CRYPT_OK;
}

int ed25519_mpi_test(void)
{
if (ltc_mp.name == NULL) return CRYPT_NOP;
DO(s_ed25519_short_key_test());
DO(s_rfc_8410_10_test());
DO(s_ed25519_pkcs8_params_test());
return CRYPT_OK;
Expand Down
14 changes: 14 additions & 0 deletions tests/ed448_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -472,9 +472,23 @@ int ed448_test(void)
return CRYPT_OK;
}

static int s_ed448_short_key_test(void)
{
/* PrivateKeyInfo and SubjectPublicKeyInfo with a 1 byte key */
const unsigned char p8[] = { 0x30, 0x0f, 0x02, 0x01, 0x00, 0x30, 0x05, 0x06, 0x03, 0x2b, 0x65, 0x71,
0x04, 0x03, 0x04, 0x01, 0x01 };
const unsigned char spki[] = { 0x30, 0x0b, 0x30, 0x05, 0x06, 0x03, 0x2b, 0x65, 0x71, 0x03, 0x02, 0x00, 0x01 };
curve448_key key;

SHOULD_FAIL_WITH(ed448_import_pkcs8(p8, sizeof(p8), NULL, &key), CRYPT_PK_INVALID_SIZE);
SHOULD_FAIL_WITH(ed448_import(spki, sizeof(spki), &key), CRYPT_PK_INVALID_SIZE);
return CRYPT_OK;
}

int ed448_mpi_test(void)
{
if (ltc_mp.name == NULL) return CRYPT_NOP;
DO(s_ed448_short_key_test());
DO(s_ed448_compat_test());
return CRYPT_OK;
}
Expand Down
14 changes: 14 additions & 0 deletions tests/x25519_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -269,9 +269,23 @@ int x25519_test(void)
return CRYPT_OK;
}

static int s_x25519_short_key_test(void)
{
/* PrivateKeyInfo and SubjectPublicKeyInfo with a 1 byte key */
const unsigned char p8[] = { 0x30, 0x0f, 0x02, 0x01, 0x00, 0x30, 0x05, 0x06, 0x03, 0x2b, 0x65, 0x6e,
0x04, 0x03, 0x04, 0x01, 0x01 };
const unsigned char spki[] = { 0x30, 0x0b, 0x30, 0x05, 0x06, 0x03, 0x2b, 0x65, 0x6e, 0x03, 0x02, 0x00, 0x01 };
curve25519_key key;

SHOULD_FAIL_WITH(x25519_import_pkcs8(p8, sizeof(p8), NULL, &key), CRYPT_PK_INVALID_SIZE);
SHOULD_FAIL_WITH(x25519_import(spki, sizeof(spki), &key), CRYPT_PK_INVALID_SIZE);
return CRYPT_OK;
}

int x25519_mpi_test(void)
{
if (ltc_mp.name == NULL) return CRYPT_NOP;
DO(s_x25519_short_key_test());
DO(s_x25519_pkcs8_test());
DO(s_x25519_pkcs8_params_test());
DO(s_rfc_8410_10_test());
Expand Down
14 changes: 14 additions & 0 deletions tests/x448_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -261,9 +261,23 @@ int x448_test(void)
return CRYPT_OK;
}

static int s_x448_short_key_test(void)
{
/* PrivateKeyInfo and SubjectPublicKeyInfo with a 1 byte key */
const unsigned char p8[] = { 0x30, 0x0f, 0x02, 0x01, 0x00, 0x30, 0x05, 0x06, 0x03, 0x2b, 0x65, 0x6f,
0x04, 0x03, 0x04, 0x01, 0x01 };
const unsigned char spki[] = { 0x30, 0x0b, 0x30, 0x05, 0x06, 0x03, 0x2b, 0x65, 0x6f, 0x03, 0x02, 0x00, 0x01 };
curve448_key key;

SHOULD_FAIL_WITH(x448_import_pkcs8(p8, sizeof(p8), NULL, &key), CRYPT_PK_INVALID_SIZE);
SHOULD_FAIL_WITH(x448_import(spki, sizeof(spki), &key), CRYPT_PK_INVALID_SIZE);
return CRYPT_OK;
}

int x448_mpi_test(void)
{
if (ltc_mp.name == NULL) return CRYPT_NOP;
DO(s_x448_short_key_test());
DO(s_x448_compat_test());
return CRYPT_OK;
}
Expand Down
Loading