1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Rogierw\RwAcme\Endpoints; |
4
|
|
|
|
5
|
|
|
use Rogierw\RwAcme\DTO\CertificateBundleData; |
6
|
|
|
use Rogierw\RwAcme\DTO\OrderData; |
7
|
|
|
use Rogierw\RwAcme\Exceptions\LetsEncryptClientException; |
8
|
|
|
use Rogierw\RwAcme\Support\Base64; |
9
|
|
|
|
10
|
|
|
class Certificate extends Endpoint |
11
|
|
|
{ |
12
|
|
|
public function getBundle(OrderData $orderData): CertificateBundleData |
13
|
|
|
{ |
14
|
|
|
$signedPayload = $this->createKeyId($orderData->accountUrl, $orderData->certificateUrl); |
|
|
|
|
15
|
|
|
|
16
|
|
|
$response = $this->client->getHttpClient()->post($orderData->certificateUrl, $signedPayload); |
|
|
|
|
17
|
|
|
|
18
|
|
|
if ($response->getHttpResponseCode() !== 200) { |
19
|
|
|
$this->logResponse('error', 'Failed to fetch certificate', $response); |
20
|
|
|
|
21
|
|
|
throw new LetsEncryptClientException('Failed to fetch certificate.'); |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
return CertificateBundleData::fromResponse($response); |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
public function revoke(string $pem, int $reason = 0): bool |
28
|
|
|
{ |
29
|
|
|
if (($data = openssl_x509_read($pem)) === false) { |
30
|
|
|
throw new LetsEncryptClientException('Could not parse the certificate.'); |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
if (openssl_x509_export($data, $certificate) === false) { |
34
|
|
|
throw new LetsEncryptClientException('Could not export the certificate.'); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
preg_match('~-----BEGIN\sCERTIFICATE-----(.*)-----END\sCERTIFICATE-----~s', $certificate, $matches); |
38
|
|
|
$certificate = trim(Base64::urlSafeEncode(base64_decode(trim($matches[1])))); |
39
|
|
|
|
40
|
|
|
$revokeUrl = $this->client->directory()->revoke(); |
41
|
|
|
|
42
|
|
|
$signedPayload = $this->createKeyId( |
43
|
|
|
$this->client->account()->get()->url, |
44
|
|
|
$revokeUrl, |
45
|
|
|
[ |
46
|
|
|
'certificate' => $certificate, |
47
|
|
|
'reason' => $reason, |
48
|
|
|
] |
49
|
|
|
); |
50
|
|
|
|
51
|
|
|
$response = $this->client->getHttpClient()->post($revokeUrl, $signedPayload); |
52
|
|
|
|
53
|
|
|
if ($response->getHttpResponseCode() !== 200) { |
54
|
|
|
$this->logResponse('error', 'Failed to revoke certificate', $response); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
return $response->getHttpResponseCode() === 200; |
58
|
|
|
} |
59
|
|
|
} |
60
|
|
|
|