Certificate   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 63
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 4
Bugs 0 Features 0
Metric Value
wmc 4
eloc 26
c 4
b 0
f 0
dl 0
loc 63
rs 10
ccs 0
cts 19
cp 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A generateCSR() 0 58 4
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,
0 ignored issues
show
Bug introduced by
It seems like $privateKey can also be of type resource; however, parameter $private_key of openssl_csr_new() does only seem to accept OpenSSLAsymmetricKey, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

53
            /** @scrutinizer ignore-type */ $privateKey,
Loading history...
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)) {
0 ignored issues
show
Bug introduced by
$csr of type OpenSSLCertificateSigningRequest|resource is incompatible with the type string expected by parameter $output of openssl_csr_export(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

64
        if(!openssl_csr_export($csr, /** @scrutinizer ignore-type */ $csr)) {
Loading history...
65
            throw new OpenSSLException('openssl_csr_export');
66
        }
67
68
        unlink($configFilePath);
69
70
        return $csr;
71
    }
72
}