Fix for wc_ecc_get_curve_id_from_oid with combinations of HAVE_OID_ENCODING and HAVE_OID_DECODING. Fix in SetCurve for unused outSz with HAVE_OID_ENCODING.

This commit is contained in:
David Garske
2022-07-08 12:56:55 -07:00
parent 56325143f1
commit 4892435004
4 changed files with 31 additions and 12 deletions

View File

@ -26322,7 +26322,7 @@ static int test_wc_ecc_get_curve_id_from_oid (void)
/* Good Case */ /* Good Case */
if (ret == 0) { if (ret == 0) {
ret = wc_ecc_get_curve_id_from_oid(oid, len); ret = wc_ecc_get_curve_id_from_oid(oid, len);
if (ret == 7) { if (ret == ECC_SECP256R1) {
ret = 0; ret = 0;
} }
} }

View File

@ -5216,7 +5216,7 @@ int EncodeObjectId(const word16* in, word32 inSz, byte* out, word32* outSz)
* @param [in] in Byte array containing OID. * @param [in] in Byte array containing OID.
* @param [in] inSz Size of OID in bytes. * @param [in] inSz Size of OID in bytes.
* @param [in] out Array to hold dotted form of OID. * @param [in] out Array to hold dotted form of OID.
* @param [in, out] outSz On in, number of elemnts in array. * @param [in, out] outSz On in, number of elements in array.
* On out, count of numbers in dotted form. * On out, count of numbers in dotted form.
* @return 0 on success * @return 0 on success
* @return BAD_FUNC_ARG when in or outSz is NULL. * @return BAD_FUNC_ARG when in or outSz is NULL.
@ -13289,14 +13289,16 @@ static int SetCurve(ecc_key* key, byte* output, size_t outSz)
return idx + oidSz; return idx + oidSz;
} }
/* verify output buffer has room */
if (oidSz > outSz)
return BUFFER_E;
#ifdef HAVE_OID_ENCODING #ifdef HAVE_OID_ENCODING
ret = EncodeObjectId(key->dp->oid, key->dp->oidSz, output+idx, &oidSz); ret = EncodeObjectId(key->dp->oid, key->dp->oidSz, output+idx, &oidSz);
if (ret != 0) { if (ret != 0) {
return ret; return ret;
} }
#else #else
if (oidSz > outSz)
return BUFFER_E;
XMEMCPY(output+idx, key->dp->oid, oidSz); XMEMCPY(output+idx, key->dp->oid, oidSz);
#endif #endif
idx += oidSz; idx += oidSz;

View File

@ -4096,16 +4096,21 @@ int wc_ecc_get_curve_id_from_dp_params(const ecc_set_type* dp)
int wc_ecc_get_curve_id_from_oid(const byte* oid, word32 len) int wc_ecc_get_curve_id_from_oid(const byte* oid, word32 len)
{ {
int curve_idx; int curve_idx;
#ifdef HAVE_OID_DECODING #if defined(HAVE_OID_DECODING) || defined(HAVE_OID_ENCODING)
int ret; int ret;
word16 decOid[MAX_OID_SZ]; #ifdef HAVE_OID_DECODING
word32 decOidSz = sizeof(decOid); word16 decOid[MAX_OID_SZ/sizeof(word16)];
#else
byte decOid[MAX_OID_SZ];
#endif
word32 decOidSz;
#endif #endif
if (oid == NULL) if (oid == NULL)
return BAD_FUNC_ARG; return BAD_FUNC_ARG;
#ifdef HAVE_OID_DECODING #ifdef HAVE_OID_DECODING
decOidSz = (word32)sizeof(decOid);
ret = DecodeObjectId(oid, len, decOid, &decOidSz); ret = DecodeObjectId(oid, len, decOid, &decOidSz);
if (ret != 0) { if (ret != 0) {
return ret; return ret;
@ -4113,18 +4118,29 @@ int wc_ecc_get_curve_id_from_oid(const byte* oid, word32 len)
#endif #endif
for (curve_idx = 0; ecc_sets[curve_idx].size != 0; curve_idx++) { for (curve_idx = 0; ecc_sets[curve_idx].size != 0; curve_idx++) {
#if defined(HAVE_OID_ENCODING) && !defined(HAVE_OID_DECODING)
decOidSz = (word32)sizeof(decOid);
ret = EncodeObjectId(ecc_sets[curve_idx].oid, ecc_sets[curve_idx].oidSz,
decOid, &decOidSz);
if (ret != 0) {
continue;
}
#endif
if ( if (
#ifndef WOLFSSL_ECC_CURVE_STATIC #ifndef WOLFSSL_ECC_CURVE_STATIC
ecc_sets[curve_idx].oid && ecc_sets[curve_idx].oid &&
#endif #endif
#ifdef HAVE_OID_DECODING #if defined(HAVE_OID_ENCODING) && !defined(HAVE_OID_DECODING)
decOidSz == len &&
XMEMCMP(decOid, oid, len) == 0
#elif defined(HAVE_OID_ENCODING) && defined(HAVE_OID_DECODING)
/* We double because decOidSz is a count of word16 elements. */ /* We double because decOidSz is a count of word16 elements. */
ecc_sets[curve_idx].oidSz == decOidSz && ecc_sets[curve_idx].oidSz == decOidSz &&
XMEMCMP(ecc_sets[curve_idx].oid, decOid, XMEMCMP(ecc_sets[curve_idx].oid, decOid, decOidSz * 2) == 0
decOidSz * 2) == 0
#else #else
ecc_sets[curve_idx].oidSz == len && ecc_sets[curve_idx].oidSz == len &&
XMEMCMP(ecc_sets[curve_idx].oid, oid, len) == 0 XMEMCMP(ecc_sets[curve_idx].oid, oid, len) == 0
#endif #endif
) { ) {
break; break;

View File

@ -929,7 +929,8 @@ enum Misc_ASN {
/* Max total extensions, id + len + others */ /* Max total extensions, id + len + others */
#endif #endif
#if defined(WOLFSSL_CERT_EXT) || defined(OPENSSL_EXTRA) || \ #if defined(WOLFSSL_CERT_EXT) || defined(OPENSSL_EXTRA) || \
defined(HAVE_PKCS7) || defined(OPENSSL_EXTRA_X509_SMALL) defined(HAVE_PKCS7) || defined(OPENSSL_EXTRA_X509_SMALL) || \
defined(HAVE_OID_DECODING) || defined(HAVE_OID_ENCODING)
MAX_OID_SZ = 32, /* Max DER length of OID*/ MAX_OID_SZ = 32, /* Max DER length of OID*/
MAX_OID_STRING_SZ = 64, /* Max string length representation of OID*/ MAX_OID_STRING_SZ = 64, /* Max string length representation of OID*/
#endif #endif