osb/app/Classes/SSL.php
2020-02-20 22:54:28 +11:00

106 lines
1.5 KiB
PHP

<?php
namespace App\Classes;
use Illuminate\Support\Arr;
class SSL
{
// Our CSR
private $csr_pem = NULL;
// Our Certificate
private $crt = [];
private $crt_pem = NULL;
// Our Key
private $key_pem = NULL;
/**
* @param $key
* @return null
* @throws \Exception
*/
public function __get($key)
{
switch($key)
{
case 'cn': return $this->cn();
case 'dn': return $this->dn();
default:
throw new \App\Exceptions\SSLUnknownAttribute($key);
}
}
private function cn()
{
$subject = Arr::get($this->crt,'subject');
if (! $subject AND $this->csr_pem) {
$subject = openssl_csr_get_subject($this->csr_pem);
}
return isset($subject['CN']) ? $subject['CN'] : NULL;
}
/**
* Add CSR
*
* @param $value
* @return mixed
*/
public function csr($value)
{
$this->csr_pem = $value;
return $this;
}
/**
* Add certificate
*
* @param $value
* @return mixed
*/
public function crt($value)
{
$this->crt_pem = $value;
$this->crt = openssl_x509_parse($this->crt);
return $this;
}
public function dn()
{
$dn = '';
if ($this->crt_pem) {
$this->crt = openssl_x509_parse($this->crt_pem);
$dn = Arr::get($this->crt,'name');
}
if (! $dn AND $this->csr_pem) {
$dna = openssl_csr_get_subject($this->csr_pem);
foreach ($dna as $k=>$v) {
if ($dn)
$dn .= ',';
$dn .= sprintf('%s=%s',$k,$v);
}
}
return $dn;
}
/**
* Add the Key
*
* @param $value
* @return mixed
*/
public function key($value)
{
$this->key_pem = $value;
return $this;
}
}