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\KeyType; |
17
|
|
|
use LetsEncrypt\Enum\RSAKeyLength; |
18
|
|
|
use LetsEncrypt\Helper\KeyGenerator; |
19
|
|
|
|
20
|
|
|
final class Certificate |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* @var Key |
24
|
|
|
*/ |
25
|
|
|
private $key; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @var \DateTimeImmutable|null |
29
|
|
|
*/ |
30
|
|
|
private $notBefore; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @var \DateTimeImmutable|null |
34
|
|
|
*/ |
35
|
|
|
private $notAfter; |
36
|
|
|
|
37
|
|
|
private function __construct(Key $key, ?string $notBefore, ?string $notAfter) |
38
|
|
|
{ |
39
|
|
|
$this->key = $key; |
40
|
|
|
$this->notBefore = $notBefore !== null ? new \DateTimeImmutable($notBefore) : null; |
41
|
|
|
$this->notAfter = $notAfter !== null ? new \DateTimeImmutable($notAfter) : null; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
public static function createWithRSAKey( |
45
|
|
|
RSAKeyLength $keyLength, |
46
|
|
|
string $notBefore = null, |
47
|
|
|
string $notAfter = null |
48
|
|
|
): self { |
49
|
|
|
return new static(Key::rsa($keyLength), $notBefore, $notAfter); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
public static function createWithECKey( |
53
|
|
|
ECKeyAlgorithm $algorithm, |
54
|
|
|
string $notBefore = null, |
55
|
|
|
string $notAfter = null |
56
|
|
|
): self { |
57
|
|
|
return new static(Key::ec($algorithm), $notBefore, $notAfter); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
public function getNotBefore(): string |
61
|
|
|
{ |
62
|
|
|
return $this->notBefore !== null ? $this->notBefore->format(DATE_RFC3339) : ''; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
public function getNotAfter(): string |
66
|
|
|
{ |
67
|
|
|
return $this->notAfter !== null ? $this->notAfter->format(DATE_RFC3339) : ''; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
public function getKeyType(): KeyType |
71
|
|
|
{ |
72
|
|
|
return $this->key->getType(); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
public function generate(KeyGenerator $keyGenerator, string $privateKeyPath, string $publicKeyPath): void |
76
|
|
|
{ |
77
|
|
|
$this->key->generate($keyGenerator, $privateKeyPath, $publicKeyPath); |
78
|
|
|
} |
79
|
|
|
} |
80
|
|
|
|