Merge pull request #3518 from julek-wolfssl/openssh-fixes-v2

Fixes for openssh
This commit is contained in:
toddouska
2021-01-25 14:45:56 -08:00
committed by GitHub
6 changed files with 126 additions and 58 deletions

104
src/ssl.c
View File

@@ -7927,7 +7927,7 @@ WOLFSSL_EVP_PKEY* wolfSSL_d2i_PrivateKey(int type, WOLFSSL_EVP_PKEY** out,
} }
break; break;
#endif /* HAVE_ECC */ #endif /* HAVE_ECC */
#if defined(WOLFSSL_QT) || defined(OPENSSL_ALL) #if defined(WOLFSSL_QT) || defined(OPENSSL_ALL) || defined(WOLFSSL_OPENSSH)
#ifndef NO_DSA #ifndef NO_DSA
case EVP_PKEY_DSA: case EVP_PKEY_DSA:
local->ownDsa = 1; local->ownDsa = 1;
@@ -7962,7 +7962,7 @@ WOLFSSL_EVP_PKEY* wolfSSL_d2i_PrivateKey(int type, WOLFSSL_EVP_PKEY** out,
break; break;
#endif /* !HAVE_FIPS || HAVE_FIPS_VERSION > 2 */ #endif /* !HAVE_FIPS || HAVE_FIPS_VERSION > 2 */
#endif /* HAVE_DH */ #endif /* HAVE_DH */
#endif /* WOLFSSL_QT || OPENSSL_ALL */ #endif /* WOLFSSL_QT || OPENSSL_ALL || WOLFSSL_OPENSSH */
default: default:
WOLFSSL_MSG("Unsupported key type"); WOLFSSL_MSG("Unsupported key type");
wolfSSL_EVP_PKEY_free(local); wolfSSL_EVP_PKEY_free(local);
@@ -36902,8 +36902,12 @@ int wolfSSL_ECDH_compute_key(void *out, size_t outlen,
void *out, size_t *outlen)) void *out, size_t *outlen))
{ {
word32 len; word32 len;
(void)KDF; ecc_key* key;
int ret;
#if defined(ECC_TIMING_RESISTANT) && !defined(HAVE_SELFTEST) \
&& !defined(HAVE_FIPS)
int setGlobalRNG = 0;
#endif
(void)KDF; (void)KDF;
WOLFSSL_ENTER("wolfSSL_ECDH_compute_key"); WOLFSSL_ENTER("wolfSSL_ECDH_compute_key");
@@ -36926,10 +36930,27 @@ int wolfSSL_ECDH_compute_key(void *out, size_t outlen,
} }
len = (word32)outlen; len = (word32)outlen;
key = (ecc_key*)ecdh->internal;
if (wc_ecc_shared_secret_ssh((ecc_key*)ecdh->internal, #if defined(ECC_TIMING_RESISTANT) && !defined(HAVE_SELFTEST) \
(ecc_point*)pub_key->internal, && !defined(HAVE_FIPS)
(byte *)out, &len) != MP_OKAY) { if (key->rng == NULL) {
if (initGlobalRNG == 0 && wolfSSL_RAND_Init() != WOLFSSL_SUCCESS) {
WOLFSSL_MSG("No RNG to use");
return WOLFSSL_FATAL_ERROR;
}
key->rng = &globalRNG;
setGlobalRNG = 1;
}
#endif
ret = wc_ecc_shared_secret_ssh(key, (ecc_point*)pub_key->internal,
(byte *)out, &len);
#if defined(ECC_TIMING_RESISTANT) && !defined(HAVE_SELFTEST) \
&& !defined(HAVE_FIPS)
if (setGlobalRNG)
key->rng = NULL;
#endif
if (ret != MP_OKAY) {
WOLFSSL_MSG("wc_ecc_shared_secret failed"); WOLFSSL_MSG("wc_ecc_shared_secret failed");
return WOLFSSL_FATAL_ERROR; return WOLFSSL_FATAL_ERROR;
} }
@@ -51829,35 +51850,11 @@ int wolfSSL_X509_set_pubkey(WOLFSSL_X509 *cert, WOLFSSL_EVP_PKEY *pkey)
if (cert == NULL || pkey == NULL) if (cert == NULL || pkey == NULL)
return WOLFSSL_FAILURE; return WOLFSSL_FAILURE;
if (pkey->type == EVP_PKEY_RSA) if (pkey->type == EVP_PKEY_RSA
cert->pubKeyOID = RSAk; #ifndef NO_DSA
else if (pkey->type == EVP_PKEY_EC) || pkey->type == EVP_PKEY_DSA
cert->pubKeyOID = ECDSAk; #endif /* !NO_DSA */
else if (pkey->type == EVP_PKEY_DSA) ) {
cert->pubKeyOID = DSAk;
else
return WOLFSSL_FAILURE;
#ifdef OPENSSL_EXTRA
#if !defined(HAVE_FAST_RSA) && defined(WOLFSSL_KEY_GEN) && \
!defined(NO_RSA) && !defined(HAVE_USER_RSA)
if (pkey->type == EVP_PKEY_RSA) {
int pLen;
/* Public and private key formats differ. Make sure to put in the
* public key format in the cert. */
if ((pLen = wolfSSL_i2d_RSAPublicKey(pkey->rsa, (const byte**)&p)) <= 0) {
WOLFSSL_MSG("wolfSSL_i2d_RSAPublicKey error");
return WOLFSSL_FAILURE;
}
if (cert->pubKey.buffer != NULL)
XFREE(cert->pubKey.buffer, cert->heap, DYNAMIC_TYPE_PUBLIC_KEY);
cert->pubKey.buffer = p;
cert->pubKey.length = pLen;
}
else
#endif
#endif /* OPENSSL_EXTRA */
{
p = (byte*)XMALLOC(pkey->pkey_sz, cert->heap, DYNAMIC_TYPE_PUBLIC_KEY); p = (byte*)XMALLOC(pkey->pkey_sz, cert->heap, DYNAMIC_TYPE_PUBLIC_KEY);
if (p == NULL) if (p == NULL)
return WOLFSSL_FAILURE; return WOLFSSL_FAILURE;
@@ -51867,7 +51864,42 @@ int wolfSSL_X509_set_pubkey(WOLFSSL_X509 *cert, WOLFSSL_EVP_PKEY *pkey)
cert->pubKey.buffer = p; cert->pubKey.buffer = p;
XMEMCPY(cert->pubKey.buffer, pkey->pkey.ptr, pkey->pkey_sz); XMEMCPY(cert->pubKey.buffer, pkey->pkey.ptr, pkey->pkey_sz);
cert->pubKey.length = pkey->pkey_sz; cert->pubKey.length = pkey->pkey_sz;
#ifndef NO_DSA
if (pkey->type == EVP_PKEY_DSA)
cert->pubKeyOID = DSAk;
else
#endif /* !NO_DSA */
cert->pubKeyOID = RSAk;
} }
#ifdef HAVE_ECC
else if (pkey->type == EVP_PKEY_EC) {
/* Generate since pkey->pkey.ptr may contain private key */
ecc_key* ecc;
int derSz;
if (pkey->ecc == NULL || pkey->ecc->internal == NULL)
return WOLFSSL_FAILURE;
ecc = (ecc_key*)pkey->ecc->internal;
derSz = wc_EccPublicKeyDerSize(ecc, 1);
if (derSz <= 0)
return WOLFSSL_FAILURE;
p = (byte*)XMALLOC(derSz, cert->heap, DYNAMIC_TYPE_PUBLIC_KEY);
if (p == NULL)
return WOLFSSL_FAILURE;
if ((derSz = wc_EccPublicKeyToDer(ecc, p, derSz, 1)) <= 0) {
XFREE(p, cert->heap, DYNAMIC_TYPE_PUBLIC_KEY);
return WOLFSSL_FAILURE;
}
cert->pubKey.buffer = p;
cert->pubKey.length = derSz;
cert->pubKeyOID = ECDSAk;
}
#endif /* HAVE_ECC */
else
return WOLFSSL_FAILURE;
return WOLFSSL_SUCCESS; return WOLFSSL_SUCCESS;
} }

