Merge pull request #631 from dgarske/ecc_privkey_import_oid

Fix for "wc_EccPrivateKeyDecode" to handle custom curve OID.
This commit is contained in:
toddouska 2016-11-14 11:52:48 -08:00 committed by GitHub
commit fa816f0460
3 changed files with 26 additions and 10 deletions

View File

@ -1313,7 +1313,7 @@ int GetObjectId(const byte* input, word32* inOutIdx, word32* oid,
#endif /* HAVE_OID_DECODING */ #endif /* HAVE_OID_DECODING */
#endif #endif
if (checkOid != NULL && if (checkOid != NULL &&
(checkOidSz != actualOidSz || (checkOidSz != actualOidSz ||
XMEMCMP(actualOid, checkOid, checkOidSz) != 0)) { XMEMCMP(actualOid, checkOid, checkOidSz) != 0)) {
WOLFSSL_MSG("OID Check Failed"); 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, int wc_EccPrivateKeyDecode(const byte* input, word32* inOutIdx, ecc_key* key,
word32 inSz) word32 inSz)
{ {
word32 oid = 0; word32 oidSum = 0;
int version, length; int version, length;
int privSz, pubSz; int privSz, pubSz;
byte b; byte b;
int ret = 0; int ret = 0;
int curve_id = ECC_CURVE_DEF;
#ifdef WOLFSSL_SMALL_STACK #ifdef WOLFSSL_SMALL_STACK
byte* priv; byte* priv;
byte* pub; byte* pub;
@ -8936,11 +8937,16 @@ int wc_EccPrivateKeyDecode(const byte* input, word32* inOutIdx, ecc_key* key,
} }
else { else {
while(length--) { while(length--) {
oid += input[*inOutIdx]; oidSum += input[*inOutIdx];
*inOutIdx += 1; *inOutIdx += 1;
} }
if (CheckCurve(oid) < 0) if ((ret = CheckCurve(oidSum)) < 0) {
ret = ECC_CURVE_OID_E; ret = ECC_CURVE_OID_E;
}
else {
curve_id = ret;
ret = 0;
}
} }
} }
} }
@ -8984,8 +8990,8 @@ int wc_EccPrivateKeyDecode(const byte* input, word32* inOutIdx, ecc_key* key,
if (pubSz < 2*(ECC_MAXSIZE+1)) { if (pubSz < 2*(ECC_MAXSIZE+1)) {
XMEMCPY(pub, &input[*inOutIdx], pubSz); XMEMCPY(pub, &input[*inOutIdx], pubSz);
*inOutIdx += length; *inOutIdx += length;
ret = wc_ecc_import_private_key(priv, privSz, pub, pubSz, ret = wc_ecc_import_private_key_ex(priv, privSz, pub,
key); pubSz, key, curve_id);
} else } else
ret = BUFFER_E; ret = BUFFER_E;
} }

View File

@ -4085,11 +4085,10 @@ int wc_ecc_export_private_only(ecc_key* key, byte* out, word32* outLen)
#endif /* HAVE_ECC_KEY_EXPORT */ #endif /* HAVE_ECC_KEY_EXPORT */
#ifdef HAVE_ECC_KEY_IMPORT #ifdef HAVE_ECC_KEY_IMPORT
/* ecc private key import, public key in ANSI X9.63 format, private raw */ int wc_ecc_import_private_key_ex(const byte* priv, word32 privSz, const byte* pub,
int wc_ecc_import_private_key(const byte* priv, word32 privSz, const byte* pub, word32 pubSz, ecc_key* key, int curve_id)
word32 pubSz, ecc_key* key)
{ {
int ret = wc_ecc_import_x963(pub, pubSz, key); int ret = wc_ecc_import_x963_ex(pub, pubSz, key, curve_id);
if (ret != 0) if (ret != 0)
return ret; return ret;
@ -4104,6 +4103,14 @@ int wc_ecc_import_private_key(const byte* priv, word32 privSz, const byte* pub,
return ret; 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 */ #endif /* HAVE_ECC_KEY_IMPORT */
#ifndef NO_ASN #ifndef NO_ASN

View File

@ -331,6 +331,9 @@ WOLFSSL_API
int wc_ecc_import_private_key(const byte* priv, word32 privSz, const byte* pub, int wc_ecc_import_private_key(const byte* priv, word32 privSz, const byte* pub,
word32 pubSz, ecc_key* key); word32 pubSz, ecc_key* key);
WOLFSSL_API 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); int wc_ecc_rs_to_sig(const char* r, const char* s, byte* out, word32* outlen);
WOLFSSL_API WOLFSSL_API
int wc_ecc_import_raw(ecc_key* key, const char* qx, const char* qy, int wc_ecc_import_raw(ecc_key* key, const char* qx, const char* qy,