This repository was archived by the owner on May 15, 2020. It is now read-only.
Repository navigation
Expand file tree
/
Copy pathstartssl.class.php
More file actions
201 lines (180 loc) · 7.77 KB
/
Copy pathstartssl.class.php
File metadata and controls
201 lines (180 loc) · 7.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
<?php
/**
* Project: StartSSL API
* By: D. van Gorkum <djvg@djvg.net>
* Date: 29-Jan-16
* Time: 22:00
*/
class StartSSL {
// Change me * no need to really change this because StartSSL will override everything *
const OPENSSL_C = "NL";
const OPENSSL_ST = "Noord-Holland";
const OPENSSL_L = "Amsterdam";
const OPENSSL_O = "Default organization";
const OPENSSL_OU = "Development";
const VERSION = 1.0;
private $cookie = NULL;
private $baseUrl = "https://startssl.com/";
private $authUrl = "https://auth.startssl.com/";
private $existingCerts = array();
private $validatedDomains = array();
private $privateKey = NULL;
private $csr = NULL;
private $logging = false;
// Options
private $opensslSettings = array("digest_alg" => "sha512", "private_key_bits" => 4096, "private_key_type" => OPENSSL_KEYTYPE_RSA);
private $defaultDn = array("countryName" => self::OPENSSL_C,"stateOrProvinceName" => self::OPENSSL_ST,"localityName" => self::OPENSSL_L,"organizationName" => self::OPENSSL_O,"organizationalUnitName" => self::OPENSSL_OU);
function __construct($clientcrt, $clientkey, $clientpw = NULL, $logging = false) {
if(is_bool($logging)) $this->logging = $logging;
if(!openssl_pkey_get_private((is_file($clientkey) ? "file://".$clientkey : $clientkey), $clientpw))
$this->log("Invalid client private key.", true);
if(!openssl_pkey_get_public((is_file($clientcrt) ? "file://".$clientcrt : $clientcrt)))
$this->log("Invalid client public key.", true);
$this->log("Certificate / key looks valid.");
$handle = curl_init();
curl_setopt($handle, CURLOPT_RETURNTRANSFER, true);
curl_setopt($handle, CURLOPT_HEADER, true);
curl_setopt($handle, CURLOPT_USERAGENT, sprintf("StartSSL-PHP-API/%s", self::VERSION));
curl_setopt($handle, CURLOPT_URL, $this->authUrl);
curl_setopt($handle, CURLOPT_SSLCERT, $clientcrt);
curl_setopt($handle, CURLOPT_SSLKEY, $clientkey);
if(!is_null($clientpw))
curl_setopt($handle, CURLOPT_SSLKEYPASSWD, $clientpw);
$this->log("Authenticating...");
$result = curl_exec($handle);
preg_match('/^Set-Cookie: (MyStartSSLCookie=.*)$/m', $result, $matches);
if(isset($matches[1])) {
$this->cookie = $matches[1];
$this->log("User authenticated.");
}else
$this->log("Unable to authenticate. Check certificate/key.", true);
}
function request($path) {
$handle = curl_init();
curl_setopt($handle, CURLOPT_RETURNTRANSFER, true);
curl_setopt($handle, CURLOPT_USERAGENT, sprintf("StartSSL-PHP-API/%s", self::VERSION));
curl_setopt($handle, CURLOPT_URL, $this->baseUrl.$path);
curl_setopt($handle, CURLOPT_COOKIE, $this->cookie);
$this->log(sprintf("Send GET to %s.", $path));
$result = curl_exec($handle);
return ($result === false ? false : $result);
}
function post($path, $data) {
$handle = curl_init();
curl_setopt($handle, CURLOPT_RETURNTRANSFER, true);
curl_setopt($handle, CURLOPT_USERAGENT, sprintf("StartSSL-PHP-API/%s", self::VERSION));
curl_setopt($handle, CURLOPT_URL, $this->baseUrl.$path);
curl_setopt($handle, CURLOPT_COOKIE, $this->cookie);
curl_setopt($handle, CURLOPT_POST, 1);
curl_setopt($handle, CURLOPT_POSTFIELDS, $data);
$this->log(sprintf("Send POST to %s.", $path));
$result = curl_exec($handle);
return ($result === false ? false : $result);
}
function getCertificates() {
$this->log("Getting all certificates.");
$certs = json_decode($this->request("/ControlPanel/AjaxRequestGetSSLCert"));
if($certs !== FALSE)
foreach($certs as $cert)
$this->existingCerts[$cert->CommonName]['id'] = $cert->_id;
else
return false;
return $this->existingCerts;
}
function getValidatedDomains() {
$this->log("Getting all validated domains.");
$domains = json_decode($this->request("/ControlPanel/AjaxRequestGetAllDomainValis"));
if($domains !== FALSE)
foreach($domains as $domain)
if($domain->Verified === "y" && !in_array($domain->Domain, $this->validatedDomains))
$this->validatedDomains[] = $domain->Domain;
else
return false;
return $this->validatedDomains;
}
function getCertificatePem($commonName) {
$this->log(sprintf("Getting PEM certificate for CN %s.", $commonName));
if(empty($this->existingCerts))
$this->getCertificates();
if(!array_key_exists($commonName, $this->existingCerts)) {
$this->log("No signed certificate found with this CN.");
return false;
}
$cert = $this->request(sprintf("/CertList/WriteCert?orderId=%s", $this->existingCerts[$commonName]['id']));
return $cert;
}
function getCertificateZip($commonName) {
$this->log(sprintf("Getting ZIP certificate for CN %s.", $commonName));
if(empty($this->existingCerts))
$this->getCertificates();
if(!array_key_exists($commonName, $this->existingCerts)) {
$this->log("No signed certificate found with this CN.");
return false;
}
$cert = $this->request(sprintf("/CertList/DownLoadCert?orderId=%s", $this->existingCerts[$commonName]['id']));
return $cert;
}
function verifyDomains($domains) {
$this->log("Verifying domains.");
$check = $this->post("/Certificates/Checkdomains", array("domains" => implode("\n", $domains)));
if(!$check)
return false;
$check = json_decode($check);
return $check->status === 1 ? true:false;
}
function verifyCsr() {
$this->log("Verifying CSR.");
$check = $this->post("/UserRequstFile/CheckCSR", array("csr" => $this->getCsr()));
return $check[0] === "1" ? true:false;
}
function newPrivateKey() {
$this->log("Creating new PK.");
$pk = openssl_pkey_new($this->opensslSettings);
if($pk !== FALSE)
$this->privateKey = $pk;
return true;
}
function getPrivateKey() {
$this->log("Getting PK.");
if(is_null($this->privateKey))
$this->newPrivateKey();
openssl_pkey_export($this->privateKey, $privateKey);
return $privateKey;
}
function newCsr() {
$this->log("Creating new CSR.");
if(is_null($this->privateKey))
$this->newPrivateKey();
$this->csr = openssl_csr_new($this->defaultDn, $this->privateKey, $this->opensslSettings);
return true;
}
function getCsr() {
$this->log("Getting CSR.");
if(is_null($this->csr))
$this->newCsr();
openssl_csr_export($this->csr, $csr);
return $csr;
}
function newCertificate($domains) {
$this->log("Requesting new certificate.");
if(is_null($this->privateKey))
$this->newPrivateKey();
if(is_null($this->csr))
$this->newCsr();
if(empty($domains) || !is_array($domains))
return false;
if(!$this->verifyDomains($domains))
return false;
if(!$this->verifyCsr())
return false;
$request = $this->post("/Certificates/ssl", array("domains" => implode("\n", $domains), "areaCSR" => $this->getCsr(), "rbcsr" => "scsr", "CsrkeySize" => "1"));
if($request !== false)
return true;
return false;
}
function log($line, $exit = false) {
if($this->logging) error_log($line);
if($exit)
exit(1);
}
}