View File

@@ -33799,6 +33799,9 @@ static void test_wolfSSL_EVP_PKEY_set1_get1_EC_KEY (void)
/* Test wolfSSL_EVP_PKEY_set1_EC_KEY */ /* Test wolfSSL_EVP_PKEY_set1_EC_KEY */
AssertIntEQ(wolfSSL_EVP_PKEY_set1_EC_KEY(NULL, ecKey), WOLFSSL_FAILURE); AssertIntEQ(wolfSSL_EVP_PKEY_set1_EC_KEY(NULL, ecKey), WOLFSSL_FAILURE);
AssertIntEQ(wolfSSL_EVP_PKEY_set1_EC_KEY(pkey, NULL), WOLFSSL_FAILURE); AssertIntEQ(wolfSSL_EVP_PKEY_set1_EC_KEY(pkey, NULL), WOLFSSL_FAILURE);
/* Should fail since ecKey is empty */
AssertIntEQ(wolfSSL_EVP_PKEY_set1_EC_KEY(pkey, ecKey), WOLFSSL_FAILURE);
AssertIntEQ(wolfSSL_EC_KEY_generate_key(ecKey), 1);
AssertIntEQ(wolfSSL_EVP_PKEY_set1_EC_KEY(pkey, ecKey), WOLFSSL_SUCCESS); AssertIntEQ(wolfSSL_EVP_PKEY_set1_EC_KEY(pkey, ecKey), WOLFSSL_SUCCESS);
/* Test wolfSSL_EVP_PKEY_get1_EC_KEY */ /* Test wolfSSL_EVP_PKEY_get1_EC_KEY */
@@ -34131,6 +34134,8 @@ static void test_wolfSSL_EVP_PKEY_assign(void)
AssertIntEQ(wolfSSL_EVP_PKEY_assign(NULL,type,ecKey), WOLFSSL_FAILURE); AssertIntEQ(wolfSSL_EVP_PKEY_assign(NULL,type,ecKey), WOLFSSL_FAILURE);
AssertIntEQ(wolfSSL_EVP_PKEY_assign(pkey,type,NULL), WOLFSSL_FAILURE); AssertIntEQ(wolfSSL_EVP_PKEY_assign(pkey,type,NULL), WOLFSSL_FAILURE);
AssertIntEQ(wolfSSL_EVP_PKEY_assign(pkey,-1,ecKey), WOLFSSL_FAILURE); AssertIntEQ(wolfSSL_EVP_PKEY_assign(pkey,-1,ecKey), WOLFSSL_FAILURE);
AssertIntEQ(wolfSSL_EVP_PKEY_assign(pkey,type,ecKey), WOLFSSL_FAILURE);
AssertIntEQ(wolfSSL_EC_KEY_generate_key(ecKey), 1);
AssertIntEQ(wolfSSL_EVP_PKEY_assign(pkey,type,ecKey), WOLFSSL_SUCCESS); AssertIntEQ(wolfSSL_EVP_PKEY_assign(pkey,type,ecKey), WOLFSSL_SUCCESS);
wolfSSL_EVP_PKEY_free(pkey); wolfSSL_EVP_PKEY_free(pkey);
#endif /* HAVE_ECC */ #endif /* HAVE_ECC */
@@ -36615,6 +36620,9 @@ static void test_EVP_PKEY_ec(void)
AssertNotNull(pkey = wolfSSL_EVP_PKEY_new()); AssertNotNull(pkey = wolfSSL_EVP_PKEY_new());
AssertIntEQ(EVP_PKEY_assign_EC_KEY(NULL, ecKey), WOLFSSL_FAILURE); AssertIntEQ(EVP_PKEY_assign_EC_KEY(NULL, ecKey), WOLFSSL_FAILURE);
AssertIntEQ(EVP_PKEY_assign_EC_KEY(pkey, NULL), WOLFSSL_FAILURE); AssertIntEQ(EVP_PKEY_assign_EC_KEY(pkey, NULL), WOLFSSL_FAILURE);
/* Should fail since ecKey is empty */
AssertIntEQ(EVP_PKEY_assign_EC_KEY(pkey, ecKey), WOLFSSL_FAILURE);
AssertIntEQ(wolfSSL_EC_KEY_generate_key(ecKey), 1);
AssertIntEQ(EVP_PKEY_assign_EC_KEY(pkey, ecKey), WOLFSSL_SUCCESS); AssertIntEQ(EVP_PKEY_assign_EC_KEY(pkey, ecKey), WOLFSSL_SUCCESS);
wolfSSL_EVP_PKEY_free(pkey); wolfSSL_EVP_PKEY_free(pkey);

