Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix for heap access violation #513

Merged
merged 5 commits into from
Aug 30, 2022
Merged
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
13 changes: 12 additions & 1 deletion source/windows/secure_channel_tls_handler.c
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ struct secure_channel_ctx {
HCRYPTPROV crypto_provider;
HCRYPTKEY private_key;
bool verify_peer;
bool should_free_pcerts;
};

struct secure_channel_handler {
Expand Down Expand Up @@ -1841,7 +1842,15 @@ static void s_secure_channel_ctx_destroy(struct secure_channel_ctx *secure_chann
}

if (secure_channel_ctx->pcerts) {
CertFreeCertificateContext(secure_channel_ctx->pcerts);
/**
* Only free the private certificate context if the private key is NOT
* from the certificate context because freeing the private key
* using CryptDestroyKey frees the certificate context and then
* trying to access it leads to a access violation.
*/
if (secure_channel_ctx->should_free_pcerts == true) {
CertFreeCertificateContext(secure_channel_ctx->pcerts);
}
}

if (secure_channel_ctx->cert_store) {
Expand Down Expand Up @@ -1887,6 +1896,7 @@ struct aws_tls_ctx *s_ctx_new(

secure_channel_ctx->verify_peer = options->verify_peer;
secure_channel_ctx->credentials.dwVersion = SCHANNEL_CRED_VERSION;
secure_channel_ctx->should_free_pcerts = true;

secure_channel_ctx->credentials.grbitEnabledProtocols = 0;

Expand Down Expand Up @@ -2015,6 +2025,7 @@ struct aws_tls_ctx *s_ctx_new(

secure_channel_ctx->credentials.paCred = &secure_channel_ctx->pcerts;
secure_channel_ctx->credentials.cCreds = 1;
secure_channel_ctx->should_free_pcerts = false;
}

return &secure_channel_ctx->ctx;
Expand Down