|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types = 1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Sop\X509\Certificate; |
|
6
|
|
|
|
|
7
|
|
|
use Sop\ASN1\Type\Constructed\Sequence; |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* Implements *Validity* ASN.1 type. |
|
11
|
|
|
* |
|
12
|
|
|
* @see https://tools.ietf.org/html/rfc5280#section-4.1.2.5 |
|
13
|
|
|
*/ |
|
14
|
|
|
class Validity |
|
15
|
|
|
{ |
|
16
|
|
|
/** |
|
17
|
|
|
* Not before time. |
|
18
|
|
|
* |
|
19
|
|
|
* @var Time |
|
20
|
|
|
*/ |
|
21
|
|
|
protected $_notBefore; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* Not after time. |
|
25
|
|
|
* |
|
26
|
|
|
* @var Time |
|
27
|
|
|
*/ |
|
28
|
|
|
protected $_notAfter; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* Constructor. |
|
32
|
|
|
* |
|
33
|
|
|
* @param Time $not_before |
|
34
|
|
|
* @param Time $not_after |
|
35
|
|
|
*/ |
|
36
|
29 |
|
public function __construct(Time $not_before, Time $not_after) |
|
37
|
|
|
{ |
|
38
|
29 |
|
$this->_notBefore = $not_before; |
|
39
|
29 |
|
$this->_notAfter = $not_after; |
|
40
|
29 |
|
} |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* Initialize from ASN.1. |
|
44
|
|
|
* |
|
45
|
|
|
* @param Sequence $seq |
|
46
|
|
|
*/ |
|
47
|
21 |
|
public static function fromASN1(Sequence $seq): self |
|
48
|
|
|
{ |
|
49
|
21 |
|
$nb = Time::fromASN1($seq->at(0)->asTime()); |
|
50
|
21 |
|
$na = Time::fromASN1($seq->at(1)->asTime()); |
|
51
|
21 |
|
return new self($nb, $na); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* Initialize from date strings. |
|
56
|
|
|
* |
|
57
|
|
|
* @param null|string $nb_date Not before date |
|
58
|
|
|
* @param null|string $na_date Not after date |
|
59
|
|
|
* @param null|string $tz Timezone string |
|
60
|
|
|
* |
|
61
|
|
|
* @return self |
|
62
|
|
|
*/ |
|
63
|
8 |
|
public static function fromStrings(?string $nb_date, ?string $na_date, |
|
64
|
|
|
?string $tz = null): self |
|
65
|
|
|
{ |
|
66
|
8 |
|
return new self(Time::fromString($nb_date, $tz), |
|
67
|
8 |
|
Time::fromString($na_date, $tz)); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* Get not before time. |
|
72
|
|
|
* |
|
73
|
|
|
* @return Time |
|
74
|
|
|
*/ |
|
75
|
46 |
|
public function notBefore(): Time |
|
76
|
|
|
{ |
|
77
|
46 |
|
return $this->_notBefore; |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* Get not after time. |
|
82
|
|
|
* |
|
83
|
|
|
* @return Time |
|
84
|
|
|
*/ |
|
85
|
45 |
|
public function notAfter(): Time |
|
86
|
|
|
{ |
|
87
|
45 |
|
return $this->_notAfter; |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* Generate ASN.1 structure. |
|
92
|
|
|
* |
|
93
|
|
|
* @return Sequence |
|
94
|
|
|
*/ |
|
95
|
66 |
|
public function toASN1(): Sequence |
|
96
|
|
|
{ |
|
97
|
66 |
|
return new Sequence($this->_notBefore->toASN1(), |
|
98
|
66 |
|
$this->_notAfter->toASN1()); |
|
99
|
|
|
} |
|
100
|
|
|
} |
|
101
|
|
|
|