@@ -7361,9 +7361,9 @@ static int check_cert_key_dev(word32 keyOID, byte* privKey, word32 privSz,
73617361 *
73627362 * Returns WOLFSSL_SUCCESS on good private key
73637363 * WOLFSSL_FAILURE if mismatched */
7364- static int check_cert_key(DerBuffer* cert, DerBuffer* key, DerBuffer* altKey ,
7365- void* heap, int devId, int isKeyLabel, int isKeyId, int altDevId ,
7366- int isAltKeyLabel, int isAltKeyId)
7364+ static int check_cert_key(const DerBuffer* cert, const DerBuffer* key ,
7365+ const DerBuffer* altKey, void* heap, int devId, int isKeyLabel, int isKeyId,
7366+ int altDevId, int isAltKeyLabel, int isAltKeyId)
73677367{
73687368 WC_DECLARE_VAR(der, DecodedCert, 1, 0);
73697369 word32 size;
@@ -7498,50 +7498,61 @@ static int check_cert_key(DerBuffer* cert, DerBuffer* key, DerBuffer* altKey,
74987498 * WOLFSSL_FAILURE if mismatched. */
74997499int wolfSSL_CTX_check_private_key(const WOLFSSL_CTX* ctx)
75007500{
7501- int res;
7501+ int res = WOLFSSL_SUCCESS;
7502+ #ifdef WOLFSSL_BLIND_PRIVATE_KEY
7503+ DerBuffer *privateKey;
7504+ #ifdef WOLFSSL_DUAL_ALG_CERTS
7505+ DerBuffer *altPrivateKey;
7506+ #endif
7507+ #else
7508+ const DerBuffer *privateKey;
7509+ #ifdef WOLFSSL_DUAL_ALG_CERTS
7510+ const DerBuffer *altPrivateKey;
7511+ #endif
7512+ #endif
75027513
75037514 if (ctx == NULL) {
75047515 return WOLFSSL_FAILURE;
75057516 }
75067517
75077518#ifdef WOLFSSL_DUAL_ALG_CERTS
75087519#ifdef WOLFSSL_BLIND_PRIVATE_KEY
7509- wolfssl_priv_der_unblind(ctx->privateKey, ctx->privateKeyMask);
7510- wolfssl_priv_der_unblind(ctx->altPrivateKey, ctx->altPrivateKeyMask);
7520+ privateKey = wolfssl_priv_der_unblind(ctx->privateKey, ctx->privateKeyMask);
7521+ altPrivateKey = wolfssl_priv_der_unblind(ctx->altPrivateKey,
7522+ ctx->altPrivateKeyMask);
7523+ if ((privateKey == NULL) || (altPrivateKey == NULL)) {
7524+ res = WOLFSSL_FAILURE;
7525+ }
7526+ #else
7527+ privateKey = ctx->privateKey;
7528+ altPrivateKey = ctx->altPrivateKey;
75117529#endif
7512- res = check_cert_key(ctx->certificate, ctx->privateKey, ctx->altPrivateKey,
7513- ctx->heap, ctx->privateKeyDevId, ctx->privateKeyLabel,
7514- ctx->privateKeyId, ctx->altPrivateKeyDevId, ctx->altPrivateKeyLabel,
7515- ctx->altPrivateKeyId) != 0;
7516- #ifdef WOLFSSL_BLIND_PRIVATE_KEY
7517- {
7518- int ret;
7519- ret = wolfssl_priv_der_blind(NULL, ctx->privateKey,
7520- (DerBuffer**)&ctx->privateKeyMask);
7521- if (ret == 0) {
7522- ret = wolfssl_priv_der_blind(NULL, ctx->altPrivateKey,
7523- (DerBuffer**)&ctx->altPrivateKeyMask);
7524- }
7525- if (ret != 0) {
7526- res = WOLFSSL_FAILURE;
7527- }
7530+ if (res == WOLFSSL_SUCCESS) {
7531+ res = check_cert_key(ctx->certificate, privateKey, altPrivateKey,
7532+ ctx->heap, ctx->privateKeyDevId, ctx->privateKeyLabel,
7533+ ctx->privateKeyId, ctx->altPrivateKeyDevId,
7534+ ctx->altPrivateKeyLabel, ctx->altPrivateKeyId) != 0;
75287535 }
7536+ #ifdef WOLFSSL_BLIND_PRIVATE_KEY
7537+ wolfssl_priv_der_unblind_free(privateKey);
7538+ wolfssl_priv_der_unblind_free(altPrivateKey);
75297539#endif
75307540#else
75317541#ifdef WOLFSSL_BLIND_PRIVATE_KEY
7532- wolfssl_priv_der_unblind(ctx->privateKey, ctx->privateKeyMask);
7542+ privateKey = wolfssl_priv_der_unblind(ctx->privateKey, ctx->privateKeyMask);
7543+ if (privateKey == NULL) {
7544+ res = WOLFSSL_FAILURE;
7545+ }
7546+ #else
7547+ privateKey = ctx->privateKey;
75337548#endif
7534- res = check_cert_key(ctx->certificate, ctx->privateKey, NULL, ctx->heap,
7535- ctx->privateKeyDevId, ctx->privateKeyLabel, ctx->privateKeyId,
7536- INVALID_DEVID, 0, 0);
7537- #ifdef WOLFSSL_BLIND_PRIVATE_KEY
7538- {
7539- int ret = wolfssl_priv_der_blind(NULL, ctx->privateKey,
7540- (DerBuffer**)&ctx->privateKeyMask);
7541- if (ret != 0) {
7542- res = WOLFSSL_FAILURE;
7543- }
7549+ if (res == WOLFSSL_SUCCESS) {
7550+ res = check_cert_key(ctx->certificate, privateKey, NULL, ctx->heap,
7551+ ctx->privateKeyDevId, ctx->privateKeyLabel,
7552+ ctx->privateKeyId, INVALID_DEVID, 0, 0);
75447553 }
7554+ #ifdef WOLFSSL_BLIND_PRIVATE_KEY
7555+ wolfssl_priv_der_unblind_free(privateKey);
75457556#endif
75467557#endif
75477558
@@ -7558,8 +7569,12 @@ int wolfSSL_CTX_check_private_key(const WOLFSSL_CTX* ctx)
75587569/**
75597570 * Return the private key of the WOLFSSL_CTX struct
75607571 * @return WOLFSSL_EVP_PKEY* The caller doesn *NOT*` free the returned object.
7572+ *
7573+ * Note, even though the supplied ctx pointer is designated const, on success
7574+ * ctx->privateKeyPKey is changed by this call. The change is done safely using
7575+ * a hardware-synchronized store.
75617576 */
7562- WOLFSSL_EVP_PKEY* wolfSSL_CTX_get0_privatekey(WOLFSSL_CTX* ctx)
7577+ WOLFSSL_EVP_PKEY* wolfSSL_CTX_get0_privatekey(const WOLFSSL_CTX* ctx)
75637578{
75647579 WOLFSSL_EVP_PKEY* res;
75657580 const unsigned char *key;
@@ -7596,19 +7611,23 @@ WOLFSSL_EVP_PKEY* wolfSSL_CTX_get0_privatekey(WOLFSSL_CTX* ctx)
75967611 return NULL;
75977612 }
75987613
7599- key = ctx->privateKey->buffer;
7600-
76017614 if (ctx->privateKeyPKey != NULL) {
76027615 res = ctx->privateKeyPKey;
76037616 }
76047617 else {
76057618 #ifdef WOLFSSL_BLIND_PRIVATE_KEY
7606- wolfssl_priv_der_unblind(ctx->privateKey, ctx->privateKeyMask);
7619+ DerBuffer *unblinded_privateKey =
7620+ wolfssl_priv_der_unblind(ctx->privateKey, ctx->privateKeyMask);
7621+ if (unblinded_privateKey == NULL)
7622+ return NULL;
7623+ key = unblinded_privateKey->buffer;
7624+ #else
7625+ key = ctx->privateKey->buffer;
76077626 #endif
76087627 res = wolfSSL_d2i_PrivateKey(type, NULL, &key,
76097628 (long)ctx->privateKey->length);
76107629 #ifdef WOLFSSL_BLIND_PRIVATE_KEY
7611- wolfssl_priv_der_unblind(ctx->privateKey, ctx->privateKeyMask );
7630+ wolfssl_priv_der_unblind_free(unblinded_privateKey );
76127631 #endif
76137632 if (res) {
76147633#ifdef WOLFSSL_ATOMIC_OPS
@@ -7621,7 +7640,7 @@ WOLFSSL_EVP_PKEY* wolfSSL_CTX_get0_privatekey(WOLFSSL_CTX* ctx)
76217640 res = current_pkey;
76227641 }
76237642#else
7624- ctx->privateKeyPKey = res;
7643+ ((WOLFSSL_CTX *) ctx) ->privateKeyPKey = res;
76257644#endif
76267645 }
76277646 }
@@ -8874,7 +8893,7 @@ int wolfSSL_check_private_key(const WOLFSSL* ssl)
88748893#endif
88758894#else
88768895#ifdef WOLFSSL_BLIND_PRIVATE_KEY
8877- wolfssl_priv_der_unblind (ssl->buffers.key, ssl->buffers.keyMask);
8896+ wolfssl_priv_der_blind_toggle (ssl->buffers.key, ssl->buffers.keyMask);
88788897#endif
88798898 res = check_cert_key(ssl->buffers.certificate, ssl->buffers.key, NULL,
88808899 ssl->heap, ssl->buffers.keyDevId, ssl->buffers.keyLabel,
@@ -20971,7 +20990,7 @@ WOLFSSL_CTX* wolfSSL_set_SSL_CTX(WOLFSSL* ssl, WOLFSSL_CTX* ctx)
2097120990 return NULL;
2097220991 }
2097320992 /* Blind the private key for the SSL with new random mask. */
20974- wolfssl_priv_der_unblind (ssl->buffers.key, ctx->privateKeyMask);
20993+ wolfssl_priv_der_blind_toggle (ssl->buffers.key, ctx->privateKeyMask);
2097520994 ret = wolfssl_priv_der_blind(ssl->rng, ssl->buffers.key,
2097620995 &ssl->buffers.keyMask);
2097720996 if (ret != 0) {
0 commit comments