1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace LE_ACME2\Utilities; |
4
|
|
|
|
5
|
|
|
use LE_ACME2\Order; |
6
|
|
|
use LE_ACME2\Exception\OpenSSLException; |
7
|
|
|
|
8
|
|
|
class Certificate { |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* @throws OpenSSLException |
12
|
|
|
*/ |
13
|
|
|
public static function generateCSR(Order $order) : string { |
14
|
|
|
|
15
|
|
|
$dn = [ |
16
|
|
|
"commonName" => $order->getSubjects()[0] |
17
|
|
|
]; |
18
|
|
|
|
19
|
|
|
$san = implode(",", array_map(function ($dns) { |
20
|
|
|
|
21
|
|
|
return "DNS:" . $dns; |
22
|
|
|
}, $order->getSubjects()) |
23
|
|
|
); |
24
|
|
|
|
25
|
|
|
$configFilePath = $order->getKeyDirectoryPath() . 'csr_config'; |
26
|
|
|
|
27
|
|
|
$config = 'HOME = . |
28
|
|
|
RANDFILE = ' . $order->getKeyDirectoryPath() . '.rnd |
29
|
|
|
[ req ] |
30
|
|
|
default_bits = 4096 |
31
|
|
|
default_keyfile = privkey.pem |
32
|
|
|
distinguished_name = req_distinguished_name |
33
|
|
|
req_extensions = v3_req |
34
|
|
|
[ req_distinguished_name ] |
35
|
|
|
countryName = Country Name (2 letter code) |
36
|
|
|
[ v3_req ] |
37
|
|
|
basicConstraints = CA:FALSE |
38
|
|
|
subjectAltName = ' . $san . ' |
39
|
|
|
keyUsage = nonRepudiation, digitalSignature, keyEncipherment'; |
40
|
|
|
|
41
|
|
|
file_put_contents($configFilePath, $config); |
42
|
|
|
|
43
|
|
|
$privateKey = openssl_pkey_get_private( |
44
|
|
|
file_get_contents($order->getKeyDirectoryPath() . 'private.pem') |
45
|
|
|
); |
46
|
|
|
|
47
|
|
|
if($privateKey === false) { |
48
|
|
|
throw new OpenSSLException('openssl_pkey_get_private'); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
$csr = openssl_csr_new( |
52
|
|
|
$dn, |
53
|
|
|
$privateKey, |
|
|
|
|
54
|
|
|
[ |
55
|
|
|
'config' => $configFilePath, |
56
|
|
|
'digest_alg' => 'sha256' |
57
|
|
|
] |
58
|
|
|
); |
59
|
|
|
|
60
|
|
|
if($csr === false) { |
61
|
|
|
throw new OpenSSLException('openssl_csr_new'); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
if(!openssl_csr_export($csr, $csr)) { |
|
|
|
|
65
|
|
|
throw new OpenSSLException('openssl_csr_export'); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
unlink($configFilePath); |
69
|
|
|
|
70
|
|
|
return $csr; |
71
|
|
|
} |
72
|
|
|
} |