@@ -11,16 +11,16 @@ |
||
| 11 | 11 | */ |
| 12 | 12 | class GroupAttributeValue extends IetfAttrSyntax |
| 13 | 13 | { |
| 14 | - const OID = "1.3.6.1.5.5.7.10.4"; |
|
| 14 | + const OID = "1.3.6.1.5.5.7.10.4"; |
|
| 15 | 15 | |
| 16 | - /** |
|
| 17 | - * Constructor. |
|
| 18 | - * |
|
| 19 | - * @param IetfAttrValue[] $values |
|
| 20 | - */ |
|
| 21 | - public function __construct(IetfAttrValue ...$values) |
|
| 22 | - { |
|
| 23 | - parent::__construct(...$values); |
|
| 24 | - $this->_oid = self::OID; |
|
| 25 | - } |
|
| 16 | + /** |
|
| 17 | + * Constructor. |
|
| 18 | + * |
|
| 19 | + * @param IetfAttrValue[] $values |
|
| 20 | + */ |
|
| 21 | + public function __construct(IetfAttrValue ...$values) |
|
| 22 | + { |
|
| 23 | + parent::__construct(...$values); |
|
| 24 | + $this->_oid = self::OID; |
|
| 25 | + } |
|
| 26 | 26 | } |
@@ -11,16 +11,16 @@ |
||
| 11 | 11 | */ |
| 12 | 12 | class ChargingIdentityAttributeValue extends IetfAttrSyntax |
| 13 | 13 | { |
| 14 | - const OID = "1.3.6.1.5.5.7.10.3"; |
|
| 14 | + const OID = "1.3.6.1.5.5.7.10.3"; |
|
| 15 | 15 | |
| 16 | - /** |
|
| 17 | - * Constructor. |
|
| 18 | - * |
|
| 19 | - * @param IetfAttrValue[] $values |
|
| 20 | - */ |
|
| 21 | - public function __construct(IetfAttrValue ...$values) |
|
| 22 | - { |
|
| 23 | - parent::__construct(...$values); |
|
| 24 | - $this->_oid = self::OID; |
|
| 25 | - } |
|
| 16 | + /** |
|
| 17 | + * Constructor. |
|
| 18 | + * |
|
| 19 | + * @param IetfAttrValue[] $values |
|
| 20 | + */ |
|
| 21 | + public function __construct(IetfAttrValue ...$values) |
|
| 22 | + { |
|
| 23 | + parent::__construct(...$values); |
|
| 24 | + $this->_oid = self::OID; |
|
| 25 | + } |
|
| 26 | 26 | } |
@@ -13,16 +13,16 @@ |
||
| 13 | 13 | */ |
| 14 | 14 | class FreshestCRLExtension extends CRLDistributionPointsExtension |
| 15 | 15 | { |
| 16 | - /** |
|
| 17 | - * Constructor. |
|
| 18 | - * |
|
| 19 | - * @param bool $critical |
|
| 20 | - * @param DistributionPoint ...$distribution_points |
|
| 21 | - */ |
|
| 22 | - public function __construct(bool $critical, |
|
| 23 | - DistributionPoint ...$distribution_points) |
|
| 24 | - { |
|
| 25 | - Extension::__construct(self::OID_FRESHEST_CRL, $critical); |
|
| 26 | - $this->_distributionPoints = $distribution_points; |
|
| 27 | - } |
|
| 16 | + /** |
|
| 17 | + * Constructor. |
|
| 18 | + * |
|
| 19 | + * @param bool $critical |
|
| 20 | + * @param DistributionPoint ...$distribution_points |
|
| 21 | + */ |
|
| 22 | + public function __construct(bool $critical, |
|
| 23 | + DistributionPoint ...$distribution_points) |
|
| 24 | + { |
|
| 25 | + Extension::__construct(self::OID_FRESHEST_CRL, $critical); |
|
| 26 | + $this->_distributionPoints = $distribution_points; |
|
| 27 | + } |
|
| 28 | 28 | } |
@@ -89,16 +89,16 @@ |
||
| 89 | 89 | { |
| 90 | 90 | $dt = $this->_dt; |
| 91 | 91 | switch ($this->_type) { |
| 92 | - case Element::TYPE_UTC_TIME: |
|
| 93 | - return new UTCTime($dt); |
|
| 94 | - case Element::TYPE_GENERALIZED_TIME: |
|
| 95 | - // GeneralizedTime must not contain fractional seconds |
|
| 96 | - // (rfc5280 4.1.2.5.2) |
|
| 97 | - if ($dt->format("u") != 0) { |
|
| 98 | - // remove fractional seconds (round down) |
|
| 99 | - $dt = self::_roundDownFractionalSeconds($dt); |
|
| 100 | - } |
|
| 101 | - return new GeneralizedTime($dt); |
|
| 92 | + case Element::TYPE_UTC_TIME: |
|
| 93 | + return new UTCTime($dt); |
|
| 94 | + case Element::TYPE_GENERALIZED_TIME: |
|
| 95 | + // GeneralizedTime must not contain fractional seconds |
|
| 96 | + // (rfc5280 4.1.2.5.2) |
|
| 97 | + if ($dt->format("u") != 0) { |
|
| 98 | + // remove fractional seconds (round down) |
|
| 99 | + $dt = self::_roundDownFractionalSeconds($dt); |
|
| 100 | + } |
|
| 101 | + return new GeneralizedTime($dt); |
|
| 102 | 102 | } |
| 103 | 103 | throw new \UnexpectedValueException( |
| 104 | 104 | "Time type " . Element::tagToName($this->_type) . " not supported."); |
@@ -17,104 +17,104 @@ |
||
| 17 | 17 | */ |
| 18 | 18 | class Time |
| 19 | 19 | { |
| 20 | - use DateTimeHelper; |
|
| 20 | + use DateTimeHelper; |
|
| 21 | 21 | |
| 22 | - /** |
|
| 23 | - * Datetime. |
|
| 24 | - * |
|
| 25 | - * @var \DateTimeImmutable $_dt |
|
| 26 | - */ |
|
| 27 | - protected $_dt; |
|
| 22 | + /** |
|
| 23 | + * Datetime. |
|
| 24 | + * |
|
| 25 | + * @var \DateTimeImmutable $_dt |
|
| 26 | + */ |
|
| 27 | + protected $_dt; |
|
| 28 | 28 | |
| 29 | - /** |
|
| 30 | - * Time ASN.1 type tag. |
|
| 31 | - * |
|
| 32 | - * @var int $_type |
|
| 33 | - */ |
|
| 34 | - protected $_type; |
|
| 29 | + /** |
|
| 30 | + * Time ASN.1 type tag. |
|
| 31 | + * |
|
| 32 | + * @var int $_type |
|
| 33 | + */ |
|
| 34 | + protected $_type; |
|
| 35 | 35 | |
| 36 | - /** |
|
| 37 | - * Constructor. |
|
| 38 | - * |
|
| 39 | - * @param \DateTimeImmutable $dt |
|
| 40 | - */ |
|
| 41 | - public function __construct(\DateTimeImmutable $dt) |
|
| 42 | - { |
|
| 43 | - $this->_dt = $dt; |
|
| 44 | - $this->_type = self::_determineType($dt); |
|
| 45 | - } |
|
| 36 | + /** |
|
| 37 | + * Constructor. |
|
| 38 | + * |
|
| 39 | + * @param \DateTimeImmutable $dt |
|
| 40 | + */ |
|
| 41 | + public function __construct(\DateTimeImmutable $dt) |
|
| 42 | + { |
|
| 43 | + $this->_dt = $dt; |
|
| 44 | + $this->_type = self::_determineType($dt); |
|
| 45 | + } |
|
| 46 | 46 | |
| 47 | - /** |
|
| 48 | - * Initialize from ASN.1. |
|
| 49 | - * |
|
| 50 | - * @param TimeType $el |
|
| 51 | - * @return self |
|
| 52 | - */ |
|
| 53 | - public static function fromASN1(TimeType $el): self |
|
| 54 | - { |
|
| 55 | - $obj = new self($el->dateTime()); |
|
| 56 | - $obj->_type = $el->tag(); |
|
| 57 | - return $obj; |
|
| 58 | - } |
|
| 47 | + /** |
|
| 48 | + * Initialize from ASN.1. |
|
| 49 | + * |
|
| 50 | + * @param TimeType $el |
|
| 51 | + * @return self |
|
| 52 | + */ |
|
| 53 | + public static function fromASN1(TimeType $el): self |
|
| 54 | + { |
|
| 55 | + $obj = new self($el->dateTime()); |
|
| 56 | + $obj->_type = $el->tag(); |
|
| 57 | + return $obj; |
|
| 58 | + } |
|
| 59 | 59 | |
| 60 | - /** |
|
| 61 | - * Initialize from date string. |
|
| 62 | - * |
|
| 63 | - * @param string|null $time |
|
| 64 | - * @param string|null $tz |
|
| 65 | - * @return self |
|
| 66 | - */ |
|
| 67 | - public static function fromString($time, $tz = null): self |
|
| 68 | - { |
|
| 69 | - return new self(self::_createDateTime($time, $tz)); |
|
| 70 | - } |
|
| 60 | + /** |
|
| 61 | + * Initialize from date string. |
|
| 62 | + * |
|
| 63 | + * @param string|null $time |
|
| 64 | + * @param string|null $tz |
|
| 65 | + * @return self |
|
| 66 | + */ |
|
| 67 | + public static function fromString($time, $tz = null): self |
|
| 68 | + { |
|
| 69 | + return new self(self::_createDateTime($time, $tz)); |
|
| 70 | + } |
|
| 71 | 71 | |
| 72 | - /** |
|
| 73 | - * Get datetime. |
|
| 74 | - * |
|
| 75 | - * @return \DateTimeImmutable |
|
| 76 | - */ |
|
| 77 | - public function dateTime(): \DateTimeImmutable |
|
| 78 | - { |
|
| 79 | - return $this->_dt; |
|
| 80 | - } |
|
| 72 | + /** |
|
| 73 | + * Get datetime. |
|
| 74 | + * |
|
| 75 | + * @return \DateTimeImmutable |
|
| 76 | + */ |
|
| 77 | + public function dateTime(): \DateTimeImmutable |
|
| 78 | + { |
|
| 79 | + return $this->_dt; |
|
| 80 | + } |
|
| 81 | 81 | |
| 82 | - /** |
|
| 83 | - * Generate ASN.1. |
|
| 84 | - * |
|
| 85 | - * @throws \UnexpectedValueException |
|
| 86 | - * @return TimeType |
|
| 87 | - */ |
|
| 88 | - public function toASN1(): TimeType |
|
| 89 | - { |
|
| 90 | - $dt = $this->_dt; |
|
| 91 | - switch ($this->_type) { |
|
| 92 | - case Element::TYPE_UTC_TIME: |
|
| 93 | - return new UTCTime($dt); |
|
| 94 | - case Element::TYPE_GENERALIZED_TIME: |
|
| 95 | - // GeneralizedTime must not contain fractional seconds |
|
| 96 | - // (rfc5280 4.1.2.5.2) |
|
| 97 | - if ($dt->format("u") != 0) { |
|
| 98 | - // remove fractional seconds (round down) |
|
| 99 | - $dt = self::_roundDownFractionalSeconds($dt); |
|
| 100 | - } |
|
| 101 | - return new GeneralizedTime($dt); |
|
| 102 | - } |
|
| 103 | - throw new \UnexpectedValueException( |
|
| 104 | - "Time type " . Element::tagToName($this->_type) . " not supported."); |
|
| 105 | - } |
|
| 82 | + /** |
|
| 83 | + * Generate ASN.1. |
|
| 84 | + * |
|
| 85 | + * @throws \UnexpectedValueException |
|
| 86 | + * @return TimeType |
|
| 87 | + */ |
|
| 88 | + public function toASN1(): TimeType |
|
| 89 | + { |
|
| 90 | + $dt = $this->_dt; |
|
| 91 | + switch ($this->_type) { |
|
| 92 | + case Element::TYPE_UTC_TIME: |
|
| 93 | + return new UTCTime($dt); |
|
| 94 | + case Element::TYPE_GENERALIZED_TIME: |
|
| 95 | + // GeneralizedTime must not contain fractional seconds |
|
| 96 | + // (rfc5280 4.1.2.5.2) |
|
| 97 | + if ($dt->format("u") != 0) { |
|
| 98 | + // remove fractional seconds (round down) |
|
| 99 | + $dt = self::_roundDownFractionalSeconds($dt); |
|
| 100 | + } |
|
| 101 | + return new GeneralizedTime($dt); |
|
| 102 | + } |
|
| 103 | + throw new \UnexpectedValueException( |
|
| 104 | + "Time type " . Element::tagToName($this->_type) . " not supported."); |
|
| 105 | + } |
|
| 106 | 106 | |
| 107 | - /** |
|
| 108 | - * Determine whether to use UTCTime or GeneralizedTime ASN.1 type. |
|
| 109 | - * |
|
| 110 | - * @param \DateTimeImmutable $dt |
|
| 111 | - * @return int Type tag |
|
| 112 | - */ |
|
| 113 | - protected static function _determineType(\DateTimeImmutable $dt): int |
|
| 114 | - { |
|
| 115 | - if ($dt->format("Y") >= 2050) { |
|
| 116 | - return Element::TYPE_GENERALIZED_TIME; |
|
| 117 | - } |
|
| 118 | - return Element::TYPE_UTC_TIME; |
|
| 119 | - } |
|
| 107 | + /** |
|
| 108 | + * Determine whether to use UTCTime or GeneralizedTime ASN.1 type. |
|
| 109 | + * |
|
| 110 | + * @param \DateTimeImmutable $dt |
|
| 111 | + * @return int Type tag |
|
| 112 | + */ |
|
| 113 | + protected static function _determineType(\DateTimeImmutable $dt): int |
|
| 114 | + { |
|
| 115 | + if ($dt->format("Y") >= 2050) { |
|
| 116 | + return Element::TYPE_GENERALIZED_TIME; |
|
| 117 | + } |
|
| 118 | + return Element::TYPE_UTC_TIME; |
|
| 119 | + } |
|
| 120 | 120 | } |
@@ -16,85 +16,85 @@ |
||
| 16 | 16 | */ |
| 17 | 17 | class PathValidationResult |
| 18 | 18 | { |
| 19 | - /** |
|
| 20 | - * Certificates in a certification path. |
|
| 21 | - * |
|
| 22 | - * @var \X509\Certificate\Certificate[] $_certificates |
|
| 23 | - */ |
|
| 24 | - protected $_certificates; |
|
| 19 | + /** |
|
| 20 | + * Certificates in a certification path. |
|
| 21 | + * |
|
| 22 | + * @var \X509\Certificate\Certificate[] $_certificates |
|
| 23 | + */ |
|
| 24 | + protected $_certificates; |
|
| 25 | 25 | |
| 26 | - /** |
|
| 27 | - * Valid policy tree. |
|
| 28 | - * |
|
| 29 | - * @var \X509\CertificationPath\Policy\PolicyTree|null $_policyTree |
|
| 30 | - */ |
|
| 31 | - protected $_policyTree; |
|
| 26 | + /** |
|
| 27 | + * Valid policy tree. |
|
| 28 | + * |
|
| 29 | + * @var \X509\CertificationPath\Policy\PolicyTree|null $_policyTree |
|
| 30 | + */ |
|
| 31 | + protected $_policyTree; |
|
| 32 | 32 | |
| 33 | - /** |
|
| 34 | - * End-entity certificate's public key. |
|
| 35 | - * |
|
| 36 | - * @var PublicKeyInfo |
|
| 37 | - */ |
|
| 38 | - protected $_publicKeyInfo; |
|
| 33 | + /** |
|
| 34 | + * End-entity certificate's public key. |
|
| 35 | + * |
|
| 36 | + * @var PublicKeyInfo |
|
| 37 | + */ |
|
| 38 | + protected $_publicKeyInfo; |
|
| 39 | 39 | |
| 40 | - /** |
|
| 41 | - * Public key algorithm. |
|
| 42 | - * |
|
| 43 | - * @var AlgorithmIdentifierType |
|
| 44 | - */ |
|
| 45 | - protected $_publicKeyAlgo; |
|
| 40 | + /** |
|
| 41 | + * Public key algorithm. |
|
| 42 | + * |
|
| 43 | + * @var AlgorithmIdentifierType |
|
| 44 | + */ |
|
| 45 | + protected $_publicKeyAlgo; |
|
| 46 | 46 | |
| 47 | - /** |
|
| 48 | - * Public key parameters. |
|
| 49 | - * |
|
| 50 | - * @var Element|null $_publicKeyParameters |
|
| 51 | - */ |
|
| 52 | - protected $_publicKeyParameters; |
|
| 47 | + /** |
|
| 48 | + * Public key parameters. |
|
| 49 | + * |
|
| 50 | + * @var Element|null $_publicKeyParameters |
|
| 51 | + */ |
|
| 52 | + protected $_publicKeyParameters; |
|
| 53 | 53 | |
| 54 | - /** |
|
| 55 | - * Constructor. |
|
| 56 | - * |
|
| 57 | - * @param \X509\Certificate\Certificate[] $certificates Certificates in a |
|
| 58 | - * certification path |
|
| 59 | - * @param \X509\CertificationPath\Policy\PolicyTree|null $policy_tree Valid |
|
| 60 | - * policy tree |
|
| 61 | - * @param PublicKeyInfo $pubkey_info Public key of the end-entity |
|
| 62 | - * certificate |
|
| 63 | - * @param AlgorithmIdentifierType $algo Public key algorithm of the |
|
| 64 | - * end-entity certificate |
|
| 65 | - * @param Element|null $params Algorithm parameters |
|
| 66 | - */ |
|
| 67 | - public function __construct(array $certificates, $policy_tree, |
|
| 68 | - PublicKeyInfo $pubkey_info, AlgorithmIdentifierType $algo, |
|
| 69 | - Element $params = null) |
|
| 70 | - { |
|
| 71 | - $this->_certificates = array_values($certificates); |
|
| 72 | - $this->_policyTree = $policy_tree; |
|
| 73 | - $this->_publicKeyInfo = $pubkey_info; |
|
| 74 | - $this->_publicKeyAlgo = $algo; |
|
| 75 | - $this->_publicKeyParameters = $params; |
|
| 76 | - } |
|
| 54 | + /** |
|
| 55 | + * Constructor. |
|
| 56 | + * |
|
| 57 | + * @param \X509\Certificate\Certificate[] $certificates Certificates in a |
|
| 58 | + * certification path |
|
| 59 | + * @param \X509\CertificationPath\Policy\PolicyTree|null $policy_tree Valid |
|
| 60 | + * policy tree |
|
| 61 | + * @param PublicKeyInfo $pubkey_info Public key of the end-entity |
|
| 62 | + * certificate |
|
| 63 | + * @param AlgorithmIdentifierType $algo Public key algorithm of the |
|
| 64 | + * end-entity certificate |
|
| 65 | + * @param Element|null $params Algorithm parameters |
|
| 66 | + */ |
|
| 67 | + public function __construct(array $certificates, $policy_tree, |
|
| 68 | + PublicKeyInfo $pubkey_info, AlgorithmIdentifierType $algo, |
|
| 69 | + Element $params = null) |
|
| 70 | + { |
|
| 71 | + $this->_certificates = array_values($certificates); |
|
| 72 | + $this->_policyTree = $policy_tree; |
|
| 73 | + $this->_publicKeyInfo = $pubkey_info; |
|
| 74 | + $this->_publicKeyAlgo = $algo; |
|
| 75 | + $this->_publicKeyParameters = $params; |
|
| 76 | + } |
|
| 77 | 77 | |
| 78 | - /** |
|
| 79 | - * Get end-entity certificate. |
|
| 80 | - * |
|
| 81 | - * @return \X509\Certificate\Certificate |
|
| 82 | - */ |
|
| 83 | - public function certificate(): Certificate |
|
| 84 | - { |
|
| 85 | - return $this->_certificates[count($this->_certificates) - 1]; |
|
| 86 | - } |
|
| 78 | + /** |
|
| 79 | + * Get end-entity certificate. |
|
| 80 | + * |
|
| 81 | + * @return \X509\Certificate\Certificate |
|
| 82 | + */ |
|
| 83 | + public function certificate(): Certificate |
|
| 84 | + { |
|
| 85 | + return $this->_certificates[count($this->_certificates) - 1]; |
|
| 86 | + } |
|
| 87 | 87 | |
| 88 | - /** |
|
| 89 | - * Get certificate policies of the end-entity certificate. |
|
| 90 | - * |
|
| 91 | - * @return \X509\Certificate\Extension\CertificatePolicy\PolicyInformation[] |
|
| 92 | - */ |
|
| 93 | - public function policies(): array |
|
| 94 | - { |
|
| 95 | - if (!$this->_policyTree) { |
|
| 96 | - return array(); |
|
| 97 | - } |
|
| 98 | - return $this->_policyTree->policiesAtDepth(count($this->_certificates)); |
|
| 99 | - } |
|
| 88 | + /** |
|
| 89 | + * Get certificate policies of the end-entity certificate. |
|
| 90 | + * |
|
| 91 | + * @return \X509\Certificate\Extension\CertificatePolicy\PolicyInformation[] |
|
| 92 | + */ |
|
| 93 | + public function policies(): array |
|
| 94 | + { |
|
| 95 | + if (!$this->_policyTree) { |
|
| 96 | + return array(); |
|
| 97 | + } |
|
| 98 | + return $this->_policyTree->policiesAtDepth(count($this->_certificates)); |
|
| 99 | + } |
|
| 100 | 100 | } |
@@ -16,137 +16,137 @@ |
||
| 16 | 16 | */ |
| 17 | 17 | class CertificationPathBuilder |
| 18 | 18 | { |
| 19 | - /** |
|
| 20 | - * Trust anchors. |
|
| 21 | - * |
|
| 22 | - * @var CertificateBundle |
|
| 23 | - */ |
|
| 24 | - protected $_trustList; |
|
| 19 | + /** |
|
| 20 | + * Trust anchors. |
|
| 21 | + * |
|
| 22 | + * @var CertificateBundle |
|
| 23 | + */ |
|
| 24 | + protected $_trustList; |
|
| 25 | 25 | |
| 26 | - /** |
|
| 27 | - * Constructor. |
|
| 28 | - * |
|
| 29 | - * @param CertificateBundle $trust_list List of trust anchors |
|
| 30 | - */ |
|
| 31 | - public function __construct(CertificateBundle $trust_list) |
|
| 32 | - { |
|
| 33 | - $this->_trustList = $trust_list; |
|
| 34 | - } |
|
| 26 | + /** |
|
| 27 | + * Constructor. |
|
| 28 | + * |
|
| 29 | + * @param CertificateBundle $trust_list List of trust anchors |
|
| 30 | + */ |
|
| 31 | + public function __construct(CertificateBundle $trust_list) |
|
| 32 | + { |
|
| 33 | + $this->_trustList = $trust_list; |
|
| 34 | + } |
|
| 35 | 35 | |
| 36 | - /** |
|
| 37 | - * Get all certification paths to given target certificate from |
|
| 38 | - * any trust anchor. |
|
| 39 | - * |
|
| 40 | - * @param Certificate $target Target certificate |
|
| 41 | - * @param CertificateBundle|null $intermediate Optional intermediate |
|
| 42 | - * certificates |
|
| 43 | - * @return CertificationPath[] |
|
| 44 | - */ |
|
| 45 | - public function allPathsToTarget(Certificate $target, |
|
| 46 | - CertificateBundle $intermediate = null): array |
|
| 47 | - { |
|
| 48 | - $paths = $this->_resolvePathsToTarget($target, $intermediate); |
|
| 49 | - // map paths to CertificationPath objects |
|
| 50 | - return array_map( |
|
| 51 | - function ($certs) { |
|
| 52 | - return new CertificationPath(...$certs); |
|
| 53 | - }, $paths); |
|
| 54 | - } |
|
| 36 | + /** |
|
| 37 | + * Get all certification paths to given target certificate from |
|
| 38 | + * any trust anchor. |
|
| 39 | + * |
|
| 40 | + * @param Certificate $target Target certificate |
|
| 41 | + * @param CertificateBundle|null $intermediate Optional intermediate |
|
| 42 | + * certificates |
|
| 43 | + * @return CertificationPath[] |
|
| 44 | + */ |
|
| 45 | + public function allPathsToTarget(Certificate $target, |
|
| 46 | + CertificateBundle $intermediate = null): array |
|
| 47 | + { |
|
| 48 | + $paths = $this->_resolvePathsToTarget($target, $intermediate); |
|
| 49 | + // map paths to CertificationPath objects |
|
| 50 | + return array_map( |
|
| 51 | + function ($certs) { |
|
| 52 | + return new CertificationPath(...$certs); |
|
| 53 | + }, $paths); |
|
| 54 | + } |
|
| 55 | 55 | |
| 56 | - /** |
|
| 57 | - * Resolve all possible certification paths from any trust anchor to |
|
| 58 | - * the target certificate, using optional intermediate certificates. |
|
| 59 | - * |
|
| 60 | - * Helper method for allPathsToTarget to be called recursively. |
|
| 61 | - * |
|
| 62 | - * @todo Implement loop detection |
|
| 63 | - * @param Certificate $target |
|
| 64 | - * @param CertificateBundle $intermediate |
|
| 65 | - * @return array[] Array of arrays containing path certificates |
|
| 66 | - */ |
|
| 67 | - private function _resolvePathsToTarget(Certificate $target, |
|
| 68 | - CertificateBundle $intermediate = null): array |
|
| 69 | - { |
|
| 70 | - // array of possible paths |
|
| 71 | - $paths = array(); |
|
| 72 | - // signed by certificate in the trust list |
|
| 73 | - foreach ($this->_findIssuers($target, $this->_trustList) as $issuer) { |
|
| 74 | - // if target is self-signed, path consists of only |
|
| 75 | - // the target certificate |
|
| 76 | - if ($target->equals($issuer)) { |
|
| 77 | - $paths[] = array($target); |
|
| 78 | - } else { |
|
| 79 | - $paths[] = array($issuer, $target); |
|
| 80 | - } |
|
| 81 | - } |
|
| 82 | - if (isset($intermediate)) { |
|
| 83 | - // signed by intermediate certificate |
|
| 84 | - foreach ($this->_findIssuers($target, $intermediate) as $issuer) { |
|
| 85 | - // intermediate certificate must not be self-signed |
|
| 86 | - if ($issuer->isSelfIssued()) { |
|
| 87 | - continue; |
|
| 88 | - } |
|
| 89 | - // resolve paths to issuer |
|
| 90 | - $subpaths = $this->_resolvePathsToTarget($issuer, $intermediate); |
|
| 91 | - foreach ($subpaths as $path) { |
|
| 92 | - $paths[] = array_merge($path, array($target)); |
|
| 93 | - } |
|
| 94 | - } |
|
| 95 | - } |
|
| 96 | - return $paths; |
|
| 97 | - } |
|
| 56 | + /** |
|
| 57 | + * Resolve all possible certification paths from any trust anchor to |
|
| 58 | + * the target certificate, using optional intermediate certificates. |
|
| 59 | + * |
|
| 60 | + * Helper method for allPathsToTarget to be called recursively. |
|
| 61 | + * |
|
| 62 | + * @todo Implement loop detection |
|
| 63 | + * @param Certificate $target |
|
| 64 | + * @param CertificateBundle $intermediate |
|
| 65 | + * @return array[] Array of arrays containing path certificates |
|
| 66 | + */ |
|
| 67 | + private function _resolvePathsToTarget(Certificate $target, |
|
| 68 | + CertificateBundle $intermediate = null): array |
|
| 69 | + { |
|
| 70 | + // array of possible paths |
|
| 71 | + $paths = array(); |
|
| 72 | + // signed by certificate in the trust list |
|
| 73 | + foreach ($this->_findIssuers($target, $this->_trustList) as $issuer) { |
|
| 74 | + // if target is self-signed, path consists of only |
|
| 75 | + // the target certificate |
|
| 76 | + if ($target->equals($issuer)) { |
|
| 77 | + $paths[] = array($target); |
|
| 78 | + } else { |
|
| 79 | + $paths[] = array($issuer, $target); |
|
| 80 | + } |
|
| 81 | + } |
|
| 82 | + if (isset($intermediate)) { |
|
| 83 | + // signed by intermediate certificate |
|
| 84 | + foreach ($this->_findIssuers($target, $intermediate) as $issuer) { |
|
| 85 | + // intermediate certificate must not be self-signed |
|
| 86 | + if ($issuer->isSelfIssued()) { |
|
| 87 | + continue; |
|
| 88 | + } |
|
| 89 | + // resolve paths to issuer |
|
| 90 | + $subpaths = $this->_resolvePathsToTarget($issuer, $intermediate); |
|
| 91 | + foreach ($subpaths as $path) { |
|
| 92 | + $paths[] = array_merge($path, array($target)); |
|
| 93 | + } |
|
| 94 | + } |
|
| 95 | + } |
|
| 96 | + return $paths; |
|
| 97 | + } |
|
| 98 | 98 | |
| 99 | - /** |
|
| 100 | - * Get shortest path to given target certificate from any trust anchor. |
|
| 101 | - * |
|
| 102 | - * @param Certificate $target Target certificate |
|
| 103 | - * @param CertificateBundle|null $intermediate Optional intermediate |
|
| 104 | - * certificates |
|
| 105 | - * @throws PathBuildingException |
|
| 106 | - * @return CertificationPath |
|
| 107 | - */ |
|
| 108 | - public function shortestPathToTarget(Certificate $target, |
|
| 109 | - CertificateBundle $intermediate = null): CertificationPath |
|
| 110 | - { |
|
| 111 | - $paths = $this->allPathsToTarget($target, $intermediate); |
|
| 112 | - if (!count($paths)) { |
|
| 113 | - throw new PathBuildingException("No certification paths."); |
|
| 114 | - } |
|
| 115 | - usort($paths, |
|
| 116 | - function ($a, $b) { |
|
| 117 | - return count($a) < count($b) ? -1 : 1; |
|
| 118 | - }); |
|
| 119 | - return reset($paths); |
|
| 120 | - } |
|
| 99 | + /** |
|
| 100 | + * Get shortest path to given target certificate from any trust anchor. |
|
| 101 | + * |
|
| 102 | + * @param Certificate $target Target certificate |
|
| 103 | + * @param CertificateBundle|null $intermediate Optional intermediate |
|
| 104 | + * certificates |
|
| 105 | + * @throws PathBuildingException |
|
| 106 | + * @return CertificationPath |
|
| 107 | + */ |
|
| 108 | + public function shortestPathToTarget(Certificate $target, |
|
| 109 | + CertificateBundle $intermediate = null): CertificationPath |
|
| 110 | + { |
|
| 111 | + $paths = $this->allPathsToTarget($target, $intermediate); |
|
| 112 | + if (!count($paths)) { |
|
| 113 | + throw new PathBuildingException("No certification paths."); |
|
| 114 | + } |
|
| 115 | + usort($paths, |
|
| 116 | + function ($a, $b) { |
|
| 117 | + return count($a) < count($b) ? -1 : 1; |
|
| 118 | + }); |
|
| 119 | + return reset($paths); |
|
| 120 | + } |
|
| 121 | 121 | |
| 122 | - /** |
|
| 123 | - * Find all issuers of the target certificate from a given bundle. |
|
| 124 | - * |
|
| 125 | - * @param Certificate $target Target certificate |
|
| 126 | - * @param CertificateBundle $bundle Certificates to search |
|
| 127 | - * @return Certificate[] |
|
| 128 | - */ |
|
| 129 | - protected function _findIssuers(Certificate $target, |
|
| 130 | - CertificateBundle $bundle): array |
|
| 131 | - { |
|
| 132 | - $issuers = array(); |
|
| 133 | - $issuer_name = $target->tbsCertificate()->issuer(); |
|
| 134 | - $extensions = $target->tbsCertificate()->extensions(); |
|
| 135 | - // find by authority key identifier |
|
| 136 | - if ($extensions->hasAuthorityKeyIdentifier()) { |
|
| 137 | - $ext = $extensions->authorityKeyIdentifier(); |
|
| 138 | - if ($ext->hasKeyIdentifier()) { |
|
| 139 | - foreach ($bundle->allBySubjectKeyIdentifier( |
|
| 140 | - $ext->keyIdentifier()) as $issuer) { |
|
| 141 | - // check that issuer name matches |
|
| 142 | - if ($issuer->tbsCertificate() |
|
| 143 | - ->subject() |
|
| 144 | - ->equals($issuer_name)) { |
|
| 145 | - $issuers[] = $issuer; |
|
| 146 | - } |
|
| 147 | - } |
|
| 148 | - } |
|
| 149 | - } |
|
| 150 | - return $issuers; |
|
| 151 | - } |
|
| 122 | + /** |
|
| 123 | + * Find all issuers of the target certificate from a given bundle. |
|
| 124 | + * |
|
| 125 | + * @param Certificate $target Target certificate |
|
| 126 | + * @param CertificateBundle $bundle Certificates to search |
|
| 127 | + * @return Certificate[] |
|
| 128 | + */ |
|
| 129 | + protected function _findIssuers(Certificate $target, |
|
| 130 | + CertificateBundle $bundle): array |
|
| 131 | + { |
|
| 132 | + $issuers = array(); |
|
| 133 | + $issuer_name = $target->tbsCertificate()->issuer(); |
|
| 134 | + $extensions = $target->tbsCertificate()->extensions(); |
|
| 135 | + // find by authority key identifier |
|
| 136 | + if ($extensions->hasAuthorityKeyIdentifier()) { |
|
| 137 | + $ext = $extensions->authorityKeyIdentifier(); |
|
| 138 | + if ($ext->hasKeyIdentifier()) { |
|
| 139 | + foreach ($bundle->allBySubjectKeyIdentifier( |
|
| 140 | + $ext->keyIdentifier()) as $issuer) { |
|
| 141 | + // check that issuer name matches |
|
| 142 | + if ($issuer->tbsCertificate() |
|
| 143 | + ->subject() |
|
| 144 | + ->equals($issuer_name)) { |
|
| 145 | + $issuers[] = $issuer; |
|
| 146 | + } |
|
| 147 | + } |
|
| 148 | + } |
|
| 149 | + } |
|
| 150 | + return $issuers; |
|
| 151 | + } |
|
| 152 | 152 | } |
@@ -273,6 +273,7 @@ |
||
| 273 | 273 | * Get self with extensions added. |
| 274 | 274 | * |
| 275 | 275 | * @param Extension ...$exts One or more Extension objects |
| 276 | + * @param Extension[] $exts |
|
| 276 | 277 | * @return self |
| 277 | 278 | */ |
| 278 | 279 | public function withAdditionalExtensions(Extension ...$exts): self |
@@ -22,435 +22,435 @@ |
||
| 22 | 22 | */ |
| 23 | 23 | class AttributeCertificateInfo |
| 24 | 24 | { |
| 25 | - const VERSION_2 = 1; |
|
| 26 | - |
|
| 27 | - /** |
|
| 28 | - * AC version. |
|
| 29 | - * |
|
| 30 | - * @var int $_version |
|
| 31 | - */ |
|
| 32 | - protected $_version; |
|
| 33 | - |
|
| 34 | - /** |
|
| 35 | - * AC holder. |
|
| 36 | - * |
|
| 37 | - * @var Holder $_holder |
|
| 38 | - */ |
|
| 39 | - protected $_holder; |
|
| 40 | - |
|
| 41 | - /** |
|
| 42 | - * AC issuer. |
|
| 43 | - * |
|
| 44 | - * @var AttCertIssuer $_issuer |
|
| 45 | - */ |
|
| 46 | - protected $_issuer; |
|
| 47 | - |
|
| 48 | - /** |
|
| 49 | - * Signature algorithm identifier. |
|
| 50 | - * |
|
| 51 | - * @var SignatureAlgorithmIdentifier $_signature |
|
| 52 | - */ |
|
| 53 | - protected $_signature; |
|
| 54 | - |
|
| 55 | - /** |
|
| 56 | - * AC serial number. |
|
| 57 | - * |
|
| 58 | - * @var string $_serialNumber |
|
| 59 | - */ |
|
| 60 | - protected $_serialNumber; |
|
| 61 | - |
|
| 62 | - /** |
|
| 63 | - * Validity period. |
|
| 64 | - * |
|
| 65 | - * @var AttCertValidityPeriod $_attrCertValidityPeriod |
|
| 66 | - */ |
|
| 67 | - protected $_attrCertValidityPeriod; |
|
| 68 | - |
|
| 69 | - /** |
|
| 70 | - * Attributes. |
|
| 71 | - * |
|
| 72 | - * @var Attributes $_attributes |
|
| 73 | - */ |
|
| 74 | - protected $_attributes; |
|
| 75 | - |
|
| 76 | - /** |
|
| 77 | - * Issuer unique identifier. |
|
| 78 | - * |
|
| 79 | - * @var UniqueIdentifier|null $_issuerUniqueID |
|
| 80 | - */ |
|
| 81 | - protected $_issuerUniqueID; |
|
| 82 | - |
|
| 83 | - /** |
|
| 84 | - * Extensions. |
|
| 85 | - * |
|
| 86 | - * @var Extensions $_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 | - * @throws \UnexpectedValueException |
|
| 114 | - * @return self |
|
| 115 | - */ |
|
| 116 | - public static function fromASN1(Sequence $seq): self |
|
| 117 | - { |
|
| 118 | - $version = $seq->at(0) |
|
| 119 | - ->asInteger() |
|
| 120 | - ->intNumber(); |
|
| 121 | - if ($version != self::VERSION_2) { |
|
| 122 | - throw new \UnexpectedValueException("Version must be 2."); |
|
| 123 | - } |
|
| 124 | - $holder = Holder::fromASN1($seq->at(1)->asSequence()); |
|
| 125 | - $issuer = AttCertIssuer::fromASN1($seq->at(2)); |
|
| 126 | - $signature = AlgorithmIdentifier::fromASN1($seq->at(3)->asSequence()); |
|
| 127 | - if (!$signature instanceof SignatureAlgorithmIdentifier) { |
|
| 128 | - throw new \UnexpectedValueException( |
|
| 129 | - "Unsupported signature algorithm " . $signature->oid() . "."); |
|
| 130 | - } |
|
| 131 | - $serial = $seq->at(4) |
|
| 132 | - ->asInteger() |
|
| 133 | - ->number(); |
|
| 134 | - $validity = AttCertValidityPeriod::fromASN1($seq->at(5)->asSequence()); |
|
| 135 | - $attribs = Attributes::fromASN1($seq->at(6)->asSequence()); |
|
| 136 | - $obj = new self($holder, $issuer, $validity, $attribs); |
|
| 137 | - $obj->_signature = $signature; |
|
| 138 | - $obj->_serialNumber = $serial; |
|
| 139 | - $idx = 7; |
|
| 140 | - if ($seq->has($idx, Element::TYPE_BIT_STRING)) { |
|
| 141 | - $obj->_issuerUniqueID = UniqueIdentifier::fromASN1( |
|
| 142 | - $seq->at($idx++)->asBitString()); |
|
| 143 | - } |
|
| 144 | - if ($seq->has($idx, Element::TYPE_SEQUENCE)) { |
|
| 145 | - $obj->_extensions = Extensions::fromASN1( |
|
| 146 | - $seq->at($idx++)->asSequence()); |
|
| 147 | - } |
|
| 148 | - return $obj; |
|
| 149 | - } |
|
| 150 | - |
|
| 151 | - /** |
|
| 152 | - * Get self with holder. |
|
| 153 | - * |
|
| 154 | - * @param Holder $holder |
|
| 155 | - * @return self |
|
| 156 | - */ |
|
| 157 | - public function withHolder(Holder $holder): self |
|
| 158 | - { |
|
| 159 | - $obj = clone $this; |
|
| 160 | - $obj->_holder = $holder; |
|
| 161 | - return $obj; |
|
| 162 | - } |
|
| 163 | - |
|
| 164 | - /** |
|
| 165 | - * Get self with issuer. |
|
| 166 | - * |
|
| 167 | - * @param AttCertIssuer $issuer |
|
| 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 | - * @return self |
|
| 182 | - */ |
|
| 183 | - public function withSignature(SignatureAlgorithmIdentifier $algo): self |
|
| 184 | - { |
|
| 185 | - $obj = clone $this; |
|
| 186 | - $obj->_signature = $algo; |
|
| 187 | - return $obj; |
|
| 188 | - } |
|
| 189 | - |
|
| 190 | - /** |
|
| 191 | - * Get self with serial number. |
|
| 192 | - * |
|
| 193 | - * @param int|string $serial |
|
| 194 | - * @return self |
|
| 195 | - */ |
|
| 196 | - public function withSerialNumber($serial): self |
|
| 197 | - { |
|
| 198 | - $obj = clone $this; |
|
| 199 | - $obj->_serialNumber = strval($serial); |
|
| 200 | - return $obj; |
|
| 201 | - } |
|
| 202 | - |
|
| 203 | - /** |
|
| 204 | - * Get self with random positive serial number. |
|
| 205 | - * |
|
| 206 | - * @param int $size Number of random bytes |
|
| 207 | - * @return self |
|
| 208 | - */ |
|
| 209 | - public function withRandomSerialNumber(int $size = 16): self |
|
| 210 | - { |
|
| 211 | - // ensure that first byte is always non-zero and having first bit unset |
|
| 212 | - $num = gmp_init(mt_rand(1, 0x7f), 10); |
|
| 213 | - for ($i = 1; $i < $size; ++$i) { |
|
| 214 | - $num <<= 8; |
|
| 215 | - $num += mt_rand(0, 0xff); |
|
| 216 | - } |
|
| 217 | - return $this->withSerialNumber(gmp_strval($num, 10)); |
|
| 218 | - } |
|
| 219 | - |
|
| 220 | - /** |
|
| 221 | - * Get self with validity period. |
|
| 222 | - * |
|
| 223 | - * @param AttCertValidityPeriod $validity |
|
| 224 | - * @return self |
|
| 225 | - */ |
|
| 226 | - public function withValidity(AttCertValidityPeriod $validity): self |
|
| 227 | - { |
|
| 228 | - $obj = clone $this; |
|
| 229 | - $obj->_attrCertValidityPeriod = $validity; |
|
| 230 | - return $obj; |
|
| 231 | - } |
|
| 232 | - |
|
| 233 | - /** |
|
| 234 | - * Get self with attributes. |
|
| 235 | - * |
|
| 236 | - * @param Attributes $attribs |
|
| 237 | - * @return self |
|
| 238 | - */ |
|
| 239 | - public function withAttributes(Attributes $attribs): self |
|
| 240 | - { |
|
| 241 | - $obj = clone $this; |
|
| 242 | - $obj->_attributes = $attribs; |
|
| 243 | - return $obj; |
|
| 244 | - } |
|
| 245 | - |
|
| 246 | - /** |
|
| 247 | - * Get self with issuer unique identifier. |
|
| 248 | - * |
|
| 249 | - * @param UniqueIdentifier $uid |
|
| 250 | - * @return self |
|
| 251 | - */ |
|
| 252 | - public function withIssuerUniqueID(UniqueIdentifier $uid): self |
|
| 253 | - { |
|
| 254 | - $obj = clone $this; |
|
| 255 | - $obj->_issuerUniqueID = $uid; |
|
| 256 | - return $obj; |
|
| 257 | - } |
|
| 258 | - |
|
| 259 | - /** |
|
| 260 | - * Get self with extensions. |
|
| 261 | - * |
|
| 262 | - * @param Extensions $extensions |
|
| 263 | - * @return self |
|
| 264 | - */ |
|
| 265 | - public function withExtensions(Extensions $extensions): self |
|
| 266 | - { |
|
| 267 | - $obj = clone $this; |
|
| 268 | - $obj->_extensions = $extensions; |
|
| 269 | - return $obj; |
|
| 270 | - } |
|
| 271 | - |
|
| 272 | - /** |
|
| 273 | - * Get self with extensions added. |
|
| 274 | - * |
|
| 275 | - * @param Extension ...$exts One or more Extension objects |
|
| 276 | - * @return self |
|
| 277 | - */ |
|
| 278 | - public function withAdditionalExtensions(Extension ...$exts): self |
|
| 279 | - { |
|
| 280 | - $obj = clone $this; |
|
| 281 | - $obj->_extensions = $obj->_extensions->withExtensions(...$exts); |
|
| 282 | - return $obj; |
|
| 283 | - } |
|
| 284 | - |
|
| 285 | - /** |
|
| 286 | - * Get version. |
|
| 287 | - * |
|
| 288 | - * @return int |
|
| 289 | - */ |
|
| 290 | - public function version(): int |
|
| 291 | - { |
|
| 292 | - return $this->_version; |
|
| 293 | - } |
|
| 294 | - |
|
| 295 | - /** |
|
| 296 | - * Get AC holder. |
|
| 297 | - * |
|
| 298 | - * @return Holder |
|
| 299 | - */ |
|
| 300 | - public function holder(): Holder |
|
| 301 | - { |
|
| 302 | - return $this->_holder; |
|
| 303 | - } |
|
| 304 | - |
|
| 305 | - /** |
|
| 306 | - * Get AC issuer. |
|
| 307 | - * |
|
| 308 | - * @return AttCertIssuer |
|
| 309 | - */ |
|
| 310 | - public function issuer(): AttCertIssuer |
|
| 311 | - { |
|
| 312 | - return $this->_issuer; |
|
| 313 | - } |
|
| 314 | - |
|
| 315 | - /** |
|
| 316 | - * Check whether signature is set. |
|
| 317 | - * |
|
| 318 | - * @return bool |
|
| 319 | - */ |
|
| 320 | - public function hasSignature(): bool |
|
| 321 | - { |
|
| 322 | - return isset($this->_signature); |
|
| 323 | - } |
|
| 324 | - |
|
| 325 | - /** |
|
| 326 | - * Get signature algorithm identifier. |
|
| 327 | - * |
|
| 328 | - * @return SignatureAlgorithmIdentifier |
|
| 329 | - */ |
|
| 330 | - public function signature(): SignatureAlgorithmIdentifier |
|
| 331 | - { |
|
| 332 | - if (!$this->hasSignature()) { |
|
| 333 | - throw new \LogicException("signature not set."); |
|
| 334 | - } |
|
| 335 | - return $this->_signature; |
|
| 336 | - } |
|
| 337 | - |
|
| 338 | - /** |
|
| 339 | - * Check whether serial number is present. |
|
| 340 | - * |
|
| 341 | - * @return bool |
|
| 342 | - */ |
|
| 343 | - public function hasSerialNumber(): bool |
|
| 344 | - { |
|
| 345 | - return isset($this->_serialNumber); |
|
| 346 | - } |
|
| 347 | - |
|
| 348 | - /** |
|
| 349 | - * Get AC serial number. |
|
| 350 | - * |
|
| 351 | - * @return string |
|
| 352 | - */ |
|
| 353 | - public function serialNumber(): string |
|
| 354 | - { |
|
| 355 | - if (!$this->hasSerialNumber()) { |
|
| 356 | - throw new \LogicException("serialNumber not set."); |
|
| 357 | - } |
|
| 358 | - return $this->_serialNumber; |
|
| 359 | - } |
|
| 360 | - |
|
| 361 | - /** |
|
| 362 | - * Get validity period. |
|
| 363 | - * |
|
| 364 | - * @return AttCertValidityPeriod |
|
| 365 | - */ |
|
| 366 | - public function validityPeriod(): AttCertValidityPeriod |
|
| 367 | - { |
|
| 368 | - return $this->_attrCertValidityPeriod; |
|
| 369 | - } |
|
| 370 | - |
|
| 371 | - /** |
|
| 372 | - * Get attributes. |
|
| 373 | - * |
|
| 374 | - * @return Attributes |
|
| 375 | - */ |
|
| 376 | - public function attributes(): Attributes |
|
| 377 | - { |
|
| 378 | - return $this->_attributes; |
|
| 379 | - } |
|
| 380 | - |
|
| 381 | - /** |
|
| 382 | - * Check whether issuer unique identifier is present. |
|
| 383 | - * |
|
| 384 | - * @return bool |
|
| 385 | - */ |
|
| 386 | - public function hasIssuerUniqueID(): bool |
|
| 387 | - { |
|
| 388 | - return isset($this->_issuerUniqueID); |
|
| 389 | - } |
|
| 390 | - |
|
| 391 | - /** |
|
| 392 | - * Get issuer unique identifier. |
|
| 393 | - * |
|
| 394 | - * @return UniqueIdentifier |
|
| 395 | - */ |
|
| 396 | - public function issuerUniqueID(): UniqueIdentifier |
|
| 397 | - { |
|
| 398 | - if (!$this->hasIssuerUniqueID()) { |
|
| 399 | - throw new \LogicException("issuerUniqueID not set."); |
|
| 400 | - } |
|
| 401 | - return $this->_issuerUniqueID; |
|
| 402 | - } |
|
| 403 | - |
|
| 404 | - /** |
|
| 405 | - * Get extensions. |
|
| 406 | - * |
|
| 407 | - * @return Extensions |
|
| 408 | - */ |
|
| 409 | - public function extensions(): Extensions |
|
| 410 | - { |
|
| 411 | - return $this->_extensions; |
|
| 412 | - } |
|
| 413 | - |
|
| 414 | - /** |
|
| 415 | - * Get ASN.1 structure. |
|
| 416 | - * |
|
| 417 | - * @return Sequence |
|
| 418 | - */ |
|
| 419 | - public function toASN1(): Sequence |
|
| 420 | - { |
|
| 421 | - $elements = array(new Integer($this->_version), $this->_holder->toASN1(), |
|
| 422 | - $this->_issuer->toASN1(), $this->signature()->toASN1(), |
|
| 423 | - new Integer($this->serialNumber()), |
|
| 424 | - $this->_attrCertValidityPeriod->toASN1(), |
|
| 425 | - $this->_attributes->toASN1()); |
|
| 426 | - if (isset($this->_issuerUniqueID)) { |
|
| 427 | - $elements[] = $this->_issuerUniqueID->toASN1(); |
|
| 428 | - } |
|
| 429 | - if (count($this->_extensions)) { |
|
| 430 | - $elements[] = $this->_extensions->toASN1(); |
|
| 431 | - } |
|
| 432 | - return new Sequence(...$elements); |
|
| 433 | - } |
|
| 434 | - |
|
| 435 | - /** |
|
| 436 | - * Create signed attribute certificate. |
|
| 437 | - * |
|
| 438 | - * @param SignatureAlgorithmIdentifier $algo Signature algorithm |
|
| 439 | - * @param PrivateKeyInfo $privkey_info Private key |
|
| 440 | - * @param Crypto|null $crypto Crypto engine, use default if not set |
|
| 441 | - * @return AttributeCertificate |
|
| 442 | - */ |
|
| 443 | - public function sign(SignatureAlgorithmIdentifier $algo, |
|
| 444 | - PrivateKeyInfo $privkey_info, Crypto $crypto = null): AttributeCertificate |
|
| 445 | - { |
|
| 446 | - $crypto = $crypto ?: Crypto::getDefault(); |
|
| 447 | - $aci = clone $this; |
|
| 448 | - if (!isset($aci->_serialNumber)) { |
|
| 449 | - $aci->_serialNumber = "0"; |
|
| 450 | - } |
|
| 451 | - $aci->_signature = $algo; |
|
| 452 | - $data = $aci->toASN1()->toDER(); |
|
| 453 | - $signature = $crypto->sign($data, $privkey_info, $algo); |
|
| 454 | - return new AttributeCertificate($aci, $algo, $signature); |
|
| 455 | - } |
|
| 25 | + const VERSION_2 = 1; |
|
| 26 | + |
|
| 27 | + /** |
|
| 28 | + * AC version. |
|
| 29 | + * |
|
| 30 | + * @var int $_version |
|
| 31 | + */ |
|
| 32 | + protected $_version; |
|
| 33 | + |
|
| 34 | + /** |
|
| 35 | + * AC holder. |
|
| 36 | + * |
|
| 37 | + * @var Holder $_holder |
|
| 38 | + */ |
|
| 39 | + protected $_holder; |
|
| 40 | + |
|
| 41 | + /** |
|
| 42 | + * AC issuer. |
|
| 43 | + * |
|
| 44 | + * @var AttCertIssuer $_issuer |
|
| 45 | + */ |
|
| 46 | + protected $_issuer; |
|
| 47 | + |
|
| 48 | + /** |
|
| 49 | + * Signature algorithm identifier. |
|
| 50 | + * |
|
| 51 | + * @var SignatureAlgorithmIdentifier $_signature |
|
| 52 | + */ |
|
| 53 | + protected $_signature; |
|
| 54 | + |
|
| 55 | + /** |
|
| 56 | + * AC serial number. |
|
| 57 | + * |
|
| 58 | + * @var string $_serialNumber |
|
| 59 | + */ |
|
| 60 | + protected $_serialNumber; |
|
| 61 | + |
|
| 62 | + /** |
|
| 63 | + * Validity period. |
|
| 64 | + * |
|
| 65 | + * @var AttCertValidityPeriod $_attrCertValidityPeriod |
|
| 66 | + */ |
|
| 67 | + protected $_attrCertValidityPeriod; |
|
| 68 | + |
|
| 69 | + /** |
|
| 70 | + * Attributes. |
|
| 71 | + * |
|
| 72 | + * @var Attributes $_attributes |
|
| 73 | + */ |
|
| 74 | + protected $_attributes; |
|
| 75 | + |
|
| 76 | + /** |
|
| 77 | + * Issuer unique identifier. |
|
| 78 | + * |
|
| 79 | + * @var UniqueIdentifier|null $_issuerUniqueID |
|
| 80 | + */ |
|
| 81 | + protected $_issuerUniqueID; |
|
| 82 | + |
|
| 83 | + /** |
|
| 84 | + * Extensions. |
|
| 85 | + * |
|
| 86 | + * @var Extensions $_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 | + * @throws \UnexpectedValueException |
|
| 114 | + * @return self |
|
| 115 | + */ |
|
| 116 | + public static function fromASN1(Sequence $seq): self |
|
| 117 | + { |
|
| 118 | + $version = $seq->at(0) |
|
| 119 | + ->asInteger() |
|
| 120 | + ->intNumber(); |
|
| 121 | + if ($version != self::VERSION_2) { |
|
| 122 | + throw new \UnexpectedValueException("Version must be 2."); |
|
| 123 | + } |
|
| 124 | + $holder = Holder::fromASN1($seq->at(1)->asSequence()); |
|
| 125 | + $issuer = AttCertIssuer::fromASN1($seq->at(2)); |
|
| 126 | + $signature = AlgorithmIdentifier::fromASN1($seq->at(3)->asSequence()); |
|
| 127 | + if (!$signature instanceof SignatureAlgorithmIdentifier) { |
|
| 128 | + throw new \UnexpectedValueException( |
|
| 129 | + "Unsupported signature algorithm " . $signature->oid() . "."); |
|
| 130 | + } |
|
| 131 | + $serial = $seq->at(4) |
|
| 132 | + ->asInteger() |
|
| 133 | + ->number(); |
|
| 134 | + $validity = AttCertValidityPeriod::fromASN1($seq->at(5)->asSequence()); |
|
| 135 | + $attribs = Attributes::fromASN1($seq->at(6)->asSequence()); |
|
| 136 | + $obj = new self($holder, $issuer, $validity, $attribs); |
|
| 137 | + $obj->_signature = $signature; |
|
| 138 | + $obj->_serialNumber = $serial; |
|
| 139 | + $idx = 7; |
|
| 140 | + if ($seq->has($idx, Element::TYPE_BIT_STRING)) { |
|
| 141 | + $obj->_issuerUniqueID = UniqueIdentifier::fromASN1( |
|
| 142 | + $seq->at($idx++)->asBitString()); |
|
| 143 | + } |
|
| 144 | + if ($seq->has($idx, Element::TYPE_SEQUENCE)) { |
|
| 145 | + $obj->_extensions = Extensions::fromASN1( |
|
| 146 | + $seq->at($idx++)->asSequence()); |
|
| 147 | + } |
|
| 148 | + return $obj; |
|
| 149 | + } |
|
| 150 | + |
|
| 151 | + /** |
|
| 152 | + * Get self with holder. |
|
| 153 | + * |
|
| 154 | + * @param Holder $holder |
|
| 155 | + * @return self |
|
| 156 | + */ |
|
| 157 | + public function withHolder(Holder $holder): self |
|
| 158 | + { |
|
| 159 | + $obj = clone $this; |
|
| 160 | + $obj->_holder = $holder; |
|
| 161 | + return $obj; |
|
| 162 | + } |
|
| 163 | + |
|
| 164 | + /** |
|
| 165 | + * Get self with issuer. |
|
| 166 | + * |
|
| 167 | + * @param AttCertIssuer $issuer |
|
| 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 | + * @return self |
|
| 182 | + */ |
|
| 183 | + public function withSignature(SignatureAlgorithmIdentifier $algo): self |
|
| 184 | + { |
|
| 185 | + $obj = clone $this; |
|
| 186 | + $obj->_signature = $algo; |
|
| 187 | + return $obj; |
|
| 188 | + } |
|
| 189 | + |
|
| 190 | + /** |
|
| 191 | + * Get self with serial number. |
|
| 192 | + * |
|
| 193 | + * @param int|string $serial |
|
| 194 | + * @return self |
|
| 195 | + */ |
|
| 196 | + public function withSerialNumber($serial): self |
|
| 197 | + { |
|
| 198 | + $obj = clone $this; |
|
| 199 | + $obj->_serialNumber = strval($serial); |
|
| 200 | + return $obj; |
|
| 201 | + } |
|
| 202 | + |
|
| 203 | + /** |
|
| 204 | + * Get self with random positive serial number. |
|
| 205 | + * |
|
| 206 | + * @param int $size Number of random bytes |
|
| 207 | + * @return self |
|
| 208 | + */ |
|
| 209 | + public function withRandomSerialNumber(int $size = 16): self |
|
| 210 | + { |
|
| 211 | + // ensure that first byte is always non-zero and having first bit unset |
|
| 212 | + $num = gmp_init(mt_rand(1, 0x7f), 10); |
|
| 213 | + for ($i = 1; $i < $size; ++$i) { |
|
| 214 | + $num <<= 8; |
|
| 215 | + $num += mt_rand(0, 0xff); |
|
| 216 | + } |
|
| 217 | + return $this->withSerialNumber(gmp_strval($num, 10)); |
|
| 218 | + } |
|
| 219 | + |
|
| 220 | + /** |
|
| 221 | + * Get self with validity period. |
|
| 222 | + * |
|
| 223 | + * @param AttCertValidityPeriod $validity |
|
| 224 | + * @return self |
|
| 225 | + */ |
|
| 226 | + public function withValidity(AttCertValidityPeriod $validity): self |
|
| 227 | + { |
|
| 228 | + $obj = clone $this; |
|
| 229 | + $obj->_attrCertValidityPeriod = $validity; |
|
| 230 | + return $obj; |
|
| 231 | + } |
|
| 232 | + |
|
| 233 | + /** |
|
| 234 | + * Get self with attributes. |
|
| 235 | + * |
|
| 236 | + * @param Attributes $attribs |
|
| 237 | + * @return self |
|
| 238 | + */ |
|
| 239 | + public function withAttributes(Attributes $attribs): self |
|
| 240 | + { |
|
| 241 | + $obj = clone $this; |
|
| 242 | + $obj->_attributes = $attribs; |
|
| 243 | + return $obj; |
|
| 244 | + } |
|
| 245 | + |
|
| 246 | + /** |
|
| 247 | + * Get self with issuer unique identifier. |
|
| 248 | + * |
|
| 249 | + * @param UniqueIdentifier $uid |
|
| 250 | + * @return self |
|
| 251 | + */ |
|
| 252 | + public function withIssuerUniqueID(UniqueIdentifier $uid): self |
|
| 253 | + { |
|
| 254 | + $obj = clone $this; |
|
| 255 | + $obj->_issuerUniqueID = $uid; |
|
| 256 | + return $obj; |
|
| 257 | + } |
|
| 258 | + |
|
| 259 | + /** |
|
| 260 | + * Get self with extensions. |
|
| 261 | + * |
|
| 262 | + * @param Extensions $extensions |
|
| 263 | + * @return self |
|
| 264 | + */ |
|
| 265 | + public function withExtensions(Extensions $extensions): self |
|
| 266 | + { |
|
| 267 | + $obj = clone $this; |
|
| 268 | + $obj->_extensions = $extensions; |
|
| 269 | + return $obj; |
|
| 270 | + } |
|
| 271 | + |
|
| 272 | + /** |
|
| 273 | + * Get self with extensions added. |
|
| 274 | + * |
|
| 275 | + * @param Extension ...$exts One or more Extension objects |
|
| 276 | + * @return self |
|
| 277 | + */ |
|
| 278 | + public function withAdditionalExtensions(Extension ...$exts): self |
|
| 279 | + { |
|
| 280 | + $obj = clone $this; |
|
| 281 | + $obj->_extensions = $obj->_extensions->withExtensions(...$exts); |
|
| 282 | + return $obj; |
|
| 283 | + } |
|
| 284 | + |
|
| 285 | + /** |
|
| 286 | + * Get version. |
|
| 287 | + * |
|
| 288 | + * @return int |
|
| 289 | + */ |
|
| 290 | + public function version(): int |
|
| 291 | + { |
|
| 292 | + return $this->_version; |
|
| 293 | + } |
|
| 294 | + |
|
| 295 | + /** |
|
| 296 | + * Get AC holder. |
|
| 297 | + * |
|
| 298 | + * @return Holder |
|
| 299 | + */ |
|
| 300 | + public function holder(): Holder |
|
| 301 | + { |
|
| 302 | + return $this->_holder; |
|
| 303 | + } |
|
| 304 | + |
|
| 305 | + /** |
|
| 306 | + * Get AC issuer. |
|
| 307 | + * |
|
| 308 | + * @return AttCertIssuer |
|
| 309 | + */ |
|
| 310 | + public function issuer(): AttCertIssuer |
|
| 311 | + { |
|
| 312 | + return $this->_issuer; |
|
| 313 | + } |
|
| 314 | + |
|
| 315 | + /** |
|
| 316 | + * Check whether signature is set. |
|
| 317 | + * |
|
| 318 | + * @return bool |
|
| 319 | + */ |
|
| 320 | + public function hasSignature(): bool |
|
| 321 | + { |
|
| 322 | + return isset($this->_signature); |
|
| 323 | + } |
|
| 324 | + |
|
| 325 | + /** |
|
| 326 | + * Get signature algorithm identifier. |
|
| 327 | + * |
|
| 328 | + * @return SignatureAlgorithmIdentifier |
|
| 329 | + */ |
|
| 330 | + public function signature(): SignatureAlgorithmIdentifier |
|
| 331 | + { |
|
| 332 | + if (!$this->hasSignature()) { |
|
| 333 | + throw new \LogicException("signature not set."); |
|
| 334 | + } |
|
| 335 | + return $this->_signature; |
|
| 336 | + } |
|
| 337 | + |
|
| 338 | + /** |
|
| 339 | + * Check whether serial number is present. |
|
| 340 | + * |
|
| 341 | + * @return bool |
|
| 342 | + */ |
|
| 343 | + public function hasSerialNumber(): bool |
|
| 344 | + { |
|
| 345 | + return isset($this->_serialNumber); |
|
| 346 | + } |
|
| 347 | + |
|
| 348 | + /** |
|
| 349 | + * Get AC serial number. |
|
| 350 | + * |
|
| 351 | + * @return string |
|
| 352 | + */ |
|
| 353 | + public function serialNumber(): string |
|
| 354 | + { |
|
| 355 | + if (!$this->hasSerialNumber()) { |
|
| 356 | + throw new \LogicException("serialNumber not set."); |
|
| 357 | + } |
|
| 358 | + return $this->_serialNumber; |
|
| 359 | + } |
|
| 360 | + |
|
| 361 | + /** |
|
| 362 | + * Get validity period. |
|
| 363 | + * |
|
| 364 | + * @return AttCertValidityPeriod |
|
| 365 | + */ |
|
| 366 | + public function validityPeriod(): AttCertValidityPeriod |
|
| 367 | + { |
|
| 368 | + return $this->_attrCertValidityPeriod; |
|
| 369 | + } |
|
| 370 | + |
|
| 371 | + /** |
|
| 372 | + * Get attributes. |
|
| 373 | + * |
|
| 374 | + * @return Attributes |
|
| 375 | + */ |
|
| 376 | + public function attributes(): Attributes |
|
| 377 | + { |
|
| 378 | + return $this->_attributes; |
|
| 379 | + } |
|
| 380 | + |
|
| 381 | + /** |
|
| 382 | + * Check whether issuer unique identifier is present. |
|
| 383 | + * |
|
| 384 | + * @return bool |
|
| 385 | + */ |
|
| 386 | + public function hasIssuerUniqueID(): bool |
|
| 387 | + { |
|
| 388 | + return isset($this->_issuerUniqueID); |
|
| 389 | + } |
|
| 390 | + |
|
| 391 | + /** |
|
| 392 | + * Get issuer unique identifier. |
|
| 393 | + * |
|
| 394 | + * @return UniqueIdentifier |
|
| 395 | + */ |
|
| 396 | + public function issuerUniqueID(): UniqueIdentifier |
|
| 397 | + { |
|
| 398 | + if (!$this->hasIssuerUniqueID()) { |
|
| 399 | + throw new \LogicException("issuerUniqueID not set."); |
|
| 400 | + } |
|
| 401 | + return $this->_issuerUniqueID; |
|
| 402 | + } |
|
| 403 | + |
|
| 404 | + /** |
|
| 405 | + * Get extensions. |
|
| 406 | + * |
|
| 407 | + * @return Extensions |
|
| 408 | + */ |
|
| 409 | + public function extensions(): Extensions |
|
| 410 | + { |
|
| 411 | + return $this->_extensions; |
|
| 412 | + } |
|
| 413 | + |
|
| 414 | + /** |
|
| 415 | + * Get ASN.1 structure. |
|
| 416 | + * |
|
| 417 | + * @return Sequence |
|
| 418 | + */ |
|
| 419 | + public function toASN1(): Sequence |
|
| 420 | + { |
|
| 421 | + $elements = array(new Integer($this->_version), $this->_holder->toASN1(), |
|
| 422 | + $this->_issuer->toASN1(), $this->signature()->toASN1(), |
|
| 423 | + new Integer($this->serialNumber()), |
|
| 424 | + $this->_attrCertValidityPeriod->toASN1(), |
|
| 425 | + $this->_attributes->toASN1()); |
|
| 426 | + if (isset($this->_issuerUniqueID)) { |
|
| 427 | + $elements[] = $this->_issuerUniqueID->toASN1(); |
|
| 428 | + } |
|
| 429 | + if (count($this->_extensions)) { |
|
| 430 | + $elements[] = $this->_extensions->toASN1(); |
|
| 431 | + } |
|
| 432 | + return new Sequence(...$elements); |
|
| 433 | + } |
|
| 434 | + |
|
| 435 | + /** |
|
| 436 | + * Create signed attribute certificate. |
|
| 437 | + * |
|
| 438 | + * @param SignatureAlgorithmIdentifier $algo Signature algorithm |
|
| 439 | + * @param PrivateKeyInfo $privkey_info Private key |
|
| 440 | + * @param Crypto|null $crypto Crypto engine, use default if not set |
|
| 441 | + * @return AttributeCertificate |
|
| 442 | + */ |
|
| 443 | + public function sign(SignatureAlgorithmIdentifier $algo, |
|
| 444 | + PrivateKeyInfo $privkey_info, Crypto $crypto = null): AttributeCertificate |
|
| 445 | + { |
|
| 446 | + $crypto = $crypto ?: Crypto::getDefault(); |
|
| 447 | + $aci = clone $this; |
|
| 448 | + if (!isset($aci->_serialNumber)) { |
|
| 449 | + $aci->_serialNumber = "0"; |
|
| 450 | + } |
|
| 451 | + $aci->_signature = $algo; |
|
| 452 | + $data = $aci->toASN1()->toDER(); |
|
| 453 | + $signature = $crypto->sign($data, $privkey_info, $algo); |
|
| 454 | + return new AttributeCertificate($aci, $algo, $signature); |
|
| 455 | + } |
|
| 456 | 456 | } |
@@ -383,6 +383,7 @@ |
||
| 383 | 383 | * Get self with extensions added. |
| 384 | 384 | * |
| 385 | 385 | * @param Extension ...$exts One or more Extension objects |
| 386 | + * @param Extension[] $exts |
|
| 386 | 387 | * @return self |
| 387 | 388 | */ |
| 388 | 389 | public function withAdditionalExtensions(Extension ...$exts): self |
@@ -27,610 +27,610 @@ |
||
| 27 | 27 | */ |
| 28 | 28 | class TBSCertificate |
| 29 | 29 | { |
| 30 | - // Certificate version enumerations |
|
| 31 | - const VERSION_1 = 0; |
|
| 32 | - const VERSION_2 = 1; |
|
| 33 | - const VERSION_3 = 2; |
|
| 34 | - |
|
| 35 | - /** |
|
| 36 | - * Certificate version. |
|
| 37 | - * |
|
| 38 | - * @var int|null |
|
| 39 | - */ |
|
| 40 | - protected $_version; |
|
| 41 | - |
|
| 42 | - /** |
|
| 43 | - * Serial number. |
|
| 44 | - * |
|
| 45 | - * @var string|null |
|
| 46 | - */ |
|
| 47 | - protected $_serialNumber; |
|
| 48 | - |
|
| 49 | - /** |
|
| 50 | - * Signature algorithm. |
|
| 51 | - * |
|
| 52 | - * @var SignatureAlgorithmIdentifier|null |
|
| 53 | - */ |
|
| 54 | - protected $_signature; |
|
| 55 | - |
|
| 56 | - /** |
|
| 57 | - * Certificate issuer. |
|
| 58 | - * |
|
| 59 | - * @var Name $_issuer |
|
| 60 | - */ |
|
| 61 | - protected $_issuer; |
|
| 62 | - |
|
| 63 | - /** |
|
| 64 | - * Certificate validity period. |
|
| 65 | - * |
|
| 66 | - * @var Validity $_validity |
|
| 67 | - */ |
|
| 68 | - protected $_validity; |
|
| 69 | - |
|
| 70 | - /** |
|
| 71 | - * Certificate subject. |
|
| 72 | - * |
|
| 73 | - * @var Name $_subject |
|
| 74 | - */ |
|
| 75 | - protected $_subject; |
|
| 76 | - |
|
| 77 | - /** |
|
| 78 | - * Subject public key. |
|
| 79 | - * |
|
| 80 | - * @var PublicKeyInfo $_subjectPublicKeyInfo |
|
| 81 | - */ |
|
| 82 | - protected $_subjectPublicKeyInfo; |
|
| 83 | - |
|
| 84 | - /** |
|
| 85 | - * Issuer unique identifier. |
|
| 86 | - * |
|
| 87 | - * @var UniqueIdentifier|null $_issuerUniqueID |
|
| 88 | - */ |
|
| 89 | - protected $_issuerUniqueID; |
|
| 90 | - |
|
| 91 | - /** |
|
| 92 | - * Subject unique identifier. |
|
| 93 | - * |
|
| 94 | - * @var UniqueIdentifier|null $_subjectUniqueID |
|
| 95 | - */ |
|
| 96 | - protected $_subjectUniqueID; |
|
| 97 | - |
|
| 98 | - /** |
|
| 99 | - * Extensions. |
|
| 100 | - * |
|
| 101 | - * @var Extensions $_extensions |
|
| 102 | - */ |
|
| 103 | - protected $_extensions; |
|
| 104 | - |
|
| 105 | - /** |
|
| 106 | - * Constructor. |
|
| 107 | - * |
|
| 108 | - * @param Name $subject Certificate subject |
|
| 109 | - * @param PublicKeyInfo $pki Subject public key |
|
| 110 | - * @param Name $issuer Certificate issuer |
|
| 111 | - * @param Validity $validity Validity period |
|
| 112 | - */ |
|
| 113 | - public function __construct(Name $subject, PublicKeyInfo $pki, Name $issuer, |
|
| 114 | - Validity $validity) |
|
| 115 | - { |
|
| 116 | - $this->_subject = $subject; |
|
| 117 | - $this->_subjectPublicKeyInfo = $pki; |
|
| 118 | - $this->_issuer = $issuer; |
|
| 119 | - $this->_validity = $validity; |
|
| 120 | - $this->_extensions = new Extensions(); |
|
| 121 | - } |
|
| 122 | - |
|
| 123 | - /** |
|
| 124 | - * Initialize from ASN.1. |
|
| 125 | - * |
|
| 126 | - * @param Sequence $seq |
|
| 127 | - * @return self |
|
| 128 | - */ |
|
| 129 | - public static function fromASN1(Sequence $seq): self |
|
| 130 | - { |
|
| 131 | - $idx = 0; |
|
| 132 | - if ($seq->hasTagged(0)) { |
|
| 133 | - $idx++; |
|
| 134 | - $version = $seq->getTagged(0) |
|
| 135 | - ->asExplicit() |
|
| 136 | - ->asInteger() |
|
| 137 | - ->intNumber(); |
|
| 138 | - } else { |
|
| 139 | - $version = self::VERSION_1; |
|
| 140 | - } |
|
| 141 | - $serial = $seq->at($idx++) |
|
| 142 | - ->asInteger() |
|
| 143 | - ->number(); |
|
| 144 | - $algo = AlgorithmIdentifier::fromASN1($seq->at($idx++)->asSequence()); |
|
| 145 | - if (!$algo instanceof SignatureAlgorithmIdentifier) { |
|
| 146 | - throw new \UnexpectedValueException( |
|
| 147 | - "Unsupported signature algorithm " . $algo->name() . "."); |
|
| 148 | - } |
|
| 149 | - $issuer = Name::fromASN1($seq->at($idx++)->asSequence()); |
|
| 150 | - $validity = Validity::fromASN1($seq->at($idx++)->asSequence()); |
|
| 151 | - $subject = Name::fromASN1($seq->at($idx++)->asSequence()); |
|
| 152 | - $pki = PublicKeyInfo::fromASN1($seq->at($idx++)->asSequence()); |
|
| 153 | - $tbs_cert = new self($subject, $pki, $issuer, $validity); |
|
| 154 | - $tbs_cert->_version = $version; |
|
| 155 | - $tbs_cert->_serialNumber = $serial; |
|
| 156 | - $tbs_cert->_signature = $algo; |
|
| 157 | - if ($seq->hasTagged(1)) { |
|
| 158 | - $tbs_cert->_issuerUniqueID = UniqueIdentifier::fromASN1( |
|
| 159 | - $seq->getTagged(1) |
|
| 160 | - ->asImplicit(Element::TYPE_BIT_STRING) |
|
| 161 | - ->asBitString()); |
|
| 162 | - } |
|
| 163 | - if ($seq->hasTagged(2)) { |
|
| 164 | - $tbs_cert->_subjectUniqueID = UniqueIdentifier::fromASN1( |
|
| 165 | - $seq->getTagged(2) |
|
| 166 | - ->asImplicit(Element::TYPE_BIT_STRING) |
|
| 167 | - ->asBitString()); |
|
| 168 | - } |
|
| 169 | - if ($seq->hasTagged(3)) { |
|
| 170 | - $tbs_cert->_extensions = Extensions::fromASN1( |
|
| 171 | - $seq->getTagged(3) |
|
| 172 | - ->asExplicit() |
|
| 173 | - ->asSequence()); |
|
| 174 | - } |
|
| 175 | - return $tbs_cert; |
|
| 176 | - } |
|
| 177 | - |
|
| 178 | - /** |
|
| 179 | - * Initialize from certification request. |
|
| 180 | - * |
|
| 181 | - * Note that signature is not verified and must be done by the caller. |
|
| 182 | - * |
|
| 183 | - * @param CertificationRequest $cr |
|
| 184 | - * @return self |
|
| 185 | - */ |
|
| 186 | - public static function fromCSR(CertificationRequest $cr): self |
|
| 187 | - { |
|
| 188 | - $cri = $cr->certificationRequestInfo(); |
|
| 189 | - $tbs_cert = new self($cri->subject(), $cri->subjectPKInfo(), new Name(), |
|
| 190 | - Validity::fromStrings(null, null)); |
|
| 191 | - // if CSR has Extension Request attribute |
|
| 192 | - if ($cri->hasAttributes()) { |
|
| 193 | - $attribs = $cri->attributes(); |
|
| 194 | - if ($attribs->hasExtensionRequest()) { |
|
| 195 | - $tbs_cert = $tbs_cert->withExtensions( |
|
| 196 | - $attribs->extensionRequest() |
|
| 197 | - ->extensions()); |
|
| 198 | - } |
|
| 199 | - } |
|
| 200 | - // add Subject Key Identifier extension |
|
| 201 | - $tbs_cert = $tbs_cert->withAdditionalExtensions( |
|
| 202 | - new SubjectKeyIdentifierExtension(false, |
|
| 203 | - $cri->subjectPKInfo() |
|
| 204 | - ->keyIdentifier())); |
|
| 205 | - return $tbs_cert; |
|
| 206 | - } |
|
| 207 | - |
|
| 208 | - /** |
|
| 209 | - * Get self with fields set from the issuer's certificate. |
|
| 210 | - * |
|
| 211 | - * Issuer shall be set to issuing certificate's subject. |
|
| 212 | - * Authority key identifier extensions shall be added with a key identifier |
|
| 213 | - * set to issuing certificate's public key identifier. |
|
| 214 | - * |
|
| 215 | - * @param Certificate $cert Issuing party's certificate |
|
| 216 | - * @return self |
|
| 217 | - */ |
|
| 218 | - public function withIssuerCertificate(Certificate $cert): self |
|
| 219 | - { |
|
| 220 | - $obj = clone $this; |
|
| 221 | - // set issuer DN from cert's subject |
|
| 222 | - $obj->_issuer = $cert->tbsCertificate()->subject(); |
|
| 223 | - // add authority key identifier extension |
|
| 224 | - $key_id = $cert->tbsCertificate() |
|
| 225 | - ->subjectPublicKeyInfo() |
|
| 226 | - ->keyIdentifier(); |
|
| 227 | - $obj->_extensions = $obj->_extensions->withExtensions( |
|
| 228 | - new AuthorityKeyIdentifierExtension(false, $key_id)); |
|
| 229 | - return $obj; |
|
| 230 | - } |
|
| 231 | - |
|
| 232 | - /** |
|
| 233 | - * Get self with given version. |
|
| 234 | - * |
|
| 235 | - * If version is not set, appropriate version is automatically |
|
| 236 | - * determined during signing. |
|
| 237 | - * |
|
| 238 | - * @param int $version |
|
| 239 | - * @return self |
|
| 240 | - */ |
|
| 241 | - public function withVersion(int $version): self |
|
| 242 | - { |
|
| 243 | - $obj = clone $this; |
|
| 244 | - $obj->_version = $version; |
|
| 245 | - return $obj; |
|
| 246 | - } |
|
| 247 | - |
|
| 248 | - /** |
|
| 249 | - * Get self with given serial number. |
|
| 250 | - * |
|
| 251 | - * @param int|string $serial Base 10 number |
|
| 252 | - * @return self |
|
| 253 | - */ |
|
| 254 | - public function withSerialNumber($serial): self |
|
| 255 | - { |
|
| 256 | - $obj = clone $this; |
|
| 257 | - $obj->_serialNumber = strval($serial); |
|
| 258 | - return $obj; |
|
| 259 | - } |
|
| 260 | - |
|
| 261 | - /** |
|
| 262 | - * Get self with random positive serial number. |
|
| 263 | - * |
|
| 264 | - * @param int $size Number of random bytes |
|
| 265 | - * @return self |
|
| 266 | - */ |
|
| 267 | - public function withRandomSerialNumber(int $size = 16): self |
|
| 268 | - { |
|
| 269 | - // ensure that first byte is always non-zero and having first bit unset |
|
| 270 | - $num = gmp_init(mt_rand(1, 0x7f), 10); |
|
| 271 | - for ($i = 1; $i < $size; ++$i) { |
|
| 272 | - $num <<= 8; |
|
| 273 | - $num += mt_rand(0, 0xff); |
|
| 274 | - } |
|
| 275 | - return $this->withSerialNumber(gmp_strval($num, 10)); |
|
| 276 | - } |
|
| 277 | - |
|
| 278 | - /** |
|
| 279 | - * Get self with given signature algorithm. |
|
| 280 | - * |
|
| 281 | - * @param SignatureAlgorithmIdentifier $algo |
|
| 282 | - * @return self |
|
| 283 | - */ |
|
| 284 | - public function withSignature(SignatureAlgorithmIdentifier $algo): self |
|
| 285 | - { |
|
| 286 | - $obj = clone $this; |
|
| 287 | - $obj->_signature = $algo; |
|
| 288 | - return $obj; |
|
| 289 | - } |
|
| 290 | - |
|
| 291 | - /** |
|
| 292 | - * Get self with given issuer. |
|
| 293 | - * |
|
| 294 | - * @param Name $issuer |
|
| 295 | - * @return self |
|
| 296 | - */ |
|
| 297 | - public function withIssuer(Name $issuer): self |
|
| 298 | - { |
|
| 299 | - $obj = clone $this; |
|
| 300 | - $obj->_issuer = $issuer; |
|
| 301 | - return $obj; |
|
| 302 | - } |
|
| 303 | - |
|
| 304 | - /** |
|
| 305 | - * Get self with given validity. |
|
| 306 | - * |
|
| 307 | - * @param Validity $validity |
|
| 308 | - * @return self |
|
| 309 | - */ |
|
| 310 | - public function withValidity(Validity $validity): self |
|
| 311 | - { |
|
| 312 | - $obj = clone $this; |
|
| 313 | - $obj->_validity = $validity; |
|
| 314 | - return $obj; |
|
| 315 | - } |
|
| 316 | - |
|
| 317 | - /** |
|
| 318 | - * Get self with given subject. |
|
| 319 | - * |
|
| 320 | - * @param Name $subject |
|
| 321 | - * @return self |
|
| 322 | - */ |
|
| 323 | - public function withSubject(Name $subject): self |
|
| 324 | - { |
|
| 325 | - $obj = clone $this; |
|
| 326 | - $obj->_subject = $subject; |
|
| 327 | - return $obj; |
|
| 328 | - } |
|
| 329 | - |
|
| 330 | - /** |
|
| 331 | - * Get self with given subject public key info. |
|
| 332 | - * |
|
| 333 | - * @param PublicKeyInfo $pub_key_info |
|
| 334 | - * @return self |
|
| 335 | - */ |
|
| 336 | - public function withSubjectPublicKeyInfo(PublicKeyInfo $pub_key_info): self |
|
| 337 | - { |
|
| 338 | - $obj = clone $this; |
|
| 339 | - $obj->_subjectPublicKeyInfo = $pub_key_info; |
|
| 340 | - return $obj; |
|
| 341 | - } |
|
| 342 | - |
|
| 343 | - /** |
|
| 344 | - * Get self with issuer unique ID. |
|
| 345 | - * |
|
| 346 | - * @param UniqueIdentifier $id |
|
| 347 | - * @return self |
|
| 348 | - */ |
|
| 349 | - public function withIssuerUniqueID(UniqueIdentifier $id): self |
|
| 350 | - { |
|
| 351 | - $obj = clone $this; |
|
| 352 | - $obj->_issuerUniqueID = $id; |
|
| 353 | - return $obj; |
|
| 354 | - } |
|
| 355 | - |
|
| 356 | - /** |
|
| 357 | - * Get self with subject unique ID. |
|
| 358 | - * |
|
| 359 | - * @param UniqueIdentifier $id |
|
| 360 | - * @return self |
|
| 361 | - */ |
|
| 362 | - public function withSubjectUniqueID(UniqueIdentifier $id): self |
|
| 363 | - { |
|
| 364 | - $obj = clone $this; |
|
| 365 | - $obj->_subjectUniqueID = $id; |
|
| 366 | - return $obj; |
|
| 367 | - } |
|
| 368 | - |
|
| 369 | - /** |
|
| 370 | - * Get self with given extensions. |
|
| 371 | - * |
|
| 372 | - * @param Extensions $extensions |
|
| 373 | - * @return self |
|
| 374 | - */ |
|
| 375 | - public function withExtensions(Extensions $extensions): self |
|
| 376 | - { |
|
| 377 | - $obj = clone $this; |
|
| 378 | - $obj->_extensions = $extensions; |
|
| 379 | - return $obj; |
|
| 380 | - } |
|
| 381 | - |
|
| 382 | - /** |
|
| 383 | - * Get self with extensions added. |
|
| 384 | - * |
|
| 385 | - * @param Extension ...$exts One or more Extension objects |
|
| 386 | - * @return self |
|
| 387 | - */ |
|
| 388 | - public function withAdditionalExtensions(Extension ...$exts): self |
|
| 389 | - { |
|
| 390 | - $obj = clone $this; |
|
| 391 | - $obj->_extensions = $obj->_extensions->withExtensions(...$exts); |
|
| 392 | - return $obj; |
|
| 393 | - } |
|
| 394 | - |
|
| 395 | - /** |
|
| 396 | - * Check whether version is set. |
|
| 397 | - * |
|
| 398 | - * @return bool |
|
| 399 | - */ |
|
| 400 | - public function hasVersion(): bool |
|
| 401 | - { |
|
| 402 | - return isset($this->_version); |
|
| 403 | - } |
|
| 404 | - |
|
| 405 | - /** |
|
| 406 | - * Get certificate version. |
|
| 407 | - * |
|
| 408 | - * @return int |
|
| 409 | - */ |
|
| 410 | - public function version(): int |
|
| 411 | - { |
|
| 412 | - if (!$this->hasVersion()) { |
|
| 413 | - throw new \LogicException("version not set."); |
|
| 414 | - } |
|
| 415 | - return $this->_version; |
|
| 416 | - } |
|
| 417 | - |
|
| 418 | - /** |
|
| 419 | - * Check whether serial number is set. |
|
| 420 | - * |
|
| 421 | - * @return bool |
|
| 422 | - */ |
|
| 423 | - public function hasSerialNumber(): bool |
|
| 424 | - { |
|
| 425 | - return isset($this->_serialNumber); |
|
| 426 | - } |
|
| 427 | - |
|
| 428 | - /** |
|
| 429 | - * Get serial number. |
|
| 430 | - * |
|
| 431 | - * @return string Base 10 integer |
|
| 432 | - */ |
|
| 433 | - public function serialNumber(): string |
|
| 434 | - { |
|
| 435 | - if (!$this->hasSerialNumber()) { |
|
| 436 | - throw new \LogicException("serialNumber not set."); |
|
| 437 | - } |
|
| 438 | - return $this->_serialNumber; |
|
| 439 | - } |
|
| 440 | - |
|
| 441 | - /** |
|
| 442 | - * Check whether signature algorithm is set. |
|
| 443 | - * |
|
| 444 | - * @return bool |
|
| 445 | - */ |
|
| 446 | - public function hasSignature(): bool |
|
| 447 | - { |
|
| 448 | - return isset($this->_signature); |
|
| 449 | - } |
|
| 450 | - |
|
| 451 | - /** |
|
| 452 | - * Get signature algorithm. |
|
| 453 | - * |
|
| 454 | - * @return SignatureAlgorithmIdentifier |
|
| 455 | - */ |
|
| 456 | - public function signature(): SignatureAlgorithmIdentifier |
|
| 457 | - { |
|
| 458 | - if (!$this->hasSignature()) { |
|
| 459 | - throw new \LogicException("signature not set."); |
|
| 460 | - } |
|
| 461 | - return $this->_signature; |
|
| 462 | - } |
|
| 463 | - |
|
| 464 | - /** |
|
| 465 | - * Get issuer. |
|
| 466 | - * |
|
| 467 | - * @return Name |
|
| 468 | - */ |
|
| 469 | - public function issuer(): Name |
|
| 470 | - { |
|
| 471 | - return $this->_issuer; |
|
| 472 | - } |
|
| 473 | - |
|
| 474 | - /** |
|
| 475 | - * Get validity period. |
|
| 476 | - * |
|
| 477 | - * @return Validity |
|
| 478 | - */ |
|
| 479 | - public function validity(): Validity |
|
| 480 | - { |
|
| 481 | - return $this->_validity; |
|
| 482 | - } |
|
| 483 | - |
|
| 484 | - /** |
|
| 485 | - * Get subject. |
|
| 486 | - * |
|
| 487 | - * @return Name |
|
| 488 | - */ |
|
| 489 | - public function subject(): Name |
|
| 490 | - { |
|
| 491 | - return $this->_subject; |
|
| 492 | - } |
|
| 493 | - |
|
| 494 | - /** |
|
| 495 | - * Get subject public key. |
|
| 496 | - * |
|
| 497 | - * @return PublicKeyInfo |
|
| 498 | - */ |
|
| 499 | - public function subjectPublicKeyInfo(): PublicKeyInfo |
|
| 500 | - { |
|
| 501 | - return $this->_subjectPublicKeyInfo; |
|
| 502 | - } |
|
| 503 | - |
|
| 504 | - /** |
|
| 505 | - * Whether issuer unique identifier is present. |
|
| 506 | - * |
|
| 507 | - * @return bool |
|
| 508 | - */ |
|
| 509 | - public function hasIssuerUniqueID(): bool |
|
| 510 | - { |
|
| 511 | - return isset($this->_issuerUniqueID); |
|
| 512 | - } |
|
| 513 | - |
|
| 514 | - /** |
|
| 515 | - * Get issuerUniqueID. |
|
| 516 | - * |
|
| 517 | - * @return UniqueIdentifier |
|
| 518 | - */ |
|
| 519 | - public function issuerUniqueID(): UniqueIdentifier |
|
| 520 | - { |
|
| 521 | - if (!$this->hasIssuerUniqueID()) { |
|
| 522 | - throw new \LogicException("issuerUniqueID not set."); |
|
| 523 | - } |
|
| 524 | - return $this->_issuerUniqueID; |
|
| 525 | - } |
|
| 526 | - |
|
| 527 | - /** |
|
| 528 | - * Whether subject unique identifier is present. |
|
| 529 | - * |
|
| 530 | - * @return bool |
|
| 531 | - */ |
|
| 532 | - public function hasSubjectUniqueID(): bool |
|
| 533 | - { |
|
| 534 | - return isset($this->_subjectUniqueID); |
|
| 535 | - } |
|
| 536 | - |
|
| 537 | - /** |
|
| 538 | - * Get subjectUniqueID. |
|
| 539 | - * |
|
| 540 | - * @return UniqueIdentifier |
|
| 541 | - */ |
|
| 542 | - public function subjectUniqueID(): UniqueIdentifier |
|
| 543 | - { |
|
| 544 | - if (!$this->hasSubjectUniqueID()) { |
|
| 545 | - throw new \LogicException("subjectUniqueID not set."); |
|
| 546 | - } |
|
| 547 | - return $this->_subjectUniqueID; |
|
| 548 | - } |
|
| 549 | - |
|
| 550 | - /** |
|
| 551 | - * Get extensions. |
|
| 552 | - * |
|
| 553 | - * @return Extensions |
|
| 554 | - */ |
|
| 555 | - public function extensions(): Extensions |
|
| 556 | - { |
|
| 557 | - return $this->_extensions; |
|
| 558 | - } |
|
| 559 | - |
|
| 560 | - /** |
|
| 561 | - * Generate ASN.1 structure. |
|
| 562 | - * |
|
| 563 | - * @return Sequence |
|
| 564 | - */ |
|
| 565 | - public function toASN1(): Sequence |
|
| 566 | - { |
|
| 567 | - $elements = array(); |
|
| 568 | - $version = $this->version(); |
|
| 569 | - // if version is not default |
|
| 570 | - if ($version != self::VERSION_1) { |
|
| 571 | - $elements[] = new ExplicitlyTaggedType(0, new Integer($version)); |
|
| 572 | - } |
|
| 573 | - $serial = $this->serialNumber(); |
|
| 574 | - $signature = $this->signature(); |
|
| 575 | - // add required elements |
|
| 576 | - array_push($elements, new Integer($serial), $signature->toASN1(), |
|
| 577 | - $this->_issuer->toASN1(), $this->_validity->toASN1(), |
|
| 578 | - $this->_subject->toASN1(), $this->_subjectPublicKeyInfo->toASN1()); |
|
| 579 | - if (isset($this->_issuerUniqueID)) { |
|
| 580 | - $elements[] = new ImplicitlyTaggedType(1, |
|
| 581 | - $this->_issuerUniqueID->toASN1()); |
|
| 582 | - } |
|
| 583 | - if (isset($this->_subjectUniqueID)) { |
|
| 584 | - $elements[] = new ImplicitlyTaggedType(2, |
|
| 585 | - $this->_subjectUniqueID->toASN1()); |
|
| 586 | - } |
|
| 587 | - if (count($this->_extensions)) { |
|
| 588 | - $elements[] = new ExplicitlyTaggedType(3, |
|
| 589 | - $this->_extensions->toASN1()); |
|
| 590 | - } |
|
| 591 | - return new Sequence(...$elements); |
|
| 592 | - } |
|
| 593 | - |
|
| 594 | - /** |
|
| 595 | - * Create signed certificate. |
|
| 596 | - * |
|
| 597 | - * @param SignatureAlgorithmIdentifier $algo Algorithm used for signing |
|
| 598 | - * @param PrivateKeyInfo $privkey_info Private key used for signing |
|
| 599 | - * @param Crypto|null $crypto Crypto engine, use default if not set |
|
| 600 | - * @return Certificate |
|
| 601 | - */ |
|
| 602 | - public function sign(SignatureAlgorithmIdentifier $algo, |
|
| 603 | - PrivateKeyInfo $privkey_info, Crypto $crypto = null): Certificate |
|
| 604 | - { |
|
| 605 | - $crypto = $crypto ?: Crypto::getDefault(); |
|
| 606 | - $tbs_cert = clone $this; |
|
| 607 | - if (!isset($tbs_cert->_version)) { |
|
| 608 | - $tbs_cert->_version = $tbs_cert->_determineVersion(); |
|
| 609 | - } |
|
| 610 | - if (!isset($tbs_cert->_serialNumber)) { |
|
| 611 | - $tbs_cert->_serialNumber = strval(0); |
|
| 612 | - } |
|
| 613 | - $tbs_cert->_signature = $algo; |
|
| 614 | - $data = $tbs_cert->toASN1()->toDER(); |
|
| 615 | - $signature = $crypto->sign($data, $privkey_info, $algo); |
|
| 616 | - return new Certificate($tbs_cert, $algo, $signature); |
|
| 617 | - } |
|
| 618 | - |
|
| 619 | - /** |
|
| 620 | - * Determine minimum version for the certificate. |
|
| 621 | - * |
|
| 622 | - * @return int |
|
| 623 | - */ |
|
| 624 | - protected function _determineVersion(): int |
|
| 625 | - { |
|
| 626 | - // if extensions are present |
|
| 627 | - if (count($this->_extensions)) { |
|
| 628 | - return self::VERSION_3; |
|
| 629 | - } |
|
| 630 | - // if UniqueIdentifier is present |
|
| 631 | - if (isset($this->_issuerUniqueID) || isset($this->_subjectUniqueID)) { |
|
| 632 | - return self::VERSION_2; |
|
| 633 | - } |
|
| 634 | - return self::VERSION_1; |
|
| 635 | - } |
|
| 30 | + // Certificate version enumerations |
|
| 31 | + const VERSION_1 = 0; |
|
| 32 | + const VERSION_2 = 1; |
|
| 33 | + const VERSION_3 = 2; |
|
| 34 | + |
|
| 35 | + /** |
|
| 36 | + * Certificate version. |
|
| 37 | + * |
|
| 38 | + * @var int|null |
|
| 39 | + */ |
|
| 40 | + protected $_version; |
|
| 41 | + |
|
| 42 | + /** |
|
| 43 | + * Serial number. |
|
| 44 | + * |
|
| 45 | + * @var string|null |
|
| 46 | + */ |
|
| 47 | + protected $_serialNumber; |
|
| 48 | + |
|
| 49 | + /** |
|
| 50 | + * Signature algorithm. |
|
| 51 | + * |
|
| 52 | + * @var SignatureAlgorithmIdentifier|null |
|
| 53 | + */ |
|
| 54 | + protected $_signature; |
|
| 55 | + |
|
| 56 | + /** |
|
| 57 | + * Certificate issuer. |
|
| 58 | + * |
|
| 59 | + * @var Name $_issuer |
|
| 60 | + */ |
|
| 61 | + protected $_issuer; |
|
| 62 | + |
|
| 63 | + /** |
|
| 64 | + * Certificate validity period. |
|
| 65 | + * |
|
| 66 | + * @var Validity $_validity |
|
| 67 | + */ |
|
| 68 | + protected $_validity; |
|
| 69 | + |
|
| 70 | + /** |
|
| 71 | + * Certificate subject. |
|
| 72 | + * |
|
| 73 | + * @var Name $_subject |
|
| 74 | + */ |
|
| 75 | + protected $_subject; |
|
| 76 | + |
|
| 77 | + /** |
|
| 78 | + * Subject public key. |
|
| 79 | + * |
|
| 80 | + * @var PublicKeyInfo $_subjectPublicKeyInfo |
|
| 81 | + */ |
|
| 82 | + protected $_subjectPublicKeyInfo; |
|
| 83 | + |
|
| 84 | + /** |
|
| 85 | + * Issuer unique identifier. |
|
| 86 | + * |
|
| 87 | + * @var UniqueIdentifier|null $_issuerUniqueID |
|
| 88 | + */ |
|
| 89 | + protected $_issuerUniqueID; |
|
| 90 | + |
|
| 91 | + /** |
|
| 92 | + * Subject unique identifier. |
|
| 93 | + * |
|
| 94 | + * @var UniqueIdentifier|null $_subjectUniqueID |
|
| 95 | + */ |
|
| 96 | + protected $_subjectUniqueID; |
|
| 97 | + |
|
| 98 | + /** |
|
| 99 | + * Extensions. |
|
| 100 | + * |
|
| 101 | + * @var Extensions $_extensions |
|
| 102 | + */ |
|
| 103 | + protected $_extensions; |
|
| 104 | + |
|
| 105 | + /** |
|
| 106 | + * Constructor. |
|
| 107 | + * |
|
| 108 | + * @param Name $subject Certificate subject |
|
| 109 | + * @param PublicKeyInfo $pki Subject public key |
|
| 110 | + * @param Name $issuer Certificate issuer |
|
| 111 | + * @param Validity $validity Validity period |
|
| 112 | + */ |
|
| 113 | + public function __construct(Name $subject, PublicKeyInfo $pki, Name $issuer, |
|
| 114 | + Validity $validity) |
|
| 115 | + { |
|
| 116 | + $this->_subject = $subject; |
|
| 117 | + $this->_subjectPublicKeyInfo = $pki; |
|
| 118 | + $this->_issuer = $issuer; |
|
| 119 | + $this->_validity = $validity; |
|
| 120 | + $this->_extensions = new Extensions(); |
|
| 121 | + } |
|
| 122 | + |
|
| 123 | + /** |
|
| 124 | + * Initialize from ASN.1. |
|
| 125 | + * |
|
| 126 | + * @param Sequence $seq |
|
| 127 | + * @return self |
|
| 128 | + */ |
|
| 129 | + public static function fromASN1(Sequence $seq): self |
|
| 130 | + { |
|
| 131 | + $idx = 0; |
|
| 132 | + if ($seq->hasTagged(0)) { |
|
| 133 | + $idx++; |
|
| 134 | + $version = $seq->getTagged(0) |
|
| 135 | + ->asExplicit() |
|
| 136 | + ->asInteger() |
|
| 137 | + ->intNumber(); |
|
| 138 | + } else { |
|
| 139 | + $version = self::VERSION_1; |
|
| 140 | + } |
|
| 141 | + $serial = $seq->at($idx++) |
|
| 142 | + ->asInteger() |
|
| 143 | + ->number(); |
|
| 144 | + $algo = AlgorithmIdentifier::fromASN1($seq->at($idx++)->asSequence()); |
|
| 145 | + if (!$algo instanceof SignatureAlgorithmIdentifier) { |
|
| 146 | + throw new \UnexpectedValueException( |
|
| 147 | + "Unsupported signature algorithm " . $algo->name() . "."); |
|
| 148 | + } |
|
| 149 | + $issuer = Name::fromASN1($seq->at($idx++)->asSequence()); |
|
| 150 | + $validity = Validity::fromASN1($seq->at($idx++)->asSequence()); |
|
| 151 | + $subject = Name::fromASN1($seq->at($idx++)->asSequence()); |
|
| 152 | + $pki = PublicKeyInfo::fromASN1($seq->at($idx++)->asSequence()); |
|
| 153 | + $tbs_cert = new self($subject, $pki, $issuer, $validity); |
|
| 154 | + $tbs_cert->_version = $version; |
|
| 155 | + $tbs_cert->_serialNumber = $serial; |
|
| 156 | + $tbs_cert->_signature = $algo; |
|
| 157 | + if ($seq->hasTagged(1)) { |
|
| 158 | + $tbs_cert->_issuerUniqueID = UniqueIdentifier::fromASN1( |
|
| 159 | + $seq->getTagged(1) |
|
| 160 | + ->asImplicit(Element::TYPE_BIT_STRING) |
|
| 161 | + ->asBitString()); |
|
| 162 | + } |
|
| 163 | + if ($seq->hasTagged(2)) { |
|
| 164 | + $tbs_cert->_subjectUniqueID = UniqueIdentifier::fromASN1( |
|
| 165 | + $seq->getTagged(2) |
|
| 166 | + ->asImplicit(Element::TYPE_BIT_STRING) |
|
| 167 | + ->asBitString()); |
|
| 168 | + } |
|
| 169 | + if ($seq->hasTagged(3)) { |
|
| 170 | + $tbs_cert->_extensions = Extensions::fromASN1( |
|
| 171 | + $seq->getTagged(3) |
|
| 172 | + ->asExplicit() |
|
| 173 | + ->asSequence()); |
|
| 174 | + } |
|
| 175 | + return $tbs_cert; |
|
| 176 | + } |
|
| 177 | + |
|
| 178 | + /** |
|
| 179 | + * Initialize from certification request. |
|
| 180 | + * |
|
| 181 | + * Note that signature is not verified and must be done by the caller. |
|
| 182 | + * |
|
| 183 | + * @param CertificationRequest $cr |
|
| 184 | + * @return self |
|
| 185 | + */ |
|
| 186 | + public static function fromCSR(CertificationRequest $cr): self |
|
| 187 | + { |
|
| 188 | + $cri = $cr->certificationRequestInfo(); |
|
| 189 | + $tbs_cert = new self($cri->subject(), $cri->subjectPKInfo(), new Name(), |
|
| 190 | + Validity::fromStrings(null, null)); |
|
| 191 | + // if CSR has Extension Request attribute |
|
| 192 | + if ($cri->hasAttributes()) { |
|
| 193 | + $attribs = $cri->attributes(); |
|
| 194 | + if ($attribs->hasExtensionRequest()) { |
|
| 195 | + $tbs_cert = $tbs_cert->withExtensions( |
|
| 196 | + $attribs->extensionRequest() |
|
| 197 | + ->extensions()); |
|
| 198 | + } |
|
| 199 | + } |
|
| 200 | + // add Subject Key Identifier extension |
|
| 201 | + $tbs_cert = $tbs_cert->withAdditionalExtensions( |
|
| 202 | + new SubjectKeyIdentifierExtension(false, |
|
| 203 | + $cri->subjectPKInfo() |
|
| 204 | + ->keyIdentifier())); |
|
| 205 | + return $tbs_cert; |
|
| 206 | + } |
|
| 207 | + |
|
| 208 | + /** |
|
| 209 | + * Get self with fields set from the issuer's certificate. |
|
| 210 | + * |
|
| 211 | + * Issuer shall be set to issuing certificate's subject. |
|
| 212 | + * Authority key identifier extensions shall be added with a key identifier |
|
| 213 | + * set to issuing certificate's public key identifier. |
|
| 214 | + * |
|
| 215 | + * @param Certificate $cert Issuing party's certificate |
|
| 216 | + * @return self |
|
| 217 | + */ |
|
| 218 | + public function withIssuerCertificate(Certificate $cert): self |
|
| 219 | + { |
|
| 220 | + $obj = clone $this; |
|
| 221 | + // set issuer DN from cert's subject |
|
| 222 | + $obj->_issuer = $cert->tbsCertificate()->subject(); |
|
| 223 | + // add authority key identifier extension |
|
| 224 | + $key_id = $cert->tbsCertificate() |
|
| 225 | + ->subjectPublicKeyInfo() |
|
| 226 | + ->keyIdentifier(); |
|
| 227 | + $obj->_extensions = $obj->_extensions->withExtensions( |
|
| 228 | + new AuthorityKeyIdentifierExtension(false, $key_id)); |
|
| 229 | + return $obj; |
|
| 230 | + } |
|
| 231 | + |
|
| 232 | + /** |
|
| 233 | + * Get self with given version. |
|
| 234 | + * |
|
| 235 | + * If version is not set, appropriate version is automatically |
|
| 236 | + * determined during signing. |
|
| 237 | + * |
|
| 238 | + * @param int $version |
|
| 239 | + * @return self |
|
| 240 | + */ |
|
| 241 | + public function withVersion(int $version): self |
|
| 242 | + { |
|
| 243 | + $obj = clone $this; |
|
| 244 | + $obj->_version = $version; |
|
| 245 | + return $obj; |
|
| 246 | + } |
|
| 247 | + |
|
| 248 | + /** |
|
| 249 | + * Get self with given serial number. |
|
| 250 | + * |
|
| 251 | + * @param int|string $serial Base 10 number |
|
| 252 | + * @return self |
|
| 253 | + */ |
|
| 254 | + public function withSerialNumber($serial): self |
|
| 255 | + { |
|
| 256 | + $obj = clone $this; |
|
| 257 | + $obj->_serialNumber = strval($serial); |
|
| 258 | + return $obj; |
|
| 259 | + } |
|
| 260 | + |
|
| 261 | + /** |
|
| 262 | + * Get self with random positive serial number. |
|
| 263 | + * |
|
| 264 | + * @param int $size Number of random bytes |
|
| 265 | + * @return self |
|
| 266 | + */ |
|
| 267 | + public function withRandomSerialNumber(int $size = 16): self |
|
| 268 | + { |
|
| 269 | + // ensure that first byte is always non-zero and having first bit unset |
|
| 270 | + $num = gmp_init(mt_rand(1, 0x7f), 10); |
|
| 271 | + for ($i = 1; $i < $size; ++$i) { |
|
| 272 | + $num <<= 8; |
|
| 273 | + $num += mt_rand(0, 0xff); |
|
| 274 | + } |
|
| 275 | + return $this->withSerialNumber(gmp_strval($num, 10)); |
|
| 276 | + } |
|
| 277 | + |
|
| 278 | + /** |
|
| 279 | + * Get self with given signature algorithm. |
|
| 280 | + * |
|
| 281 | + * @param SignatureAlgorithmIdentifier $algo |
|
| 282 | + * @return self |
|
| 283 | + */ |
|
| 284 | + public function withSignature(SignatureAlgorithmIdentifier $algo): self |
|
| 285 | + { |
|
| 286 | + $obj = clone $this; |
|
| 287 | + $obj->_signature = $algo; |
|
| 288 | + return $obj; |
|
| 289 | + } |
|
| 290 | + |
|
| 291 | + /** |
|
| 292 | + * Get self with given issuer. |
|
| 293 | + * |
|
| 294 | + * @param Name $issuer |
|
| 295 | + * @return self |
|
| 296 | + */ |
|
| 297 | + public function withIssuer(Name $issuer): self |
|
| 298 | + { |
|
| 299 | + $obj = clone $this; |
|
| 300 | + $obj->_issuer = $issuer; |
|
| 301 | + return $obj; |
|
| 302 | + } |
|
| 303 | + |
|
| 304 | + /** |
|
| 305 | + * Get self with given validity. |
|
| 306 | + * |
|
| 307 | + * @param Validity $validity |
|
| 308 | + * @return self |
|
| 309 | + */ |
|
| 310 | + public function withValidity(Validity $validity): self |
|
| 311 | + { |
|
| 312 | + $obj = clone $this; |
|
| 313 | + $obj->_validity = $validity; |
|
| 314 | + return $obj; |
|
| 315 | + } |
|
| 316 | + |
|
| 317 | + /** |
|
| 318 | + * Get self with given subject. |
|
| 319 | + * |
|
| 320 | + * @param Name $subject |
|
| 321 | + * @return self |
|
| 322 | + */ |
|
| 323 | + public function withSubject(Name $subject): self |
|
| 324 | + { |
|
| 325 | + $obj = clone $this; |
|
| 326 | + $obj->_subject = $subject; |
|
| 327 | + return $obj; |
|
| 328 | + } |
|
| 329 | + |
|
| 330 | + /** |
|
| 331 | + * Get self with given subject public key info. |
|
| 332 | + * |
|
| 333 | + * @param PublicKeyInfo $pub_key_info |
|
| 334 | + * @return self |
|
| 335 | + */ |
|
| 336 | + public function withSubjectPublicKeyInfo(PublicKeyInfo $pub_key_info): self |
|
| 337 | + { |
|
| 338 | + $obj = clone $this; |
|
| 339 | + $obj->_subjectPublicKeyInfo = $pub_key_info; |
|
| 340 | + return $obj; |
|
| 341 | + } |
|
| 342 | + |
|
| 343 | + /** |
|
| 344 | + * Get self with issuer unique ID. |
|
| 345 | + * |
|
| 346 | + * @param UniqueIdentifier $id |
|
| 347 | + * @return self |
|
| 348 | + */ |
|
| 349 | + public function withIssuerUniqueID(UniqueIdentifier $id): self |
|
| 350 | + { |
|
| 351 | + $obj = clone $this; |
|
| 352 | + $obj->_issuerUniqueID = $id; |
|
| 353 | + return $obj; |
|
| 354 | + } |
|
| 355 | + |
|
| 356 | + /** |
|
| 357 | + * Get self with subject unique ID. |
|
| 358 | + * |
|
| 359 | + * @param UniqueIdentifier $id |
|
| 360 | + * @return self |
|
| 361 | + */ |
|
| 362 | + public function withSubjectUniqueID(UniqueIdentifier $id): self |
|
| 363 | + { |
|
| 364 | + $obj = clone $this; |
|
| 365 | + $obj->_subjectUniqueID = $id; |
|
| 366 | + return $obj; |
|
| 367 | + } |
|
| 368 | + |
|
| 369 | + /** |
|
| 370 | + * Get self with given extensions. |
|
| 371 | + * |
|
| 372 | + * @param Extensions $extensions |
|
| 373 | + * @return self |
|
| 374 | + */ |
|
| 375 | + public function withExtensions(Extensions $extensions): self |
|
| 376 | + { |
|
| 377 | + $obj = clone $this; |
|
| 378 | + $obj->_extensions = $extensions; |
|
| 379 | + return $obj; |
|
| 380 | + } |
|
| 381 | + |
|
| 382 | + /** |
|
| 383 | + * Get self with extensions added. |
|
| 384 | + * |
|
| 385 | + * @param Extension ...$exts One or more Extension objects |
|
| 386 | + * @return self |
|
| 387 | + */ |
|
| 388 | + public function withAdditionalExtensions(Extension ...$exts): self |
|
| 389 | + { |
|
| 390 | + $obj = clone $this; |
|
| 391 | + $obj->_extensions = $obj->_extensions->withExtensions(...$exts); |
|
| 392 | + return $obj; |
|
| 393 | + } |
|
| 394 | + |
|
| 395 | + /** |
|
| 396 | + * Check whether version is set. |
|
| 397 | + * |
|
| 398 | + * @return bool |
|
| 399 | + */ |
|
| 400 | + public function hasVersion(): bool |
|
| 401 | + { |
|
| 402 | + return isset($this->_version); |
|
| 403 | + } |
|
| 404 | + |
|
| 405 | + /** |
|
| 406 | + * Get certificate version. |
|
| 407 | + * |
|
| 408 | + * @return int |
|
| 409 | + */ |
|
| 410 | + public function version(): int |
|
| 411 | + { |
|
| 412 | + if (!$this->hasVersion()) { |
|
| 413 | + throw new \LogicException("version not set."); |
|
| 414 | + } |
|
| 415 | + return $this->_version; |
|
| 416 | + } |
|
| 417 | + |
|
| 418 | + /** |
|
| 419 | + * Check whether serial number is set. |
|
| 420 | + * |
|
| 421 | + * @return bool |
|
| 422 | + */ |
|
| 423 | + public function hasSerialNumber(): bool |
|
| 424 | + { |
|
| 425 | + return isset($this->_serialNumber); |
|
| 426 | + } |
|
| 427 | + |
|
| 428 | + /** |
|
| 429 | + * Get serial number. |
|
| 430 | + * |
|
| 431 | + * @return string Base 10 integer |
|
| 432 | + */ |
|
| 433 | + public function serialNumber(): string |
|
| 434 | + { |
|
| 435 | + if (!$this->hasSerialNumber()) { |
|
| 436 | + throw new \LogicException("serialNumber not set."); |
|
| 437 | + } |
|
| 438 | + return $this->_serialNumber; |
|
| 439 | + } |
|
| 440 | + |
|
| 441 | + /** |
|
| 442 | + * Check whether signature algorithm is set. |
|
| 443 | + * |
|
| 444 | + * @return bool |
|
| 445 | + */ |
|
| 446 | + public function hasSignature(): bool |
|
| 447 | + { |
|
| 448 | + return isset($this->_signature); |
|
| 449 | + } |
|
| 450 | + |
|
| 451 | + /** |
|
| 452 | + * Get signature algorithm. |
|
| 453 | + * |
|
| 454 | + * @return SignatureAlgorithmIdentifier |
|
| 455 | + */ |
|
| 456 | + public function signature(): SignatureAlgorithmIdentifier |
|
| 457 | + { |
|
| 458 | + if (!$this->hasSignature()) { |
|
| 459 | + throw new \LogicException("signature not set."); |
|
| 460 | + } |
|
| 461 | + return $this->_signature; |
|
| 462 | + } |
|
| 463 | + |
|
| 464 | + /** |
|
| 465 | + * Get issuer. |
|
| 466 | + * |
|
| 467 | + * @return Name |
|
| 468 | + */ |
|
| 469 | + public function issuer(): Name |
|
| 470 | + { |
|
| 471 | + return $this->_issuer; |
|
| 472 | + } |
|
| 473 | + |
|
| 474 | + /** |
|
| 475 | + * Get validity period. |
|
| 476 | + * |
|
| 477 | + * @return Validity |
|
| 478 | + */ |
|
| 479 | + public function validity(): Validity |
|
| 480 | + { |
|
| 481 | + return $this->_validity; |
|
| 482 | + } |
|
| 483 | + |
|
| 484 | + /** |
|
| 485 | + * Get subject. |
|
| 486 | + * |
|
| 487 | + * @return Name |
|
| 488 | + */ |
|
| 489 | + public function subject(): Name |
|
| 490 | + { |
|
| 491 | + return $this->_subject; |
|
| 492 | + } |
|
| 493 | + |
|
| 494 | + /** |
|
| 495 | + * Get subject public key. |
|
| 496 | + * |
|
| 497 | + * @return PublicKeyInfo |
|
| 498 | + */ |
|
| 499 | + public function subjectPublicKeyInfo(): PublicKeyInfo |
|
| 500 | + { |
|
| 501 | + return $this->_subjectPublicKeyInfo; |
|
| 502 | + } |
|
| 503 | + |
|
| 504 | + /** |
|
| 505 | + * Whether issuer unique identifier is present. |
|
| 506 | + * |
|
| 507 | + * @return bool |
|
| 508 | + */ |
|
| 509 | + public function hasIssuerUniqueID(): bool |
|
| 510 | + { |
|
| 511 | + return isset($this->_issuerUniqueID); |
|
| 512 | + } |
|
| 513 | + |
|
| 514 | + /** |
|
| 515 | + * Get issuerUniqueID. |
|
| 516 | + * |
|
| 517 | + * @return UniqueIdentifier |
|
| 518 | + */ |
|
| 519 | + public function issuerUniqueID(): UniqueIdentifier |
|
| 520 | + { |
|
| 521 | + if (!$this->hasIssuerUniqueID()) { |
|
| 522 | + throw new \LogicException("issuerUniqueID not set."); |
|
| 523 | + } |
|
| 524 | + return $this->_issuerUniqueID; |
|
| 525 | + } |
|
| 526 | + |
|
| 527 | + /** |
|
| 528 | + * Whether subject unique identifier is present. |
|
| 529 | + * |
|
| 530 | + * @return bool |
|
| 531 | + */ |
|
| 532 | + public function hasSubjectUniqueID(): bool |
|
| 533 | + { |
|
| 534 | + return isset($this->_subjectUniqueID); |
|
| 535 | + } |
|
| 536 | + |
|
| 537 | + /** |
|
| 538 | + * Get subjectUniqueID. |
|
| 539 | + * |
|
| 540 | + * @return UniqueIdentifier |
|
| 541 | + */ |
|
| 542 | + public function subjectUniqueID(): UniqueIdentifier |
|
| 543 | + { |
|
| 544 | + if (!$this->hasSubjectUniqueID()) { |
|
| 545 | + throw new \LogicException("subjectUniqueID not set."); |
|
| 546 | + } |
|
| 547 | + return $this->_subjectUniqueID; |
|
| 548 | + } |
|
| 549 | + |
|
| 550 | + /** |
|
| 551 | + * Get extensions. |
|
| 552 | + * |
|
| 553 | + * @return Extensions |
|
| 554 | + */ |
|
| 555 | + public function extensions(): Extensions |
|
| 556 | + { |
|
| 557 | + return $this->_extensions; |
|
| 558 | + } |
|
| 559 | + |
|
| 560 | + /** |
|
| 561 | + * Generate ASN.1 structure. |
|
| 562 | + * |
|
| 563 | + * @return Sequence |
|
| 564 | + */ |
|
| 565 | + public function toASN1(): Sequence |
|
| 566 | + { |
|
| 567 | + $elements = array(); |
|
| 568 | + $version = $this->version(); |
|
| 569 | + // if version is not default |
|
| 570 | + if ($version != self::VERSION_1) { |
|
| 571 | + $elements[] = new ExplicitlyTaggedType(0, new Integer($version)); |
|
| 572 | + } |
|
| 573 | + $serial = $this->serialNumber(); |
|
| 574 | + $signature = $this->signature(); |
|
| 575 | + // add required elements |
|
| 576 | + array_push($elements, new Integer($serial), $signature->toASN1(), |
|
| 577 | + $this->_issuer->toASN1(), $this->_validity->toASN1(), |
|
| 578 | + $this->_subject->toASN1(), $this->_subjectPublicKeyInfo->toASN1()); |
|
| 579 | + if (isset($this->_issuerUniqueID)) { |
|
| 580 | + $elements[] = new ImplicitlyTaggedType(1, |
|
| 581 | + $this->_issuerUniqueID->toASN1()); |
|
| 582 | + } |
|
| 583 | + if (isset($this->_subjectUniqueID)) { |
|
| 584 | + $elements[] = new ImplicitlyTaggedType(2, |
|
| 585 | + $this->_subjectUniqueID->toASN1()); |
|
| 586 | + } |
|
| 587 | + if (count($this->_extensions)) { |
|
| 588 | + $elements[] = new ExplicitlyTaggedType(3, |
|
| 589 | + $this->_extensions->toASN1()); |
|
| 590 | + } |
|
| 591 | + return new Sequence(...$elements); |
|
| 592 | + } |
|
| 593 | + |
|
| 594 | + /** |
|
| 595 | + * Create signed certificate. |
|
| 596 | + * |
|
| 597 | + * @param SignatureAlgorithmIdentifier $algo Algorithm used for signing |
|
| 598 | + * @param PrivateKeyInfo $privkey_info Private key used for signing |
|
| 599 | + * @param Crypto|null $crypto Crypto engine, use default if not set |
|
| 600 | + * @return Certificate |
|
| 601 | + */ |
|
| 602 | + public function sign(SignatureAlgorithmIdentifier $algo, |
|
| 603 | + PrivateKeyInfo $privkey_info, Crypto $crypto = null): Certificate |
|
| 604 | + { |
|
| 605 | + $crypto = $crypto ?: Crypto::getDefault(); |
|
| 606 | + $tbs_cert = clone $this; |
|
| 607 | + if (!isset($tbs_cert->_version)) { |
|
| 608 | + $tbs_cert->_version = $tbs_cert->_determineVersion(); |
|
| 609 | + } |
|
| 610 | + if (!isset($tbs_cert->_serialNumber)) { |
|
| 611 | + $tbs_cert->_serialNumber = strval(0); |
|
| 612 | + } |
|
| 613 | + $tbs_cert->_signature = $algo; |
|
| 614 | + $data = $tbs_cert->toASN1()->toDER(); |
|
| 615 | + $signature = $crypto->sign($data, $privkey_info, $algo); |
|
| 616 | + return new Certificate($tbs_cert, $algo, $signature); |
|
| 617 | + } |
|
| 618 | + |
|
| 619 | + /** |
|
| 620 | + * Determine minimum version for the certificate. |
|
| 621 | + * |
|
| 622 | + * @return int |
|
| 623 | + */ |
|
| 624 | + protected function _determineVersion(): int |
|
| 625 | + { |
|
| 626 | + // if extensions are present |
|
| 627 | + if (count($this->_extensions)) { |
|
| 628 | + return self::VERSION_3; |
|
| 629 | + } |
|
| 630 | + // if UniqueIdentifier is present |
|
| 631 | + if (isset($this->_issuerUniqueID) || isset($this->_subjectUniqueID)) { |
|
| 632 | + return self::VERSION_2; |
|
| 633 | + } |
|
| 634 | + return self::VERSION_1; |
|
| 635 | + } |
|
| 636 | 636 | } |
@@ -52,6 +52,7 @@ |
||
| 52 | 52 | * @param PathValidationConfig $config Validation config |
| 53 | 53 | * @param Certificate ...$certificates Certificates from the trust anchor to |
| 54 | 54 | * the end-entity certificate |
| 55 | + * @param Certificate[] $certificates |
|
| 55 | 56 | */ |
| 56 | 57 | public function __construct(Crypto $crypto, PathValidationConfig $config, |
| 57 | 58 | Certificate ...$certificates) |
@@ -17,585 +17,585 @@ |
||
| 17 | 17 | */ |
| 18 | 18 | class PathValidator |
| 19 | 19 | { |
| 20 | - /** |
|
| 21 | - * Crypto engine. |
|
| 22 | - * |
|
| 23 | - * @var Crypto $_crypto |
|
| 24 | - */ |
|
| 25 | - protected $_crypto; |
|
| 20 | + /** |
|
| 21 | + * Crypto engine. |
|
| 22 | + * |
|
| 23 | + * @var Crypto $_crypto |
|
| 24 | + */ |
|
| 25 | + protected $_crypto; |
|
| 26 | 26 | |
| 27 | - /** |
|
| 28 | - * Path validation configuration. |
|
| 29 | - * |
|
| 30 | - * @var PathValidationConfig $_config |
|
| 31 | - */ |
|
| 32 | - protected $_config; |
|
| 27 | + /** |
|
| 28 | + * Path validation configuration. |
|
| 29 | + * |
|
| 30 | + * @var PathValidationConfig $_config |
|
| 31 | + */ |
|
| 32 | + protected $_config; |
|
| 33 | 33 | |
| 34 | - /** |
|
| 35 | - * Certification path. |
|
| 36 | - * |
|
| 37 | - * @var Certificate[] $_certificates |
|
| 38 | - */ |
|
| 39 | - protected $_certificates; |
|
| 34 | + /** |
|
| 35 | + * Certification path. |
|
| 36 | + * |
|
| 37 | + * @var Certificate[] $_certificates |
|
| 38 | + */ |
|
| 39 | + protected $_certificates; |
|
| 40 | 40 | |
| 41 | - /** |
|
| 42 | - * Certification path trust anchor. |
|
| 43 | - * |
|
| 44 | - * @var Certificate $_trustAnchor |
|
| 45 | - */ |
|
| 46 | - protected $_trustAnchor; |
|
| 41 | + /** |
|
| 42 | + * Certification path trust anchor. |
|
| 43 | + * |
|
| 44 | + * @var Certificate $_trustAnchor |
|
| 45 | + */ |
|
| 46 | + protected $_trustAnchor; |
|
| 47 | 47 | |
| 48 | - /** |
|
| 49 | - * Constructor. |
|
| 50 | - * |
|
| 51 | - * @param Crypto $crypto Crypto engine |
|
| 52 | - * @param PathValidationConfig $config Validation config |
|
| 53 | - * @param Certificate ...$certificates Certificates from the trust anchor to |
|
| 54 | - * the end-entity certificate |
|
| 55 | - */ |
|
| 56 | - public function __construct(Crypto $crypto, PathValidationConfig $config, |
|
| 57 | - Certificate ...$certificates) |
|
| 58 | - { |
|
| 59 | - if (!count($certificates)) { |
|
| 60 | - throw new \LogicException("No certificates."); |
|
| 61 | - } |
|
| 62 | - $this->_crypto = $crypto; |
|
| 63 | - $this->_config = $config; |
|
| 64 | - $this->_certificates = $certificates; |
|
| 65 | - // if trust anchor is explicitly given in configuration |
|
| 66 | - if ($config->hasTrustAnchor()) { |
|
| 67 | - $this->_trustAnchor = $config->trustAnchor(); |
|
| 68 | - } else { |
|
| 69 | - $this->_trustAnchor = $certificates[0]; |
|
| 70 | - } |
|
| 71 | - } |
|
| 48 | + /** |
|
| 49 | + * Constructor. |
|
| 50 | + * |
|
| 51 | + * @param Crypto $crypto Crypto engine |
|
| 52 | + * @param PathValidationConfig $config Validation config |
|
| 53 | + * @param Certificate ...$certificates Certificates from the trust anchor to |
|
| 54 | + * the end-entity certificate |
|
| 55 | + */ |
|
| 56 | + public function __construct(Crypto $crypto, PathValidationConfig $config, |
|
| 57 | + Certificate ...$certificates) |
|
| 58 | + { |
|
| 59 | + if (!count($certificates)) { |
|
| 60 | + throw new \LogicException("No certificates."); |
|
| 61 | + } |
|
| 62 | + $this->_crypto = $crypto; |
|
| 63 | + $this->_config = $config; |
|
| 64 | + $this->_certificates = $certificates; |
|
| 65 | + // if trust anchor is explicitly given in configuration |
|
| 66 | + if ($config->hasTrustAnchor()) { |
|
| 67 | + $this->_trustAnchor = $config->trustAnchor(); |
|
| 68 | + } else { |
|
| 69 | + $this->_trustAnchor = $certificates[0]; |
|
| 70 | + } |
|
| 71 | + } |
|
| 72 | 72 | |
| 73 | - /** |
|
| 74 | - * Validate certification path. |
|
| 75 | - * |
|
| 76 | - * @throws PathValidationException |
|
| 77 | - * @return PathValidationResult |
|
| 78 | - */ |
|
| 79 | - public function validate(): PathValidationResult |
|
| 80 | - { |
|
| 81 | - $n = count($this->_certificates); |
|
| 82 | - $state = ValidatorState::initialize($this->_config, $this->_trustAnchor, |
|
| 83 | - $n); |
|
| 84 | - for ($i = 0; $i < $n; ++$i) { |
|
| 85 | - $state = $state->withIndex($i + 1); |
|
| 86 | - $cert = $this->_certificates[$i]; |
|
| 87 | - // process certificate (section 6.1.3.) |
|
| 88 | - $state = $this->_processCertificate($state, $cert); |
|
| 89 | - if (!$state->isFinal()) { |
|
| 90 | - // prepare next certificate (section 6.1.4.) |
|
| 91 | - $state = $this->_prepareNext($state, $cert); |
|
| 92 | - } |
|
| 93 | - } |
|
| 94 | - if (!isset($cert)) { |
|
| 95 | - throw new \LogicException("No certificates."); |
|
| 96 | - } |
|
| 97 | - // wrap-up (section 6.1.5.) |
|
| 98 | - $state = $this->_wrapUp($state, $cert); |
|
| 99 | - // return outputs |
|
| 100 | - return $state->getResult($this->_certificates); |
|
| 101 | - } |
|
| 73 | + /** |
|
| 74 | + * Validate certification path. |
|
| 75 | + * |
|
| 76 | + * @throws PathValidationException |
|
| 77 | + * @return PathValidationResult |
|
| 78 | + */ |
|
| 79 | + public function validate(): PathValidationResult |
|
| 80 | + { |
|
| 81 | + $n = count($this->_certificates); |
|
| 82 | + $state = ValidatorState::initialize($this->_config, $this->_trustAnchor, |
|
| 83 | + $n); |
|
| 84 | + for ($i = 0; $i < $n; ++$i) { |
|
| 85 | + $state = $state->withIndex($i + 1); |
|
| 86 | + $cert = $this->_certificates[$i]; |
|
| 87 | + // process certificate (section 6.1.3.) |
|
| 88 | + $state = $this->_processCertificate($state, $cert); |
|
| 89 | + if (!$state->isFinal()) { |
|
| 90 | + // prepare next certificate (section 6.1.4.) |
|
| 91 | + $state = $this->_prepareNext($state, $cert); |
|
| 92 | + } |
|
| 93 | + } |
|
| 94 | + if (!isset($cert)) { |
|
| 95 | + throw new \LogicException("No certificates."); |
|
| 96 | + } |
|
| 97 | + // wrap-up (section 6.1.5.) |
|
| 98 | + $state = $this->_wrapUp($state, $cert); |
|
| 99 | + // return outputs |
|
| 100 | + return $state->getResult($this->_certificates); |
|
| 101 | + } |
|
| 102 | 102 | |
| 103 | - /** |
|
| 104 | - * Apply basic certificate processing according to RFC 5280 section 6.1.3. |
|
| 105 | - * |
|
| 106 | - * @link https://tools.ietf.org/html/rfc5280#section-6.1.3 |
|
| 107 | - * @param ValidatorState $state |
|
| 108 | - * @param Certificate $cert |
|
| 109 | - * @throws PathValidationException |
|
| 110 | - * @return ValidatorState |
|
| 111 | - */ |
|
| 112 | - private function _processCertificate(ValidatorState $state, Certificate $cert): ValidatorState |
|
| 113 | - { |
|
| 114 | - // (a.1) verify signature |
|
| 115 | - $this->_verifySignature($state, $cert); |
|
| 116 | - // (a.2) check validity period |
|
| 117 | - $this->_checkValidity($cert); |
|
| 118 | - // (a.3) check that certificate is not revoked |
|
| 119 | - $this->_checkRevocation($cert); |
|
| 120 | - // (a.4) check issuer |
|
| 121 | - $this->_checkIssuer($state, $cert); |
|
| 122 | - // (b)(c) if certificate is self-issued and it is not |
|
| 123 | - // the final certificate in the path, skip this step |
|
| 124 | - if (!($cert->isSelfIssued() && !$state->isFinal())) { |
|
| 125 | - // (b) check permitted subtrees |
|
| 126 | - $this->_checkPermittedSubtrees($state, $cert); |
|
| 127 | - // (c) check excluded subtrees |
|
| 128 | - $this->_checkExcludedSubtrees($state, $cert); |
|
| 129 | - } |
|
| 130 | - $extensions = $cert->tbsCertificate()->extensions(); |
|
| 131 | - if ($extensions->hasCertificatePolicies()) { |
|
| 132 | - // (d) process policy information |
|
| 133 | - if ($state->hasValidPolicyTree()) { |
|
| 134 | - $state = $state->validPolicyTree()->processPolicies($state, |
|
| 135 | - $cert); |
|
| 136 | - } |
|
| 137 | - } else { |
|
| 138 | - // (e) certificate policies extension not present, |
|
| 139 | - // set the valid_policy_tree to NULL |
|
| 140 | - $state = $state->withoutValidPolicyTree(); |
|
| 141 | - } |
|
| 142 | - // (f) check that explicit_policy > 0 or valid_policy_tree is set |
|
| 143 | - if (!($state->explicitPolicy() > 0 || $state->hasValidPolicyTree())) { |
|
| 144 | - throw new PathValidationException("No valid policies."); |
|
| 145 | - } |
|
| 146 | - return $state; |
|
| 147 | - } |
|
| 103 | + /** |
|
| 104 | + * Apply basic certificate processing according to RFC 5280 section 6.1.3. |
|
| 105 | + * |
|
| 106 | + * @link https://tools.ietf.org/html/rfc5280#section-6.1.3 |
|
| 107 | + * @param ValidatorState $state |
|
| 108 | + * @param Certificate $cert |
|
| 109 | + * @throws PathValidationException |
|
| 110 | + * @return ValidatorState |
|
| 111 | + */ |
|
| 112 | + private function _processCertificate(ValidatorState $state, Certificate $cert): ValidatorState |
|
| 113 | + { |
|
| 114 | + // (a.1) verify signature |
|
| 115 | + $this->_verifySignature($state, $cert); |
|
| 116 | + // (a.2) check validity period |
|
| 117 | + $this->_checkValidity($cert); |
|
| 118 | + // (a.3) check that certificate is not revoked |
|
| 119 | + $this->_checkRevocation($cert); |
|
| 120 | + // (a.4) check issuer |
|
| 121 | + $this->_checkIssuer($state, $cert); |
|
| 122 | + // (b)(c) if certificate is self-issued and it is not |
|
| 123 | + // the final certificate in the path, skip this step |
|
| 124 | + if (!($cert->isSelfIssued() && !$state->isFinal())) { |
|
| 125 | + // (b) check permitted subtrees |
|
| 126 | + $this->_checkPermittedSubtrees($state, $cert); |
|
| 127 | + // (c) check excluded subtrees |
|
| 128 | + $this->_checkExcludedSubtrees($state, $cert); |
|
| 129 | + } |
|
| 130 | + $extensions = $cert->tbsCertificate()->extensions(); |
|
| 131 | + if ($extensions->hasCertificatePolicies()) { |
|
| 132 | + // (d) process policy information |
|
| 133 | + if ($state->hasValidPolicyTree()) { |
|
| 134 | + $state = $state->validPolicyTree()->processPolicies($state, |
|
| 135 | + $cert); |
|
| 136 | + } |
|
| 137 | + } else { |
|
| 138 | + // (e) certificate policies extension not present, |
|
| 139 | + // set the valid_policy_tree to NULL |
|
| 140 | + $state = $state->withoutValidPolicyTree(); |
|
| 141 | + } |
|
| 142 | + // (f) check that explicit_policy > 0 or valid_policy_tree is set |
|
| 143 | + if (!($state->explicitPolicy() > 0 || $state->hasValidPolicyTree())) { |
|
| 144 | + throw new PathValidationException("No valid policies."); |
|
| 145 | + } |
|
| 146 | + return $state; |
|
| 147 | + } |
|
| 148 | 148 | |
| 149 | - /** |
|
| 150 | - * Apply preparation for the certificate i+1 according to rfc5280 section |
|
| 151 | - * 6.1.4. |
|
| 152 | - * |
|
| 153 | - * @link https://tools.ietf.org/html/rfc5280#section-6.1.4 |
|
| 154 | - * @param ValidatorState $state |
|
| 155 | - * @param Certificate $cert |
|
| 156 | - * @return ValidatorState |
|
| 157 | - */ |
|
| 158 | - private function _prepareNext(ValidatorState $state, Certificate $cert): ValidatorState |
|
| 159 | - { |
|
| 160 | - // (a)(b) if policy mappings extension is present |
|
| 161 | - $state = $this->_preparePolicyMappings($state, $cert); |
|
| 162 | - // (c) assign working_issuer_name |
|
| 163 | - $state = $state->withWorkingIssuerName( |
|
| 164 | - $cert->tbsCertificate() |
|
| 165 | - ->subject()); |
|
| 166 | - // (d)(e)(f) |
|
| 167 | - $state = $this->_setPublicKeyState($state, $cert); |
|
| 168 | - // (g) if name constraints extension is present |
|
| 169 | - $state = $this->_prepareNameConstraints($state, $cert); |
|
| 170 | - // (h) if certificate is not self-issued |
|
| 171 | - if (!$cert->isSelfIssued()) { |
|
| 172 | - $state = $this->_prepareNonSelfIssued($state); |
|
| 173 | - } |
|
| 174 | - // (i) if policy constraints extension is present |
|
| 175 | - $state = $this->_preparePolicyConstraints($state, $cert); |
|
| 176 | - // (j) if inhibit any policy extension is present |
|
| 177 | - $state = $this->_prepareInhibitAnyPolicy($state, $cert); |
|
| 178 | - // (k) check basic constraints |
|
| 179 | - $this->_processBasicContraints($cert); |
|
| 180 | - // (l) verify max_path_length |
|
| 181 | - $state = $this->_verifyMaxPathLength($state, $cert); |
|
| 182 | - // (m) check pathLenContraint |
|
| 183 | - $state = $this->_processPathLengthContraint($state, $cert); |
|
| 184 | - // (n) check key usage |
|
| 185 | - $this->_checkKeyUsage($cert); |
|
| 186 | - // (o) process relevant extensions |
|
| 187 | - $state = $this->_processExtensions($state, $cert); |
|
| 188 | - return $state; |
|
| 189 | - } |
|
| 149 | + /** |
|
| 150 | + * Apply preparation for the certificate i+1 according to rfc5280 section |
|
| 151 | + * 6.1.4. |
|
| 152 | + * |
|
| 153 | + * @link https://tools.ietf.org/html/rfc5280#section-6.1.4 |
|
| 154 | + * @param ValidatorState $state |
|
| 155 | + * @param Certificate $cert |
|
| 156 | + * @return ValidatorState |
|
| 157 | + */ |
|
| 158 | + private function _prepareNext(ValidatorState $state, Certificate $cert): ValidatorState |
|
| 159 | + { |
|
| 160 | + // (a)(b) if policy mappings extension is present |
|
| 161 | + $state = $this->_preparePolicyMappings($state, $cert); |
|
| 162 | + // (c) assign working_issuer_name |
|
| 163 | + $state = $state->withWorkingIssuerName( |
|
| 164 | + $cert->tbsCertificate() |
|
| 165 | + ->subject()); |
|
| 166 | + // (d)(e)(f) |
|
| 167 | + $state = $this->_setPublicKeyState($state, $cert); |
|
| 168 | + // (g) if name constraints extension is present |
|
| 169 | + $state = $this->_prepareNameConstraints($state, $cert); |
|
| 170 | + // (h) if certificate is not self-issued |
|
| 171 | + if (!$cert->isSelfIssued()) { |
|
| 172 | + $state = $this->_prepareNonSelfIssued($state); |
|
| 173 | + } |
|
| 174 | + // (i) if policy constraints extension is present |
|
| 175 | + $state = $this->_preparePolicyConstraints($state, $cert); |
|
| 176 | + // (j) if inhibit any policy extension is present |
|
| 177 | + $state = $this->_prepareInhibitAnyPolicy($state, $cert); |
|
| 178 | + // (k) check basic constraints |
|
| 179 | + $this->_processBasicContraints($cert); |
|
| 180 | + // (l) verify max_path_length |
|
| 181 | + $state = $this->_verifyMaxPathLength($state, $cert); |
|
| 182 | + // (m) check pathLenContraint |
|
| 183 | + $state = $this->_processPathLengthContraint($state, $cert); |
|
| 184 | + // (n) check key usage |
|
| 185 | + $this->_checkKeyUsage($cert); |
|
| 186 | + // (o) process relevant extensions |
|
| 187 | + $state = $this->_processExtensions($state, $cert); |
|
| 188 | + return $state; |
|
| 189 | + } |
|
| 190 | 190 | |
| 191 | - /** |
|
| 192 | - * Apply wrap-up procedure according to RFC 5280 section 6.1.5. |
|
| 193 | - * |
|
| 194 | - * @link https://tools.ietf.org/html/rfc5280#section-6.1.5 |
|
| 195 | - * @param ValidatorState $state |
|
| 196 | - * @param Certificate $cert |
|
| 197 | - * @throws PathValidationException |
|
| 198 | - * @return ValidatorState |
|
| 199 | - */ |
|
| 200 | - private function _wrapUp(ValidatorState $state, Certificate $cert): ValidatorState |
|
| 201 | - { |
|
| 202 | - $tbs_cert = $cert->tbsCertificate(); |
|
| 203 | - $extensions = $tbs_cert->extensions(); |
|
| 204 | - // (a) |
|
| 205 | - if ($state->explicitPolicy() > 0) { |
|
| 206 | - $state = $state->withExplicitPolicy($state->explicitPolicy() - 1); |
|
| 207 | - } |
|
| 208 | - // (b) |
|
| 209 | - if ($extensions->hasPolicyConstraints()) { |
|
| 210 | - $ext = $extensions->policyConstraints(); |
|
| 211 | - if ($ext->hasRequireExplicitPolicy() && |
|
| 212 | - $ext->requireExplicitPolicy() == 0) { |
|
| 213 | - $state = $state->withExplicitPolicy(0); |
|
| 214 | - } |
|
| 215 | - } |
|
| 216 | - // (c)(d)(e) |
|
| 217 | - $state = $this->_setPublicKeyState($state, $cert); |
|
| 218 | - // (f) process relevant extensions |
|
| 219 | - $state = $this->_processExtensions($state, $cert); |
|
| 220 | - // (g) intersection of valid_policy_tree and the initial-policy-set |
|
| 221 | - $state = $this->_calculatePolicyIntersection($state); |
|
| 222 | - // check that explicit_policy > 0 or valid_policy_tree is set |
|
| 223 | - if (!($state->explicitPolicy() > 0 || $state->hasValidPolicyTree())) { |
|
| 224 | - throw new PathValidationException("No valid policies."); |
|
| 225 | - } |
|
| 226 | - // path validation succeeded |
|
| 227 | - return $state; |
|
| 228 | - } |
|
| 191 | + /** |
|
| 192 | + * Apply wrap-up procedure according to RFC 5280 section 6.1.5. |
|
| 193 | + * |
|
| 194 | + * @link https://tools.ietf.org/html/rfc5280#section-6.1.5 |
|
| 195 | + * @param ValidatorState $state |
|
| 196 | + * @param Certificate $cert |
|
| 197 | + * @throws PathValidationException |
|
| 198 | + * @return ValidatorState |
|
| 199 | + */ |
|
| 200 | + private function _wrapUp(ValidatorState $state, Certificate $cert): ValidatorState |
|
| 201 | + { |
|
| 202 | + $tbs_cert = $cert->tbsCertificate(); |
|
| 203 | + $extensions = $tbs_cert->extensions(); |
|
| 204 | + // (a) |
|
| 205 | + if ($state->explicitPolicy() > 0) { |
|
| 206 | + $state = $state->withExplicitPolicy($state->explicitPolicy() - 1); |
|
| 207 | + } |
|
| 208 | + // (b) |
|
| 209 | + if ($extensions->hasPolicyConstraints()) { |
|
| 210 | + $ext = $extensions->policyConstraints(); |
|
| 211 | + if ($ext->hasRequireExplicitPolicy() && |
|
| 212 | + $ext->requireExplicitPolicy() == 0) { |
|
| 213 | + $state = $state->withExplicitPolicy(0); |
|
| 214 | + } |
|
| 215 | + } |
|
| 216 | + // (c)(d)(e) |
|
| 217 | + $state = $this->_setPublicKeyState($state, $cert); |
|
| 218 | + // (f) process relevant extensions |
|
| 219 | + $state = $this->_processExtensions($state, $cert); |
|
| 220 | + // (g) intersection of valid_policy_tree and the initial-policy-set |
|
| 221 | + $state = $this->_calculatePolicyIntersection($state); |
|
| 222 | + // check that explicit_policy > 0 or valid_policy_tree is set |
|
| 223 | + if (!($state->explicitPolicy() > 0 || $state->hasValidPolicyTree())) { |
|
| 224 | + throw new PathValidationException("No valid policies."); |
|
| 225 | + } |
|
| 226 | + // path validation succeeded |
|
| 227 | + return $state; |
|
| 228 | + } |
|
| 229 | 229 | |
| 230 | - /** |
|
| 231 | - * Update working_public_key, working_public_key_parameters and |
|
| 232 | - * working_public_key_algorithm state variables from certificate. |
|
| 233 | - * |
|
| 234 | - * @param ValidatorState $state |
|
| 235 | - * @param Certificate $cert |
|
| 236 | - * @return ValidatorState |
|
| 237 | - */ |
|
| 238 | - private function _setPublicKeyState(ValidatorState $state, Certificate $cert): ValidatorState |
|
| 239 | - { |
|
| 240 | - $pk_info = $cert->tbsCertificate()->subjectPublicKeyInfo(); |
|
| 241 | - // assign working_public_key |
|
| 242 | - $state = $state->withWorkingPublicKey($pk_info); |
|
| 243 | - // assign working_public_key_parameters |
|
| 244 | - $params = ValidatorState::getAlgorithmParameters( |
|
| 245 | - $pk_info->algorithmIdentifier()); |
|
| 246 | - if (null !== $params) { |
|
| 247 | - $state = $state->withWorkingPublicKeyParameters($params); |
|
| 248 | - } else { |
|
| 249 | - // if algorithms differ, set parameters to null |
|
| 250 | - if ($pk_info->algorithmIdentifier()->oid() !== |
|
| 251 | - $state->workingPublicKeyAlgorithm()->oid()) { |
|
| 252 | - $state = $state->withWorkingPublicKeyParameters(null); |
|
| 253 | - } |
|
| 254 | - } |
|
| 255 | - // assign working_public_key_algorithm |
|
| 256 | - $state = $state->withWorkingPublicKeyAlgorithm( |
|
| 257 | - $pk_info->algorithmIdentifier()); |
|
| 258 | - return $state; |
|
| 259 | - } |
|
| 230 | + /** |
|
| 231 | + * Update working_public_key, working_public_key_parameters and |
|
| 232 | + * working_public_key_algorithm state variables from certificate. |
|
| 233 | + * |
|
| 234 | + * @param ValidatorState $state |
|
| 235 | + * @param Certificate $cert |
|
| 236 | + * @return ValidatorState |
|
| 237 | + */ |
|
| 238 | + private function _setPublicKeyState(ValidatorState $state, Certificate $cert): ValidatorState |
|
| 239 | + { |
|
| 240 | + $pk_info = $cert->tbsCertificate()->subjectPublicKeyInfo(); |
|
| 241 | + // assign working_public_key |
|
| 242 | + $state = $state->withWorkingPublicKey($pk_info); |
|
| 243 | + // assign working_public_key_parameters |
|
| 244 | + $params = ValidatorState::getAlgorithmParameters( |
|
| 245 | + $pk_info->algorithmIdentifier()); |
|
| 246 | + if (null !== $params) { |
|
| 247 | + $state = $state->withWorkingPublicKeyParameters($params); |
|
| 248 | + } else { |
|
| 249 | + // if algorithms differ, set parameters to null |
|
| 250 | + if ($pk_info->algorithmIdentifier()->oid() !== |
|
| 251 | + $state->workingPublicKeyAlgorithm()->oid()) { |
|
| 252 | + $state = $state->withWorkingPublicKeyParameters(null); |
|
| 253 | + } |
|
| 254 | + } |
|
| 255 | + // assign working_public_key_algorithm |
|
| 256 | + $state = $state->withWorkingPublicKeyAlgorithm( |
|
| 257 | + $pk_info->algorithmIdentifier()); |
|
| 258 | + return $state; |
|
| 259 | + } |
|
| 260 | 260 | |
| 261 | - /** |
|
| 262 | - * Verify certificate signature. |
|
| 263 | - * |
|
| 264 | - * @param ValidatorState $state |
|
| 265 | - * @param Certificate $cert |
|
| 266 | - * @throws PathValidationException |
|
| 267 | - */ |
|
| 268 | - private function _verifySignature(ValidatorState $state, Certificate $cert) |
|
| 269 | - { |
|
| 270 | - try { |
|
| 271 | - $valid = $cert->verify($state->workingPublicKey(), $this->_crypto); |
|
| 272 | - } catch (\RuntimeException $e) { |
|
| 273 | - throw new PathValidationException( |
|
| 274 | - "Failed to verify signature: " . $e->getMessage(), 0, $e); |
|
| 275 | - } |
|
| 276 | - if (!$valid) { |
|
| 277 | - throw new PathValidationException( |
|
| 278 | - "Certificate signature doesn't match."); |
|
| 279 | - } |
|
| 280 | - } |
|
| 261 | + /** |
|
| 262 | + * Verify certificate signature. |
|
| 263 | + * |
|
| 264 | + * @param ValidatorState $state |
|
| 265 | + * @param Certificate $cert |
|
| 266 | + * @throws PathValidationException |
|
| 267 | + */ |
|
| 268 | + private function _verifySignature(ValidatorState $state, Certificate $cert) |
|
| 269 | + { |
|
| 270 | + try { |
|
| 271 | + $valid = $cert->verify($state->workingPublicKey(), $this->_crypto); |
|
| 272 | + } catch (\RuntimeException $e) { |
|
| 273 | + throw new PathValidationException( |
|
| 274 | + "Failed to verify signature: " . $e->getMessage(), 0, $e); |
|
| 275 | + } |
|
| 276 | + if (!$valid) { |
|
| 277 | + throw new PathValidationException( |
|
| 278 | + "Certificate signature doesn't match."); |
|
| 279 | + } |
|
| 280 | + } |
|
| 281 | 281 | |
| 282 | - /** |
|
| 283 | - * Check certificate validity. |
|
| 284 | - * |
|
| 285 | - * @param Certificate $cert |
|
| 286 | - * @throws PathValidationException |
|
| 287 | - */ |
|
| 288 | - private function _checkValidity(Certificate $cert) |
|
| 289 | - { |
|
| 290 | - $refdt = $this->_config->dateTime(); |
|
| 291 | - $validity = $cert->tbsCertificate()->validity(); |
|
| 292 | - if ($validity->notBefore() |
|
| 293 | - ->dateTime() |
|
| 294 | - ->diff($refdt)->invert) { |
|
| 295 | - throw new PathValidationException( |
|
| 296 | - "Certificate validity period has not started."); |
|
| 297 | - } |
|
| 298 | - if ($refdt->diff($validity->notAfter() |
|
| 299 | - ->dateTime())->invert) { |
|
| 300 | - throw new PathValidationException("Certificate has expired."); |
|
| 301 | - } |
|
| 302 | - } |
|
| 282 | + /** |
|
| 283 | + * Check certificate validity. |
|
| 284 | + * |
|
| 285 | + * @param Certificate $cert |
|
| 286 | + * @throws PathValidationException |
|
| 287 | + */ |
|
| 288 | + private function _checkValidity(Certificate $cert) |
|
| 289 | + { |
|
| 290 | + $refdt = $this->_config->dateTime(); |
|
| 291 | + $validity = $cert->tbsCertificate()->validity(); |
|
| 292 | + if ($validity->notBefore() |
|
| 293 | + ->dateTime() |
|
| 294 | + ->diff($refdt)->invert) { |
|
| 295 | + throw new PathValidationException( |
|
| 296 | + "Certificate validity period has not started."); |
|
| 297 | + } |
|
| 298 | + if ($refdt->diff($validity->notAfter() |
|
| 299 | + ->dateTime())->invert) { |
|
| 300 | + throw new PathValidationException("Certificate has expired."); |
|
| 301 | + } |
|
| 302 | + } |
|
| 303 | 303 | |
| 304 | - /** |
|
| 305 | - * Check certificate revocation. |
|
| 306 | - * |
|
| 307 | - * @param Certificate $cert |
|
| 308 | - */ |
|
| 309 | - private function _checkRevocation(Certificate $cert) |
|
| 310 | - { |
|
| 311 | - // @todo Implement CRL handling |
|
| 312 | - } |
|
| 304 | + /** |
|
| 305 | + * Check certificate revocation. |
|
| 306 | + * |
|
| 307 | + * @param Certificate $cert |
|
| 308 | + */ |
|
| 309 | + private function _checkRevocation(Certificate $cert) |
|
| 310 | + { |
|
| 311 | + // @todo Implement CRL handling |
|
| 312 | + } |
|
| 313 | 313 | |
| 314 | - /** |
|
| 315 | - * Check certificate issuer. |
|
| 316 | - * |
|
| 317 | - * @param ValidatorState $state |
|
| 318 | - * @param Certificate $cert |
|
| 319 | - * @throws PathValidationException |
|
| 320 | - */ |
|
| 321 | - private function _checkIssuer(ValidatorState $state, Certificate $cert) |
|
| 322 | - { |
|
| 323 | - if (!$cert->tbsCertificate() |
|
| 324 | - ->issuer() |
|
| 325 | - ->equals($state->workingIssuerName())) { |
|
| 326 | - throw new PathValidationException("Certification issuer mismatch."); |
|
| 327 | - } |
|
| 328 | - } |
|
| 314 | + /** |
|
| 315 | + * Check certificate issuer. |
|
| 316 | + * |
|
| 317 | + * @param ValidatorState $state |
|
| 318 | + * @param Certificate $cert |
|
| 319 | + * @throws PathValidationException |
|
| 320 | + */ |
|
| 321 | + private function _checkIssuer(ValidatorState $state, Certificate $cert) |
|
| 322 | + { |
|
| 323 | + if (!$cert->tbsCertificate() |
|
| 324 | + ->issuer() |
|
| 325 | + ->equals($state->workingIssuerName())) { |
|
| 326 | + throw new PathValidationException("Certification issuer mismatch."); |
|
| 327 | + } |
|
| 328 | + } |
|
| 329 | 329 | |
| 330 | - /** |
|
| 331 | - * |
|
| 332 | - * @param ValidatorState $state |
|
| 333 | - * @param Certificate $cert |
|
| 334 | - */ |
|
| 335 | - private function _checkPermittedSubtrees(ValidatorState $state, |
|
| 336 | - Certificate $cert) |
|
| 337 | - { |
|
| 338 | - // @todo Implement |
|
| 339 | - $state->permittedSubtrees(); |
|
| 340 | - } |
|
| 330 | + /** |
|
| 331 | + * |
|
| 332 | + * @param ValidatorState $state |
|
| 333 | + * @param Certificate $cert |
|
| 334 | + */ |
|
| 335 | + private function _checkPermittedSubtrees(ValidatorState $state, |
|
| 336 | + Certificate $cert) |
|
| 337 | + { |
|
| 338 | + // @todo Implement |
|
| 339 | + $state->permittedSubtrees(); |
|
| 340 | + } |
|
| 341 | 341 | |
| 342 | - /** |
|
| 343 | - * |
|
| 344 | - * @param ValidatorState $state |
|
| 345 | - * @param Certificate $cert |
|
| 346 | - */ |
|
| 347 | - private function _checkExcludedSubtrees(ValidatorState $state, |
|
| 348 | - Certificate $cert) |
|
| 349 | - { |
|
| 350 | - // @todo Implement |
|
| 351 | - $state->excludedSubtrees(); |
|
| 352 | - } |
|
| 342 | + /** |
|
| 343 | + * |
|
| 344 | + * @param ValidatorState $state |
|
| 345 | + * @param Certificate $cert |
|
| 346 | + */ |
|
| 347 | + private function _checkExcludedSubtrees(ValidatorState $state, |
|
| 348 | + Certificate $cert) |
|
| 349 | + { |
|
| 350 | + // @todo Implement |
|
| 351 | + $state->excludedSubtrees(); |
|
| 352 | + } |
|
| 353 | 353 | |
| 354 | - /** |
|
| 355 | - * Apply policy mappings handling for the preparation step. |
|
| 356 | - * |
|
| 357 | - * @param ValidatorState $state |
|
| 358 | - * @param Certificate $cert |
|
| 359 | - * @throws PathValidationException |
|
| 360 | - * @return ValidatorState |
|
| 361 | - */ |
|
| 362 | - private function _preparePolicyMappings(ValidatorState $state, |
|
| 363 | - Certificate $cert): ValidatorState |
|
| 364 | - { |
|
| 365 | - $extensions = $cert->tbsCertificate()->extensions(); |
|
| 366 | - if ($extensions->hasPolicyMappings()) { |
|
| 367 | - // (a) verify that anyPolicy mapping is not used |
|
| 368 | - if ($extensions->policyMappings()->hasAnyPolicyMapping()) { |
|
| 369 | - throw new PathValidationException("anyPolicy mapping found."); |
|
| 370 | - } |
|
| 371 | - // (b) process policy mappings |
|
| 372 | - if ($state->hasValidPolicyTree()) { |
|
| 373 | - $state = $state->validPolicyTree()->processMappings($state, |
|
| 374 | - $cert); |
|
| 375 | - } |
|
| 376 | - } |
|
| 377 | - return $state; |
|
| 378 | - } |
|
| 354 | + /** |
|
| 355 | + * Apply policy mappings handling for the preparation step. |
|
| 356 | + * |
|
| 357 | + * @param ValidatorState $state |
|
| 358 | + * @param Certificate $cert |
|
| 359 | + * @throws PathValidationException |
|
| 360 | + * @return ValidatorState |
|
| 361 | + */ |
|
| 362 | + private function _preparePolicyMappings(ValidatorState $state, |
|
| 363 | + Certificate $cert): ValidatorState |
|
| 364 | + { |
|
| 365 | + $extensions = $cert->tbsCertificate()->extensions(); |
|
| 366 | + if ($extensions->hasPolicyMappings()) { |
|
| 367 | + // (a) verify that anyPolicy mapping is not used |
|
| 368 | + if ($extensions->policyMappings()->hasAnyPolicyMapping()) { |
|
| 369 | + throw new PathValidationException("anyPolicy mapping found."); |
|
| 370 | + } |
|
| 371 | + // (b) process policy mappings |
|
| 372 | + if ($state->hasValidPolicyTree()) { |
|
| 373 | + $state = $state->validPolicyTree()->processMappings($state, |
|
| 374 | + $cert); |
|
| 375 | + } |
|
| 376 | + } |
|
| 377 | + return $state; |
|
| 378 | + } |
|
| 379 | 379 | |
| 380 | - /** |
|
| 381 | - * Apply name constraints handling for the preparation step. |
|
| 382 | - * |
|
| 383 | - * @param ValidatorState $state |
|
| 384 | - * @param Certificate $cert |
|
| 385 | - * @return ValidatorState |
|
| 386 | - */ |
|
| 387 | - private function _prepareNameConstraints(ValidatorState $state, |
|
| 388 | - Certificate $cert): ValidatorState |
|
| 389 | - { |
|
| 390 | - $extensions = $cert->tbsCertificate()->extensions(); |
|
| 391 | - if ($extensions->hasNameConstraints()) { |
|
| 392 | - $state = $this->_processNameConstraints($state, $cert); |
|
| 393 | - } |
|
| 394 | - return $state; |
|
| 395 | - } |
|
| 380 | + /** |
|
| 381 | + * Apply name constraints handling for the preparation step. |
|
| 382 | + * |
|
| 383 | + * @param ValidatorState $state |
|
| 384 | + * @param Certificate $cert |
|
| 385 | + * @return ValidatorState |
|
| 386 | + */ |
|
| 387 | + private function _prepareNameConstraints(ValidatorState $state, |
|
| 388 | + Certificate $cert): ValidatorState |
|
| 389 | + { |
|
| 390 | + $extensions = $cert->tbsCertificate()->extensions(); |
|
| 391 | + if ($extensions->hasNameConstraints()) { |
|
| 392 | + $state = $this->_processNameConstraints($state, $cert); |
|
| 393 | + } |
|
| 394 | + return $state; |
|
| 395 | + } |
|
| 396 | 396 | |
| 397 | - /** |
|
| 398 | - * Apply preparation for a non-self-signed certificate. |
|
| 399 | - * |
|
| 400 | - * @param ValidatorState $state |
|
| 401 | - * @return ValidatorState |
|
| 402 | - */ |
|
| 403 | - private function _prepareNonSelfIssued(ValidatorState $state): ValidatorState |
|
| 404 | - { |
|
| 405 | - // (h.1) |
|
| 406 | - if ($state->explicitPolicy() > 0) { |
|
| 407 | - $state = $state->withExplicitPolicy($state->explicitPolicy() - 1); |
|
| 408 | - } |
|
| 409 | - // (h.2) |
|
| 410 | - if ($state->policyMapping() > 0) { |
|
| 411 | - $state = $state->withPolicyMapping($state->policyMapping() - 1); |
|
| 412 | - } |
|
| 413 | - // (h.3) |
|
| 414 | - if ($state->inhibitAnyPolicy() > 0) { |
|
| 415 | - $state = $state->withInhibitAnyPolicy( |
|
| 416 | - $state->inhibitAnyPolicy() - 1); |
|
| 417 | - } |
|
| 418 | - return $state; |
|
| 419 | - } |
|
| 397 | + /** |
|
| 398 | + * Apply preparation for a non-self-signed certificate. |
|
| 399 | + * |
|
| 400 | + * @param ValidatorState $state |
|
| 401 | + * @return ValidatorState |
|
| 402 | + */ |
|
| 403 | + private function _prepareNonSelfIssued(ValidatorState $state): ValidatorState |
|
| 404 | + { |
|
| 405 | + // (h.1) |
|
| 406 | + if ($state->explicitPolicy() > 0) { |
|
| 407 | + $state = $state->withExplicitPolicy($state->explicitPolicy() - 1); |
|
| 408 | + } |
|
| 409 | + // (h.2) |
|
| 410 | + if ($state->policyMapping() > 0) { |
|
| 411 | + $state = $state->withPolicyMapping($state->policyMapping() - 1); |
|
| 412 | + } |
|
| 413 | + // (h.3) |
|
| 414 | + if ($state->inhibitAnyPolicy() > 0) { |
|
| 415 | + $state = $state->withInhibitAnyPolicy( |
|
| 416 | + $state->inhibitAnyPolicy() - 1); |
|
| 417 | + } |
|
| 418 | + return $state; |
|
| 419 | + } |
|
| 420 | 420 | |
| 421 | - /** |
|
| 422 | - * Apply policy constraints handling for the preparation step. |
|
| 423 | - * |
|
| 424 | - * @param ValidatorState $state |
|
| 425 | - * @param Certificate $cert |
|
| 426 | - * @return ValidatorState |
|
| 427 | - */ |
|
| 428 | - private function _preparePolicyConstraints(ValidatorState $state, |
|
| 429 | - Certificate $cert): ValidatorState |
|
| 430 | - { |
|
| 431 | - $extensions = $cert->tbsCertificate()->extensions(); |
|
| 432 | - if (!$extensions->hasPolicyConstraints()) { |
|
| 433 | - return $state; |
|
| 434 | - } |
|
| 435 | - $ext = $extensions->policyConstraints(); |
|
| 436 | - // (i.1) |
|
| 437 | - if ($ext->hasRequireExplicitPolicy() && |
|
| 438 | - $ext->requireExplicitPolicy() < $state->explicitPolicy()) { |
|
| 439 | - $state = $state->withExplicitPolicy($ext->requireExplicitPolicy()); |
|
| 440 | - } |
|
| 441 | - // (i.2) |
|
| 442 | - if ($ext->hasInhibitPolicyMapping() && |
|
| 443 | - $ext->inhibitPolicyMapping() < $state->policyMapping()) { |
|
| 444 | - $state = $state->withPolicyMapping($ext->inhibitPolicyMapping()); |
|
| 445 | - } |
|
| 446 | - return $state; |
|
| 447 | - } |
|
| 421 | + /** |
|
| 422 | + * Apply policy constraints handling for the preparation step. |
|
| 423 | + * |
|
| 424 | + * @param ValidatorState $state |
|
| 425 | + * @param Certificate $cert |
|
| 426 | + * @return ValidatorState |
|
| 427 | + */ |
|
| 428 | + private function _preparePolicyConstraints(ValidatorState $state, |
|
| 429 | + Certificate $cert): ValidatorState |
|
| 430 | + { |
|
| 431 | + $extensions = $cert->tbsCertificate()->extensions(); |
|
| 432 | + if (!$extensions->hasPolicyConstraints()) { |
|
| 433 | + return $state; |
|
| 434 | + } |
|
| 435 | + $ext = $extensions->policyConstraints(); |
|
| 436 | + // (i.1) |
|
| 437 | + if ($ext->hasRequireExplicitPolicy() && |
|
| 438 | + $ext->requireExplicitPolicy() < $state->explicitPolicy()) { |
|
| 439 | + $state = $state->withExplicitPolicy($ext->requireExplicitPolicy()); |
|
| 440 | + } |
|
| 441 | + // (i.2) |
|
| 442 | + if ($ext->hasInhibitPolicyMapping() && |
|
| 443 | + $ext->inhibitPolicyMapping() < $state->policyMapping()) { |
|
| 444 | + $state = $state->withPolicyMapping($ext->inhibitPolicyMapping()); |
|
| 445 | + } |
|
| 446 | + return $state; |
|
| 447 | + } |
|
| 448 | 448 | |
| 449 | - /** |
|
| 450 | - * Apply inhibit any-policy handling for the preparation step. |
|
| 451 | - * |
|
| 452 | - * @param ValidatorState $state |
|
| 453 | - * @param Certificate $cert |
|
| 454 | - * @return ValidatorState |
|
| 455 | - */ |
|
| 456 | - private function _prepareInhibitAnyPolicy(ValidatorState $state, |
|
| 457 | - Certificate $cert): ValidatorState |
|
| 458 | - { |
|
| 459 | - $extensions = $cert->tbsCertificate()->extensions(); |
|
| 460 | - if ($extensions->hasInhibitAnyPolicy()) { |
|
| 461 | - $ext = $extensions->inhibitAnyPolicy(); |
|
| 462 | - if ($ext->skipCerts() < $state->inhibitAnyPolicy()) { |
|
| 463 | - $state = $state->withInhibitAnyPolicy($ext->skipCerts()); |
|
| 464 | - } |
|
| 465 | - } |
|
| 466 | - return $state; |
|
| 467 | - } |
|
| 449 | + /** |
|
| 450 | + * Apply inhibit any-policy handling for the preparation step. |
|
| 451 | + * |
|
| 452 | + * @param ValidatorState $state |
|
| 453 | + * @param Certificate $cert |
|
| 454 | + * @return ValidatorState |
|
| 455 | + */ |
|
| 456 | + private function _prepareInhibitAnyPolicy(ValidatorState $state, |
|
| 457 | + Certificate $cert): ValidatorState |
|
| 458 | + { |
|
| 459 | + $extensions = $cert->tbsCertificate()->extensions(); |
|
| 460 | + if ($extensions->hasInhibitAnyPolicy()) { |
|
| 461 | + $ext = $extensions->inhibitAnyPolicy(); |
|
| 462 | + if ($ext->skipCerts() < $state->inhibitAnyPolicy()) { |
|
| 463 | + $state = $state->withInhibitAnyPolicy($ext->skipCerts()); |
|
| 464 | + } |
|
| 465 | + } |
|
| 466 | + return $state; |
|
| 467 | + } |
|
| 468 | 468 | |
| 469 | - /** |
|
| 470 | - * Verify maximum certification path length for the preparation step. |
|
| 471 | - * |
|
| 472 | - * @param ValidatorState $state |
|
| 473 | - * @param Certificate $cert |
|
| 474 | - * @throws PathValidationException |
|
| 475 | - * @return ValidatorState |
|
| 476 | - */ |
|
| 477 | - private function _verifyMaxPathLength(ValidatorState $state, |
|
| 478 | - Certificate $cert): ValidatorState |
|
| 479 | - { |
|
| 480 | - if (!$cert->isSelfIssued()) { |
|
| 481 | - if ($state->maxPathLength() <= 0) { |
|
| 482 | - throw new PathValidationException( |
|
| 483 | - "Certification path length exceeded."); |
|
| 484 | - } |
|
| 485 | - $state = $state->withMaxPathLength($state->maxPathLength() - 1); |
|
| 486 | - } |
|
| 487 | - return $state; |
|
| 488 | - } |
|
| 469 | + /** |
|
| 470 | + * Verify maximum certification path length for the preparation step. |
|
| 471 | + * |
|
| 472 | + * @param ValidatorState $state |
|
| 473 | + * @param Certificate $cert |
|
| 474 | + * @throws PathValidationException |
|
| 475 | + * @return ValidatorState |
|
| 476 | + */ |
|
| 477 | + private function _verifyMaxPathLength(ValidatorState $state, |
|
| 478 | + Certificate $cert): ValidatorState |
|
| 479 | + { |
|
| 480 | + if (!$cert->isSelfIssued()) { |
|
| 481 | + if ($state->maxPathLength() <= 0) { |
|
| 482 | + throw new PathValidationException( |
|
| 483 | + "Certification path length exceeded."); |
|
| 484 | + } |
|
| 485 | + $state = $state->withMaxPathLength($state->maxPathLength() - 1); |
|
| 486 | + } |
|
| 487 | + return $state; |
|
| 488 | + } |
|
| 489 | 489 | |
| 490 | - /** |
|
| 491 | - * Check key usage extension for the preparation step. |
|
| 492 | - * |
|
| 493 | - * @param Certificate $cert |
|
| 494 | - * @throws PathValidationException |
|
| 495 | - */ |
|
| 496 | - private function _checkKeyUsage(Certificate $cert) |
|
| 497 | - { |
|
| 498 | - $extensions = $cert->tbsCertificate()->extensions(); |
|
| 499 | - if ($extensions->hasKeyUsage()) { |
|
| 500 | - $ext = $extensions->keyUsage(); |
|
| 501 | - if (!$ext->isKeyCertSign()) { |
|
| 502 | - throw new PathValidationException("keyCertSign usage not set."); |
|
| 503 | - } |
|
| 504 | - } |
|
| 505 | - } |
|
| 490 | + /** |
|
| 491 | + * Check key usage extension for the preparation step. |
|
| 492 | + * |
|
| 493 | + * @param Certificate $cert |
|
| 494 | + * @throws PathValidationException |
|
| 495 | + */ |
|
| 496 | + private function _checkKeyUsage(Certificate $cert) |
|
| 497 | + { |
|
| 498 | + $extensions = $cert->tbsCertificate()->extensions(); |
|
| 499 | + if ($extensions->hasKeyUsage()) { |
|
| 500 | + $ext = $extensions->keyUsage(); |
|
| 501 | + if (!$ext->isKeyCertSign()) { |
|
| 502 | + throw new PathValidationException("keyCertSign usage not set."); |
|
| 503 | + } |
|
| 504 | + } |
|
| 505 | + } |
|
| 506 | 506 | |
| 507 | - /** |
|
| 508 | - * |
|
| 509 | - * @param ValidatorState $state |
|
| 510 | - * @param Certificate $cert |
|
| 511 | - * @return ValidatorState |
|
| 512 | - */ |
|
| 513 | - private function _processNameConstraints(ValidatorState $state, |
|
| 514 | - Certificate $cert): ValidatorState |
|
| 515 | - { |
|
| 516 | - // @todo Implement |
|
| 517 | - return $state; |
|
| 518 | - } |
|
| 507 | + /** |
|
| 508 | + * |
|
| 509 | + * @param ValidatorState $state |
|
| 510 | + * @param Certificate $cert |
|
| 511 | + * @return ValidatorState |
|
| 512 | + */ |
|
| 513 | + private function _processNameConstraints(ValidatorState $state, |
|
| 514 | + Certificate $cert): ValidatorState |
|
| 515 | + { |
|
| 516 | + // @todo Implement |
|
| 517 | + return $state; |
|
| 518 | + } |
|
| 519 | 519 | |
| 520 | - /** |
|
| 521 | - * Process basic constraints extension. |
|
| 522 | - * |
|
| 523 | - * @param Certificate $cert |
|
| 524 | - * @throws PathValidationException |
|
| 525 | - */ |
|
| 526 | - private function _processBasicContraints(Certificate $cert) |
|
| 527 | - { |
|
| 528 | - if ($cert->tbsCertificate()->version() == TBSCertificate::VERSION_3) { |
|
| 529 | - $extensions = $cert->tbsCertificate()->extensions(); |
|
| 530 | - if (!$extensions->hasBasicConstraints()) { |
|
| 531 | - throw new PathValidationException( |
|
| 532 | - "v3 certificate must have basicConstraints extension."); |
|
| 533 | - } |
|
| 534 | - // verify that cA is set to TRUE |
|
| 535 | - if (!$extensions->basicConstraints()->isCA()) { |
|
| 536 | - throw new PathValidationException( |
|
| 537 | - "Certificate is not a CA certificate."); |
|
| 538 | - } |
|
| 539 | - } |
|
| 540 | - } |
|
| 520 | + /** |
|
| 521 | + * Process basic constraints extension. |
|
| 522 | + * |
|
| 523 | + * @param Certificate $cert |
|
| 524 | + * @throws PathValidationException |
|
| 525 | + */ |
|
| 526 | + private function _processBasicContraints(Certificate $cert) |
|
| 527 | + { |
|
| 528 | + if ($cert->tbsCertificate()->version() == TBSCertificate::VERSION_3) { |
|
| 529 | + $extensions = $cert->tbsCertificate()->extensions(); |
|
| 530 | + if (!$extensions->hasBasicConstraints()) { |
|
| 531 | + throw new PathValidationException( |
|
| 532 | + "v3 certificate must have basicConstraints extension."); |
|
| 533 | + } |
|
| 534 | + // verify that cA is set to TRUE |
|
| 535 | + if (!$extensions->basicConstraints()->isCA()) { |
|
| 536 | + throw new PathValidationException( |
|
| 537 | + "Certificate is not a CA certificate."); |
|
| 538 | + } |
|
| 539 | + } |
|
| 540 | + } |
|
| 541 | 541 | |
| 542 | - /** |
|
| 543 | - * Process pathLenConstraint. |
|
| 544 | - * |
|
| 545 | - * @param ValidatorState $state |
|
| 546 | - * @param Certificate $cert |
|
| 547 | - * @return ValidatorState |
|
| 548 | - */ |
|
| 549 | - private function _processPathLengthContraint(ValidatorState $state, |
|
| 550 | - Certificate $cert): ValidatorState |
|
| 551 | - { |
|
| 552 | - $extensions = $cert->tbsCertificate()->extensions(); |
|
| 553 | - if ($extensions->hasBasicConstraints()) { |
|
| 554 | - $ext = $extensions->basicConstraints(); |
|
| 555 | - if ($ext->hasPathLen()) { |
|
| 556 | - if ($ext->pathLen() < $state->maxPathLength()) { |
|
| 557 | - $state = $state->withMaxPathLength($ext->pathLen()); |
|
| 558 | - } |
|
| 559 | - } |
|
| 560 | - } |
|
| 561 | - return $state; |
|
| 562 | - } |
|
| 542 | + /** |
|
| 543 | + * Process pathLenConstraint. |
|
| 544 | + * |
|
| 545 | + * @param ValidatorState $state |
|
| 546 | + * @param Certificate $cert |
|
| 547 | + * @return ValidatorState |
|
| 548 | + */ |
|
| 549 | + private function _processPathLengthContraint(ValidatorState $state, |
|
| 550 | + Certificate $cert): ValidatorState |
|
| 551 | + { |
|
| 552 | + $extensions = $cert->tbsCertificate()->extensions(); |
|
| 553 | + if ($extensions->hasBasicConstraints()) { |
|
| 554 | + $ext = $extensions->basicConstraints(); |
|
| 555 | + if ($ext->hasPathLen()) { |
|
| 556 | + if ($ext->pathLen() < $state->maxPathLength()) { |
|
| 557 | + $state = $state->withMaxPathLength($ext->pathLen()); |
|
| 558 | + } |
|
| 559 | + } |
|
| 560 | + } |
|
| 561 | + return $state; |
|
| 562 | + } |
|
| 563 | 563 | |
| 564 | - /** |
|
| 565 | - * |
|
| 566 | - * @param ValidatorState $state |
|
| 567 | - * @param Certificate $cert |
|
| 568 | - * @return ValidatorState |
|
| 569 | - */ |
|
| 570 | - private function _processExtensions(ValidatorState $state, Certificate $cert): ValidatorState |
|
| 571 | - { |
|
| 572 | - // @todo Implement |
|
| 573 | - return $state; |
|
| 574 | - } |
|
| 564 | + /** |
|
| 565 | + * |
|
| 566 | + * @param ValidatorState $state |
|
| 567 | + * @param Certificate $cert |
|
| 568 | + * @return ValidatorState |
|
| 569 | + */ |
|
| 570 | + private function _processExtensions(ValidatorState $state, Certificate $cert): ValidatorState |
|
| 571 | + { |
|
| 572 | + // @todo Implement |
|
| 573 | + return $state; |
|
| 574 | + } |
|
| 575 | 575 | |
| 576 | - /** |
|
| 577 | - * |
|
| 578 | - * @param ValidatorState $state |
|
| 579 | - * @return ValidatorState |
|
| 580 | - */ |
|
| 581 | - private function _calculatePolicyIntersection(ValidatorState $state): ValidatorState |
|
| 582 | - { |
|
| 583 | - // (i) If the valid_policy_tree is NULL, the intersection is NULL |
|
| 584 | - if (!$state->hasValidPolicyTree()) { |
|
| 585 | - return $state; |
|
| 586 | - } |
|
| 587 | - // (ii) If the valid_policy_tree is not NULL and |
|
| 588 | - // the user-initial-policy-set is any-policy, the intersection |
|
| 589 | - // is the entire valid_policy_tree |
|
| 590 | - $initial_policies = $this->_config->policySet(); |
|
| 591 | - if (in_array(PolicyInformation::OID_ANY_POLICY, $initial_policies)) { |
|
| 592 | - return $state; |
|
| 593 | - } |
|
| 594 | - // (iii) If the valid_policy_tree is not NULL and the |
|
| 595 | - // user-initial-policy-set is not any-policy, calculate |
|
| 596 | - // the intersection of the valid_policy_tree and the |
|
| 597 | - // user-initial-policy-set as follows |
|
| 598 | - return $state->validPolicyTree()->calculateIntersection($state, |
|
| 599 | - $initial_policies); |
|
| 600 | - } |
|
| 576 | + /** |
|
| 577 | + * |
|
| 578 | + * @param ValidatorState $state |
|
| 579 | + * @return ValidatorState |
|
| 580 | + */ |
|
| 581 | + private function _calculatePolicyIntersection(ValidatorState $state): ValidatorState |
|
| 582 | + { |
|
| 583 | + // (i) If the valid_policy_tree is NULL, the intersection is NULL |
|
| 584 | + if (!$state->hasValidPolicyTree()) { |
|
| 585 | + return $state; |
|
| 586 | + } |
|
| 587 | + // (ii) If the valid_policy_tree is not NULL and |
|
| 588 | + // the user-initial-policy-set is any-policy, the intersection |
|
| 589 | + // is the entire valid_policy_tree |
|
| 590 | + $initial_policies = $this->_config->policySet(); |
|
| 591 | + if (in_array(PolicyInformation::OID_ANY_POLICY, $initial_policies)) { |
|
| 592 | + return $state; |
|
| 593 | + } |
|
| 594 | + // (iii) If the valid_policy_tree is not NULL and the |
|
| 595 | + // user-initial-policy-set is not any-policy, calculate |
|
| 596 | + // the intersection of the valid_policy_tree and the |
|
| 597 | + // user-initial-policy-set as follows |
|
| 598 | + return $state->validPolicyTree()->calculateIntersection($state, |
|
| 599 | + $initial_policies); |
|
| 600 | + } |
|
| 601 | 601 | } |