| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace Punkstar\Ssl; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | class Reader | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |     const DEFAULT_CONNECTION_TIMEOUT = 5; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |     const OPT_CONNECTION_TIMEOUT = 'connection_timeout'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |      * Connect to a URL and retrieve the SSL certificate. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |      * Available options: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |      *     - connection_timeout: Timeout when connection to the URL, specified in seconds. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |      * @param $url | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |      * @param array $options | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |      * @return Certificate | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |      * @throws Exception | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 22 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 23 | 1 |  |     public function readFromUrl($url, $options = []) | 
            
                                                                        
                            
            
                                    
            
            
                | 24 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 25 | 1 |  |         $urlHost = parse_url($url, PHP_URL_HOST); | 
            
                                                                        
                            
            
                                    
            
            
                | 26 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 27 | 1 |  |         if ($urlHost === null) { | 
            
                                                                        
                            
            
                                    
            
            
                | 28 |  |  |             $urlHost = $url; | 
            
                                                                        
                            
            
                                    
            
            
                | 29 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 30 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 31 | 1 |  |         $options = $this->prepareReadFromUrlOptions($options); | 
            
                                                                        
                            
            
                                    
            
            
                | 32 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 33 | 1 |  |         $streamContext = stream_context_create(array( | 
            
                                                                        
                            
            
                                    
            
            
                | 34 |  |  |             "ssl" => array( | 
            
                                                                        
                            
            
                                    
            
            
                | 35 |  |  |                 "capture_peer_cert" => TRUE, | 
            
                                                                        
                            
            
                                    
            
            
                | 36 |  |  |                 "verify_peer" => FALSE, | 
            
                                                                        
                            
            
                                    
            
            
                | 37 |  |  |                 "verify_peer_name" => FALSE | 
            
                                                                        
                            
            
                                    
            
            
                | 38 |  |  |             ) | 
            
                                                                        
                            
            
                                    
            
            
                | 39 | 1 |  |         )); | 
            
                                                                        
                            
            
                                    
            
            
                | 40 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 41 | 1 |  |         $stream = @stream_socket_client("ssl://" . $urlHost . ":443", $errorNumber, $errorString, $options[self::OPT_CONNECTION_TIMEOUT], STREAM_CLIENT_CONNECT, $streamContext); | 
            
                                                                        
                            
            
                                    
            
            
                | 42 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 43 | 1 |  |         if ($stream) { | 
            
                                                                        
                            
            
                                    
            
            
                | 44 | 1 |  |             $streamParams = stream_context_get_params($stream); | 
            
                                                                        
                            
            
                                    
            
            
                | 45 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 46 | 1 |  |             $certResource = $streamParams['options']['ssl']['peer_certificate']; | 
            
                                                                        
                            
            
                                    
            
            
                | 47 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 48 | 1 |  |             return new Certificate($this->certResourceToString($certResource)); | 
            
                                                                        
                            
            
                                    
            
            
                | 49 |  |  |         } else { | 
            
                                                                        
                            
            
                                    
            
            
                | 50 |  |  |             throw new Exception(sprintf("Unable to connect to %s", $urlHost), Exception::CONNECTION_PROBLEM); | 
            
                                                                        
                            
            
                                    
            
            
                | 51 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 52 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |      | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |      * @param $file | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |      * @return Certificate | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |      * @throws Exception | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 | 8 |  |     public function readFromFile($file) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 | 8 |  |         if (!file_exists($file)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 | 1 |  |             throw new Exception(sprintf("File '%s' does not exist", $file), Exception::FILE_NOT_FOUND); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 | 7 |  |         return new Certificate(file_get_contents($file)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |      * @param $certResource | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |      * @return string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 | 1 |  |     protected function certResourceToString($certResource) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 | 1 |  |         $output = null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 | 1 |  |         openssl_x509_export($certResource, $output); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 | 1 |  |         return $output; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |      * @param $options | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |      * @return mixed | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 | 1 |  |     protected function prepareReadFromUrlOptions($options) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 | 1 |  |         if (!isset($options[self::OPT_CONNECTION_TIMEOUT])) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 | 1 |  |             $options[self::OPT_CONNECTION_TIMEOUT] = self::DEFAULT_CONNECTION_TIMEOUT; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 | 1 |  |         return $options; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 93 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 94 |  |  |  |