View File

@@ -16607,13 +16607,15 @@ int wc_EccPrivateKeyToDer(ecc_key* key, byte* output, word32 inLen)
} }
#ifdef HAVE_PKCS8 #ifdef HAVE_PKCS8
/* Write only private ecc key to unencrypted PKCS#8 format. /* Write only private ecc key or both private and public parts to unencrypted
* PKCS#8 format.
* *
* If output is NULL, places required PKCS#8 buffer size in outLen and * If output is NULL, places required PKCS#8 buffer size in outLen and
* returns LENGTH_ONLY_E. * returns LENGTH_ONLY_E.
* *
* return length on success else < 0 */ * return length on success else < 0 */
int wc_EccPrivateKeyToPKCS8(ecc_key* key, byte* output, word32* outLen) static int eccToPKCS8(ecc_key* key, byte* output, word32* outLen,
int includePublic)
{ {
int ret, tmpDerSz; int ret, tmpDerSz;
int algoID = 0; int algoID = 0;
@@ -16626,7 +16628,7 @@ int wc_EccPrivateKeyToPKCS8(ecc_key* key, byte* output, word32* outLen)
byte* tmpDer = NULL; byte* tmpDer = NULL;
#endif #endif
if (key == NULL || outLen == NULL) if (key == NULL || key->dp == NULL || outLen == NULL)
return BAD_FUNC_ARG; return BAD_FUNC_ARG;
/* set algoID, get curve OID */ /* set algoID, get curve OID */
@@ -16643,7 +16645,7 @@ int wc_EccPrivateKeyToPKCS8(ecc_key* key, byte* output, word32* outLen)
#endif #endif
XMEMSET(tmpDer, 0, ECC_BUFSIZE); XMEMSET(tmpDer, 0, ECC_BUFSIZE);
tmpDerSz = wc_BuildEccKeyDer(key, tmpDer, ECC_BUFSIZE, 0); tmpDerSz = wc_BuildEccKeyDer(key, tmpDer, ECC_BUFSIZE, includePublic);
if (tmpDerSz < 0) { if (tmpDerSz < 0) {
#ifndef WOLFSSL_NO_MALLOC #ifndef WOLFSSL_NO_MALLOC
XFREE(tmpDer, key->heap, DYNAMIC_TYPE_TMP_BUFFER); XFREE(tmpDer, key->heap, DYNAMIC_TYPE_TMP_BUFFER);
@@ -16693,6 +16695,23 @@ int wc_EccPrivateKeyToPKCS8(ecc_key* key, byte* output, word32* outLen)
*outLen = ret; *outLen = ret;
return ret; return ret;
} }
/* Write only private ecc key to unencrypted PKCS#8 format.
*
* return length on success else < 0 */
int wc_EccPrivateKeyToPKCS8(ecc_key* key, byte* output, word32* outLen)
{
return eccToPKCS8(key, output, outLen, 0);
}
/* Write both private and public ecc keys to unencrypted PKCS#8 format.
*
* return length on success else < 0 */
int wc_EccKeyToPKCS8(ecc_key* key, byte* output,
word32* outLen)
{
return eccToPKCS8(key, output, outLen, 1);
}
#endif /* HAVE_PKCS8 */ #endif /* HAVE_PKCS8 */
#endif /* HAVE_ECC_KEY_EXPORT && !NO_ASN_CRYPT */ #endif /* HAVE_ECC_KEY_EXPORT && !NO_ASN_CRYPT */
#endif /* HAVE_ECC */ #endif /* HAVE_ECC */

View File

@@ -3845,6 +3845,7 @@ static int wc_ecc_shared_secret_gen_sync(ecc_key* private_key, ecc_point* point,
#ifdef HAVE_ECC_CDH #ifdef HAVE_ECC_CDH
mp_int k_lcl; mp_int k_lcl;
WOLFSSL_ENTER("wc_ecc_shared_secret_gen_sync");
/* if cofactor flag has been set */ /* if cofactor flag has been set */
if (private_key->flags & WC_ECC_FLAG_COFACTOR) { if (private_key->flags & WC_ECC_FLAG_COFACTOR) {
mp_digit cofactor = (mp_digit)private_key->dp->cofactor; mp_digit cofactor = (mp_digit)private_key->dp->cofactor;
@@ -3861,6 +3862,8 @@ static int wc_ecc_shared_secret_gen_sync(ecc_key* private_key, ecc_point* point,
} }
} }
} }
#else
WOLFSSL_ENTER("wc_ecc_shared_secret_gen_sync");
#endif #endif
#ifdef WOLFSSL_HAVE_SP_ECC #ifdef WOLFSSL_HAVE_SP_ECC
@@ -3945,6 +3948,8 @@ static int wc_ecc_shared_secret_gen_sync(ecc_key* private_key, ecc_point* point,
mp_clear(k); mp_clear(k);
#endif #endif
WOLFSSL_LEAVE("wc_ecc_shared_secret_gen_sync", err);
return err; return err;
} }
@@ -4075,12 +4080,15 @@ int wc_ecc_shared_secret_ex(ecc_key* private_key, ecc_point* point,
/* type valid? */ /* type valid? */
if (private_key->type != ECC_PRIVATEKEY && if (private_key->type != ECC_PRIVATEKEY &&
private_key->type != ECC_PRIVATEKEY_ONLY) { private_key->type != ECC_PRIVATEKEY_ONLY) {
WOLFSSL_MSG("ECC_BAD_ARG_E");
return ECC_BAD_ARG_E; return ECC_BAD_ARG_E;
} }
/* Verify domain params supplied */ /* Verify domain params supplied */
if (wc_ecc_is_valid_idx(private_key->idx) == 0) if (wc_ecc_is_valid_idx(private_key->idx) == 0) {
WOLFSSL_MSG("wc_ecc_is_valid_idx failed");
return ECC_BAD_ARG_E; return ECC_BAD_ARG_E;
}
switch(private_key->state) { switch(private_key->state) {
case ECC_STATE_NONE: case ECC_STATE_NONE:
@@ -4114,6 +4122,8 @@ int wc_ecc_shared_secret_ex(ecc_key* private_key, ecc_point* point,
err = BAD_STATE_E; err = BAD_STATE_E;
} /* switch */ } /* switch */
WOLFSSL_LEAVE("wc_ecc_shared_secret_ex", err);
/* if async pending then return and skip done cleanup below */ /* if async pending then return and skip done cleanup below */
if (err == WC_PENDING_E) { if (err == WC_PENDING_E) {
private_key->state++; private_key->state++;

View File

@@ -6201,29 +6201,29 @@ int wolfSSL_EVP_PKEY_assign(WOLFSSL_EVP_PKEY *pkey, int type, void *key)
#if defined(HAVE_ECC) #if defined(HAVE_ECC)
/* try and populate public pkey_sz and pkey.ptr */ /* try and populate public pkey_sz and pkey.ptr */
static void ECC_populate_EVP_PKEY(EVP_PKEY* pkey, ecc_key* ecc) static int ECC_populate_EVP_PKEY(EVP_PKEY* pkey, ecc_key* ecc)
{ {
int ret; word32 derSz = 0;
if (!pkey || !ecc) if (!pkey || !ecc)
return; return WOLFSSL_FAILURE;
if ((ret = wc_EccPublicKeyDerSize(ecc, 1)) > 0) { if (wc_EccKeyToPKCS8(ecc, NULL, &derSz) == LENGTH_ONLY_E) {
int derSz = ret; byte* derBuf = (byte*)XMALLOC(derSz, NULL, DYNAMIC_TYPE_OPENSSL);
char* derBuf = (char*)XMALLOC(derSz, NULL, DYNAMIC_TYPE_TMP_BUFFER);
if (derBuf) { if (derBuf) {
ret = wc_EccPublicKeyToDer(ecc, (byte*)derBuf, derSz, 1); if (wc_EccKeyToPKCS8(ecc, derBuf, &derSz) >= 0) {
if (ret >= 0) {
if (pkey->pkey.ptr) { if (pkey->pkey.ptr) {
XFREE(pkey->pkey.ptr, NULL, DYNAMIC_TYPE_OPENSSL); XFREE(pkey->pkey.ptr, NULL, DYNAMIC_TYPE_OPENSSL);
} }
pkey->pkey_sz = ret; pkey->pkey_sz = (int)derSz;
pkey->pkey.ptr = derBuf; pkey->pkey.ptr = (char*)derBuf;
return WOLFSSL_SUCCESS;
} }
else { /* failure - okay to ignore */ else {
XFREE(derBuf, NULL, DYNAMIC_TYPE_TMP_BUFFER); XFREE(derBuf, NULL, DYNAMIC_TYPE_OPENSSL);
derBuf = NULL; derBuf = NULL;
} }
} }
} }
return WOLFSSL_FAILURE;
} }
WOLFSSL_API int wolfSSL_EVP_PKEY_set1_EC_KEY(WOLFSSL_EVP_PKEY *pkey, WOLFSSL_EC_KEY *key) WOLFSSL_API int wolfSSL_EVP_PKEY_set1_EC_KEY(WOLFSSL_EVP_PKEY *pkey, WOLFSSL_EC_KEY *key)
@@ -6255,8 +6255,7 @@ WOLFSSL_API int wolfSSL_EVP_PKEY_set1_EC_KEY(WOLFSSL_EVP_PKEY *pkey, WOLFSSL_EC_
pkey->ecc = key; pkey->ecc = key;
pkey->ownEcc = 0; /* pkey does not own EC key */ pkey->ownEcc = 0; /* pkey does not own EC key */
pkey->type = EVP_PKEY_EC; pkey->type = EVP_PKEY_EC;
ECC_populate_EVP_PKEY(pkey, (ecc_key*)key->internal); return ECC_populate_EVP_PKEY(pkey, (ecc_key*)key->internal);
return WOLFSSL_SUCCESS;
#else #else
(void)pkey; (void)pkey;
(void)key; (void)key;
@@ -6292,9 +6291,7 @@ int wolfSSL_EVP_PKEY_assign_EC_KEY(EVP_PKEY* pkey, WOLFSSL_EC_KEY* key)
pkey->ownEcc = 1; pkey->ownEcc = 1;
/* try and populate public pkey_sz and pkey.ptr */ /* try and populate public pkey_sz and pkey.ptr */
ECC_populate_EVP_PKEY(pkey, (ecc_key*)key->internal); return ECC_populate_EVP_PKEY(pkey, (ecc_key*)key->internal);
return WOLFSSL_SUCCESS;
} }
#endif /* HAVE_ECC */ #endif /* HAVE_ECC */

View File

@@ -517,6 +517,8 @@ WOLFSSL_API void wc_FreeDer(DerBuffer** pDer);
word32 inLen); word32 inLen);
WOLFSSL_API int wc_EccPrivateKeyToPKCS8(ecc_key* key, byte* output, WOLFSSL_API int wc_EccPrivateKeyToPKCS8(ecc_key* key, byte* output,
word32* outLen); word32* outLen);
WOLFSSL_API int wc_EccKeyToPKCS8(ecc_key* key, byte* output,
word32* outLen);
/* public key helper */ /* public key helper */
WOLFSSL_API int wc_EccPublicKeyDecode(const byte*, word32*, WOLFSSL_API int wc_EccPublicKeyDecode(const byte*, word32*,