Last active
August 13, 2020 08:20
-
-
Save dol/e0b7f084e2e7158efc87 to your computer and use it in GitHub Desktop.
PHP CSR with subjectAltName
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$keyConfig = [ | |
'private_key_type' => OPENSSL_KEYTYPE_RSA, | |
'private_key_bits' => 2048, | |
]; | |
$key = openssl_pkey_new($keyConfig); | |
$sanDomains = [ | |
'mydomain.tld', | |
'seconddomain.tld', | |
]; | |
$dn = [ | |
'commonName' => reset($sanDomains), | |
]; | |
$csrConfig = [ | |
'config' => __DIR__ . '/openssl.cnf', | |
'req_extensions' => 'v3_req', | |
'digest_alg' => 'sha256', | |
]; | |
$addPrefix = function ($value) { | |
// Important: Sanatize domain value and check if a valid domain | |
return 'DNS:' . $value; | |
}; | |
$sanDomainPrefixed = array_map($addPrefix, $sanDomains); | |
putenv('PHP_PASS_SUBJECTALTNAME=' . implode(',', $sanDomainPrefixed)); | |
$csr = openssl_csr_new($dn, $key, $csrConfig); | |
if (false === $csr) { | |
while (($e = openssl_error_string()) !== false) { | |
echo $e . '\n'; | |
} | |
return; | |
} | |
openssl_csr_export($csr, $csrout); | |
echo $csrout; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[ req ] | |
distinguished_name = req_distinguished_name | |
req_extensions = v3_req | |
[ req_distinguished_name ] | |
[ v3_req ] | |
basicConstraints = CA:FALSE | |
keyUsage = nonRepudiation, digitalSignature, keyEncipherment | |
subjectAltName = ${ENV::PHP_PASS_SUBJECTALTNAME} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@thestorm-star https://lmgtfy.com/?q=php+read+pfx+private+key