Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .github/workflows/ocsp.yml
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,8 @@ jobs:
# reused by every later connection on it, with the CTX owning it. None of the
# jobs above reach that cache: it is only populated when the SSL shares the
# CTX certificate buffer (ssl->buffers.weOwnCert == 0), and OPENSSL_ALL
# implies WOLFSSL_COPY_CERT, which gives every SSL its own copy instead.
# implies WOLFSSL_COPY_CERT, which gives every SSL its own reference to, or
# copy of, it instead.
ocsp_ctx_request_cache:
name: ocsp ctx request cache (${{ matrix.name }})
if: ${{ (github.repository_owner == 'wolfssl') && (github.event_name != 'pull_request' || github.event.pull_request.draft == false) }}
Expand Down
290 changes: 191 additions & 99 deletions src/internal.c

Large diffs are not rendered by default.

105 changes: 9 additions & 96 deletions src/ssl.c
Original file line number Diff line number Diff line change
Expand Up @@ -225,8 +225,10 @@ static struct SystemCryptoPolicy crypto_policy;
* WOLFSSL_CALLBACKS: Handshake monitoring callbacks default: off
* NO_HANDSHAKE_DONE_CB: Disable handshake completion callback default: off
* WOLFSSL_SHUTDOWNONCE: Send close_notify only once default: off
* WOLFSSL_COPY_CERT: Copy certificate buffer (own copy) default: off
* WOLFSSL_COPY_KEY: Copy private key buffer (own copy) default: off
* WOLFSSL_COPY_CERT: SSL keeps CTX cert if CTX replaces it default: off
* (on with OPENSSL_ALL)
* WOLFSSL_COPY_KEY: SSL keeps CTX key if CTX replaces it default: off
* (on with OPENSSL_ALL)
* WOLF_PRIVATE_KEY_ID: Reference private keys by ID default: off
* WOLFSSL_REFCNT_ERROR_RETURN:
* Return errors on ref counting failures default: off
Expand Down Expand Up @@ -8643,7 +8645,7 @@ void wolfSSL_certs_clear(WOLFSSL* ssl)
}
#endif
if (ssl->buffers.weOwnKey) {
FreeDer(&ssl->buffers.key);
FreeKeyDer(&ssl->buffers.key);
#ifdef WOLFSSL_BLIND_PRIVATE_KEY
FreeDer(&ssl->buffers.keyMask);
#endif
Expand All @@ -8660,7 +8662,7 @@ void wolfSSL_certs_clear(WOLFSSL* ssl)
ssl->buffers.keyDevId = 0;
#ifdef WOLFSSL_DUAL_ALG_CERTS
if (ssl->buffers.weOwnAltKey) {
FreeDer(&ssl->buffers.altKey);
FreeKeyDer(&ssl->buffers.altKey);
#ifdef WOLFSSL_BLIND_PRIVATE_KEY
FreeDer(&ssl->buffers.altKeyMask);
#endif
Expand Down Expand Up @@ -9070,74 +9072,10 @@ WOLFSSL_CTX* wolfSSL_set_SSL_CTX(WOLFSSL* ssl, WOLFSSL_CTX* ctx)
/* Release the certificate, chain and keys this SSL owns so that a ctx
* without them leaves none of the old ones behind. */
wolfSSL_certs_clear(ssl);
#ifdef WOLFSSL_COPY_CERT
/* If WOLFSSL_COPY_CERT defined, always make new copy of cert from ctx */
if (ctx->certificate != NULL) {
ret = AllocCopyDer(&ssl->buffers.certificate, ctx->certificate->buffer,
ctx->certificate->length, ctx->certificate->type,
ctx->certificate->heap);
if (ret != 0) {
return NULL;
}

ssl->buffers.weOwnCert = 1;
}
if (ctx->certChain != NULL) {
ret = AllocCopyDer(&ssl->buffers.certChain, ctx->certChain->buffer,
ctx->certChain->length, ctx->certChain->type,
ctx->certChain->heap);
if (ret != 0) {
return NULL;
}

ssl->buffers.weOwnCertChain = 1;
}
#else
/* ctx owns certificate, certChain and key */
ssl->buffers.certificate = ctx->certificate;
ssl->buffers.certChain = ctx->certChain;
#endif
ssl->buffers.certChainCnt = ctx->certChainCnt;
#ifndef WOLFSSL_BLIND_PRIVATE_KEY
#ifdef WOLFSSL_COPY_KEY
if (ctx->privateKey != NULL) {
ret = AllocCopyDer(&ssl->buffers.key, ctx->privateKey->buffer,
ctx->privateKey->length, ctx->privateKey->type,
ctx->privateKey->heap);
if (ret != 0) {
return NULL;
}
ssl->buffers.weOwnKey = 1;
}
else {
ssl->buffers.key = ctx->privateKey;
}
#else
ssl->buffers.key = ctx->privateKey;
#endif
#else
if (ctx->privateKey != NULL) {
ret = AllocCopyDer(&ssl->buffers.key, ctx->privateKey->buffer,
ctx->privateKey->length, ctx->privateKey->type,
ctx->privateKey->heap);
if (ret != 0) {
return NULL;
}
ssl->buffers.weOwnKey = 1;
/* Blind the private key for the SSL with new random mask. */
wolfssl_priv_der_blind_toggle(ssl->buffers.key, ctx->privateKeyMask);
ret = wolfssl_priv_der_blind(ssl->rng, ssl->buffers.key,
&ssl->buffers.keyMask);
if (ret != 0) {
return NULL;
}
/* Same certificate, chain and keys as an SSL made from this ctx. */
if (SetSSL_CTX_CertsAndKeys(ssl, ctx) != 0) {
return NULL;
}
#endif
ssl->buffers.keyType = ctx->privateKeyType;
ssl->buffers.keyId = ctx->privateKeyId;
ssl->buffers.keyLabel = ctx->privateKeyLabel;
ssl->buffers.keySz = ctx->privateKeySz;
ssl->buffers.keyDevId = ctx->privateKeyDevId;
/* flags indicating what certs/keys are available */
ssl->options.haveRSA = ctx->haveRSA;
ssl->options.haveDH = ctx->haveDH;
Expand All @@ -9147,31 +9085,6 @@ WOLFSSL_CTX* wolfSSL_set_SSL_CTX(WOLFSSL* ssl, WOLFSSL_CTX* ctx)
ssl->options.haveFalconSig = ctx->haveFalconSig;
ssl->options.haveMlDsaSig = ctx->haveMlDsaSig;
ssl->options.haveSlhDsaSig = ctx->haveSlhDsaSig;
#ifdef WOLFSSL_DUAL_ALG_CERTS
#ifndef WOLFSSL_BLIND_PRIVATE_KEY
ssl->buffers.altKey = ctx->altPrivateKey;
#else
if (ctx->altPrivateKey != NULL) {
ret = AllocCopyDer(&ssl->buffers.altKey, ctx->altPrivateKey->buffer,
ctx->altPrivateKey->length, ctx->altPrivateKey->type,
ctx->altPrivateKey->heap);
if (ret != 0) {
return NULL;
}
ssl->buffers.weOwnAltKey = 1;
/* Blind the private key for the SSL with new random mask. */
wolfssl_priv_der_blind_toggle(ssl->buffers.altKey,
ctx->altPrivateKeyMask);
ret = wolfssl_priv_der_blind(ssl->rng, ssl->buffers.altKey,
&ssl->buffers.altKeyMask);
if (ret != 0) {
return NULL;
}
}
#endif
ssl->buffers.altKeySz = ctx->altPrivateKeySz;
ssl->buffers.altKeyType = ctx->altPrivateKeyType;
#endif /* WOLFSSL_DUAL_ALG_CERTS */
#endif

#ifdef WOLFSSL_SESSION_ID_CTX
Expand Down
23 changes: 10 additions & 13 deletions src/ssl_api_cert.c
Original file line number Diff line number Diff line change
Expand Up @@ -1126,13 +1126,12 @@ int wolfSSL_UnloadCertsKeys(WOLFSSL* ssl)
ssl->buffers.weOwnCertChain = 0;
}

/* FreeKeyDer() zeroes the key when the last holder lets go of it. It
* can be shared with the CTX (WOLFSSL_DER_REFCOUNT), so it must not
* be zeroed here. */
if (ssl->buffers.weOwnKey) {
WOLFSSL_MSG("Unloading key");
if ((ssl->buffers.key != NULL) &&
(ssl->buffers.key->buffer != NULL)) {
ForceZero(ssl->buffers.key->buffer, ssl->buffers.key->length);
}
FreeDer(&ssl->buffers.key);
FreeKeyDer(&ssl->buffers.key);
#ifdef WOLFSSL_BLIND_PRIVATE_KEY
FreeDer(&ssl->buffers.keyMask);
#endif
Expand All @@ -1142,12 +1141,7 @@ int wolfSSL_UnloadCertsKeys(WOLFSSL* ssl)
#ifdef WOLFSSL_DUAL_ALG_CERTS
if (ssl->buffers.weOwnAltKey) {
WOLFSSL_MSG("Unloading alt key");
if ((ssl->buffers.altKey != NULL) &&
(ssl->buffers.altKey->buffer != NULL)) {
ForceZero(ssl->buffers.altKey->buffer,
ssl->buffers.altKey->length);
}
FreeDer(&ssl->buffers.altKey);
FreeKeyDer(&ssl->buffers.altKey);
#ifdef WOLFSSL_BLIND_PRIVATE_KEY
FreeDer(&ssl->buffers.altKeyMask);
#endif
Expand Down Expand Up @@ -2114,10 +2108,13 @@ WOLFSSL_X509* wolfSSL_get_certificate(WOLFSSL* ssl)
* applications (like nginx OCSP stapling) that use the X509 pointer
* from SSL_CTX_use_certificate as a lookup key. */
if ((ssl->ctx != NULL) && (ssl->ctx->ourCert != NULL)) {
/* Compare cert buffers to make sure they are the same */
/* Compare cert buffers to make sure they are the same. The
* SSL may hold the CTX's own buffer (WOLFSSL_DER_REFCOUNT). */
if ((ssl->buffers.certificate == NULL) ||
(ssl->buffers.certificate->buffer == NULL) ||
((ssl->buffers.certificate->length ==
(ssl->buffers.certificate == ssl->ctx->certificate) ||
((ssl->ctx->certificate != NULL) &&
(ssl->buffers.certificate->length ==
ssl->ctx->certificate->length) &&
(XMEMCMP(ssl->buffers.certificate->buffer,
ssl->ctx->certificate->buffer,
Expand Down
28 changes: 16 additions & 12 deletions src/ssl_load.c
Original file line number Diff line number Diff line change
Expand Up @@ -1426,7 +1426,7 @@ static int ProcessBufferPrivKeyHandleDer(WOLFSSL_CTX* ctx, WOLFSSL* ssl,
if (ssl != NULL) {
/* Dispose of previous key if not context's. */
if (ssl->buffers.weOwnAltKey) {
FreeDer(&ssl->buffers.altKey);
FreeKeyDer(&ssl->buffers.altKey);
#ifdef WOLFSSL_BLIND_PRIVATE_KEY
FreeDer(&ssl->buffers.altKeyMask);
#endif
Expand All @@ -1443,7 +1443,7 @@ static int ProcessBufferPrivKeyHandleDer(WOLFSSL_CTX* ctx, WOLFSSL* ssl,
}
else if (ctx != NULL) {
/* Dispose of previous key. */
FreeDer(&ctx->altPrivateKey);
FreeKeyDer(&ctx->altPrivateKey);
ctx->altPrivateKeyId = 0;
ctx->altPrivateKeyLabel = 0;
ctx->altPrivateKeyDevId = INVALID_DEVID;
Expand All @@ -1459,7 +1459,7 @@ static int ProcessBufferPrivKeyHandleDer(WOLFSSL_CTX* ctx, WOLFSSL* ssl,
if (ssl != NULL) {
/* Dispose of previous key if not context's. */
if (ssl->buffers.weOwnKey) {
FreeDer(&ssl->buffers.key);
FreeKeyDer(&ssl->buffers.key);
#ifdef WOLFSSL_BLIND_PRIVATE_KEY
FreeDer(&ssl->buffers.keyMask);
#endif
Expand All @@ -1476,7 +1476,7 @@ static int ProcessBufferPrivKeyHandleDer(WOLFSSL_CTX* ctx, WOLFSSL* ssl,
}
else if (ctx != NULL) {
/* Dispose of previous key. */
FreeDer(&ctx->privateKey);
FreeKeyDer(&ctx->privateKey);
ctx->privateKeyId = 0;
ctx->privateKeyLabel = 0;
ctx->privateKeyDevId = INVALID_DEVID;
Expand Down Expand Up @@ -2535,6 +2535,8 @@ static int ProcessBufferCertHandleDer(WOLFSSL_CTX* ctx, WOLFSSL* ssl,
/* Store certificate as ours. */
ctx->certificate = der;
/* ourCert is created on demand. */
/* The cached OCSP request was built from the old certificate. */
FreeCtxOcspRequestCache(ctx);
}
}
else {
Expand Down Expand Up @@ -4576,7 +4578,7 @@ int wolfSSL_CTX_use_PrivateKey_Id(WOLFSSL_CTX* ctx, const unsigned char* id,
}

/* Dispose of old private key and allocate and copy in id. */
FreeDer(&ctx->privateKey);
FreeKeyDer(&ctx->privateKey);
if (AllocCopyDer(&ctx->privateKey, id, (word32)sz, PRIVATEKEY_TYPE,
ctx->heap) != 0) {
ret = 0;
Expand Down Expand Up @@ -4654,7 +4656,7 @@ int wolfSSL_CTX_use_PrivateKey_Label(WOLFSSL_CTX* ctx, const char* label,
sz = (word32)XSTRLEN(label) + 1;

/* Dispose of old private key and allocate and copy in label. */
FreeDer(&ctx->privateKey);
FreeKeyDer(&ctx->privateKey);
if (AllocCopyDer(&ctx->privateKey, (const byte*)label, (word32)sz,
PRIVATEKEY_TYPE, ctx->heap) != 0) {
ret = 0;
Expand Down Expand Up @@ -4695,7 +4697,7 @@ int wolfSSL_CTX_use_AltPrivateKey_Id(WOLFSSL_CTX* ctx, const unsigned char* id,
}

if (ret == 1) {
FreeDer(&ctx->altPrivateKey);
FreeKeyDer(&ctx->altPrivateKey);
if (AllocDer(&ctx->altPrivateKey, (word32)sz, ALT_PRIVATEKEY_TYPE,
ctx->heap) != 0) {
ret = 0;
Expand Down Expand Up @@ -4746,7 +4748,7 @@ int wolfSSL_CTX_use_AltPrivateKey_Label(WOLFSSL_CTX* ctx, const char* label,

if (ret == 1) {
sz = (word32)XSTRLEN(label) + 1;
FreeDer(&ctx->altPrivateKey);
FreeKeyDer(&ctx->altPrivateKey);
if (AllocDer(&ctx->altPrivateKey, (word32)sz, ALT_PRIVATEKEY_TYPE,
ctx->heap) != 0) {
ret = 0;
Expand Down Expand Up @@ -4989,7 +4991,7 @@ int wolfSSL_use_PrivateKey_Id(WOLFSSL* ssl, const unsigned char* id,

/* Dispose of old private key if owned and allocate and copy in id. */
if (ssl->buffers.weOwnKey) {
FreeDer(&ssl->buffers.key);
FreeKeyDer(&ssl->buffers.key);
#ifdef WOLFSSL_BLIND_PRIVATE_KEY
FreeDer(&ssl->buffers.keyMask);
#endif
Expand Down Expand Up @@ -5065,7 +5067,7 @@ int wolfSSL_use_PrivateKey_Label(WOLFSSL* ssl, const char* label, int devId)

/* Dispose of old private key if owned and allocate and copy in label. */
if (ssl->buffers.weOwnKey) {
FreeDer(&ssl->buffers.key);
FreeKeyDer(&ssl->buffers.key);
#ifdef WOLFSSL_BLIND_PRIVATE_KEY
FreeDer(&ssl->buffers.keyMask);
#endif
Expand Down Expand Up @@ -5110,7 +5112,7 @@ int wolfSSL_use_AltPrivateKey_Id(WOLFSSL* ssl, const unsigned char* id, long sz,

if (ret == 1) {
if (ssl->buffers.weOwnAltKey) {
FreeDer(&ssl->buffers.altKey);
FreeKeyDer(&ssl->buffers.altKey);
#ifdef WOLFSSL_BLIND_PRIVATE_KEY
FreeDer(&ssl->buffers.altKeyMask);
#endif
Expand Down Expand Up @@ -5158,7 +5160,7 @@ int wolfSSL_use_AltPrivateKey_Label(WOLFSSL* ssl, const char* label, int devId)
if (ret == 1) {
sz = (word32)XSTRLEN(label) + 1;
if (ssl->buffers.weOwnAltKey) {
FreeDer(&ssl->buffers.altKey);
FreeKeyDer(&ssl->buffers.altKey);
#ifdef WOLFSSL_BLIND_PRIVATE_KEY
FreeDer(&ssl->buffers.altKeyMask);
#endif
Expand Down Expand Up @@ -5413,6 +5415,8 @@ int wolfSSL_CTX_use_certificate(WOLFSSL_CTX *ctx, WOLFSSL_X509 *x)
if (res == 1) {
/* Replace certificate buffer with one holding the new certificate. */
FreeDer(&ctx->certificate);
/* The cached OCSP request was built from the old certificate. */
FreeCtxOcspRequestCache(ctx);
ret = AllocCopyDer(&ctx->certificate, x->derCert->buffer,
x->derCert->length, CERT_TYPE, ctx->heap);
if (ret != 0) {
Expand Down
13 changes: 5 additions & 8 deletions src/tls13.c
Original file line number Diff line number Diff line change
Expand Up @@ -10511,7 +10511,6 @@ static int SetupOcspResp(WOLFSSL* ssl)
TLSX* extension = NULL;
int ret = 0;
OcspRequest* request = NULL;
byte ctxOwnsRequest = 0;

extension = TLSX_Find(ssl->extensions, TLSX_STATUS_REQUEST);
#ifdef WOLFSSL_POST_HANDSHAKE_AUTH
Expand Down Expand Up @@ -10589,12 +10588,10 @@ static int SetupOcspResp(WOLFSSL* ssl)
}
}
request = &csr->request.ocsp[0];
ret = CreateOcspResponse(ssl, &request, &csr->responses[0],
&ctxOwnsRequest);
/* Only a successful call replaces "request", and only a request the CTX did
* not take ownership of is ours to free. Both are checked, matching the
* SendCertificateStatus() callers. */
if (ret == 0 && request != &csr->request.ocsp[0] && !ctxOwnsRequest) {
ret = CreateOcspResponse(ssl, &request, &csr->responses[0]);
/* Only a successful call replaces "request", with one it built for us to
* free. */
if (ret == 0 && request != &csr->request.ocsp[0]) {
/* request was allocated in CreateOcspResponse() */
FreeOcspRequest(request);
XFREE(request, ssl->heap, DYNAMIC_TYPE_OCSP_REQUEST);
Expand Down Expand Up @@ -11460,7 +11457,7 @@ static int SendTls13CertificateVerify(WOLFSSL* ssl)
ssl->buffers.keySz = ssl->buffers.altKeySz;
/* If we own it, free key before overriding it. */
if (ssl->buffers.weOwnKey) {
FreeDer(&ssl->buffers.key);
FreeKeyDer(&ssl->buffers.key);
#ifdef WOLFSSL_BLIND_PRIVATE_KEY
FreeDer(&ssl->buffers.keyMask);
#endif
Expand Down
Loading
Loading