-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
25 changed files
with
654 additions
and
101 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,4 +2,5 @@ build | |
composer.lock | ||
docs | ||
vendor | ||
coverage | ||
coverage | ||
temp |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
C:30:"PHPUnit\Runner\TestResultCache":562:{a:2:{s:7:"defects";a:0:{}s:5:"times";a:15:{s:22:"instanceOfCsdConverter";d:0.028;s:22:"fileExistsOrIsReadable";d:0.003;s:14:"fileIsNotEmpty";d:0.002;s:15:"convertCerToPem";d:0.014;s:31:"convertCerToPemIfIsNotConverted";d:0.015;s:13:"cerIsValidCsd";d:0.016;s:15:"getSerialNumber";d:0.015;s:8:"getTaxId";d:0.015;s:24:"datesAreInstanceOfCarbon";d:0.014;s:7:"isValid";d:0.015;s:31:"convertKeyToPemIfIsNotConverted";d:0.019;s:17:"assertSeeFilename";d:0.022;s:16:"saveToAGivenPath";d:0.023;s:28:"saveToAGivenPathWithFilename";d:0.018;s:16:"encryptPemInDes3";d:0.029;}}} |
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,130 @@ | ||
<?php | ||
|
||
namespace FeiMx\Csd; | ||
|
||
use Carbon\Carbon; | ||
use FeiMx\Csd\Strategies\Cer; | ||
use FeiMx\Csd\Strategies\Key; | ||
use InvalidArgumentException; | ||
use RuntimeException; | ||
|
||
class CsdConverter | ||
{ | ||
const VALID = 1; | ||
|
||
const INVALID = 0; | ||
|
||
const EXPIRED = -1; | ||
|
||
public $cerStrategy; | ||
|
||
public $keyStrategy; | ||
|
||
public $data = []; | ||
|
||
public $version; | ||
|
||
public $serial_number; | ||
|
||
public $tax_id; | ||
|
||
public $name; | ||
|
||
public $valid_from; | ||
|
||
public $valid_to; | ||
|
||
/** | ||
* Create a new CsdConverter Instance. | ||
*/ | ||
public function __construct(string $cer_file, string $key_file, string $password) | ||
{ | ||
$this->assertValidFile($cer_file); | ||
$this->cerStrategy = new Cer($cer_file); | ||
|
||
$this->assertValidFile($key_file); | ||
$this->keyStrategy = new Key($key_file, $password); | ||
|
||
$this->setData(); | ||
} | ||
|
||
protected function assertValidFile(string $cer_file) | ||
{ | ||
if (!file_exists($cer_file) || !is_readable($cer_file)) { | ||
throw new InvalidArgumentException("File {$cer_file} does not exists or is not readable"); | ||
} | ||
} | ||
|
||
protected function setData() | ||
{ | ||
$this->data = openssl_x509_parse($this->cerStrategy->pem_content, true); | ||
if (!is_array($this->data)) { | ||
throw new RuntimeException("Cannot parse the certificate file {$this->cerStrategy->filename}"); | ||
} | ||
|
||
$this->mapDataToProps($this->data); | ||
} | ||
|
||
public function isValidCsd() | ||
{ | ||
return isset($this->data['subject']['OU']) && !empty($this->data['subject']['OU']); | ||
} | ||
|
||
public function getStatus() | ||
{ | ||
$now = Carbon::now(); | ||
if ($now > $this->valid_to) { | ||
return self::EXPIRED; | ||
} | ||
|
||
if ($now < $this->valid_from) { | ||
return self::INVALID; | ||
} | ||
|
||
if ($now > $this->valid_from && $now < $this->valid_to) { | ||
return self::VALID; | ||
} | ||
} | ||
|
||
protected function mapDataToProps($data) | ||
{ | ||
$this->version = $data['version']; | ||
|
||
$this->serial_number = $this->paserSeriaNumberHex($data['serialNumberHex']); | ||
|
||
$this->tax_id = preg_split('/\s/', $data['subject']['x500UniqueIdentifier'])[0]; | ||
|
||
$this->name = $data['subject']['name']; | ||
|
||
$this->valid_from = Carbon::createFromTimestamp($data['validFrom_time_t']); | ||
|
||
$this->valid_to = Carbon::createFromTimestamp($data['validTo_time_t']); | ||
} | ||
|
||
public function paserSeriaNumberHex($serialNumber) | ||
{ | ||
$parsedSerialNumber = ''; | ||
for ($i = 0; $i < strlen($serialNumber); ++$i) { | ||
if (0 != $i % 2) { | ||
$parsedSerialNumber .= substr($serialNumber, $i, 1); | ||
} | ||
} | ||
|
||
return $parsedSerialNumber; | ||
} | ||
|
||
public function save(string $path, string $filename = null) | ||
{ | ||
if (!is_dir($path)) { | ||
mkdir($path, 0777, true); | ||
} | ||
$this->cerStrategy->save($path, $filename); | ||
|
||
$this->keyStrategy->save($path, $filename); | ||
} | ||
|
||
public function encryptKey(string $pemfile, string $password) | ||
{ | ||
return $this->keyStrategy->encrypt($pemfile, $password); | ||
} | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
<?php | ||
|
||
namespace FeiMx\Csd; | ||
|
||
use Illuminate\Support\ServiceProvider; | ||
|
||
class CsdServiceProvider extends ServiceProvider | ||
{ | ||
/** | ||
* Bootstrap the application services. | ||
*/ | ||
public function boot() | ||
{ | ||
if ($this->app->runningInConsole()) { | ||
} | ||
} | ||
|
||
/** | ||
* Register the application services. | ||
*/ | ||
public function register() | ||
{ | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.