| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  |  * @author     Chris Hilsdon <[email protected]> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  * @package    ComodoDecodeCSR | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  * @copyright  2016 Xigen | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  * @license    GNU General Public License v3 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  * @link       https://github.com/XigenChris/ComodoDecodeCSR | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | namespace Xigen; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | use GuzzleHttp\Client; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | use GuzzleHttp\Exception\ClientException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | class ComodoDecodeCSR | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |     use Traits\ComodoDecodeCSR\Getters; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |     use Traits\ComodoDecodeCSR\Setters; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |     protected $MD5; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |     protected $SHA1; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |     protected $Endpoint = "https://secure.comodo.net/products/!decodeCSR"; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |     protected $CSR; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |     protected $Form = [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |         'responseFormat' => 'N', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |         'showErrorCodes' => 'N', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |         'showErrorMessages' => 'N', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |         'showFieldNames' => 'N', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |         'showEmptyFields' => 'N', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |         'showCN' => 'N', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |         'showAddress' => 'N', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |         'showPublicKey' => 'N', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |         'showKeySize' => 'N', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |         'showSANDNSNames' => 'Y', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |         'showCSR' => 'N', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |         'showCSRHashes' => 'Y', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |         'showSignatureAlgorithm' => 'N', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |         'product' => '', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |         'countryNameType' => 'TWOCHAR' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |     ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |     private $request; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 | 9 |  |     public function fetchHashes() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 | 9 |  |         $client = new Client(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 | 9 |  |         $this->request = $client->request('POST', $this->getEndpoint(), [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 | 9 |  |             'form_params' => $this->Form | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 | 9 |  |         ]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 | 9 |  |         return $this->processResponse(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 | 9 |  |     public function checkInstalled() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 | 2 |  |         $CSRInfo = $this->decodeCSR(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 | 2 |  |         $domain = $CSRInfo['subject']['CN']; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 | 2 |  |         $URL = 'http://' . $domain . "/" . $this->getmd5() . '.txt'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 | 2 |  |         $client = new Client(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |         try { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 | 2 |  |             $request = $client->request('GET', $URL); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 | 2 |  |         } catch (ClientException $e) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 | 9 |  |             return false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 | 1 |  |         $responce = "" . $request->getBody(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 | 1 |  |         return ($responce === $this->generateDVC()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 | 6 |  |     public function generateDVC() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 | 6 |  |         $DVC = $this->getSHA1() . "\n"; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 | 6 |  |         $DVC .= "comodoca.com\n"; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 | 6 |  |         return $DVC; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 | 5 |  |     public function checkDVC($response) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 | 5 |  |         $DVC = $this->generateDVC(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |         //If the response matches the DVC value return true | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 | 5 |  |         if ($response === $DVC) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 | 1 |  |             return true; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |         //Check if last character is not a new line | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 | 4 |  |         if (substr($response, -1) !== "\n") { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |             //Add said new line | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 | 2 |  |             $response = $response . "\n"; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 | 2 |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |         //Check it again | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 | 4 |  |         if ($response === $DVC) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 | 1 |  |             return true; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 | 3 |  |         return false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 | 2 |  |     private function decodeCSR() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 | 2 |  |         $data = openssl_csr_get_public_key($this->getCSR()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 | 2 |  |         $details = openssl_pkey_get_details($data); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 | 2 |  |         $key = $details['key']; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 | 2 |  |         $subject = openssl_csr_get_subject($this->getCSR()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |         return array( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 | 2 |  |             "subject" => $subject, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |             "key" => $key | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 | 2 |  |         ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 115 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 116 | 9 |  |     private function processResponse() | 
            
                                                        
            
                                    
            
            
                | 117 |  |  |     { | 
            
                                                        
            
                                    
            
            
                | 118 | 9 |  |         $response = $this->request->getBody(); | 
            
                                                        
            
                                    
            
            
                | 119 | 9 |  |         $lines = explode("\n", $response); | 
            
                                                        
            
                                    
            
            
                | 120 | 9 |  |         $data = array(); | 
            
                                                        
            
                                    
            
            
                | 121 |  |  |         //Remove the first array as we don't need the SAN and can cause problems | 
            
                                                        
            
                                    
            
            
                | 122 |  |  |         //with a multi domain SAN | 
            
                                                        
            
                                    
            
            
                | 123 | 9 |  |         unset($lines[0]); | 
            
                                                        
            
                                    
            
            
                | 124 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 125 | 9 |  |         foreach ($lines as $v) { | 
            
                                                        
            
                                    
            
            
                | 126 | 9 |  |             if (!empty($v)) { | 
            
                                                        
            
                                    
            
            
                | 127 | 9 |  |                 $value = explode("=", $v); | 
            
                                                        
            
                                    
            
            
                | 128 | 9 |  |                 $data[$value[0]] = $value[1]; | 
            
                                                        
            
                                    
            
            
                | 129 | 9 |  |             } | 
            
                                                        
            
                                    
            
            
                | 130 | 9 |  |         } | 
            
                                                        
            
                                    
            
            
                | 131 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 132 | 9 |  |         $this->setMD5($data["md5"]); | 
            
                                                        
            
                                    
            
            
                | 133 | 9 |  |         $this->setSHA1($data["sha1"]); | 
            
                                                        
            
                                    
            
            
                | 134 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 135 | 9 |  |         return $data ? $data : false; | 
            
                                                        
            
                                    
            
            
                | 136 |  |  |     } | 
            
                                                        
            
                                    
            
            
                | 137 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 138 |  |  |  |