@@ -13,86 +13,86 @@ |
||
| 13 | 13 | */ |
| 14 | 14 | class Validity |
| 15 | 15 | { |
| 16 | - /** |
|
| 17 | - * Not before time. |
|
| 18 | - * |
|
| 19 | - * @var Time $_notBefore |
|
| 20 | - */ |
|
| 21 | - protected $_notBefore; |
|
| 16 | + /** |
|
| 17 | + * Not before time. |
|
| 18 | + * |
|
| 19 | + * @var Time $_notBefore |
|
| 20 | + */ |
|
| 21 | + protected $_notBefore; |
|
| 22 | 22 | |
| 23 | - /** |
|
| 24 | - * Not after time. |
|
| 25 | - * |
|
| 26 | - * @var Time $_notAfter |
|
| 27 | - */ |
|
| 28 | - protected $_notAfter; |
|
| 23 | + /** |
|
| 24 | + * Not after time. |
|
| 25 | + * |
|
| 26 | + * @var Time $_notAfter |
|
| 27 | + */ |
|
| 28 | + protected $_notAfter; |
|
| 29 | 29 | |
| 30 | - /** |
|
| 31 | - * Constructor. |
|
| 32 | - * |
|
| 33 | - * @param Time $not_before |
|
| 34 | - * @param Time $not_after |
|
| 35 | - */ |
|
| 36 | - public function __construct(Time $not_before, Time $not_after) |
|
| 37 | - { |
|
| 38 | - $this->_notBefore = $not_before; |
|
| 39 | - $this->_notAfter = $not_after; |
|
| 40 | - } |
|
| 30 | + /** |
|
| 31 | + * Constructor. |
|
| 32 | + * |
|
| 33 | + * @param Time $not_before |
|
| 34 | + * @param Time $not_after |
|
| 35 | + */ |
|
| 36 | + public function __construct(Time $not_before, Time $not_after) |
|
| 37 | + { |
|
| 38 | + $this->_notBefore = $not_before; |
|
| 39 | + $this->_notAfter = $not_after; |
|
| 40 | + } |
|
| 41 | 41 | |
| 42 | - /** |
|
| 43 | - * Initialize from ASN.1. |
|
| 44 | - * |
|
| 45 | - * @param Sequence $seq |
|
| 46 | - */ |
|
| 47 | - public static function fromASN1(Sequence $seq): self |
|
| 48 | - { |
|
| 49 | - $nb = Time::fromASN1($seq->at(0)->asTime()); |
|
| 50 | - $na = Time::fromASN1($seq->at(1)->asTime()); |
|
| 51 | - return new self($nb, $na); |
|
| 52 | - } |
|
| 42 | + /** |
|
| 43 | + * Initialize from ASN.1. |
|
| 44 | + * |
|
| 45 | + * @param Sequence $seq |
|
| 46 | + */ |
|
| 47 | + public static function fromASN1(Sequence $seq): self |
|
| 48 | + { |
|
| 49 | + $nb = Time::fromASN1($seq->at(0)->asTime()); |
|
| 50 | + $na = Time::fromASN1($seq->at(1)->asTime()); |
|
| 51 | + return new self($nb, $na); |
|
| 52 | + } |
|
| 53 | 53 | |
| 54 | - /** |
|
| 55 | - * Initialize from date strings. |
|
| 56 | - * |
|
| 57 | - * @param string|null $nb_date Not before date |
|
| 58 | - * @param string|null $na_date Not after date |
|
| 59 | - * @param string|null $tz Timezone string |
|
| 60 | - * @return self |
|
| 61 | - */ |
|
| 62 | - public static function fromStrings($nb_date, $na_date, $tz = null): self |
|
| 63 | - { |
|
| 64 | - return new self(Time::fromString($nb_date, $tz), |
|
| 65 | - Time::fromString($na_date, $tz)); |
|
| 66 | - } |
|
| 54 | + /** |
|
| 55 | + * Initialize from date strings. |
|
| 56 | + * |
|
| 57 | + * @param string|null $nb_date Not before date |
|
| 58 | + * @param string|null $na_date Not after date |
|
| 59 | + * @param string|null $tz Timezone string |
|
| 60 | + * @return self |
|
| 61 | + */ |
|
| 62 | + public static function fromStrings($nb_date, $na_date, $tz = null): self |
|
| 63 | + { |
|
| 64 | + return new self(Time::fromString($nb_date, $tz), |
|
| 65 | + Time::fromString($na_date, $tz)); |
|
| 66 | + } |
|
| 67 | 67 | |
| 68 | - /** |
|
| 69 | - * Get not before time. |
|
| 70 | - * |
|
| 71 | - * @return Time |
|
| 72 | - */ |
|
| 73 | - public function notBefore(): Time |
|
| 74 | - { |
|
| 75 | - return $this->_notBefore; |
|
| 76 | - } |
|
| 68 | + /** |
|
| 69 | + * Get not before time. |
|
| 70 | + * |
|
| 71 | + * @return Time |
|
| 72 | + */ |
|
| 73 | + public function notBefore(): Time |
|
| 74 | + { |
|
| 75 | + return $this->_notBefore; |
|
| 76 | + } |
|
| 77 | 77 | |
| 78 | - /** |
|
| 79 | - * Get not after time. |
|
| 80 | - * |
|
| 81 | - * @return Time |
|
| 82 | - */ |
|
| 83 | - public function notAfter(): Time |
|
| 84 | - { |
|
| 85 | - return $this->_notAfter; |
|
| 86 | - } |
|
| 78 | + /** |
|
| 79 | + * Get not after time. |
|
| 80 | + * |
|
| 81 | + * @return Time |
|
| 82 | + */ |
|
| 83 | + public function notAfter(): Time |
|
| 84 | + { |
|
| 85 | + return $this->_notAfter; |
|
| 86 | + } |
|
| 87 | 87 | |
| 88 | - /** |
|
| 89 | - * Generate ASN.1 structure. |
|
| 90 | - * |
|
| 91 | - * @return Sequence |
|
| 92 | - */ |
|
| 93 | - public function toASN1(): Sequence |
|
| 94 | - { |
|
| 95 | - return new Sequence($this->_notBefore->toASN1(), |
|
| 96 | - $this->_notAfter->toASN1()); |
|
| 97 | - } |
|
| 88 | + /** |
|
| 89 | + * Generate ASN.1 structure. |
|
| 90 | + * |
|
| 91 | + * @return Sequence |
|
| 92 | + */ |
|
| 93 | + public function toASN1(): Sequence |
|
| 94 | + { |
|
| 95 | + return new Sequence($this->_notBefore->toASN1(), |
|
| 96 | + $this->_notAfter->toASN1()); |
|
| 97 | + } |
|
| 98 | 98 | } |
@@ -53,7 +53,7 @@ |
||
| 53 | 53 | public static function fromPEMs(PEM ...$pems): self |
| 54 | 54 | { |
| 55 | 55 | $certs = array_map( |
| 56 | - function ($pem) { |
|
| 56 | + function($pem) { |
|
| 57 | 57 | return Certificate::fromPEM($pem); |
| 58 | 58 | }, $pems); |
| 59 | 59 | return new self(...$certs); |
@@ -12,208 +12,208 @@ |
||
| 12 | 12 | */ |
| 13 | 13 | class CertificateBundle implements \Countable, \IteratorAggregate |
| 14 | 14 | { |
| 15 | - /** |
|
| 16 | - * Certificates. |
|
| 17 | - * |
|
| 18 | - * @var Certificate[] $_certs |
|
| 19 | - */ |
|
| 20 | - protected $_certs; |
|
| 15 | + /** |
|
| 16 | + * Certificates. |
|
| 17 | + * |
|
| 18 | + * @var Certificate[] $_certs |
|
| 19 | + */ |
|
| 20 | + protected $_certs; |
|
| 21 | 21 | |
| 22 | - /** |
|
| 23 | - * Mapping from public key id to array of certificates. |
|
| 24 | - * |
|
| 25 | - * @var null|(Certificate[])[] |
|
| 26 | - */ |
|
| 27 | - private $_keyIdMap; |
|
| 22 | + /** |
|
| 23 | + * Mapping from public key id to array of certificates. |
|
| 24 | + * |
|
| 25 | + * @var null|(Certificate[])[] |
|
| 26 | + */ |
|
| 27 | + private $_keyIdMap; |
|
| 28 | 28 | |
| 29 | - /** |
|
| 30 | - * Constructor. |
|
| 31 | - * |
|
| 32 | - * @param Certificate ...$certs Certificate objects |
|
| 33 | - */ |
|
| 34 | - public function __construct(Certificate ...$certs) |
|
| 35 | - { |
|
| 36 | - $this->_certs = $certs; |
|
| 37 | - } |
|
| 29 | + /** |
|
| 30 | + * Constructor. |
|
| 31 | + * |
|
| 32 | + * @param Certificate ...$certs Certificate objects |
|
| 33 | + */ |
|
| 34 | + public function __construct(Certificate ...$certs) |
|
| 35 | + { |
|
| 36 | + $this->_certs = $certs; |
|
| 37 | + } |
|
| 38 | 38 | |
| 39 | - /** |
|
| 40 | - * Reset internal cached variables on clone. |
|
| 41 | - */ |
|
| 42 | - public function __clone() |
|
| 43 | - { |
|
| 44 | - $this->_keyIdMap = null; |
|
| 45 | - } |
|
| 39 | + /** |
|
| 40 | + * Reset internal cached variables on clone. |
|
| 41 | + */ |
|
| 42 | + public function __clone() |
|
| 43 | + { |
|
| 44 | + $this->_keyIdMap = null; |
|
| 45 | + } |
|
| 46 | 46 | |
| 47 | - /** |
|
| 48 | - * Initialize from PEMs. |
|
| 49 | - * |
|
| 50 | - * @param PEM ...$pems PEM objects |
|
| 51 | - * @return self |
|
| 52 | - */ |
|
| 53 | - public static function fromPEMs(PEM ...$pems): self |
|
| 54 | - { |
|
| 55 | - $certs = array_map( |
|
| 56 | - function ($pem) { |
|
| 57 | - return Certificate::fromPEM($pem); |
|
| 58 | - }, $pems); |
|
| 59 | - return new self(...$certs); |
|
| 60 | - } |
|
| 47 | + /** |
|
| 48 | + * Initialize from PEMs. |
|
| 49 | + * |
|
| 50 | + * @param PEM ...$pems PEM objects |
|
| 51 | + * @return self |
|
| 52 | + */ |
|
| 53 | + public static function fromPEMs(PEM ...$pems): self |
|
| 54 | + { |
|
| 55 | + $certs = array_map( |
|
| 56 | + function ($pem) { |
|
| 57 | + return Certificate::fromPEM($pem); |
|
| 58 | + }, $pems); |
|
| 59 | + return new self(...$certs); |
|
| 60 | + } |
|
| 61 | 61 | |
| 62 | - /** |
|
| 63 | - * Initialize from PEM bundle. |
|
| 64 | - * |
|
| 65 | - * @param PEMBundle $pem_bundle |
|
| 66 | - * @return self |
|
| 67 | - */ |
|
| 68 | - public static function fromPEMBundle(PEMBundle $pem_bundle): self |
|
| 69 | - { |
|
| 70 | - return self::fromPEMs(...$pem_bundle->all()); |
|
| 71 | - } |
|
| 62 | + /** |
|
| 63 | + * Initialize from PEM bundle. |
|
| 64 | + * |
|
| 65 | + * @param PEMBundle $pem_bundle |
|
| 66 | + * @return self |
|
| 67 | + */ |
|
| 68 | + public static function fromPEMBundle(PEMBundle $pem_bundle): self |
|
| 69 | + { |
|
| 70 | + return self::fromPEMs(...$pem_bundle->all()); |
|
| 71 | + } |
|
| 72 | 72 | |
| 73 | - /** |
|
| 74 | - * Get self with certificates added. |
|
| 75 | - * |
|
| 76 | - * @param Certificate ...$cert |
|
| 77 | - * @return self |
|
| 78 | - */ |
|
| 79 | - public function withCertificates(Certificate ...$cert): self |
|
| 80 | - { |
|
| 81 | - $obj = clone $this; |
|
| 82 | - $obj->_certs = array_merge($obj->_certs, $cert); |
|
| 83 | - return $obj; |
|
| 84 | - } |
|
| 73 | + /** |
|
| 74 | + * Get self with certificates added. |
|
| 75 | + * |
|
| 76 | + * @param Certificate ...$cert |
|
| 77 | + * @return self |
|
| 78 | + */ |
|
| 79 | + public function withCertificates(Certificate ...$cert): self |
|
| 80 | + { |
|
| 81 | + $obj = clone $this; |
|
| 82 | + $obj->_certs = array_merge($obj->_certs, $cert); |
|
| 83 | + return $obj; |
|
| 84 | + } |
|
| 85 | 85 | |
| 86 | - /** |
|
| 87 | - * Get self with certificates from PEMBundle added. |
|
| 88 | - * |
|
| 89 | - * @param PEMBundle $pem_bundle |
|
| 90 | - * @return self |
|
| 91 | - */ |
|
| 92 | - public function withPEMBundle(PEMBundle $pem_bundle): self |
|
| 93 | - { |
|
| 94 | - $certs = $this->_certs; |
|
| 95 | - foreach ($pem_bundle as $pem) { |
|
| 96 | - $certs[] = Certificate::fromPEM($pem); |
|
| 97 | - } |
|
| 98 | - return new self(...$certs); |
|
| 99 | - } |
|
| 86 | + /** |
|
| 87 | + * Get self with certificates from PEMBundle added. |
|
| 88 | + * |
|
| 89 | + * @param PEMBundle $pem_bundle |
|
| 90 | + * @return self |
|
| 91 | + */ |
|
| 92 | + public function withPEMBundle(PEMBundle $pem_bundle): self |
|
| 93 | + { |
|
| 94 | + $certs = $this->_certs; |
|
| 95 | + foreach ($pem_bundle as $pem) { |
|
| 96 | + $certs[] = Certificate::fromPEM($pem); |
|
| 97 | + } |
|
| 98 | + return new self(...$certs); |
|
| 99 | + } |
|
| 100 | 100 | |
| 101 | - /** |
|
| 102 | - * Get self with single certificate from PEM added. |
|
| 103 | - * |
|
| 104 | - * @param PEM $pem |
|
| 105 | - * @return self |
|
| 106 | - */ |
|
| 107 | - public function withPEM(PEM $pem): self |
|
| 108 | - { |
|
| 109 | - $certs = $this->_certs; |
|
| 110 | - $certs[] = Certificate::fromPEM($pem); |
|
| 111 | - return new self(...$certs); |
|
| 112 | - } |
|
| 101 | + /** |
|
| 102 | + * Get self with single certificate from PEM added. |
|
| 103 | + * |
|
| 104 | + * @param PEM $pem |
|
| 105 | + * @return self |
|
| 106 | + */ |
|
| 107 | + public function withPEM(PEM $pem): self |
|
| 108 | + { |
|
| 109 | + $certs = $this->_certs; |
|
| 110 | + $certs[] = Certificate::fromPEM($pem); |
|
| 111 | + return new self(...$certs); |
|
| 112 | + } |
|
| 113 | 113 | |
| 114 | - /** |
|
| 115 | - * Check whether bundle contains a given certificate. |
|
| 116 | - * |
|
| 117 | - * @param Certificate $cert |
|
| 118 | - * @return bool |
|
| 119 | - */ |
|
| 120 | - public function contains(Certificate $cert): bool |
|
| 121 | - { |
|
| 122 | - $id = self::_getCertKeyId($cert); |
|
| 123 | - $map = $this->_getKeyIdMap(); |
|
| 124 | - if (!isset($map[$id])) { |
|
| 125 | - return false; |
|
| 126 | - } |
|
| 127 | - foreach ($map[$id] as $c) { |
|
| 128 | - /** @var Certificate $c */ |
|
| 129 | - if ($cert->equals($c)) { |
|
| 130 | - return true; |
|
| 131 | - } |
|
| 132 | - } |
|
| 133 | - return false; |
|
| 134 | - } |
|
| 114 | + /** |
|
| 115 | + * Check whether bundle contains a given certificate. |
|
| 116 | + * |
|
| 117 | + * @param Certificate $cert |
|
| 118 | + * @return bool |
|
| 119 | + */ |
|
| 120 | + public function contains(Certificate $cert): bool |
|
| 121 | + { |
|
| 122 | + $id = self::_getCertKeyId($cert); |
|
| 123 | + $map = $this->_getKeyIdMap(); |
|
| 124 | + if (!isset($map[$id])) { |
|
| 125 | + return false; |
|
| 126 | + } |
|
| 127 | + foreach ($map[$id] as $c) { |
|
| 128 | + /** @var Certificate $c */ |
|
| 129 | + if ($cert->equals($c)) { |
|
| 130 | + return true; |
|
| 131 | + } |
|
| 132 | + } |
|
| 133 | + return false; |
|
| 134 | + } |
|
| 135 | 135 | |
| 136 | - /** |
|
| 137 | - * Get all certificates that have given subject key identifier. |
|
| 138 | - * |
|
| 139 | - * @param string $id |
|
| 140 | - * @return Certificate[] |
|
| 141 | - */ |
|
| 142 | - public function allBySubjectKeyIdentifier(string $id): array |
|
| 143 | - { |
|
| 144 | - $map = $this->_getKeyIdMap(); |
|
| 145 | - if (!isset($map[$id])) { |
|
| 146 | - return array(); |
|
| 147 | - } |
|
| 148 | - return $map[$id]; |
|
| 149 | - } |
|
| 136 | + /** |
|
| 137 | + * Get all certificates that have given subject key identifier. |
|
| 138 | + * |
|
| 139 | + * @param string $id |
|
| 140 | + * @return Certificate[] |
|
| 141 | + */ |
|
| 142 | + public function allBySubjectKeyIdentifier(string $id): array |
|
| 143 | + { |
|
| 144 | + $map = $this->_getKeyIdMap(); |
|
| 145 | + if (!isset($map[$id])) { |
|
| 146 | + return array(); |
|
| 147 | + } |
|
| 148 | + return $map[$id]; |
|
| 149 | + } |
|
| 150 | 150 | |
| 151 | - /** |
|
| 152 | - * Get all certificates in a bundle. |
|
| 153 | - * |
|
| 154 | - * @return Certificate[] |
|
| 155 | - */ |
|
| 156 | - public function all(): array |
|
| 157 | - { |
|
| 158 | - return $this->_certs; |
|
| 159 | - } |
|
| 151 | + /** |
|
| 152 | + * Get all certificates in a bundle. |
|
| 153 | + * |
|
| 154 | + * @return Certificate[] |
|
| 155 | + */ |
|
| 156 | + public function all(): array |
|
| 157 | + { |
|
| 158 | + return $this->_certs; |
|
| 159 | + } |
|
| 160 | 160 | |
| 161 | - /** |
|
| 162 | - * Get certificate mapping by public key id. |
|
| 163 | - * |
|
| 164 | - * @return (Certificate[])[] |
|
| 165 | - */ |
|
| 166 | - private function _getKeyIdMap(): array |
|
| 167 | - { |
|
| 168 | - // lazily build mapping |
|
| 169 | - if (!isset($this->_keyIdMap)) { |
|
| 170 | - $this->_keyIdMap = array(); |
|
| 171 | - foreach ($this->_certs as $cert) { |
|
| 172 | - $id = self::_getCertKeyId($cert); |
|
| 173 | - if (!isset($this->_keyIdMap[$id])) { |
|
| 174 | - $this->_keyIdMap[$id] = array(); |
|
| 175 | - } |
|
| 176 | - array_push($this->_keyIdMap[$id], $cert); |
|
| 177 | - } |
|
| 178 | - } |
|
| 179 | - return $this->_keyIdMap; |
|
| 180 | - } |
|
| 161 | + /** |
|
| 162 | + * Get certificate mapping by public key id. |
|
| 163 | + * |
|
| 164 | + * @return (Certificate[])[] |
|
| 165 | + */ |
|
| 166 | + private function _getKeyIdMap(): array |
|
| 167 | + { |
|
| 168 | + // lazily build mapping |
|
| 169 | + if (!isset($this->_keyIdMap)) { |
|
| 170 | + $this->_keyIdMap = array(); |
|
| 171 | + foreach ($this->_certs as $cert) { |
|
| 172 | + $id = self::_getCertKeyId($cert); |
|
| 173 | + if (!isset($this->_keyIdMap[$id])) { |
|
| 174 | + $this->_keyIdMap[$id] = array(); |
|
| 175 | + } |
|
| 176 | + array_push($this->_keyIdMap[$id], $cert); |
|
| 177 | + } |
|
| 178 | + } |
|
| 179 | + return $this->_keyIdMap; |
|
| 180 | + } |
|
| 181 | 181 | |
| 182 | - /** |
|
| 183 | - * Get public key id for the certificate. |
|
| 184 | - * |
|
| 185 | - * @param Certificate $cert |
|
| 186 | - * @return string |
|
| 187 | - */ |
|
| 188 | - private static function _getCertKeyId(Certificate $cert): string |
|
| 189 | - { |
|
| 190 | - $exts = $cert->tbsCertificate()->extensions(); |
|
| 191 | - if ($exts->hasSubjectKeyIdentifier()) { |
|
| 192 | - return $exts->subjectKeyIdentifier()->keyIdentifier(); |
|
| 193 | - } |
|
| 194 | - return $cert->tbsCertificate() |
|
| 195 | - ->subjectPublicKeyInfo() |
|
| 196 | - ->keyIdentifier(); |
|
| 197 | - } |
|
| 182 | + /** |
|
| 183 | + * Get public key id for the certificate. |
|
| 184 | + * |
|
| 185 | + * @param Certificate $cert |
|
| 186 | + * @return string |
|
| 187 | + */ |
|
| 188 | + private static function _getCertKeyId(Certificate $cert): string |
|
| 189 | + { |
|
| 190 | + $exts = $cert->tbsCertificate()->extensions(); |
|
| 191 | + if ($exts->hasSubjectKeyIdentifier()) { |
|
| 192 | + return $exts->subjectKeyIdentifier()->keyIdentifier(); |
|
| 193 | + } |
|
| 194 | + return $cert->tbsCertificate() |
|
| 195 | + ->subjectPublicKeyInfo() |
|
| 196 | + ->keyIdentifier(); |
|
| 197 | + } |
|
| 198 | 198 | |
| 199 | - /** |
|
| 200 | - * |
|
| 201 | - * @see \Countable::count() |
|
| 202 | - * @return int |
|
| 203 | - */ |
|
| 204 | - public function count(): int |
|
| 205 | - { |
|
| 206 | - return count($this->_certs); |
|
| 207 | - } |
|
| 199 | + /** |
|
| 200 | + * |
|
| 201 | + * @see \Countable::count() |
|
| 202 | + * @return int |
|
| 203 | + */ |
|
| 204 | + public function count(): int |
|
| 205 | + { |
|
| 206 | + return count($this->_certs); |
|
| 207 | + } |
|
| 208 | 208 | |
| 209 | - /** |
|
| 210 | - * Get iterator for certificates. |
|
| 211 | - * |
|
| 212 | - * @see \IteratorAggregate::getIterator() |
|
| 213 | - * @return \ArrayIterator |
|
| 214 | - */ |
|
| 215 | - public function getIterator(): \ArrayIterator |
|
| 216 | - { |
|
| 217 | - return new \ArrayIterator($this->_certs); |
|
| 218 | - } |
|
| 209 | + /** |
|
| 210 | + * Get iterator for certificates. |
|
| 211 | + * |
|
| 212 | + * @see \IteratorAggregate::getIterator() |
|
| 213 | + * @return \ArrayIterator |
|
| 214 | + */ |
|
| 215 | + public function getIterator(): \ArrayIterator |
|
| 216 | + { |
|
| 217 | + return new \ArrayIterator($this->_certs); |
|
| 218 | + } |
|
| 219 | 219 | } |
@@ -47,7 +47,7 @@ discard block |
||
| 47 | 47 | public static function fromASN1(Sequence $seq): self |
| 48 | 48 | { |
| 49 | 49 | $extensions = array_map( |
| 50 | - function (UnspecifiedType $el) { |
|
| 50 | + function(UnspecifiedType $el) { |
|
| 51 | 51 | return Ext\Extension::fromASN1($el->asSequence()); |
| 52 | 52 | }, $seq->elements()); |
| 53 | 53 | return new self(...$extensions); |
@@ -62,7 +62,7 @@ discard block |
||
| 62 | 62 | { |
| 63 | 63 | $elements = array_values( |
| 64 | 64 | array_map( |
| 65 | - function ($ext) { |
|
| 65 | + function($ext) { |
|
| 66 | 66 | return $ext->toASN1(); |
| 67 | 67 | }, $this->_extensions)); |
| 68 | 68 | return new Sequence(...$elements); |
@@ -18,414 +18,414 @@ |
||
| 18 | 18 | */ |
| 19 | 19 | class Extensions implements \Countable, \IteratorAggregate |
| 20 | 20 | { |
| 21 | - /** |
|
| 22 | - * Extensions. |
|
| 23 | - * |
|
| 24 | - * @var Extension\Extension[] $_extensions |
|
| 25 | - */ |
|
| 26 | - protected $_extensions; |
|
| 27 | - |
|
| 28 | - /** |
|
| 29 | - * Constructor. |
|
| 30 | - * |
|
| 31 | - * @param Ext\Extension ...$extensions Extension objects |
|
| 32 | - */ |
|
| 33 | - public function __construct(Ext\Extension ...$extensions) |
|
| 34 | - { |
|
| 35 | - $this->_extensions = array(); |
|
| 36 | - foreach ($extensions as $ext) { |
|
| 37 | - $this->_extensions[$ext->oid()] = $ext; |
|
| 38 | - } |
|
| 39 | - } |
|
| 40 | - |
|
| 41 | - /** |
|
| 42 | - * Initialize from ASN.1. |
|
| 43 | - * |
|
| 44 | - * @param Sequence $seq |
|
| 45 | - * @return self |
|
| 46 | - */ |
|
| 47 | - public static function fromASN1(Sequence $seq): self |
|
| 48 | - { |
|
| 49 | - $extensions = array_map( |
|
| 50 | - function (UnspecifiedType $el) { |
|
| 51 | - return Ext\Extension::fromASN1($el->asSequence()); |
|
| 52 | - }, $seq->elements()); |
|
| 53 | - return new self(...$extensions); |
|
| 54 | - } |
|
| 55 | - |
|
| 56 | - /** |
|
| 57 | - * Generate ASN.1 structure. |
|
| 58 | - * |
|
| 59 | - * @return Sequence |
|
| 60 | - */ |
|
| 61 | - public function toASN1(): Sequence |
|
| 62 | - { |
|
| 63 | - $elements = array_values( |
|
| 64 | - array_map( |
|
| 65 | - function ($ext) { |
|
| 66 | - return $ext->toASN1(); |
|
| 67 | - }, $this->_extensions)); |
|
| 68 | - return new Sequence(...$elements); |
|
| 69 | - } |
|
| 70 | - |
|
| 71 | - /** |
|
| 72 | - * Get self with extensions added. |
|
| 73 | - * |
|
| 74 | - * @param Ext\Extension ...$exts One or more extensions to add |
|
| 75 | - * @return self |
|
| 76 | - */ |
|
| 77 | - public function withExtensions(Ext\Extension ...$exts): self |
|
| 78 | - { |
|
| 79 | - $obj = clone $this; |
|
| 80 | - foreach ($exts as $ext) { |
|
| 81 | - $obj->_extensions[$ext->oid()] = $ext; |
|
| 82 | - } |
|
| 83 | - return $obj; |
|
| 84 | - } |
|
| 85 | - |
|
| 86 | - /** |
|
| 87 | - * Check whether extension is present. |
|
| 88 | - * |
|
| 89 | - * @param string $oid Extensions OID |
|
| 90 | - * @return bool |
|
| 91 | - */ |
|
| 92 | - public function has(string $oid): bool |
|
| 93 | - { |
|
| 94 | - return isset($this->_extensions[$oid]); |
|
| 95 | - } |
|
| 96 | - |
|
| 97 | - /** |
|
| 98 | - * Get extension by OID. |
|
| 99 | - * |
|
| 100 | - * @param string $oid |
|
| 101 | - * @throws \LogicException If extension is not present |
|
| 102 | - * @return Extension\Extension |
|
| 103 | - */ |
|
| 104 | - public function get(string $oid): Ext\Extension |
|
| 105 | - { |
|
| 106 | - if (!$this->has($oid)) { |
|
| 107 | - throw new \LogicException("No extension by OID $oid."); |
|
| 108 | - } |
|
| 109 | - return $this->_extensions[$oid]; |
|
| 110 | - } |
|
| 111 | - |
|
| 112 | - /** |
|
| 113 | - * Check whether 'Authority Key Identifier' extension is present. |
|
| 114 | - * |
|
| 115 | - * @return bool |
|
| 116 | - */ |
|
| 117 | - public function hasAuthorityKeyIdentifier(): bool |
|
| 118 | - { |
|
| 119 | - return $this->has(Ext\Extension::OID_AUTHORITY_KEY_IDENTIFIER); |
|
| 120 | - } |
|
| 121 | - |
|
| 122 | - /** |
|
| 123 | - * Get 'Authority Key Identifier' extension. |
|
| 124 | - * |
|
| 125 | - * @throws \LogicException If extension is not present |
|
| 126 | - * @return \X509\Certificate\Extension\AuthorityKeyIdentifierExtension |
|
| 127 | - */ |
|
| 128 | - public function authorityKeyIdentifier(): Ext\AuthorityKeyIdentifierExtension |
|
| 129 | - { |
|
| 130 | - /** @var Extension\AuthorityKeyIdentifierExtension $keyIdentifier */ |
|
| 131 | - $keyIdentifier = $this->get(Ext\Extension::OID_AUTHORITY_KEY_IDENTIFIER); |
|
| 132 | - return $keyIdentifier; |
|
| 133 | - } |
|
| 134 | - |
|
| 135 | - /** |
|
| 136 | - * Check whether 'Subject Key Identifier' extension is present. |
|
| 137 | - * |
|
| 138 | - * @return bool |
|
| 139 | - */ |
|
| 140 | - public function hasSubjectKeyIdentifier(): bool |
|
| 141 | - { |
|
| 142 | - return $this->has(Ext\Extension::OID_SUBJECT_KEY_IDENTIFIER); |
|
| 143 | - } |
|
| 144 | - |
|
| 145 | - /** |
|
| 146 | - * Get 'Subject Key Identifier' extension. |
|
| 147 | - * |
|
| 148 | - * @throws \LogicException If extension is not present |
|
| 149 | - * @return \X509\Certificate\Extension\SubjectKeyIdentifierExtension |
|
| 150 | - */ |
|
| 151 | - public function subjectKeyIdentifier(): Ext\SubjectKeyIdentifierExtension |
|
| 152 | - { |
|
| 153 | - /** @var Extension\SubjectKeyIdentifierExtension $subjectKeyIdentifier */ |
|
| 154 | - $subjectKeyIdentifier = $this->get( |
|
| 155 | - Ext\Extension::OID_SUBJECT_KEY_IDENTIFIER); |
|
| 156 | - return $subjectKeyIdentifier; |
|
| 157 | - } |
|
| 158 | - |
|
| 159 | - /** |
|
| 160 | - * Check whether 'Key Usage' extension is present. |
|
| 161 | - * |
|
| 162 | - * @return bool |
|
| 163 | - */ |
|
| 164 | - public function hasKeyUsage(): bool |
|
| 165 | - { |
|
| 166 | - return $this->has(Ext\Extension::OID_KEY_USAGE); |
|
| 167 | - } |
|
| 168 | - |
|
| 169 | - /** |
|
| 170 | - * Get 'Key Usage' extension. |
|
| 171 | - * |
|
| 172 | - * @throws \LogicException If extension is not present |
|
| 173 | - * @return \X509\Certificate\Extension\KeyUsageExtension |
|
| 174 | - */ |
|
| 175 | - public function keyUsage(): Ext\KeyUsageExtension |
|
| 176 | - { |
|
| 177 | - /** @var Extension\KeyUsageExtension $keyUsage */ |
|
| 178 | - $keyUsage = $this->get(Ext\Extension::OID_KEY_USAGE); |
|
| 179 | - return $keyUsage; |
|
| 180 | - } |
|
| 181 | - |
|
| 182 | - /** |
|
| 183 | - * Check whether 'Certificate Policies' extension is present. |
|
| 184 | - * |
|
| 185 | - * @return bool |
|
| 186 | - */ |
|
| 187 | - public function hasCertificatePolicies(): bool |
|
| 188 | - { |
|
| 189 | - return $this->has(Ext\Extension::OID_CERTIFICATE_POLICIES); |
|
| 190 | - } |
|
| 191 | - |
|
| 192 | - /** |
|
| 193 | - * Get 'Certificate Policies' extension. |
|
| 194 | - * |
|
| 195 | - * @throws \LogicException If extension is not present |
|
| 196 | - * @return \X509\Certificate\Extension\CertificatePoliciesExtension |
|
| 197 | - */ |
|
| 198 | - public function certificatePolicies(): Ext\CertificatePoliciesExtension |
|
| 199 | - { |
|
| 200 | - /** @var Extension\CertificatePoliciesExtension $certPolicies */ |
|
| 201 | - $certPolicies = $this->get(Ext\Extension::OID_CERTIFICATE_POLICIES); |
|
| 202 | - return $certPolicies; |
|
| 203 | - } |
|
| 204 | - |
|
| 205 | - /** |
|
| 206 | - * Check whether 'Policy Mappings' extension is present. |
|
| 207 | - * |
|
| 208 | - * @return bool |
|
| 209 | - */ |
|
| 210 | - public function hasPolicyMappings(): bool |
|
| 211 | - { |
|
| 212 | - return $this->has(Ext\Extension::OID_POLICY_MAPPINGS); |
|
| 213 | - } |
|
| 214 | - |
|
| 215 | - /** |
|
| 216 | - * Get 'Policy Mappings' extension. |
|
| 217 | - * |
|
| 218 | - * @throws \LogicException If extension is not present |
|
| 219 | - * @return \X509\Certificate\Extension\PolicyMappingsExtension |
|
| 220 | - */ |
|
| 221 | - public function policyMappings(): Ext\PolicyMappingsExtension |
|
| 222 | - { |
|
| 223 | - /** @var Extension\PolicyMappingsExtension $policyMappings */ |
|
| 224 | - $policyMappings = $this->get(Ext\Extension::OID_POLICY_MAPPINGS); |
|
| 225 | - return $policyMappings; |
|
| 226 | - } |
|
| 227 | - |
|
| 228 | - /** |
|
| 229 | - * Check whether 'Subject Alternative Name' extension is present. |
|
| 230 | - * |
|
| 231 | - * @return bool |
|
| 232 | - */ |
|
| 233 | - public function hasSubjectAlternativeName(): bool |
|
| 234 | - { |
|
| 235 | - return $this->has(Ext\Extension::OID_SUBJECT_ALT_NAME); |
|
| 236 | - } |
|
| 237 | - |
|
| 238 | - /** |
|
| 239 | - * Get 'Subject Alternative Name' extension. |
|
| 240 | - * |
|
| 241 | - * @throws \LogicException If extension is not present |
|
| 242 | - * @return \X509\Certificate\Extension\SubjectAlternativeNameExtension |
|
| 243 | - */ |
|
| 244 | - public function subjectAlternativeName(): Ext\SubjectAlternativeNameExtension |
|
| 245 | - { |
|
| 246 | - /** @var Extension\SubjectAlternativeNameExtension $subjectAltName */ |
|
| 247 | - $subjectAltName = $this->get(Ext\Extension::OID_SUBJECT_ALT_NAME); |
|
| 248 | - return $subjectAltName; |
|
| 249 | - } |
|
| 250 | - |
|
| 251 | - /** |
|
| 252 | - * Check whether 'Issuer Alternative Name' extension is present. |
|
| 253 | - * |
|
| 254 | - * @return bool |
|
| 255 | - */ |
|
| 256 | - public function hasIssuerAlternativeName(): bool |
|
| 257 | - { |
|
| 258 | - return $this->has(Ext\Extension::OID_ISSUER_ALT_NAME); |
|
| 259 | - } |
|
| 260 | - |
|
| 261 | - /** |
|
| 262 | - * Get 'Issuer Alternative Name' extension. |
|
| 263 | - * |
|
| 264 | - * @return \X509\Certificate\Extension\IssuerAlternativeNameExtension |
|
| 265 | - */ |
|
| 266 | - public function issuerAlternativeName(): Ext\IssuerAlternativeNameExtension |
|
| 267 | - { |
|
| 268 | - /** @var Extension\IssuerAlternativeNameExtension $issuerAltName */ |
|
| 269 | - $issuerAltName = $this->get(Ext\Extension::OID_ISSUER_ALT_NAME); |
|
| 270 | - return $issuerAltName; |
|
| 271 | - } |
|
| 272 | - |
|
| 273 | - /** |
|
| 274 | - * Check whether 'Basic Constraints' extension is present. |
|
| 275 | - * |
|
| 276 | - * @return bool |
|
| 277 | - */ |
|
| 278 | - public function hasBasicConstraints(): bool |
|
| 279 | - { |
|
| 280 | - return $this->has(Ext\Extension::OID_BASIC_CONSTRAINTS); |
|
| 281 | - } |
|
| 282 | - |
|
| 283 | - /** |
|
| 284 | - * Get 'Basic Constraints' extension. |
|
| 285 | - * |
|
| 286 | - * @throws \LogicException If extension is not present |
|
| 287 | - * @return \X509\Certificate\Extension\BasicConstraintsExtension |
|
| 288 | - */ |
|
| 289 | - public function basicConstraints(): Ext\BasicConstraintsExtension |
|
| 290 | - { |
|
| 291 | - /** @var Extension\BasicConstraintsExtension $basicConstraints */ |
|
| 292 | - $basicConstraints = $this->get(Ext\Extension::OID_BASIC_CONSTRAINTS); |
|
| 293 | - return $basicConstraints; |
|
| 294 | - } |
|
| 295 | - |
|
| 296 | - /** |
|
| 297 | - * Check whether 'Name Constraints' extension is present. |
|
| 298 | - * |
|
| 299 | - * @return bool |
|
| 300 | - */ |
|
| 301 | - public function hasNameConstraints(): bool |
|
| 302 | - { |
|
| 303 | - return $this->has(Ext\Extension::OID_NAME_CONSTRAINTS); |
|
| 304 | - } |
|
| 305 | - |
|
| 306 | - /** |
|
| 307 | - * Get 'Name Constraints' extension. |
|
| 308 | - * |
|
| 309 | - * @throws \LogicException If extension is not present |
|
| 310 | - * @return \X509\Certificate\Extension\NameConstraintsExtension |
|
| 311 | - */ |
|
| 312 | - public function nameConstraints(): Ext\NameConstraintsExtension |
|
| 313 | - { |
|
| 314 | - /** @var Extension\NameConstraintsExtension $nameConstraints */ |
|
| 315 | - $nameConstraints = $this->get(Ext\Extension::OID_NAME_CONSTRAINTS); |
|
| 316 | - return $nameConstraints; |
|
| 317 | - } |
|
| 318 | - |
|
| 319 | - /** |
|
| 320 | - * Check whether 'Policy Constraints' extension is present. |
|
| 321 | - * |
|
| 322 | - * @return bool |
|
| 323 | - */ |
|
| 324 | - public function hasPolicyConstraints(): bool |
|
| 325 | - { |
|
| 326 | - return $this->has(Ext\Extension::OID_POLICY_CONSTRAINTS); |
|
| 327 | - } |
|
| 328 | - |
|
| 329 | - /** |
|
| 330 | - * Get 'Policy Constraints' extension. |
|
| 331 | - * |
|
| 332 | - * @throws \LogicException If extension is not present |
|
| 333 | - * @return \X509\Certificate\Extension\PolicyConstraintsExtension |
|
| 334 | - */ |
|
| 335 | - public function policyConstraints(): Ext\PolicyConstraintsExtension |
|
| 336 | - { |
|
| 337 | - /** @var Extension\PolicyConstraintsExtension $policyConstraints */ |
|
| 338 | - $policyConstraints = $this->get(Ext\Extension::OID_POLICY_CONSTRAINTS); |
|
| 339 | - return $policyConstraints; |
|
| 340 | - } |
|
| 341 | - |
|
| 342 | - /** |
|
| 343 | - * Check whether 'Extended Key Usage' extension is present. |
|
| 344 | - * |
|
| 345 | - * @return bool |
|
| 346 | - */ |
|
| 347 | - public function hasExtendedKeyUsage(): bool |
|
| 348 | - { |
|
| 349 | - return $this->has(Ext\Extension::OID_EXT_KEY_USAGE); |
|
| 350 | - } |
|
| 351 | - |
|
| 352 | - /** |
|
| 353 | - * Get 'Extended Key Usage' extension. |
|
| 354 | - * |
|
| 355 | - * @throws \LogicException If extension is not present |
|
| 356 | - * @return \X509\Certificate\Extension\ExtendedKeyUsageExtension |
|
| 357 | - */ |
|
| 358 | - public function extendedKeyUsage(): Ext\ExtendedKeyUsageExtension |
|
| 359 | - { |
|
| 360 | - /** @var Extension\ExtendedKeyUsageExtension $keyUsage */ |
|
| 361 | - $keyUsage = $this->get(Ext\Extension::OID_EXT_KEY_USAGE); |
|
| 362 | - return $keyUsage; |
|
| 363 | - } |
|
| 364 | - |
|
| 365 | - /** |
|
| 366 | - * Check whether 'CRL Distribution Points' extension is present. |
|
| 367 | - * |
|
| 368 | - * @return bool |
|
| 369 | - */ |
|
| 370 | - public function hasCRLDistributionPoints(): bool |
|
| 371 | - { |
|
| 372 | - return $this->has(Ext\Extension::OID_CRL_DISTRIBUTION_POINTS); |
|
| 373 | - } |
|
| 374 | - |
|
| 375 | - /** |
|
| 376 | - * Get 'CRL Distribution Points' extension. |
|
| 377 | - * |
|
| 378 | - * @throws \LogicException If extension is not present |
|
| 379 | - * @return \X509\Certificate\Extension\CRLDistributionPointsExtension |
|
| 380 | - */ |
|
| 381 | - public function crlDistributionPoints(): Ext\CRLDistributionPointsExtension |
|
| 382 | - { |
|
| 383 | - /** @var Extension\CRLDistributionPointsExtension $crlDist */ |
|
| 384 | - $crlDist = $this->get(Ext\Extension::OID_CRL_DISTRIBUTION_POINTS); |
|
| 385 | - return $crlDist; |
|
| 386 | - } |
|
| 387 | - |
|
| 388 | - /** |
|
| 389 | - * Check whether 'Inhibit anyPolicy' extension is present. |
|
| 390 | - * |
|
| 391 | - * @return bool |
|
| 392 | - */ |
|
| 393 | - public function hasInhibitAnyPolicy(): bool |
|
| 394 | - { |
|
| 395 | - return $this->has(Ext\Extension::OID_INHIBIT_ANY_POLICY); |
|
| 396 | - } |
|
| 397 | - |
|
| 398 | - /** |
|
| 399 | - * Get 'Inhibit anyPolicy' extension. |
|
| 400 | - * |
|
| 401 | - * @throws \LogicException If extension is not present |
|
| 402 | - * @return \X509\Certificate\Extension\InhibitAnyPolicyExtension |
|
| 403 | - */ |
|
| 404 | - public function inhibitAnyPolicy(): Ext\InhibitAnyPolicyExtension |
|
| 405 | - { |
|
| 406 | - /** @var Extension\InhibitAnyPolicyExtension $inhibitAny */ |
|
| 407 | - $inhibitAny = $this->get(Ext\Extension::OID_INHIBIT_ANY_POLICY); |
|
| 408 | - return $inhibitAny; |
|
| 409 | - } |
|
| 410 | - |
|
| 411 | - /** |
|
| 412 | - * |
|
| 413 | - * @see \Countable::count() |
|
| 414 | - * @return int |
|
| 415 | - */ |
|
| 416 | - public function count(): int |
|
| 417 | - { |
|
| 418 | - return count($this->_extensions); |
|
| 419 | - } |
|
| 420 | - |
|
| 421 | - /** |
|
| 422 | - * Get iterator for extensions. |
|
| 423 | - * |
|
| 424 | - * @see \IteratorAggregate::getIterator() |
|
| 425 | - * @return \Traversable |
|
| 426 | - */ |
|
| 427 | - public function getIterator(): \Traversable |
|
| 428 | - { |
|
| 429 | - return new \ArrayIterator($this->_extensions); |
|
| 430 | - } |
|
| 21 | + /** |
|
| 22 | + * Extensions. |
|
| 23 | + * |
|
| 24 | + * @var Extension\Extension[] $_extensions |
|
| 25 | + */ |
|
| 26 | + protected $_extensions; |
|
| 27 | + |
|
| 28 | + /** |
|
| 29 | + * Constructor. |
|
| 30 | + * |
|
| 31 | + * @param Ext\Extension ...$extensions Extension objects |
|
| 32 | + */ |
|
| 33 | + public function __construct(Ext\Extension ...$extensions) |
|
| 34 | + { |
|
| 35 | + $this->_extensions = array(); |
|
| 36 | + foreach ($extensions as $ext) { |
|
| 37 | + $this->_extensions[$ext->oid()] = $ext; |
|
| 38 | + } |
|
| 39 | + } |
|
| 40 | + |
|
| 41 | + /** |
|
| 42 | + * Initialize from ASN.1. |
|
| 43 | + * |
|
| 44 | + * @param Sequence $seq |
|
| 45 | + * @return self |
|
| 46 | + */ |
|
| 47 | + public static function fromASN1(Sequence $seq): self |
|
| 48 | + { |
|
| 49 | + $extensions = array_map( |
|
| 50 | + function (UnspecifiedType $el) { |
|
| 51 | + return Ext\Extension::fromASN1($el->asSequence()); |
|
| 52 | + }, $seq->elements()); |
|
| 53 | + return new self(...$extensions); |
|
| 54 | + } |
|
| 55 | + |
|
| 56 | + /** |
|
| 57 | + * Generate ASN.1 structure. |
|
| 58 | + * |
|
| 59 | + * @return Sequence |
|
| 60 | + */ |
|
| 61 | + public function toASN1(): Sequence |
|
| 62 | + { |
|
| 63 | + $elements = array_values( |
|
| 64 | + array_map( |
|
| 65 | + function ($ext) { |
|
| 66 | + return $ext->toASN1(); |
|
| 67 | + }, $this->_extensions)); |
|
| 68 | + return new Sequence(...$elements); |
|
| 69 | + } |
|
| 70 | + |
|
| 71 | + /** |
|
| 72 | + * Get self with extensions added. |
|
| 73 | + * |
|
| 74 | + * @param Ext\Extension ...$exts One or more extensions to add |
|
| 75 | + * @return self |
|
| 76 | + */ |
|
| 77 | + public function withExtensions(Ext\Extension ...$exts): self |
|
| 78 | + { |
|
| 79 | + $obj = clone $this; |
|
| 80 | + foreach ($exts as $ext) { |
|
| 81 | + $obj->_extensions[$ext->oid()] = $ext; |
|
| 82 | + } |
|
| 83 | + return $obj; |
|
| 84 | + } |
|
| 85 | + |
|
| 86 | + /** |
|
| 87 | + * Check whether extension is present. |
|
| 88 | + * |
|
| 89 | + * @param string $oid Extensions OID |
|
| 90 | + * @return bool |
|
| 91 | + */ |
|
| 92 | + public function has(string $oid): bool |
|
| 93 | + { |
|
| 94 | + return isset($this->_extensions[$oid]); |
|
| 95 | + } |
|
| 96 | + |
|
| 97 | + /** |
|
| 98 | + * Get extension by OID. |
|
| 99 | + * |
|
| 100 | + * @param string $oid |
|
| 101 | + * @throws \LogicException If extension is not present |
|
| 102 | + * @return Extension\Extension |
|
| 103 | + */ |
|
| 104 | + public function get(string $oid): Ext\Extension |
|
| 105 | + { |
|
| 106 | + if (!$this->has($oid)) { |
|
| 107 | + throw new \LogicException("No extension by OID $oid."); |
|
| 108 | + } |
|
| 109 | + return $this->_extensions[$oid]; |
|
| 110 | + } |
|
| 111 | + |
|
| 112 | + /** |
|
| 113 | + * Check whether 'Authority Key Identifier' extension is present. |
|
| 114 | + * |
|
| 115 | + * @return bool |
|
| 116 | + */ |
|
| 117 | + public function hasAuthorityKeyIdentifier(): bool |
|
| 118 | + { |
|
| 119 | + return $this->has(Ext\Extension::OID_AUTHORITY_KEY_IDENTIFIER); |
|
| 120 | + } |
|
| 121 | + |
|
| 122 | + /** |
|
| 123 | + * Get 'Authority Key Identifier' extension. |
|
| 124 | + * |
|
| 125 | + * @throws \LogicException If extension is not present |
|
| 126 | + * @return \X509\Certificate\Extension\AuthorityKeyIdentifierExtension |
|
| 127 | + */ |
|
| 128 | + public function authorityKeyIdentifier(): Ext\AuthorityKeyIdentifierExtension |
|
| 129 | + { |
|
| 130 | + /** @var Extension\AuthorityKeyIdentifierExtension $keyIdentifier */ |
|
| 131 | + $keyIdentifier = $this->get(Ext\Extension::OID_AUTHORITY_KEY_IDENTIFIER); |
|
| 132 | + return $keyIdentifier; |
|
| 133 | + } |
|
| 134 | + |
|
| 135 | + /** |
|
| 136 | + * Check whether 'Subject Key Identifier' extension is present. |
|
| 137 | + * |
|
| 138 | + * @return bool |
|
| 139 | + */ |
|
| 140 | + public function hasSubjectKeyIdentifier(): bool |
|
| 141 | + { |
|
| 142 | + return $this->has(Ext\Extension::OID_SUBJECT_KEY_IDENTIFIER); |
|
| 143 | + } |
|
| 144 | + |
|
| 145 | + /** |
|
| 146 | + * Get 'Subject Key Identifier' extension. |
|
| 147 | + * |
|
| 148 | + * @throws \LogicException If extension is not present |
|
| 149 | + * @return \X509\Certificate\Extension\SubjectKeyIdentifierExtension |
|
| 150 | + */ |
|
| 151 | + public function subjectKeyIdentifier(): Ext\SubjectKeyIdentifierExtension |
|
| 152 | + { |
|
| 153 | + /** @var Extension\SubjectKeyIdentifierExtension $subjectKeyIdentifier */ |
|
| 154 | + $subjectKeyIdentifier = $this->get( |
|
| 155 | + Ext\Extension::OID_SUBJECT_KEY_IDENTIFIER); |
|
| 156 | + return $subjectKeyIdentifier; |
|
| 157 | + } |
|
| 158 | + |
|
| 159 | + /** |
|
| 160 | + * Check whether 'Key Usage' extension is present. |
|
| 161 | + * |
|
| 162 | + * @return bool |
|
| 163 | + */ |
|
| 164 | + public function hasKeyUsage(): bool |
|
| 165 | + { |
|
| 166 | + return $this->has(Ext\Extension::OID_KEY_USAGE); |
|
| 167 | + } |
|
| 168 | + |
|
| 169 | + /** |
|
| 170 | + * Get 'Key Usage' extension. |
|
| 171 | + * |
|
| 172 | + * @throws \LogicException If extension is not present |
|
| 173 | + * @return \X509\Certificate\Extension\KeyUsageExtension |
|
| 174 | + */ |
|
| 175 | + public function keyUsage(): Ext\KeyUsageExtension |
|
| 176 | + { |
|
| 177 | + /** @var Extension\KeyUsageExtension $keyUsage */ |
|
| 178 | + $keyUsage = $this->get(Ext\Extension::OID_KEY_USAGE); |
|
| 179 | + return $keyUsage; |
|
| 180 | + } |
|
| 181 | + |
|
| 182 | + /** |
|
| 183 | + * Check whether 'Certificate Policies' extension is present. |
|
| 184 | + * |
|
| 185 | + * @return bool |
|
| 186 | + */ |
|
| 187 | + public function hasCertificatePolicies(): bool |
|
| 188 | + { |
|
| 189 | + return $this->has(Ext\Extension::OID_CERTIFICATE_POLICIES); |
|
| 190 | + } |
|
| 191 | + |
|
| 192 | + /** |
|
| 193 | + * Get 'Certificate Policies' extension. |
|
| 194 | + * |
|
| 195 | + * @throws \LogicException If extension is not present |
|
| 196 | + * @return \X509\Certificate\Extension\CertificatePoliciesExtension |
|
| 197 | + */ |
|
| 198 | + public function certificatePolicies(): Ext\CertificatePoliciesExtension |
|
| 199 | + { |
|
| 200 | + /** @var Extension\CertificatePoliciesExtension $certPolicies */ |
|
| 201 | + $certPolicies = $this->get(Ext\Extension::OID_CERTIFICATE_POLICIES); |
|
| 202 | + return $certPolicies; |
|
| 203 | + } |
|
| 204 | + |
|
| 205 | + /** |
|
| 206 | + * Check whether 'Policy Mappings' extension is present. |
|
| 207 | + * |
|
| 208 | + * @return bool |
|
| 209 | + */ |
|
| 210 | + public function hasPolicyMappings(): bool |
|
| 211 | + { |
|
| 212 | + return $this->has(Ext\Extension::OID_POLICY_MAPPINGS); |
|
| 213 | + } |
|
| 214 | + |
|
| 215 | + /** |
|
| 216 | + * Get 'Policy Mappings' extension. |
|
| 217 | + * |
|
| 218 | + * @throws \LogicException If extension is not present |
|
| 219 | + * @return \X509\Certificate\Extension\PolicyMappingsExtension |
|
| 220 | + */ |
|
| 221 | + public function policyMappings(): Ext\PolicyMappingsExtension |
|
| 222 | + { |
|
| 223 | + /** @var Extension\PolicyMappingsExtension $policyMappings */ |
|
| 224 | + $policyMappings = $this->get(Ext\Extension::OID_POLICY_MAPPINGS); |
|
| 225 | + return $policyMappings; |
|
| 226 | + } |
|
| 227 | + |
|
| 228 | + /** |
|
| 229 | + * Check whether 'Subject Alternative Name' extension is present. |
|
| 230 | + * |
|
| 231 | + * @return bool |
|
| 232 | + */ |
|
| 233 | + public function hasSubjectAlternativeName(): bool |
|
| 234 | + { |
|
| 235 | + return $this->has(Ext\Extension::OID_SUBJECT_ALT_NAME); |
|
| 236 | + } |
|
| 237 | + |
|
| 238 | + /** |
|
| 239 | + * Get 'Subject Alternative Name' extension. |
|
| 240 | + * |
|
| 241 | + * @throws \LogicException If extension is not present |
|
| 242 | + * @return \X509\Certificate\Extension\SubjectAlternativeNameExtension |
|
| 243 | + */ |
|
| 244 | + public function subjectAlternativeName(): Ext\SubjectAlternativeNameExtension |
|
| 245 | + { |
|
| 246 | + /** @var Extension\SubjectAlternativeNameExtension $subjectAltName */ |
|
| 247 | + $subjectAltName = $this->get(Ext\Extension::OID_SUBJECT_ALT_NAME); |
|
| 248 | + return $subjectAltName; |
|
| 249 | + } |
|
| 250 | + |
|
| 251 | + /** |
|
| 252 | + * Check whether 'Issuer Alternative Name' extension is present. |
|
| 253 | + * |
|
| 254 | + * @return bool |
|
| 255 | + */ |
|
| 256 | + public function hasIssuerAlternativeName(): bool |
|
| 257 | + { |
|
| 258 | + return $this->has(Ext\Extension::OID_ISSUER_ALT_NAME); |
|
| 259 | + } |
|
| 260 | + |
|
| 261 | + /** |
|
| 262 | + * Get 'Issuer Alternative Name' extension. |
|
| 263 | + * |
|
| 264 | + * @return \X509\Certificate\Extension\IssuerAlternativeNameExtension |
|
| 265 | + */ |
|
| 266 | + public function issuerAlternativeName(): Ext\IssuerAlternativeNameExtension |
|
| 267 | + { |
|
| 268 | + /** @var Extension\IssuerAlternativeNameExtension $issuerAltName */ |
|
| 269 | + $issuerAltName = $this->get(Ext\Extension::OID_ISSUER_ALT_NAME); |
|
| 270 | + return $issuerAltName; |
|
| 271 | + } |
|
| 272 | + |
|
| 273 | + /** |
|
| 274 | + * Check whether 'Basic Constraints' extension is present. |
|
| 275 | + * |
|
| 276 | + * @return bool |
|
| 277 | + */ |
|
| 278 | + public function hasBasicConstraints(): bool |
|
| 279 | + { |
|
| 280 | + return $this->has(Ext\Extension::OID_BASIC_CONSTRAINTS); |
|
| 281 | + } |
|
| 282 | + |
|
| 283 | + /** |
|
| 284 | + * Get 'Basic Constraints' extension. |
|
| 285 | + * |
|
| 286 | + * @throws \LogicException If extension is not present |
|
| 287 | + * @return \X509\Certificate\Extension\BasicConstraintsExtension |
|
| 288 | + */ |
|
| 289 | + public function basicConstraints(): Ext\BasicConstraintsExtension |
|
| 290 | + { |
|
| 291 | + /** @var Extension\BasicConstraintsExtension $basicConstraints */ |
|
| 292 | + $basicConstraints = $this->get(Ext\Extension::OID_BASIC_CONSTRAINTS); |
|
| 293 | + return $basicConstraints; |
|
| 294 | + } |
|
| 295 | + |
|
| 296 | + /** |
|
| 297 | + * Check whether 'Name Constraints' extension is present. |
|
| 298 | + * |
|
| 299 | + * @return bool |
|
| 300 | + */ |
|
| 301 | + public function hasNameConstraints(): bool |
|
| 302 | + { |
|
| 303 | + return $this->has(Ext\Extension::OID_NAME_CONSTRAINTS); |
|
| 304 | + } |
|
| 305 | + |
|
| 306 | + /** |
|
| 307 | + * Get 'Name Constraints' extension. |
|
| 308 | + * |
|
| 309 | + * @throws \LogicException If extension is not present |
|
| 310 | + * @return \X509\Certificate\Extension\NameConstraintsExtension |
|
| 311 | + */ |
|
| 312 | + public function nameConstraints(): Ext\NameConstraintsExtension |
|
| 313 | + { |
|
| 314 | + /** @var Extension\NameConstraintsExtension $nameConstraints */ |
|
| 315 | + $nameConstraints = $this->get(Ext\Extension::OID_NAME_CONSTRAINTS); |
|
| 316 | + return $nameConstraints; |
|
| 317 | + } |
|
| 318 | + |
|
| 319 | + /** |
|
| 320 | + * Check whether 'Policy Constraints' extension is present. |
|
| 321 | + * |
|
| 322 | + * @return bool |
|
| 323 | + */ |
|
| 324 | + public function hasPolicyConstraints(): bool |
|
| 325 | + { |
|
| 326 | + return $this->has(Ext\Extension::OID_POLICY_CONSTRAINTS); |
|
| 327 | + } |
|
| 328 | + |
|
| 329 | + /** |
|
| 330 | + * Get 'Policy Constraints' extension. |
|
| 331 | + * |
|
| 332 | + * @throws \LogicException If extension is not present |
|
| 333 | + * @return \X509\Certificate\Extension\PolicyConstraintsExtension |
|
| 334 | + */ |
|
| 335 | + public function policyConstraints(): Ext\PolicyConstraintsExtension |
|
| 336 | + { |
|
| 337 | + /** @var Extension\PolicyConstraintsExtension $policyConstraints */ |
|
| 338 | + $policyConstraints = $this->get(Ext\Extension::OID_POLICY_CONSTRAINTS); |
|
| 339 | + return $policyConstraints; |
|
| 340 | + } |
|
| 341 | + |
|
| 342 | + /** |
|
| 343 | + * Check whether 'Extended Key Usage' extension is present. |
|
| 344 | + * |
|
| 345 | + * @return bool |
|
| 346 | + */ |
|
| 347 | + public function hasExtendedKeyUsage(): bool |
|
| 348 | + { |
|
| 349 | + return $this->has(Ext\Extension::OID_EXT_KEY_USAGE); |
|
| 350 | + } |
|
| 351 | + |
|
| 352 | + /** |
|
| 353 | + * Get 'Extended Key Usage' extension. |
|
| 354 | + * |
|
| 355 | + * @throws \LogicException If extension is not present |
|
| 356 | + * @return \X509\Certificate\Extension\ExtendedKeyUsageExtension |
|
| 357 | + */ |
|
| 358 | + public function extendedKeyUsage(): Ext\ExtendedKeyUsageExtension |
|
| 359 | + { |
|
| 360 | + /** @var Extension\ExtendedKeyUsageExtension $keyUsage */ |
|
| 361 | + $keyUsage = $this->get(Ext\Extension::OID_EXT_KEY_USAGE); |
|
| 362 | + return $keyUsage; |
|
| 363 | + } |
|
| 364 | + |
|
| 365 | + /** |
|
| 366 | + * Check whether 'CRL Distribution Points' extension is present. |
|
| 367 | + * |
|
| 368 | + * @return bool |
|
| 369 | + */ |
|
| 370 | + public function hasCRLDistributionPoints(): bool |
|
| 371 | + { |
|
| 372 | + return $this->has(Ext\Extension::OID_CRL_DISTRIBUTION_POINTS); |
|
| 373 | + } |
|
| 374 | + |
|
| 375 | + /** |
|
| 376 | + * Get 'CRL Distribution Points' extension. |
|
| 377 | + * |
|
| 378 | + * @throws \LogicException If extension is not present |
|
| 379 | + * @return \X509\Certificate\Extension\CRLDistributionPointsExtension |
|
| 380 | + */ |
|
| 381 | + public function crlDistributionPoints(): Ext\CRLDistributionPointsExtension |
|
| 382 | + { |
|
| 383 | + /** @var Extension\CRLDistributionPointsExtension $crlDist */ |
|
| 384 | + $crlDist = $this->get(Ext\Extension::OID_CRL_DISTRIBUTION_POINTS); |
|
| 385 | + return $crlDist; |
|
| 386 | + } |
|
| 387 | + |
|
| 388 | + /** |
|
| 389 | + * Check whether 'Inhibit anyPolicy' extension is present. |
|
| 390 | + * |
|
| 391 | + * @return bool |
|
| 392 | + */ |
|
| 393 | + public function hasInhibitAnyPolicy(): bool |
|
| 394 | + { |
|
| 395 | + return $this->has(Ext\Extension::OID_INHIBIT_ANY_POLICY); |
|
| 396 | + } |
|
| 397 | + |
|
| 398 | + /** |
|
| 399 | + * Get 'Inhibit anyPolicy' extension. |
|
| 400 | + * |
|
| 401 | + * @throws \LogicException If extension is not present |
|
| 402 | + * @return \X509\Certificate\Extension\InhibitAnyPolicyExtension |
|
| 403 | + */ |
|
| 404 | + public function inhibitAnyPolicy(): Ext\InhibitAnyPolicyExtension |
|
| 405 | + { |
|
| 406 | + /** @var Extension\InhibitAnyPolicyExtension $inhibitAny */ |
|
| 407 | + $inhibitAny = $this->get(Ext\Extension::OID_INHIBIT_ANY_POLICY); |
|
| 408 | + return $inhibitAny; |
|
| 409 | + } |
|
| 410 | + |
|
| 411 | + /** |
|
| 412 | + * |
|
| 413 | + * @see \Countable::count() |
|
| 414 | + * @return int |
|
| 415 | + */ |
|
| 416 | + public function count(): int |
|
| 417 | + { |
|
| 418 | + return count($this->_extensions); |
|
| 419 | + } |
|
| 420 | + |
|
| 421 | + /** |
|
| 422 | + * Get iterator for extensions. |
|
| 423 | + * |
|
| 424 | + * @see \IteratorAggregate::getIterator() |
|
| 425 | + * @return \Traversable |
|
| 426 | + */ |
|
| 427 | + public function getIterator(): \Traversable |
|
| 428 | + { |
|
| 429 | + return new \ArrayIterator($this->_extensions); |
|
| 430 | + } |
|
| 431 | 431 | } |
@@ -19,235 +19,235 @@ |
||
| 19 | 19 | */ |
| 20 | 20 | class Certificate |
| 21 | 21 | { |
| 22 | - /** |
|
| 23 | - * "To be signed" certificate information. |
|
| 24 | - * |
|
| 25 | - * @var TBSCertificate $_tbsCertificate |
|
| 26 | - */ |
|
| 27 | - protected $_tbsCertificate; |
|
| 28 | - |
|
| 29 | - /** |
|
| 30 | - * Signature algorithm. |
|
| 31 | - * |
|
| 32 | - * @var SignatureAlgorithmIdentifier $_signatureAlgorithm |
|
| 33 | - */ |
|
| 34 | - protected $_signatureAlgorithm; |
|
| 35 | - |
|
| 36 | - /** |
|
| 37 | - * Signature value. |
|
| 38 | - * |
|
| 39 | - * @var Signature $_signatureValue |
|
| 40 | - */ |
|
| 41 | - protected $_signatureValue; |
|
| 42 | - |
|
| 43 | - /** |
|
| 44 | - * Constructor. |
|
| 45 | - * |
|
| 46 | - * @param TBSCertificate $tbsCert |
|
| 47 | - * @param SignatureAlgorithmIdentifier $algo |
|
| 48 | - * @param Signature $signature |
|
| 49 | - */ |
|
| 50 | - public function __construct(TBSCertificate $tbsCert, |
|
| 51 | - SignatureAlgorithmIdentifier $algo, Signature $signature) |
|
| 52 | - { |
|
| 53 | - $this->_tbsCertificate = $tbsCert; |
|
| 54 | - $this->_signatureAlgorithm = $algo; |
|
| 55 | - $this->_signatureValue = $signature; |
|
| 56 | - } |
|
| 57 | - |
|
| 58 | - /** |
|
| 59 | - * Initialize from ASN.1. |
|
| 60 | - * |
|
| 61 | - * @param Sequence $seq |
|
| 62 | - * @return self |
|
| 63 | - */ |
|
| 64 | - public static function fromASN1(Sequence $seq): self |
|
| 65 | - { |
|
| 66 | - $tbsCert = TBSCertificate::fromASN1($seq->at(0)->asSequence()); |
|
| 67 | - $algo = AlgorithmIdentifier::fromASN1($seq->at(1)->asSequence()); |
|
| 68 | - if (!$algo instanceof SignatureAlgorithmIdentifier) { |
|
| 69 | - throw new \UnexpectedValueException( |
|
| 70 | - "Unsupported signature algorithm " . $algo->oid() . "."); |
|
| 71 | - } |
|
| 72 | - $signature = Signature::fromSignatureData( |
|
| 73 | - $seq->at(2) |
|
| 74 | - ->asBitString() |
|
| 75 | - ->string(), $algo); |
|
| 76 | - return new self($tbsCert, $algo, $signature); |
|
| 77 | - } |
|
| 78 | - |
|
| 79 | - /** |
|
| 80 | - * Initialize from DER. |
|
| 81 | - * |
|
| 82 | - * @param string $data |
|
| 83 | - * @return self |
|
| 84 | - */ |
|
| 85 | - public static function fromDER(string $data): self |
|
| 86 | - { |
|
| 87 | - return self::fromASN1(Sequence::fromDER($data)); |
|
| 88 | - } |
|
| 89 | - |
|
| 90 | - /** |
|
| 91 | - * Initialize from PEM. |
|
| 92 | - * |
|
| 93 | - * @param PEM $pem |
|
| 94 | - * @throws \UnexpectedValueException |
|
| 95 | - * @return self |
|
| 96 | - */ |
|
| 97 | - public static function fromPEM(PEM $pem): self |
|
| 98 | - { |
|
| 99 | - if ($pem->type() != PEM::TYPE_CERTIFICATE) { |
|
| 100 | - throw new \UnexpectedValueException("Invalid PEM type."); |
|
| 101 | - } |
|
| 102 | - return self::fromDER($pem->data()); |
|
| 103 | - } |
|
| 104 | - |
|
| 105 | - /** |
|
| 106 | - * Get certificate information. |
|
| 107 | - * |
|
| 108 | - * @return TBSCertificate |
|
| 109 | - */ |
|
| 110 | - public function tbsCertificate(): TBSCertificate |
|
| 111 | - { |
|
| 112 | - return $this->_tbsCertificate; |
|
| 113 | - } |
|
| 114 | - |
|
| 115 | - /** |
|
| 116 | - * Get signature algorithm. |
|
| 117 | - * |
|
| 118 | - * @return SignatureAlgorithmIdentifier |
|
| 119 | - */ |
|
| 120 | - public function signatureAlgorithm(): SignatureAlgorithmIdentifier |
|
| 121 | - { |
|
| 122 | - return $this->_signatureAlgorithm; |
|
| 123 | - } |
|
| 124 | - |
|
| 125 | - /** |
|
| 126 | - * Get signature value. |
|
| 127 | - * |
|
| 128 | - * @return Signature |
|
| 129 | - */ |
|
| 130 | - public function signatureValue(): Signature |
|
| 131 | - { |
|
| 132 | - return $this->_signatureValue; |
|
| 133 | - } |
|
| 134 | - |
|
| 135 | - /** |
|
| 136 | - * Check whether certificate is self-issued. |
|
| 137 | - * |
|
| 138 | - * @return bool |
|
| 139 | - */ |
|
| 140 | - public function isSelfIssued(): bool |
|
| 141 | - { |
|
| 142 | - return $this->_tbsCertificate->subject()->equals( |
|
| 143 | - $this->_tbsCertificate->issuer()); |
|
| 144 | - } |
|
| 145 | - |
|
| 146 | - /** |
|
| 147 | - * Check whether certificate is semantically equal to another. |
|
| 148 | - * |
|
| 149 | - * @param Certificate $cert Certificate to compare to |
|
| 150 | - * @return bool |
|
| 151 | - */ |
|
| 152 | - public function equals(Certificate $cert): bool |
|
| 153 | - { |
|
| 154 | - return $this->_hasEqualSerialNumber($cert) && |
|
| 155 | - $this->_hasEqualPublicKey($cert) && $this->_hasEqualSubject($cert); |
|
| 156 | - } |
|
| 157 | - |
|
| 158 | - /** |
|
| 159 | - * Check whether certificate has serial number equal to another. |
|
| 160 | - * |
|
| 161 | - * @param Certificate $cert |
|
| 162 | - * @return bool |
|
| 163 | - */ |
|
| 164 | - private function _hasEqualSerialNumber(Certificate $cert): bool |
|
| 165 | - { |
|
| 166 | - $sn1 = $this->_tbsCertificate->serialNumber(); |
|
| 167 | - $sn2 = $cert->_tbsCertificate->serialNumber(); |
|
| 168 | - return $sn1 == $sn2; |
|
| 169 | - } |
|
| 170 | - |
|
| 171 | - /** |
|
| 172 | - * Check whether certificate has public key equal to another. |
|
| 173 | - * |
|
| 174 | - * @param Certificate $cert |
|
| 175 | - * @return bool |
|
| 176 | - */ |
|
| 177 | - private function _hasEqualPublicKey(Certificate $cert): bool |
|
| 178 | - { |
|
| 179 | - $kid1 = $this->_tbsCertificate->subjectPublicKeyInfo()->keyIdentifier(); |
|
| 180 | - $kid2 = $cert->_tbsCertificate->subjectPublicKeyInfo()->keyIdentifier(); |
|
| 181 | - return $kid1 == $kid2; |
|
| 182 | - } |
|
| 183 | - |
|
| 184 | - /** |
|
| 185 | - * Check whether certificate has subject equal to another. |
|
| 186 | - * |
|
| 187 | - * @param Certificate $cert |
|
| 188 | - * @return bool |
|
| 189 | - */ |
|
| 190 | - private function _hasEqualSubject(Certificate $cert): bool |
|
| 191 | - { |
|
| 192 | - $dn1 = $this->_tbsCertificate->subject(); |
|
| 193 | - $dn2 = $cert->_tbsCertificate->subject(); |
|
| 194 | - return $dn1->equals($dn2); |
|
| 195 | - } |
|
| 196 | - |
|
| 197 | - /** |
|
| 198 | - * Generate ASN.1 structure. |
|
| 199 | - * |
|
| 200 | - * @return Sequence |
|
| 201 | - */ |
|
| 202 | - public function toASN1(): Sequence |
|
| 203 | - { |
|
| 204 | - return new Sequence($this->_tbsCertificate->toASN1(), |
|
| 205 | - $this->_signatureAlgorithm->toASN1(), |
|
| 206 | - $this->_signatureValue->bitString()); |
|
| 207 | - } |
|
| 208 | - |
|
| 209 | - /** |
|
| 210 | - * Get certificate as a DER. |
|
| 211 | - * |
|
| 212 | - * @return string |
|
| 213 | - */ |
|
| 214 | - public function toDER(): string |
|
| 215 | - { |
|
| 216 | - return $this->toASN1()->toDER(); |
|
| 217 | - } |
|
| 218 | - |
|
| 219 | - /** |
|
| 220 | - * Get certificate as a PEM. |
|
| 221 | - * |
|
| 222 | - * @return PEM |
|
| 223 | - */ |
|
| 224 | - public function toPEM(): PEM |
|
| 225 | - { |
|
| 226 | - return new PEM(PEM::TYPE_CERTIFICATE, $this->toDER()); |
|
| 227 | - } |
|
| 228 | - |
|
| 229 | - /** |
|
| 230 | - * Verify certificate signature. |
|
| 231 | - * |
|
| 232 | - * @param PublicKeyInfo $pubkey_info Issuer's public key |
|
| 233 | - * @param Crypto|null $crypto Crypto engine, use default if not set |
|
| 234 | - * @return bool True if certificate signature is valid |
|
| 235 | - */ |
|
| 236 | - public function verify(PublicKeyInfo $pubkey_info, Crypto $crypto = null): bool |
|
| 237 | - { |
|
| 238 | - $crypto = $crypto ?: Crypto::getDefault(); |
|
| 239 | - $data = $this->_tbsCertificate->toASN1()->toDER(); |
|
| 240 | - return $crypto->verify($data, $this->_signatureValue, $pubkey_info, |
|
| 241 | - $this->_signatureAlgorithm); |
|
| 242 | - } |
|
| 243 | - |
|
| 244 | - /** |
|
| 245 | - * Get certificate as a PEM formatted string. |
|
| 246 | - * |
|
| 247 | - * @return string |
|
| 248 | - */ |
|
| 249 | - public function __toString() |
|
| 250 | - { |
|
| 251 | - return $this->toPEM()->string(); |
|
| 252 | - } |
|
| 22 | + /** |
|
| 23 | + * "To be signed" certificate information. |
|
| 24 | + * |
|
| 25 | + * @var TBSCertificate $_tbsCertificate |
|
| 26 | + */ |
|
| 27 | + protected $_tbsCertificate; |
|
| 28 | + |
|
| 29 | + /** |
|
| 30 | + * Signature algorithm. |
|
| 31 | + * |
|
| 32 | + * @var SignatureAlgorithmIdentifier $_signatureAlgorithm |
|
| 33 | + */ |
|
| 34 | + protected $_signatureAlgorithm; |
|
| 35 | + |
|
| 36 | + /** |
|
| 37 | + * Signature value. |
|
| 38 | + * |
|
| 39 | + * @var Signature $_signatureValue |
|
| 40 | + */ |
|
| 41 | + protected $_signatureValue; |
|
| 42 | + |
|
| 43 | + /** |
|
| 44 | + * Constructor. |
|
| 45 | + * |
|
| 46 | + * @param TBSCertificate $tbsCert |
|
| 47 | + * @param SignatureAlgorithmIdentifier $algo |
|
| 48 | + * @param Signature $signature |
|
| 49 | + */ |
|
| 50 | + public function __construct(TBSCertificate $tbsCert, |
|
| 51 | + SignatureAlgorithmIdentifier $algo, Signature $signature) |
|
| 52 | + { |
|
| 53 | + $this->_tbsCertificate = $tbsCert; |
|
| 54 | + $this->_signatureAlgorithm = $algo; |
|
| 55 | + $this->_signatureValue = $signature; |
|
| 56 | + } |
|
| 57 | + |
|
| 58 | + /** |
|
| 59 | + * Initialize from ASN.1. |
|
| 60 | + * |
|
| 61 | + * @param Sequence $seq |
|
| 62 | + * @return self |
|
| 63 | + */ |
|
| 64 | + public static function fromASN1(Sequence $seq): self |
|
| 65 | + { |
|
| 66 | + $tbsCert = TBSCertificate::fromASN1($seq->at(0)->asSequence()); |
|
| 67 | + $algo = AlgorithmIdentifier::fromASN1($seq->at(1)->asSequence()); |
|
| 68 | + if (!$algo instanceof SignatureAlgorithmIdentifier) { |
|
| 69 | + throw new \UnexpectedValueException( |
|
| 70 | + "Unsupported signature algorithm " . $algo->oid() . "."); |
|
| 71 | + } |
|
| 72 | + $signature = Signature::fromSignatureData( |
|
| 73 | + $seq->at(2) |
|
| 74 | + ->asBitString() |
|
| 75 | + ->string(), $algo); |
|
| 76 | + return new self($tbsCert, $algo, $signature); |
|
| 77 | + } |
|
| 78 | + |
|
| 79 | + /** |
|
| 80 | + * Initialize from DER. |
|
| 81 | + * |
|
| 82 | + * @param string $data |
|
| 83 | + * @return self |
|
| 84 | + */ |
|
| 85 | + public static function fromDER(string $data): self |
|
| 86 | + { |
|
| 87 | + return self::fromASN1(Sequence::fromDER($data)); |
|
| 88 | + } |
|
| 89 | + |
|
| 90 | + /** |
|
| 91 | + * Initialize from PEM. |
|
| 92 | + * |
|
| 93 | + * @param PEM $pem |
|
| 94 | + * @throws \UnexpectedValueException |
|
| 95 | + * @return self |
|
| 96 | + */ |
|
| 97 | + public static function fromPEM(PEM $pem): self |
|
| 98 | + { |
|
| 99 | + if ($pem->type() != PEM::TYPE_CERTIFICATE) { |
|
| 100 | + throw new \UnexpectedValueException("Invalid PEM type."); |
|
| 101 | + } |
|
| 102 | + return self::fromDER($pem->data()); |
|
| 103 | + } |
|
| 104 | + |
|
| 105 | + /** |
|
| 106 | + * Get certificate information. |
|
| 107 | + * |
|
| 108 | + * @return TBSCertificate |
|
| 109 | + */ |
|
| 110 | + public function tbsCertificate(): TBSCertificate |
|
| 111 | + { |
|
| 112 | + return $this->_tbsCertificate; |
|
| 113 | + } |
|
| 114 | + |
|
| 115 | + /** |
|
| 116 | + * Get signature algorithm. |
|
| 117 | + * |
|
| 118 | + * @return SignatureAlgorithmIdentifier |
|
| 119 | + */ |
|
| 120 | + public function signatureAlgorithm(): SignatureAlgorithmIdentifier |
|
| 121 | + { |
|
| 122 | + return $this->_signatureAlgorithm; |
|
| 123 | + } |
|
| 124 | + |
|
| 125 | + /** |
|
| 126 | + * Get signature value. |
|
| 127 | + * |
|
| 128 | + * @return Signature |
|
| 129 | + */ |
|
| 130 | + public function signatureValue(): Signature |
|
| 131 | + { |
|
| 132 | + return $this->_signatureValue; |
|
| 133 | + } |
|
| 134 | + |
|
| 135 | + /** |
|
| 136 | + * Check whether certificate is self-issued. |
|
| 137 | + * |
|
| 138 | + * @return bool |
|
| 139 | + */ |
|
| 140 | + public function isSelfIssued(): bool |
|
| 141 | + { |
|
| 142 | + return $this->_tbsCertificate->subject()->equals( |
|
| 143 | + $this->_tbsCertificate->issuer()); |
|
| 144 | + } |
|
| 145 | + |
|
| 146 | + /** |
|
| 147 | + * Check whether certificate is semantically equal to another. |
|
| 148 | + * |
|
| 149 | + * @param Certificate $cert Certificate to compare to |
|
| 150 | + * @return bool |
|
| 151 | + */ |
|
| 152 | + public function equals(Certificate $cert): bool |
|
| 153 | + { |
|
| 154 | + return $this->_hasEqualSerialNumber($cert) && |
|
| 155 | + $this->_hasEqualPublicKey($cert) && $this->_hasEqualSubject($cert); |
|
| 156 | + } |
|
| 157 | + |
|
| 158 | + /** |
|
| 159 | + * Check whether certificate has serial number equal to another. |
|
| 160 | + * |
|
| 161 | + * @param Certificate $cert |
|
| 162 | + * @return bool |
|
| 163 | + */ |
|
| 164 | + private function _hasEqualSerialNumber(Certificate $cert): bool |
|
| 165 | + { |
|
| 166 | + $sn1 = $this->_tbsCertificate->serialNumber(); |
|
| 167 | + $sn2 = $cert->_tbsCertificate->serialNumber(); |
|
| 168 | + return $sn1 == $sn2; |
|
| 169 | + } |
|
| 170 | + |
|
| 171 | + /** |
|
| 172 | + * Check whether certificate has public key equal to another. |
|
| 173 | + * |
|
| 174 | + * @param Certificate $cert |
|
| 175 | + * @return bool |
|
| 176 | + */ |
|
| 177 | + private function _hasEqualPublicKey(Certificate $cert): bool |
|
| 178 | + { |
|
| 179 | + $kid1 = $this->_tbsCertificate->subjectPublicKeyInfo()->keyIdentifier(); |
|
| 180 | + $kid2 = $cert->_tbsCertificate->subjectPublicKeyInfo()->keyIdentifier(); |
|
| 181 | + return $kid1 == $kid2; |
|
| 182 | + } |
|
| 183 | + |
|
| 184 | + /** |
|
| 185 | + * Check whether certificate has subject equal to another. |
|
| 186 | + * |
|
| 187 | + * @param Certificate $cert |
|
| 188 | + * @return bool |
|
| 189 | + */ |
|
| 190 | + private function _hasEqualSubject(Certificate $cert): bool |
|
| 191 | + { |
|
| 192 | + $dn1 = $this->_tbsCertificate->subject(); |
|
| 193 | + $dn2 = $cert->_tbsCertificate->subject(); |
|
| 194 | + return $dn1->equals($dn2); |
|
| 195 | + } |
|
| 196 | + |
|
| 197 | + /** |
|
| 198 | + * Generate ASN.1 structure. |
|
| 199 | + * |
|
| 200 | + * @return Sequence |
|
| 201 | + */ |
|
| 202 | + public function toASN1(): Sequence |
|
| 203 | + { |
|
| 204 | + return new Sequence($this->_tbsCertificate->toASN1(), |
|
| 205 | + $this->_signatureAlgorithm->toASN1(), |
|
| 206 | + $this->_signatureValue->bitString()); |
|
| 207 | + } |
|
| 208 | + |
|
| 209 | + /** |
|
| 210 | + * Get certificate as a DER. |
|
| 211 | + * |
|
| 212 | + * @return string |
|
| 213 | + */ |
|
| 214 | + public function toDER(): string |
|
| 215 | + { |
|
| 216 | + return $this->toASN1()->toDER(); |
|
| 217 | + } |
|
| 218 | + |
|
| 219 | + /** |
|
| 220 | + * Get certificate as a PEM. |
|
| 221 | + * |
|
| 222 | + * @return PEM |
|
| 223 | + */ |
|
| 224 | + public function toPEM(): PEM |
|
| 225 | + { |
|
| 226 | + return new PEM(PEM::TYPE_CERTIFICATE, $this->toDER()); |
|
| 227 | + } |
|
| 228 | + |
|
| 229 | + /** |
|
| 230 | + * Verify certificate signature. |
|
| 231 | + * |
|
| 232 | + * @param PublicKeyInfo $pubkey_info Issuer's public key |
|
| 233 | + * @param Crypto|null $crypto Crypto engine, use default if not set |
|
| 234 | + * @return bool True if certificate signature is valid |
|
| 235 | + */ |
|
| 236 | + public function verify(PublicKeyInfo $pubkey_info, Crypto $crypto = null): bool |
|
| 237 | + { |
|
| 238 | + $crypto = $crypto ?: Crypto::getDefault(); |
|
| 239 | + $data = $this->_tbsCertificate->toASN1()->toDER(); |
|
| 240 | + return $crypto->verify($data, $this->_signatureValue, $pubkey_info, |
|
| 241 | + $this->_signatureAlgorithm); |
|
| 242 | + } |
|
| 243 | + |
|
| 244 | + /** |
|
| 245 | + * Get certificate as a PEM formatted string. |
|
| 246 | + * |
|
| 247 | + * @return string |
|
| 248 | + */ |
|
| 249 | + public function __toString() |
|
| 250 | + { |
|
| 251 | + return $this->toPEM()->string(); |
|
| 252 | + } |
|
| 253 | 253 | } |
@@ -27,610 +27,610 @@ |
||
| 27 | 27 | */ |
| 28 | 28 | class TBSCertificate |
| 29 | 29 | { |
| 30 | - // Certificate version enumerations |
|
| 31 | - const VERSION_1 = 0; |
|
| 32 | - const VERSION_2 = 1; |
|
| 33 | - const VERSION_3 = 2; |
|
| 34 | - |
|
| 35 | - /** |
|
| 36 | - * Certificate version. |
|
| 37 | - * |
|
| 38 | - * @var int|null |
|
| 39 | - */ |
|
| 40 | - protected $_version; |
|
| 41 | - |
|
| 42 | - /** |
|
| 43 | - * Serial number. |
|
| 44 | - * |
|
| 45 | - * @var string|null |
|
| 46 | - */ |
|
| 47 | - protected $_serialNumber; |
|
| 48 | - |
|
| 49 | - /** |
|
| 50 | - * Signature algorithm. |
|
| 51 | - * |
|
| 52 | - * @var SignatureAlgorithmIdentifier|null |
|
| 53 | - */ |
|
| 54 | - protected $_signature; |
|
| 55 | - |
|
| 56 | - /** |
|
| 57 | - * Certificate issuer. |
|
| 58 | - * |
|
| 59 | - * @var Name $_issuer |
|
| 60 | - */ |
|
| 61 | - protected $_issuer; |
|
| 62 | - |
|
| 63 | - /** |
|
| 64 | - * Certificate validity period. |
|
| 65 | - * |
|
| 66 | - * @var Validity $_validity |
|
| 67 | - */ |
|
| 68 | - protected $_validity; |
|
| 69 | - |
|
| 70 | - /** |
|
| 71 | - * Certificate subject. |
|
| 72 | - * |
|
| 73 | - * @var Name $_subject |
|
| 74 | - */ |
|
| 75 | - protected $_subject; |
|
| 76 | - |
|
| 77 | - /** |
|
| 78 | - * Subject public key. |
|
| 79 | - * |
|
| 80 | - * @var PublicKeyInfo $_subjectPublicKeyInfo |
|
| 81 | - */ |
|
| 82 | - protected $_subjectPublicKeyInfo; |
|
| 83 | - |
|
| 84 | - /** |
|
| 85 | - * Issuer unique identifier. |
|
| 86 | - * |
|
| 87 | - * @var UniqueIdentifier|null $_issuerUniqueID |
|
| 88 | - */ |
|
| 89 | - protected $_issuerUniqueID; |
|
| 90 | - |
|
| 91 | - /** |
|
| 92 | - * Subject unique identifier. |
|
| 93 | - * |
|
| 94 | - * @var UniqueIdentifier|null $_subjectUniqueID |
|
| 95 | - */ |
|
| 96 | - protected $_subjectUniqueID; |
|
| 97 | - |
|
| 98 | - /** |
|
| 99 | - * Extensions. |
|
| 100 | - * |
|
| 101 | - * @var Extensions $_extensions |
|
| 102 | - */ |
|
| 103 | - protected $_extensions; |
|
| 104 | - |
|
| 105 | - /** |
|
| 106 | - * Constructor. |
|
| 107 | - * |
|
| 108 | - * @param Name $subject Certificate subject |
|
| 109 | - * @param PublicKeyInfo $pki Subject public key |
|
| 110 | - * @param Name $issuer Certificate issuer |
|
| 111 | - * @param Validity $validity Validity period |
|
| 112 | - */ |
|
| 113 | - public function __construct(Name $subject, PublicKeyInfo $pki, Name $issuer, |
|
| 114 | - Validity $validity) |
|
| 115 | - { |
|
| 116 | - $this->_subject = $subject; |
|
| 117 | - $this->_subjectPublicKeyInfo = $pki; |
|
| 118 | - $this->_issuer = $issuer; |
|
| 119 | - $this->_validity = $validity; |
|
| 120 | - $this->_extensions = new Extensions(); |
|
| 121 | - } |
|
| 122 | - |
|
| 123 | - /** |
|
| 124 | - * Initialize from ASN.1. |
|
| 125 | - * |
|
| 126 | - * @param Sequence $seq |
|
| 127 | - * @return self |
|
| 128 | - */ |
|
| 129 | - public static function fromASN1(Sequence $seq): self |
|
| 130 | - { |
|
| 131 | - $idx = 0; |
|
| 132 | - if ($seq->hasTagged(0)) { |
|
| 133 | - $idx++; |
|
| 134 | - $version = $seq->getTagged(0) |
|
| 135 | - ->asExplicit() |
|
| 136 | - ->asInteger() |
|
| 137 | - ->intNumber(); |
|
| 138 | - } else { |
|
| 139 | - $version = self::VERSION_1; |
|
| 140 | - } |
|
| 141 | - $serial = $seq->at($idx++) |
|
| 142 | - ->asInteger() |
|
| 143 | - ->number(); |
|
| 144 | - $algo = AlgorithmIdentifier::fromASN1($seq->at($idx++)->asSequence()); |
|
| 145 | - if (!$algo instanceof SignatureAlgorithmIdentifier) { |
|
| 146 | - throw new \UnexpectedValueException( |
|
| 147 | - "Unsupported signature algorithm " . $algo->name() . "."); |
|
| 148 | - } |
|
| 149 | - $issuer = Name::fromASN1($seq->at($idx++)->asSequence()); |
|
| 150 | - $validity = Validity::fromASN1($seq->at($idx++)->asSequence()); |
|
| 151 | - $subject = Name::fromASN1($seq->at($idx++)->asSequence()); |
|
| 152 | - $pki = PublicKeyInfo::fromASN1($seq->at($idx++)->asSequence()); |
|
| 153 | - $tbs_cert = new self($subject, $pki, $issuer, $validity); |
|
| 154 | - $tbs_cert->_version = $version; |
|
| 155 | - $tbs_cert->_serialNumber = $serial; |
|
| 156 | - $tbs_cert->_signature = $algo; |
|
| 157 | - if ($seq->hasTagged(1)) { |
|
| 158 | - $tbs_cert->_issuerUniqueID = UniqueIdentifier::fromASN1( |
|
| 159 | - $seq->getTagged(1) |
|
| 160 | - ->asImplicit(Element::TYPE_BIT_STRING) |
|
| 161 | - ->asBitString()); |
|
| 162 | - } |
|
| 163 | - if ($seq->hasTagged(2)) { |
|
| 164 | - $tbs_cert->_subjectUniqueID = UniqueIdentifier::fromASN1( |
|
| 165 | - $seq->getTagged(2) |
|
| 166 | - ->asImplicit(Element::TYPE_BIT_STRING) |
|
| 167 | - ->asBitString()); |
|
| 168 | - } |
|
| 169 | - if ($seq->hasTagged(3)) { |
|
| 170 | - $tbs_cert->_extensions = Extensions::fromASN1( |
|
| 171 | - $seq->getTagged(3) |
|
| 172 | - ->asExplicit() |
|
| 173 | - ->asSequence()); |
|
| 174 | - } |
|
| 175 | - return $tbs_cert; |
|
| 176 | - } |
|
| 177 | - |
|
| 178 | - /** |
|
| 179 | - * Initialize from certification request. |
|
| 180 | - * |
|
| 181 | - * Note that signature is not verified and must be done by the caller. |
|
| 182 | - * |
|
| 183 | - * @param CertificationRequest $cr |
|
| 184 | - * @return self |
|
| 185 | - */ |
|
| 186 | - public static function fromCSR(CertificationRequest $cr): self |
|
| 187 | - { |
|
| 188 | - $cri = $cr->certificationRequestInfo(); |
|
| 189 | - $tbs_cert = new self($cri->subject(), $cri->subjectPKInfo(), new Name(), |
|
| 190 | - Validity::fromStrings(null, null)); |
|
| 191 | - // if CSR has Extension Request attribute |
|
| 192 | - if ($cri->hasAttributes()) { |
|
| 193 | - $attribs = $cri->attributes(); |
|
| 194 | - if ($attribs->hasExtensionRequest()) { |
|
| 195 | - $tbs_cert = $tbs_cert->withExtensions( |
|
| 196 | - $attribs->extensionRequest() |
|
| 197 | - ->extensions()); |
|
| 198 | - } |
|
| 199 | - } |
|
| 200 | - // add Subject Key Identifier extension |
|
| 201 | - $tbs_cert = $tbs_cert->withAdditionalExtensions( |
|
| 202 | - new SubjectKeyIdentifierExtension(false, |
|
| 203 | - $cri->subjectPKInfo() |
|
| 204 | - ->keyIdentifier())); |
|
| 205 | - return $tbs_cert; |
|
| 206 | - } |
|
| 207 | - |
|
| 208 | - /** |
|
| 209 | - * Get self with fields set from the issuer's certificate. |
|
| 210 | - * |
|
| 211 | - * Issuer shall be set to issuing certificate's subject. |
|
| 212 | - * Authority key identifier extensions shall be added with a key identifier |
|
| 213 | - * set to issuing certificate's public key identifier. |
|
| 214 | - * |
|
| 215 | - * @param Certificate $cert Issuing party's certificate |
|
| 216 | - * @return self |
|
| 217 | - */ |
|
| 218 | - public function withIssuerCertificate(Certificate $cert): self |
|
| 219 | - { |
|
| 220 | - $obj = clone $this; |
|
| 221 | - // set issuer DN from cert's subject |
|
| 222 | - $obj->_issuer = $cert->tbsCertificate()->subject(); |
|
| 223 | - // add authority key identifier extension |
|
| 224 | - $key_id = $cert->tbsCertificate() |
|
| 225 | - ->subjectPublicKeyInfo() |
|
| 226 | - ->keyIdentifier(); |
|
| 227 | - $obj->_extensions = $obj->_extensions->withExtensions( |
|
| 228 | - new AuthorityKeyIdentifierExtension(false, $key_id)); |
|
| 229 | - return $obj; |
|
| 230 | - } |
|
| 231 | - |
|
| 232 | - /** |
|
| 233 | - * Get self with given version. |
|
| 234 | - * |
|
| 235 | - * If version is not set, appropriate version is automatically |
|
| 236 | - * determined during signing. |
|
| 237 | - * |
|
| 238 | - * @param int $version |
|
| 239 | - * @return self |
|
| 240 | - */ |
|
| 241 | - public function withVersion(int $version): self |
|
| 242 | - { |
|
| 243 | - $obj = clone $this; |
|
| 244 | - $obj->_version = $version; |
|
| 245 | - return $obj; |
|
| 246 | - } |
|
| 247 | - |
|
| 248 | - /** |
|
| 249 | - * Get self with given serial number. |
|
| 250 | - * |
|
| 251 | - * @param int|string $serial Base 10 number |
|
| 252 | - * @return self |
|
| 253 | - */ |
|
| 254 | - public function withSerialNumber($serial): self |
|
| 255 | - { |
|
| 256 | - $obj = clone $this; |
|
| 257 | - $obj->_serialNumber = strval($serial); |
|
| 258 | - return $obj; |
|
| 259 | - } |
|
| 260 | - |
|
| 261 | - /** |
|
| 262 | - * Get self with random positive serial number. |
|
| 263 | - * |
|
| 264 | - * @param int $size Number of random bytes |
|
| 265 | - * @return self |
|
| 266 | - */ |
|
| 267 | - public function withRandomSerialNumber(int $size = 16): self |
|
| 268 | - { |
|
| 269 | - // ensure that first byte is always non-zero and having first bit unset |
|
| 270 | - $num = gmp_init(mt_rand(1, 0x7f), 10); |
|
| 271 | - for ($i = 1; $i < $size; ++$i) { |
|
| 272 | - $num <<= 8; |
|
| 273 | - $num += mt_rand(0, 0xff); |
|
| 274 | - } |
|
| 275 | - return $this->withSerialNumber(gmp_strval($num, 10)); |
|
| 276 | - } |
|
| 277 | - |
|
| 278 | - /** |
|
| 279 | - * Get self with given signature algorithm. |
|
| 280 | - * |
|
| 281 | - * @param SignatureAlgorithmIdentifier $algo |
|
| 282 | - * @return self |
|
| 283 | - */ |
|
| 284 | - public function withSignature(SignatureAlgorithmIdentifier $algo): self |
|
| 285 | - { |
|
| 286 | - $obj = clone $this; |
|
| 287 | - $obj->_signature = $algo; |
|
| 288 | - return $obj; |
|
| 289 | - } |
|
| 290 | - |
|
| 291 | - /** |
|
| 292 | - * Get self with given issuer. |
|
| 293 | - * |
|
| 294 | - * @param Name $issuer |
|
| 295 | - * @return self |
|
| 296 | - */ |
|
| 297 | - public function withIssuer(Name $issuer): self |
|
| 298 | - { |
|
| 299 | - $obj = clone $this; |
|
| 300 | - $obj->_issuer = $issuer; |
|
| 301 | - return $obj; |
|
| 302 | - } |
|
| 303 | - |
|
| 304 | - /** |
|
| 305 | - * Get self with given validity. |
|
| 306 | - * |
|
| 307 | - * @param Validity $validity |
|
| 308 | - * @return self |
|
| 309 | - */ |
|
| 310 | - public function withValidity(Validity $validity): self |
|
| 311 | - { |
|
| 312 | - $obj = clone $this; |
|
| 313 | - $obj->_validity = $validity; |
|
| 314 | - return $obj; |
|
| 315 | - } |
|
| 316 | - |
|
| 317 | - /** |
|
| 318 | - * Get self with given subject. |
|
| 319 | - * |
|
| 320 | - * @param Name $subject |
|
| 321 | - * @return self |
|
| 322 | - */ |
|
| 323 | - public function withSubject(Name $subject): self |
|
| 324 | - { |
|
| 325 | - $obj = clone $this; |
|
| 326 | - $obj->_subject = $subject; |
|
| 327 | - return $obj; |
|
| 328 | - } |
|
| 329 | - |
|
| 330 | - /** |
|
| 331 | - * Get self with given subject public key info. |
|
| 332 | - * |
|
| 333 | - * @param PublicKeyInfo $pub_key_info |
|
| 334 | - * @return self |
|
| 335 | - */ |
|
| 336 | - public function withSubjectPublicKeyInfo(PublicKeyInfo $pub_key_info): self |
|
| 337 | - { |
|
| 338 | - $obj = clone $this; |
|
| 339 | - $obj->_subjectPublicKeyInfo = $pub_key_info; |
|
| 340 | - return $obj; |
|
| 341 | - } |
|
| 342 | - |
|
| 343 | - /** |
|
| 344 | - * Get self with issuer unique ID. |
|
| 345 | - * |
|
| 346 | - * @param UniqueIdentifier $id |
|
| 347 | - * @return self |
|
| 348 | - */ |
|
| 349 | - public function withIssuerUniqueID(UniqueIdentifier $id): self |
|
| 350 | - { |
|
| 351 | - $obj = clone $this; |
|
| 352 | - $obj->_issuerUniqueID = $id; |
|
| 353 | - return $obj; |
|
| 354 | - } |
|
| 355 | - |
|
| 356 | - /** |
|
| 357 | - * Get self with subject unique ID. |
|
| 358 | - * |
|
| 359 | - * @param UniqueIdentifier $id |
|
| 360 | - * @return self |
|
| 361 | - */ |
|
| 362 | - public function withSubjectUniqueID(UniqueIdentifier $id): self |
|
| 363 | - { |
|
| 364 | - $obj = clone $this; |
|
| 365 | - $obj->_subjectUniqueID = $id; |
|
| 366 | - return $obj; |
|
| 367 | - } |
|
| 368 | - |
|
| 369 | - /** |
|
| 370 | - * Get self with given extensions. |
|
| 371 | - * |
|
| 372 | - * @param Extensions $extensions |
|
| 373 | - * @return self |
|
| 374 | - */ |
|
| 375 | - public function withExtensions(Extensions $extensions): self |
|
| 376 | - { |
|
| 377 | - $obj = clone $this; |
|
| 378 | - $obj->_extensions = $extensions; |
|
| 379 | - return $obj; |
|
| 380 | - } |
|
| 381 | - |
|
| 382 | - /** |
|
| 383 | - * Get self with extensions added. |
|
| 384 | - * |
|
| 385 | - * @param Extension ...$exts One or more Extension objects |
|
| 386 | - * @return self |
|
| 387 | - */ |
|
| 388 | - public function withAdditionalExtensions(Extension ...$exts): self |
|
| 389 | - { |
|
| 390 | - $obj = clone $this; |
|
| 391 | - $obj->_extensions = $obj->_extensions->withExtensions(...$exts); |
|
| 392 | - return $obj; |
|
| 393 | - } |
|
| 394 | - |
|
| 395 | - /** |
|
| 396 | - * Check whether version is set. |
|
| 397 | - * |
|
| 398 | - * @return bool |
|
| 399 | - */ |
|
| 400 | - public function hasVersion(): bool |
|
| 401 | - { |
|
| 402 | - return isset($this->_version); |
|
| 403 | - } |
|
| 404 | - |
|
| 405 | - /** |
|
| 406 | - * Get certificate version. |
|
| 407 | - * |
|
| 408 | - * @return int |
|
| 409 | - */ |
|
| 410 | - public function version(): int |
|
| 411 | - { |
|
| 412 | - if (!$this->hasVersion()) { |
|
| 413 | - throw new \LogicException("version not set."); |
|
| 414 | - } |
|
| 415 | - return $this->_version; |
|
| 416 | - } |
|
| 417 | - |
|
| 418 | - /** |
|
| 419 | - * Check whether serial number is set. |
|
| 420 | - * |
|
| 421 | - * @return bool |
|
| 422 | - */ |
|
| 423 | - public function hasSerialNumber(): bool |
|
| 424 | - { |
|
| 425 | - return isset($this->_serialNumber); |
|
| 426 | - } |
|
| 427 | - |
|
| 428 | - /** |
|
| 429 | - * Get serial number. |
|
| 430 | - * |
|
| 431 | - * @return string Base 10 integer |
|
| 432 | - */ |
|
| 433 | - public function serialNumber(): string |
|
| 434 | - { |
|
| 435 | - if (!$this->hasSerialNumber()) { |
|
| 436 | - throw new \LogicException("serialNumber not set."); |
|
| 437 | - } |
|
| 438 | - return $this->_serialNumber; |
|
| 439 | - } |
|
| 440 | - |
|
| 441 | - /** |
|
| 442 | - * Check whether signature algorithm is set. |
|
| 443 | - * |
|
| 444 | - * @return bool |
|
| 445 | - */ |
|
| 446 | - public function hasSignature(): bool |
|
| 447 | - { |
|
| 448 | - return isset($this->_signature); |
|
| 449 | - } |
|
| 450 | - |
|
| 451 | - /** |
|
| 452 | - * Get signature algorithm. |
|
| 453 | - * |
|
| 454 | - * @return SignatureAlgorithmIdentifier |
|
| 455 | - */ |
|
| 456 | - public function signature(): SignatureAlgorithmIdentifier |
|
| 457 | - { |
|
| 458 | - if (!$this->hasSignature()) { |
|
| 459 | - throw new \LogicException("signature not set."); |
|
| 460 | - } |
|
| 461 | - return $this->_signature; |
|
| 462 | - } |
|
| 463 | - |
|
| 464 | - /** |
|
| 465 | - * Get issuer. |
|
| 466 | - * |
|
| 467 | - * @return Name |
|
| 468 | - */ |
|
| 469 | - public function issuer(): Name |
|
| 470 | - { |
|
| 471 | - return $this->_issuer; |
|
| 472 | - } |
|
| 473 | - |
|
| 474 | - /** |
|
| 475 | - * Get validity period. |
|
| 476 | - * |
|
| 477 | - * @return Validity |
|
| 478 | - */ |
|
| 479 | - public function validity(): Validity |
|
| 480 | - { |
|
| 481 | - return $this->_validity; |
|
| 482 | - } |
|
| 483 | - |
|
| 484 | - /** |
|
| 485 | - * Get subject. |
|
| 486 | - * |
|
| 487 | - * @return Name |
|
| 488 | - */ |
|
| 489 | - public function subject(): Name |
|
| 490 | - { |
|
| 491 | - return $this->_subject; |
|
| 492 | - } |
|
| 493 | - |
|
| 494 | - /** |
|
| 495 | - * Get subject public key. |
|
| 496 | - * |
|
| 497 | - * @return PublicKeyInfo |
|
| 498 | - */ |
|
| 499 | - public function subjectPublicKeyInfo(): PublicKeyInfo |
|
| 500 | - { |
|
| 501 | - return $this->_subjectPublicKeyInfo; |
|
| 502 | - } |
|
| 503 | - |
|
| 504 | - /** |
|
| 505 | - * Whether issuer unique identifier is present. |
|
| 506 | - * |
|
| 507 | - * @return bool |
|
| 508 | - */ |
|
| 509 | - public function hasIssuerUniqueID(): bool |
|
| 510 | - { |
|
| 511 | - return isset($this->_issuerUniqueID); |
|
| 512 | - } |
|
| 513 | - |
|
| 514 | - /** |
|
| 515 | - * Get issuerUniqueID. |
|
| 516 | - * |
|
| 517 | - * @return UniqueIdentifier |
|
| 518 | - */ |
|
| 519 | - public function issuerUniqueID(): UniqueIdentifier |
|
| 520 | - { |
|
| 521 | - if (!$this->hasIssuerUniqueID()) { |
|
| 522 | - throw new \LogicException("issuerUniqueID not set."); |
|
| 523 | - } |
|
| 524 | - return $this->_issuerUniqueID; |
|
| 525 | - } |
|
| 526 | - |
|
| 527 | - /** |
|
| 528 | - * Whether subject unique identifier is present. |
|
| 529 | - * |
|
| 530 | - * @return bool |
|
| 531 | - */ |
|
| 532 | - public function hasSubjectUniqueID(): bool |
|
| 533 | - { |
|
| 534 | - return isset($this->_subjectUniqueID); |
|
| 535 | - } |
|
| 536 | - |
|
| 537 | - /** |
|
| 538 | - * Get subjectUniqueID. |
|
| 539 | - * |
|
| 540 | - * @return UniqueIdentifier |
|
| 541 | - */ |
|
| 542 | - public function subjectUniqueID(): UniqueIdentifier |
|
| 543 | - { |
|
| 544 | - if (!$this->hasSubjectUniqueID()) { |
|
| 545 | - throw new \LogicException("subjectUniqueID not set."); |
|
| 546 | - } |
|
| 547 | - return $this->_subjectUniqueID; |
|
| 548 | - } |
|
| 549 | - |
|
| 550 | - /** |
|
| 551 | - * Get extensions. |
|
| 552 | - * |
|
| 553 | - * @return Extensions |
|
| 554 | - */ |
|
| 555 | - public function extensions(): Extensions |
|
| 556 | - { |
|
| 557 | - return $this->_extensions; |
|
| 558 | - } |
|
| 559 | - |
|
| 560 | - /** |
|
| 561 | - * Generate ASN.1 structure. |
|
| 562 | - * |
|
| 563 | - * @return Sequence |
|
| 564 | - */ |
|
| 565 | - public function toASN1(): Sequence |
|
| 566 | - { |
|
| 567 | - $elements = array(); |
|
| 568 | - $version = $this->version(); |
|
| 569 | - // if version is not default |
|
| 570 | - if ($version != self::VERSION_1) { |
|
| 571 | - $elements[] = new ExplicitlyTaggedType(0, new Integer($version)); |
|
| 572 | - } |
|
| 573 | - $serial = $this->serialNumber(); |
|
| 574 | - $signature = $this->signature(); |
|
| 575 | - // add required elements |
|
| 576 | - array_push($elements, new Integer($serial), $signature->toASN1(), |
|
| 577 | - $this->_issuer->toASN1(), $this->_validity->toASN1(), |
|
| 578 | - $this->_subject->toASN1(), $this->_subjectPublicKeyInfo->toASN1()); |
|
| 579 | - if (isset($this->_issuerUniqueID)) { |
|
| 580 | - $elements[] = new ImplicitlyTaggedType(1, |
|
| 581 | - $this->_issuerUniqueID->toASN1()); |
|
| 582 | - } |
|
| 583 | - if (isset($this->_subjectUniqueID)) { |
|
| 584 | - $elements[] = new ImplicitlyTaggedType(2, |
|
| 585 | - $this->_subjectUniqueID->toASN1()); |
|
| 586 | - } |
|
| 587 | - if (count($this->_extensions)) { |
|
| 588 | - $elements[] = new ExplicitlyTaggedType(3, |
|
| 589 | - $this->_extensions->toASN1()); |
|
| 590 | - } |
|
| 591 | - return new Sequence(...$elements); |
|
| 592 | - } |
|
| 593 | - |
|
| 594 | - /** |
|
| 595 | - * Create signed certificate. |
|
| 596 | - * |
|
| 597 | - * @param SignatureAlgorithmIdentifier $algo Algorithm used for signing |
|
| 598 | - * @param PrivateKeyInfo $privkey_info Private key used for signing |
|
| 599 | - * @param Crypto|null $crypto Crypto engine, use default if not set |
|
| 600 | - * @return Certificate |
|
| 601 | - */ |
|
| 602 | - public function sign(SignatureAlgorithmIdentifier $algo, |
|
| 603 | - PrivateKeyInfo $privkey_info, Crypto $crypto = null): Certificate |
|
| 604 | - { |
|
| 605 | - $crypto = $crypto ?: Crypto::getDefault(); |
|
| 606 | - $tbs_cert = clone $this; |
|
| 607 | - if (!isset($tbs_cert->_version)) { |
|
| 608 | - $tbs_cert->_version = $tbs_cert->_determineVersion(); |
|
| 609 | - } |
|
| 610 | - if (!isset($tbs_cert->_serialNumber)) { |
|
| 611 | - $tbs_cert->_serialNumber = strval(0); |
|
| 612 | - } |
|
| 613 | - $tbs_cert->_signature = $algo; |
|
| 614 | - $data = $tbs_cert->toASN1()->toDER(); |
|
| 615 | - $signature = $crypto->sign($data, $privkey_info, $algo); |
|
| 616 | - return new Certificate($tbs_cert, $algo, $signature); |
|
| 617 | - } |
|
| 618 | - |
|
| 619 | - /** |
|
| 620 | - * Determine minimum version for the certificate. |
|
| 621 | - * |
|
| 622 | - * @return int |
|
| 623 | - */ |
|
| 624 | - protected function _determineVersion(): int |
|
| 625 | - { |
|
| 626 | - // if extensions are present |
|
| 627 | - if (count($this->_extensions)) { |
|
| 628 | - return self::VERSION_3; |
|
| 629 | - } |
|
| 630 | - // if UniqueIdentifier is present |
|
| 631 | - if (isset($this->_issuerUniqueID) || isset($this->_subjectUniqueID)) { |
|
| 632 | - return self::VERSION_2; |
|
| 633 | - } |
|
| 634 | - return self::VERSION_1; |
|
| 635 | - } |
|
| 30 | + // Certificate version enumerations |
|
| 31 | + const VERSION_1 = 0; |
|
| 32 | + const VERSION_2 = 1; |
|
| 33 | + const VERSION_3 = 2; |
|
| 34 | + |
|
| 35 | + /** |
|
| 36 | + * Certificate version. |
|
| 37 | + * |
|
| 38 | + * @var int|null |
|
| 39 | + */ |
|
| 40 | + protected $_version; |
|
| 41 | + |
|
| 42 | + /** |
|
| 43 | + * Serial number. |
|
| 44 | + * |
|
| 45 | + * @var string|null |
|
| 46 | + */ |
|
| 47 | + protected $_serialNumber; |
|
| 48 | + |
|
| 49 | + /** |
|
| 50 | + * Signature algorithm. |
|
| 51 | + * |
|
| 52 | + * @var SignatureAlgorithmIdentifier|null |
|
| 53 | + */ |
|
| 54 | + protected $_signature; |
|
| 55 | + |
|
| 56 | + /** |
|
| 57 | + * Certificate issuer. |
|
| 58 | + * |
|
| 59 | + * @var Name $_issuer |
|
| 60 | + */ |
|
| 61 | + protected $_issuer; |
|
| 62 | + |
|
| 63 | + /** |
|
| 64 | + * Certificate validity period. |
|
| 65 | + * |
|
| 66 | + * @var Validity $_validity |
|
| 67 | + */ |
|
| 68 | + protected $_validity; |
|
| 69 | + |
|
| 70 | + /** |
|
| 71 | + * Certificate subject. |
|
| 72 | + * |
|
| 73 | + * @var Name $_subject |
|
| 74 | + */ |
|
| 75 | + protected $_subject; |
|
| 76 | + |
|
| 77 | + /** |
|
| 78 | + * Subject public key. |
|
| 79 | + * |
|
| 80 | + * @var PublicKeyInfo $_subjectPublicKeyInfo |
|
| 81 | + */ |
|
| 82 | + protected $_subjectPublicKeyInfo; |
|
| 83 | + |
|
| 84 | + /** |
|
| 85 | + * Issuer unique identifier. |
|
| 86 | + * |
|
| 87 | + * @var UniqueIdentifier|null $_issuerUniqueID |
|
| 88 | + */ |
|
| 89 | + protected $_issuerUniqueID; |
|
| 90 | + |
|
| 91 | + /** |
|
| 92 | + * Subject unique identifier. |
|
| 93 | + * |
|
| 94 | + * @var UniqueIdentifier|null $_subjectUniqueID |
|
| 95 | + */ |
|
| 96 | + protected $_subjectUniqueID; |
|
| 97 | + |
|
| 98 | + /** |
|
| 99 | + * Extensions. |
|
| 100 | + * |
|
| 101 | + * @var Extensions $_extensions |
|
| 102 | + */ |
|
| 103 | + protected $_extensions; |
|
| 104 | + |
|
| 105 | + /** |
|
| 106 | + * Constructor. |
|
| 107 | + * |
|
| 108 | + * @param Name $subject Certificate subject |
|
| 109 | + * @param PublicKeyInfo $pki Subject public key |
|
| 110 | + * @param Name $issuer Certificate issuer |
|
| 111 | + * @param Validity $validity Validity period |
|
| 112 | + */ |
|
| 113 | + public function __construct(Name $subject, PublicKeyInfo $pki, Name $issuer, |
|
| 114 | + Validity $validity) |
|
| 115 | + { |
|
| 116 | + $this->_subject = $subject; |
|
| 117 | + $this->_subjectPublicKeyInfo = $pki; |
|
| 118 | + $this->_issuer = $issuer; |
|
| 119 | + $this->_validity = $validity; |
|
| 120 | + $this->_extensions = new Extensions(); |
|
| 121 | + } |
|
| 122 | + |
|
| 123 | + /** |
|
| 124 | + * Initialize from ASN.1. |
|
| 125 | + * |
|
| 126 | + * @param Sequence $seq |
|
| 127 | + * @return self |
|
| 128 | + */ |
|
| 129 | + public static function fromASN1(Sequence $seq): self |
|
| 130 | + { |
|
| 131 | + $idx = 0; |
|
| 132 | + if ($seq->hasTagged(0)) { |
|
| 133 | + $idx++; |
|
| 134 | + $version = $seq->getTagged(0) |
|
| 135 | + ->asExplicit() |
|
| 136 | + ->asInteger() |
|
| 137 | + ->intNumber(); |
|
| 138 | + } else { |
|
| 139 | + $version = self::VERSION_1; |
|
| 140 | + } |
|
| 141 | + $serial = $seq->at($idx++) |
|
| 142 | + ->asInteger() |
|
| 143 | + ->number(); |
|
| 144 | + $algo = AlgorithmIdentifier::fromASN1($seq->at($idx++)->asSequence()); |
|
| 145 | + if (!$algo instanceof SignatureAlgorithmIdentifier) { |
|
| 146 | + throw new \UnexpectedValueException( |
|
| 147 | + "Unsupported signature algorithm " . $algo->name() . "."); |
|
| 148 | + } |
|
| 149 | + $issuer = Name::fromASN1($seq->at($idx++)->asSequence()); |
|
| 150 | + $validity = Validity::fromASN1($seq->at($idx++)->asSequence()); |
|
| 151 | + $subject = Name::fromASN1($seq->at($idx++)->asSequence()); |
|
| 152 | + $pki = PublicKeyInfo::fromASN1($seq->at($idx++)->asSequence()); |
|
| 153 | + $tbs_cert = new self($subject, $pki, $issuer, $validity); |
|
| 154 | + $tbs_cert->_version = $version; |
|
| 155 | + $tbs_cert->_serialNumber = $serial; |
|
| 156 | + $tbs_cert->_signature = $algo; |
|
| 157 | + if ($seq->hasTagged(1)) { |
|
| 158 | + $tbs_cert->_issuerUniqueID = UniqueIdentifier::fromASN1( |
|
| 159 | + $seq->getTagged(1) |
|
| 160 | + ->asImplicit(Element::TYPE_BIT_STRING) |
|
| 161 | + ->asBitString()); |
|
| 162 | + } |
|
| 163 | + if ($seq->hasTagged(2)) { |
|
| 164 | + $tbs_cert->_subjectUniqueID = UniqueIdentifier::fromASN1( |
|
| 165 | + $seq->getTagged(2) |
|
| 166 | + ->asImplicit(Element::TYPE_BIT_STRING) |
|
| 167 | + ->asBitString()); |
|
| 168 | + } |
|
| 169 | + if ($seq->hasTagged(3)) { |
|
| 170 | + $tbs_cert->_extensions = Extensions::fromASN1( |
|
| 171 | + $seq->getTagged(3) |
|
| 172 | + ->asExplicit() |
|
| 173 | + ->asSequence()); |
|
| 174 | + } |
|
| 175 | + return $tbs_cert; |
|
| 176 | + } |
|
| 177 | + |
|
| 178 | + /** |
|
| 179 | + * Initialize from certification request. |
|
| 180 | + * |
|
| 181 | + * Note that signature is not verified and must be done by the caller. |
|
| 182 | + * |
|
| 183 | + * @param CertificationRequest $cr |
|
| 184 | + * @return self |
|
| 185 | + */ |
|
| 186 | + public static function fromCSR(CertificationRequest $cr): self |
|
| 187 | + { |
|
| 188 | + $cri = $cr->certificationRequestInfo(); |
|
| 189 | + $tbs_cert = new self($cri->subject(), $cri->subjectPKInfo(), new Name(), |
|
| 190 | + Validity::fromStrings(null, null)); |
|
| 191 | + // if CSR has Extension Request attribute |
|
| 192 | + if ($cri->hasAttributes()) { |
|
| 193 | + $attribs = $cri->attributes(); |
|
| 194 | + if ($attribs->hasExtensionRequest()) { |
|
| 195 | + $tbs_cert = $tbs_cert->withExtensions( |
|
| 196 | + $attribs->extensionRequest() |
|
| 197 | + ->extensions()); |
|
| 198 | + } |
|
| 199 | + } |
|
| 200 | + // add Subject Key Identifier extension |
|
| 201 | + $tbs_cert = $tbs_cert->withAdditionalExtensions( |
|
| 202 | + new SubjectKeyIdentifierExtension(false, |
|
| 203 | + $cri->subjectPKInfo() |
|
| 204 | + ->keyIdentifier())); |
|
| 205 | + return $tbs_cert; |
|
| 206 | + } |
|
| 207 | + |
|
| 208 | + /** |
|
| 209 | + * Get self with fields set from the issuer's certificate. |
|
| 210 | + * |
|
| 211 | + * Issuer shall be set to issuing certificate's subject. |
|
| 212 | + * Authority key identifier extensions shall be added with a key identifier |
|
| 213 | + * set to issuing certificate's public key identifier. |
|
| 214 | + * |
|
| 215 | + * @param Certificate $cert Issuing party's certificate |
|
| 216 | + * @return self |
|
| 217 | + */ |
|
| 218 | + public function withIssuerCertificate(Certificate $cert): self |
|
| 219 | + { |
|
| 220 | + $obj = clone $this; |
|
| 221 | + // set issuer DN from cert's subject |
|
| 222 | + $obj->_issuer = $cert->tbsCertificate()->subject(); |
|
| 223 | + // add authority key identifier extension |
|
| 224 | + $key_id = $cert->tbsCertificate() |
|
| 225 | + ->subjectPublicKeyInfo() |
|
| 226 | + ->keyIdentifier(); |
|
| 227 | + $obj->_extensions = $obj->_extensions->withExtensions( |
|
| 228 | + new AuthorityKeyIdentifierExtension(false, $key_id)); |
|
| 229 | + return $obj; |
|
| 230 | + } |
|
| 231 | + |
|
| 232 | + /** |
|
| 233 | + * Get self with given version. |
|
| 234 | + * |
|
| 235 | + * If version is not set, appropriate version is automatically |
|
| 236 | + * determined during signing. |
|
| 237 | + * |
|
| 238 | + * @param int $version |
|
| 239 | + * @return self |
|
| 240 | + */ |
|
| 241 | + public function withVersion(int $version): self |
|
| 242 | + { |
|
| 243 | + $obj = clone $this; |
|
| 244 | + $obj->_version = $version; |
|
| 245 | + return $obj; |
|
| 246 | + } |
|
| 247 | + |
|
| 248 | + /** |
|
| 249 | + * Get self with given serial number. |
|
| 250 | + * |
|
| 251 | + * @param int|string $serial Base 10 number |
|
| 252 | + * @return self |
|
| 253 | + */ |
|
| 254 | + public function withSerialNumber($serial): self |
|
| 255 | + { |
|
| 256 | + $obj = clone $this; |
|
| 257 | + $obj->_serialNumber = strval($serial); |
|
| 258 | + return $obj; |
|
| 259 | + } |
|
| 260 | + |
|
| 261 | + /** |
|
| 262 | + * Get self with random positive serial number. |
|
| 263 | + * |
|
| 264 | + * @param int $size Number of random bytes |
|
| 265 | + * @return self |
|
| 266 | + */ |
|
| 267 | + public function withRandomSerialNumber(int $size = 16): self |
|
| 268 | + { |
|
| 269 | + // ensure that first byte is always non-zero and having first bit unset |
|
| 270 | + $num = gmp_init(mt_rand(1, 0x7f), 10); |
|
| 271 | + for ($i = 1; $i < $size; ++$i) { |
|
| 272 | + $num <<= 8; |
|
| 273 | + $num += mt_rand(0, 0xff); |
|
| 274 | + } |
|
| 275 | + return $this->withSerialNumber(gmp_strval($num, 10)); |
|
| 276 | + } |
|
| 277 | + |
|
| 278 | + /** |
|
| 279 | + * Get self with given signature algorithm. |
|
| 280 | + * |
|
| 281 | + * @param SignatureAlgorithmIdentifier $algo |
|
| 282 | + * @return self |
|
| 283 | + */ |
|
| 284 | + public function withSignature(SignatureAlgorithmIdentifier $algo): self |
|
| 285 | + { |
|
| 286 | + $obj = clone $this; |
|
| 287 | + $obj->_signature = $algo; |
|
| 288 | + return $obj; |
|
| 289 | + } |
|
| 290 | + |
|
| 291 | + /** |
|
| 292 | + * Get self with given issuer. |
|
| 293 | + * |
|
| 294 | + * @param Name $issuer |
|
| 295 | + * @return self |
|
| 296 | + */ |
|
| 297 | + public function withIssuer(Name $issuer): self |
|
| 298 | + { |
|
| 299 | + $obj = clone $this; |
|
| 300 | + $obj->_issuer = $issuer; |
|
| 301 | + return $obj; |
|
| 302 | + } |
|
| 303 | + |
|
| 304 | + /** |
|
| 305 | + * Get self with given validity. |
|
| 306 | + * |
|
| 307 | + * @param Validity $validity |
|
| 308 | + * @return self |
|
| 309 | + */ |
|
| 310 | + public function withValidity(Validity $validity): self |
|
| 311 | + { |
|
| 312 | + $obj = clone $this; |
|
| 313 | + $obj->_validity = $validity; |
|
| 314 | + return $obj; |
|
| 315 | + } |
|
| 316 | + |
|
| 317 | + /** |
|
| 318 | + * Get self with given subject. |
|
| 319 | + * |
|
| 320 | + * @param Name $subject |
|
| 321 | + * @return self |
|
| 322 | + */ |
|
| 323 | + public function withSubject(Name $subject): self |
|
| 324 | + { |
|
| 325 | + $obj = clone $this; |
|
| 326 | + $obj->_subject = $subject; |
|
| 327 | + return $obj; |
|
| 328 | + } |
|
| 329 | + |
|
| 330 | + /** |
|
| 331 | + * Get self with given subject public key info. |
|
| 332 | + * |
|
| 333 | + * @param PublicKeyInfo $pub_key_info |
|
| 334 | + * @return self |
|
| 335 | + */ |
|
| 336 | + public function withSubjectPublicKeyInfo(PublicKeyInfo $pub_key_info): self |
|
| 337 | + { |
|
| 338 | + $obj = clone $this; |
|
| 339 | + $obj->_subjectPublicKeyInfo = $pub_key_info; |
|
| 340 | + return $obj; |
|
| 341 | + } |
|
| 342 | + |
|
| 343 | + /** |
|
| 344 | + * Get self with issuer unique ID. |
|
| 345 | + * |
|
| 346 | + * @param UniqueIdentifier $id |
|
| 347 | + * @return self |
|
| 348 | + */ |
|
| 349 | + public function withIssuerUniqueID(UniqueIdentifier $id): self |
|
| 350 | + { |
|
| 351 | + $obj = clone $this; |
|
| 352 | + $obj->_issuerUniqueID = $id; |
|
| 353 | + return $obj; |
|
| 354 | + } |
|
| 355 | + |
|
| 356 | + /** |
|
| 357 | + * Get self with subject unique ID. |
|
| 358 | + * |
|
| 359 | + * @param UniqueIdentifier $id |
|
| 360 | + * @return self |
|
| 361 | + */ |
|
| 362 | + public function withSubjectUniqueID(UniqueIdentifier $id): self |
|
| 363 | + { |
|
| 364 | + $obj = clone $this; |
|
| 365 | + $obj->_subjectUniqueID = $id; |
|
| 366 | + return $obj; |
|
| 367 | + } |
|
| 368 | + |
|
| 369 | + /** |
|
| 370 | + * Get self with given extensions. |
|
| 371 | + * |
|
| 372 | + * @param Extensions $extensions |
|
| 373 | + * @return self |
|
| 374 | + */ |
|
| 375 | + public function withExtensions(Extensions $extensions): self |
|
| 376 | + { |
|
| 377 | + $obj = clone $this; |
|
| 378 | + $obj->_extensions = $extensions; |
|
| 379 | + return $obj; |
|
| 380 | + } |
|
| 381 | + |
|
| 382 | + /** |
|
| 383 | + * Get self with extensions added. |
|
| 384 | + * |
|
| 385 | + * @param Extension ...$exts One or more Extension objects |
|
| 386 | + * @return self |
|
| 387 | + */ |
|
| 388 | + public function withAdditionalExtensions(Extension ...$exts): self |
|
| 389 | + { |
|
| 390 | + $obj = clone $this; |
|
| 391 | + $obj->_extensions = $obj->_extensions->withExtensions(...$exts); |
|
| 392 | + return $obj; |
|
| 393 | + } |
|
| 394 | + |
|
| 395 | + /** |
|
| 396 | + * Check whether version is set. |
|
| 397 | + * |
|
| 398 | + * @return bool |
|
| 399 | + */ |
|
| 400 | + public function hasVersion(): bool |
|
| 401 | + { |
|
| 402 | + return isset($this->_version); |
|
| 403 | + } |
|
| 404 | + |
|
| 405 | + /** |
|
| 406 | + * Get certificate version. |
|
| 407 | + * |
|
| 408 | + * @return int |
|
| 409 | + */ |
|
| 410 | + public function version(): int |
|
| 411 | + { |
|
| 412 | + if (!$this->hasVersion()) { |
|
| 413 | + throw new \LogicException("version not set."); |
|
| 414 | + } |
|
| 415 | + return $this->_version; |
|
| 416 | + } |
|
| 417 | + |
|
| 418 | + /** |
|
| 419 | + * Check whether serial number is set. |
|
| 420 | + * |
|
| 421 | + * @return bool |
|
| 422 | + */ |
|
| 423 | + public function hasSerialNumber(): bool |
|
| 424 | + { |
|
| 425 | + return isset($this->_serialNumber); |
|
| 426 | + } |
|
| 427 | + |
|
| 428 | + /** |
|
| 429 | + * Get serial number. |
|
| 430 | + * |
|
| 431 | + * @return string Base 10 integer |
|
| 432 | + */ |
|
| 433 | + public function serialNumber(): string |
|
| 434 | + { |
|
| 435 | + if (!$this->hasSerialNumber()) { |
|
| 436 | + throw new \LogicException("serialNumber not set."); |
|
| 437 | + } |
|
| 438 | + return $this->_serialNumber; |
|
| 439 | + } |
|
| 440 | + |
|
| 441 | + /** |
|
| 442 | + * Check whether signature algorithm is set. |
|
| 443 | + * |
|
| 444 | + * @return bool |
|
| 445 | + */ |
|
| 446 | + public function hasSignature(): bool |
|
| 447 | + { |
|
| 448 | + return isset($this->_signature); |
|
| 449 | + } |
|
| 450 | + |
|
| 451 | + /** |
|
| 452 | + * Get signature algorithm. |
|
| 453 | + * |
|
| 454 | + * @return SignatureAlgorithmIdentifier |
|
| 455 | + */ |
|
| 456 | + public function signature(): SignatureAlgorithmIdentifier |
|
| 457 | + { |
|
| 458 | + if (!$this->hasSignature()) { |
|
| 459 | + throw new \LogicException("signature not set."); |
|
| 460 | + } |
|
| 461 | + return $this->_signature; |
|
| 462 | + } |
|
| 463 | + |
|
| 464 | + /** |
|
| 465 | + * Get issuer. |
|
| 466 | + * |
|
| 467 | + * @return Name |
|
| 468 | + */ |
|
| 469 | + public function issuer(): Name |
|
| 470 | + { |
|
| 471 | + return $this->_issuer; |
|
| 472 | + } |
|
| 473 | + |
|
| 474 | + /** |
|
| 475 | + * Get validity period. |
|
| 476 | + * |
|
| 477 | + * @return Validity |
|
| 478 | + */ |
|
| 479 | + public function validity(): Validity |
|
| 480 | + { |
|
| 481 | + return $this->_validity; |
|
| 482 | + } |
|
| 483 | + |
|
| 484 | + /** |
|
| 485 | + * Get subject. |
|
| 486 | + * |
|
| 487 | + * @return Name |
|
| 488 | + */ |
|
| 489 | + public function subject(): Name |
|
| 490 | + { |
|
| 491 | + return $this->_subject; |
|
| 492 | + } |
|
| 493 | + |
|
| 494 | + /** |
|
| 495 | + * Get subject public key. |
|
| 496 | + * |
|
| 497 | + * @return PublicKeyInfo |
|
| 498 | + */ |
|
| 499 | + public function subjectPublicKeyInfo(): PublicKeyInfo |
|
| 500 | + { |
|
| 501 | + return $this->_subjectPublicKeyInfo; |
|
| 502 | + } |
|
| 503 | + |
|
| 504 | + /** |
|
| 505 | + * Whether issuer unique identifier is present. |
|
| 506 | + * |
|
| 507 | + * @return bool |
|
| 508 | + */ |
|
| 509 | + public function hasIssuerUniqueID(): bool |
|
| 510 | + { |
|
| 511 | + return isset($this->_issuerUniqueID); |
|
| 512 | + } |
|
| 513 | + |
|
| 514 | + /** |
|
| 515 | + * Get issuerUniqueID. |
|
| 516 | + * |
|
| 517 | + * @return UniqueIdentifier |
|
| 518 | + */ |
|
| 519 | + public function issuerUniqueID(): UniqueIdentifier |
|
| 520 | + { |
|
| 521 | + if (!$this->hasIssuerUniqueID()) { |
|
| 522 | + throw new \LogicException("issuerUniqueID not set."); |
|
| 523 | + } |
|
| 524 | + return $this->_issuerUniqueID; |
|
| 525 | + } |
|
| 526 | + |
|
| 527 | + /** |
|
| 528 | + * Whether subject unique identifier is present. |
|
| 529 | + * |
|
| 530 | + * @return bool |
|
| 531 | + */ |
|
| 532 | + public function hasSubjectUniqueID(): bool |
|
| 533 | + { |
|
| 534 | + return isset($this->_subjectUniqueID); |
|
| 535 | + } |
|
| 536 | + |
|
| 537 | + /** |
|
| 538 | + * Get subjectUniqueID. |
|
| 539 | + * |
|
| 540 | + * @return UniqueIdentifier |
|
| 541 | + */ |
|
| 542 | + public function subjectUniqueID(): UniqueIdentifier |
|
| 543 | + { |
|
| 544 | + if (!$this->hasSubjectUniqueID()) { |
|
| 545 | + throw new \LogicException("subjectUniqueID not set."); |
|
| 546 | + } |
|
| 547 | + return $this->_subjectUniqueID; |
|
| 548 | + } |
|
| 549 | + |
|
| 550 | + /** |
|
| 551 | + * Get extensions. |
|
| 552 | + * |
|
| 553 | + * @return Extensions |
|
| 554 | + */ |
|
| 555 | + public function extensions(): Extensions |
|
| 556 | + { |
|
| 557 | + return $this->_extensions; |
|
| 558 | + } |
|
| 559 | + |
|
| 560 | + /** |
|
| 561 | + * Generate ASN.1 structure. |
|
| 562 | + * |
|
| 563 | + * @return Sequence |
|
| 564 | + */ |
|
| 565 | + public function toASN1(): Sequence |
|
| 566 | + { |
|
| 567 | + $elements = array(); |
|
| 568 | + $version = $this->version(); |
|
| 569 | + // if version is not default |
|
| 570 | + if ($version != self::VERSION_1) { |
|
| 571 | + $elements[] = new ExplicitlyTaggedType(0, new Integer($version)); |
|
| 572 | + } |
|
| 573 | + $serial = $this->serialNumber(); |
|
| 574 | + $signature = $this->signature(); |
|
| 575 | + // add required elements |
|
| 576 | + array_push($elements, new Integer($serial), $signature->toASN1(), |
|
| 577 | + $this->_issuer->toASN1(), $this->_validity->toASN1(), |
|
| 578 | + $this->_subject->toASN1(), $this->_subjectPublicKeyInfo->toASN1()); |
|
| 579 | + if (isset($this->_issuerUniqueID)) { |
|
| 580 | + $elements[] = new ImplicitlyTaggedType(1, |
|
| 581 | + $this->_issuerUniqueID->toASN1()); |
|
| 582 | + } |
|
| 583 | + if (isset($this->_subjectUniqueID)) { |
|
| 584 | + $elements[] = new ImplicitlyTaggedType(2, |
|
| 585 | + $this->_subjectUniqueID->toASN1()); |
|
| 586 | + } |
|
| 587 | + if (count($this->_extensions)) { |
|
| 588 | + $elements[] = new ExplicitlyTaggedType(3, |
|
| 589 | + $this->_extensions->toASN1()); |
|
| 590 | + } |
|
| 591 | + return new Sequence(...$elements); |
|
| 592 | + } |
|
| 593 | + |
|
| 594 | + /** |
|
| 595 | + * Create signed certificate. |
|
| 596 | + * |
|
| 597 | + * @param SignatureAlgorithmIdentifier $algo Algorithm used for signing |
|
| 598 | + * @param PrivateKeyInfo $privkey_info Private key used for signing |
|
| 599 | + * @param Crypto|null $crypto Crypto engine, use default if not set |
|
| 600 | + * @return Certificate |
|
| 601 | + */ |
|
| 602 | + public function sign(SignatureAlgorithmIdentifier $algo, |
|
| 603 | + PrivateKeyInfo $privkey_info, Crypto $crypto = null): Certificate |
|
| 604 | + { |
|
| 605 | + $crypto = $crypto ?: Crypto::getDefault(); |
|
| 606 | + $tbs_cert = clone $this; |
|
| 607 | + if (!isset($tbs_cert->_version)) { |
|
| 608 | + $tbs_cert->_version = $tbs_cert->_determineVersion(); |
|
| 609 | + } |
|
| 610 | + if (!isset($tbs_cert->_serialNumber)) { |
|
| 611 | + $tbs_cert->_serialNumber = strval(0); |
|
| 612 | + } |
|
| 613 | + $tbs_cert->_signature = $algo; |
|
| 614 | + $data = $tbs_cert->toASN1()->toDER(); |
|
| 615 | + $signature = $crypto->sign($data, $privkey_info, $algo); |
|
| 616 | + return new Certificate($tbs_cert, $algo, $signature); |
|
| 617 | + } |
|
| 618 | + |
|
| 619 | + /** |
|
| 620 | + * Determine minimum version for the certificate. |
|
| 621 | + * |
|
| 622 | + * @return int |
|
| 623 | + */ |
|
| 624 | + protected function _determineVersion(): int |
|
| 625 | + { |
|
| 626 | + // if extensions are present |
|
| 627 | + if (count($this->_extensions)) { |
|
| 628 | + return self::VERSION_3; |
|
| 629 | + } |
|
| 630 | + // if UniqueIdentifier is present |
|
| 631 | + if (isset($this->_issuerUniqueID) || isset($this->_subjectUniqueID)) { |
|
| 632 | + return self::VERSION_2; |
|
| 633 | + } |
|
| 634 | + return self::VERSION_1; |
|
| 635 | + } |
|
| 636 | 636 | } |
@@ -273,6 +273,7 @@ |
||
| 273 | 273 | * Get self with extensions added. |
| 274 | 274 | * |
| 275 | 275 | * @param Extension ...$exts One or more Extension objects |
| 276 | + * @param Extension[] $exts |
|
| 276 | 277 | * @return self |
| 277 | 278 | */ |
| 278 | 279 | public function withAdditionalExtensions(Extension ...$exts): self |
@@ -45,7 +45,7 @@ discard block |
||
| 45 | 45 | protected static function _fromDER(string $data, bool $critical): self |
| 46 | 46 | { |
| 47 | 47 | $mappings = array_map( |
| 48 | - function (UnspecifiedType $el) { |
|
| 48 | + function(UnspecifiedType $el) { |
|
| 49 | 49 | return PolicyMapping::fromASN1($el->asSequence()); |
| 50 | 50 | }, Sequence::fromDER($data)->elements()); |
| 51 | 51 | if (!count($mappings)) { |
@@ -66,7 +66,7 @@ discard block |
||
| 66 | 66 | throw new \LogicException("No mappings."); |
| 67 | 67 | } |
| 68 | 68 | $elements = array_map( |
| 69 | - function (PolicyMapping $mapping) { |
|
| 69 | + function(PolicyMapping $mapping) { |
|
| 70 | 70 | return $mapping->toASN1(); |
| 71 | 71 | }, $this->_mappings); |
| 72 | 72 | return new Sequence(...$elements); |
@@ -131,7 +131,7 @@ discard block |
||
| 131 | 131 | public function issuerDomainPolicies(): array |
| 132 | 132 | { |
| 133 | 133 | $idps = array_map( |
| 134 | - function (PolicyMapping $mapping) { |
|
| 134 | + function(PolicyMapping $mapping) { |
|
| 135 | 135 | return $mapping->issuerDomainPolicy(); |
| 136 | 136 | }, $this->_mappings); |
| 137 | 137 | return array_values(array_unique($idps)); |
@@ -15,170 +15,170 @@ |
||
| 15 | 15 | * @link https://tools.ietf.org/html/rfc5280#section-4.2.1.5 |
| 16 | 16 | */ |
| 17 | 17 | class PolicyMappingsExtension extends Extension implements |
| 18 | - \Countable, |
|
| 19 | - \IteratorAggregate |
|
| 18 | + \Countable, |
|
| 19 | + \IteratorAggregate |
|
| 20 | 20 | { |
| 21 | - /** |
|
| 22 | - * Policy mappings. |
|
| 23 | - * |
|
| 24 | - * @var PolicyMapping[] $_mappings |
|
| 25 | - */ |
|
| 26 | - protected $_mappings; |
|
| 21 | + /** |
|
| 22 | + * Policy mappings. |
|
| 23 | + * |
|
| 24 | + * @var PolicyMapping[] $_mappings |
|
| 25 | + */ |
|
| 26 | + protected $_mappings; |
|
| 27 | 27 | |
| 28 | - /** |
|
| 29 | - * Constructor. |
|
| 30 | - * |
|
| 31 | - * @param bool $critical |
|
| 32 | - * @param PolicyMapping ...$mappings One or more PolicyMapping objects |
|
| 33 | - */ |
|
| 34 | - public function __construct(bool $critical, PolicyMapping ...$mappings) |
|
| 35 | - { |
|
| 36 | - parent::__construct(self::OID_POLICY_MAPPINGS, $critical); |
|
| 37 | - $this->_mappings = $mappings; |
|
| 38 | - } |
|
| 28 | + /** |
|
| 29 | + * Constructor. |
|
| 30 | + * |
|
| 31 | + * @param bool $critical |
|
| 32 | + * @param PolicyMapping ...$mappings One or more PolicyMapping objects |
|
| 33 | + */ |
|
| 34 | + public function __construct(bool $critical, PolicyMapping ...$mappings) |
|
| 35 | + { |
|
| 36 | + parent::__construct(self::OID_POLICY_MAPPINGS, $critical); |
|
| 37 | + $this->_mappings = $mappings; |
|
| 38 | + } |
|
| 39 | 39 | |
| 40 | - /** |
|
| 41 | - * |
|
| 42 | - * {@inheritdoc} |
|
| 43 | - * @return self |
|
| 44 | - */ |
|
| 45 | - protected static function _fromDER(string $data, bool $critical): self |
|
| 46 | - { |
|
| 47 | - $mappings = array_map( |
|
| 48 | - function (UnspecifiedType $el) { |
|
| 49 | - return PolicyMapping::fromASN1($el->asSequence()); |
|
| 50 | - }, Sequence::fromDER($data)->elements()); |
|
| 51 | - if (!count($mappings)) { |
|
| 52 | - throw new \UnexpectedValueException( |
|
| 53 | - "PolicyMappings must have at least one mapping."); |
|
| 54 | - } |
|
| 55 | - return new self($critical, ...$mappings); |
|
| 56 | - } |
|
| 40 | + /** |
|
| 41 | + * |
|
| 42 | + * {@inheritdoc} |
|
| 43 | + * @return self |
|
| 44 | + */ |
|
| 45 | + protected static function _fromDER(string $data, bool $critical): self |
|
| 46 | + { |
|
| 47 | + $mappings = array_map( |
|
| 48 | + function (UnspecifiedType $el) { |
|
| 49 | + return PolicyMapping::fromASN1($el->asSequence()); |
|
| 50 | + }, Sequence::fromDER($data)->elements()); |
|
| 51 | + if (!count($mappings)) { |
|
| 52 | + throw new \UnexpectedValueException( |
|
| 53 | + "PolicyMappings must have at least one mapping."); |
|
| 54 | + } |
|
| 55 | + return new self($critical, ...$mappings); |
|
| 56 | + } |
|
| 57 | 57 | |
| 58 | - /** |
|
| 59 | - * |
|
| 60 | - * {@inheritdoc} |
|
| 61 | - * @return Sequence |
|
| 62 | - */ |
|
| 63 | - protected function _valueASN1(): Sequence |
|
| 64 | - { |
|
| 65 | - if (!count($this->_mappings)) { |
|
| 66 | - throw new \LogicException("No mappings."); |
|
| 67 | - } |
|
| 68 | - $elements = array_map( |
|
| 69 | - function (PolicyMapping $mapping) { |
|
| 70 | - return $mapping->toASN1(); |
|
| 71 | - }, $this->_mappings); |
|
| 72 | - return new Sequence(...$elements); |
|
| 73 | - } |
|
| 58 | + /** |
|
| 59 | + * |
|
| 60 | + * {@inheritdoc} |
|
| 61 | + * @return Sequence |
|
| 62 | + */ |
|
| 63 | + protected function _valueASN1(): Sequence |
|
| 64 | + { |
|
| 65 | + if (!count($this->_mappings)) { |
|
| 66 | + throw new \LogicException("No mappings."); |
|
| 67 | + } |
|
| 68 | + $elements = array_map( |
|
| 69 | + function (PolicyMapping $mapping) { |
|
| 70 | + return $mapping->toASN1(); |
|
| 71 | + }, $this->_mappings); |
|
| 72 | + return new Sequence(...$elements); |
|
| 73 | + } |
|
| 74 | 74 | |
| 75 | - /** |
|
| 76 | - * Get all mappings. |
|
| 77 | - * |
|
| 78 | - * @return PolicyMapping[] |
|
| 79 | - */ |
|
| 80 | - public function mappings(): array |
|
| 81 | - { |
|
| 82 | - return $this->_mappings; |
|
| 83 | - } |
|
| 75 | + /** |
|
| 76 | + * Get all mappings. |
|
| 77 | + * |
|
| 78 | + * @return PolicyMapping[] |
|
| 79 | + */ |
|
| 80 | + public function mappings(): array |
|
| 81 | + { |
|
| 82 | + return $this->_mappings; |
|
| 83 | + } |
|
| 84 | 84 | |
| 85 | - /** |
|
| 86 | - * Get mappings flattened into a single array of arrays of subject domains |
|
| 87 | - * keyed by issuer domain. |
|
| 88 | - * |
|
| 89 | - * Eg. if policy mappings contains multiple mappings with the same issuer |
|
| 90 | - * domain policy, their corresponding subject domain policies are placed |
|
| 91 | - * under the same key. |
|
| 92 | - * |
|
| 93 | - * @return (string[])[] |
|
| 94 | - */ |
|
| 95 | - public function flattenedMappings(): array |
|
| 96 | - { |
|
| 97 | - $mappings = array(); |
|
| 98 | - foreach ($this->_mappings as $mapping) { |
|
| 99 | - $idp = $mapping->issuerDomainPolicy(); |
|
| 100 | - if (!isset($mappings[$idp])) { |
|
| 101 | - $mappings[$idp] = array(); |
|
| 102 | - } |
|
| 103 | - array_push($mappings[$idp], $mapping->subjectDomainPolicy()); |
|
| 104 | - } |
|
| 105 | - return $mappings; |
|
| 106 | - } |
|
| 85 | + /** |
|
| 86 | + * Get mappings flattened into a single array of arrays of subject domains |
|
| 87 | + * keyed by issuer domain. |
|
| 88 | + * |
|
| 89 | + * Eg. if policy mappings contains multiple mappings with the same issuer |
|
| 90 | + * domain policy, their corresponding subject domain policies are placed |
|
| 91 | + * under the same key. |
|
| 92 | + * |
|
| 93 | + * @return (string[])[] |
|
| 94 | + */ |
|
| 95 | + public function flattenedMappings(): array |
|
| 96 | + { |
|
| 97 | + $mappings = array(); |
|
| 98 | + foreach ($this->_mappings as $mapping) { |
|
| 99 | + $idp = $mapping->issuerDomainPolicy(); |
|
| 100 | + if (!isset($mappings[$idp])) { |
|
| 101 | + $mappings[$idp] = array(); |
|
| 102 | + } |
|
| 103 | + array_push($mappings[$idp], $mapping->subjectDomainPolicy()); |
|
| 104 | + } |
|
| 105 | + return $mappings; |
|
| 106 | + } |
|
| 107 | 107 | |
| 108 | - /** |
|
| 109 | - * Get all subject domain policy OIDs that are mapped to given issuer |
|
| 110 | - * domain policy OID. |
|
| 111 | - * |
|
| 112 | - * @param string $oid Issuer domain policy |
|
| 113 | - * @return string[] List of OIDs in dotted format |
|
| 114 | - */ |
|
| 115 | - public function issuerMappings(string $oid): array |
|
| 116 | - { |
|
| 117 | - $oids = array(); |
|
| 118 | - foreach ($this->_mappings as $mapping) { |
|
| 119 | - if ($mapping->issuerDomainPolicy() == $oid) { |
|
| 120 | - $oids[] = $mapping->subjectDomainPolicy(); |
|
| 121 | - } |
|
| 122 | - } |
|
| 123 | - return $oids; |
|
| 124 | - } |
|
| 108 | + /** |
|
| 109 | + * Get all subject domain policy OIDs that are mapped to given issuer |
|
| 110 | + * domain policy OID. |
|
| 111 | + * |
|
| 112 | + * @param string $oid Issuer domain policy |
|
| 113 | + * @return string[] List of OIDs in dotted format |
|
| 114 | + */ |
|
| 115 | + public function issuerMappings(string $oid): array |
|
| 116 | + { |
|
| 117 | + $oids = array(); |
|
| 118 | + foreach ($this->_mappings as $mapping) { |
|
| 119 | + if ($mapping->issuerDomainPolicy() == $oid) { |
|
| 120 | + $oids[] = $mapping->subjectDomainPolicy(); |
|
| 121 | + } |
|
| 122 | + } |
|
| 123 | + return $oids; |
|
| 124 | + } |
|
| 125 | 125 | |
| 126 | - /** |
|
| 127 | - * Get all mapped issuer domain policy OIDs. |
|
| 128 | - * |
|
| 129 | - * @return string[] |
|
| 130 | - */ |
|
| 131 | - public function issuerDomainPolicies(): array |
|
| 132 | - { |
|
| 133 | - $idps = array_map( |
|
| 134 | - function (PolicyMapping $mapping) { |
|
| 135 | - return $mapping->issuerDomainPolicy(); |
|
| 136 | - }, $this->_mappings); |
|
| 137 | - return array_values(array_unique($idps)); |
|
| 138 | - } |
|
| 126 | + /** |
|
| 127 | + * Get all mapped issuer domain policy OIDs. |
|
| 128 | + * |
|
| 129 | + * @return string[] |
|
| 130 | + */ |
|
| 131 | + public function issuerDomainPolicies(): array |
|
| 132 | + { |
|
| 133 | + $idps = array_map( |
|
| 134 | + function (PolicyMapping $mapping) { |
|
| 135 | + return $mapping->issuerDomainPolicy(); |
|
| 136 | + }, $this->_mappings); |
|
| 137 | + return array_values(array_unique($idps)); |
|
| 138 | + } |
|
| 139 | 139 | |
| 140 | - /** |
|
| 141 | - * Check whether policy mappings have anyPolicy mapped. |
|
| 142 | - * |
|
| 143 | - * RFC 5280 section 4.2.1.5 states that "Policies MUST NOT be mapped either |
|
| 144 | - * to or from the special value anyPolicy". |
|
| 145 | - * |
|
| 146 | - * @return bool |
|
| 147 | - */ |
|
| 148 | - public function hasAnyPolicyMapping(): bool |
|
| 149 | - { |
|
| 150 | - foreach ($this->_mappings as $mapping) { |
|
| 151 | - if ($mapping->issuerDomainPolicy() == |
|
| 152 | - PolicyInformation::OID_ANY_POLICY) { |
|
| 153 | - return true; |
|
| 154 | - } |
|
| 155 | - if ($mapping->subjectDomainPolicy() == |
|
| 156 | - PolicyInformation::OID_ANY_POLICY) { |
|
| 157 | - return true; |
|
| 158 | - } |
|
| 159 | - } |
|
| 160 | - return false; |
|
| 161 | - } |
|
| 140 | + /** |
|
| 141 | + * Check whether policy mappings have anyPolicy mapped. |
|
| 142 | + * |
|
| 143 | + * RFC 5280 section 4.2.1.5 states that "Policies MUST NOT be mapped either |
|
| 144 | + * to or from the special value anyPolicy". |
|
| 145 | + * |
|
| 146 | + * @return bool |
|
| 147 | + */ |
|
| 148 | + public function hasAnyPolicyMapping(): bool |
|
| 149 | + { |
|
| 150 | + foreach ($this->_mappings as $mapping) { |
|
| 151 | + if ($mapping->issuerDomainPolicy() == |
|
| 152 | + PolicyInformation::OID_ANY_POLICY) { |
|
| 153 | + return true; |
|
| 154 | + } |
|
| 155 | + if ($mapping->subjectDomainPolicy() == |
|
| 156 | + PolicyInformation::OID_ANY_POLICY) { |
|
| 157 | + return true; |
|
| 158 | + } |
|
| 159 | + } |
|
| 160 | + return false; |
|
| 161 | + } |
|
| 162 | 162 | |
| 163 | - /** |
|
| 164 | - * Get the number of mappings. |
|
| 165 | - * |
|
| 166 | - * @see \Countable::count() |
|
| 167 | - * @return int |
|
| 168 | - */ |
|
| 169 | - public function count(): int |
|
| 170 | - { |
|
| 171 | - return count($this->_mappings); |
|
| 172 | - } |
|
| 163 | + /** |
|
| 164 | + * Get the number of mappings. |
|
| 165 | + * |
|
| 166 | + * @see \Countable::count() |
|
| 167 | + * @return int |
|
| 168 | + */ |
|
| 169 | + public function count(): int |
|
| 170 | + { |
|
| 171 | + return count($this->_mappings); |
|
| 172 | + } |
|
| 173 | 173 | |
| 174 | - /** |
|
| 175 | - * Get iterator for policy mappings. |
|
| 176 | - * |
|
| 177 | - * @see \IteratorAggregate::getIterator() |
|
| 178 | - * @return \ArrayIterator |
|
| 179 | - */ |
|
| 180 | - public function getIterator(): \ArrayIterator |
|
| 181 | - { |
|
| 182 | - return new \ArrayIterator($this->_mappings); |
|
| 183 | - } |
|
| 174 | + /** |
|
| 175 | + * Get iterator for policy mappings. |
|
| 176 | + * |
|
| 177 | + * @see \IteratorAggregate::getIterator() |
|
| 178 | + * @return \ArrayIterator |
|
| 179 | + */ |
|
| 180 | + public function getIterator(): \ArrayIterator |
|
| 181 | + { |
|
| 182 | + return new \ArrayIterator($this->_mappings); |
|
| 183 | + } |
|
| 184 | 184 | } |
@@ -13,52 +13,52 @@ |
||
| 13 | 13 | */ |
| 14 | 14 | class SubjectKeyIdentifierExtension extends Extension |
| 15 | 15 | { |
| 16 | - /** |
|
| 17 | - * Key identifier. |
|
| 18 | - * |
|
| 19 | - * @var string $_keyIdentifier |
|
| 20 | - */ |
|
| 21 | - protected $_keyIdentifier; |
|
| 16 | + /** |
|
| 17 | + * Key identifier. |
|
| 18 | + * |
|
| 19 | + * @var string $_keyIdentifier |
|
| 20 | + */ |
|
| 21 | + protected $_keyIdentifier; |
|
| 22 | 22 | |
| 23 | - /** |
|
| 24 | - * Constructor. |
|
| 25 | - * |
|
| 26 | - * @param bool $critical |
|
| 27 | - * @param string $keyIdentifier |
|
| 28 | - */ |
|
| 29 | - public function __construct(bool $critical, string $keyIdentifier) |
|
| 30 | - { |
|
| 31 | - parent::__construct(self::OID_SUBJECT_KEY_IDENTIFIER, $critical); |
|
| 32 | - $this->_keyIdentifier = $keyIdentifier; |
|
| 33 | - } |
|
| 23 | + /** |
|
| 24 | + * Constructor. |
|
| 25 | + * |
|
| 26 | + * @param bool $critical |
|
| 27 | + * @param string $keyIdentifier |
|
| 28 | + */ |
|
| 29 | + public function __construct(bool $critical, string $keyIdentifier) |
|
| 30 | + { |
|
| 31 | + parent::__construct(self::OID_SUBJECT_KEY_IDENTIFIER, $critical); |
|
| 32 | + $this->_keyIdentifier = $keyIdentifier; |
|
| 33 | + } |
|
| 34 | 34 | |
| 35 | - /** |
|
| 36 | - * |
|
| 37 | - * {@inheritdoc} |
|
| 38 | - * @return self |
|
| 39 | - */ |
|
| 40 | - protected static function _fromDER(string $data, bool $critical): self |
|
| 41 | - { |
|
| 42 | - return new self($critical, OctetString::fromDER($data)->string()); |
|
| 43 | - } |
|
| 35 | + /** |
|
| 36 | + * |
|
| 37 | + * {@inheritdoc} |
|
| 38 | + * @return self |
|
| 39 | + */ |
|
| 40 | + protected static function _fromDER(string $data, bool $critical): self |
|
| 41 | + { |
|
| 42 | + return new self($critical, OctetString::fromDER($data)->string()); |
|
| 43 | + } |
|
| 44 | 44 | |
| 45 | - /** |
|
| 46 | - * Get key identifier. |
|
| 47 | - * |
|
| 48 | - * @return string |
|
| 49 | - */ |
|
| 50 | - public function keyIdentifier(): string |
|
| 51 | - { |
|
| 52 | - return $this->_keyIdentifier; |
|
| 53 | - } |
|
| 45 | + /** |
|
| 46 | + * Get key identifier. |
|
| 47 | + * |
|
| 48 | + * @return string |
|
| 49 | + */ |
|
| 50 | + public function keyIdentifier(): string |
|
| 51 | + { |
|
| 52 | + return $this->_keyIdentifier; |
|
| 53 | + } |
|
| 54 | 54 | |
| 55 | - /** |
|
| 56 | - * |
|
| 57 | - * {@inheritdoc} |
|
| 58 | - * @return OctetString |
|
| 59 | - */ |
|
| 60 | - protected function _valueASN1(): OctetString |
|
| 61 | - { |
|
| 62 | - return new OctetString($this->_keyIdentifier); |
|
| 63 | - } |
|
| 55 | + /** |
|
| 56 | + * |
|
| 57 | + * {@inheritdoc} |
|
| 58 | + * @return OctetString |
|
| 59 | + */ |
|
| 60 | + protected function _valueASN1(): OctetString |
|
| 61 | + { |
|
| 62 | + return new OctetString($this->_keyIdentifier); |
|
| 63 | + } |
|
| 64 | 64 | } |
@@ -16,125 +16,125 @@ |
||
| 16 | 16 | */ |
| 17 | 17 | class PolicyConstraintsExtension extends Extension |
| 18 | 18 | { |
| 19 | - /** |
|
| 20 | - * |
|
| 21 | - * @var int $_requireExplicitPolicy |
|
| 22 | - */ |
|
| 23 | - protected $_requireExplicitPolicy; |
|
| 19 | + /** |
|
| 20 | + * |
|
| 21 | + * @var int $_requireExplicitPolicy |
|
| 22 | + */ |
|
| 23 | + protected $_requireExplicitPolicy; |
|
| 24 | 24 | |
| 25 | - /** |
|
| 26 | - * |
|
| 27 | - * @var int $_inhibitPolicyMapping |
|
| 28 | - */ |
|
| 29 | - protected $_inhibitPolicyMapping; |
|
| 25 | + /** |
|
| 26 | + * |
|
| 27 | + * @var int $_inhibitPolicyMapping |
|
| 28 | + */ |
|
| 29 | + protected $_inhibitPolicyMapping; |
|
| 30 | 30 | |
| 31 | - /** |
|
| 32 | - * Constructor. |
|
| 33 | - * |
|
| 34 | - * @param bool $critical |
|
| 35 | - * @param int|null $require_explicit_policy |
|
| 36 | - * @param int|null $inhibit_policy_mapping |
|
| 37 | - */ |
|
| 38 | - public function __construct(bool $critical, $require_explicit_policy = null, |
|
| 39 | - $inhibit_policy_mapping = null) |
|
| 40 | - { |
|
| 41 | - parent::__construct(self::OID_POLICY_CONSTRAINTS, $critical); |
|
| 42 | - $this->_requireExplicitPolicy = isset($require_explicit_policy) ? intval( |
|
| 43 | - $require_explicit_policy) : null; |
|
| 44 | - $this->_inhibitPolicyMapping = isset($inhibit_policy_mapping) ? intval( |
|
| 45 | - $inhibit_policy_mapping) : null; |
|
| 46 | - } |
|
| 31 | + /** |
|
| 32 | + * Constructor. |
|
| 33 | + * |
|
| 34 | + * @param bool $critical |
|
| 35 | + * @param int|null $require_explicit_policy |
|
| 36 | + * @param int|null $inhibit_policy_mapping |
|
| 37 | + */ |
|
| 38 | + public function __construct(bool $critical, $require_explicit_policy = null, |
|
| 39 | + $inhibit_policy_mapping = null) |
|
| 40 | + { |
|
| 41 | + parent::__construct(self::OID_POLICY_CONSTRAINTS, $critical); |
|
| 42 | + $this->_requireExplicitPolicy = isset($require_explicit_policy) ? intval( |
|
| 43 | + $require_explicit_policy) : null; |
|
| 44 | + $this->_inhibitPolicyMapping = isset($inhibit_policy_mapping) ? intval( |
|
| 45 | + $inhibit_policy_mapping) : null; |
|
| 46 | + } |
|
| 47 | 47 | |
| 48 | - /** |
|
| 49 | - * |
|
| 50 | - * {@inheritdoc} |
|
| 51 | - * @return self |
|
| 52 | - */ |
|
| 53 | - protected static function _fromDER(string $data, bool $critical): self |
|
| 54 | - { |
|
| 55 | - $seq = Sequence::fromDER($data); |
|
| 56 | - $require_explicit_policy = null; |
|
| 57 | - $inhibit_policy_mapping = null; |
|
| 58 | - if ($seq->hasTagged(0)) { |
|
| 59 | - $require_explicit_policy = $seq->getTagged(0) |
|
| 60 | - ->asImplicit(Element::TYPE_INTEGER) |
|
| 61 | - ->asInteger() |
|
| 62 | - ->intNumber(); |
|
| 63 | - } |
|
| 64 | - if ($seq->hasTagged(1)) { |
|
| 65 | - $inhibit_policy_mapping = $seq->getTagged(1) |
|
| 66 | - ->asImplicit(Element::TYPE_INTEGER) |
|
| 67 | - ->asInteger() |
|
| 68 | - ->intNumber(); |
|
| 69 | - } |
|
| 70 | - return new self($critical, $require_explicit_policy, |
|
| 71 | - $inhibit_policy_mapping); |
|
| 72 | - } |
|
| 48 | + /** |
|
| 49 | + * |
|
| 50 | + * {@inheritdoc} |
|
| 51 | + * @return self |
|
| 52 | + */ |
|
| 53 | + protected static function _fromDER(string $data, bool $critical): self |
|
| 54 | + { |
|
| 55 | + $seq = Sequence::fromDER($data); |
|
| 56 | + $require_explicit_policy = null; |
|
| 57 | + $inhibit_policy_mapping = null; |
|
| 58 | + if ($seq->hasTagged(0)) { |
|
| 59 | + $require_explicit_policy = $seq->getTagged(0) |
|
| 60 | + ->asImplicit(Element::TYPE_INTEGER) |
|
| 61 | + ->asInteger() |
|
| 62 | + ->intNumber(); |
|
| 63 | + } |
|
| 64 | + if ($seq->hasTagged(1)) { |
|
| 65 | + $inhibit_policy_mapping = $seq->getTagged(1) |
|
| 66 | + ->asImplicit(Element::TYPE_INTEGER) |
|
| 67 | + ->asInteger() |
|
| 68 | + ->intNumber(); |
|
| 69 | + } |
|
| 70 | + return new self($critical, $require_explicit_policy, |
|
| 71 | + $inhibit_policy_mapping); |
|
| 72 | + } |
|
| 73 | 73 | |
| 74 | - /** |
|
| 75 | - * Whether requireExplicitPolicy is present. |
|
| 76 | - * |
|
| 77 | - * @return bool |
|
| 78 | - */ |
|
| 79 | - public function hasRequireExplicitPolicy(): bool |
|
| 80 | - { |
|
| 81 | - return isset($this->_requireExplicitPolicy); |
|
| 82 | - } |
|
| 74 | + /** |
|
| 75 | + * Whether requireExplicitPolicy is present. |
|
| 76 | + * |
|
| 77 | + * @return bool |
|
| 78 | + */ |
|
| 79 | + public function hasRequireExplicitPolicy(): bool |
|
| 80 | + { |
|
| 81 | + return isset($this->_requireExplicitPolicy); |
|
| 82 | + } |
|
| 83 | 83 | |
| 84 | - /** |
|
| 85 | - * Get requireExplicitPolicy. |
|
| 86 | - * |
|
| 87 | - * @throws \LogicException |
|
| 88 | - * @return int |
|
| 89 | - */ |
|
| 90 | - public function requireExplicitPolicy(): int |
|
| 91 | - { |
|
| 92 | - if (!$this->hasRequireExplicitPolicy()) { |
|
| 93 | - throw new \LogicException("requireExplicitPolicy not set."); |
|
| 94 | - } |
|
| 95 | - return $this->_requireExplicitPolicy; |
|
| 96 | - } |
|
| 84 | + /** |
|
| 85 | + * Get requireExplicitPolicy. |
|
| 86 | + * |
|
| 87 | + * @throws \LogicException |
|
| 88 | + * @return int |
|
| 89 | + */ |
|
| 90 | + public function requireExplicitPolicy(): int |
|
| 91 | + { |
|
| 92 | + if (!$this->hasRequireExplicitPolicy()) { |
|
| 93 | + throw new \LogicException("requireExplicitPolicy not set."); |
|
| 94 | + } |
|
| 95 | + return $this->_requireExplicitPolicy; |
|
| 96 | + } |
|
| 97 | 97 | |
| 98 | - /** |
|
| 99 | - * Whether inhibitPolicyMapping is present. |
|
| 100 | - * |
|
| 101 | - * @return bool |
|
| 102 | - */ |
|
| 103 | - public function hasInhibitPolicyMapping(): bool |
|
| 104 | - { |
|
| 105 | - return isset($this->_inhibitPolicyMapping); |
|
| 106 | - } |
|
| 98 | + /** |
|
| 99 | + * Whether inhibitPolicyMapping is present. |
|
| 100 | + * |
|
| 101 | + * @return bool |
|
| 102 | + */ |
|
| 103 | + public function hasInhibitPolicyMapping(): bool |
|
| 104 | + { |
|
| 105 | + return isset($this->_inhibitPolicyMapping); |
|
| 106 | + } |
|
| 107 | 107 | |
| 108 | - /** |
|
| 109 | - * Get inhibitPolicyMapping. |
|
| 110 | - * |
|
| 111 | - * @throws \LogicException |
|
| 112 | - * @return int |
|
| 113 | - */ |
|
| 114 | - public function inhibitPolicyMapping(): int |
|
| 115 | - { |
|
| 116 | - if (!$this->hasInhibitPolicyMapping()) { |
|
| 117 | - throw new \LogicException("inhibitPolicyMapping not set."); |
|
| 118 | - } |
|
| 119 | - return $this->_inhibitPolicyMapping; |
|
| 120 | - } |
|
| 108 | + /** |
|
| 109 | + * Get inhibitPolicyMapping. |
|
| 110 | + * |
|
| 111 | + * @throws \LogicException |
|
| 112 | + * @return int |
|
| 113 | + */ |
|
| 114 | + public function inhibitPolicyMapping(): int |
|
| 115 | + { |
|
| 116 | + if (!$this->hasInhibitPolicyMapping()) { |
|
| 117 | + throw new \LogicException("inhibitPolicyMapping not set."); |
|
| 118 | + } |
|
| 119 | + return $this->_inhibitPolicyMapping; |
|
| 120 | + } |
|
| 121 | 121 | |
| 122 | - /** |
|
| 123 | - * |
|
| 124 | - * {@inheritdoc} |
|
| 125 | - * @return Sequence |
|
| 126 | - */ |
|
| 127 | - protected function _valueASN1(): Sequence |
|
| 128 | - { |
|
| 129 | - $elements = array(); |
|
| 130 | - if (isset($this->_requireExplicitPolicy)) { |
|
| 131 | - $elements[] = new ImplicitlyTaggedType(0, |
|
| 132 | - new Integer($this->_requireExplicitPolicy)); |
|
| 133 | - } |
|
| 134 | - if (isset($this->_inhibitPolicyMapping)) { |
|
| 135 | - $elements[] = new ImplicitlyTaggedType(1, |
|
| 136 | - new Integer($this->_inhibitPolicyMapping)); |
|
| 137 | - } |
|
| 138 | - return new Sequence(...$elements); |
|
| 139 | - } |
|
| 122 | + /** |
|
| 123 | + * |
|
| 124 | + * {@inheritdoc} |
|
| 125 | + * @return Sequence |
|
| 126 | + */ |
|
| 127 | + protected function _valueASN1(): Sequence |
|
| 128 | + { |
|
| 129 | + $elements = array(); |
|
| 130 | + if (isset($this->_requireExplicitPolicy)) { |
|
| 131 | + $elements[] = new ImplicitlyTaggedType(0, |
|
| 132 | + new Integer($this->_requireExplicitPolicy)); |
|
| 133 | + } |
|
| 134 | + if (isset($this->_inhibitPolicyMapping)) { |
|
| 135 | + $elements[] = new ImplicitlyTaggedType(1, |
|
| 136 | + new Integer($this->_inhibitPolicyMapping)); |
|
| 137 | + } |
|
| 138 | + return new Sequence(...$elements); |
|
| 139 | + } |
|
| 140 | 140 | } |
@@ -14,136 +14,136 @@ |
||
| 14 | 14 | * @link https://tools.ietf.org/html/rfc5280#section-4.2.1.4 |
| 15 | 15 | */ |
| 16 | 16 | class CertificatePoliciesExtension extends Extension implements |
| 17 | - \Countable, |
|
| 18 | - \IteratorAggregate |
|
| 17 | + \Countable, |
|
| 18 | + \IteratorAggregate |
|
| 19 | 19 | { |
| 20 | - /** |
|
| 21 | - * Policy information terms. |
|
| 22 | - * |
|
| 23 | - * @var PolicyInformation[] $_policies |
|
| 24 | - */ |
|
| 25 | - protected $_policies; |
|
| 20 | + /** |
|
| 21 | + * Policy information terms. |
|
| 22 | + * |
|
| 23 | + * @var PolicyInformation[] $_policies |
|
| 24 | + */ |
|
| 25 | + protected $_policies; |
|
| 26 | 26 | |
| 27 | - /** |
|
| 28 | - * Constructor. |
|
| 29 | - * |
|
| 30 | - * @param bool $critical |
|
| 31 | - * @param PolicyInformation ...$policies |
|
| 32 | - */ |
|
| 33 | - public function __construct(bool $critical, PolicyInformation ...$policies) |
|
| 34 | - { |
|
| 35 | - parent::__construct(Extension::OID_CERTIFICATE_POLICIES, $critical); |
|
| 36 | - $this->_policies = []; |
|
| 37 | - foreach ($policies as $policy) { |
|
| 38 | - $this->_policies[$policy->oid()] = $policy; |
|
| 39 | - } |
|
| 40 | - } |
|
| 27 | + /** |
|
| 28 | + * Constructor. |
|
| 29 | + * |
|
| 30 | + * @param bool $critical |
|
| 31 | + * @param PolicyInformation ...$policies |
|
| 32 | + */ |
|
| 33 | + public function __construct(bool $critical, PolicyInformation ...$policies) |
|
| 34 | + { |
|
| 35 | + parent::__construct(Extension::OID_CERTIFICATE_POLICIES, $critical); |
|
| 36 | + $this->_policies = []; |
|
| 37 | + foreach ($policies as $policy) { |
|
| 38 | + $this->_policies[$policy->oid()] = $policy; |
|
| 39 | + } |
|
| 40 | + } |
|
| 41 | 41 | |
| 42 | - /** |
|
| 43 | - * |
|
| 44 | - * {@inheritdoc} |
|
| 45 | - * @return self |
|
| 46 | - */ |
|
| 47 | - protected static function _fromDER(string $data, bool $critical): self |
|
| 48 | - { |
|
| 49 | - $policies = array_map( |
|
| 50 | - function (UnspecifiedType $el) { |
|
| 51 | - return PolicyInformation::fromASN1($el->asSequence()); |
|
| 52 | - }, Sequence::fromDER($data)->elements()); |
|
| 53 | - if (!count($policies)) { |
|
| 54 | - throw new \UnexpectedValueException( |
|
| 55 | - "certificatePolicies must contain" . |
|
| 56 | - " at least one PolicyInformation."); |
|
| 57 | - } |
|
| 58 | - return new self($critical, ...$policies); |
|
| 59 | - } |
|
| 42 | + /** |
|
| 43 | + * |
|
| 44 | + * {@inheritdoc} |
|
| 45 | + * @return self |
|
| 46 | + */ |
|
| 47 | + protected static function _fromDER(string $data, bool $critical): self |
|
| 48 | + { |
|
| 49 | + $policies = array_map( |
|
| 50 | + function (UnspecifiedType $el) { |
|
| 51 | + return PolicyInformation::fromASN1($el->asSequence()); |
|
| 52 | + }, Sequence::fromDER($data)->elements()); |
|
| 53 | + if (!count($policies)) { |
|
| 54 | + throw new \UnexpectedValueException( |
|
| 55 | + "certificatePolicies must contain" . |
|
| 56 | + " at least one PolicyInformation."); |
|
| 57 | + } |
|
| 58 | + return new self($critical, ...$policies); |
|
| 59 | + } |
|
| 60 | 60 | |
| 61 | - /** |
|
| 62 | - * Check whether policy information by OID is present. |
|
| 63 | - * |
|
| 64 | - * @param string $oid |
|
| 65 | - * @return bool |
|
| 66 | - */ |
|
| 67 | - public function has(string $oid): bool |
|
| 68 | - { |
|
| 69 | - return isset($this->_policies[$oid]); |
|
| 70 | - } |
|
| 61 | + /** |
|
| 62 | + * Check whether policy information by OID is present. |
|
| 63 | + * |
|
| 64 | + * @param string $oid |
|
| 65 | + * @return bool |
|
| 66 | + */ |
|
| 67 | + public function has(string $oid): bool |
|
| 68 | + { |
|
| 69 | + return isset($this->_policies[$oid]); |
|
| 70 | + } |
|
| 71 | 71 | |
| 72 | - /** |
|
| 73 | - * Get policy information by OID. |
|
| 74 | - * |
|
| 75 | - * @param string $oid |
|
| 76 | - * @throws \LogicException |
|
| 77 | - * @return PolicyInformation |
|
| 78 | - */ |
|
| 79 | - public function get(string $oid): PolicyInformation |
|
| 80 | - { |
|
| 81 | - if (!$this->has($oid)) { |
|
| 82 | - throw new \LogicException("Not certificate policy by OID $oid."); |
|
| 83 | - } |
|
| 84 | - return $this->_policies[$oid]; |
|
| 85 | - } |
|
| 72 | + /** |
|
| 73 | + * Get policy information by OID. |
|
| 74 | + * |
|
| 75 | + * @param string $oid |
|
| 76 | + * @throws \LogicException |
|
| 77 | + * @return PolicyInformation |
|
| 78 | + */ |
|
| 79 | + public function get(string $oid): PolicyInformation |
|
| 80 | + { |
|
| 81 | + if (!$this->has($oid)) { |
|
| 82 | + throw new \LogicException("Not certificate policy by OID $oid."); |
|
| 83 | + } |
|
| 84 | + return $this->_policies[$oid]; |
|
| 85 | + } |
|
| 86 | 86 | |
| 87 | - /** |
|
| 88 | - * Check whether anyPolicy is present. |
|
| 89 | - * |
|
| 90 | - * @return bool |
|
| 91 | - */ |
|
| 92 | - public function hasAnyPolicy(): bool |
|
| 93 | - { |
|
| 94 | - return $this->has(PolicyInformation::OID_ANY_POLICY); |
|
| 95 | - } |
|
| 87 | + /** |
|
| 88 | + * Check whether anyPolicy is present. |
|
| 89 | + * |
|
| 90 | + * @return bool |
|
| 91 | + */ |
|
| 92 | + public function hasAnyPolicy(): bool |
|
| 93 | + { |
|
| 94 | + return $this->has(PolicyInformation::OID_ANY_POLICY); |
|
| 95 | + } |
|
| 96 | 96 | |
| 97 | - /** |
|
| 98 | - * Get anyPolicy information. |
|
| 99 | - * |
|
| 100 | - * @throws \LogicException If anyPolicy is not present. |
|
| 101 | - * @return PolicyInformation |
|
| 102 | - */ |
|
| 103 | - public function anyPolicy(): PolicyInformation |
|
| 104 | - { |
|
| 105 | - if (!$this->hasAnyPolicy()) { |
|
| 106 | - throw new \LogicException("No anyPolicy."); |
|
| 107 | - } |
|
| 108 | - return $this->get(PolicyInformation::OID_ANY_POLICY); |
|
| 109 | - } |
|
| 97 | + /** |
|
| 98 | + * Get anyPolicy information. |
|
| 99 | + * |
|
| 100 | + * @throws \LogicException If anyPolicy is not present. |
|
| 101 | + * @return PolicyInformation |
|
| 102 | + */ |
|
| 103 | + public function anyPolicy(): PolicyInformation |
|
| 104 | + { |
|
| 105 | + if (!$this->hasAnyPolicy()) { |
|
| 106 | + throw new \LogicException("No anyPolicy."); |
|
| 107 | + } |
|
| 108 | + return $this->get(PolicyInformation::OID_ANY_POLICY); |
|
| 109 | + } |
|
| 110 | 110 | |
| 111 | - /** |
|
| 112 | - * |
|
| 113 | - * {@inheritdoc} |
|
| 114 | - * @return Sequence |
|
| 115 | - */ |
|
| 116 | - protected function _valueASN1(): Sequence |
|
| 117 | - { |
|
| 118 | - if (!count($this->_policies)) { |
|
| 119 | - throw new \LogicException("No policies."); |
|
| 120 | - } |
|
| 121 | - $elements = array_map( |
|
| 122 | - function (PolicyInformation $pi) { |
|
| 123 | - return $pi->toASN1(); |
|
| 124 | - }, array_values($this->_policies)); |
|
| 125 | - return new Sequence(...$elements); |
|
| 126 | - } |
|
| 111 | + /** |
|
| 112 | + * |
|
| 113 | + * {@inheritdoc} |
|
| 114 | + * @return Sequence |
|
| 115 | + */ |
|
| 116 | + protected function _valueASN1(): Sequence |
|
| 117 | + { |
|
| 118 | + if (!count($this->_policies)) { |
|
| 119 | + throw new \LogicException("No policies."); |
|
| 120 | + } |
|
| 121 | + $elements = array_map( |
|
| 122 | + function (PolicyInformation $pi) { |
|
| 123 | + return $pi->toASN1(); |
|
| 124 | + }, array_values($this->_policies)); |
|
| 125 | + return new Sequence(...$elements); |
|
| 126 | + } |
|
| 127 | 127 | |
| 128 | - /** |
|
| 129 | - * Get the number of policies. |
|
| 130 | - * |
|
| 131 | - * @see \Countable::count() |
|
| 132 | - * @return int |
|
| 133 | - */ |
|
| 134 | - public function count(): int |
|
| 135 | - { |
|
| 136 | - return count($this->_policies); |
|
| 137 | - } |
|
| 128 | + /** |
|
| 129 | + * Get the number of policies. |
|
| 130 | + * |
|
| 131 | + * @see \Countable::count() |
|
| 132 | + * @return int |
|
| 133 | + */ |
|
| 134 | + public function count(): int |
|
| 135 | + { |
|
| 136 | + return count($this->_policies); |
|
| 137 | + } |
|
| 138 | 138 | |
| 139 | - /** |
|
| 140 | - * Get iterator for policy information terms. |
|
| 141 | - * |
|
| 142 | - * @see \IteratorAggregate::getIterator() |
|
| 143 | - * @return \ArrayIterator |
|
| 144 | - */ |
|
| 145 | - public function getIterator(): \ArrayIterator |
|
| 146 | - { |
|
| 147 | - return new \ArrayIterator($this->_policies); |
|
| 148 | - } |
|
| 139 | + /** |
|
| 140 | + * Get iterator for policy information terms. |
|
| 141 | + * |
|
| 142 | + * @see \IteratorAggregate::getIterator() |
|
| 143 | + * @return \ArrayIterator |
|
| 144 | + */ |
|
| 145 | + public function getIterator(): \ArrayIterator |
|
| 146 | + { |
|
| 147 | + return new \ArrayIterator($this->_policies); |
|
| 148 | + } |
|
| 149 | 149 | } |
@@ -47,7 +47,7 @@ discard block |
||
| 47 | 47 | protected static function _fromDER(string $data, bool $critical): self |
| 48 | 48 | { |
| 49 | 49 | $policies = array_map( |
| 50 | - function (UnspecifiedType $el) { |
|
| 50 | + function(UnspecifiedType $el) { |
|
| 51 | 51 | return PolicyInformation::fromASN1($el->asSequence()); |
| 52 | 52 | }, Sequence::fromDER($data)->elements()); |
| 53 | 53 | if (!count($policies)) { |
@@ -119,7 +119,7 @@ discard block |
||
| 119 | 119 | throw new \LogicException("No policies."); |
| 120 | 120 | } |
| 121 | 121 | $elements = array_map( |
| 122 | - function (PolicyInformation $pi) { |
|
| 122 | + function(PolicyInformation $pi) { |
|
| 123 | 123 | return $pi->toASN1(); |
| 124 | 124 | }, array_values($this->_policies)); |
| 125 | 125 | return new Sequence(...$elements); |