Subversion Repositories php_utils

Rev

Rev 31 | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 31 Rev 34
1
<?php
1
<?php
2
 
2
 
3
/*
3
/*
4
 * OpenSSL php functions implemented using phpseclib
4
 * OpenSSL php functions implemented using phpseclib
5
 * Copyright 2022 Daniel Marschall, ViaThinkSoft
5
 * Copyright 2022 Daniel Marschall, ViaThinkSoft
6
 * Version 2022-04-10
6
 * Version 2022-07-17
7
 *
7
 *
8
 * Licensed under the Apache License, Version 2.0 (the "License");
8
 * Licensed under the Apache License, Version 2.0 (the "License");
9
 * you may not use this file except in compliance with the License.
9
 * you may not use this file except in compliance with the License.
10
 * You may obtain a copy of the License at
10
 * You may obtain a copy of the License at
11
 *
11
 *
12
 *     http://www.apache.org/licenses/LICENSE-2.0
12
 *     http://www.apache.org/licenses/LICENSE-2.0
13
 *
13
 *
14
 * Unless required by applicable law or agreed to in writing, software
14
 * Unless required by applicable law or agreed to in writing, software
15
 * distributed under the License is distributed on an "AS IS" BASIS,
15
 * distributed under the License is distributed on an "AS IS" BASIS,
16
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17
 * See the License for the specific language governing permissions and
17
 * See the License for the specific language governing permissions and
18
 * limitations under the License.
18
 * limitations under the License.
19
 */
19
 */
20
 
20
 
21
// How to use this supplement:
21
// How to use this supplement:
22
// 1. Include phpseclib using composer and include the autoloader
22
// 1. Include phpseclib using composer and include the autoloader
23
// 2. Then, include this file. The openssl functions are now available.
23
// 2. Then, include this file. The openssl functions are now available.
24
 
24
 
25
// ATTENTION: This supplement/polyfill does only implement a few openssl_*() functions,
25
// ATTENTION: This supplement/polyfill does only implement a few openssl_*() functions,
26
// and only a few algorithms: AES and RSA! Feel free to extend this library!
26
// and only a few algorithms: AES and RSA! Feel free to extend this library!
27
// The sign/verify and encrypt/decrypt functions should be binary compatible with
27
// The sign/verify and encrypt/decrypt functions should be binary compatible with
28
// the actual openssl functions.
28
// the actual openssl functions.
29
 
29
 
