Completed
Push — develop ( 4146c2...e30e33 )
by Chris
35:42
created

ComodoDecodeCSR::checkInstalled()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 20
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 3.009

Importance

Changes 13
Bugs 0 Features 5
Metric Value
c 13
b 0
f 5
dl 0
loc 20
ccs 9
cts 10
cp 0.9
rs 9.4285
cc 3
eloc 12
nc 3
nop 0
crap 3.009
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
use GuzzleHttp\Psr7\Response;
15
16
class ComodoDecodeCSR
17
{
18
    use Traits\GetSetUnset;
19
20
    protected $MD5;
21
    protected $SHA1;
22
    protected $Endpoint = "https://secure.comodo.net/products/!decodeCSR";
23
    protected $CSR;
24
    /**
25
     * An array of warnings that can be show after the test
26
     * @var array
27
     */
28
    protected $warnings = [];
29
    protected $Form = [
30
        'responseFormat' => 'N',
31
        'showErrorCodes' => 'N',
32
        'showErrorMessages' => 'N',
33
        'showFieldNames' => 'N',
34
        'showEmptyFields' => 'N',
35
        'showCN' => 'N',
36
        'showAddress' => 'N',
37
        'showPublicKey' => 'N',
38
        'showKeySize' => 'N',
39
        'showSANDNSNames' => 'Y',
40
        'showCSR' => 'N',
41
        'showCSRHashes' => 'Y',
42
        'showSignatureAlgorithm' => 'N',
43
        'product' => '',
44
        'countryNameType' => 'TWOCHAR'
45
    ];
46
    private $request;
47
48 4
    public function getCN()
49
    {
50 4
        $CSRInfo = $this->decodeCSR();
51 4
        return $CSRInfo['subject']['CN'];
52
    }
53
54 15
    public function setCSR($csr)
55
    {
56 15
        $this->CSR = $csr;
57
        //Check that this is a valid CSR
58 15
        $this->decodeCSR();
59 12
        $this->Form['csr'] = $csr;
60 12
    }
61
62 12
    protected function addWarning($code, $message)
63
    {
64 1
        $this->warnings[] = [
65 12
            $code => $message
66 1
        ];
67 1
    }
68
69 12
    public function fetchHashes()
70
    {
71 12
        $client = new Client();
72
73 12
        $this->request = $client->request('POST', $this->getEndpoint(), [
74 12
            'form_params' => $this->Form
75 12
        ]);
76
77 12
        return $this->processResponse();
78
    }
79
80 3
    public function checkInstalled()
81
    {
82
        try {
83 3
            $domain = $this->getCN();
84 3
        } catch (\Exception $e) {
85
            return false;
86
        }
87
        //We do most of our DVC over http:// unless the site is fully SSL
88 3
        $url = 'http://' . $domain . "/" . $this->getMD5() . '.txt';
89
90 3
        $client = new Client(['allow_redirects' => false, 'verify' => false]);
91
92
        try {
93 3
            $response = $client->request('GET', $url);
94 3
        } catch (ClientException $e) {
95 1
            return false;
96
        }
97
98 2
        return $this->checkDVC($response);
0 ignored issues
show
Compatibility introduced by
$response of type object<Psr\Http\Message\ResponseInterface> is not a sub-type of object<GuzzleHttp\Psr7\Response>. It seems like you assume a concrete implementation of the interface Psr\Http\Message\ResponseInterface to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
99
    }
100
101 11
    public function generateDVC()
102
    {
103 11
        $DVC = $this->getSHA1() . "\n";
104 11
        $DVC .= "comodoca.com\n";
105
106 11
        return $DVC;
107
    }
108
109
    /**
110
     *
111
     * @param  GuzzleHttp\Psr7\Response $response
112
     * @return bool
113
     */
114 13
    public function checkDVC(Response $response)
115
    {
116 10
        $body = $response->getBody() . '';
117 10
        $DVC = $this->generateDVC();
118
119
        //Check if we received a 301 or 302 redirect
120 10
        if ($response->getStatusCode() === 301 || $response->getStatusCode() == 301) {
121 1
            $message = 'There is a redirect inplace. Make sure that its not redirecting to https://';
122 1
            $this->addWarning(301, $message);
123
124 1
            return false;
125
        }
126
127
        //If the response matches the DVC value return true
128 13
        if ($body === $DVC) {
129 3
            return true;
130
        }
131
132
        //Check if last 2 characters are new lines
133 6
        if (substr($body, -2) === "\n\n") {
134 1
            $body = substr($body, 0, -2) . "\n";
135 1
        }
136
137
        //Check if last character is not a new line
138 6
        if (substr($body, -1) !== "\n") {
139
            //Add said new line
140 3
            $body = $body . "\n";
141 3
        }
142
143
        //Check it again
144 6
        if ($body === $DVC) {
145 2
            return true;
146
        }
147
148 4
        return false;
149
    }
150
151 15
    private function decodeCSR()
152
    {
153
        try {
154 15
            $data = openssl_csr_get_public_key($this->getCSR());
155 15
            $details = openssl_pkey_get_details($data);
156 12
            $key = $details['key'];
157 12
            $subject = openssl_csr_get_subject($this->getCSR());
158 15
        } catch (\Exception $e) {
159 3
            throw new Exception("Invalid CSR");
160
        }
161
162
        return array(
163 12
            "subject" => $subject,
164
            "key" => $key
165 12
        );
166
    }
167
168 12
    private function processResponse()
169
    {
170 12
        $response = $this->request->getBody();
171 12
        $lines = explode("\n", $response);
172 12
        $data = array();
173
        //Remove the first array as we don't need the SAN and can cause problems
174
        //with a multi domain SAN
175 12
        unset($lines[0]);
176
177 12
        foreach ($lines as $v) {
178 12
            if (!empty($v)) {
179 12
                $value = explode("=", $v);
180 12
                $data[$value[0]] = $value[1];
181 12
            }
182 12
        }
183
184 12
        $this->setMD5($data["md5"]);
185 12
        $this->setSHA1($data["sha1"]);
186
187 12
        return $data ? $data : false;
188
    }
189
}
190