This repository has been archived by the owner on Nov 17, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
/
LE.inc.php
328 lines (274 loc) · 7.99 KB
/
LE.inc.php
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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
<?php
/*
CertLE - A Let's Encrypt PHP Command Line ACME Client
Copyright (C) 2016 S.Körfgen
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
class LE {
private
$directory='https://acme-v01.api.letsencrypt.org/directory', // live
//$directory='https://acme-staging.api.letsencrypt.org/directory', // staging
$resources=null,
$nonce='',
$header, // JOSE Header
$account_key;
protected
$thumbprint,
$acme_path='.well-known/acme-challenge/';
public function __construct($account_key_pem){
// load account key
if (false===($this->account_key=openssl_pkey_get_private('file://'.$account_key_pem))){
throw new Exception(
'Could not load account key: '.$account_key_pem."\n".
openssl_error_string()
);
}
// get account key details
if (false===($details=openssl_pkey_get_details($this->account_key))){
throw new Exception(
'Could not get account key details: '.$account_key_pem."\n".
openssl_error_string()
);
}
// JOSE Header - RFC7515
$this->header=array(
'alg'=>'RS256',
'jwk'=>array( // JSON Web Key
'e'=>$this->base64url($details['rsa']['e']), // public exponent
'kty'=>'RSA',
'n'=>$this->base64url($details['rsa']['n']) // public modulus
)
);
// JSON Web Key (JWK) Thumbprint - RFC7638
$this->thumbprint=$this->base64url(
hash(
'sha256',
json_encode($this->header['jwk']),
true
)
);
}
public function __destruct(){
if ($this->account_key) {
openssl_pkey_free($this->account_key);
}
}
private function init(){
$ret=$this->http_request($this->directory); // Read ACME Directory
$this->resources=$ret['body']; // store resources for later use
$this->nonce=$ret['headers']['replay-nonce']; // capture first replay-nonce
}
// Encapsulate $payload into JSON Web Signature (JWS) - RFC7515
private function jws_encapsulate($payload){
$protected=$this->header;
$protected['nonce']=$this->nonce; // replay-nonce
$protected64=$this->base64url(json_encode($protected));
$payload64=$this->base64url(json_encode($payload));
if (false===openssl_sign(
$protected64.'.'.$payload64,
$signature,
$this->account_key,
OPENSSL_ALGO_SHA256
)){
throw new Exception(
'Failed to sign payload !'."\n".
openssl_error_string()
);
}
return array(
'header'=>$this->header,
'protected'=>$protected64,
'payload'=>$payload64,
'signature'=>$this->base64url($signature)
);
}
// RFC7515 - Appendix C
final protected function base64url($data){
return rtrim(strtr(base64_encode($data),'+/','-_'),'=');
}
final protected function request($type,$payload=array(),$url=null,$raw=false,$accept=null){
if ($this->resources===null){
$this->init(); // read AMCE directory and get first replay-nonce
}
$data=json_encode(
$this->jws_encapsulate(
array_merge(
$payload,
array('resource'=>$type)
)
)
);
$ret=$this->http_request($url===null?$this->resources[$type]:$url,$data,$raw,$accept);
$this->nonce=$ret['headers']['replay-nonce']; // capture replay-nonce
return $ret;
}
final protected function http_request($url,$data=null,$raw=false,$accept=null){
$ctx=stream_context_create(
array(
'http'=>array(
'header'=>$data===null?'':'Content-Type: application/json',
'method'=>$data===null?'GET':'POST',
'user_agent'=>'CertLE (PHP LE Client)',
'ignore_errors'=>true,
'timeout'=>60,
'content'=>$data
)
)
);
$body=@file_get_contents($url,false,$ctx);
if ($body===false){
throw new Exception('request error: '.$url);
}
$headers=array_reduce( // parse http headers into array
$http_response_header,
function($carry,$item)use(&$code,&$status){
$parts=explode(':',$item,2);
if (count($parts)===1){
list(,$code,$status)=explode(' ',$item,3);
$carry=array();
}else{
list($k,$v)=$parts;
$k=strtolower(trim($k));
$v=trim($v);
if ($k==='link'){ // parse Link Headers
if (preg_match("/<(.*)>\\s*;\\s*rel=\"(.*)\"/",$v,$matches)){
$carry['link'][$matches[2]]=$matches[1];
}
}else{
$carry[$k]=$v;
}
}
return $carry;
},
array()
);
if (!$raw) {
if ($body==''){
$json='';
}else{
$json=json_decode($body,true);
}
}else{
$json=null;
}
if (is_array($json)){
if ($accept!='409' && isset($json['detail'])) {
throw new Exception($json['detail']);
}
if (isset($json['error']) && is_array($json['error']) && isset($json['error']['detail'])) {
throw new Exception($json['error']['detail']);
}
}
if ( ($code!=$accept) && ($code[0]!='2') ){
throw new Exception('request failed: '.$code.' ['.$status.']: '.$url);
}
if (!$raw) {
if ($json===null) {
throw new Exception('json_decode failed: '.print_r($headers,true).$body);
}else{
$body=$json;
}
}
$ret=array(
'code'=>$code,
'status'=>$status,
'headers'=>$headers,
'body'=>$body
);
//print_r($ret);
return $ret;
}
final protected function write_challenge($docroot,$challenge){
if (!is_dir($docroot)){
throw new Exception('docroot does not exist: '.$docroot);
}
@mkdir($docroot.$this->acme_path,0755,true);
if (!is_dir($docroot.$this->acme_path)){
throw new Exception('failed to create acme challenge directory: '.$docroot.$this->acme_path);
}
$keyAuthorization=$challenge['token'].'.'.$this->thumbprint;
if (false===@file_put_contents($docroot.$this->acme_path.$challenge['token'],$keyAuthorization)){
throw new Exception('failed to create challenge file: '.$docroot.$this->acme_path.$challenge['token']);
}
}
final protected function remove_challenge($docroot,$challenge){
unlink($docroot.$this->acme_path.$challenge['token']);
rmdir($docroot.$this->acme_path);
rmdir($docroot.dirname($this->acme_path));
}
final protected function pem2der($pem) {
return base64_decode(
implode(
'',
array_slice(
array_map('trim',explode("\n",trim($pem))),
1,
-1
)
)
);
}
final protected function der2pem($der) {
return "-----BEGIN CERTIFICATE-----\n".
chunk_split(base64_encode($der),64,"\n").
"-----END CERTIFICATE-----\n";
}
final protected function generate_csr($domain_key_pem,$domains){
if (false===($domain_key=openssl_pkey_get_private('file://'.$domain_key_pem))){
throw new Exception(
'Could not load domain key: '.$domain_key_pem."\n".
openssl_error_string()
);
}
if (false===($fn=tempnam(sys_get_temp_dir(), "CNF_"))){
throw new Exception('Failed to create temp file !');
}
if (false===@file_put_contents($fn,
'HOME = .'."\n".
'RANDFILE=$ENV::HOME/.rnd'."\n".
'[req]'."\n".
'distinguished_name=req_distinguished_name'."\n".
'[req_distinguished_name]'."\n".
'[v3_req]'."\n".
'[v3_ca]'."\n".
'[SAN]'."\n".
'subjectAltName='.
implode(',',array_map(function($domain){
return 'DNS:'.$domain;
},$domains)).
"\n"
)){
throw new Exception('Failed to write tmp file: '.$fn);
}
$dn=array('commonName'=>reset($domains));
$csr=openssl_csr_new($dn,$domain_key,array(
'config'=>$fn,
'req_extensions'=>'SAN',
'digest_alg'=>'sha512'
));
unlink($fn);
openssl_pkey_free($domain_key);
if (!$csr) {
throw new Exception(
'Could not generate CSR !'."\n".
openssl_error_string()
);
}
if (false===openssl_csr_export($csr,$out)){
throw new Exception(
'Could not export CSR !'."\n".
openssl_error_string()
);
}
return $out;
}
}