Skip to content

Commit 21a2da2

Browse files
committed
Generate temporary config file when generating certificates
The putenv trick doesn't work on ZTS Windows, so generate a new openssl config every time.
1 parent f3e6b12 commit 21a2da2

File tree

2 files changed

+47
-37
lines changed

2 files changed

+47
-37
lines changed

ext/openssl/tests/CertificateGenerator.inc

Lines changed: 47 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
class CertificateGenerator
44
{
55
const CONFIG = __DIR__. DIRECTORY_SEPARATOR . 'openssl.cnf';
6-
const SAN_CONFIG = __DIR__ . DIRECTORY_SEPARATOR . 'san.cnf';
76

87
/** @var resource */
98
private $ca;
@@ -96,32 +95,56 @@ class CertificateGenerator
9695
$dn['commonName'] = $commonNameForCert;
9796
}
9897

99-
$config = [
100-
'digest_alg' => 'sha256',
101-
'req_extensions' => 'v3_req',
102-
'x509_extensions' => 'usr_cert',
103-
];
104-
if ($subjectAltName !== null) {
105-
putenv("PHP_SUBJECTALTNAME=$subjectAltName");
106-
$config['config'] = self::SAN_CONFIG;
107-
}
108-
109-
$this->lastKey = self::generateKey($keyLength);
110-
$this->lastCert = openssl_csr_sign(
111-
openssl_csr_new($dn, $this->lastKey, $config),
112-
$this->ca,
113-
$this->caKey,
114-
/* days */ 2,
115-
$config,
116-
);
98+
$subjectAltNameConfig =
99+
$subjectAltName ? "subjectAltName = $subjectAltName" : "";
100+
$configCode = <<<CONFIG
101+
[ req ]
102+
distinguished_name = req_distinguished_name
103+
default_md = sha256
104+
105+
[ req_distinguished_name ]
106+
107+
[ v3_req ]
108+
basicConstraints = CA:FALSE
109+
keyUsage = nonRepudiation, digitalSignature, keyEncipherment
110+
$subjectAltNameConfig
111+
112+
[ usr_cert ]
113+
basicConstraints = CA:FALSE
114+
$subjectAltNameConfig
115+
CONFIG;
116+
$configFile = $file . '.cnf';
117+
file_put_contents($configFile, $configCode);
118+
119+
try {
120+
$config = [
121+
'config' => $configFile,
122+
'req_extensions' => 'v3_req',
123+
'x509_extensions' => 'usr_cert',
124+
];
125+
126+
$this->lastKey = self::generateKey($keyLength);
127+
$this->lastCert = openssl_csr_sign(
128+
openssl_csr_new($dn, $this->lastKey, $config),
129+
$this->ca,
130+
$this->caKey,
131+
/* days */ 2,
132+
$config,
133+
);
134+
if (!$this->lastCert) {
135+
throw new Exception('Failed to create certificate');
136+
}
117137

118-
$certText = '';
119-
openssl_x509_export($this->lastCert, $certText);
138+
$certText = '';
139+
openssl_x509_export($this->lastCert, $certText);
120140

121-
$keyText = '';
122-
openssl_pkey_export($this->lastKey, $keyText);
141+
$keyText = '';
142+
openssl_pkey_export($this->lastKey, $keyText);
123143

124-
file_put_contents($file, $certText . PHP_EOL . $keyText);
144+
file_put_contents($file, $certText . PHP_EOL . $keyText);
145+
} finally {
146+
unlink($configFile);
147+
}
125148
}
126149

127150
public function getCertDigest($algo)

ext/openssl/tests/san.cnf

Lines changed: 0 additions & 13 deletions
This file was deleted.

0 commit comments

Comments
 (0)