From 07efd88e4d501f00e900bd29332464c10f718fea Mon Sep 17 00:00:00 2001 From: David Garske Date: Mon, 14 Nov 2016 09:53:31 -0800 Subject: [PATCH 1/2] Fix for "wc_EccPrivateKeyDecode" to handle custom curve OID. --- wolfcrypt/src/asn.c | 19 +++++++++++++------ wolfcrypt/src/ecc.c | 15 +++++++++++---- wolfssl/wolfcrypt/ecc.h | 3 +++ 3 files changed, 27 insertions(+), 10 deletions(-) diff --git a/wolfcrypt/src/asn.c b/wolfcrypt/src/asn.c index 30acdf711..cf5a53ed1 100644 --- a/wolfcrypt/src/asn.c +++ b/wolfcrypt/src/asn.c @@ -1313,7 +1313,7 @@ int GetObjectId(const byte* input, word32* inOutIdx, word32* oid, #endif /* HAVE_OID_DECODING */ #endif - if (checkOid != NULL && + if (checkOid != NULL && (checkOidSz != actualOidSz || XMEMCMP(actualOid, checkOid, checkOidSz) != 0)) { WOLFSSL_MSG("OID Check Failed"); @@ -8864,11 +8864,12 @@ int DecodeECC_DSA_Sig(const byte* sig, word32 sigLen, mp_int* r, mp_int* s) int wc_EccPrivateKeyDecode(const byte* input, word32* inOutIdx, ecc_key* key, word32 inSz) { - word32 oid = 0; + word32 oidSum = 0; int version, length; int privSz, pubSz; byte b; int ret = 0; + int curve_id = ECC_CURVE_DEF; #ifdef WOLFSSL_SMALL_STACK byte* priv; byte* pub; @@ -8936,11 +8937,17 @@ int wc_EccPrivateKeyDecode(const byte* input, word32* inOutIdx, ecc_key* key, } else { while(length--) { - oid += input[*inOutIdx]; + oidSum += input[*inOutIdx]; *inOutIdx += 1; } - if (CheckCurve(oid) < 0) + if (CheckCurve(oidSum) < 0) ret = ECC_CURVE_OID_E; + + ret = wc_ecc_get_oid(oidSum, NULL, NULL); + if (ret >= 0) { + curve_id = ret; + ret = 0; + } } } } @@ -8984,8 +8991,8 @@ int wc_EccPrivateKeyDecode(const byte* input, word32* inOutIdx, ecc_key* key, if (pubSz < 2*(ECC_MAXSIZE+1)) { XMEMCPY(pub, &input[*inOutIdx], pubSz); *inOutIdx += length; - ret = wc_ecc_import_private_key(priv, privSz, pub, pubSz, - key); + ret = wc_ecc_import_private_key_ex(priv, privSz, pub, + pubSz, key, curve_id); } else ret = BUFFER_E; } diff --git a/wolfcrypt/src/ecc.c b/wolfcrypt/src/ecc.c index 400136024..d75e88c0e 100644 --- a/wolfcrypt/src/ecc.c +++ b/wolfcrypt/src/ecc.c @@ -4086,11 +4086,10 @@ int wc_ecc_export_private_only(ecc_key* key, byte* out, word32* outLen) #endif /* HAVE_ECC_KEY_EXPORT */ #ifdef HAVE_ECC_KEY_IMPORT -/* ecc private key import, public key in ANSI X9.63 format, private raw */ -int wc_ecc_import_private_key(const byte* priv, word32 privSz, const byte* pub, - word32 pubSz, ecc_key* key) +int wc_ecc_import_private_key_ex(const byte* priv, word32 privSz, const byte* pub, + word32 pubSz, ecc_key* key, int curve_id) { - int ret = wc_ecc_import_x963(pub, pubSz, key); + int ret = wc_ecc_import_x963_ex(pub, pubSz, key, curve_id); if (ret != 0) return ret; @@ -4105,6 +4104,14 @@ int wc_ecc_import_private_key(const byte* priv, word32 privSz, const byte* pub, return ret; } + +/* ecc private key import, public key in ANSI X9.63 format, private raw */ +int wc_ecc_import_private_key(const byte* priv, word32 privSz, const byte* pub, + word32 pubSz, ecc_key* key) +{ + return wc_ecc_import_private_key_ex(priv, privSz, pub, pubSz, key, + ECC_CURVE_DEF); +} #endif /* HAVE_ECC_KEY_IMPORT */ #ifndef NO_ASN diff --git a/wolfssl/wolfcrypt/ecc.h b/wolfssl/wolfcrypt/ecc.h index 70d67e481..7c25c13cf 100644 --- a/wolfssl/wolfcrypt/ecc.h +++ b/wolfssl/wolfcrypt/ecc.h @@ -331,6 +331,9 @@ WOLFSSL_API int wc_ecc_import_private_key(const byte* priv, word32 privSz, const byte* pub, word32 pubSz, ecc_key* key); WOLFSSL_API +int wc_ecc_import_private_key_ex(const byte* priv, word32 privSz, + const byte* pub, word32 pubSz, ecc_key* key, int curve_id); +WOLFSSL_API int wc_ecc_rs_to_sig(const char* r, const char* s, byte* out, word32* outlen); WOLFSSL_API int wc_ecc_import_raw(ecc_key* key, const char* qx, const char* qy, From a2d29e4c71598b121a5f7b2f02bbe1c2673d805b Mon Sep 17 00:00:00 2001 From: David Garske Date: Mon, 14 Nov 2016 10:06:20 -0800 Subject: [PATCH 2/2] Further improve the "wc_EccPrivateKeyDecode" to use the CheckCurve return code, which is the curve_id in the success case. Fixes scan-build warning. --- wolfcrypt/src/asn.c | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/wolfcrypt/src/asn.c b/wolfcrypt/src/asn.c index cf5a53ed1..a153f3841 100644 --- a/wolfcrypt/src/asn.c +++ b/wolfcrypt/src/asn.c @@ -8940,11 +8940,10 @@ int wc_EccPrivateKeyDecode(const byte* input, word32* inOutIdx, ecc_key* key, oidSum += input[*inOutIdx]; *inOutIdx += 1; } - if (CheckCurve(oidSum) < 0) + if ((ret = CheckCurve(oidSum)) < 0) { ret = ECC_CURVE_OID_E; - - ret = wc_ecc_get_oid(oidSum, NULL, NULL); - if (ret >= 0) { + } + else { curve_id = ret; ret = 0; }