summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNikita Popov <nikita.ppv@gmail.com>2019-06-21 14:17:05 +0200
committerNikita Popov <nikita.ppv@gmail.com>2019-06-21 15:07:10 +0200
commite0bafc6da440f5b028453ca19d012d6a02234fc8 (patch)
treef83e87e2c58865960dd037fe0efad87449c6c329
parent90cb3743be293aab801a78196ad038c039786c19 (diff)
downloadphp-git-e0bafc6da440f5b028453ca19d012d6a02234fc8.tar.gz
Fix CSR leaks in openssl
-rw-r--r--ext/openssl/openssl.c29
1 files changed, 20 insertions, 9 deletions
diff --git a/ext/openssl/openssl.c b/ext/openssl/openssl.c
index 91df229c3d..d266220d3e 100644
--- a/ext/openssl/openssl.c
+++ b/ext/openssl/openssl.c
@@ -3619,7 +3619,10 @@ PHP_FUNCTION(openssl_csr_get_subject)
array_init(return_value);
php_openssl_add_assoc_name_entry(return_value, NULL, subject, use_shortnames);
- return;
+
+ if (!csr_resource) {
+ X509_REQ_free(csr);
+ }
}
/* }}} */
@@ -3631,16 +3634,16 @@ PHP_FUNCTION(openssl_csr_get_public_key)
zend_bool use_shortnames = 1;
zend_resource *csr_resource;
- X509_REQ * csr;
+ X509_REQ *orig_csr, *csr;
EVP_PKEY *tpubkey;
if (zend_parse_parameters(ZEND_NUM_ARGS(), "z|b", &zcsr, &use_shortnames) == FAILURE) {
return;
}
- csr = php_openssl_csr_from_zval(zcsr, 0, &csr_resource);
+ orig_csr = php_openssl_csr_from_zval(zcsr, 0, &csr_resource);
- if (csr == NULL) {
+ if (orig_csr == NULL) {
RETURN_FALSE;
}
@@ -3650,15 +3653,23 @@ PHP_FUNCTION(openssl_csr_get_public_key)
* a private key, it will be returned including the private part.
* If we duplicate it, then we get just the public part which is
* the same behavior as for OpenSSL 1.0 */
- csr = X509_REQ_dup(csr);
+ csr = X509_REQ_dup(orig_csr);
+#else
+ csr = orig_csr;
#endif
+
/* Retrieve the public key from the CSR */
tpubkey = X509_REQ_get_pubkey(csr);
-#if PHP_OPENSSL_API_VERSION >= 0x10100
- /* We need to free the CSR as it was duplicated */
- X509_REQ_free(csr);
-#endif
+ if (csr != orig_csr) {
+ /* We need to free the duplicated CSR */
+ X509_REQ_free(csr);
+ }
+
+ if (!csr_resource) {
+ /* We also need to free the original CSR if it was freshly created */
+ X509_REQ_free(orig_csr);
+ }
if (tpubkey == NULL) {
php_openssl_store_errors();