3 declare(strict_types=1);
5 namespace SpomkyLabs\Pki\CryptoTypes\AlgorithmIdentifier;
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;
13 * Implements AlgorithmIdentifier ASN.1 type.
15 * @see https://tools.ietf.org/html/rfc2898#appendix-C
16 * @see https://tools.ietf.org/html/rfc3447#appendix-C
18 abstract class AlgorithmIdentifier implements AlgorithmIdentifierType
21 final public const OID_RSA_ENCRYPTION = '1.2.840.113549.1.1.1';
23 // RSA signature algorithms
24 final public const OID_MD2_WITH_RSA_ENCRYPTION = '1.2.840.113549.1.1.2';
26 final public const OID_MD4_WITH_RSA_ENCRYPTION = '1.2.840.113549.1.1.3';
28 final public const OID_MD5_WITH_RSA_ENCRYPTION = '1.2.840.113549.1.1.4';
30 final public const OID_SHA1_WITH_RSA_ENCRYPTION = '1.2.840.113549.1.1.5';
32 final public const OID_SHA256_WITH_RSA_ENCRYPTION = '1.2.840.113549.1.1.11';
34 final public const OID_SHA384_WITH_RSA_ENCRYPTION = '1.2.840.113549.1.1.12';
36 final public const OID_SHA512_WITH_RSA_ENCRYPTION = '1.2.840.113549.1.1.13';
38 final public const OID_SHA224_WITH_RSA_ENCRYPTION = '1.2.840.113549.1.1.14';
40 // Elliptic Curve signature algorithms
41 final public const OID_ECDSA_WITH_SHA1 = '1.2.840.10045.4.1';
43 final public const OID_ECDSA_WITH_SHA224 = '1.2.840.10045.4.3.1';
45 final public const OID_ECDSA_WITH_SHA256 = '1.2.840.10045.4.3.2';
47 final public const OID_ECDSA_WITH_SHA384 = '1.2.840.10045.4.3.3';
49 final public const OID_ECDSA_WITH_SHA512 = '1.2.840.10045.4.3.4';
51 // Elliptic Curve public key
52 final public const OID_EC_PUBLIC_KEY = '1.2.840.10045.2.1';
54 // Elliptic curve / algorithm pairs from RFC 8410
55 final public const OID_X25519 = '1.3.101.110';
57 final public const OID_X448 = '1.3.101.111';
59 final public const OID_ED25519 = '1.3.101.112';
61 final public const OID_ED448 = '1.3.101.113';
64 final public const OID_DES_CBC = '1.3.14.3.2.7';
66 final public const OID_RC2_CBC = '1.2.840.113549.3.2';
68 final public const OID_DES_EDE3_CBC = '1.2.840.113549.3.7';
70 final public const OID_AES_128_CBC = '2.16.840.1.101.3.4.1.2';
72 final public const OID_AES_192_CBC = '2.16.840.1.101.3.4.1.22';
74 final public const OID_AES_256_CBC = '2.16.840.1.101.3.4.1.42';
76 // HMAC-SHA-1 from RFC 8018
77 final public const OID_HMAC_WITH_SHA1 = '1.2.840.113549.2.7';
79 // HMAC algorithms from RFC 4231
80 final public const OID_HMAC_WITH_SHA224 = '1.2.840.113549.2.8';
82 final public const OID_HMAC_WITH_SHA256 = '1.2.840.113549.2.9';
84 final public const OID_HMAC_WITH_SHA384 = '1.2.840.113549.2.10';
86 final public const OID_HMAC_WITH_SHA512 = '1.2.840.113549.2.11';
88 // Message digest algorithms
89 final public const OID_MD5 = '1.2.840.113549.2.5';
91 final public const OID_SHA1 = '1.3.14.3.2.26';
93 final public const OID_SHA224 = '2.16.840.1.101.3.4.2.4';
95 final public const OID_SHA256 = '2.16.840.1.101.3.4.2.1';
97 final public const OID_SHA384 = '2.16.840.1.101.3.4.2.2';
99 final public const OID_SHA512 = '2.16.840.1.101.3.4.2.3';
101 protected function __construct(
102 protected readonly string $oid
107 * Initialize from ASN.1.
109 public static function fromASN1(Sequence $seq): self
111 return AlgorithmIdentifierFactory::create()->parse($seq);
114 public function oid(): string
119 public function toASN1(): Sequence
121 $elements = [ObjectIdentifier::create($this->oid)];
122 $params = $this->paramsASN1();
123 if (isset($params)) {
124 $elements[] = $params;
126 return Sequence::create(...$elements);
130 * Get algorithm identifier parameters as ASN.1.
132 * If type allows parameters to be omitted, return null.
134 abstract protected function paramsASN1(): ?Element;