1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @author Chris Hilsdon <[email protected]> |
4
|
|
|
*/ |
5
|
|
|
namespace Cloudflare; |
6
|
|
|
|
7
|
|
|
class SSL extends Base |
8
|
|
|
{ |
9
|
|
|
private $config = [ |
10
|
|
|
'digest_alg' => 'sha512', |
11
|
|
|
'private_key_bits' => 4096, |
12
|
|
|
'private_key_type' => OPENSSL_KEYTYPE_RSA, |
13
|
|
|
]; |
14
|
|
|
|
15
|
|
|
protected $baseUrl = 'certificates'; |
16
|
|
|
|
17
|
|
|
protected $zone; |
18
|
|
|
|
19
|
|
|
public function __construct(Cloudflare $cloudflare, $zone) |
20
|
|
|
{ |
21
|
|
|
parent::__construct($cloudflare); |
22
|
|
|
$cfzone = new Zone($this->CF); |
23
|
|
|
$zone = $cfzone->getDetails($zone); |
24
|
|
|
$this->zone = $zone; |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
private function getZoneDomain() |
28
|
|
|
{ |
29
|
|
|
return $this->zone->result->name; |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
private function getZoneId() |
33
|
|
|
{ |
34
|
|
|
return $this->zone->result->id; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
View Code Duplication |
public function listOriginCerts() |
|
|
|
|
38
|
|
|
{ |
39
|
|
|
$id = $this->getZoneId(); |
40
|
|
|
$this->makeRequest($this->baseUrl . '?zone_id=' . $id, 'GET', null, true); |
41
|
|
|
|
42
|
|
|
$response = $this->getResponse(); |
43
|
|
|
|
44
|
|
|
if ($response->success !== true) { |
45
|
|
|
return false; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
return $response->result; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
public function revokeCert($id) |
52
|
|
|
{ |
53
|
|
|
$this->makeRequest($this->baseUrl . '/' . $id, 'DELETE', null, true); |
54
|
|
|
$response = $this->getResponse(); |
55
|
|
|
|
56
|
|
|
return $response->success; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
public function getNewOriginCert() |
60
|
|
|
{ |
61
|
|
|
$domain = $this->getZoneDomain(); |
62
|
|
|
$csr = $this->createCSR($domain); |
63
|
|
|
$cert = $this->requestNewOriginCert($csr, $domain); |
64
|
|
|
$csr['cert'] = $cert; |
65
|
|
|
|
66
|
|
|
return $csr; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
private function requestNewOriginCert($csr, $domain) |
70
|
|
|
{ |
71
|
|
|
$data = [ |
72
|
|
|
'hostnames' => [ |
73
|
|
|
$domain, |
74
|
|
|
'*.' . $domain |
75
|
|
|
], |
76
|
|
|
'requested_validity' => 5475, |
77
|
|
|
'request_type' => 'origin-rsa', |
78
|
|
|
'csr' => $csr['csr'] |
79
|
|
|
]; |
80
|
|
|
$this->makeRequest('certificates', 'POST', $data, true); |
81
|
|
|
|
82
|
|
|
$response = $this->getResponse(); |
83
|
|
|
|
84
|
|
|
if ($response->success !== true) { |
85
|
|
|
return false; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
return [ |
89
|
|
|
'id' => $response->result->id, |
90
|
|
|
'certificate' => $response->result->certificate |
91
|
|
|
]; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
private function createCSR($domain) |
95
|
|
|
{ |
96
|
|
|
$this->domain = $domain; |
|
|
|
|
97
|
|
|
$keys = $this->generateKeys(); |
98
|
|
|
|
99
|
|
|
$csr = openssl_csr_new(['commonName' => $domain], $privkey, $this->config); |
100
|
|
|
openssl_csr_export($csr, $csrString); |
101
|
|
|
|
102
|
|
|
$keys['csr'] = $csrString;; |
103
|
|
|
|
104
|
|
|
return $keys; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
private function generateKeys() |
108
|
|
|
{ |
109
|
|
|
// Create the private and public key |
110
|
|
|
$res = openssl_pkey_new($this->config); |
111
|
|
|
|
112
|
|
|
// Extract the private key from $res to $privKey |
113
|
|
|
openssl_pkey_export($res, $privKey); |
114
|
|
|
|
115
|
|
|
// Extract the public key from $res to $pubKey |
116
|
|
|
$pubKey = openssl_pkey_get_details($res); |
117
|
|
|
$pubKey = $pubKey["key"]; |
118
|
|
|
|
119
|
|
|
return [ |
120
|
|
|
'pubKey' => $pubKey, |
121
|
|
|
'privKey' => $privKey |
122
|
|
|
]; |
123
|
|
|
} |
124
|
|
|
} |
125
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.