71226d439586f3c9e614b9c1905b5ee1aa369045
[GitHub/WoltLab/WCF.git] /
1 <?php
2
3 declare(strict_types=1);
4
5 namespace SpomkyLabs\Pki\CryptoTypes\AlgorithmIdentifier;
6
7 use SpomkyLabs\Pki\ASN1\Element;
8 use SpomkyLabs\Pki\ASN1\Type\Constructed\Sequence;
9 use SpomkyLabs\Pki\ASN1\Type\Primitive\ObjectIdentifier;
10 use SpomkyLabs\Pki\CryptoTypes\AlgorithmIdentifier\Feature\AlgorithmIdentifierType;
11
12 /**
13 * Implements AlgorithmIdentifier ASN.1 type.
14 *
15 * @see https://tools.ietf.org/html/rfc2898#appendix-C
16 * @see https://tools.ietf.org/html/rfc3447#appendix-C
17 */
18 abstract class AlgorithmIdentifier implements AlgorithmIdentifierType
19 {
20 // RSA encryption
21 final public const OID_RSA_ENCRYPTION = '1.2.840.113549.1.1.1';
22
23 // RSA signature algorithms
24 final public const OID_MD2_WITH_RSA_ENCRYPTION = '1.2.840.113549.1.1.2';
25
26 final public const OID_MD4_WITH_RSA_ENCRYPTION = '1.2.840.113549.1.1.3';
27
28 final public const OID_MD5_WITH_RSA_ENCRYPTION = '1.2.840.113549.1.1.4';
29
30 final public const OID_SHA1_WITH_RSA_ENCRYPTION = '1.2.840.113549.1.1.5';
31
32 final public const OID_SHA256_WITH_RSA_ENCRYPTION = '1.2.840.113549.1.1.11';
33
34 final public const OID_SHA384_WITH_RSA_ENCRYPTION = '1.2.840.113549.1.1.12';
35
36 final public const OID_SHA512_WITH_RSA_ENCRYPTION = '1.2.840.113549.1.1.13';
37
38 final public const OID_SHA224_WITH_RSA_ENCRYPTION = '1.2.840.113549.1.1.14';
39
40 // Elliptic Curve signature algorithms
41 final public const OID_ECDSA_WITH_SHA1 = '1.2.840.10045.4.1';
42
43 final public const OID_ECDSA_WITH_SHA224 = '1.2.840.10045.4.3.1';
44
45 final public const OID_ECDSA_WITH_SHA256 = '1.2.840.10045.4.3.2';
46
47 final public const OID_ECDSA_WITH_SHA384 = '1.2.840.10045.4.3.3';
48
49 final public const OID_ECDSA_WITH_SHA512 = '1.2.840.10045.4.3.4';
50
51 // Elliptic Curve public key
52 final public const OID_EC_PUBLIC_KEY = '1.2.840.10045.2.1';
53
54 // Elliptic curve / algorithm pairs from RFC 8410
55 final public const OID_X25519 = '1.3.101.110';
56
57 final public const OID_X448 = '1.3.101.111';
58
59 final public const OID_ED25519 = '1.3.101.112';
60
61 final public const OID_ED448 = '1.3.101.113';
62
63 // Cipher algorithms
64 final public const OID_DES_CBC = '1.3.14.3.2.7';
65
66 final public const OID_RC2_CBC = '1.2.840.113549.3.2';
67
68 final public const OID_DES_EDE3_CBC = '1.2.840.113549.3.7';
69
70 final public const OID_AES_128_CBC = '2.16.840.1.101.3.4.1.2';
71
72 final public const OID_AES_192_CBC = '2.16.840.1.101.3.4.1.22';
73
74 final public const OID_AES_256_CBC = '2.16.840.1.101.3.4.1.42';
75
76 // HMAC-SHA-1 from RFC 8018
77 final public const OID_HMAC_WITH_SHA1 = '1.2.840.113549.2.7';
78
79 // HMAC algorithms from RFC 4231
80 final public const OID_HMAC_WITH_SHA224 = '1.2.840.113549.2.8';
81
82 final public const OID_HMAC_WITH_SHA256 = '1.2.840.113549.2.9';
83
84 final public const OID_HMAC_WITH_SHA384 = '1.2.840.113549.2.10';
85
86 final public const OID_HMAC_WITH_SHA512 = '1.2.840.113549.2.11';
87
88 // Message digest algorithms
89 final public const OID_MD5 = '1.2.840.113549.2.5';
90
91 final public const OID_SHA1 = '1.3.14.3.2.26';
92
93 final public const OID_SHA224 = '2.16.840.1.101.3.4.2.4';
94
95 final public const OID_SHA256 = '2.16.840.1.101.3.4.2.1';
96
97 final public const OID_SHA384 = '2.16.840.1.101.3.4.2.2';
98
99 final public const OID_SHA512 = '2.16.840.1.101.3.4.2.3';
100
101 protected function __construct(
102 protected readonly string $oid
103 ) {
104 }
105
106 /**
107 * Initialize from ASN.1.
108 */
109 public static function fromASN1(Sequence $seq): self
110 {
111 return AlgorithmIdentifierFactory::create()->parse($seq);
112 }
113
114 public function oid(): string
115 {
116 return $this->oid;
117 }
118
119 public function toASN1(): Sequence
120 {
121 $elements = [ObjectIdentifier::create($this->oid)];
122 $params = $this->paramsASN1();
123 if (isset($params)) {
124 $elements[] = $params;
125 }
126 return Sequence::create(...$elements);
127 }
128
129 /**
130 * Get algorithm identifier parameters as ASN.1.
131 *
132 * If type allows parameters to be omitted, return null.
133 */
134 abstract protected function paramsASN1(): ?Element;
135 }