1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/* |
6
|
|
|
* This file is part of the LetsEncrypt ACME client. |
7
|
|
|
* |
8
|
|
|
* @author Ivanov Aleksandr <[email protected]> |
9
|
|
|
* @copyright 2019 |
10
|
|
|
* @license https://github.com/misantron/letsencrypt-client/blob/master/LICENSE MIT License |
11
|
|
|
*/ |
12
|
|
|
|
13
|
|
|
namespace LetsEncrypt\Certificate; |
14
|
|
|
|
15
|
|
|
use LetsEncrypt\Enum\ECKeyAlgorithm; |
16
|
|
|
use LetsEncrypt\Enum\RSAKeyLength; |
17
|
|
|
use LetsEncrypt\Helper\KeyGenerator; |
18
|
|
|
|
19
|
|
|
final class Certificate |
20
|
|
|
{ |
21
|
|
|
/** |
22
|
|
|
* @var Key |
23
|
|
|
*/ |
24
|
|
|
private $key; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var \DateTimeImmutable|null |
28
|
|
|
*/ |
29
|
|
|
private $notBefore; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var \DateTimeImmutable|null |
33
|
|
|
*/ |
34
|
|
|
private $notAfter; |
35
|
|
|
|
36
|
|
|
private function __construct(Key $key, ?string $notBefore, ?string $notAfter) |
37
|
|
|
{ |
38
|
|
|
$this->key = $key; |
39
|
|
|
$this->notBefore = $notBefore !== null ? new \DateTimeImmutable($notBefore) : null; |
40
|
|
|
$this->notAfter = $notAfter !== null ? new \DateTimeImmutable($notAfter) : null; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
public static function createWithRSAKey( |
44
|
|
|
RSAKeyLength $keyLength, |
45
|
|
|
string $notBefore = null, |
46
|
|
|
string $notAfter = null |
47
|
|
|
): self { |
48
|
|
|
return new static(Key::rsa($keyLength), $notBefore, $notAfter); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
public static function createWithECKey( |
52
|
|
|
ECKeyAlgorithm $algorithm, |
53
|
|
|
string $notBefore = null, |
54
|
|
|
string $notAfter = null |
55
|
|
|
): self { |
56
|
|
|
return new static(Key::ec($algorithm), $notBefore, $notAfter); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
public function getNotBefore(): string |
60
|
|
|
{ |
61
|
|
|
return $this->notBefore !== null ? $this->notBefore->format(DATE_RFC3339) : ''; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
public function getNotAfter(): string |
65
|
|
|
{ |
66
|
|
|
return $this->notAfter !== null ? $this->notAfter->format(DATE_RFC3339) : ''; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
public function generate(KeyGenerator $keyGenerator, string $privateKeyPath, string $publicKeyPath): void |
70
|
|
|
{ |
71
|
|
|
$this->key->generate($keyGenerator, $privateKeyPath, $publicKeyPath); |
72
|
|
|
} |
73
|
|
|
} |
74
|
|
|
|