@@ -12,114 +12,114 @@ |
||
| 12 | 12 | */ |
| 13 | 13 | class ACValidationConfig |
| 14 | 14 | { |
| 15 | - /** |
|
| 16 | - * Certification path of the AC holder. |
|
| 17 | - * |
|
| 18 | - * @var CertificationPath |
|
| 19 | - */ |
|
| 20 | - protected $_holderPath; |
|
| 15 | + /** |
|
| 16 | + * Certification path of the AC holder. |
|
| 17 | + * |
|
| 18 | + * @var CertificationPath |
|
| 19 | + */ |
|
| 20 | + protected $_holderPath; |
|
| 21 | 21 | |
| 22 | - /** |
|
| 23 | - * Certification path of the AC issuer. |
|
| 24 | - * |
|
| 25 | - * @var CertificationPath |
|
| 26 | - */ |
|
| 27 | - protected $_issuerPath; |
|
| 22 | + /** |
|
| 23 | + * Certification path of the AC issuer. |
|
| 24 | + * |
|
| 25 | + * @var CertificationPath |
|
| 26 | + */ |
|
| 27 | + protected $_issuerPath; |
|
| 28 | 28 | |
| 29 | - /** |
|
| 30 | - * Evaluation reference time. |
|
| 31 | - * |
|
| 32 | - * @var \DateTimeImmutable |
|
| 33 | - */ |
|
| 34 | - protected $_evalTime; |
|
| 29 | + /** |
|
| 30 | + * Evaluation reference time. |
|
| 31 | + * |
|
| 32 | + * @var \DateTimeImmutable |
|
| 33 | + */ |
|
| 34 | + protected $_evalTime; |
|
| 35 | 35 | |
| 36 | - /** |
|
| 37 | - * Permitted targets. |
|
| 38 | - * |
|
| 39 | - * @var Target[] |
|
| 40 | - */ |
|
| 41 | - protected $_targets; |
|
| 36 | + /** |
|
| 37 | + * Permitted targets. |
|
| 38 | + * |
|
| 39 | + * @var Target[] |
|
| 40 | + */ |
|
| 41 | + protected $_targets; |
|
| 42 | 42 | |
| 43 | - /** |
|
| 44 | - * Constructor. |
|
| 45 | - * |
|
| 46 | - * @param CertificationPath $holder_path Certification path of the AC holder |
|
| 47 | - * @param CertificationPath $issuer_path Certification path of the AC issuer |
|
| 48 | - */ |
|
| 49 | - public function __construct(CertificationPath $holder_path, |
|
| 50 | - CertificationPath $issuer_path) |
|
| 51 | - { |
|
| 52 | - $this->_holderPath = $holder_path; |
|
| 53 | - $this->_issuerPath = $issuer_path; |
|
| 54 | - $this->_evalTime = new \DateTimeImmutable(); |
|
| 55 | - $this->_targets = []; |
|
| 56 | - } |
|
| 43 | + /** |
|
| 44 | + * Constructor. |
|
| 45 | + * |
|
| 46 | + * @param CertificationPath $holder_path Certification path of the AC holder |
|
| 47 | + * @param CertificationPath $issuer_path Certification path of the AC issuer |
|
| 48 | + */ |
|
| 49 | + public function __construct(CertificationPath $holder_path, |
|
| 50 | + CertificationPath $issuer_path) |
|
| 51 | + { |
|
| 52 | + $this->_holderPath = $holder_path; |
|
| 53 | + $this->_issuerPath = $issuer_path; |
|
| 54 | + $this->_evalTime = new \DateTimeImmutable(); |
|
| 55 | + $this->_targets = []; |
|
| 56 | + } |
|
| 57 | 57 | |
| 58 | - /** |
|
| 59 | - * Get certification path of the AC's holder. |
|
| 60 | - * |
|
| 61 | - * @return CertificationPath |
|
| 62 | - */ |
|
| 63 | - public function holderPath(): CertificationPath |
|
| 64 | - { |
|
| 65 | - return $this->_holderPath; |
|
| 66 | - } |
|
| 58 | + /** |
|
| 59 | + * Get certification path of the AC's holder. |
|
| 60 | + * |
|
| 61 | + * @return CertificationPath |
|
| 62 | + */ |
|
| 63 | + public function holderPath(): CertificationPath |
|
| 64 | + { |
|
| 65 | + return $this->_holderPath; |
|
| 66 | + } |
|
| 67 | 67 | |
| 68 | - /** |
|
| 69 | - * Get certification path of the AC's issuer. |
|
| 70 | - * |
|
| 71 | - * @return CertificationPath |
|
| 72 | - */ |
|
| 73 | - public function issuerPath(): CertificationPath |
|
| 74 | - { |
|
| 75 | - return $this->_issuerPath; |
|
| 76 | - } |
|
| 68 | + /** |
|
| 69 | + * Get certification path of the AC's issuer. |
|
| 70 | + * |
|
| 71 | + * @return CertificationPath |
|
| 72 | + */ |
|
| 73 | + public function issuerPath(): CertificationPath |
|
| 74 | + { |
|
| 75 | + return $this->_issuerPath; |
|
| 76 | + } |
|
| 77 | 77 | |
| 78 | - /** |
|
| 79 | - * Get self with given evaluation reference time. |
|
| 80 | - * |
|
| 81 | - * @param \DateTimeImmutable $dt |
|
| 82 | - * |
|
| 83 | - * @return self |
|
| 84 | - */ |
|
| 85 | - public function withEvaluationTime(\DateTimeImmutable $dt): self |
|
| 86 | - { |
|
| 87 | - $obj = clone $this; |
|
| 88 | - $obj->_evalTime = $dt; |
|
| 89 | - return $obj; |
|
| 90 | - } |
|
| 78 | + /** |
|
| 79 | + * Get self with given evaluation reference time. |
|
| 80 | + * |
|
| 81 | + * @param \DateTimeImmutable $dt |
|
| 82 | + * |
|
| 83 | + * @return self |
|
| 84 | + */ |
|
| 85 | + public function withEvaluationTime(\DateTimeImmutable $dt): self |
|
| 86 | + { |
|
| 87 | + $obj = clone $this; |
|
| 88 | + $obj->_evalTime = $dt; |
|
| 89 | + return $obj; |
|
| 90 | + } |
|
| 91 | 91 | |
| 92 | - /** |
|
| 93 | - * Get the evaluation reference time. |
|
| 94 | - * |
|
| 95 | - * @return \DateTimeImmutable |
|
| 96 | - */ |
|
| 97 | - public function evaluationTime(): \DateTimeImmutable |
|
| 98 | - { |
|
| 99 | - return $this->_evalTime; |
|
| 100 | - } |
|
| 92 | + /** |
|
| 93 | + * Get the evaluation reference time. |
|
| 94 | + * |
|
| 95 | + * @return \DateTimeImmutable |
|
| 96 | + */ |
|
| 97 | + public function evaluationTime(): \DateTimeImmutable |
|
| 98 | + { |
|
| 99 | + return $this->_evalTime; |
|
| 100 | + } |
|
| 101 | 101 | |
| 102 | - /** |
|
| 103 | - * Get self with permitted targets. |
|
| 104 | - * |
|
| 105 | - * @param Target ...$targets |
|
| 106 | - * |
|
| 107 | - * @return self |
|
| 108 | - */ |
|
| 109 | - public function withTargets(Target ...$targets): self |
|
| 110 | - { |
|
| 111 | - $obj = clone $this; |
|
| 112 | - $obj->_targets = $targets; |
|
| 113 | - return $obj; |
|
| 114 | - } |
|
| 102 | + /** |
|
| 103 | + * Get self with permitted targets. |
|
| 104 | + * |
|
| 105 | + * @param Target ...$targets |
|
| 106 | + * |
|
| 107 | + * @return self |
|
| 108 | + */ |
|
| 109 | + public function withTargets(Target ...$targets): self |
|
| 110 | + { |
|
| 111 | + $obj = clone $this; |
|
| 112 | + $obj->_targets = $targets; |
|
| 113 | + return $obj; |
|
| 114 | + } |
|
| 115 | 115 | |
| 116 | - /** |
|
| 117 | - * Get array of permitted targets. |
|
| 118 | - * |
|
| 119 | - * @return Target[] |
|
| 120 | - */ |
|
| 121 | - public function targets(): array |
|
| 122 | - { |
|
| 123 | - return $this->_targets; |
|
| 124 | - } |
|
| 116 | + /** |
|
| 117 | + * Get array of permitted targets. |
|
| 118 | + * |
|
| 119 | + * @return Target[] |
|
| 120 | + */ |
|
| 121 | + public function targets(): array |
|
| 122 | + { |
|
| 123 | + return $this->_targets; |
|
| 124 | + } |
|
| 125 | 125 | } |
@@ -21,186 +21,186 @@ |
||
| 21 | 21 | */ |
| 22 | 22 | class ACValidator |
| 23 | 23 | { |
| 24 | - /** |
|
| 25 | - * Attribute certificate. |
|
| 26 | - * |
|
| 27 | - * @var AttributeCertificate |
|
| 28 | - */ |
|
| 29 | - protected $_ac; |
|
| 24 | + /** |
|
| 25 | + * Attribute certificate. |
|
| 26 | + * |
|
| 27 | + * @var AttributeCertificate |
|
| 28 | + */ |
|
| 29 | + protected $_ac; |
|
| 30 | 30 | |
| 31 | - /** |
|
| 32 | - * Validation configuration. |
|
| 33 | - * |
|
| 34 | - * @var ACValidationConfig |
|
| 35 | - */ |
|
| 36 | - protected $_config; |
|
| 31 | + /** |
|
| 32 | + * Validation configuration. |
|
| 33 | + * |
|
| 34 | + * @var ACValidationConfig |
|
| 35 | + */ |
|
| 36 | + protected $_config; |
|
| 37 | 37 | |
| 38 | - /** |
|
| 39 | - * Crypto engine. |
|
| 40 | - * |
|
| 41 | - * @var Crypto |
|
| 42 | - */ |
|
| 43 | - protected $_crypto; |
|
| 38 | + /** |
|
| 39 | + * Crypto engine. |
|
| 40 | + * |
|
| 41 | + * @var Crypto |
|
| 42 | + */ |
|
| 43 | + protected $_crypto; |
|
| 44 | 44 | |
| 45 | - /** |
|
| 46 | - * Constructor. |
|
| 47 | - * |
|
| 48 | - * @param AttributeCertificate $ac Attribute certificate to validate |
|
| 49 | - * @param ACValidationConfig $config Validation configuration |
|
| 50 | - * @param null|Crypto $crypto Crypto engine, use default if not set |
|
| 51 | - */ |
|
| 52 | - public function __construct(AttributeCertificate $ac, |
|
| 53 | - ACValidationConfig $config, ?Crypto $crypto = null) |
|
| 54 | - { |
|
| 55 | - $this->_ac = $ac; |
|
| 56 | - $this->_config = $config; |
|
| 57 | - $this->_crypto = $crypto ?? Crypto::getDefault(); |
|
| 58 | - } |
|
| 45 | + /** |
|
| 46 | + * Constructor. |
|
| 47 | + * |
|
| 48 | + * @param AttributeCertificate $ac Attribute certificate to validate |
|
| 49 | + * @param ACValidationConfig $config Validation configuration |
|
| 50 | + * @param null|Crypto $crypto Crypto engine, use default if not set |
|
| 51 | + */ |
|
| 52 | + public function __construct(AttributeCertificate $ac, |
|
| 53 | + ACValidationConfig $config, ?Crypto $crypto = null) |
|
| 54 | + { |
|
| 55 | + $this->_ac = $ac; |
|
| 56 | + $this->_config = $config; |
|
| 57 | + $this->_crypto = $crypto ?? Crypto::getDefault(); |
|
| 58 | + } |
|
| 59 | 59 | |
| 60 | - /** |
|
| 61 | - * Validate attribute certificate. |
|
| 62 | - * |
|
| 63 | - * @throws ACValidationException If validation fails |
|
| 64 | - * |
|
| 65 | - * @return AttributeCertificate Validated AC |
|
| 66 | - */ |
|
| 67 | - public function validate(): AttributeCertificate |
|
| 68 | - { |
|
| 69 | - $this->_validateHolder(); |
|
| 70 | - $issuer = $this->_verifyIssuer(); |
|
| 71 | - $this->_validateIssuerProfile($issuer); |
|
| 72 | - $this->_validateTime(); |
|
| 73 | - $this->_validateTargeting(); |
|
| 74 | - return $this->_ac; |
|
| 75 | - } |
|
| 60 | + /** |
|
| 61 | + * Validate attribute certificate. |
|
| 62 | + * |
|
| 63 | + * @throws ACValidationException If validation fails |
|
| 64 | + * |
|
| 65 | + * @return AttributeCertificate Validated AC |
|
| 66 | + */ |
|
| 67 | + public function validate(): AttributeCertificate |
|
| 68 | + { |
|
| 69 | + $this->_validateHolder(); |
|
| 70 | + $issuer = $this->_verifyIssuer(); |
|
| 71 | + $this->_validateIssuerProfile($issuer); |
|
| 72 | + $this->_validateTime(); |
|
| 73 | + $this->_validateTargeting(); |
|
| 74 | + return $this->_ac; |
|
| 75 | + } |
|
| 76 | 76 | |
| 77 | - /** |
|
| 78 | - * Validate AC holder's certification. |
|
| 79 | - * |
|
| 80 | - * @throws ACValidationException |
|
| 81 | - * |
|
| 82 | - * @return Certificate Certificate of the AC's holder |
|
| 83 | - */ |
|
| 84 | - private function _validateHolder(): Certificate |
|
| 85 | - { |
|
| 86 | - $path = $this->_config->holderPath(); |
|
| 87 | - $config = PathValidationConfig::defaultConfig() |
|
| 88 | - ->withMaxLength(count($path)) |
|
| 89 | - ->withDateTime($this->_config->evaluationTime()); |
|
| 90 | - try { |
|
| 91 | - $holder = $path->validate($config, $this->_crypto)->certificate(); |
|
| 92 | - } catch (PathValidationException $e) { |
|
| 93 | - throw new ACValidationException( |
|
| 94 | - "Failed to validate holder PKC's certification path.", 0, $e); |
|
| 95 | - } |
|
| 96 | - if (!$this->_ac->isHeldBy($holder)) { |
|
| 97 | - throw new ACValidationException("Name mismatch of AC's holder PKC."); |
|
| 98 | - } |
|
| 99 | - return $holder; |
|
| 100 | - } |
|
| 77 | + /** |
|
| 78 | + * Validate AC holder's certification. |
|
| 79 | + * |
|
| 80 | + * @throws ACValidationException |
|
| 81 | + * |
|
| 82 | + * @return Certificate Certificate of the AC's holder |
|
| 83 | + */ |
|
| 84 | + private function _validateHolder(): Certificate |
|
| 85 | + { |
|
| 86 | + $path = $this->_config->holderPath(); |
|
| 87 | + $config = PathValidationConfig::defaultConfig() |
|
| 88 | + ->withMaxLength(count($path)) |
|
| 89 | + ->withDateTime($this->_config->evaluationTime()); |
|
| 90 | + try { |
|
| 91 | + $holder = $path->validate($config, $this->_crypto)->certificate(); |
|
| 92 | + } catch (PathValidationException $e) { |
|
| 93 | + throw new ACValidationException( |
|
| 94 | + "Failed to validate holder PKC's certification path.", 0, $e); |
|
| 95 | + } |
|
| 96 | + if (!$this->_ac->isHeldBy($holder)) { |
|
| 97 | + throw new ACValidationException("Name mismatch of AC's holder PKC."); |
|
| 98 | + } |
|
| 99 | + return $holder; |
|
| 100 | + } |
|
| 101 | 101 | |
| 102 | - /** |
|
| 103 | - * Verify AC's signature and issuer's certification. |
|
| 104 | - * |
|
| 105 | - * @throws ACValidationException |
|
| 106 | - * |
|
| 107 | - * @return Certificate Certificate of the AC's issuer |
|
| 108 | - */ |
|
| 109 | - private function _verifyIssuer(): Certificate |
|
| 110 | - { |
|
| 111 | - $path = $this->_config->issuerPath(); |
|
| 112 | - $config = PathValidationConfig::defaultConfig() |
|
| 113 | - ->withMaxLength(count($path)) |
|
| 114 | - ->withDateTime($this->_config->evaluationTime()); |
|
| 115 | - try { |
|
| 116 | - $issuer = $path->validate($config, $this->_crypto)->certificate(); |
|
| 117 | - } catch (PathValidationException $e) { |
|
| 118 | - throw new ACValidationException( |
|
| 119 | - "Failed to validate issuer PKC's certification path.", 0, $e); |
|
| 120 | - } |
|
| 121 | - if (!$this->_ac->isIssuedBy($issuer)) { |
|
| 122 | - throw new ACValidationException("Name mismatch of AC's issuer PKC."); |
|
| 123 | - } |
|
| 124 | - $pubkey_info = $issuer->tbsCertificate()->subjectPublicKeyInfo(); |
|
| 125 | - if (!$this->_ac->verify($pubkey_info, $this->_crypto)) { |
|
| 126 | - throw new ACValidationException('Failed to verify signature.'); |
|
| 127 | - } |
|
| 128 | - return $issuer; |
|
| 129 | - } |
|
| 102 | + /** |
|
| 103 | + * Verify AC's signature and issuer's certification. |
|
| 104 | + * |
|
| 105 | + * @throws ACValidationException |
|
| 106 | + * |
|
| 107 | + * @return Certificate Certificate of the AC's issuer |
|
| 108 | + */ |
|
| 109 | + private function _verifyIssuer(): Certificate |
|
| 110 | + { |
|
| 111 | + $path = $this->_config->issuerPath(); |
|
| 112 | + $config = PathValidationConfig::defaultConfig() |
|
| 113 | + ->withMaxLength(count($path)) |
|
| 114 | + ->withDateTime($this->_config->evaluationTime()); |
|
| 115 | + try { |
|
| 116 | + $issuer = $path->validate($config, $this->_crypto)->certificate(); |
|
| 117 | + } catch (PathValidationException $e) { |
|
| 118 | + throw new ACValidationException( |
|
| 119 | + "Failed to validate issuer PKC's certification path.", 0, $e); |
|
| 120 | + } |
|
| 121 | + if (!$this->_ac->isIssuedBy($issuer)) { |
|
| 122 | + throw new ACValidationException("Name mismatch of AC's issuer PKC."); |
|
| 123 | + } |
|
| 124 | + $pubkey_info = $issuer->tbsCertificate()->subjectPublicKeyInfo(); |
|
| 125 | + if (!$this->_ac->verify($pubkey_info, $this->_crypto)) { |
|
| 126 | + throw new ACValidationException('Failed to verify signature.'); |
|
| 127 | + } |
|
| 128 | + return $issuer; |
|
| 129 | + } |
|
| 130 | 130 | |
| 131 | - /** |
|
| 132 | - * Validate AC issuer's profile. |
|
| 133 | - * |
|
| 134 | - * @see https://tools.ietf.org/html/rfc5755#section-4.5 |
|
| 135 | - * |
|
| 136 | - * @param Certificate $cert |
|
| 137 | - * |
|
| 138 | - * @throws ACValidationException |
|
| 139 | - */ |
|
| 140 | - private function _validateIssuerProfile(Certificate $cert): void |
|
| 141 | - { |
|
| 142 | - $exts = $cert->tbsCertificate()->extensions(); |
|
| 143 | - if ($exts->hasKeyUsage() && !$exts->keyUsage()->isDigitalSignature()) { |
|
| 144 | - throw new ACValidationException( |
|
| 145 | - "Issuer PKC's Key Usage extension doesn't permit" . |
|
| 146 | - ' verification of digital signatures.'); |
|
| 147 | - } |
|
| 148 | - if ($exts->hasBasicConstraints() && $exts->basicConstraints()->isCA()) { |
|
| 149 | - throw new ACValidationException('Issuer PKC must not be a CA.'); |
|
| 150 | - } |
|
| 151 | - } |
|
| 131 | + /** |
|
| 132 | + * Validate AC issuer's profile. |
|
| 133 | + * |
|
| 134 | + * @see https://tools.ietf.org/html/rfc5755#section-4.5 |
|
| 135 | + * |
|
| 136 | + * @param Certificate $cert |
|
| 137 | + * |
|
| 138 | + * @throws ACValidationException |
|
| 139 | + */ |
|
| 140 | + private function _validateIssuerProfile(Certificate $cert): void |
|
| 141 | + { |
|
| 142 | + $exts = $cert->tbsCertificate()->extensions(); |
|
| 143 | + if ($exts->hasKeyUsage() && !$exts->keyUsage()->isDigitalSignature()) { |
|
| 144 | + throw new ACValidationException( |
|
| 145 | + "Issuer PKC's Key Usage extension doesn't permit" . |
|
| 146 | + ' verification of digital signatures.'); |
|
| 147 | + } |
|
| 148 | + if ($exts->hasBasicConstraints() && $exts->basicConstraints()->isCA()) { |
|
| 149 | + throw new ACValidationException('Issuer PKC must not be a CA.'); |
|
| 150 | + } |
|
| 151 | + } |
|
| 152 | 152 | |
| 153 | - /** |
|
| 154 | - * Validate AC's validity period. |
|
| 155 | - * |
|
| 156 | - * @throws ACValidationException |
|
| 157 | - */ |
|
| 158 | - private function _validateTime(): void |
|
| 159 | - { |
|
| 160 | - $t = $this->_config->evaluationTime(); |
|
| 161 | - $validity = $this->_ac->acinfo()->validityPeriod(); |
|
| 162 | - if ($validity->notBeforeTime()->diff($t)->invert) { |
|
| 163 | - throw new ACValidationException('Validity period has not started.'); |
|
| 164 | - } |
|
| 165 | - if ($t->diff($validity->notAfterTime())->invert) { |
|
| 166 | - throw new ACValidationException('Attribute certificate has expired.'); |
|
| 167 | - } |
|
| 168 | - } |
|
| 153 | + /** |
|
| 154 | + * Validate AC's validity period. |
|
| 155 | + * |
|
| 156 | + * @throws ACValidationException |
|
| 157 | + */ |
|
| 158 | + private function _validateTime(): void |
|
| 159 | + { |
|
| 160 | + $t = $this->_config->evaluationTime(); |
|
| 161 | + $validity = $this->_ac->acinfo()->validityPeriod(); |
|
| 162 | + if ($validity->notBeforeTime()->diff($t)->invert) { |
|
| 163 | + throw new ACValidationException('Validity period has not started.'); |
|
| 164 | + } |
|
| 165 | + if ($t->diff($validity->notAfterTime())->invert) { |
|
| 166 | + throw new ACValidationException('Attribute certificate has expired.'); |
|
| 167 | + } |
|
| 168 | + } |
|
| 169 | 169 | |
| 170 | - /** |
|
| 171 | - * Validate AC's target information. |
|
| 172 | - * |
|
| 173 | - * @throws ACValidationException |
|
| 174 | - */ |
|
| 175 | - private function _validateTargeting(): void |
|
| 176 | - { |
|
| 177 | - $exts = $this->_ac->acinfo()->extensions(); |
|
| 178 | - // if target information extension is not present |
|
| 179 | - if (!$exts->has(Extension::OID_TARGET_INFORMATION)) { |
|
| 180 | - return; |
|
| 181 | - } |
|
| 182 | - $ext = $exts->get(Extension::OID_TARGET_INFORMATION); |
|
| 183 | - if ($ext instanceof TargetInformationExtension && |
|
| 184 | - !$this->_hasMatchingTarget($ext->targets())) { |
|
| 185 | - throw new ACValidationException( |
|
| 186 | - "Attribute certificate doesn't have a matching target."); |
|
| 187 | - } |
|
| 188 | - } |
|
| 170 | + /** |
|
| 171 | + * Validate AC's target information. |
|
| 172 | + * |
|
| 173 | + * @throws ACValidationException |
|
| 174 | + */ |
|
| 175 | + private function _validateTargeting(): void |
|
| 176 | + { |
|
| 177 | + $exts = $this->_ac->acinfo()->extensions(); |
|
| 178 | + // if target information extension is not present |
|
| 179 | + if (!$exts->has(Extension::OID_TARGET_INFORMATION)) { |
|
| 180 | + return; |
|
| 181 | + } |
|
| 182 | + $ext = $exts->get(Extension::OID_TARGET_INFORMATION); |
|
| 183 | + if ($ext instanceof TargetInformationExtension && |
|
| 184 | + !$this->_hasMatchingTarget($ext->targets())) { |
|
| 185 | + throw new ACValidationException( |
|
| 186 | + "Attribute certificate doesn't have a matching target."); |
|
| 187 | + } |
|
| 188 | + } |
|
| 189 | 189 | |
| 190 | - /** |
|
| 191 | - * Check whether validation configuration has matching targets. |
|
| 192 | - * |
|
| 193 | - * @param Targets $targets Set of eligible targets |
|
| 194 | - * |
|
| 195 | - * @return bool |
|
| 196 | - */ |
|
| 197 | - private function _hasMatchingTarget(Targets $targets): bool |
|
| 198 | - { |
|
| 199 | - foreach ($this->_config->targets() as $target) { |
|
| 200 | - if ($targets->hasTarget($target)) { |
|
| 201 | - return true; |
|
| 202 | - } |
|
| 203 | - } |
|
| 204 | - return false; |
|
| 205 | - } |
|
| 190 | + /** |
|
| 191 | + * Check whether validation configuration has matching targets. |
|
| 192 | + * |
|
| 193 | + * @param Targets $targets Set of eligible targets |
|
| 194 | + * |
|
| 195 | + * @return bool |
|
| 196 | + */ |
|
| 197 | + private function _hasMatchingTarget(Targets $targets): bool |
|
| 198 | + { |
|
| 199 | + foreach ($this->_config->targets() as $target) { |
|
| 200 | + if ($targets->hasTarget($target)) { |
|
| 201 | + return true; |
|
| 202 | + } |
|
| 203 | + } |
|
| 204 | + return false; |
|
| 205 | + } |
|
| 206 | 206 | } |
@@ -22,450 +22,450 @@ |
||
| 22 | 22 | */ |
| 23 | 23 | class AttributeCertificateInfo |
| 24 | 24 | { |
| 25 | - const VERSION_2 = 1; |
|
| 26 | - |
|
| 27 | - /** |
|
| 28 | - * AC version. |
|
| 29 | - * |
|
| 30 | - * @var int |
|
| 31 | - */ |
|
| 32 | - protected $_version; |
|
| 33 | - |
|
| 34 | - /** |
|
| 35 | - * AC holder. |
|
| 36 | - * |
|
| 37 | - * @var Holder |
|
| 38 | - */ |
|
| 39 | - protected $_holder; |
|
| 40 | - |
|
| 41 | - /** |
|
| 42 | - * AC issuer. |
|
| 43 | - * |
|
| 44 | - * @var AttCertIssuer |
|
| 45 | - */ |
|
| 46 | - protected $_issuer; |
|
| 47 | - |
|
| 48 | - /** |
|
| 49 | - * Signature algorithm identifier. |
|
| 50 | - * |
|
| 51 | - * @var SignatureAlgorithmIdentifier |
|
| 52 | - */ |
|
| 53 | - protected $_signature; |
|
| 54 | - |
|
| 55 | - /** |
|
| 56 | - * AC serial number as a base 10 integer. |
|
| 57 | - * |
|
| 58 | - * @var string |
|
| 59 | - */ |
|
| 60 | - protected $_serialNumber; |
|
| 61 | - |
|
| 62 | - /** |
|
| 63 | - * Validity period. |
|
| 64 | - * |
|
| 65 | - * @var AttCertValidityPeriod |
|
| 66 | - */ |
|
| 67 | - protected $_attrCertValidityPeriod; |
|
| 68 | - |
|
| 69 | - /** |
|
| 70 | - * Attributes. |
|
| 71 | - * |
|
| 72 | - * @var Attributes |
|
| 73 | - */ |
|
| 74 | - protected $_attributes; |
|
| 75 | - |
|
| 76 | - /** |
|
| 77 | - * Issuer unique identifier. |
|
| 78 | - * |
|
| 79 | - * @var null|UniqueIdentifier |
|
| 80 | - */ |
|
| 81 | - protected $_issuerUniqueID; |
|
| 82 | - |
|
| 83 | - /** |
|
| 84 | - * Extensions. |
|
| 85 | - * |
|
| 86 | - * @var Extensions |
|
| 87 | - */ |
|
| 88 | - protected $_extensions; |
|
| 89 | - |
|
| 90 | - /** |
|
| 91 | - * Constructor. |
|
| 92 | - * |
|
| 93 | - * @param Holder $holder AC holder |
|
| 94 | - * @param AttCertIssuer $issuer AC issuer |
|
| 95 | - * @param AttCertValidityPeriod $validity Validity |
|
| 96 | - * @param Attributes $attribs Attributes |
|
| 97 | - */ |
|
| 98 | - public function __construct(Holder $holder, AttCertIssuer $issuer, |
|
| 99 | - AttCertValidityPeriod $validity, Attributes $attribs) |
|
| 100 | - { |
|
| 101 | - $this->_version = self::VERSION_2; |
|
| 102 | - $this->_holder = $holder; |
|
| 103 | - $this->_issuer = $issuer; |
|
| 104 | - $this->_attrCertValidityPeriod = $validity; |
|
| 105 | - $this->_attributes = $attribs; |
|
| 106 | - $this->_extensions = new Extensions(); |
|
| 107 | - } |
|
| 108 | - |
|
| 109 | - /** |
|
| 110 | - * Initialize from ASN.1. |
|
| 111 | - * |
|
| 112 | - * @param Sequence $seq |
|
| 113 | - * |
|
| 114 | - * @throws \UnexpectedValueException |
|
| 115 | - * |
|
| 116 | - * @return self |
|
| 117 | - */ |
|
| 118 | - public static function fromASN1(Sequence $seq): self |
|
| 119 | - { |
|
| 120 | - $idx = 0; |
|
| 121 | - $version = $seq->at($idx++)->asInteger()->intNumber(); |
|
| 122 | - if (self::VERSION_2 !== $version) { |
|
| 123 | - throw new \UnexpectedValueException('Version must be 2.'); |
|
| 124 | - } |
|
| 125 | - $holder = Holder::fromASN1($seq->at($idx++)->asSequence()); |
|
| 126 | - $issuer = AttCertIssuer::fromASN1($seq->at($idx++)); |
|
| 127 | - $signature = AlgorithmIdentifier::fromASN1($seq->at($idx++)->asSequence()); |
|
| 128 | - if (!$signature instanceof SignatureAlgorithmIdentifier) { |
|
| 129 | - throw new \UnexpectedValueException( |
|
| 130 | - 'Unsupported signature algorithm ' . $signature->oid() . '.'); |
|
| 131 | - } |
|
| 132 | - $serial = $seq->at($idx++)->asInteger()->number(); |
|
| 133 | - $validity = AttCertValidityPeriod::fromASN1($seq->at($idx++)->asSequence()); |
|
| 134 | - $attribs = Attributes::fromASN1($seq->at($idx++)->asSequence()); |
|
| 135 | - $obj = new self($holder, $issuer, $validity, $attribs); |
|
| 136 | - $obj->_signature = $signature; |
|
| 137 | - $obj->_serialNumber = $serial; |
|
| 138 | - if ($seq->has($idx, Element::TYPE_BIT_STRING)) { |
|
| 139 | - $obj->_issuerUniqueID = UniqueIdentifier::fromASN1( |
|
| 140 | - $seq->at($idx++)->asBitString()); |
|
| 141 | - } |
|
| 142 | - if ($seq->has($idx, Element::TYPE_SEQUENCE)) { |
|
| 143 | - $obj->_extensions = Extensions::fromASN1( |
|
| 144 | - $seq->at($idx++)->asSequence()); |
|
| 145 | - } |
|
| 146 | - return $obj; |
|
| 147 | - } |
|
| 148 | - |
|
| 149 | - /** |
|
| 150 | - * Get self with holder. |
|
| 151 | - * |
|
| 152 | - * @param Holder $holder |
|
| 153 | - * |
|
| 154 | - * @return self |
|
| 155 | - */ |
|
| 156 | - public function withHolder(Holder $holder): self |
|
| 157 | - { |
|
| 158 | - $obj = clone $this; |
|
| 159 | - $obj->_holder = $holder; |
|
| 160 | - return $obj; |
|
| 161 | - } |
|
| 162 | - |
|
| 163 | - /** |
|
| 164 | - * Get self with issuer. |
|
| 165 | - * |
|
| 166 | - * @param AttCertIssuer $issuer |
|
| 167 | - * |
|
| 168 | - * @return self |
|
| 169 | - */ |
|
| 170 | - public function withIssuer(AttCertIssuer $issuer): self |
|
| 171 | - { |
|
| 172 | - $obj = clone $this; |
|
| 173 | - $obj->_issuer = $issuer; |
|
| 174 | - return $obj; |
|
| 175 | - } |
|
| 176 | - |
|
| 177 | - /** |
|
| 178 | - * Get self with signature algorithm identifier. |
|
| 179 | - * |
|
| 180 | - * @param SignatureAlgorithmIdentifier $algo |
|
| 181 | - * |
|
| 182 | - * @return self |
|
| 183 | - */ |
|
| 184 | - public function withSignature(SignatureAlgorithmIdentifier $algo): self |
|
| 185 | - { |
|
| 186 | - $obj = clone $this; |
|
| 187 | - $obj->_signature = $algo; |
|
| 188 | - return $obj; |
|
| 189 | - } |
|
| 190 | - |
|
| 191 | - /** |
|
| 192 | - * Get self with serial number. |
|
| 193 | - * |
|
| 194 | - * @param int|string $serial Base 10 serial number |
|
| 195 | - * |
|
| 196 | - * @return self |
|
| 197 | - */ |
|
| 198 | - public function withSerialNumber($serial): self |
|
| 199 | - { |
|
| 200 | - $obj = clone $this; |
|
| 201 | - $obj->_serialNumber = strval($serial); |
|
| 202 | - return $obj; |
|
| 203 | - } |
|
| 204 | - |
|
| 205 | - /** |
|
| 206 | - * Get self with random positive serial number. |
|
| 207 | - * |
|
| 208 | - * @param int $size Number of random bytes |
|
| 209 | - * |
|
| 210 | - * @return self |
|
| 211 | - */ |
|
| 212 | - public function withRandomSerialNumber(int $size = 16): self |
|
| 213 | - { |
|
| 214 | - // ensure that first byte is always non-zero and having first bit unset |
|
| 215 | - $num = gmp_init(mt_rand(1, 0x7f), 10); |
|
| 216 | - for ($i = 1; $i < $size; ++$i) { |
|
| 217 | - $num <<= 8; |
|
| 218 | - $num += mt_rand(0, 0xff); |
|
| 219 | - } |
|
| 220 | - return $this->withSerialNumber(gmp_strval($num, 10)); |
|
| 221 | - } |
|
| 222 | - |
|
| 223 | - /** |
|
| 224 | - * Get self with validity period. |
|
| 225 | - * |
|
| 226 | - * @param AttCertValidityPeriod $validity |
|
| 227 | - * |
|
| 228 | - * @return self |
|
| 229 | - */ |
|
| 230 | - public function withValidity(AttCertValidityPeriod $validity): self |
|
| 231 | - { |
|
| 232 | - $obj = clone $this; |
|
| 233 | - $obj->_attrCertValidityPeriod = $validity; |
|
| 234 | - return $obj; |
|
| 235 | - } |
|
| 236 | - |
|
| 237 | - /** |
|
| 238 | - * Get self with attributes. |
|
| 239 | - * |
|
| 240 | - * @param Attributes $attribs |
|
| 241 | - * |
|
| 242 | - * @return self |
|
| 243 | - */ |
|
| 244 | - public function withAttributes(Attributes $attribs): self |
|
| 245 | - { |
|
| 246 | - $obj = clone $this; |
|
| 247 | - $obj->_attributes = $attribs; |
|
| 248 | - return $obj; |
|
| 249 | - } |
|
| 250 | - |
|
| 251 | - /** |
|
| 252 | - * Get self with issuer unique identifier. |
|
| 253 | - * |
|
| 254 | - * @param UniqueIdentifier $uid |
|
| 255 | - * |
|
| 256 | - * @return self |
|
| 257 | - */ |
|
| 258 | - public function withIssuerUniqueID(UniqueIdentifier $uid): self |
|
| 259 | - { |
|
| 260 | - $obj = clone $this; |
|
| 261 | - $obj->_issuerUniqueID = $uid; |
|
| 262 | - return $obj; |
|
| 263 | - } |
|
| 264 | - |
|
| 265 | - /** |
|
| 266 | - * Get self with extensions. |
|
| 267 | - * |
|
| 268 | - * @param Extensions $extensions |
|
| 269 | - * |
|
| 270 | - * @return self |
|
| 271 | - */ |
|
| 272 | - public function withExtensions(Extensions $extensions): self |
|
| 273 | - { |
|
| 274 | - $obj = clone $this; |
|
| 275 | - $obj->_extensions = $extensions; |
|
| 276 | - return $obj; |
|
| 277 | - } |
|
| 278 | - |
|
| 279 | - /** |
|
| 280 | - * Get self with extensions added. |
|
| 281 | - * |
|
| 282 | - * @param Extension ...$exts One or more Extension objects |
|
| 283 | - * |
|
| 284 | - * @return self |
|
| 285 | - */ |
|
| 286 | - public function withAdditionalExtensions(Extension ...$exts): self |
|
| 287 | - { |
|
| 288 | - $obj = clone $this; |
|
| 289 | - $obj->_extensions = $obj->_extensions->withExtensions(...$exts); |
|
| 290 | - return $obj; |
|
| 291 | - } |
|
| 292 | - |
|
| 293 | - /** |
|
| 294 | - * Get version. |
|
| 295 | - * |
|
| 296 | - * @return int |
|
| 297 | - */ |
|
| 298 | - public function version(): int |
|
| 299 | - { |
|
| 300 | - return $this->_version; |
|
| 301 | - } |
|
| 302 | - |
|
| 303 | - /** |
|
| 304 | - * Get AC holder. |
|
| 305 | - * |
|
| 306 | - * @return Holder |
|
| 307 | - */ |
|
| 308 | - public function holder(): Holder |
|
| 309 | - { |
|
| 310 | - return $this->_holder; |
|
| 311 | - } |
|
| 312 | - |
|
| 313 | - /** |
|
| 314 | - * Get AC issuer. |
|
| 315 | - * |
|
| 316 | - * @return AttCertIssuer |
|
| 317 | - */ |
|
| 318 | - public function issuer(): AttCertIssuer |
|
| 319 | - { |
|
| 320 | - return $this->_issuer; |
|
| 321 | - } |
|
| 322 | - |
|
| 323 | - /** |
|
| 324 | - * Check whether signature is set. |
|
| 325 | - * |
|
| 326 | - * @return bool |
|
| 327 | - */ |
|
| 328 | - public function hasSignature(): bool |
|
| 329 | - { |
|
| 330 | - return isset($this->_signature); |
|
| 331 | - } |
|
| 332 | - |
|
| 333 | - /** |
|
| 334 | - * Get signature algorithm identifier. |
|
| 335 | - * |
|
| 336 | - * @throws \LogicException If not set |
|
| 337 | - * |
|
| 338 | - * @return SignatureAlgorithmIdentifier |
|
| 339 | - */ |
|
| 340 | - public function signature(): SignatureAlgorithmIdentifier |
|
| 341 | - { |
|
| 342 | - if (!$this->hasSignature()) { |
|
| 343 | - throw new \LogicException('signature not set.'); |
|
| 344 | - } |
|
| 345 | - return $this->_signature; |
|
| 346 | - } |
|
| 347 | - |
|
| 348 | - /** |
|
| 349 | - * Check whether serial number is present. |
|
| 350 | - * |
|
| 351 | - * @return bool |
|
| 352 | - */ |
|
| 353 | - public function hasSerialNumber(): bool |
|
| 354 | - { |
|
| 355 | - return isset($this->_serialNumber); |
|
| 356 | - } |
|
| 357 | - |
|
| 358 | - /** |
|
| 359 | - * Get AC serial number as a base 10 integer. |
|
| 360 | - * |
|
| 361 | - * @throws \LogicException If not set |
|
| 362 | - * |
|
| 363 | - * @return string |
|
| 364 | - */ |
|
| 365 | - public function serialNumber(): string |
|
| 366 | - { |
|
| 367 | - if (!$this->hasSerialNumber()) { |
|
| 368 | - throw new \LogicException('serialNumber not set.'); |
|
| 369 | - } |
|
| 370 | - return $this->_serialNumber; |
|
| 371 | - } |
|
| 372 | - |
|
| 373 | - /** |
|
| 374 | - * Get validity period. |
|
| 375 | - * |
|
| 376 | - * @return AttCertValidityPeriod |
|
| 377 | - */ |
|
| 378 | - public function validityPeriod(): AttCertValidityPeriod |
|
| 379 | - { |
|
| 380 | - return $this->_attrCertValidityPeriod; |
|
| 381 | - } |
|
| 382 | - |
|
| 383 | - /** |
|
| 384 | - * Get attributes. |
|
| 385 | - * |
|
| 386 | - * @return Attributes |
|
| 387 | - */ |
|
| 388 | - public function attributes(): Attributes |
|
| 389 | - { |
|
| 390 | - return $this->_attributes; |
|
| 391 | - } |
|
| 392 | - |
|
| 393 | - /** |
|
| 394 | - * Check whether issuer unique identifier is present. |
|
| 395 | - * |
|
| 396 | - * @return bool |
|
| 397 | - */ |
|
| 398 | - public function hasIssuerUniqueID(): bool |
|
| 399 | - { |
|
| 400 | - return isset($this->_issuerUniqueID); |
|
| 401 | - } |
|
| 402 | - |
|
| 403 | - /** |
|
| 404 | - * Get issuer unique identifier. |
|
| 405 | - * |
|
| 406 | - * @throws \LogicException If not set |
|
| 407 | - * |
|
| 408 | - * @return UniqueIdentifier |
|
| 409 | - */ |
|
| 410 | - public function issuerUniqueID(): UniqueIdentifier |
|
| 411 | - { |
|
| 412 | - if (!$this->hasIssuerUniqueID()) { |
|
| 413 | - throw new \LogicException('issuerUniqueID not set.'); |
|
| 414 | - } |
|
| 415 | - return $this->_issuerUniqueID; |
|
| 416 | - } |
|
| 417 | - |
|
| 418 | - /** |
|
| 419 | - * Get extensions. |
|
| 420 | - * |
|
| 421 | - * @return Extensions |
|
| 422 | - */ |
|
| 423 | - public function extensions(): Extensions |
|
| 424 | - { |
|
| 425 | - return $this->_extensions; |
|
| 426 | - } |
|
| 427 | - |
|
| 428 | - /** |
|
| 429 | - * Get ASN.1 structure. |
|
| 430 | - * |
|
| 431 | - * @return Sequence |
|
| 432 | - */ |
|
| 433 | - public function toASN1(): Sequence |
|
| 434 | - { |
|
| 435 | - $elements = [new Integer($this->_version), $this->_holder->toASN1(), |
|
| 436 | - $this->_issuer->toASN1(), $this->signature()->toASN1(), |
|
| 437 | - new Integer($this->serialNumber()), |
|
| 438 | - $this->_attrCertValidityPeriod->toASN1(), |
|
| 439 | - $this->_attributes->toASN1(), ]; |
|
| 440 | - if (isset($this->_issuerUniqueID)) { |
|
| 441 | - $elements[] = $this->_issuerUniqueID->toASN1(); |
|
| 442 | - } |
|
| 443 | - if (count($this->_extensions)) { |
|
| 444 | - $elements[] = $this->_extensions->toASN1(); |
|
| 445 | - } |
|
| 446 | - return new Sequence(...$elements); |
|
| 447 | - } |
|
| 448 | - |
|
| 449 | - /** |
|
| 450 | - * Create signed attribute certificate. |
|
| 451 | - * |
|
| 452 | - * @param SignatureAlgorithmIdentifier $algo Signature algorithm |
|
| 453 | - * @param PrivateKeyInfo $privkey_info Private key |
|
| 454 | - * @param null|Crypto $crypto Crypto engine, use default if not set |
|
| 455 | - * |
|
| 456 | - * @return AttributeCertificate |
|
| 457 | - */ |
|
| 458 | - public function sign(SignatureAlgorithmIdentifier $algo, |
|
| 459 | - PrivateKeyInfo $privkey_info, ?Crypto $crypto = null): AttributeCertificate |
|
| 460 | - { |
|
| 461 | - $crypto = $crypto ?? Crypto::getDefault(); |
|
| 462 | - $aci = clone $this; |
|
| 463 | - if (!isset($aci->_serialNumber)) { |
|
| 464 | - $aci->_serialNumber = '0'; |
|
| 465 | - } |
|
| 466 | - $aci->_signature = $algo; |
|
| 467 | - $data = $aci->toASN1()->toDER(); |
|
| 468 | - $signature = $crypto->sign($data, $privkey_info, $algo); |
|
| 469 | - return new AttributeCertificate($aci, $algo, $signature); |
|
| 470 | - } |
|
| 25 | + const VERSION_2 = 1; |
|
| 26 | + |
|
| 27 | + /** |
|
| 28 | + * AC version. |
|
| 29 | + * |
|
| 30 | + * @var int |
|
| 31 | + */ |
|
| 32 | + protected $_version; |
|
| 33 | + |
|
| 34 | + /** |
|
| 35 | + * AC holder. |
|
| 36 | + * |
|
| 37 | + * @var Holder |
|
| 38 | + */ |
|
| 39 | + protected $_holder; |
|
| 40 | + |
|
| 41 | + /** |
|
| 42 | + * AC issuer. |
|
| 43 | + * |
|
| 44 | + * @var AttCertIssuer |
|
| 45 | + */ |
|
| 46 | + protected $_issuer; |
|
| 47 | + |
|
| 48 | + /** |
|
| 49 | + * Signature algorithm identifier. |
|
| 50 | + * |
|
| 51 | + * @var SignatureAlgorithmIdentifier |
|
| 52 | + */ |
|
| 53 | + protected $_signature; |
|
| 54 | + |
|
| 55 | + /** |
|
| 56 | + * AC serial number as a base 10 integer. |
|
| 57 | + * |
|
| 58 | + * @var string |
|
| 59 | + */ |
|
| 60 | + protected $_serialNumber; |
|
| 61 | + |
|
| 62 | + /** |
|
| 63 | + * Validity period. |
|
| 64 | + * |
|
| 65 | + * @var AttCertValidityPeriod |
|
| 66 | + */ |
|
| 67 | + protected $_attrCertValidityPeriod; |
|
| 68 | + |
|
| 69 | + /** |
|
| 70 | + * Attributes. |
|
| 71 | + * |
|
| 72 | + * @var Attributes |
|
| 73 | + */ |
|
| 74 | + protected $_attributes; |
|
| 75 | + |
|
| 76 | + /** |
|
| 77 | + * Issuer unique identifier. |
|
| 78 | + * |
|
| 79 | + * @var null|UniqueIdentifier |
|
| 80 | + */ |
|
| 81 | + protected $_issuerUniqueID; |
|
| 82 | + |
|
| 83 | + /** |
|
| 84 | + * Extensions. |
|
| 85 | + * |
|
| 86 | + * @var Extensions |
|
| 87 | + */ |
|
| 88 | + protected $_extensions; |
|
| 89 | + |
|
| 90 | + /** |
|
| 91 | + * Constructor. |
|
| 92 | + * |
|
| 93 | + * @param Holder $holder AC holder |
|
| 94 | + * @param AttCertIssuer $issuer AC issuer |
|
| 95 | + * @param AttCertValidityPeriod $validity Validity |
|
| 96 | + * @param Attributes $attribs Attributes |
|
| 97 | + */ |
|
| 98 | + public function __construct(Holder $holder, AttCertIssuer $issuer, |
|
| 99 | + AttCertValidityPeriod $validity, Attributes $attribs) |
|
| 100 | + { |
|
| 101 | + $this->_version = self::VERSION_2; |
|
| 102 | + $this->_holder = $holder; |
|
| 103 | + $this->_issuer = $issuer; |
|
| 104 | + $this->_attrCertValidityPeriod = $validity; |
|
| 105 | + $this->_attributes = $attribs; |
|
| 106 | + $this->_extensions = new Extensions(); |
|
| 107 | + } |
|
| 108 | + |
|
| 109 | + /** |
|
| 110 | + * Initialize from ASN.1. |
|
| 111 | + * |
|
| 112 | + * @param Sequence $seq |
|
| 113 | + * |
|
| 114 | + * @throws \UnexpectedValueException |
|
| 115 | + * |
|
| 116 | + * @return self |
|
| 117 | + */ |
|
| 118 | + public static function fromASN1(Sequence $seq): self |
|
| 119 | + { |
|
| 120 | + $idx = 0; |
|
| 121 | + $version = $seq->at($idx++)->asInteger()->intNumber(); |
|
| 122 | + if (self::VERSION_2 !== $version) { |
|
| 123 | + throw new \UnexpectedValueException('Version must be 2.'); |
|
| 124 | + } |
|
| 125 | + $holder = Holder::fromASN1($seq->at($idx++)->asSequence()); |
|
| 126 | + $issuer = AttCertIssuer::fromASN1($seq->at($idx++)); |
|
| 127 | + $signature = AlgorithmIdentifier::fromASN1($seq->at($idx++)->asSequence()); |
|
| 128 | + if (!$signature instanceof SignatureAlgorithmIdentifier) { |
|
| 129 | + throw new \UnexpectedValueException( |
|
| 130 | + 'Unsupported signature algorithm ' . $signature->oid() . '.'); |
|
| 131 | + } |
|
| 132 | + $serial = $seq->at($idx++)->asInteger()->number(); |
|
| 133 | + $validity = AttCertValidityPeriod::fromASN1($seq->at($idx++)->asSequence()); |
|
| 134 | + $attribs = Attributes::fromASN1($seq->at($idx++)->asSequence()); |
|
| 135 | + $obj = new self($holder, $issuer, $validity, $attribs); |
|
| 136 | + $obj->_signature = $signature; |
|
| 137 | + $obj->_serialNumber = $serial; |
|
| 138 | + if ($seq->has($idx, Element::TYPE_BIT_STRING)) { |
|
| 139 | + $obj->_issuerUniqueID = UniqueIdentifier::fromASN1( |
|
| 140 | + $seq->at($idx++)->asBitString()); |
|
| 141 | + } |
|
| 142 | + if ($seq->has($idx, Element::TYPE_SEQUENCE)) { |
|
| 143 | + $obj->_extensions = Extensions::fromASN1( |
|
| 144 | + $seq->at($idx++)->asSequence()); |
|
| 145 | + } |
|
| 146 | + return $obj; |
|
| 147 | + } |
|
| 148 | + |
|
| 149 | + /** |
|
| 150 | + * Get self with holder. |
|
| 151 | + * |
|
| 152 | + * @param Holder $holder |
|
| 153 | + * |
|
| 154 | + * @return self |
|
| 155 | + */ |
|
| 156 | + public function withHolder(Holder $holder): self |
|
| 157 | + { |
|
| 158 | + $obj = clone $this; |
|
| 159 | + $obj->_holder = $holder; |
|
| 160 | + return $obj; |
|
| 161 | + } |
|
| 162 | + |
|
| 163 | + /** |
|
| 164 | + * Get self with issuer. |
|
| 165 | + * |
|
| 166 | + * @param AttCertIssuer $issuer |
|
| 167 | + * |
|
| 168 | + * @return self |
|
| 169 | + */ |
|
| 170 | + public function withIssuer(AttCertIssuer $issuer): self |
|
| 171 | + { |
|
| 172 | + $obj = clone $this; |
|
| 173 | + $obj->_issuer = $issuer; |
|
| 174 | + return $obj; |
|
| 175 | + } |
|
| 176 | + |
|
| 177 | + /** |
|
| 178 | + * Get self with signature algorithm identifier. |
|
| 179 | + * |
|
| 180 | + * @param SignatureAlgorithmIdentifier $algo |
|
| 181 | + * |
|
| 182 | + * @return self |
|
| 183 | + */ |
|
| 184 | + public function withSignature(SignatureAlgorithmIdentifier $algo): self |
|
| 185 | + { |
|
| 186 | + $obj = clone $this; |
|
| 187 | + $obj->_signature = $algo; |
|
| 188 | + return $obj; |
|
| 189 | + } |
|
| 190 | + |
|
| 191 | + /** |
|
| 192 | + * Get self with serial number. |
|
| 193 | + * |
|
| 194 | + * @param int|string $serial Base 10 serial number |
|
| 195 | + * |
|
| 196 | + * @return self |
|
| 197 | + */ |
|
| 198 | + public function withSerialNumber($serial): self |
|
| 199 | + { |
|
| 200 | + $obj = clone $this; |
|
| 201 | + $obj->_serialNumber = strval($serial); |
|
| 202 | + return $obj; |
|
| 203 | + } |
|
| 204 | + |
|
| 205 | + /** |
|
| 206 | + * Get self with random positive serial number. |
|
| 207 | + * |
|
| 208 | + * @param int $size Number of random bytes |
|
| 209 | + * |
|
| 210 | + * @return self |
|
| 211 | + */ |
|
| 212 | + public function withRandomSerialNumber(int $size = 16): self |
|
| 213 | + { |
|
| 214 | + // ensure that first byte is always non-zero and having first bit unset |
|
| 215 | + $num = gmp_init(mt_rand(1, 0x7f), 10); |
|
| 216 | + for ($i = 1; $i < $size; ++$i) { |
|
| 217 | + $num <<= 8; |
|
| 218 | + $num += mt_rand(0, 0xff); |
|
| 219 | + } |
|
| 220 | + return $this->withSerialNumber(gmp_strval($num, 10)); |
|
| 221 | + } |
|
| 222 | + |
|
| 223 | + /** |
|
| 224 | + * Get self with validity period. |
|
| 225 | + * |
|
| 226 | + * @param AttCertValidityPeriod $validity |
|
| 227 | + * |
|
| 228 | + * @return self |
|
| 229 | + */ |
|
| 230 | + public function withValidity(AttCertValidityPeriod $validity): self |
|
| 231 | + { |
|
| 232 | + $obj = clone $this; |
|
| 233 | + $obj->_attrCertValidityPeriod = $validity; |
|
| 234 | + return $obj; |
|
| 235 | + } |
|
| 236 | + |
|
| 237 | + /** |
|
| 238 | + * Get self with attributes. |
|
| 239 | + * |
|
| 240 | + * @param Attributes $attribs |
|
| 241 | + * |
|
| 242 | + * @return self |
|
| 243 | + */ |
|
| 244 | + public function withAttributes(Attributes $attribs): self |
|
| 245 | + { |
|
| 246 | + $obj = clone $this; |
|
| 247 | + $obj->_attributes = $attribs; |
|
| 248 | + return $obj; |
|
| 249 | + } |
|
| 250 | + |
|
| 251 | + /** |
|
| 252 | + * Get self with issuer unique identifier. |
|
| 253 | + * |
|
| 254 | + * @param UniqueIdentifier $uid |
|
| 255 | + * |
|
| 256 | + * @return self |
|
| 257 | + */ |
|
| 258 | + public function withIssuerUniqueID(UniqueIdentifier $uid): self |
|
| 259 | + { |
|
| 260 | + $obj = clone $this; |
|
| 261 | + $obj->_issuerUniqueID = $uid; |
|
| 262 | + return $obj; |
|
| 263 | + } |
|
| 264 | + |
|
| 265 | + /** |
|
| 266 | + * Get self with extensions. |
|
| 267 | + * |
|
| 268 | + * @param Extensions $extensions |
|
| 269 | + * |
|
| 270 | + * @return self |
|
| 271 | + */ |
|
| 272 | + public function withExtensions(Extensions $extensions): self |
|
| 273 | + { |
|
| 274 | + $obj = clone $this; |
|
| 275 | + $obj->_extensions = $extensions; |
|
| 276 | + return $obj; |
|
| 277 | + } |
|
| 278 | + |
|
| 279 | + /** |
|
| 280 | + * Get self with extensions added. |
|
| 281 | + * |
|
| 282 | + * @param Extension ...$exts One or more Extension objects |
|
| 283 | + * |
|
| 284 | + * @return self |
|
| 285 | + */ |
|
| 286 | + public function withAdditionalExtensions(Extension ...$exts): self |
|
| 287 | + { |
|
| 288 | + $obj = clone $this; |
|
| 289 | + $obj->_extensions = $obj->_extensions->withExtensions(...$exts); |
|
| 290 | + return $obj; |
|
| 291 | + } |
|
| 292 | + |
|
| 293 | + /** |
|
| 294 | + * Get version. |
|
| 295 | + * |
|
| 296 | + * @return int |
|
| 297 | + */ |
|
| 298 | + public function version(): int |
|
| 299 | + { |
|
| 300 | + return $this->_version; |
|
| 301 | + } |
|
| 302 | + |
|
| 303 | + /** |
|
| 304 | + * Get AC holder. |
|
| 305 | + * |
|
| 306 | + * @return Holder |
|
| 307 | + */ |
|
| 308 | + public function holder(): Holder |
|
| 309 | + { |
|
| 310 | + return $this->_holder; |
|
| 311 | + } |
|
| 312 | + |
|
| 313 | + /** |
|
| 314 | + * Get AC issuer. |
|
| 315 | + * |
|
| 316 | + * @return AttCertIssuer |
|
| 317 | + */ |
|
| 318 | + public function issuer(): AttCertIssuer |
|
| 319 | + { |
|
| 320 | + return $this->_issuer; |
|
| 321 | + } |
|
| 322 | + |
|
| 323 | + /** |
|
| 324 | + * Check whether signature is set. |
|
| 325 | + * |
|
| 326 | + * @return bool |
|
| 327 | + */ |
|
| 328 | + public function hasSignature(): bool |
|
| 329 | + { |
|
| 330 | + return isset($this->_signature); |
|
| 331 | + } |
|
| 332 | + |
|
| 333 | + /** |
|
| 334 | + * Get signature algorithm identifier. |
|
| 335 | + * |
|
| 336 | + * @throws \LogicException If not set |
|
| 337 | + * |
|
| 338 | + * @return SignatureAlgorithmIdentifier |
|
| 339 | + */ |
|
| 340 | + public function signature(): SignatureAlgorithmIdentifier |
|
| 341 | + { |
|
| 342 | + if (!$this->hasSignature()) { |
|
| 343 | + throw new \LogicException('signature not set.'); |
|
| 344 | + } |
|
| 345 | + return $this->_signature; |
|
| 346 | + } |
|
| 347 | + |
|
| 348 | + /** |
|
| 349 | + * Check whether serial number is present. |
|
| 350 | + * |
|
| 351 | + * @return bool |
|
| 352 | + */ |
|
| 353 | + public function hasSerialNumber(): bool |
|
| 354 | + { |
|
| 355 | + return isset($this->_serialNumber); |
|
| 356 | + } |
|
| 357 | + |
|
| 358 | + /** |
|
| 359 | + * Get AC serial number as a base 10 integer. |
|
| 360 | + * |
|
| 361 | + * @throws \LogicException If not set |
|
| 362 | + * |
|
| 363 | + * @return string |
|
| 364 | + */ |
|
| 365 | + public function serialNumber(): string |
|
| 366 | + { |
|
| 367 | + if (!$this->hasSerialNumber()) { |
|
| 368 | + throw new \LogicException('serialNumber not set.'); |
|
| 369 | + } |
|
| 370 | + return $this->_serialNumber; |
|
| 371 | + } |
|
| 372 | + |
|
| 373 | + /** |
|
| 374 | + * Get validity period. |
|
| 375 | + * |
|
| 376 | + * @return AttCertValidityPeriod |
|
| 377 | + */ |
|
| 378 | + public function validityPeriod(): AttCertValidityPeriod |
|
| 379 | + { |
|
| 380 | + return $this->_attrCertValidityPeriod; |
|
| 381 | + } |
|
| 382 | + |
|
| 383 | + /** |
|
| 384 | + * Get attributes. |
|
| 385 | + * |
|
| 386 | + * @return Attributes |
|
| 387 | + */ |
|
| 388 | + public function attributes(): Attributes |
|
| 389 | + { |
|
| 390 | + return $this->_attributes; |
|
| 391 | + } |
|
| 392 | + |
|
| 393 | + /** |
|
| 394 | + * Check whether issuer unique identifier is present. |
|
| 395 | + * |
|
| 396 | + * @return bool |
|
| 397 | + */ |
|
| 398 | + public function hasIssuerUniqueID(): bool |
|
| 399 | + { |
|
| 400 | + return isset($this->_issuerUniqueID); |
|
| 401 | + } |
|
| 402 | + |
|
| 403 | + /** |
|
| 404 | + * Get issuer unique identifier. |
|
| 405 | + * |
|
| 406 | + * @throws \LogicException If not set |
|
| 407 | + * |
|
| 408 | + * @return UniqueIdentifier |
|
| 409 | + */ |
|
| 410 | + public function issuerUniqueID(): UniqueIdentifier |
|
| 411 | + { |
|
| 412 | + if (!$this->hasIssuerUniqueID()) { |
|
| 413 | + throw new \LogicException('issuerUniqueID not set.'); |
|
| 414 | + } |
|
| 415 | + return $this->_issuerUniqueID; |
|
| 416 | + } |
|
| 417 | + |
|
| 418 | + /** |
|
| 419 | + * Get extensions. |
|
| 420 | + * |
|
| 421 | + * @return Extensions |
|
| 422 | + */ |
|
| 423 | + public function extensions(): Extensions |
|
| 424 | + { |
|
| 425 | + return $this->_extensions; |
|
| 426 | + } |
|
| 427 | + |
|
| 428 | + /** |
|
| 429 | + * Get ASN.1 structure. |
|
| 430 | + * |
|
| 431 | + * @return Sequence |
|
| 432 | + */ |
|
| 433 | + public function toASN1(): Sequence |
|
| 434 | + { |
|
| 435 | + $elements = [new Integer($this->_version), $this->_holder->toASN1(), |
|
| 436 | + $this->_issuer->toASN1(), $this->signature()->toASN1(), |
|
| 437 | + new Integer($this->serialNumber()), |
|
| 438 | + $this->_attrCertValidityPeriod->toASN1(), |
|
| 439 | + $this->_attributes->toASN1(), ]; |
|
| 440 | + if (isset($this->_issuerUniqueID)) { |
|
| 441 | + $elements[] = $this->_issuerUniqueID->toASN1(); |
|
| 442 | + } |
|
| 443 | + if (count($this->_extensions)) { |
|
| 444 | + $elements[] = $this->_extensions->toASN1(); |
|
| 445 | + } |
|
| 446 | + return new Sequence(...$elements); |
|
| 447 | + } |
|
| 448 | + |
|
| 449 | + /** |
|
| 450 | + * Create signed attribute certificate. |
|
| 451 | + * |
|
| 452 | + * @param SignatureAlgorithmIdentifier $algo Signature algorithm |
|
| 453 | + * @param PrivateKeyInfo $privkey_info Private key |
|
| 454 | + * @param null|Crypto $crypto Crypto engine, use default if not set |
|
| 455 | + * |
|
| 456 | + * @return AttributeCertificate |
|
| 457 | + */ |
|
| 458 | + public function sign(SignatureAlgorithmIdentifier $algo, |
|
| 459 | + PrivateKeyInfo $privkey_info, ?Crypto $crypto = null): AttributeCertificate |
|
| 460 | + { |
|
| 461 | + $crypto = $crypto ?? Crypto::getDefault(); |
|
| 462 | + $aci = clone $this; |
|
| 463 | + if (!isset($aci->_serialNumber)) { |
|
| 464 | + $aci->_serialNumber = '0'; |
|
| 465 | + } |
|
| 466 | + $aci->_signature = $algo; |
|
| 467 | + $data = $aci->toASN1()->toDER(); |
|
| 468 | + $signature = $crypto->sign($data, $privkey_info, $algo); |
|
| 469 | + return new AttributeCertificate($aci, $algo, $signature); |
|
| 470 | + } |
|
| 471 | 471 | } |
@@ -21,208 +21,208 @@ |
||
| 21 | 21 | */ |
| 22 | 22 | class AttributeCertificate |
| 23 | 23 | { |
| 24 | - /** |
|
| 25 | - * Attribute certificate info. |
|
| 26 | - * |
|
| 27 | - * @var AttributeCertificateInfo |
|
| 28 | - */ |
|
| 29 | - protected $_acinfo; |
|
| 30 | - |
|
| 31 | - /** |
|
| 32 | - * Signature algorithm identifier. |
|
| 33 | - * |
|
| 34 | - * @var SignatureAlgorithmIdentifier |
|
| 35 | - */ |
|
| 36 | - protected $_signatureAlgorithm; |
|
| 37 | - |
|
| 38 | - /** |
|
| 39 | - * Signature value. |
|
| 40 | - * |
|
| 41 | - * @var Signature |
|
| 42 | - */ |
|
| 43 | - protected $_signatureValue; |
|
| 44 | - |
|
| 45 | - /** |
|
| 46 | - * Constructor. |
|
| 47 | - * |
|
| 48 | - * @param AttributeCertificateInfo $acinfo |
|
| 49 | - * @param SignatureAlgorithmIdentifier $algo |
|
| 50 | - * @param Signature $signature |
|
| 51 | - */ |
|
| 52 | - public function __construct(AttributeCertificateInfo $acinfo, |
|
| 53 | - SignatureAlgorithmIdentifier $algo, Signature $signature) |
|
| 54 | - { |
|
| 55 | - $this->_acinfo = $acinfo; |
|
| 56 | - $this->_signatureAlgorithm = $algo; |
|
| 57 | - $this->_signatureValue = $signature; |
|
| 58 | - } |
|
| 59 | - |
|
| 60 | - /** |
|
| 61 | - * Get attribute certificate as a PEM formatted string. |
|
| 62 | - * |
|
| 63 | - * @return string |
|
| 64 | - */ |
|
| 65 | - public function __toString(): string |
|
| 66 | - { |
|
| 67 | - return $this->toPEM()->string(); |
|
| 68 | - } |
|
| 69 | - |
|
| 70 | - /** |
|
| 71 | - * Initialize from ASN.1. |
|
| 72 | - * |
|
| 73 | - * @param Sequence $seq |
|
| 74 | - * |
|
| 75 | - * @return self |
|
| 76 | - */ |
|
| 77 | - public static function fromASN1(Sequence $seq): self |
|
| 78 | - { |
|
| 79 | - $acinfo = AttributeCertificateInfo::fromASN1($seq->at(0)->asSequence()); |
|
| 80 | - $algo = AlgorithmIdentifier::fromASN1($seq->at(1)->asSequence()); |
|
| 81 | - if (!$algo instanceof SignatureAlgorithmIdentifier) { |
|
| 82 | - throw new \UnexpectedValueException( |
|
| 83 | - 'Unsupported signature algorithm ' . $algo->oid() . '.'); |
|
| 84 | - } |
|
| 85 | - $signature = Signature::fromSignatureData( |
|
| 86 | - $seq->at(2)->asBitString()->string(), $algo); |
|
| 87 | - return new self($acinfo, $algo, $signature); |
|
| 88 | - } |
|
| 89 | - |
|
| 90 | - /** |
|
| 91 | - * Initialize from DER data. |
|
| 92 | - * |
|
| 93 | - * @param string $data |
|
| 94 | - * |
|
| 95 | - * @return self |
|
| 96 | - */ |
|
| 97 | - public static function fromDER(string $data): self |
|
| 98 | - { |
|
| 99 | - return self::fromASN1(UnspecifiedType::fromDER($data)->asSequence()); |
|
| 100 | - } |
|
| 101 | - |
|
| 102 | - /** |
|
| 103 | - * Initialize from PEM. |
|
| 104 | - * |
|
| 105 | - * @param PEM $pem |
|
| 106 | - * |
|
| 107 | - * @throws \UnexpectedValueException |
|
| 108 | - * |
|
| 109 | - * @return self |
|
| 110 | - */ |
|
| 111 | - public static function fromPEM(PEM $pem): self |
|
| 112 | - { |
|
| 113 | - if (PEM::TYPE_ATTRIBUTE_CERTIFICATE !== $pem->type()) { |
|
| 114 | - throw new \UnexpectedValueException('Invalid PEM type.'); |
|
| 115 | - } |
|
| 116 | - return self::fromDER($pem->data()); |
|
| 117 | - } |
|
| 118 | - |
|
| 119 | - /** |
|
| 120 | - * Get attribute certificate info. |
|
| 121 | - * |
|
| 122 | - * @return AttributeCertificateInfo |
|
| 123 | - */ |
|
| 124 | - public function acinfo(): AttributeCertificateInfo |
|
| 125 | - { |
|
| 126 | - return $this->_acinfo; |
|
| 127 | - } |
|
| 128 | - |
|
| 129 | - /** |
|
| 130 | - * Get signature algorithm identifier. |
|
| 131 | - * |
|
| 132 | - * @return SignatureAlgorithmIdentifier |
|
| 133 | - */ |
|
| 134 | - public function signatureAlgorithm(): SignatureAlgorithmIdentifier |
|
| 135 | - { |
|
| 136 | - return $this->_signatureAlgorithm; |
|
| 137 | - } |
|
| 138 | - |
|
| 139 | - /** |
|
| 140 | - * Get signature value. |
|
| 141 | - * |
|
| 142 | - * @return Signature |
|
| 143 | - */ |
|
| 144 | - public function signatureValue(): Signature |
|
| 145 | - { |
|
| 146 | - return $this->_signatureValue; |
|
| 147 | - } |
|
| 148 | - |
|
| 149 | - /** |
|
| 150 | - * Get ASN.1 structure. |
|
| 151 | - * |
|
| 152 | - * @return Sequence |
|
| 153 | - */ |
|
| 154 | - public function toASN1(): Sequence |
|
| 155 | - { |
|
| 156 | - return new Sequence($this->_acinfo->toASN1(), |
|
| 157 | - $this->_signatureAlgorithm->toASN1(), |
|
| 158 | - $this->_signatureValue->bitString()); |
|
| 159 | - } |
|
| 160 | - |
|
| 161 | - /** |
|
| 162 | - * Get attribute certificate as a DER. |
|
| 163 | - * |
|
| 164 | - * @return string |
|
| 165 | - */ |
|
| 166 | - public function toDER(): string |
|
| 167 | - { |
|
| 168 | - return $this->toASN1()->toDER(); |
|
| 169 | - } |
|
| 170 | - |
|
| 171 | - /** |
|
| 172 | - * Get attribute certificate as a PEM. |
|
| 173 | - * |
|
| 174 | - * @return PEM |
|
| 175 | - */ |
|
| 176 | - public function toPEM(): PEM |
|
| 177 | - { |
|
| 178 | - return new PEM(PEM::TYPE_ATTRIBUTE_CERTIFICATE, $this->toDER()); |
|
| 179 | - } |
|
| 180 | - |
|
| 181 | - /** |
|
| 182 | - * Check whether attribute certificate is issued to the subject identified |
|
| 183 | - * by given public key certificate. |
|
| 184 | - * |
|
| 185 | - * @param Certificate $cert Certificate |
|
| 186 | - * |
|
| 187 | - * @return bool |
|
| 188 | - */ |
|
| 189 | - public function isHeldBy(Certificate $cert): bool |
|
| 190 | - { |
|
| 191 | - if (!$this->_acinfo->holder()->identifiesPKC($cert)) { |
|
| 192 | - return false; |
|
| 193 | - } |
|
| 194 | - return true; |
|
| 195 | - } |
|
| 196 | - |
|
| 197 | - /** |
|
| 198 | - * Check whether attribute certificate is issued by given public key |
|
| 199 | - * certificate. |
|
| 200 | - * |
|
| 201 | - * @param Certificate $cert Certificate |
|
| 202 | - * |
|
| 203 | - * @return bool |
|
| 204 | - */ |
|
| 205 | - public function isIssuedBy(Certificate $cert): bool |
|
| 206 | - { |
|
| 207 | - if (!$this->_acinfo->issuer()->identifiesPKC($cert)) { |
|
| 208 | - return false; |
|
| 209 | - } |
|
| 210 | - return true; |
|
| 211 | - } |
|
| 212 | - |
|
| 213 | - /** |
|
| 214 | - * Verify signature. |
|
| 215 | - * |
|
| 216 | - * @param PublicKeyInfo $pubkey_info Signer's public key |
|
| 217 | - * @param null|Crypto $crypto Crypto engine, use default if not set |
|
| 218 | - * |
|
| 219 | - * @return bool |
|
| 220 | - */ |
|
| 221 | - public function verify(PublicKeyInfo $pubkey_info, ?Crypto $crypto = null): bool |
|
| 222 | - { |
|
| 223 | - $crypto = $crypto ?? Crypto::getDefault(); |
|
| 224 | - $data = $this->_acinfo->toASN1()->toDER(); |
|
| 225 | - return $crypto->verify($data, $this->_signatureValue, $pubkey_info, |
|
| 226 | - $this->_signatureAlgorithm); |
|
| 227 | - } |
|
| 24 | + /** |
|
| 25 | + * Attribute certificate info. |
|
| 26 | + * |
|
| 27 | + * @var AttributeCertificateInfo |
|
| 28 | + */ |
|
| 29 | + protected $_acinfo; |
|
| 30 | + |
|
| 31 | + /** |
|
| 32 | + * Signature algorithm identifier. |
|
| 33 | + * |
|
| 34 | + * @var SignatureAlgorithmIdentifier |
|
| 35 | + */ |
|
| 36 | + protected $_signatureAlgorithm; |
|
| 37 | + |
|
| 38 | + /** |
|
| 39 | + * Signature value. |
|
| 40 | + * |
|
| 41 | + * @var Signature |
|
| 42 | + */ |
|
| 43 | + protected $_signatureValue; |
|
| 44 | + |
|
| 45 | + /** |
|
| 46 | + * Constructor. |
|
| 47 | + * |
|
| 48 | + * @param AttributeCertificateInfo $acinfo |
|
| 49 | + * @param SignatureAlgorithmIdentifier $algo |
|
| 50 | + * @param Signature $signature |
|
| 51 | + */ |
|
| 52 | + public function __construct(AttributeCertificateInfo $acinfo, |
|
| 53 | + SignatureAlgorithmIdentifier $algo, Signature $signature) |
|
| 54 | + { |
|
| 55 | + $this->_acinfo = $acinfo; |
|
| 56 | + $this->_signatureAlgorithm = $algo; |
|
| 57 | + $this->_signatureValue = $signature; |
|
| 58 | + } |
|
| 59 | + |
|
| 60 | + /** |
|
| 61 | + * Get attribute certificate as a PEM formatted string. |
|
| 62 | + * |
|
| 63 | + * @return string |
|
| 64 | + */ |
|
| 65 | + public function __toString(): string |
|
| 66 | + { |
|
| 67 | + return $this->toPEM()->string(); |
|
| 68 | + } |
|
| 69 | + |
|
| 70 | + /** |
|
| 71 | + * Initialize from ASN.1. |
|
| 72 | + * |
|
| 73 | + * @param Sequence $seq |
|
| 74 | + * |
|
| 75 | + * @return self |
|
| 76 | + */ |
|
| 77 | + public static function fromASN1(Sequence $seq): self |
|
| 78 | + { |
|
| 79 | + $acinfo = AttributeCertificateInfo::fromASN1($seq->at(0)->asSequence()); |
|
| 80 | + $algo = AlgorithmIdentifier::fromASN1($seq->at(1)->asSequence()); |
|
| 81 | + if (!$algo instanceof SignatureAlgorithmIdentifier) { |
|
| 82 | + throw new \UnexpectedValueException( |
|
| 83 | + 'Unsupported signature algorithm ' . $algo->oid() . '.'); |
|
| 84 | + } |
|
| 85 | + $signature = Signature::fromSignatureData( |
|
| 86 | + $seq->at(2)->asBitString()->string(), $algo); |
|
| 87 | + return new self($acinfo, $algo, $signature); |
|
| 88 | + } |
|
| 89 | + |
|
| 90 | + /** |
|
| 91 | + * Initialize from DER data. |
|
| 92 | + * |
|
| 93 | + * @param string $data |
|
| 94 | + * |
|
| 95 | + * @return self |
|
| 96 | + */ |
|
| 97 | + public static function fromDER(string $data): self |
|
| 98 | + { |
|
| 99 | + return self::fromASN1(UnspecifiedType::fromDER($data)->asSequence()); |
|
| 100 | + } |
|
| 101 | + |
|
| 102 | + /** |
|
| 103 | + * Initialize from PEM. |
|
| 104 | + * |
|
| 105 | + * @param PEM $pem |
|
| 106 | + * |
|
| 107 | + * @throws \UnexpectedValueException |
|
| 108 | + * |
|
| 109 | + * @return self |
|
| 110 | + */ |
|
| 111 | + public static function fromPEM(PEM $pem): self |
|
| 112 | + { |
|
| 113 | + if (PEM::TYPE_ATTRIBUTE_CERTIFICATE !== $pem->type()) { |
|
| 114 | + throw new \UnexpectedValueException('Invalid PEM type.'); |
|
| 115 | + } |
|
| 116 | + return self::fromDER($pem->data()); |
|
| 117 | + } |
|
| 118 | + |
|
| 119 | + /** |
|
| 120 | + * Get attribute certificate info. |
|
| 121 | + * |
|
| 122 | + * @return AttributeCertificateInfo |
|
| 123 | + */ |
|
| 124 | + public function acinfo(): AttributeCertificateInfo |
|
| 125 | + { |
|
| 126 | + return $this->_acinfo; |
|
| 127 | + } |
|
| 128 | + |
|
| 129 | + /** |
|
| 130 | + * Get signature algorithm identifier. |
|
| 131 | + * |
|
| 132 | + * @return SignatureAlgorithmIdentifier |
|
| 133 | + */ |
|
| 134 | + public function signatureAlgorithm(): SignatureAlgorithmIdentifier |
|
| 135 | + { |
|
| 136 | + return $this->_signatureAlgorithm; |
|
| 137 | + } |
|
| 138 | + |
|
| 139 | + /** |
|
| 140 | + * Get signature value. |
|
| 141 | + * |
|
| 142 | + * @return Signature |
|
| 143 | + */ |
|
| 144 | + public function signatureValue(): Signature |
|
| 145 | + { |
|
| 146 | + return $this->_signatureValue; |
|
| 147 | + } |
|
| 148 | + |
|
| 149 | + /** |
|
| 150 | + * Get ASN.1 structure. |
|
| 151 | + * |
|
| 152 | + * @return Sequence |
|
| 153 | + */ |
|
| 154 | + public function toASN1(): Sequence |
|
| 155 | + { |
|
| 156 | + return new Sequence($this->_acinfo->toASN1(), |
|
| 157 | + $this->_signatureAlgorithm->toASN1(), |
|
| 158 | + $this->_signatureValue->bitString()); |
|
| 159 | + } |
|
| 160 | + |
|
| 161 | + /** |
|
| 162 | + * Get attribute certificate as a DER. |
|
| 163 | + * |
|
| 164 | + * @return string |
|
| 165 | + */ |
|
| 166 | + public function toDER(): string |
|
| 167 | + { |
|
| 168 | + return $this->toASN1()->toDER(); |
|
| 169 | + } |
|
| 170 | + |
|
| 171 | + /** |
|
| 172 | + * Get attribute certificate as a PEM. |
|
| 173 | + * |
|
| 174 | + * @return PEM |
|
| 175 | + */ |
|
| 176 | + public function toPEM(): PEM |
|
| 177 | + { |
|
| 178 | + return new PEM(PEM::TYPE_ATTRIBUTE_CERTIFICATE, $this->toDER()); |
|
| 179 | + } |
|
| 180 | + |
|
| 181 | + /** |
|
| 182 | + * Check whether attribute certificate is issued to the subject identified |
|
| 183 | + * by given public key certificate. |
|
| 184 | + * |
|
| 185 | + * @param Certificate $cert Certificate |
|
| 186 | + * |
|
| 187 | + * @return bool |
|
| 188 | + */ |
|
| 189 | + public function isHeldBy(Certificate $cert): bool |
|
| 190 | + { |
|
| 191 | + if (!$this->_acinfo->holder()->identifiesPKC($cert)) { |
|
| 192 | + return false; |
|
| 193 | + } |
|
| 194 | + return true; |
|
| 195 | + } |
|
| 196 | + |
|
| 197 | + /** |
|
| 198 | + * Check whether attribute certificate is issued by given public key |
|
| 199 | + * certificate. |
|
| 200 | + * |
|
| 201 | + * @param Certificate $cert Certificate |
|
| 202 | + * |
|
| 203 | + * @return bool |
|
| 204 | + */ |
|
| 205 | + public function isIssuedBy(Certificate $cert): bool |
|
| 206 | + { |
|
| 207 | + if (!$this->_acinfo->issuer()->identifiesPKC($cert)) { |
|
| 208 | + return false; |
|
| 209 | + } |
|
| 210 | + return true; |
|
| 211 | + } |
|
| 212 | + |
|
| 213 | + /** |
|
| 214 | + * Verify signature. |
|
| 215 | + * |
|
| 216 | + * @param PublicKeyInfo $pubkey_info Signer's public key |
|
| 217 | + * @param null|Crypto $crypto Crypto engine, use default if not set |
|
| 218 | + * |
|
| 219 | + * @return bool |
|
| 220 | + */ |
|
| 221 | + public function verify(PublicKeyInfo $pubkey_info, ?Crypto $crypto = null): bool |
|
| 222 | + { |
|
| 223 | + $crypto = $crypto ?? Crypto::getDefault(); |
|
| 224 | + $data = $this->_acinfo->toASN1()->toDER(); |
|
| 225 | + return $crypto->verify($data, $this->_signatureValue, $pubkey_info, |
|
| 226 | + $this->_signatureAlgorithm); |
|
| 227 | + } |
|
| 228 | 228 | } |
@@ -19,173 +19,173 @@ |
||
| 19 | 19 | */ |
| 20 | 20 | class IssuerSerial |
| 21 | 21 | { |
| 22 | - /** |
|
| 23 | - * Issuer name. |
|
| 24 | - * |
|
| 25 | - * @var GeneralNames |
|
| 26 | - */ |
|
| 27 | - protected $_issuer; |
|
| 28 | - |
|
| 29 | - /** |
|
| 30 | - * Serial number as a base 10 integer. |
|
| 31 | - * |
|
| 32 | - * @var string |
|
| 33 | - */ |
|
| 34 | - protected $_serial; |
|
| 35 | - |
|
| 36 | - /** |
|
| 37 | - * Issuer unique ID. |
|
| 38 | - * |
|
| 39 | - * @var null|UniqueIdentifier |
|
| 40 | - */ |
|
| 41 | - protected $_issuerUID; |
|
| 42 | - |
|
| 43 | - /** |
|
| 44 | - * Constructor. |
|
| 45 | - * |
|
| 46 | - * @param GeneralNames $issuer |
|
| 47 | - * @param int|string $serial |
|
| 48 | - * @param null|UniqueIdentifier $uid |
|
| 49 | - */ |
|
| 50 | - public function __construct(GeneralNames $issuer, $serial, |
|
| 51 | - ?UniqueIdentifier $uid = null) |
|
| 52 | - { |
|
| 53 | - $this->_issuer = $issuer; |
|
| 54 | - $this->_serial = strval($serial); |
|
| 55 | - $this->_issuerUID = $uid; |
|
| 56 | - } |
|
| 57 | - |
|
| 58 | - /** |
|
| 59 | - * Initialize from ASN.1. |
|
| 60 | - * |
|
| 61 | - * @param Sequence $seq |
|
| 62 | - * |
|
| 63 | - * @return self |
|
| 64 | - */ |
|
| 65 | - public static function fromASN1(Sequence $seq): IssuerSerial |
|
| 66 | - { |
|
| 67 | - $issuer = GeneralNames::fromASN1($seq->at(0)->asSequence()); |
|
| 68 | - $serial = $seq->at(1)->asInteger()->number(); |
|
| 69 | - $uid = null; |
|
| 70 | - if ($seq->has(2, Element::TYPE_BIT_STRING)) { |
|
| 71 | - $uid = UniqueIdentifier::fromASN1($seq->at(2)->asBitString()); |
|
| 72 | - } |
|
| 73 | - return new self($issuer, $serial, $uid); |
|
| 74 | - } |
|
| 75 | - |
|
| 76 | - /** |
|
| 77 | - * Initialize from a public key certificate. |
|
| 78 | - * |
|
| 79 | - * @param Certificate $cert |
|
| 80 | - * |
|
| 81 | - * @return self |
|
| 82 | - */ |
|
| 83 | - public static function fromPKC(Certificate $cert): IssuerSerial |
|
| 84 | - { |
|
| 85 | - $tbsCert = $cert->tbsCertificate(); |
|
| 86 | - $issuer = new GeneralNames(new DirectoryName($tbsCert->issuer())); |
|
| 87 | - $serial = $tbsCert->serialNumber(); |
|
| 88 | - $uid = $tbsCert->hasIssuerUniqueID() ? $tbsCert->issuerUniqueID() : null; |
|
| 89 | - return new self($issuer, $serial, $uid); |
|
| 90 | - } |
|
| 91 | - |
|
| 92 | - /** |
|
| 93 | - * Get issuer name. |
|
| 94 | - * |
|
| 95 | - * @return GeneralNames |
|
| 96 | - */ |
|
| 97 | - public function issuer(): GeneralNames |
|
| 98 | - { |
|
| 99 | - return $this->_issuer; |
|
| 100 | - } |
|
| 101 | - |
|
| 102 | - /** |
|
| 103 | - * Get serial number. |
|
| 104 | - * |
|
| 105 | - * @return string |
|
| 106 | - */ |
|
| 107 | - public function serial(): string |
|
| 108 | - { |
|
| 109 | - return $this->_serial; |
|
| 110 | - } |
|
| 111 | - |
|
| 112 | - /** |
|
| 113 | - * Check whether issuer unique identifier is present. |
|
| 114 | - * |
|
| 115 | - * @return bool |
|
| 116 | - */ |
|
| 117 | - public function hasIssuerUID(): bool |
|
| 118 | - { |
|
| 119 | - return isset($this->_issuerUID); |
|
| 120 | - } |
|
| 121 | - |
|
| 122 | - /** |
|
| 123 | - * Get issuer unique identifier. |
|
| 124 | - * |
|
| 125 | - * @throws \LogicException If not set |
|
| 126 | - * |
|
| 127 | - * @return UniqueIdentifier |
|
| 128 | - */ |
|
| 129 | - public function issuerUID(): UniqueIdentifier |
|
| 130 | - { |
|
| 131 | - if (!$this->hasIssuerUID()) { |
|
| 132 | - throw new \LogicException('issuerUID not set.'); |
|
| 133 | - } |
|
| 134 | - return $this->_issuerUID; |
|
| 135 | - } |
|
| 136 | - |
|
| 137 | - /** |
|
| 138 | - * Generate ASN.1 structure. |
|
| 139 | - * |
|
| 140 | - * @return Sequence |
|
| 141 | - */ |
|
| 142 | - public function toASN1(): Sequence |
|
| 143 | - { |
|
| 144 | - $elements = [$this->_issuer->toASN1(), new Integer($this->_serial)]; |
|
| 145 | - if (isset($this->_issuerUID)) { |
|
| 146 | - $elements[] = $this->_issuerUID->toASN1(); |
|
| 147 | - } |
|
| 148 | - return new Sequence(...$elements); |
|
| 149 | - } |
|
| 150 | - |
|
| 151 | - /** |
|
| 152 | - * Check whether this IssuerSerial identifies given certificate. |
|
| 153 | - * |
|
| 154 | - * @param Certificate $cert |
|
| 155 | - * |
|
| 156 | - * @return bool |
|
| 157 | - */ |
|
| 158 | - public function identifiesPKC(Certificate $cert): bool |
|
| 159 | - { |
|
| 160 | - $tbs = $cert->tbsCertificate(); |
|
| 161 | - if (!$tbs->issuer()->equals($this->_issuer->firstDN())) { |
|
| 162 | - return false; |
|
| 163 | - } |
|
| 164 | - if ($tbs->serialNumber() !== $this->_serial) { |
|
| 165 | - return false; |
|
| 166 | - } |
|
| 167 | - if ($this->_issuerUID && !$this->_checkUniqueID($cert)) { |
|
| 168 | - return false; |
|
| 169 | - } |
|
| 170 | - return true; |
|
| 171 | - } |
|
| 172 | - |
|
| 173 | - /** |
|
| 174 | - * Check whether issuerUID matches given certificate. |
|
| 175 | - * |
|
| 176 | - * @param Certificate $cert |
|
| 177 | - * |
|
| 178 | - * @return bool |
|
| 179 | - */ |
|
| 180 | - private function _checkUniqueID(Certificate $cert): bool |
|
| 181 | - { |
|
| 182 | - if (!$cert->tbsCertificate()->hasIssuerUniqueID()) { |
|
| 183 | - return false; |
|
| 184 | - } |
|
| 185 | - $uid = $cert->tbsCertificate()->issuerUniqueID()->string(); |
|
| 186 | - if ($this->_issuerUID->string() != $uid) { |
|
| 187 | - return false; |
|
| 188 | - } |
|
| 189 | - return true; |
|
| 190 | - } |
|
| 22 | + /** |
|
| 23 | + * Issuer name. |
|
| 24 | + * |
|
| 25 | + * @var GeneralNames |
|
| 26 | + */ |
|
| 27 | + protected $_issuer; |
|
| 28 | + |
|
| 29 | + /** |
|
| 30 | + * Serial number as a base 10 integer. |
|
| 31 | + * |
|
| 32 | + * @var string |
|
| 33 | + */ |
|
| 34 | + protected $_serial; |
|
| 35 | + |
|
| 36 | + /** |
|
| 37 | + * Issuer unique ID. |
|
| 38 | + * |
|
| 39 | + * @var null|UniqueIdentifier |
|
| 40 | + */ |
|
| 41 | + protected $_issuerUID; |
|
| 42 | + |
|
| 43 | + /** |
|
| 44 | + * Constructor. |
|
| 45 | + * |
|
| 46 | + * @param GeneralNames $issuer |
|
| 47 | + * @param int|string $serial |
|
| 48 | + * @param null|UniqueIdentifier $uid |
|
| 49 | + */ |
|
| 50 | + public function __construct(GeneralNames $issuer, $serial, |
|
| 51 | + ?UniqueIdentifier $uid = null) |
|
| 52 | + { |
|
| 53 | + $this->_issuer = $issuer; |
|
| 54 | + $this->_serial = strval($serial); |
|
| 55 | + $this->_issuerUID = $uid; |
|
| 56 | + } |
|
| 57 | + |
|
| 58 | + /** |
|
| 59 | + * Initialize from ASN.1. |
|
| 60 | + * |
|
| 61 | + * @param Sequence $seq |
|
| 62 | + * |
|
| 63 | + * @return self |
|
| 64 | + */ |
|
| 65 | + public static function fromASN1(Sequence $seq): IssuerSerial |
|
| 66 | + { |
|
| 67 | + $issuer = GeneralNames::fromASN1($seq->at(0)->asSequence()); |
|
| 68 | + $serial = $seq->at(1)->asInteger()->number(); |
|
| 69 | + $uid = null; |
|
| 70 | + if ($seq->has(2, Element::TYPE_BIT_STRING)) { |
|
| 71 | + $uid = UniqueIdentifier::fromASN1($seq->at(2)->asBitString()); |
|
| 72 | + } |
|
| 73 | + return new self($issuer, $serial, $uid); |
|
| 74 | + } |
|
| 75 | + |
|
| 76 | + /** |
|
| 77 | + * Initialize from a public key certificate. |
|
| 78 | + * |
|
| 79 | + * @param Certificate $cert |
|
| 80 | + * |
|
| 81 | + * @return self |
|
| 82 | + */ |
|
| 83 | + public static function fromPKC(Certificate $cert): IssuerSerial |
|
| 84 | + { |
|
| 85 | + $tbsCert = $cert->tbsCertificate(); |
|
| 86 | + $issuer = new GeneralNames(new DirectoryName($tbsCert->issuer())); |
|
| 87 | + $serial = $tbsCert->serialNumber(); |
|
| 88 | + $uid = $tbsCert->hasIssuerUniqueID() ? $tbsCert->issuerUniqueID() : null; |
|
| 89 | + return new self($issuer, $serial, $uid); |
|
| 90 | + } |
|
| 91 | + |
|
| 92 | + /** |
|
| 93 | + * Get issuer name. |
|
| 94 | + * |
|
| 95 | + * @return GeneralNames |
|
| 96 | + */ |
|
| 97 | + public function issuer(): GeneralNames |
|
| 98 | + { |
|
| 99 | + return $this->_issuer; |
|
| 100 | + } |
|
| 101 | + |
|
| 102 | + /** |
|
| 103 | + * Get serial number. |
|
| 104 | + * |
|
| 105 | + * @return string |
|
| 106 | + */ |
|
| 107 | + public function serial(): string |
|
| 108 | + { |
|
| 109 | + return $this->_serial; |
|
| 110 | + } |
|
| 111 | + |
|
| 112 | + /** |
|
| 113 | + * Check whether issuer unique identifier is present. |
|
| 114 | + * |
|
| 115 | + * @return bool |
|
| 116 | + */ |
|
| 117 | + public function hasIssuerUID(): bool |
|
| 118 | + { |
|
| 119 | + return isset($this->_issuerUID); |
|
| 120 | + } |
|
| 121 | + |
|
| 122 | + /** |
|
| 123 | + * Get issuer unique identifier. |
|
| 124 | + * |
|
| 125 | + * @throws \LogicException If not set |
|
| 126 | + * |
|
| 127 | + * @return UniqueIdentifier |
|
| 128 | + */ |
|
| 129 | + public function issuerUID(): UniqueIdentifier |
|
| 130 | + { |
|
| 131 | + if (!$this->hasIssuerUID()) { |
|
| 132 | + throw new \LogicException('issuerUID not set.'); |
|
| 133 | + } |
|
| 134 | + return $this->_issuerUID; |
|
| 135 | + } |
|
| 136 | + |
|
| 137 | + /** |
|
| 138 | + * Generate ASN.1 structure. |
|
| 139 | + * |
|
| 140 | + * @return Sequence |
|
| 141 | + */ |
|
| 142 | + public function toASN1(): Sequence |
|
| 143 | + { |
|
| 144 | + $elements = [$this->_issuer->toASN1(), new Integer($this->_serial)]; |
|
| 145 | + if (isset($this->_issuerUID)) { |
|
| 146 | + $elements[] = $this->_issuerUID->toASN1(); |
|
| 147 | + } |
|
| 148 | + return new Sequence(...$elements); |
|
| 149 | + } |
|
| 150 | + |
|
| 151 | + /** |
|
| 152 | + * Check whether this IssuerSerial identifies given certificate. |
|
| 153 | + * |
|
| 154 | + * @param Certificate $cert |
|
| 155 | + * |
|
| 156 | + * @return bool |
|
| 157 | + */ |
|
| 158 | + public function identifiesPKC(Certificate $cert): bool |
|
| 159 | + { |
|
| 160 | + $tbs = $cert->tbsCertificate(); |
|
| 161 | + if (!$tbs->issuer()->equals($this->_issuer->firstDN())) { |
|
| 162 | + return false; |
|
| 163 | + } |
|
| 164 | + if ($tbs->serialNumber() !== $this->_serial) { |
|
| 165 | + return false; |
|
| 166 | + } |
|
| 167 | + if ($this->_issuerUID && !$this->_checkUniqueID($cert)) { |
|
| 168 | + return false; |
|
| 169 | + } |
|
| 170 | + return true; |
|
| 171 | + } |
|
| 172 | + |
|
| 173 | + /** |
|
| 174 | + * Check whether issuerUID matches given certificate. |
|
| 175 | + * |
|
| 176 | + * @param Certificate $cert |
|
| 177 | + * |
|
| 178 | + * @return bool |
|
| 179 | + */ |
|
| 180 | + private function _checkUniqueID(Certificate $cert): bool |
|
| 181 | + { |
|
| 182 | + if (!$cert->tbsCertificate()->hasIssuerUniqueID()) { |
|
| 183 | + return false; |
|
| 184 | + } |
|
| 185 | + $uid = $cert->tbsCertificate()->issuerUniqueID()->string(); |
|
| 186 | + if ($this->_issuerUID->string() != $uid) { |
|
| 187 | + return false; |
|
| 188 | + } |
|
| 189 | + return true; |
|
| 190 | + } |
|
| 191 | 191 | } |
@@ -18,72 +18,72 @@ |
||
| 18 | 18 | */ |
| 19 | 19 | abstract class AttCertIssuer |
| 20 | 20 | { |
| 21 | - /** |
|
| 22 | - * Generate ASN.1 element. |
|
| 23 | - * |
|
| 24 | - * @return Element |
|
| 25 | - */ |
|
| 26 | - abstract public function toASN1(): Element; |
|
| 21 | + /** |
|
| 22 | + * Generate ASN.1 element. |
|
| 23 | + * |
|
| 24 | + * @return Element |
|
| 25 | + */ |
|
| 26 | + abstract public function toASN1(): Element; |
|
| 27 | 27 | |
| 28 | - /** |
|
| 29 | - * Check whether AttCertIssuer identifies given certificate. |
|
| 30 | - * |
|
| 31 | - * @param Certificate $cert |
|
| 32 | - * |
|
| 33 | - * @return bool |
|
| 34 | - */ |
|
| 35 | - abstract public function identifiesPKC(Certificate $cert): bool; |
|
| 28 | + /** |
|
| 29 | + * Check whether AttCertIssuer identifies given certificate. |
|
| 30 | + * |
|
| 31 | + * @param Certificate $cert |
|
| 32 | + * |
|
| 33 | + * @return bool |
|
| 34 | + */ |
|
| 35 | + abstract public function identifiesPKC(Certificate $cert): bool; |
|
| 36 | 36 | |
| 37 | - /** |
|
| 38 | - * Initialize from distinguished name. |
|
| 39 | - * |
|
| 40 | - * This conforms to RFC 5755 which states that only v2Form must be used, |
|
| 41 | - * and issuerName must contain exactly one GeneralName of DirectoryName |
|
| 42 | - * type. |
|
| 43 | - * |
|
| 44 | - * @see https://tools.ietf.org/html/rfc5755#section-4.2.3 |
|
| 45 | - * |
|
| 46 | - * @param Name $name |
|
| 47 | - * |
|
| 48 | - * @return self |
|
| 49 | - */ |
|
| 50 | - public static function fromName(Name $name): self |
|
| 51 | - { |
|
| 52 | - return new V2Form(new GeneralNames(new DirectoryName($name))); |
|
| 53 | - } |
|
| 37 | + /** |
|
| 38 | + * Initialize from distinguished name. |
|
| 39 | + * |
|
| 40 | + * This conforms to RFC 5755 which states that only v2Form must be used, |
|
| 41 | + * and issuerName must contain exactly one GeneralName of DirectoryName |
|
| 42 | + * type. |
|
| 43 | + * |
|
| 44 | + * @see https://tools.ietf.org/html/rfc5755#section-4.2.3 |
|
| 45 | + * |
|
| 46 | + * @param Name $name |
|
| 47 | + * |
|
| 48 | + * @return self |
|
| 49 | + */ |
|
| 50 | + public static function fromName(Name $name): self |
|
| 51 | + { |
|
| 52 | + return new V2Form(new GeneralNames(new DirectoryName($name))); |
|
| 53 | + } |
|
| 54 | 54 | |
| 55 | - /** |
|
| 56 | - * Initialize from an issuer's public key certificate. |
|
| 57 | - * |
|
| 58 | - * @param Certificate $cert |
|
| 59 | - * |
|
| 60 | - * @return self |
|
| 61 | - */ |
|
| 62 | - public static function fromPKC(Certificate $cert): self |
|
| 63 | - { |
|
| 64 | - return self::fromName($cert->tbsCertificate()->subject()); |
|
| 65 | - } |
|
| 55 | + /** |
|
| 56 | + * Initialize from an issuer's public key certificate. |
|
| 57 | + * |
|
| 58 | + * @param Certificate $cert |
|
| 59 | + * |
|
| 60 | + * @return self |
|
| 61 | + */ |
|
| 62 | + public static function fromPKC(Certificate $cert): self |
|
| 63 | + { |
|
| 64 | + return self::fromName($cert->tbsCertificate()->subject()); |
|
| 65 | + } |
|
| 66 | 66 | |
| 67 | - /** |
|
| 68 | - * Initialize from ASN.1. |
|
| 69 | - * |
|
| 70 | - * @param UnspecifiedType $el CHOICE |
|
| 71 | - * |
|
| 72 | - * @throws \UnexpectedValueException |
|
| 73 | - * |
|
| 74 | - * @return self |
|
| 75 | - */ |
|
| 76 | - public static function fromASN1(UnspecifiedType $el): self |
|
| 77 | - { |
|
| 78 | - if (!$el->isTagged()) { |
|
| 79 | - throw new \UnexpectedValueException('v1Form issuer not supported.'); |
|
| 80 | - } |
|
| 81 | - $tagged = $el->asTagged(); |
|
| 82 | - switch ($tagged->tag()) { |
|
| 83 | - case 0: |
|
| 84 | - return V2Form::fromV2ASN1( |
|
| 85 | - $tagged->asImplicit(Element::TYPE_SEQUENCE)->asSequence()); |
|
| 86 | - } |
|
| 87 | - throw new \UnexpectedValueException('Unsupported issuer type.'); |
|
| 88 | - } |
|
| 67 | + /** |
|
| 68 | + * Initialize from ASN.1. |
|
| 69 | + * |
|
| 70 | + * @param UnspecifiedType $el CHOICE |
|
| 71 | + * |
|
| 72 | + * @throws \UnexpectedValueException |
|
| 73 | + * |
|
| 74 | + * @return self |
|
| 75 | + */ |
|
| 76 | + public static function fromASN1(UnspecifiedType $el): self |
|
| 77 | + { |
|
| 78 | + if (!$el->isTagged()) { |
|
| 79 | + throw new \UnexpectedValueException('v1Form issuer not supported.'); |
|
| 80 | + } |
|
| 81 | + $tagged = $el->asTagged(); |
|
| 82 | + switch ($tagged->tag()) { |
|
| 83 | + case 0: |
|
| 84 | + return V2Form::fromV2ASN1( |
|
| 85 | + $tagged->asImplicit(Element::TYPE_SEQUENCE)->asSequence()); |
|
| 86 | + } |
|
| 87 | + throw new \UnexpectedValueException('Unsupported issuer type.'); |
|
| 88 | + } |
|
| 89 | 89 | } |
@@ -13,17 +13,17 @@ |
||
| 13 | 13 | */ |
| 14 | 14 | class AccessIdentityAttributeValue extends SvceAuthInfo |
| 15 | 15 | { |
| 16 | - const OID = '1.3.6.1.5.5.7.10.2'; |
|
| 16 | + const OID = '1.3.6.1.5.5.7.10.2'; |
|
| 17 | 17 | |
| 18 | - /** |
|
| 19 | - * Constructor. |
|
| 20 | - * |
|
| 21 | - * @param GeneralName $service |
|
| 22 | - * @param GeneralName $ident |
|
| 23 | - */ |
|
| 24 | - public function __construct(GeneralName $service, GeneralName $ident) |
|
| 25 | - { |
|
| 26 | - parent::__construct($service, $ident, null); |
|
| 27 | - $this->_oid = self::OID; |
|
| 28 | - } |
|
| 18 | + /** |
|
| 19 | + * Constructor. |
|
| 20 | + * |
|
| 21 | + * @param GeneralName $service |
|
| 22 | + * @param GeneralName $ident |
|
| 23 | + */ |
|
| 24 | + public function __construct(GeneralName $service, GeneralName $ident) |
|
| 25 | + { |
|
| 26 | + parent::__construct($service, $ident, null); |
|
| 27 | + $this->_oid = self::OID; |
|
| 28 | + } |
|
| 29 | 29 | } |
@@ -21,145 +21,145 @@ |
||
| 21 | 21 | */ |
| 22 | 22 | abstract class SvceAuthInfo extends AttributeValue |
| 23 | 23 | { |
| 24 | - /** |
|
| 25 | - * Service. |
|
| 26 | - * |
|
| 27 | - * @var GeneralName |
|
| 28 | - */ |
|
| 29 | - protected $_service; |
|
| 30 | - |
|
| 31 | - /** |
|
| 32 | - * Ident. |
|
| 33 | - * |
|
| 34 | - * @var GeneralName |
|
| 35 | - */ |
|
| 36 | - protected $_ident; |
|
| 37 | - |
|
| 38 | - /** |
|
| 39 | - * Auth info. |
|
| 40 | - * |
|
| 41 | - * @var null|string |
|
| 42 | - */ |
|
| 43 | - protected $_authInfo; |
|
| 44 | - |
|
| 45 | - /** |
|
| 46 | - * Constructor. |
|
| 47 | - * |
|
| 48 | - * @param GeneralName $service |
|
| 49 | - * @param GeneralName $ident |
|
| 50 | - * @param null|string $auth_info |
|
| 51 | - */ |
|
| 52 | - public function __construct(GeneralName $service, GeneralName $ident, |
|
| 53 | - ?string $auth_info = null) |
|
| 54 | - { |
|
| 55 | - $this->_service = $service; |
|
| 56 | - $this->_ident = $ident; |
|
| 57 | - $this->_authInfo = $auth_info; |
|
| 58 | - } |
|
| 59 | - |
|
| 60 | - /** |
|
| 61 | - * {@inheritdoc} |
|
| 62 | - * |
|
| 63 | - * @return self |
|
| 64 | - */ |
|
| 65 | - public static function fromASN1(UnspecifiedType $el): AttributeValue |
|
| 66 | - { |
|
| 67 | - $seq = $el->asSequence(); |
|
| 68 | - $service = GeneralName::fromASN1($seq->at(0)->asTagged()); |
|
| 69 | - $ident = GeneralName::fromASN1($seq->at(1)->asTagged()); |
|
| 70 | - $auth_info = null; |
|
| 71 | - if ($seq->has(2, Element::TYPE_OCTET_STRING)) { |
|
| 72 | - $auth_info = $seq->at(2)->asString()->string(); |
|
| 73 | - } |
|
| 74 | - return new static($service, $ident, $auth_info); |
|
| 75 | - } |
|
| 76 | - |
|
| 77 | - /** |
|
| 78 | - * Get service name. |
|
| 79 | - * |
|
| 80 | - * @return GeneralName |
|
| 81 | - */ |
|
| 82 | - public function service(): GeneralName |
|
| 83 | - { |
|
| 84 | - return $this->_service; |
|
| 85 | - } |
|
| 86 | - |
|
| 87 | - /** |
|
| 88 | - * Get ident. |
|
| 89 | - * |
|
| 90 | - * @return GeneralName |
|
| 91 | - */ |
|
| 92 | - public function ident(): GeneralName |
|
| 93 | - { |
|
| 94 | - return $this->_ident; |
|
| 95 | - } |
|
| 96 | - |
|
| 97 | - /** |
|
| 98 | - * Check whether authentication info is present. |
|
| 99 | - * |
|
| 100 | - * @return bool |
|
| 101 | - */ |
|
| 102 | - public function hasAuthInfo(): bool |
|
| 103 | - { |
|
| 104 | - return isset($this->_authInfo); |
|
| 105 | - } |
|
| 106 | - |
|
| 107 | - /** |
|
| 108 | - * Get authentication info. |
|
| 109 | - * |
|
| 110 | - * @throws \LogicException If not set |
|
| 111 | - * |
|
| 112 | - * @return string |
|
| 113 | - */ |
|
| 114 | - public function authInfo(): string |
|
| 115 | - { |
|
| 116 | - if (!$this->hasAuthInfo()) { |
|
| 117 | - throw new \LogicException('authInfo not set.'); |
|
| 118 | - } |
|
| 119 | - return $this->_authInfo; |
|
| 120 | - } |
|
| 121 | - |
|
| 122 | - /** |
|
| 123 | - * {@inheritdoc} |
|
| 124 | - */ |
|
| 125 | - public function toASN1(): Element |
|
| 126 | - { |
|
| 127 | - $elements = [$this->_service->toASN1(), $this->_ident->toASN1()]; |
|
| 128 | - if (isset($this->_authInfo)) { |
|
| 129 | - $elements[] = new OctetString($this->_authInfo); |
|
| 130 | - } |
|
| 131 | - return new Sequence(...$elements); |
|
| 132 | - } |
|
| 133 | - |
|
| 134 | - /** |
|
| 135 | - * {@inheritdoc} |
|
| 136 | - */ |
|
| 137 | - public function stringValue(): string |
|
| 138 | - { |
|
| 139 | - return '#' . bin2hex($this->toASN1()->toDER()); |
|
| 140 | - } |
|
| 141 | - |
|
| 142 | - /** |
|
| 143 | - * {@inheritdoc} |
|
| 144 | - */ |
|
| 145 | - public function equalityMatchingRule(): MatchingRule |
|
| 146 | - { |
|
| 147 | - return new BinaryMatch(); |
|
| 148 | - } |
|
| 149 | - |
|
| 150 | - /** |
|
| 151 | - * {@inheritdoc} |
|
| 152 | - */ |
|
| 153 | - public function rfc2253String(): string |
|
| 154 | - { |
|
| 155 | - return $this->stringValue(); |
|
| 156 | - } |
|
| 157 | - |
|
| 158 | - /** |
|
| 159 | - * {@inheritdoc} |
|
| 160 | - */ |
|
| 161 | - protected function _transcodedString(): string |
|
| 162 | - { |
|
| 163 | - return $this->stringValue(); |
|
| 164 | - } |
|
| 24 | + /** |
|
| 25 | + * Service. |
|
| 26 | + * |
|
| 27 | + * @var GeneralName |
|
| 28 | + */ |
|
| 29 | + protected $_service; |
|
| 30 | + |
|
| 31 | + /** |
|
| 32 | + * Ident. |
|
| 33 | + * |
|
| 34 | + * @var GeneralName |
|
| 35 | + */ |
|
| 36 | + protected $_ident; |
|
| 37 | + |
|
| 38 | + /** |
|
| 39 | + * Auth info. |
|
| 40 | + * |
|
| 41 | + * @var null|string |
|
| 42 | + */ |
|
| 43 | + protected $_authInfo; |
|
| 44 | + |
|
| 45 | + /** |
|
| 46 | + * Constructor. |
|
| 47 | + * |
|
| 48 | + * @param GeneralName $service |
|
| 49 | + * @param GeneralName $ident |
|
| 50 | + * @param null|string $auth_info |
|
| 51 | + */ |
|
| 52 | + public function __construct(GeneralName $service, GeneralName $ident, |
|
| 53 | + ?string $auth_info = null) |
|
| 54 | + { |
|
| 55 | + $this->_service = $service; |
|
| 56 | + $this->_ident = $ident; |
|
| 57 | + $this->_authInfo = $auth_info; |
|
| 58 | + } |
|
| 59 | + |
|
| 60 | + /** |
|
| 61 | + * {@inheritdoc} |
|
| 62 | + * |
|
| 63 | + * @return self |
|
| 64 | + */ |
|
| 65 | + public static function fromASN1(UnspecifiedType $el): AttributeValue |
|
| 66 | + { |
|
| 67 | + $seq = $el->asSequence(); |
|
| 68 | + $service = GeneralName::fromASN1($seq->at(0)->asTagged()); |
|
| 69 | + $ident = GeneralName::fromASN1($seq->at(1)->asTagged()); |
|
| 70 | + $auth_info = null; |
|
| 71 | + if ($seq->has(2, Element::TYPE_OCTET_STRING)) { |
|
| 72 | + $auth_info = $seq->at(2)->asString()->string(); |
|
| 73 | + } |
|
| 74 | + return new static($service, $ident, $auth_info); |
|
| 75 | + } |
|
| 76 | + |
|
| 77 | + /** |
|
| 78 | + * Get service name. |
|
| 79 | + * |
|
| 80 | + * @return GeneralName |
|
| 81 | + */ |
|
| 82 | + public function service(): GeneralName |
|
| 83 | + { |
|
| 84 | + return $this->_service; |
|
| 85 | + } |
|
| 86 | + |
|
| 87 | + /** |
|
| 88 | + * Get ident. |
|
| 89 | + * |
|
| 90 | + * @return GeneralName |
|
| 91 | + */ |
|
| 92 | + public function ident(): GeneralName |
|
| 93 | + { |
|
| 94 | + return $this->_ident; |
|
| 95 | + } |
|
| 96 | + |
|
| 97 | + /** |
|
| 98 | + * Check whether authentication info is present. |
|
| 99 | + * |
|
| 100 | + * @return bool |
|
| 101 | + */ |
|
| 102 | + public function hasAuthInfo(): bool |
|
| 103 | + { |
|
| 104 | + return isset($this->_authInfo); |
|
| 105 | + } |
|
| 106 | + |
|
| 107 | + /** |
|
| 108 | + * Get authentication info. |
|
| 109 | + * |
|
| 110 | + * @throws \LogicException If not set |
|
| 111 | + * |
|
| 112 | + * @return string |
|
| 113 | + */ |
|
| 114 | + public function authInfo(): string |
|
| 115 | + { |
|
| 116 | + if (!$this->hasAuthInfo()) { |
|
| 117 | + throw new \LogicException('authInfo not set.'); |
|
| 118 | + } |
|
| 119 | + return $this->_authInfo; |
|
| 120 | + } |
|
| 121 | + |
|
| 122 | + /** |
|
| 123 | + * {@inheritdoc} |
|
| 124 | + */ |
|
| 125 | + public function toASN1(): Element |
|
| 126 | + { |
|
| 127 | + $elements = [$this->_service->toASN1(), $this->_ident->toASN1()]; |
|
| 128 | + if (isset($this->_authInfo)) { |
|
| 129 | + $elements[] = new OctetString($this->_authInfo); |
|
| 130 | + } |
|
| 131 | + return new Sequence(...$elements); |
|
| 132 | + } |
|
| 133 | + |
|
| 134 | + /** |
|
| 135 | + * {@inheritdoc} |
|
| 136 | + */ |
|
| 137 | + public function stringValue(): string |
|
| 138 | + { |
|
| 139 | + return '#' . bin2hex($this->toASN1()->toDER()); |
|
| 140 | + } |
|
| 141 | + |
|
| 142 | + /** |
|
| 143 | + * {@inheritdoc} |
|
| 144 | + */ |
|
| 145 | + public function equalityMatchingRule(): MatchingRule |
|
| 146 | + { |
|
| 147 | + return new BinaryMatch(); |
|
| 148 | + } |
|
| 149 | + |
|
| 150 | + /** |
|
| 151 | + * {@inheritdoc} |
|
| 152 | + */ |
|
| 153 | + public function rfc2253String(): string |
|
| 154 | + { |
|
| 155 | + return $this->stringValue(); |
|
| 156 | + } |
|
| 157 | + |
|
| 158 | + /** |
|
| 159 | + * {@inheritdoc} |
|
| 160 | + */ |
|
| 161 | + protected function _transcodedString(): string |
|
| 162 | + { |
|
| 163 | + return $this->stringValue(); |
|
| 164 | + } |
|
| 165 | 165 | } |
@@ -24,146 +24,146 @@ |
||
| 24 | 24 | */ |
| 25 | 25 | class RoleAttributeValue extends AttributeValue |
| 26 | 26 | { |
| 27 | - /** |
|
| 28 | - * Issuing authority. |
|
| 29 | - * |
|
| 30 | - * @var null|GeneralNames |
|
| 31 | - */ |
|
| 32 | - protected $_roleAuthority; |
|
| 33 | - |
|
| 34 | - /** |
|
| 35 | - * Role name. |
|
| 36 | - * |
|
| 37 | - * @var GeneralName |
|
| 38 | - */ |
|
| 39 | - protected $_roleName; |
|
| 40 | - |
|
| 41 | - /** |
|
| 42 | - * Constructor. |
|
| 43 | - * |
|
| 44 | - * @param GeneralName $name Role name |
|
| 45 | - * @param null|GeneralNames $authority Issuing authority |
|
| 46 | - */ |
|
| 47 | - public function __construct(GeneralName $name, |
|
| 48 | - ?GeneralNames $authority = null) |
|
| 49 | - { |
|
| 50 | - $this->_roleAuthority = $authority; |
|
| 51 | - $this->_roleName = $name; |
|
| 52 | - $this->_oid = AttributeType::OID_ROLE; |
|
| 53 | - } |
|
| 54 | - |
|
| 55 | - /** |
|
| 56 | - * Initialize from a role string. |
|
| 57 | - * |
|
| 58 | - * @param string $role_name Role name in URI format |
|
| 59 | - * @param null|GeneralNames $authority Issuing authority |
|
| 60 | - * |
|
| 61 | - * @return self |
|
| 62 | - */ |
|
| 63 | - public static function fromString(string $role_name, |
|
| 64 | - ?GeneralNames $authority = null): self |
|
| 65 | - { |
|
| 66 | - return new self(new UniformResourceIdentifier($role_name), $authority); |
|
| 67 | - } |
|
| 68 | - |
|
| 69 | - /** |
|
| 70 | - * {@inheritdoc} |
|
| 71 | - * |
|
| 72 | - * @return self |
|
| 73 | - */ |
|
| 74 | - public static function fromASN1(UnspecifiedType $el): AttributeValue |
|
| 75 | - { |
|
| 76 | - $seq = $el->asSequence(); |
|
| 77 | - $authority = null; |
|
| 78 | - if ($seq->hasTagged(0)) { |
|
| 79 | - $authority = GeneralNames::fromASN1( |
|
| 80 | - $seq->getTagged(0)->asImplicit(Element::TYPE_SEQUENCE) |
|
| 81 | - ->asSequence()); |
|
| 82 | - } |
|
| 83 | - $name = GeneralName::fromASN1($seq->getTagged(1) |
|
| 84 | - ->asExplicit()->asTagged()); |
|
| 85 | - return new self($name, $authority); |
|
| 86 | - } |
|
| 87 | - |
|
| 88 | - /** |
|
| 89 | - * Check whether issuing authority is present. |
|
| 90 | - * |
|
| 91 | - * @return bool |
|
| 92 | - */ |
|
| 93 | - public function hasRoleAuthority(): bool |
|
| 94 | - { |
|
| 95 | - return isset($this->_roleAuthority); |
|
| 96 | - } |
|
| 97 | - |
|
| 98 | - /** |
|
| 99 | - * Get issuing authority. |
|
| 100 | - * |
|
| 101 | - * @throws \LogicException If not set |
|
| 102 | - * |
|
| 103 | - * @return GeneralNames |
|
| 104 | - */ |
|
| 105 | - public function roleAuthority(): GeneralNames |
|
| 106 | - { |
|
| 107 | - if (!$this->hasRoleAuthority()) { |
|
| 108 | - throw new \LogicException('roleAuthority not set.'); |
|
| 109 | - } |
|
| 110 | - return $this->_roleAuthority; |
|
| 111 | - } |
|
| 112 | - |
|
| 113 | - /** |
|
| 114 | - * Get role name. |
|
| 115 | - * |
|
| 116 | - * @return GeneralName |
|
| 117 | - */ |
|
| 118 | - public function roleName(): GeneralName |
|
| 119 | - { |
|
| 120 | - return $this->_roleName; |
|
| 121 | - } |
|
| 122 | - |
|
| 123 | - /** |
|
| 124 | - * {@inheritdoc} |
|
| 125 | - */ |
|
| 126 | - public function toASN1(): Element |
|
| 127 | - { |
|
| 128 | - $elements = []; |
|
| 129 | - if (isset($this->_roleAuthority)) { |
|
| 130 | - $elements[] = new ImplicitlyTaggedType( |
|
| 131 | - 0, $this->_roleAuthority->toASN1()); |
|
| 132 | - } |
|
| 133 | - $elements[] = new ExplicitlyTaggedType( |
|
| 134 | - 1, $this->_roleName->toASN1()); |
|
| 135 | - return new Sequence(...$elements); |
|
| 136 | - } |
|
| 137 | - |
|
| 138 | - /** |
|
| 139 | - * {@inheritdoc} |
|
| 140 | - */ |
|
| 141 | - public function stringValue(): string |
|
| 142 | - { |
|
| 143 | - return '#' . bin2hex($this->toASN1()->toDER()); |
|
| 144 | - } |
|
| 145 | - |
|
| 146 | - /** |
|
| 147 | - * {@inheritdoc} |
|
| 148 | - */ |
|
| 149 | - public function equalityMatchingRule(): MatchingRule |
|
| 150 | - { |
|
| 151 | - return new BinaryMatch(); |
|
| 152 | - } |
|
| 153 | - |
|
| 154 | - /** |
|
| 155 | - * {@inheritdoc} |
|
| 156 | - */ |
|
| 157 | - public function rfc2253String(): string |
|
| 158 | - { |
|
| 159 | - return $this->stringValue(); |
|
| 160 | - } |
|
| 161 | - |
|
| 162 | - /** |
|
| 163 | - * {@inheritdoc} |
|
| 164 | - */ |
|
| 165 | - protected function _transcodedString(): string |
|
| 166 | - { |
|
| 167 | - return $this->stringValue(); |
|
| 168 | - } |
|
| 27 | + /** |
|
| 28 | + * Issuing authority. |
|
| 29 | + * |
|
| 30 | + * @var null|GeneralNames |
|
| 31 | + */ |
|
| 32 | + protected $_roleAuthority; |
|
| 33 | + |
|
| 34 | + /** |
|
| 35 | + * Role name. |
|
| 36 | + * |
|
| 37 | + * @var GeneralName |
|
| 38 | + */ |
|
| 39 | + protected $_roleName; |
|
| 40 | + |
|
| 41 | + /** |
|
| 42 | + * Constructor. |
|
| 43 | + * |
|
| 44 | + * @param GeneralName $name Role name |
|
| 45 | + * @param null|GeneralNames $authority Issuing authority |
|
| 46 | + */ |
|
| 47 | + public function __construct(GeneralName $name, |
|
| 48 | + ?GeneralNames $authority = null) |
|
| 49 | + { |
|
| 50 | + $this->_roleAuthority = $authority; |
|
| 51 | + $this->_roleName = $name; |
|
| 52 | + $this->_oid = AttributeType::OID_ROLE; |
|
| 53 | + } |
|
| 54 | + |
|
| 55 | + /** |
|
| 56 | + * Initialize from a role string. |
|
| 57 | + * |
|
| 58 | + * @param string $role_name Role name in URI format |
|
| 59 | + * @param null|GeneralNames $authority Issuing authority |
|
| 60 | + * |
|
| 61 | + * @return self |
|
| 62 | + */ |
|
| 63 | + public static function fromString(string $role_name, |
|
| 64 | + ?GeneralNames $authority = null): self |
|
| 65 | + { |
|
| 66 | + return new self(new UniformResourceIdentifier($role_name), $authority); |
|
| 67 | + } |
|
| 68 | + |
|
| 69 | + /** |
|
| 70 | + * {@inheritdoc} |
|
| 71 | + * |
|
| 72 | + * @return self |
|
| 73 | + */ |
|
| 74 | + public static function fromASN1(UnspecifiedType $el): AttributeValue |
|
| 75 | + { |
|
| 76 | + $seq = $el->asSequence(); |
|
| 77 | + $authority = null; |
|
| 78 | + if ($seq->hasTagged(0)) { |
|
| 79 | + $authority = GeneralNames::fromASN1( |
|
| 80 | + $seq->getTagged(0)->asImplicit(Element::TYPE_SEQUENCE) |
|
| 81 | + ->asSequence()); |
|
| 82 | + } |
|
| 83 | + $name = GeneralName::fromASN1($seq->getTagged(1) |
|
| 84 | + ->asExplicit()->asTagged()); |
|
| 85 | + return new self($name, $authority); |
|
| 86 | + } |
|
| 87 | + |
|
| 88 | + /** |
|
| 89 | + * Check whether issuing authority is present. |
|
| 90 | + * |
|
| 91 | + * @return bool |
|
| 92 | + */ |
|
| 93 | + public function hasRoleAuthority(): bool |
|
| 94 | + { |
|
| 95 | + return isset($this->_roleAuthority); |
|
| 96 | + } |
|
| 97 | + |
|
| 98 | + /** |
|
| 99 | + * Get issuing authority. |
|
| 100 | + * |
|
| 101 | + * @throws \LogicException If not set |
|
| 102 | + * |
|
| 103 | + * @return GeneralNames |
|
| 104 | + */ |
|
| 105 | + public function roleAuthority(): GeneralNames |
|
| 106 | + { |
|
| 107 | + if (!$this->hasRoleAuthority()) { |
|
| 108 | + throw new \LogicException('roleAuthority not set.'); |
|
| 109 | + } |
|
| 110 | + return $this->_roleAuthority; |
|
| 111 | + } |
|
| 112 | + |
|
| 113 | + /** |
|
| 114 | + * Get role name. |
|
| 115 | + * |
|
| 116 | + * @return GeneralName |
|
| 117 | + */ |
|
| 118 | + public function roleName(): GeneralName |
|
| 119 | + { |
|
| 120 | + return $this->_roleName; |
|
| 121 | + } |
|
| 122 | + |
|
| 123 | + /** |
|
| 124 | + * {@inheritdoc} |
|
| 125 | + */ |
|
| 126 | + public function toASN1(): Element |
|
| 127 | + { |
|
| 128 | + $elements = []; |
|
| 129 | + if (isset($this->_roleAuthority)) { |
|
| 130 | + $elements[] = new ImplicitlyTaggedType( |
|
| 131 | + 0, $this->_roleAuthority->toASN1()); |
|
| 132 | + } |
|
| 133 | + $elements[] = new ExplicitlyTaggedType( |
|
| 134 | + 1, $this->_roleName->toASN1()); |
|
| 135 | + return new Sequence(...$elements); |
|
| 136 | + } |
|
| 137 | + |
|
| 138 | + /** |
|
| 139 | + * {@inheritdoc} |
|
| 140 | + */ |
|
| 141 | + public function stringValue(): string |
|
| 142 | + { |
|
| 143 | + return '#' . bin2hex($this->toASN1()->toDER()); |
|
| 144 | + } |
|
| 145 | + |
|
| 146 | + /** |
|
| 147 | + * {@inheritdoc} |
|
| 148 | + */ |
|
| 149 | + public function equalityMatchingRule(): MatchingRule |
|
| 150 | + { |
|
| 151 | + return new BinaryMatch(); |
|
| 152 | + } |
|
| 153 | + |
|
| 154 | + /** |
|
| 155 | + * {@inheritdoc} |
|
| 156 | + */ |
|
| 157 | + public function rfc2253String(): string |
|
| 158 | + { |
|
| 159 | + return $this->stringValue(); |
|
| 160 | + } |
|
| 161 | + |
|
| 162 | + /** |
|
| 163 | + * {@inheritdoc} |
|
| 164 | + */ |
|
| 165 | + protected function _transcodedString(): string |
|
| 166 | + { |
|
| 167 | + return $this->stringValue(); |
|
| 168 | + } |
|
| 169 | 169 | } |