Debian Codegraph @

lib/3rdParty/composer/spomky-labs/pki-framework/src/X509/AttributeCertificate/ObjectDigestInfo.php

raw symbols SCIP
1 <?php
2
3 declare(strict_types=1);
4
5 namespace SpomkyLabs\Pki\X509\AttributeCertificate;
6
7 use SpomkyLabs\Pki\ASN1\Element;
8 use SpomkyLabs\Pki\ASN1\Type\Constructed\Sequence;
9 use SpomkyLabs\Pki\ASN1\Type\Primitive\BitString;
10 use SpomkyLabs\Pki\ASN1\Type\Primitive\Enumerated;
11 use SpomkyLabs\Pki\ASN1\Type\Primitive\ObjectIdentifier;
12 use SpomkyLabs\Pki\CryptoTypes\AlgorithmIdentifier\AlgorithmIdentifier;
13 use SpomkyLabs\Pki\CryptoTypes\AlgorithmIdentifier\Feature\AlgorithmIdentifierType;
14
15 /**
16 * Implements *ObjectDigestInfo* 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 final class ObjectDigestInfo
22 {
23 final public const TYPE_PUBLIC_KEY = 0;
24
25 final public const TYPE_PUBLIC_KEY_CERT = 1;
26
27 final public const TYPE_OTHER_OBJECT_TYPES = 2;
28
29 private function __construct(
30 private readonly int $digestedObjectType,
31 private readonly AlgorithmIdentifierType $digestAlgorithm,
32 private readonly BitString $objectDigest,
33 private readonly ?string $otherObjectTypeID
34 ) {
35 }
36
37 public static function create(
38 int $digestedObjectType,
39 AlgorithmIdentifierType $digestAlgorithm,
40 BitString $objectDigest,
41 ?string $otherObjectTypeID = null
42 ): self {
43 return new self($digestedObjectType, $digestAlgorithm, $objectDigest, $otherObjectTypeID);
44 }
45
46 /**
47 * Initialize from ASN.1.
48 */
49 public static function fromASN1(Sequence $seq): self
50 {
51 $idx = 0;
52 $oid = null;
53 $type = $seq->at($idx++)
54 ->asEnumerated()
55 ->intNumber();
56 if ($seq->has($idx, Element::TYPE_OBJECT_IDENTIFIER)) {
57 $oid = $seq->at($idx++)
58 ->asObjectIdentifier()
59 ->oid();
60 }
61 $algo = AlgorithmIdentifier::fromASN1($seq->at($idx++)->asSequence());
62 $digest = $seq->at($idx)
63 ->asBitString();
64 return self::create($type, $algo, $digest, $oid);
65 }
66
67 /**
68 * Generate ASN.1 structure.
69 */
70 public function toASN1(): Sequence
71 {
72 $elements = [Enumerated::create($this->digestedObjectType)];
73 if (isset($this->otherObjectTypeID)) {
74 $elements[] = ObjectIdentifier::create($this->otherObjectTypeID);
75 }
76 $elements[] = $this->digestAlgorithm->toASN1();
77 $elements[] = $this->objectDigest;
78 return Sequence::create(...$elements);
79 }
80 }