1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types = 1); |
4
|
|
|
|
5
|
|
|
namespace X509\AttributeCertificate; |
6
|
|
|
|
7
|
|
|
use ASN1\Element; |
8
|
|
|
use ASN1\Type\Constructed\Sequence; |
9
|
|
|
use ASN1\Type\Primitive\BitString; |
10
|
|
|
use ASN1\Type\Primitive\Enumerated; |
11
|
|
|
use ASN1\Type\Primitive\ObjectIdentifier; |
12
|
|
|
use Sop\CryptoTypes\AlgorithmIdentifier\AlgorithmIdentifier; |
13
|
|
|
use Sop\CryptoTypes\AlgorithmIdentifier\Feature\AlgorithmIdentifierType; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Implements <i>ObjectDigestInfo</i> ASN.1 type. |
17
|
|
|
* |
18
|
|
|
* @link https://tools.ietf.org/html/rfc5755#section-4.1 |
19
|
|
|
* @link https://tools.ietf.org/html/rfc5755#section-7.3 |
20
|
|
|
*/ |
21
|
|
|
class ObjectDigestInfo |
22
|
|
|
{ |
23
|
|
|
const TYPE_PUBLIC_KEY = 0; |
24
|
|
|
const TYPE_PUBLIC_KEY_CERT = 1; |
25
|
|
|
const TYPE_OTHER_OBJECT_TYPES = 2; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Object type. |
29
|
|
|
* |
30
|
|
|
* @var int $_digestedObjectType |
31
|
|
|
*/ |
32
|
|
|
protected $_digestedObjectType; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* OID of other object type. |
36
|
|
|
* |
37
|
|
|
* @var string|null $_otherObjectTypeID |
38
|
|
|
*/ |
39
|
|
|
protected $_otherObjectTypeID; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Digest algorithm. |
43
|
|
|
* |
44
|
|
|
* @var AlgorithmIdentifierType $_digestAlgorithm |
45
|
|
|
*/ |
46
|
|
|
protected $_digestAlgorithm; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Object digest. |
50
|
|
|
* |
51
|
|
|
* @var BitString $_objectDigest |
52
|
|
|
*/ |
53
|
|
|
protected $_objectDigest; |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Constructor. |
57
|
|
|
* |
58
|
|
|
* @param int $type |
59
|
|
|
* @param AlgorithmIdentifierType $algo |
60
|
|
|
* @param BitString $digest |
61
|
|
|
*/ |
62
|
5 |
|
public function __construct(int $type, AlgorithmIdentifierType $algo, |
63
|
|
|
BitString $digest) |
64
|
|
|
{ |
65
|
5 |
|
$this->_digestedObjectType = $type; |
66
|
5 |
|
$this->_otherObjectTypeID = null; |
67
|
5 |
|
$this->_digestAlgorithm = $algo; |
68
|
5 |
|
$this->_objectDigest = $digest; |
69
|
5 |
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Initialize from ASN.1. |
73
|
|
|
* |
74
|
|
|
* @param Sequence $seq |
75
|
|
|
* @return self |
76
|
|
|
*/ |
77
|
4 |
|
public static function fromASN1(Sequence $seq): ObjectDigestInfo |
78
|
|
|
{ |
79
|
4 |
|
$type = $seq->at(0) |
80
|
4 |
|
->asEnumerated() |
81
|
4 |
|
->intNumber(); |
82
|
4 |
|
$oid = null; |
83
|
4 |
|
$idx = 1; |
84
|
4 |
|
if ($seq->has($idx, Element::TYPE_OBJECT_IDENTIFIER)) { |
85
|
1 |
|
$oid = $seq->at($idx++) |
86
|
1 |
|
->asObjectIdentifier() |
87
|
1 |
|
->oid(); |
88
|
|
|
} |
89
|
4 |
|
$algo = AlgorithmIdentifier::fromASN1($seq->at($idx++)->asSequence()); |
90
|
4 |
|
$digest = $seq->at($idx)->asBitString(); |
91
|
4 |
|
$obj = new self($type, $algo, $digest); |
92
|
4 |
|
$obj->_otherObjectTypeID = $oid; |
93
|
4 |
|
return $obj; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Generate ASN.1 structure. |
98
|
|
|
* |
99
|
|
|
* @return Sequence |
100
|
|
|
*/ |
101
|
5 |
|
public function toASN1(): Sequence |
102
|
|
|
{ |
103
|
5 |
|
$elements = array(new Enumerated($this->_digestedObjectType)); |
104
|
5 |
|
if (isset($this->_otherObjectTypeID)) { |
105
|
1 |
|
$elements[] = new ObjectIdentifier($this->_otherObjectTypeID); |
106
|
|
|
} |
107
|
5 |
|
$elements[] = $this->_digestAlgorithm->toASN1(); |
108
|
5 |
|
$elements[] = $this->_objectDigest; |
109
|
5 |
|
return new Sequence(...$elements); |
110
|
|
|
} |
111
|
|
|
} |
112
|
|
|
|