Passed
Branch develop (6f73d8)
by Chris
02:31
created

ComodoDecodeCSR::checkInstalled()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 17
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 2.003

Importance

Changes 4
Bugs 0 Features 2
Metric Value
c 4
b 0
f 2
dl 0
loc 17
ccs 10
cts 11
cp 0.9091
rs 9.4285
cc 2
eloc 11
nc 2
nop 0
crap 2.003
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
14
class ComodoDecodeCSR
15
{
16
    use Traits\ComodoDecodeCSR\Getters;
17
    use Traits\ComodoDecodeCSR\Setters;
18
19
    protected $MD5;
20
    protected $SHA1;
21
    protected $Endpoint = "https://secure.comodo.net/products/!decodeCSR";
22
    protected $CSR;
23
    protected $Form = [
24
        'responseFormat' => 'N',
25
        'showErrorCodes' => 'N',
26
        'showErrorMessages' => 'N',
27
        'showFieldNames' => 'N',
28
        'showEmptyFields' => 'N',
29
        'showCN' => 'N',
30
        'showAddress' => 'N',
31
        'showPublicKey' => 'N',
32
        'showKeySize' => 'N',
33
        'showSANDNSNames' => 'Y',
34
        'showCSR' => 'N',
35
        'showCSRHashes' => 'Y',
36
        'showSignatureAlgorithm' => 'N',
37
        'product' => '',
38
        'countryNameType' => 'TWOCHAR'
39
    ];
40
    private $request;
41
42 8
    public function fetchHashes()
43
    {
44 8
        $client = new Client();
45
46 8
        $this->request = $client->request('POST', $this->getEndpoint(), [
47 8
            'form_params' => $this->Form
48 8
        ]);
49
50 8
        return $this->processResponce();
51
    }
52
53 8
    public function checkInstalled()
54
    {
55 1
        $CSRInfo = $this->decodeCSR();
56 1
        $domain = $CSRInfo['subject']['CN'];
57 1
        $URL = 'http://' . $domain . "/" . $this->getmd5() . '.txt';
58
59 1
        $client = new Client();
60
61
        try {
62 1
            $request = $client->request('GET', $URL);
63 1
        } catch (ClientException $e) {
0 ignored issues
show
Bug introduced by
The class Xigen\ClientException does not exist. Did you forget a USE statement, or did you not list all dependencies?

Scrutinizer analyzes your composer.json/composer.lock file if available to determine the classes, and functions that are defined by your dependencies.

It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.

Loading history...
64
            return false;
65 8
        }
66
67 1
        $responce = "" . $request->getBody();
68 1
        return $this->checkDVC($responce);
69
    }
70
71 6
    public function generateDVC()
72
    {
73 6
        $DVC = $this->getSHA1() . "\n";
74 6
        $DVC .= "comodoca.com\n";
75
76 6
        return $DVC;
77
    }
78
79 6
    public function checkDVC($responce)
80
    {
81 6
        $DVC = $this->generateDVC();
82
83
        //If the responce matches the DVC value return true
84 6
        if($responce === $DVC){
85 2
            return true;
86
        }
87
88
        //Check if last character is not a new line
89 4
        if (substr($responce, -1) != "\n")
90 4
        {
91
            //Add said new line
92 2
            $responce = $responce . "\n";
93 2
        }
94
95
        //Check it again
96 4
        if($responce === $DVC){
97 1
            return true;
98
        }
99
100 3
        return false;
101
    }
102
103 1
    private function decodeCSR()
104
    {
105 1
        $data = openssl_csr_get_public_key($this->getCSR());
106 1
        $details = openssl_pkey_get_details($data);
107 1
        $key = $details['key'];
108 1
        $subject = openssl_csr_get_subject($this->getCSR());
109
110
        return array(
111 1
            "subject" => $subject,
112
            "key" => $key
113 1
        );
114
    }
115
116 8
    private function processResponce()
117
    {
118 8
        $responce = $this->request->getBody();
119 8
        $lines = explode("\n", $responce);
120 8
        $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 8
        unset($lines[0]);
124
125 8
        foreach ($lines as $v) {
126 8
            if (!empty($v)) {
127 8
                $value = explode("=", $v);
128 8
                $data[$value[0]] = $value[1];
129 8
            }
130 8
        }
131
132 8
        $this->setMD5($data["md5"]);
133 8
        $this->setSHA1($data["sha1"]);
134
135 8
        return $data ? $data : false;
136
    }
137
}
138