Skip to content

Commit f073d83

Browse files
committed
Fix memory leak in SignatureVerifier_OpenSSL when signature is invalid
SignatureVerifier_OpenSSL contained a memory leak when it was attempting to verify an invalid signature. This should never occur in normal operation.
1 parent ce10522 commit f073d83

File tree

1 file changed

+11
-8
lines changed

1 file changed

+11
-8
lines changed

src/SignatureVerifier_OpenSSL.cpp

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -38,33 +38,36 @@ verify(basic_Error & e, RSA * rsa, std::string const& message, std::string const
3838
if (e) { return; }
3939

4040
int r;
41+
EVP_MD_CTX * ctx = NULL;
42+
EVP_PKEY * pkey = NULL;
4143

42-
if (rsa == NULL) { e.set(api, Subsystem::SignatureVerifier, RSA_NULL); return; }
44+
if (rsa == NULL) { e.set(api, Subsystem::SignatureVerifier, RSA_NULL); goto end; }
4345

4446
#if OPENSSL_VERSION_NUMBER < 0x10100000L
4547
ctx = EVP_MD_CTX_create();
4648
#else
4749
ctx = EVP_MD_CTX_new();
4850
#endif
49-
if (ctx == NULL) { e.set(api, Subsystem::SignatureVerifier, CTX_CREATE_FAILED); return; }
51+
if (ctx == NULL) { e.set(api, Subsystem::SignatureVerifier, CTX_CREATE_FAILED); goto end; }
5052

51-
EVP_PKEY * pkey = EVP_PKEY_new();
52-
if (pkey == NULL) { e.set(api, Subsystem::SignatureVerifier, PKEY_NEW_FAILED); return; }
53+
pkey = EVP_PKEY_new();
54+
if (pkey == NULL) { e.set(api, Subsystem::SignatureVerifier, PKEY_NEW_FAILED); goto end; }
5355

5456
r = EVP_PKEY_set1_RSA(pkey, rsa);
55-
if (r != 1) { e.set(api, Subsystem::SignatureVerifier, PKEY_SET1_RSA_FAILED); return; }
57+
if (r != 1) { e.set(api, Subsystem::SignatureVerifier, PKEY_SET1_RSA_FAILED); goto end; }
5658

5759

5860

5961
r = EVP_DigestVerifyInit(ctx, NULL, EVP_sha256(), NULL, pkey);
60-
if (r != 1) { e.set(api, Subsystem::SignatureVerifier, DIGEST_VERIFY_INIT_FAILED); return; }
62+
if (r != 1) { e.set(api, Subsystem::SignatureVerifier, DIGEST_VERIFY_INIT_FAILED); goto end; }
6163

6264
r = EVP_DigestVerifyUpdate(ctx, (unsigned char*)message.c_str(), message.size());
63-
if (r != 1) { e.set(api, Subsystem::SignatureVerifier, DIGEST_VERIFY_UPDATE_FAILED); return; }
65+
if (r != 1) { e.set(api, Subsystem::SignatureVerifier, DIGEST_VERIFY_UPDATE_FAILED); goto end; }
6466

6567
r = EVP_DigestVerifyFinal(ctx, (unsigned char*)sig.c_str(), sig.size());
66-
if (r != 1) { e.set(api, Subsystem::SignatureVerifier, DIGEST_VERIFY_FINAL_FAILED); return; }
68+
if (r != 1) { e.set(api, Subsystem::SignatureVerifier, DIGEST_VERIFY_FINAL_FAILED); goto end; }
6769

70+
end:
6871
// Void return type
6972
EVP_PKEY_free(pkey);
7073

0 commit comments

Comments
 (0)