This repository has been archived by the owner on Sep 7, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 13
/
Client.php
486 lines (429 loc) · 11.4 KB
/
Client.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
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
<?php
/**
* WPENC\Core\Client class
*
* @package WPENC
* @subpackage Core
* @author Felix Arntz <felix-arntz@leaves-and-love.net>
* @since 1.0.0
*/
namespace WPENC\Core;
use WP_Error;
if ( ! defined( 'ABSPATH' ) ) {
die();
}
if ( ! class_exists( 'WPENC\Core\Client' ) ) {
/**
* This class contains core methods to communicate with the Let's Encrypt API.
*
* @since 1.0.0
*/
final class Client {
/**
* The API URL for Let's Encrypt.
*
* @since 1.0.0
*/
const API_URL = 'https://acme-v01.api.letsencrypt.org';
/**
* The API endpoint to register an account.
*
* @since 1.0.0
*/
const ENDPOINT_REGISTER = 'acme/new-reg';
/**
* The API endpoint to generate challenges.
*
* @since 1.0.0
*/
const ENDPOINT_AUTH = 'acme/new-authz';
/**
* The API endpoint to generate a certificate.
*
* @since 1.0.0
*/
const ENDPOINT_NEW = 'acme/new-cert';
/**
* The API endpoint to revoke a certificate.
*
* @since 1.0.0
*/
const ENDPOINT_REVOKE = 'acme/revoke-cert';
/**
* The API endpoint overview.
*
* @since 1.0.0
*/
const ENDPOINT_DIRECTORY = 'directory';
/**
* The resource to pass to the account registering endpoint.
*
* @since 1.0.0
*/
const RESOURCE_REGISTER = 'new-reg';
/**
* The resource to pass to the challenges generating endpoint.
*
* @since 1.0.0
*/
const RESOURCE_AUTH = 'new-authz';
/**
* The resource to pass to the challenges validating endpoint.
*
* @since 1.0.0
*/
const RESOURCE_CHALLENGE = 'challenge';
/**
* The resource to pass to the certificate generating endpoint.
*
* @since 1.0.0
*/
const RESOURCE_NEW = 'new-cert';
/**
* Singleton instance.
*
* @since 1.0.0
* @access private
* @static
* @var WPENC\Core\Client
*/
private static $instance = null;
/**
* Singleton method.
*
* @since 1.0.0
* @access public
* @static
*
* @return WPENC\Core\Client The class instance.
*/
public static function get() {
if ( null === self::$instance ) {
self::$instance = new self();
}
return self::$instance;
}
/**
* The response code received in the last response.
*
* @since 1.0.0
* @access private
* @var string
*/
private $last_response_code = null;
/**
* The response header received in the last response.
*
* @since 1.0.0
* @access private
* @var string
*/
private $last_response_header = null;
/**
* The nonce received in the last response.
*
* @since 1.0.0
* @access private
* @var string
*/
private $last_nonce = null;
/**
* The location received in the last response.
*
* @since 1.0.0
* @access private
* @var string
*/
private $last_location = null;
/**
* The links received in the last response.
*
* @since 1.0.0
* @access private
* @var string
*/
private $last_links = null;
/**
* Array of license document URLs.
*
* @since 1.0.0
* @access private
* @var array
*/
private $licenses = array();
/**
* Constructor.
*
* Sets the license document URLs.
*
* @since 1.0.0
* @access private
*/
private function __construct() {
// Newer licenses must come first.
$this->licenses = array(
'2017-11-15' => 'https://letsencrypt.org/documents/LE-SA-v1.2-November-15-2017.pdf',
'2016-08-01' => 'https://letsencrypt.org/documents/LE-SA-v1.1.1-August-1-2016.pdf',
'2015-07-27' => 'https://letsencrypt.org/documents/LE-SA-v1.0.1-July-27-2015.pdf',
);
}
/**
* Returns the URL to the current license document.
*
* @since 1.0.0
* @access private
* @return string URL to the license document.
*/
public function get_license_url() {
$now = current_time( 'timestamp' );
foreach ( $this->licenses as $date => $url ) {
if ( $now >= strtotime( $date ) ) {
return $url;
}
}
return '';
}
/**
* Registers an account with Let's Encrypt.
*
* @since 1.0.0
* @access public
*
* @return array|WP_Error The response array if successful or an error object otherwise.
*/
public function register() {
return $this->signed_request( self::ENDPOINT_REGISTER, array(
'resource' => self::RESOURCE_REGISTER,
'agreement' => $this->get_license_url(),
) );
}
/**
* Generates challenges for a domain with Let's Encrypt.
*
* @since 1.0.0
* @access public
*
* @param string $domain The domain to receive challenges for.
* @return array|WP_Error The response array if successful or an error object otherwise.
*/
public function auth( $domain ) {
return $this->signed_request( self::ENDPOINT_AUTH, array(
'resource' => self::RESOURCE_AUTH,
'identifier' => array(
'type' => 'dns',
'value' => $domain,
),
) );
}
/**
* Validates a domain challenge with Let's Encrypt.
*
* @since 1.0.0
* @access public
*
* @param string $uri The URI to check.
* @param string $token The token for the challenge.
* @param string $data Key authorization data.
* @return array|WP_Error The response array if successful or an error object otherwise.
*/
public function challenge( $uri, $token, $data ) {
return $this->signed_request( $uri, array(
'resource' => self::RESOURCE_CHALLENGE,
'type' => 'http-01',
'keyAuthorization' => $data,
'token' => $token,
) );
}
/**
* Generates a certificate with Let's Encrypt.
*
* @since 1.0.0
* @access public
*
* @param string $csr The CSR for the certificate.
* @return string|WP_Error The certificate if successful or an error object otherwise.
*/
public function generate( $csr ) {
return $this->signed_request( self::ENDPOINT_NEW, array(
'resource' => self::RESOURCE_NEW,
'csr' => Util::base64_url_encode( base64_decode( $csr ) ),
) );
}
/**
* Revokes a certificate with Let's Encrypt.
*
* @since 1.0.0
* @access public
*
* @param string $cert The certificate to revoke.
* @return array|WP_Error The response array if successful or an error object otherwise.
*/
public function revoke( $cert ) {
return $this->signed_request( self::ENDPOINT_REVOKE, array(
'certificate' => Util::base64_url_encode( base64_decode( $cert ) ),
) );
}
/**
* Lists the directory overview and general entry point.
*
* @since 1.0.0
* @access public
*
* @return array|WP_Error The response array if successful or an error object otherwise.
*/
public function directory() {
return $this->request( 'directory', 'GET' );
}
/**
* Sends a signed request to the Let's Encrypt API.
*
* All requests except the directory request go through this method.
*
* @since 1.0.0
* @access public
*
* @param string $endpoint The endpoint to send a request to.
* @param array $data Data to send with the request.
* @return string|array|WP_Error Either a JSON-decoded array response, a plain text response or an error object.
*/
public function signed_request( $endpoint, $data = null ) {
$account_keypair = AccountKeyPair::get();
$account_key_resource = $account_keypair->read_private();
if ( is_wp_error( $account_key_resource ) ) {
return $account_key_resource;
}
$account_key_details = $account_keypair->get_private_details();
if ( is_wp_error( $account_key_details ) ) {
return $account_key_details;
}
$protected = $header = array(
'alg' => 'RS256',
'jwk' => array(
'kty' => 'RSA',
'n' => Util::base64_url_encode( $account_key_details['rsa']['n'] ),
'e' => Util::base64_url_encode( $account_key_details['rsa']['e'] ),
),
);
if ( null !== ( $nonce = $this->get_last_nonce() ) ) {
$protected['nonce'] = $nonce;
} else {
$this->directory();
if ( null !== ( $nonce = $this->get_last_nonce() ) ) {
$protected['nonce'] = $nonce;
}
}
if ( ! isset( $protected['nonce'] ) ) {
return new WP_Error( 'signed_request_no_nonce', __( 'No nonce available for a signed request.', 'wp-encrypt' ) );
}
$data64 = Util::base64_url_encode( str_replace( '\\/', '/', json_encode( $data ) ) );
$protected64 = Util::base64_url_encode( json_encode( $protected ) );
$sign_status = openssl_sign( $protected64 . '.' . $data64, $signature, $account_key_resource, 'SHA256' );
if ( false === $sign_status ) {
return new WP_Error( 'private_key_cannot_sign', sprintf( __( 'Could not sign request with private key. Original error message: %s', 'wp-encrypt' ), openssl_error_string() ) );
}
$signature64 = Util::base64_url_encode( $signature );
return $this->request( $endpoint, 'POST', array(
'header' => $header,
'protected' => $protected64,
'payload' => $data64,
'signature' => $signature64,
) );
}
/**
* Sends a regular request to the Let's Encrypt API.
*
* @since 1.0.0
* @access public
*
* @param string $endpoint The endpoint to send a request to.
* @param string $method The request method ('GET' or 'POST').
* @param array $data Data to send with the request.
* @return string|array|WP_Error Either a JSON-decoded array response, a plain text response or an error object.
*/
public function request( $endpoint, $method = 'GET', $data = null ) {
if ( is_array( $data ) ) {
$data = json_encode( $data );
}
$args = array(
'method' => strtoupper( $method ),
'timeout' => 10,
'headers' => array(
'Accept' => 'application/json',
'Content-Type' => 'application/json; charset=' . get_option( 'blog_charset' ),
),
'body' => $data,
);
$url = $endpoint;
if ( false === strpos( $url, 'http://' ) && false === strpos( $url, 'https://' ) ) {
$url = self::API_URL . '/' . ltrim( $endpoint, '/' );
}
$response = wp_remote_request( $url, $args );
if ( is_wp_error( $response ) ) {
return $response;
}
$this->last_response_code = wp_remote_retrieve_response_code( $response );
$this->last_nonce = wp_remote_retrieve_header( $response, 'replay-nonce' );
$this->last_location = wp_remote_retrieve_header( $response, 'location' );
$this->last_links = wp_remote_retrieve_header( $response, 'link' );
$body = wp_remote_retrieve_body( $response );
$response = json_decode( $body, true );
return null === $response ? $body : $response;
}
/**
* Returns the last response code.
*
* @since 1.0.0
* @access public
*
* @return string The last response code.
*/
public function get_last_code() {
if ( $this->last_response_code ) {
return $this->last_response_code;
}
return null;
}
/**
* Returns the last response nonce.
*
* @since 1.0.0
* @access public
*
* @return string The last response nonce.
*/
public function get_last_nonce() {
if ( $this->last_nonce ) {
return $this->last_nonce;
}
return null;
}
/**
* Returns the last response location.
*
* @since 1.0.0
* @access public
*
* @return string The last response location.
*/
public function get_last_location() {
if ( $this->last_location ) {
return $this->last_location;
}
return null;
}
/**
* Returns the last response links.
*
* @since 1.0.0
* @access public
*
* @return array The last response links.
*/
public function get_last_links() {
if ( $this->last_links && preg_match_all( '#<(.*?)>;rel="up"#x', $this->last_links, $matches ) ) {
return $matches[1];
}
return array();
}
}
}