|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types = 1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Sop\X509\AttributeCertificate; |
|
6
|
|
|
|
|
7
|
|
|
use Sop\ASN1\Element; |
|
8
|
|
|
use Sop\ASN1\Type\Constructed\Sequence; |
|
9
|
|
|
use Sop\ASN1\Type\Primitive\BitString; |
|
10
|
|
|
use Sop\ASN1\Type\Primitive\Enumerated; |
|
11
|
|
|
use Sop\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
|
|
|
* @see https://tools.ietf.org/html/rfc5755#section-4.1 |
|
19
|
|
|
* @see 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 |
|
31
|
|
|
*/ |
|
32
|
|
|
protected $_digestedObjectType; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* OID of other object type. |
|
36
|
|
|
* |
|
37
|
|
|
* @var null|string |
|
38
|
|
|
*/ |
|
39
|
|
|
protected $_otherObjectTypeID; |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* Digest algorithm. |
|
43
|
|
|
* |
|
44
|
|
|
* @var AlgorithmIdentifierType |
|
45
|
|
|
*/ |
|
46
|
|
|
protected $_digestAlgorithm; |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* Object digest. |
|
50
|
|
|
* |
|
51
|
|
|
* @var BitString |
|
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
|
|
|
* |
|
76
|
|
|
* @return self |
|
77
|
|
|
*/ |
|
78
|
4 |
|
public static function fromASN1(Sequence $seq): ObjectDigestInfo |
|
79
|
|
|
{ |
|
80
|
4 |
|
$idx = 0; |
|
81
|
4 |
|
$oid = null; |
|
82
|
4 |
|
$type = $seq->at($idx++)->asEnumerated()->intNumber(); |
|
83
|
4 |
|
if ($seq->has($idx, Element::TYPE_OBJECT_IDENTIFIER)) { |
|
84
|
1 |
|
$oid = $seq->at($idx++)->asObjectIdentifier()->oid(); |
|
85
|
|
|
} |
|
86
|
4 |
|
$algo = AlgorithmIdentifier::fromASN1($seq->at($idx++)->asSequence()); |
|
87
|
4 |
|
$digest = $seq->at($idx)->asBitString(); |
|
88
|
4 |
|
$obj = new self($type, $algo, $digest); |
|
89
|
4 |
|
$obj->_otherObjectTypeID = $oid; |
|
90
|
4 |
|
return $obj; |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* Generate ASN.1 structure. |
|
95
|
|
|
* |
|
96
|
|
|
* @return Sequence |
|
97
|
|
|
*/ |
|
98
|
5 |
|
public function toASN1(): Sequence |
|
99
|
|
|
{ |
|
100
|
5 |
|
$elements = [new Enumerated($this->_digestedObjectType)]; |
|
101
|
5 |
|
if (isset($this->_otherObjectTypeID)) { |
|
102
|
1 |
|
$elements[] = new ObjectIdentifier($this->_otherObjectTypeID); |
|
|
|
|
|
|
103
|
|
|
} |
|
104
|
5 |
|
$elements[] = $this->_digestAlgorithm->toASN1(); |
|
105
|
5 |
|
$elements[] = $this->_objectDigest; |
|
106
|
5 |
|
return new Sequence(...$elements); |
|
107
|
|
|
} |
|
108
|
|
|
} |
|
109
|
|
|
|