30
if (!function_exists('openssl_pkey_new') && class_exists('\\phpseclib3\\Crypt\\RSA')) {
30
if (!function_exists('openssl_pkey_new') && class_exists('\\phpseclib3\\Crypt\\RSA')) {
31
 
31
 
32
        define('OPENSSL_SUPPLEMENT', 1);
32
        define('OPENSSL_SUPPLEMENT', 1);
33
 
33
 
34
        $openssl_supplement_last_error = '';
34
        // ---------------------------------------------------------------------
35
 
35
 
-
 
36
        // https://www.php.net/manual/en/openssl.purpose-check.php
-
 
37
        if (!defined('X509_PURPOSE_SSL_CLIENT')) define('X509_PURPOSE_SSL_CLIENT', 1);
-
 
38
        if (!defined('X509_PURPOSE_SSL_SERVER')) define('X509_PURPOSE_SSL_SERVER', 2);
-
 
39
        if (!defined('X509_PURPOSE_NS_SSL_SERVER')) define('X509_PURPOSE_NS_SSL_SERVER', 3);
-
 
40
        if (!defined('X509_PURPOSE_SMIME_SIGN')) define('X509_PURPOSE_SMIME_SIGN', 4);
-
 
41
        if (!defined('X509_PURPOSE_SMIME_ENCRYPT')) define('X509_PURPOSE_SMIME_ENCRYPT', 5);
-
 
42
        if (!defined('X509_PURPOSE_CRL_SIGN')) define('X509_PURPOSE_CRL_SIGN', 6);
-
 
43
        if (!defined('X509_PURPOSE_ANY')) define('X509_PURPOSE_ANY', 7);
-
 
44
 
-
 
45
        // https://www.php.net/manual/en/openssl.padding.php
36
        if (!defined('OPENSSL_KEYTYPE_RSA')) define('OPENSSL_KEYTYPE_RSA', 0);
46
        if (!defined('OPENSSL_PKCS1_PADDING')) define('OPENSSL_PKCS1_PADDING', 1);
-
 
47
        if (!defined('OPENSSL_SSLV23_PADDING')) define('OPENSSL_SSLV23_PADDING', 2);
-
 
48
        if (!defined('OPENSSL_NO_PADDING')) define('OPENSSL_NO_PADDING', 3);
-
 
49
        if (!defined('OPENSSL_PKCS1_OAEP_PADDING')) define('OPENSSL_PKCS1_OAEP_PADDING', 4);
37
 
50
 
-
 
51
        // https://www.php.net/manual/en/openssl.key-types.php
-
 
52
        if (!defined('OPENSSL_KEYTYPE_RSA')) define('OPENSSL_KEYTYPE_RSA', 0);
-
 
53
        if (!defined('OPENSSL_KEYTYPE_DSA')) define('OPENSSL_KEYTYPE_DSA', 1);
-
 
54
        if (!defined('OPENSSL_KEYTYPE_DH')) define('OPENSSL_KEYTYPE_DH', 2);
-
 
55
        if (!defined('OPENSSL_KEYTYPE_EC')) define('OPENSSL_KEYTYPE_EC', 3);
-
 
56
 
-
 
57
        // https://www.php.net/manual/en/openssl.pkcs7.flags.php
-
 
58
        if (!defined('PKCS7_TEXT')) define('PKCS7_TEXT', 1);
-
 
59
        if (!defined('PKCS7_BINARY')) define('PKCS7_BINARY', 128);
-
 
60
        if (!defined('PKCS7_NOINTERN')) define('PKCS7_NOINTERN', 16);
-
 
61
        if (!defined('PKCS7_NOVERIFY')) define('PKCS7_NOVERIFY', 32);
-
 
62
        if (!defined('PKCS7_NOCHAIN')) define('PKCS7_NOCHAIN', 8);
-
 
63
        if (!defined('PKCS7_NOCERTS')) define('PKCS7_NOCERTS', 2);
-
 
64
        if (!defined('PKCS7_NOATTR')) define('PKCS7_NOATTR', 256);
-
 
65
        if (!defined('PKCS7_DETACHED')) define('PKCS7_DETACHED', 64);
-
 
66
        if (!defined('PKCS7_NOSIGS')) define('PKCS7_NOSIGS', 4);
-
 
67
 
-
 
68
        // https://www.php.net/manual/en/openssl.cms.flags.php
38
        if (!defined('OPENSSL_RAW_DATA')) define('OPENSSL_RAW_DATA', 1);
69
        if (!defined('OPENSSL_CMS_TEXT')) define('OPENSSL_CMS_TEXT', 1);
-
 
70
        if (!defined('OPENSSL_CMS_BINARY')) define('OPENSSL_CMS_BINARY', 128);
-
 
71
        if (!defined('OPENSSL_CMS_NOINTERN')) define('OPENSSL_CMS_NOINTERN', 16);
-
 
72
        if (!defined('OPENSSL_CMS_NOVERIFY')) define('OPENSSL_CMS_NOVERIFY', 32);
-
 
73
        if (!defined('OPENSSL_CMS_NOCERTS')) define('OPENSSL_CMS_NOCERTS', 2);
-
 
74
        if (!defined('OPENSSL_CMS_NOATTR')) define('OPENSSL_CMS_NOATTR', 256);
-
 
75
        if (!defined('OPENSSL_CMS_DETACHED')) define('OPENSSL_CMS_DETACHED', 64);
39
        if (!defined('OPENSSL_ZERO_PADDING')) define('OPENSSL_ZERO_PADDING', 2);
76
        if (!defined('OPENSSL_CMS_NOSIGS')) define('OPENSSL_CMS_NOSIGS', 12);
40
 
77
 
-
 
78
        // https://www.php.net/manual/en/openssl.signature-algos.php
-
 
79
        if (!defined('OPENSSL_ALGO_DSS1')) define('OPENSSL_ALGO_DSS1', 5); // Only defined when php/openssl compiled with MD2 support
41
        if (!defined('OPENSSL_ALGO_SHA1')) define('OPENSSL_ALGO_SHA1', 1);
80
        if (!defined('OPENSSL_ALGO_SHA1')) define('OPENSSL_ALGO_SHA1', 1);
42
        if (!defined('OPENSSL_ALGO_SHA224')) define('OPENSSL_ALGO_SHA224', 6);
81
        if (!defined('OPENSSL_ALGO_SHA224')) define('OPENSSL_ALGO_SHA224', 6);
43
        if (!defined('OPENSSL_ALGO_SHA256')) define('OPENSSL_ALGO_SHA256', 7);
82
        if (!defined('OPENSSL_ALGO_SHA256')) define('OPENSSL_ALGO_SHA256', 7);
44
        if (!defined('OPENSSL_ALGO_SHA384')) define('OPENSSL_ALGO_SHA384', 8);
83
        if (!defined('OPENSSL_ALGO_SHA384')) define('OPENSSL_ALGO_SHA384', 8);
45
        if (!defined('OPENSSL_ALGO_SHA512')) define('OPENSSL_ALGO_SHA512', 9);
84
        if (!defined('OPENSSL_ALGO_SHA512')) define('OPENSSL_ALGO_SHA512', 9);
46
        if (!defined('OPENSSL_ALGO_RMD160')) define('OPENSSL_ALGO_RMD160', 10);
85
        if (!defined('OPENSSL_ALGO_RMD160')) define('OPENSSL_ALGO_RMD160', 10);
47
        if (!defined('OPENSSL_ALGO_MD5')) define('OPENSSL_ALGO_MD5', 2);
86
        if (!defined('OPENSSL_ALGO_MD5')) define('OPENSSL_ALGO_MD5', 2);
48
        if (!defined('OPENSSL_ALGO_MD4')) define('OPENSSL_ALGO_MD4', 3);
87
        if (!defined('OPENSSL_ALGO_MD4')) define('OPENSSL_ALGO_MD4', 3);
-
 
88
        if (!defined('OPENSSL_ALGO_MD2')) define('OPENSSL_ALGO_MD2', 4); // Only defined when php/openssl compiled with MD2 support
-
 
89
 
-
 
90
        // https://www.php.net/manual/en/openssl.ciphers.php
-
 
91
        if (!defined('OPENSSL_CIPHER_RC2_40')) define('OPENSSL_CIPHER_RC2_40', 0);
-
 
92
        if (!defined('OPENSSL_CIPHER_RC2_128')) define('OPENSSL_CIPHER_RC2_128', 1);
-
 
93
        if (!defined('OPENSSL_CIPHER_RC2_64')) define('OPENSSL_CIPHER_RC2_64', 2);
-
 
94
        if (!defined('OPENSSL_CIPHER_DES')) define('OPENSSL_CIPHER_DES', 3);
-
 
95
        if (!defined('OPENSSL_CIPHER_3DES')) define('OPENSSL_CIPHER_3DES', 4);
-
 
96
        if (!defined('OPENSSL_CIPHER_AES_128_CBC')) define('OPENSSL_CIPHER_AES_128_CBC', 5);
-
 
97
        if (!defined('OPENSSL_CIPHER_AES_192_CBC')) define('OPENSSL_CIPHER_AES_192_CBC', 6);
-
 
98
        if (!defined('OPENSSL_CIPHER_AES_256_CBC')) define('OPENSSL_CIPHER_AES_256_CBC', 7);
-
 
99
 
-
 
100
        // https://www.php.net/manual/en/openssl.constversion.php
-
 
101
        // OPENSSL_VERSION_TEXT (string)
-
 
102
        // OPENSSL_VERSION_NUMBER (int)
-
 
103
 
-
 
104
        // https://www.php.net/manual/en/openssl.constsni.php
-
 
105
        // OPENSSL_TLSEXT_SERVER_NAME (string)
-
 
106
 
-
 
107
        // https://www.php.net/manual/en/openssl.constants.other.php
-
 
108
        if (!defined('OPENSSL_RAW_DATA')) define('OPENSSL_RAW_DATA', 1);
-
 
109
        if (!defined('OPENSSL_ZERO_PADDING')) define('OPENSSL_ZERO_PADDING', 2);
-
 
110
        if (!defined('OPENSSL_ENCODING_SMIME')) define('OPENSSL_ENCODING_SMIME', 1);
-
 
111
        if (!defined('OPENSSL_ENCODING_DER')) define('OPENSSL_ENCODING_DER', 0);
-
 
112
        if (!defined('OPENSSL_ENCODING_PEM')) define('OPENSSL_ENCODING_PEM', 2);
-
 
113
 
-
 
114
        // ---------------------------------------------------------------------
-
 
115
 
-
 
116
        $openssl_supplement_last_error = '';
49
 
117
 
50
        function openssl_pkey_new($pkey_config=null) {
118
        function openssl_pkey_new($pkey_config=null) {
51
                try {
119
                try {
52
                        $algo = $pkey_config && isset($pkey_config["private_key_type"]) ? $pkey_config["private_key_type"] : OPENSSL_KEYTYPE_RSA;
120
                        $algo = $pkey_config && isset($pkey_config["private_key_type"]) ? $pkey_config["private_key_type"] : OPENSSL_KEYTYPE_RSA;
53
                        $bits = $pkey_config && isset($pkey_config["private_key_bits"]) ? $pkey_config["private_key_bits"] : 2048;
121
                        $bits = $pkey_config && isset($pkey_config["private_key_bits"]) ? $pkey_config["private_key_bits"] : 2048;
54
 
122
 
55
                        // TODO: Also support $pkey_config['encrypt_key'] and $pkey_config['encrypt_key_cipher'] ?
123
                        // TODO: Also support $pkey_config['encrypt_key'] and $pkey_config['encrypt_key_cipher'] ?
56
 
124
 
57
                        if ($algo == OPENSSL_KEYTYPE_RSA) {
125
                        if ($algo == OPENSSL_KEYTYPE_RSA) {
58
                                $private = \phpseclib3\Crypt\RSA::createKey($bits);
126
                                $private = \phpseclib3\Crypt\RSA::createKey($bits);
59
                        } else {
127
                        } else {
60
                                throw new Exception("Algo not implemented");
128
                                throw new Exception("Algo not implemented");
61
                        }
129
                        }
62
 
130
 
63
                        $private = $private->withPadding(\phpseclib3\Crypt\RSA::ENCRYPTION_PKCS1 | \phpseclib3\Crypt\RSA::SIGNATURE_PKCS1);
131
                        $private = $private->withPadding(\phpseclib3\Crypt\RSA::ENCRYPTION_PKCS1 | \phpseclib3\Crypt\RSA::SIGNATURE_PKCS1);
64
 
132
 
65
                        $public = $private->getPublicKey()->withPadding(\phpseclib3\Crypt\RSA::ENCRYPTION_PKCS1 | \phpseclib3\Crypt\RSA::SIGNATURE_PKCS1);
133
                        $public = $private->getPublicKey()->withPadding(\phpseclib3\Crypt\RSA::ENCRYPTION_PKCS1 | \phpseclib3\Crypt\RSA::SIGNATURE_PKCS1);
66
 
134
 
67
                        return array($algo, $bits, $private, $public);
135
                        return array($algo, $bits, $private, $public);
68
                } catch (Exception $e) {
136
                } catch (Exception $e) {
69
                        global $openssl_supplement_last_error;
137
                        global $openssl_supplement_last_error;
70
                        $openssl_supplement_last_error = $e->getMessage();
138
                        $openssl_supplement_last_error = $e->getMessage();
71
                        return false;
139
                        return false;
72
                }
140
                }
73
        }
141
        }
74
 
142
 
75
        function openssl_pkey_export($res, &$privKey, $passphrase = null, $options = null) {
143
        function openssl_pkey_export($res, &$privKey, $passphrase = null, $options = null) {
76
                try {
144
                try {
77
                        if ($res instanceof \phpseclib3\Crypt\Common\PrivateKey /*\phpseclib3\Crypt\RSA\PrivateKey*/ ) {
145
                        if ($res instanceof \phpseclib3\Crypt\Common\PrivateKey /*\phpseclib3\Crypt\RSA\PrivateKey*/ ) {
78
                                $privKey = $res;
146
                                $privKey = $res;
79
                                if (!is_null($passphrase)) {
147
                                if (!is_null($passphrase)) {
80
                                        $privKey = $res->withPassword($passphrase);
148
                                        $privKey = $res->withPassword($passphrase);
81
                                }
149
                                }
82
                                $privKey = $privKey."";
150
                                $privKey = $privKey."";
83
                                return true;
151
                                return true;
84
                        } else if (is_string($res)) {
152
                        } else if (is_string($res)) {
85
                                $privKey = $res;
153
                                $privKey = $res;
86
                                if (!is_null($passphrase)) {
154
                                if (!is_null($passphrase)) {
87
                                        $privKey = \phpseclib3\Crypt\RSA::load($privKey);
155
                                        $privKey = \phpseclib3\Crypt\RSA::load($privKey);
88
                                        $privKey = $res->withPassword($passphrase);
156
                                        $privKey = $res->withPassword($passphrase);
89
                                        $privKey = $privKey."";
157
                                        $privKey = $privKey."";
90
                                }
158
                                }
91
                                return true;
159
                                return true;
92
                        } else if (is_array($res)) {
160
                        } else if (is_array($res)) {
93
                                $privKey = $res[2]."";
161
                                $privKey = $res[2]."";
94
                                if (!is_null($passphrase)) {
162
                                if (!is_null($passphrase)) {
95
                                        $privKey = \phpseclib3\Crypt\RSA::load($privKey);
163
                                        $privKey = \phpseclib3\Crypt\RSA::load($privKey);
96
                                        $privKey = $res->withPassword($passphrase);
164
                                        $privKey = $res->withPassword($passphrase);
97
                                        $privKey = $privKey."";
165
                                        $privKey = $privKey."";
98
                                }
166
                                }
99
                                return true;
167
                                return true;
100
                        } else {
168
                        } else {
101
                                throw new Exception("Invalid input datatype");
169
                                throw new Exception("Invalid input datatype");
102
                        }
170
                        }
103
                } catch (Exception $e) {
171
                } catch (Exception $e) {
104
                        global $openssl_supplement_last_error;
172
                        global $openssl_supplement_last_error;
105
                        $openssl_supplement_last_error = $e->getMessage();
173
                        $openssl_supplement_last_error = $e->getMessage();
106
                        return false;
174
                        return false;
107
                }
175
                }
108
        }
176
        }
109
 
177
 
110
        function openssl_pkey_get_details($res) {
178
        function openssl_pkey_get_details($res) {
111
                return array(
179
                return array(
112
                        "bits" => $res[1],
180
                        "bits" => $res[1],
113
                        "key" => $res[3]."",
181
                        "key" => $res[3]."",
114
                        "type" => $res[0]
182
                        "type" => $res[0]
115
                );
183
                );
116
        }
184
        }
117
 
185
 
118
        function openssl_public_encrypt($data, &$encrypted, $pubKey) {
186
        function openssl_public_encrypt($data, &$encrypted, $pubKey) {
119
                try {
187
                try {
120
                        if (is_string($pubKey)) $pubKey = openssl_pkey_get_public($pubKey);
188
                        if (is_string($pubKey)) $pubKey = openssl_pkey_get_public($pubKey);
121
                        if (!is_object($pubKey) || !method_exists($pubKey,'encrypt'))
189
                        if (!is_object($pubKey) || !method_exists($pubKey,'encrypt'))
122
                                throw new Exception("Invalid input datatype");
190
                                throw new Exception("Invalid input datatype");
123
                        $encrypted = $pubKey->encrypt($data);
191
                        $encrypted = $pubKey->encrypt($data);
124
                        return true;
192
                        return true;
125
                } catch (Exception $e) {
193
                } catch (Exception $e) {
126
                        global $openssl_supplement_last_error;
194
                        global $openssl_supplement_last_error;
127
                        $openssl_supplement_last_error = $e->getMessage();
195
                        $openssl_supplement_last_error = $e->getMessage();
128
                        return false;
196
                        return false;
129
                }
197
                }
130
        }
198
        }
131
 
199
 
132
        function openssl_private_decrypt($encrypted, &$decrypted, $privKey) {
200
        function openssl_private_decrypt($encrypted, &$decrypted, $privKey) {
133
                try {
201
                try {
134
                        if (is_string($privKey)) $privKey = openssl_pkey_get_private($privKey);
202
                        if (is_string($privKey)) $privKey = openssl_pkey_get_private($privKey);
135
                        if (!is_object($privKey) || !method_exists($privKey,'decrypt'))
203
                        if (!is_object($privKey) || !method_exists($privKey,'decrypt'))
136
                                throw new Exception("Invalid input datatype");
204
                                throw new Exception("Invalid input datatype");
137
                        $decrypted = $privKey->decrypt($encrypted);
205
                        $decrypted = $privKey->decrypt($encrypted);
138
                        return true;
206
                        return true;
139
                } catch (Exception $e) {
207
                } catch (Exception $e) {
140
                        global $openssl_supplement_last_error;
208
                        global $openssl_supplement_last_error;
141
                        $openssl_supplement_last_error = $e->getMessage();
209
                        $openssl_supplement_last_error = $e->getMessage();
142
                        return false;
210
                        return false;
143
                }
211
                }
144
        }
212
        }
145
 
213
 
146
        function openssl_verify($msg, $signature, $public, $algorithm=OPENSSL_ALGO_SHA1) {
214
        function openssl_verify($msg, $signature, $public, $algorithm=OPENSSL_ALGO_SHA1) {
147
                try {
215
                try {
148
                        if ($algorithm == OPENSSL_ALGO_SHA1) $algorithm = 'SHA1';
216
                        if ($algorithm == OPENSSL_ALGO_SHA1) $algorithm = 'SHA1';
149
                        if ($algorithm == OPENSSL_ALGO_SHA224) $algorithm = 'SHA224';
217
                        if ($algorithm == OPENSSL_ALGO_SHA224) $algorithm = 'SHA224';
150
                        if ($algorithm == OPENSSL_ALGO_SHA256) $algorithm = 'SHA256)';
218
                        if ($algorithm == OPENSSL_ALGO_SHA256) $algorithm = 'SHA256';
151
                        if ($algorithm == OPENSSL_ALGO_SHA384) $algorithm = 'SHA384';
219
                        if ($algorithm == OPENSSL_ALGO_SHA384) $algorithm = 'SHA384';
152
                        if ($algorithm == OPENSSL_ALGO_SHA512) $algorithm = 'SHA512';
220
                        if ($algorithm == OPENSSL_ALGO_SHA512) $algorithm = 'SHA512';
153
                        if ($algorithm == OPENSSL_ALGO_RMD160) $algorithm = 'RMD160';
221
                        if ($algorithm == OPENSSL_ALGO_RMD160) $algorithm = 'RMD160';
154
                        if ($algorithm == OPENSSL_ALGO_MD5) $algorithm = 'MD5';
222
                        if ($algorithm == OPENSSL_ALGO_MD5) $algorithm = 'MD5';
155
                        if ($algorithm == OPENSSL_ALGO_MD4) $algorithm = 'MD4';
223
                        if ($algorithm == OPENSSL_ALGO_MD4) $algorithm = 'MD4';
156
                        if (is_string($public)) $public = openssl_pkey_get_public($public);
224
                        if (is_string($public)) $public = openssl_pkey_get_public($public);
157
                        if (!is_object($public) || !method_exists($public,'verify'))
225
                        if (!is_object($public) || !method_exists($public,'verify'))
158
                                throw new Exception("Invalid input datatype");
226
                                throw new Exception("Invalid input datatype");
159
                        return $public->withHash($algorithm)->verify($msg, $signature) ? 1 : 0;
227
                        return $public->withHash($algorithm)->verify($msg, $signature) ? 1 : 0;
160
                } catch (Exception $e) {
228
                } catch (Exception $e) {
161
                        global $openssl_supplement_last_error;
229
                        global $openssl_supplement_last_error;
162
                        $openssl_supplement_last_error = $e->getMessage();
230
                        $openssl_supplement_last_error = $e->getMessage();
163
                        return false;
231
                        return false;
164
                }
232
                }
165
        }
233
        }
166
 
234
 
167
        function openssl_sign($msg, &$signature, $private, $algorithm=OPENSSL_ALGO_SHA1) {
235
        function openssl_sign($msg, &$signature, $private, $algorithm=OPENSSL_ALGO_SHA1) {
168
                try {
236
                try {
169
                        if ($algorithm == OPENSSL_ALGO_SHA1) $algorithm = 'SHA1';
237
                        if ($algorithm == OPENSSL_ALGO_SHA1) $algorithm = 'SHA1';
170
                        if ($algorithm == OPENSSL_ALGO_SHA224) $algorithm = 'SHA224';
238
                        if ($algorithm == OPENSSL_ALGO_SHA224) $algorithm = 'SHA224';
171
                        if ($algorithm == OPENSSL_ALGO_SHA256) $algorithm = 'SHA256)';
239
                        if ($algorithm == OPENSSL_ALGO_SHA256) $algorithm = 'SHA256';
172
                        if ($algorithm == OPENSSL_ALGO_SHA384) $algorithm = 'SHA384';
240
                        if ($algorithm == OPENSSL_ALGO_SHA384) $algorithm = 'SHA384';
173
                        if ($algorithm == OPENSSL_ALGO_SHA512) $algorithm = 'SHA512';
241
                        if ($algorithm == OPENSSL_ALGO_SHA512) $algorithm = 'SHA512';
174
                        if ($algorithm == OPENSSL_ALGO_RMD160) $algorithm = 'RMD160';
242
                        if ($algorithm == OPENSSL_ALGO_RMD160) $algorithm = 'RMD160';
175
                        if ($algorithm == OPENSSL_ALGO_MD5) $algorithm = 'MD5';
243
                        if ($algorithm == OPENSSL_ALGO_MD5) $algorithm = 'MD5';
176
                        if ($algorithm == OPENSSL_ALGO_MD4) $algorithm = 'MD4';
244
                        if ($algorithm == OPENSSL_ALGO_MD4) $algorithm = 'MD4';
177
                        if (is_string($private)) $private = openssl_pkey_get_private($private);
245
                        if (is_string($private)) $private = openssl_pkey_get_private($private);
178
                        if (!is_object($private) || !method_exists($private,'sign'))
246
                        if (!is_object($private) || !method_exists($private,'sign'))
179
                                throw new Exception("Invalid input datatype");
247
                                throw new Exception("Invalid input datatype");
180
                        $signature = $private->withHash($algorithm)->sign($msg);
248
                        $signature = $private->withHash($algorithm)->sign($msg);
181
                        return true;
249
                        return true;
182
                } catch (Exception $e) {
250
                } catch (Exception $e) {
183
                        global $openssl_supplement_last_error;
251
                        global $openssl_supplement_last_error;
184
                        $openssl_supplement_last_error = $e->getMessage();
252
                        $openssl_supplement_last_error = $e->getMessage();
185
                        return false;
253
                        return false;
186
                }
254
                }
187
        }
255
        }
188
 
256
 
189
        function openssl_error_string() {
257
        function openssl_error_string() {
190
                global $openssl_supplement_last_error;
258
                global $openssl_supplement_last_error;
191
                return $openssl_supplement_last_error;
259
                $res = $openssl_supplement_last_error;
-
 
260
                $openssl_supplement_last_error = '';
-
 
261
                return $res;
192
        }
262
        }
193
 
263
 
194
        function openssl_random_pseudo_bytes($len) {
264
        function openssl_random_pseudo_bytes($len) {
195
                /*
265
                /*
196
                if (function_exists('openssl_random_pseudo_bytes')) {
266
                if (function_exists('openssl_random_pseudo_bytes')) {
197
                        $a = openssl_random_pseudo_bytes($len);
267
                        $a = openssl_random_pseudo_bytes($len);
198
                        if ($a) return $a;
268
                        if ($a) return $a;
199
                }
269
                }
200
                */
270
                */
201
 
271
 
202
                if (function_exists('mcrypt_create_iv')) {
272
                if (function_exists('mcrypt_create_iv')) {
203
                        $a = bin2hex(mcrypt_create_iv($len, MCRYPT_DEV_URANDOM));
273
                        $a = bin2hex(mcrypt_create_iv($len, MCRYPT_DEV_URANDOM));
204
                        if ($a) return $a;
274
                        if ($a) return $a;
205
                }
275
                }
206
 
276
 
207
                if (function_exists('random_bytes')) {
277
                if (function_exists('random_bytes')) {
208
                        $a = random_bytes($len);
278
                        $a = random_bytes($len);
209
                        if ($a) return $a;
279
                        if ($a) return $a;
210
                }
280
                }
211
 
281
 
212
                // Fallback to non-secure RNG
282
                // Fallback to non-secure RNG
213
                $a = '';
283
                $a = '';
214
                while (strlen($a) < $len*2) {
284
                while (strlen($a) < $len*2) {
215
                        $a .= sha1(uniqid((string)mt_rand(), true));
285
                        $a .= sha1(uniqid((string)mt_rand(), true));
216
                }
286
                }
217
                $a = substr($a, 0, $len*2);
287
                $a = substr($a, 0, $len*2);
218
                return hex2bin($a);
288
                return hex2bin($a);
219
        }
289
        }
220
 
290
 
221
        function openssl_encrypt($data, $cipher_algo, $passphrase, $options=0, $iv="", &$tag=null, $aad="", $tag_length=16) {
291
        function openssl_encrypt($data, $cipher_algo, $passphrase, $options=0, $iv="", &$tag=null, $aad="", $tag_length=16) {
222
                try {
292
                try {
223
                        if (!is_null($tag)) throw new Exception("tag not implemented");
293
                        if (!is_null($tag)) throw new Exception("tag not implemented");
224
                        if ($aad != "") throw new Exception("aad not implemented");
294
                        if ($aad != "") throw new Exception("aad not implemented");
225
                        if ($tag_length != 16) throw new Exception("tag_length not implemented");
295
                        if ($tag_length != 16) throw new Exception("tag_length not implemented");
226
                        if (!preg_match('@AES\\-(.+)\\-(.+)@i', $cipher_algo, $m)) throw new Exception("Algo not implemented");
296
                        if (!preg_match('@AES\\-(.+)\\-(.+)@i', $cipher_algo, $m)) throw new Exception("Algo not implemented");
227
                        if (($options & OPENSSL_ZERO_PADDING) != 0) throw new Exception("OPENSSL_ZERO_PADDING not implemented");
297
                        if (($options & OPENSSL_ZERO_PADDING) != 0) throw new Exception("OPENSSL_ZERO_PADDING not implemented");
228
                        $aes = new \phpseclib3\Crypt\AES($m[2]);
298
                        $aes = new \phpseclib3\Crypt\AES($m[2]);
229
                        $aes->setKeyLength($m[1]);
299
                        $aes->setKeyLength($m[1]);
230
                        $passphrase = substr($passphrase, 0, $m[1]/8);
300
                        $passphrase = substr($passphrase, 0, $m[1]/8);
231
                        $passphrase = str_pad($passphrase, $m[1]/8, "\0", STR_PAD_RIGHT);
301
                        $passphrase = str_pad($passphrase, $m[1]/8, "\0", STR_PAD_RIGHT);
232
                        $aes->setKey($passphrase);
302
                        $aes->setKey($passphrase);
233
                        $aes->setIV($iv);
303
                        $aes->setIV($iv);
234
                        $res = $aes->encrypt($data);
304
                        $res = $aes->encrypt($data);
235
                        if (($options & OPENSSL_RAW_DATA) == 0) $res = base64_encode($res);
305
                        if (($options & OPENSSL_RAW_DATA) == 0) $res = base64_encode($res);
236
                        return $res;
306
                        return $res;
237
                } catch (Exception $e) {
307
                } catch (Exception $e) {
238
                        global $openssl_supplement_last_error;
308
                        global $openssl_supplement_last_error;
239
                        $openssl_supplement_last_error = $e->getMessage();
309
                        $openssl_supplement_last_error = $e->getMessage();
240
                        return false;
310
                        return false;
241
                }
311
                }
242
        }
312
        }
243
 
313
 
244
        function openssl_decrypt($data, $cipher_algo, $passphrase, $options=0, $iv="", $tag=null, $aad="") {
314
        function openssl_decrypt($data, $cipher_algo, $passphrase, $options=0, $iv="", $tag=null, $aad="") {
245
                try {
315
                try {
246
                        if (!is_null($tag)) throw new Exception("tag not implemented");
316
                        if (!is_null($tag)) throw new Exception("tag not implemented");
247
                        if ($aad != "") throw new Exception("aad not implemented");
317
                        if ($aad != "") throw new Exception("aad not implemented");
248
                        if (!preg_match('@AES\\-(.+)\\-(.+)@i', $cipher_algo, $m)) throw new Exception("Algo not implemented");
318
                        if (!preg_match('@AES\\-(.+)\\-(.+)@i', $cipher_algo, $m)) throw new Exception("Algo not implemented");
249
                        if (($options & OPENSSL_ZERO_PADDING) != 0) throw new Exception("OPENSSL_ZERO_PADDING not implemented");
319
                        if (($options & OPENSSL_ZERO_PADDING) != 0) throw new Exception("OPENSSL_ZERO_PADDING not implemented");
250
                        $aes = new \phpseclib3\Crypt\AES($m[2]);
320
                        $aes = new \phpseclib3\Crypt\AES($m[2]);
251
                        $aes->setKeyLength($m[1]);
321
                        $aes->setKeyLength($m[1]);
252
                        $passphrase = substr($passphrase, 0, $m[1]/8);
322
                        $passphrase = substr($passphrase, 0, $m[1]/8);
253
                        $passphrase = str_pad($passphrase, $m[1]/8, "\0", STR_PAD_RIGHT);
323
                        $passphrase = str_pad($passphrase, $m[1]/8, "\0", STR_PAD_RIGHT);
254
                        $aes->setKey($passphrase);
324
                        $aes->setKey($passphrase);
255
                        $aes->setIV($iv);
325
                        $aes->setIV($iv);
256
                        if (($options & OPENSSL_RAW_DATA) == 0) $data = base64_decode($data);
326
                        if (($options & OPENSSL_RAW_DATA) == 0) $data = base64_decode($data);
257
                        return $aes->decrypt($data);
327
                        return $aes->decrypt($data);
258
                } catch (Exception $e) {
328
                } catch (Exception $e) {
259
                        global $openssl_supplement_last_error;
329
                        global $openssl_supplement_last_error;
260
                        $openssl_supplement_last_error = $e->getMessage();
330
                        $openssl_supplement_last_error = $e->getMessage();
261
                        return false;
331
                        return false;
262
                }
332
                }
263
        }
333
        }
264
 
334
 
265
        function openssl_free_key($key) {
335
        function openssl_free_key($key) {
266
                // does nothing
336
                // does nothing
267
        }
337
        }
268
 
338
 
-
 
339
        function openssl_get_privatekey($key, $passphrase=null) {
-
 
340
                return openssl_pkey_get_private($key, $passphrase=null);
-
 
341
        }
-
 
342
 
269
        function openssl_pkey_get_private($key, $passphrase=null) {
343
        function openssl_pkey_get_private($key, $passphrase=null) {
270
                try {
344
                try {
271
                        if (substr($key,0,7) === 'file://') {
345
                        if (substr($key,0,7) === 'file://') {
272
                                if (!file_exists($file = substr($key, 7))) throw new Exception("file not found");
346
                                if (!file_exists($file = substr($key, 7))) throw new Exception("file not found");
273
                                $key = file_get_contents($file);
347
                                $key = file_get_contents($file);
274
                        }
348
                        }
275
                        if (is_null($passphrase)) $passphrase = false;
349
                        if (is_null($passphrase)) $passphrase = false;
276
                        $privKey = \phpseclib3\Crypt\RSA::load($key, $passphrase);
350
                        $privKey = \phpseclib3\Crypt\RSA::load($key, $passphrase);
277
                        return $privKey->withPassword(false)->withPadding(\phpseclib3\Crypt\RSA::ENCRYPTION_PKCS1 | \phpseclib3\Crypt\RSA::SIGNATURE_PKCS1); /** @phpstan-ignore-line */ // Call to an undefined method phpseclib3\Crypt\Common\AsymmetricKey::withPadding().
351
                        return $privKey->withPassword(false)->withPadding(\phpseclib3\Crypt\RSA::ENCRYPTION_PKCS1 | \phpseclib3\Crypt\RSA::SIGNATURE_PKCS1); /** @phpstan-ignore-line */ // Call to an undefined method phpseclib3\Crypt\Common\AsymmetricKey::withPadding().
278
                } catch (Exception $e) {
352
                } catch (Exception $e) {
279
                        global $openssl_supplement_last_error;
353
                        global $openssl_supplement_last_error;
280
                        $openssl_supplement_last_error = $e->getMessage();
354
                        $openssl_supplement_last_error = $e->getMessage();
281
                        return false;
355
                        return false;
282
                }
356
                }
283
        }
357
        }
284
 
358
 
-
 
359
        function openssl_get_publickey($public_key) {
-
 
360
                return openssl_pkey_get_public($public_key);
-
 
361
        }
-
 
362
 
285
        function openssl_pkey_get_public($public_key) {
363
        function openssl_pkey_get_public($public_key) {
286
                try {
364
                try {
287
                        if (substr($public_key,0,7) === 'file://') {
365
                        if (substr($public_key,0,7) === 'file://') {
288
                                if (!file_exists($file = substr($public_key, 7))) throw new Exception("file not found");
366
                                if (!file_exists($file = substr($public_key, 7))) throw new Exception("file not found");
289
                                $public_key = file_get_contents($file);
367
                                $public_key = file_get_contents($file);
290
                        }
368
                        }
291
                        $pubKey = \phpseclib3\Crypt\RSA::load($public_key);
369
                        $pubKey = \phpseclib3\Crypt\RSA::load($public_key);
292
                        return $pubKey->withPadding(\phpseclib3\Crypt\RSA::ENCRYPTION_PKCS1 | \phpseclib3\Crypt\RSA::SIGNATURE_PKCS1); /** @phpstan-ignore-line */ // Call to an undefined method phpseclib3\Crypt\Common\AsymmetricKey::withPadding().
370
                        return $pubKey->withPadding(\phpseclib3\Crypt\RSA::ENCRYPTION_PKCS1 | \phpseclib3\Crypt\RSA::SIGNATURE_PKCS1); /** @phpstan-ignore-line */ // Call to an undefined method phpseclib3\Crypt\Common\AsymmetricKey::withPadding().
293
                } catch (Exception $e) {
371
                } catch (Exception $e) {
294
                        global $openssl_supplement_last_error;
372
                        global $openssl_supplement_last_error;
295
                        $openssl_supplement_last_error = $e->getMessage();
373
                        $openssl_supplement_last_error = $e->getMessage();
296
                        return false;
374
                        return false;
297
                }
375
                }
298
        }
376
        }
-
 
377
 
-
 
378
        function openssl_pkey_free($key) {
-
 
379
        }
299
 
380
 
300
}
381
}
301
 
382