diff --git a/NEWS b/NEWS index 32d4375d545..d79d0e4fe22 100644 --- a/NEWS +++ b/NEWS @@ -12,6 +12,10 @@ PHP NEWS - LibXML: . Fixed bug GH-12702 (libxml2 2.12.0 issue building from src). (nono303) +- OpenSSL: + . Fixed bug #50713 (openssl_pkcs7_verify() may ignore untrusted CAs). + (Jakub Zelenka) + - PCRE: . Fixed bug GH-12628 (The gh11374 test fails on Alpinelinux). (nielsdos) diff --git a/ext/openssl/openssl.c b/ext/openssl/openssl.c index 31baa2d0e02..a6a05fe03db 100644 --- a/ext/openssl/openssl.c +++ b/ext/openssl/openssl.c @@ -5382,7 +5382,7 @@ PHP_FUNCTION(openssl_pkcs7_verify) signersfilename, signersfilename_len, 3, PHP_OPENSSL_BIO_MODE_W(PKCS7_BINARY)); if (certout) { int i; - signers = PKCS7_get0_signers(p7, NULL, (int)flags); + signers = PKCS7_get0_signers(p7, others, (int)flags); if (signers != NULL) { for (i = 0; i < sk_X509_num(signers); i++) { diff --git a/ext/openssl/tests/CertificateGenerator.inc b/ext/openssl/tests/CertificateGenerator.inc index c36718cd548..12764c8b63d 100644 --- a/ext/openssl/tests/CertificateGenerator.inc +++ b/ext/openssl/tests/CertificateGenerator.inc @@ -85,8 +85,8 @@ class CertificateGenerator openssl_x509_export_to_file($this->ca, $file); } - public function saveNewCertAndKey( - $commonNameForCert, $certFile, $keyFile, $keyLength = null, $subjectAltName = null + private function generateCertAndKey( + $commonNameForCert, $file, $keyLength = null, $subjectAltName = null ) { $dn = [ 'countryName' => 'BY', @@ -117,51 +117,53 @@ $subjectAltNameConfig basicConstraints = CA:FALSE $subjectAltNameConfig CONFIG; - $configFile = $certFile . '.cnf'; + $configFile = $file . '.cnf'; file_put_contents($configFile, $configCode); - try { - $config = [ - 'config' => $configFile, - 'req_extensions' => 'v3_req', - 'x509_extensions' => 'usr_cert', - ]; + $config = [ + 'config' => $configFile, + 'req_extensions' => 'v3_req', + 'x509_extensions' => 'usr_cert', + ]; - $this->lastKey = self::generateKey($keyLength); - $csr = openssl_csr_new($dn, $this->lastKey, $config); - $this->lastCert = openssl_csr_sign( - $csr, - $this->ca, - $this->caKey, - /* days */ 2, - $config, - ); - if (!$this->lastCert) { - throw new Exception('Failed to create certificate'); - } + $this->lastKey = self::generateKey($keyLength); + $csr = openssl_csr_new($dn, $this->lastKey, $config); + $this->lastCert = openssl_csr_sign( + $csr, + $this->ca, + $this->caKey, + /* days */ 2, + $config, + ); - $certText = ''; - openssl_x509_export($this->lastCert, $certText); - - $keyText = ''; - openssl_pkey_export($this->lastKey, $keyText, null, $config); - - if ($certFile === $keyFile) { - file_put_contents($certFile, $certText . PHP_EOL . $keyText); - } else { - file_put_contents($certFile, $certText); - file_put_contents($keyFile, $keyText); - } - } finally { - unlink($configFile); - } + return $config; } - public function saveNewCertAsFileWithKey( $commonNameForCert, $file, $keyLength = null, $subjectAltName = null ) { - $this->saveNewCertAndKey($commonNameForCert, $file, $file, $keyLength, $subjectAltName); + $config = $this->generateCertAndKey($commonNameForCert, $file, $keyLength, $subjectAltName); + + $certText = ''; + openssl_x509_export($this->lastCert, $certText); + + $keyText = ''; + openssl_pkey_export($this->lastKey, $keyText, null, $config); + + file_put_contents($file, $certText . PHP_EOL . $keyText); + + unlink($config['config']); + } + + public function saveNewCertAndKey( + $commonNameForCert, $certFile, $keyFile, $keyLength = null, $subjectAltName = null + ) { + $config = $this->generateCertAndKey($commonNameForCert, $certFile, $keyLength, $subjectAltName); + + openssl_x509_export_to_file($this->lastCert, $certFile); + openssl_pkey_export_to_file($this->lastKey, $keyFile, null, $config); + + unlink($config['config']); } public function getCertDigest($algo) diff --git a/ext/openssl/tests/bug50713.phpt b/ext/openssl/tests/bug50713.phpt new file mode 100644 index 00000000000..95eff2e75f9 --- /dev/null +++ b/ext/openssl/tests/bug50713.phpt @@ -0,0 +1,40 @@ +--TEST-- +Bug #50713 (openssl_pkcs7_verify() may ignore untrusted CAs) +--EXTENSIONS-- +openssl +--FILE-- +saveCaCert($cacertFile); +$certificateGenerator->saveNewCertAndKey('bug50713', $certFile, $keyFile, 1024); + +var_dump(openssl_pkcs7_sign($inFile, $outFile, 'file://' . $certFile, 'file://' . $keyFile, [], PKCS7_NOCERTS)); +var_dump(openssl_pkcs7_verify($outFile, 0, $signersFile, [$cacertFile], $certFile)); +var_dump(strlen(file_get_contents($signersFile)) > 0); +?> +--CLEAN-- + +--EXPECT-- +bool(true) +bool(true) +bool(true)