Skip to content

Instantly share code, notes, and snippets.

@md-riaz
Created June 21, 2023 07:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save md-riaz/19a07b285fd9a991d43bf41b54f6a389 to your computer and use it in GitHub Desktop.
Save md-riaz/19a07b285fd9a991d43bf41b54f6a389 to your computer and use it in GitHub Desktop.
<?php
function get_sans_from_csr($csr) {
$tempFile = tempnam(sys_get_temp_dir(), 'csr');
file_put_contents($tempFile, $csr);
$opensslCsrOutput = shell_exec("openssl req -noout -text -in " . escapeshellarg($tempFile));
unlink($tempFile);
if ($opensslCsrOutput !== null) {
$matches = [];
preg_match('/Subject Alternative Name:\s*(.*)/', $opensslCsrOutput, $matches);
if (!empty($matches[1])) {
$sans = [];
$sanValues = explode(', ', $matches[1]);
foreach ($sanValues as $value) {
$san = trim(str_replace(['DNS:', 'IP:'], '', $value));
$sans[] = $san;
}
return $sans;
}
}
return null;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment