Passed
Pull Request — master (#1)
by John
02:11
created

LEFunctions::ECGenerateKeys()   A

Complexity

Conditions 5
Paths 6

Size

Total Lines 32
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 18
nc 6
nop 1
dl 0
loc 32
rs 9.3554
c 0
b 0
f 0
1
<?php
2
3
namespace Zwartpet\PHPCertificateToolbox;
4
5
use Zwartpet\PHPCertificateToolbox\Exception\LogicException;
6
use Zwartpet\PHPCertificateToolbox\Exception\RuntimeException;
7
8
/**
9
 * LetsEncrypt Functions class, supplying the LetsEncrypt Client with supportive functions.
10
 *
11
 * @author     Youri van Weegberg <[email protected]>
12
 * @copyright  2018 Youri van Weegberg
13
 * @license    https://opensource.org/licenses/mit-license.php  MIT License
14
 */
15
class LEFunctions
16
{
17
    /**
18
     * Generates a new RSA keypair and returns both
19
     *
20
     * @param integer $keySize RSA key size, must be between 2048 and 4096 (default is 4096)
21
     * @return array containing public and private indexes containing the new keys
22
     */
23
    public static function RSAGenerateKeys($keySize = 4096)
24
    {
25
26
        if ($keySize < 2048 || $keySize > 4096) {
27
            throw new LogicException("RSA key size must be between 2048 and 4096");
28
        }
29
30
        $res = openssl_pkey_new([
31
            "private_key_type" => OPENSSL_KEYTYPE_RSA,
32
            "private_key_bits" => intval($keySize),
33
        ]);
34
35
        if (!openssl_pkey_export($res, $privateKey)) {
36
            throw new RuntimeException("RSA keypair export failed!"); //@codeCoverageIgnore
37
        }
38
39
        $details = openssl_pkey_get_details($res);
40
41
        $result = ['public' => $details['key'], 'private' => $privateKey];
42
43
        openssl_pkey_free($res);
44
45
        return $result;
46
    }
47
48
49
    /**
50
     * Generates a new EC prime256v1 keypair and saves both keys to a new file.
51
     *
52
     * @param integer $keySize EC key size, possible values are 256 (prime256v1) or 384 (secp384r1),
53
     *                               default is 256
54
     * @return array containing public and private indexes containing the new keys
55
     */
56
    public static function ECGenerateKeys($keySize = 256)
57
    {
58
        if (version_compare(PHP_VERSION, '7.1.0') == -1) {
59
            throw new RuntimeException("PHP 7.1+ required for EC keys"); //@codeCoverageIgnore
60
        }
61
62
        if ($keySize == 256) {
63
            $res = openssl_pkey_new([
64
                "private_key_type" => OPENSSL_KEYTYPE_EC,
65
                "curve_name" => "prime256v1",
66
            ]);
67
        } elseif ($keySize == 384) {
68
            $res = openssl_pkey_new([
69
                "private_key_type" => OPENSSL_KEYTYPE_EC,
70
                "curve_name" => "secp384r1",
71
            ]);
72
        } else {
73
            throw new LogicException("EC key size must be 256 or 384");
74
        }
75
76
77
        if (!openssl_pkey_export($res, $privateKey)) {
78
            throw new RuntimeException("EC keypair export failed!"); //@codeCoverageIgnore
79
        }
80
81
        $details = openssl_pkey_get_details($res);
82
83
        $result = ['public' => $details['key'], 'private' => $privateKey];
84
85
        openssl_pkey_free($res);
86
87
        return $result;
88
    }
89
90
91
    /**
92
     * Encodes a string input to a base64 encoded string which is URL safe.
93
     *
94
     * @param string $input The input string to encode.
95
     *
96
     * @return string   Returns a URL safe base64 encoded string.
97
     */
98
    public static function base64UrlSafeEncode($input)
99
    {
100
        return str_replace('=', '', strtr(base64_encode($input), '+/', '-_'));
101
    }
102
103
    /**
104
     * Decodes a string that is URL safe base64 encoded.
105
     *
106
     * @param string $input The encoded input string to decode.
107
     *
108
     * @return string   Returns the decoded input string.
109
     */
110
    public static function base64UrlSafeDecode($input)
111
    {
112
        $remainder = strlen($input) % 4;
113
        if ($remainder) {
114
            $padlen = 4 - $remainder;
115
            $input .= str_repeat('=', $padlen);
116
        }
117
        return base64_decode(strtr($input, '-_', '+/'));
118
    }
119
}
120