|
13 | 13 | use function base64_encode; |
14 | 14 | use function chunk_split; |
15 | 15 | use function count; |
16 | | -use function defined; |
17 | 16 | use function explode; |
18 | | -use function extension_loaded; |
19 | | -use function hash_equals; |
20 | | -use function hash_hmac; |
21 | 17 | use function implode; |
22 | | -use function mb_strlen; |
23 | | -use function mb_substr; |
24 | | -use function openssl_decrypt; |
25 | | -use function openssl_digest; |
26 | | -use function openssl_encrypt; |
27 | | -use function openssl_random_pseudo_bytes; |
28 | 18 | use function strncmp; |
29 | 19 | use function strpos; |
30 | 20 | use function substr; |
|
38 | 28 |
|
39 | 29 | class Crypto |
40 | 30 | { |
41 | | - /** |
42 | | - * Decrypt data using AES-256-CBC and the key provided as a parameter. |
43 | | - * |
44 | | - * @param string $ciphertext The HMAC of the encrypted data, the IV used and the encrypted data, concatenated. |
45 | | - * @param string $secret The secret to use to decrypt the data. |
46 | | - * |
47 | | - * @return string The decrypted data. |
48 | | - * @throws \InvalidArgumentException If $ciphertext is not a string. |
49 | | - * @throws Error\Exception If the openssl module is not loaded. |
50 | | - * |
51 | | - * @see \SimpleSAML\Utils\Crypto::aesDecrypt() |
52 | | - */ |
53 | | - private function aesDecryptInternal(string $ciphertext, string $secret): string |
54 | | - { |
55 | | - if (!extension_loaded('openssl')) { |
56 | | - throw new Error\Exception("The openssl PHP module is not loaded."); |
57 | | - } |
58 | | - |
59 | | - $len = mb_strlen($ciphertext, '8bit'); |
60 | | - if ($len < 48) { |
61 | | - throw new InvalidArgumentException( |
62 | | - 'Input parameter "$ciphertext" must be a string with more than 48 characters.' |
63 | | - ); |
64 | | - } |
65 | | - |
66 | | - // derive encryption and authentication keys from the secret |
67 | | - $key = openssl_digest($secret, 'sha512'); |
68 | | - |
69 | | - $hmac = mb_substr($ciphertext, 0, 32, '8bit'); |
70 | | - $iv = mb_substr($ciphertext, 32, 16, '8bit'); |
71 | | - $msg = mb_substr($ciphertext, 48, $len - 48, '8bit'); |
72 | | - |
73 | | - // authenticate the ciphertext |
74 | | - if (hash_equals(hash_hmac('sha256', $iv . $msg, substr($key, 64, 64), true), $hmac)) { |
75 | | - $plaintext = openssl_decrypt( |
76 | | - $msg, |
77 | | - 'AES-256-CBC', |
78 | | - substr($key, 0, 64), |
79 | | - defined('OPENSSL_RAW_DATA') ? OPENSSL_RAW_DATA : 1, |
80 | | - $iv |
81 | | - ); |
82 | | - |
83 | | - if ($plaintext !== false) { |
84 | | - return $plaintext; |
85 | | - } |
86 | | - } |
87 | | - |
88 | | - throw new Error\Exception("Failed to decrypt ciphertext."); |
89 | | - } |
90 | | - |
91 | | - |
92 | | - /** |
93 | | - * Decrypt data using AES-256-CBC and the system-wide secret salt as key. |
94 | | - * |
95 | | - * @param string $ciphertext The HMAC of the encrypted data, the IV used and the encrypted data, concatenated. |
96 | | - * @param string $secret The secret to use to decrypt the data. |
97 | | - * If not provided, the secret salt from the configuration will be used |
98 | | - * |
99 | | - * @return string The decrypted data. |
100 | | - * @throws \InvalidArgumentException If $ciphertext is not a string. |
101 | | - * @throws Error\Exception If the openssl module is not loaded. |
102 | | - * |
103 | | - */ |
104 | | - public function aesDecrypt(string $ciphertext, string $secret = null): string |
105 | | - { |
106 | | - if ($secret === null) { |
107 | | - $configUtils = new Config(); |
108 | | - $secret = $configUtils->getSecretSalt(); |
109 | | - } |
110 | | - |
111 | | - return $this->aesDecryptInternal($ciphertext, $secret); |
112 | | - } |
113 | | - |
114 | | - |
115 | | - /** |
116 | | - * Encrypt data using AES-256-CBC and the key provided as a parameter. |
117 | | - * |
118 | | - * @param string $data The data to encrypt. |
119 | | - * @param string $secret The secret to use to encrypt the data. |
120 | | - * |
121 | | - * @return string An HMAC of the encrypted data, the IV and the encrypted data, concatenated. |
122 | | - * @throws \InvalidArgumentException If $data is not a string. |
123 | | - * @throws Error\Exception If the openssl module is not loaded. |
124 | | - * |
125 | | - * @see \SimpleSAML\Utils\Crypto::aesEncrypt() |
126 | | - */ |
127 | | - private function aesEncryptInternal(string $data, string $secret): string |
128 | | - { |
129 | | - if (!extension_loaded('openssl')) { |
130 | | - throw new Error\Exception('The openssl PHP module is not loaded.'); |
131 | | - } |
132 | | - |
133 | | - // derive encryption and authentication keys from the secret |
134 | | - $key = openssl_digest($secret, 'sha512'); |
135 | | - |
136 | | - // generate a random IV |
137 | | - $iv = openssl_random_pseudo_bytes(16); |
138 | | - |
139 | | - // encrypt the message |
140 | | - $ciphertext = openssl_encrypt( |
141 | | - $data, |
142 | | - 'AES-256-CBC', |
143 | | - substr($key, 0, 64), |
144 | | - defined('OPENSSL_RAW_DATA') ? OPENSSL_RAW_DATA : 1, |
145 | | - $iv |
146 | | - ); |
147 | | - |
148 | | - if ($ciphertext === false) { |
149 | | - throw new Error\Exception("Failed to encrypt plaintext."); |
150 | | - } |
151 | | - |
152 | | - // return the ciphertext with proper authentication |
153 | | - return hash_hmac('sha256', $iv . $ciphertext, substr($key, 64, 64), true) . $iv . $ciphertext; |
154 | | - } |
155 | | - |
156 | | - |
157 | | - /** |
158 | | - * Encrypt data using AES-256-CBC and the system-wide secret salt as key. |
159 | | - * |
160 | | - * @param string $data The data to encrypt. |
161 | | - * @param string $secret The secret to use to decrypt the data. |
162 | | - * If not provided, the secret salt from the configuration will be used |
163 | | - * |
164 | | - * @return string An HMAC of the encrypted data, the IV and the encrypted data, concatenated. |
165 | | - * @throws \InvalidArgumentException If $data is not a string. |
166 | | - * @throws Error\Exception If the openssl module is not loaded. |
167 | | - * |
168 | | - */ |
169 | | - public function aesEncrypt(string $data, string $secret = null): string |
170 | | - { |
171 | | - if ($secret === null) { |
172 | | - $configUtils = new Config(); |
173 | | - $secret = $configUtils->getSecretSalt(); |
174 | | - } |
175 | | - |
176 | | - return $this->aesEncryptInternal($data, $secret); |
177 | | - } |
178 | | - |
179 | | - |
180 | 31 | /** |
181 | 32 | * Convert data from DER to PEM encoding. |
182 | 33 | * |
|
0 commit comments