Skip to content

Commit 3a15536

Browse files
committed
WIP
1 parent b21c7b9 commit 3a15536

File tree

206 files changed

+14498
-169
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

206 files changed

+14498
-169
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,4 @@ composer.lock
66
.php-cs-fixer.cache
77
.DS_Store
88
.idea
9+
vendor

composer.json

Lines changed: 25 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -17,31 +17,31 @@
1717
}
1818
],
1919
"require": {
20-
"kduma/simple-dal": "^0.1",
21-
"kduma/simple-dal-adapter-contracts": "^0.1",
22-
"kduma/simple-dal-encryption": "^0.1",
23-
"kduma/simple-dal-encryption-phpseclib": "^0.1",
24-
"kduma/simple-dal-encryption-sodium": "^0.1",
25-
"kduma/simple-dal-integrity": "^0.1",
26-
"kduma/simple-dal-integrity-contracts": "^0.1",
27-
"kduma/simple-dal-integrity-hash": "^0.1",
28-
"kduma/simple-dal-integrity-phpseclib": "^0.1",
29-
"kduma/simple-dal-integrity-sodium": "^0.1",
30-
"kduma/simple-dal-db-adapter": "^0.1",
31-
"kduma/simple-dal-flysystem-adapter": "^0.1",
32-
"league/flysystem-ziparchive": "^3.0",
33-
"kduma/simple-dal-typed": "^0.1",
34-
"laravel-zero/framework": "^12.0.2",
35-
"league/flysystem": "^3.0",
20+
"kduma/simple-dal": "^v0.2",
21+
"kduma/simple-dal-adapter-contracts": "^v0.2",
22+
"kduma/simple-dal-encryption": "^v0.2",
23+
"kduma/simple-dal-encryption-phpseclib": "^v0.2",
24+
"kduma/simple-dal-encryption-sodium": "^v0.2",
25+
"kduma/simple-dal-integrity": "^v0.2",
26+
"kduma/simple-dal-integrity-contracts": "^v0.2",
27+
"kduma/simple-dal-integrity-hash": "^v0.2",
28+
"kduma/simple-dal-integrity-phpseclib": "^v0.2",
29+
"kduma/simple-dal-integrity-sodium": "^v0.2",
30+
"kduma/simple-dal-db-adapter": "^v0.2",
31+
"kduma/simple-dal-flysystem-adapter": "^v0.2",
32+
"league/flysystem-ziparchive": "^3.31",
33+
"kduma/simple-dal-typed": "^v0.2",
34+
"laravel-zero/framework": "^12.0.5",
35+
"league/flysystem": "^3.32",
3636
"php": "^8.4",
37-
"phpseclib/phpseclib": "^3.0",
37+
"phpseclib/phpseclib": "^3.0.50",
3838
"paragonie/sodium_compat": "^2.5"
3939
},
4040
"require-dev": {
41-
"laravel/pint": "^1.25",
42-
"mockery/mockery": "^1.6",
43-
"monorepo-php/monorepo": "^12.0",
44-
"pestphp/pest": "^3.0|^4.0",
41+
"laravel/pint": "^1.29",
42+
"mockery/mockery": "^1.6.12",
43+
"monorepo-php/monorepo": "^12.5.1",
44+
"pestphp/pest": "^3.0|^4.4.2",
4545
"phpacker/phpacker": "^0.6.4"
4646
},
4747
"replace": {
@@ -56,7 +56,10 @@
5656
"KDuma\\PhpCA\\": "src/library/src/",
5757
"KDuma\\PhpCA\\ConfigManager\\": "src/config-manager/src/",
5858
"KDuma\\PhpCA\\Contracts\\": "src/contracts/src/"
59-
}
59+
},
60+
"files": [
61+
"src/cli/app/helpers.php"
62+
]
6063
},
6164
"autoload-dev": {
6265
"psr-4": {

php-ca-config.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"adapter": {
3+
"type": "directory",
4+
"path": "./workdir/example_data"
5+
}
6+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
3+
namespace App\Commands;
4+
5+
use App\Concerns\DiscoversConfigurationTrait;
6+
use LaravelZero\Framework\Commands\Command;
7+
8+
abstract class BaseCommand extends Command
9+
{
10+
use DiscoversConfigurationTrait;
11+
12+
public function __construct()
13+
{
14+
parent::__construct();
15+
$this->bootDiscoversConfigurationTrait();
16+
}
17+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
3+
namespace App\Commands\Ca;
4+
5+
use App\Commands\BaseCommand;
6+
7+
use function Laravel\Prompts\error;
8+
use function Laravel\Prompts\info;
9+
10+
class ActivateCommand extends BaseCommand
11+
{
12+
protected $signature = 'ca:activate {id}';
13+
protected $description = 'Set the active CA certificate';
14+
15+
public function handle(): int
16+
{
17+
$ca = $this->getCertificationAuthority();
18+
$id = $this->argument('id');
19+
20+
if (! $ca->caCertificates->has($id)) {
21+
error("CA certificate \"{$id}\" not found.");
22+
return self::FAILURE;
23+
}
24+
25+
$ca->state->setActiveCaCertificateId($id);
26+
info("Active CA certificate set to \"{$id}\".");
27+
28+
return self::SUCCESS;
29+
}
30+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?php
2+
3+
namespace App\Commands\Ca\Csr;
4+
5+
use App\Commands\BaseCommand;
6+
use KDuma\PhpCA\Record\CertificateSubject\CertificateSubject;
7+
8+
use function Laravel\Prompts\error;
9+
use function Laravel\Prompts\info;
10+
use function Laravel\Prompts\text;
11+
12+
class CreateCommand extends BaseCommand
13+
{
14+
protected $signature = 'ca:csr:create {--id=} {--key=} {--dn=}';
15+
protected $description = 'Create a CSR for the CA';
16+
17+
public function handle(): int
18+
{
19+
$ca = $this->getCertificationAuthority();
20+
21+
$keyId = $this->option('key') ?? text('Key ID', required: true);
22+
$dn = $this->option('dn') ?? text('Distinguished Name', required: true);
23+
24+
try {
25+
$builder = $ca->caCsrs->getBuilder()
26+
->key($keyId)
27+
->subject(CertificateSubject::fromString($dn));
28+
29+
if ($this->option('id')) {
30+
$builder->id($this->option('id'));
31+
}
32+
33+
$csr = $builder->save();
34+
} catch (\Throwable $e) {
35+
error($e->getMessage());
36+
return self::FAILURE;
37+
}
38+
39+
info("CA CSR created: {$csr->id}");
40+
41+
return self::SUCCESS;
42+
}
43+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
namespace App\Commands\Ca\Csr;
4+
5+
use App\Commands\BaseCommand;
6+
7+
use function Laravel\Prompts\error;
8+
9+
class GetCommand extends BaseCommand
10+
{
11+
protected $signature = 'ca:csr:get {id}';
12+
protected $description = 'Output CA CSR PEM';
13+
14+
public function handle(): int
15+
{
16+
$ca = $this->getCertificationAuthority();
17+
$csr = $ca->caCsrs->findOrNull($this->argument('id'));
18+
19+
if ($csr === null) {
20+
error('CA CSR not found.');
21+
return self::FAILURE;
22+
}
23+
24+
$this->output->writeln($csr->csr);
25+
26+
return self::SUCCESS;
27+
}
28+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
3+
namespace App\Commands\Ca\Csr;
4+
5+
use App\Commands\BaseCommand;
6+
7+
use function Laravel\Prompts\info;
8+
9+
class ListCommand extends BaseCommand
10+
{
11+
protected $signature = 'ca:csr:list';
12+
protected $description = 'List all CA CSRs';
13+
14+
public function handle(): int
15+
{
16+
$ca = $this->getCertificationAuthority();
17+
$csrs = $ca->caCsrs->all();
18+
19+
if (empty($csrs)) {
20+
info('No CA CSRs found.');
21+
return self::SUCCESS;
22+
}
23+
24+
$this->table(
25+
['ID', 'Subject', 'Key ID', 'CA Cert ID'],
26+
array_map(fn ($c) => [
27+
$c->id,
28+
$c->getSubjectString(),
29+
$c->keyId,
30+
$c->caCertificateId ?? '-',
31+
], $csrs),
32+
);
33+
34+
return self::SUCCESS;
35+
}
36+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
namespace App\Commands\Ca;
4+
5+
use App\Commands\BaseCommand;
6+
7+
use function Laravel\Prompts\error;
8+
9+
class GetCommand extends BaseCommand
10+
{
11+
protected $signature = 'ca:get {id}';
12+
protected $description = 'Output CA certificate PEM';
13+
14+
public function handle(): int
15+
{
16+
$ca = $this->getCertificationAuthority();
17+
$cert = $ca->caCertificates->findOrNull($this->argument('id'));
18+
19+
if ($cert === null) {
20+
error('CA certificate not found.');
21+
return self::FAILURE;
22+
}
23+
24+
$this->output->writeln($cert->certificate);
25+
26+
return self::SUCCESS;
27+
}
28+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?php
2+
3+
namespace App\Commands\Ca;
4+
5+
use App\Commands\BaseCommand;
6+
use phpseclib3\File\X509;
7+
8+
use function Laravel\Prompts\error;
9+
use function Laravel\Prompts\info;
10+
11+
class GetDerCommand extends BaseCommand
12+
{
13+
protected $signature = 'ca:get:der {id} {--stdout : Output raw DER to stdout} {--output= : Output file path}';
14+
protected $description = 'Output CA certificate in DER format';
15+
16+
public function handle(): int
17+
{
18+
$ca = $this->getCertificationAuthority();
19+
$cert = $ca->caCertificates->findOrNull($this->argument('id'));
20+
21+
if ($cert === null) {
22+
error('CA certificate not found.');
23+
return self::FAILURE;
24+
}
25+
26+
$x509 = new X509();
27+
$x509->loadX509($cert->certificate);
28+
$der = $x509->saveX509($x509->getCurrentCert(), X509::FORMAT_DER);
29+
30+
if ($this->option('stdout')) {
31+
$this->output->write($der);
32+
return self::SUCCESS;
33+
}
34+
35+
$outputPath = $this->option('output') ?? $this->argument('id') . '.der';
36+
37+
file_put_contents($outputPath, $der);
38+
info("DER certificate written to {$outputPath}");
39+
40+
return self::SUCCESS;
41+
}
42+
}

0 commit comments

Comments
 (0)