| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace X509\CertificationPath; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use CryptoUtil\Crypto\Crypto; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use X509\Certificate\Certificate; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use X509\Certificate\CertificateBundle; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use X509\CertificationPath\Exception\PathValidationException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | use X509\CertificationPath\PathBuilding\CertificationPathBuilder; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | use X509\CertificationPath\PathValidation\PathValidationConfig; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | use X509\CertificationPath\PathValidation\PathValidationResult; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | use X509\CertificationPath\PathValidation\PathValidator; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |  * Implements certification path structure. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |  * Certification path is a list of certificates from the trust anchor to | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |  * the end entity certificate, possibly spanning over multiple intermediate | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |  * certificates. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |  * @link https://tools.ietf.org/html/rfc5280#section-3.2 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  | class CertificationPath implements \Countable, \IteratorAggregate | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  | 	 * Certification path. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  | 	 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  | 	 * @var Certificate[] $_certificates | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  | 	protected $_certificates; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  | 	 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  | 	 * Constructor. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  | 	 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  | 	 * @param Certificate ...$certificates Certificates from the trust anchor | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  | 	 *        to the target end-entity certificate | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 | 28 |  | 	public function __construct(Certificate ...$certificates) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 | 28 |  | 		$this->_certificates = $certificates; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 | 28 |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  | 	 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  | 	 * Build certification path to given target. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  | 	 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  | 	 * @param Certificate $target Target end-entity certificate | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  | 	 * @param CertificateBundle $trust_anchors List of trust anchors | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  | 	 * @param CertificateBundle|null $intermediate Optional intermediate | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  | 	 *        certificates | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  | 	 * @return self | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 | 2 |  | 	public static function toTarget(Certificate $target,  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  | 			CertificateBundle $trust_anchors,  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  | 			CertificateBundle $intermediate = null) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 | 2 |  | 		$builder = new CertificationPathBuilder($trust_anchors); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 | 2 |  | 		return $builder->shortestPathToTarget($target, $intermediate); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  | 	 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  | 	 * Build certification path from given trust anchor to target certificate, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  | 	 * using intermediate certificates from given bundle. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  | 	 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  | 	 * @param Certificate $trust_anchor Trust anchor certificate | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  | 	 * @param Certificate $target Target end-entity certificate | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  | 	 * @param CertificateBundle|null $intermediate Optional intermediate | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  | 	 *        certificates | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  | 	 * @return self | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 | 2 |  | 	public static function fromTrustAnchorToTarget(Certificate $trust_anchor,  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  | 			Certificate $target, CertificateBundle $intermediate = null) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 | 2 |  | 		return self::toTarget($target, new CertificateBundle($trust_anchor),  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 | 2 |  | 			$intermediate); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  | 	 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  | 	 * Get certificates. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  | 	 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  | 	 * @return Certificate[] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 | 5 |  | 	public function certificates() { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 | 5 |  | 		return $this->_certificates; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  | 	 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  | 	 * Get the trust anchor certificate from the path. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  | 	 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  | 	 * @throws \LogicException If path is empty | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  | 	 * @return Certificate | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 | 2 |  | 	public function trustAnchorCertificate() { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 | 2 |  | 		if (!count($this->_certificates)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 | 1 |  | 			throw new \LogicException("No certificates."); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 | 1 |  | 		return $this->_certificates[0]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  | 	 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  | 	 * Get the end-entity certificate from the path. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  | 	 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  | 	 * @throws \LogicException If path is empty | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  | 	 * @return Certificate | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 102 |  |  | 	 */ | 
            
                                                                        
                            
            
                                    
            
            
                | 103 | 2 |  | 	public function endEntityCertificate() { | 
            
                                                                        
                            
            
                                    
            
            
                | 104 | 2 |  | 		if (!count($this->_certificates)) { | 
            
                                                                        
                            
            
                                    
            
            
                | 105 | 1 |  | 			throw new \LogicException("No certificates."); | 
            
                                                                        
                            
            
                                    
            
            
                | 106 |  |  | 		} | 
            
                                                                        
                            
            
                                    
            
            
                | 107 | 1 |  | 		return $this->_certificates[count($this->_certificates) - 1]; | 
            
                                                                        
                            
            
                                    
            
            
                | 108 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  | 	 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  | 	 * Check whether certification path starts with one ore more given | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  | 	 * certificates in parameter order. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  | 	 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  | 	 * @param Certificate ...$certs Certificates | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  | 	 * @return true | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 | 5 |  | 	public function startsWith(Certificate ...$certs) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 | 5 |  | 		$n = count($certs); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 | 5 |  | 		if ($n > count($this->_certificates)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 | 1 |  | 			return false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 | 4 |  | 		for ($i = 0; $i < $n; ++$i) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 | 4 |  | 			if (!$certs[$i]->equals($this->_certificates[$i])) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 | 1 |  | 				return false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  | 			} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 | 3 |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 | 3 |  | 		return true; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  | 	 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  | 	 * Validate certification path. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  | 	 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 |  |  | 	 * @param Crypto $crypto | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 |  |  | 	 * @param PathValidationConfig $config | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 |  |  | 	 * @throws PathValidationException | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 |  |  | 	 * @return PathValidationResult | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 | 35 |  | 	public function validate(Crypto $crypto, PathValidationConfig $config) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 | 35 |  | 		$validator = new PathValidator($crypto, $config, ...$this->_certificates); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 140 | 35 |  | 		return $validator->validate(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 141 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 142 |  |  | 	 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 143 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 144 |  |  | 	 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 145 |  |  | 	 * @see Countable::count() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 146 |  |  | 	 * @return int | 
            
                                                                                                            
                            
            
                                    
            
            
                | 147 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 148 | 18 |  | 	public function count() { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 149 | 18 |  | 		return count($this->_certificates); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 150 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 151 |  |  | 	 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 152 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 153 |  |  | 	 * Get iterator for certificates. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 154 |  |  | 	 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 155 |  |  | 	 * @see IteratorAggregate::getIterator() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 156 |  |  | 	 * @return \ArrayIterator | 
            
                                                                                                            
                            
            
                                    
            
            
                | 157 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 158 | 1 |  | 	public function getIterator() { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 159 | 1 |  | 		return new \ArrayIterator($this->_certificates); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 160 |  |  | 	} | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 161 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 162 |  |  |  |