Skip to content

Commit 17a64b1

Browse files
committed
Fix GH-12987: openssl_csr_sign might leak new cert on error
Closes GH-12988
1 parent c727f29 commit 17a64b1

File tree

1 file changed

+5
-3
lines changed

1 file changed

+5
-3
lines changed

ext/openssl/openssl.c

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3154,6 +3154,7 @@ PHP_FUNCTION(openssl_csr_sign)
31543154
X509 *cert = NULL, *new_cert = NULL;
31553155
EVP_PKEY * key = NULL, *priv_key = NULL;
31563156
int i;
3157+
bool new_cert_used = false;
31573158
struct php_x509_request req;
31583159

31593160
ZEND_PARSE_PARAMETERS_START(4, 6)
@@ -3275,11 +3276,12 @@ PHP_FUNCTION(openssl_csr_sign)
32753276
object_init_ex(return_value, php_openssl_certificate_ce);
32763277
cert_object = Z_OPENSSL_CERTIFICATE_P(return_value);
32773278
cert_object->x509 = new_cert;
3279+
new_cert_used = true;
32783280

32793281
cleanup:
32803282

3281-
if (cert == new_cert) {
3282-
cert = NULL;
3283+
if (!new_cert_used && new_cert) {
3284+
X509_free(new_cert);
32833285
}
32843286

32853287
PHP_SSL_REQ_DISPOSE(&req);
@@ -3288,7 +3290,7 @@ PHP_FUNCTION(openssl_csr_sign)
32883290
if (csr_str) {
32893291
X509_REQ_free(csr);
32903292
}
3291-
if (cert_str && cert) {
3293+
if (cert_str && cert && cert != new_cert) {
32923294
X509_free(cert);
32933295
}
32943296
}

0 commit comments

Comments
 (0)