forked from zer.ooo/web
You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
73 lines
1.5 KiB
PHP
73 lines
1.5 KiB
PHP
<?php
|
|
/**
|
|
* Created by PhpStorm.
|
|
* User: eater
|
|
* Date: 4/4/16
|
|
* Time: 9:25 PM
|
|
*/
|
|
|
|
namespace Eater\Glim\Service;
|
|
|
|
|
|
use Eater\Glim\Core;
|
|
|
|
class CA extends Main
|
|
{
|
|
/**
|
|
* @return string
|
|
*/
|
|
public function getOpenSslError()
|
|
{
|
|
$error = "";
|
|
while ($msg = openssl_error_string()) {
|
|
$error .= $msg . "\n";
|
|
}
|
|
|
|
return $error;
|
|
}
|
|
|
|
/**
|
|
* Signs a client certificate and returns the signed certificate
|
|
* @param string $csr
|
|
* @return string
|
|
*/
|
|
public function signClientCsr($csr)
|
|
{
|
|
/** @var Core $core */
|
|
$core = $this->get('core');
|
|
|
|
$csrPath = tempnam(sys_get_temp_dir(), '0.');
|
|
$crtPath = tempnam(sys_get_temp_dir(), '0.');
|
|
|
|
file_put_contents($csrPath, $csr);
|
|
|
|
exec(escapeshellcmd($core->getBaseDir() . '/bin/sign-client-csr') . ' ' . escapeshellarg($csrPath) . ' ' . escapeshellarg($crtPath) . ' 2>&1', $output, $exitCode);
|
|
|
|
if ($exitCode !== 0) {
|
|
throw new \Exception("Failed signing CSR: " . implode("\n", $output));
|
|
}
|
|
|
|
$crt = file_get_contents($crtPath);
|
|
|
|
unlink($crtPath);
|
|
unlink($csrPath);
|
|
|
|
return $crt;
|
|
}
|
|
|
|
/**
|
|
* @param string $csr
|
|
* @throws \Exception
|
|
*/
|
|
public function getCommonNameFromCsr($csr)
|
|
{
|
|
$subject = openssl_csr_get_subject($csr);
|
|
|
|
if ($subject === false) {
|
|
throw new \Exception("Failed to read CSR: " . $this->getOpenSslError());
|
|
}
|
|
|
|
return $subject["CN"];
|
|
}
|
|
|
|
} |