Exceptions
5 days ago
apache.php
5 days ago
apcu.php
5 days ago
array.php
5 days ago
bzip2.php
5 days ago
calendar.php
5 days ago
classobj.php
5 days ago
com.php
5 days ago
cubrid.php
5 days ago
curl.php
5 days ago
datetime.php
5 days ago
dir.php
5 days ago
eio.php
5 days ago
errorfunc.php
5 days ago
exec.php
5 days ago
fileinfo.php
5 days ago
filesystem.php
5 days ago
filter.php
5 days ago
fpm.php
5 days ago
ftp.php
5 days ago
funchand.php
5 days ago
functionsList.php
5 days ago
gmp.php
5 days ago
gnupg.php
5 days ago
hash.php
5 days ago
ibase.php
5 days ago
ibmDb2.php
5 days ago
iconv.php
5 days ago
image.php
5 days ago
imap.php
5 days ago
info.php
5 days ago
ingres-ii.php
5 days ago
inotify.php
5 days ago
json.php
5 days ago
ldap.php
5 days ago
libxml.php
5 days ago
lzf.php
5 days ago
mailparse.php
5 days ago
mbstring.php
5 days ago
misc.php
5 days ago
msql.php
5 days ago
mysql.php
5 days ago
mysqli.php
5 days ago
mysqlndMs.php
5 days ago
mysqlndQc.php
5 days ago
network.php
5 days ago
oci8.php
5 days ago
opcache.php
5 days ago
openssl.php
5 days ago
outcontrol.php
5 days ago
password.php
5 days ago
pcntl.php
5 days ago
pcre.php
5 days ago
pdf.php
5 days ago
pgsql.php
5 days ago
posix.php
5 days ago
ps.php
5 days ago
pspell.php
5 days ago
readline.php
5 days ago
rpminfo.php
5 days ago
rrd.php
5 days ago
sem.php
5 days ago
session.php
5 days ago
shmop.php
5 days ago
simplexml.php
5 days ago
sockets.php
5 days ago
sodium.php
5 days ago
solr.php
5 days ago
spl.php
5 days ago
sqlsrv.php
5 days ago
ssdeep.php
5 days ago
ssh2.php
5 days ago
stream.php
5 days ago
strings.php
5 days ago
swoole.php
5 days ago
uodbc.php
5 days ago
uopz.php
5 days ago
url.php
5 days ago
var.php
5 days ago
xdiff.php
5 days ago
xml.php
5 days ago
xmlrpc.php
5 days ago
yaml.php
5 days ago
yaz.php
5 days ago
zip.php
5 days ago
zlib.php
5 days ago
openssl.php
1022 lines
| 1 | <?php |
| 2 | |
| 3 | namespace ProfilePressVendor\Safe; |
| 4 | |
| 5 | use ProfilePressVendor\Safe\Exceptions\OpensslException; |
| 6 | /** |
| 7 | * Gets the cipher initialization vector (iv) length. |
| 8 | * |
| 9 | * @param string $method The cipher method, see openssl_get_cipher_methods for a list of potential values. |
| 10 | * @return int Returns the cipher length on success. |
| 11 | * @throws OpensslException |
| 12 | * |
| 13 | */ |
| 14 | function openssl_cipher_iv_length(string $method): int |
| 15 | { |
| 16 | error_clear_last(); |
| 17 | $result = \openssl_cipher_iv_length($method); |
| 18 | if ($result === \false) { |
| 19 | throw OpensslException::createFromPhpError(); |
| 20 | } |
| 21 | return $result; |
| 22 | } |
| 23 | /** |
| 24 | * openssl_csr_export_to_file takes the Certificate |
| 25 | * Signing Request represented by csr and saves it |
| 26 | * in PEM format into the file named by outfilename. |
| 27 | * |
| 28 | * @param string|resource $csr See CSR parameters for a list of valid values. |
| 29 | * @param string $outfilename Path to the output file. |
| 30 | * @param bool $notext |
| 31 | * The optional parameter notext affects |
| 32 | * the verbosity of the output; if it is FALSE, then additional human-readable |
| 33 | * information is included in the output. The default value of |
| 34 | * notext is TRUE. |
| 35 | * @throws OpensslException |
| 36 | * |
| 37 | */ |
| 38 | function openssl_csr_export_to_file($csr, string $outfilename, bool $notext = \true): void |
| 39 | { |
| 40 | error_clear_last(); |
| 41 | $result = \openssl_csr_export_to_file($csr, $outfilename, $notext); |
| 42 | if ($result === \false) { |
| 43 | throw OpensslException::createFromPhpError(); |
| 44 | } |
| 45 | } |
| 46 | /** |
| 47 | * openssl_csr_export takes the Certificate Signing |
| 48 | * Request represented by csr and stores it in |
| 49 | * PEM format in out, which is passed by |
| 50 | * reference. |
| 51 | * |
| 52 | * @param string|resource $csr See CSR parameters for a list of valid values. |
| 53 | * @param string|null $out on success, this string will contain the PEM encoded CSR |
| 54 | * @param bool $notext |
| 55 | * The optional parameter notext affects |
| 56 | * the verbosity of the output; if it is FALSE, then additional human-readable |
| 57 | * information is included in the output. The default value of |
| 58 | * notext is TRUE. |
| 59 | * @throws OpensslException |
| 60 | * |
| 61 | */ |
| 62 | function openssl_csr_export($csr, ?string &$out, bool $notext = \true): void |
| 63 | { |
| 64 | error_clear_last(); |
| 65 | $result = \openssl_csr_export($csr, $out, $notext); |
| 66 | if ($result === \false) { |
| 67 | throw OpensslException::createFromPhpError(); |
| 68 | } |
| 69 | } |
| 70 | /** |
| 71 | * openssl_csr_get_subject returns subject |
| 72 | * distinguished name information encoded in the csr |
| 73 | * including fields commonName (CN), organizationName (O), countryName (C) etc. |
| 74 | * |
| 75 | * @param string|resource $csr See CSR parameters for a list of valid values. |
| 76 | * @param bool $use_shortnames shortnames controls how the data is indexed in the |
| 77 | * array - if shortnames is TRUE (the default) then |
| 78 | * fields will be indexed with the short name form, otherwise, the long name |
| 79 | * form will be used - e.g.: CN is the shortname form of commonName. |
| 80 | * @return array Returns an associative array with subject description. |
| 81 | * @throws OpensslException |
| 82 | * |
| 83 | */ |
| 84 | function openssl_csr_get_subject($csr, bool $use_shortnames = \true): array |
| 85 | { |
| 86 | error_clear_last(); |
| 87 | $result = \openssl_csr_get_subject($csr, $use_shortnames); |
| 88 | if ($result === \false) { |
| 89 | throw OpensslException::createFromPhpError(); |
| 90 | } |
| 91 | return $result; |
| 92 | } |
| 93 | /** |
| 94 | * openssl_csr_new generates a new CSR (Certificate Signing Request) |
| 95 | * based on the information provided by dn. |
| 96 | * |
| 97 | * @param array $dn The Distinguished Name or subject fields to be used in the certificate. |
| 98 | * @param resource $privkey privkey should be set to a private key that was |
| 99 | * previously generated by openssl_pkey_new (or |
| 100 | * otherwise obtained from the other openssl_pkey family of functions). |
| 101 | * The corresponding public portion of the key will be used to sign the |
| 102 | * CSR. |
| 103 | * @param array $configargs By default, the information in your system openssl.conf |
| 104 | * is used to initialize the request; you can specify a configuration file |
| 105 | * section by setting the config_section_section key of |
| 106 | * configargs. You can also specify an alternative |
| 107 | * openssl configuration file by setting the value of the |
| 108 | * config key to the path of the file you want to use. |
| 109 | * The following keys, if present in configargs |
| 110 | * behave as their equivalents in the openssl.conf, as |
| 111 | * listed in the table below. |
| 112 | * |
| 113 | * Configuration overrides |
| 114 | * |
| 115 | * |
| 116 | * |
| 117 | * configargs key |
| 118 | * type |
| 119 | * openssl.conf equivalent |
| 120 | * description |
| 121 | * |
| 122 | * |
| 123 | * |
| 124 | * |
| 125 | * digest_alg |
| 126 | * string |
| 127 | * default_md |
| 128 | * Digest method or signature hash, usually one of openssl_get_md_methods |
| 129 | * |
| 130 | * |
| 131 | * x509_extensions |
| 132 | * string |
| 133 | * x509_extensions |
| 134 | * Selects which extensions should be used when creating an x509 |
| 135 | * certificate |
| 136 | * |
| 137 | * |
| 138 | * req_extensions |
| 139 | * string |
| 140 | * req_extensions |
| 141 | * Selects which extensions should be used when creating a CSR |
| 142 | * |
| 143 | * |
| 144 | * private_key_bits |
| 145 | * integer |
| 146 | * default_bits |
| 147 | * Specifies how many bits should be used to generate a private |
| 148 | * key |
| 149 | * |
| 150 | * |
| 151 | * private_key_type |
| 152 | * integer |
| 153 | * none |
| 154 | * Specifies the type of private key to create. This can be one |
| 155 | * of OPENSSL_KEYTYPE_DSA, |
| 156 | * OPENSSL_KEYTYPE_DH, |
| 157 | * OPENSSL_KEYTYPE_RSA or |
| 158 | * OPENSSL_KEYTYPE_EC. |
| 159 | * The default value is OPENSSL_KEYTYPE_RSA. |
| 160 | * |
| 161 | * |
| 162 | * |
| 163 | * encrypt_key |
| 164 | * boolean |
| 165 | * encrypt_key |
| 166 | * Should an exported key (with passphrase) be encrypted? |
| 167 | * |
| 168 | * |
| 169 | * encrypt_key_cipher |
| 170 | * integer |
| 171 | * none |
| 172 | * |
| 173 | * One of cipher constants. |
| 174 | * |
| 175 | * |
| 176 | * |
| 177 | * curve_name |
| 178 | * string |
| 179 | * none |
| 180 | * |
| 181 | * One of openssl_get_curve_names. |
| 182 | * |
| 183 | * |
| 184 | * |
| 185 | * config |
| 186 | * string |
| 187 | * N/A |
| 188 | * |
| 189 | * Path to your own alternative openssl.conf file. |
| 190 | * |
| 191 | * |
| 192 | * |
| 193 | * |
| 194 | * |
| 195 | * @param array $extraattribs extraattribs is used to specify additional |
| 196 | * configuration options for the CSR. Both dn and |
| 197 | * extraattribs are associative arrays whose keys are |
| 198 | * converted to OIDs and applied to the relevant part of the request. |
| 199 | * @return resource Returns the CSR. |
| 200 | * @throws OpensslException |
| 201 | * |
| 202 | */ |
| 203 | function openssl_csr_new(array $dn, &$privkey, array $configargs = null, array $extraattribs = null) |
| 204 | { |
| 205 | error_clear_last(); |
| 206 | if ($extraattribs !== null) { |
| 207 | $result = \openssl_csr_new($dn, $privkey, $configargs, $extraattribs); |
| 208 | } elseif ($configargs !== null) { |
| 209 | $result = \openssl_csr_new($dn, $privkey, $configargs); |
| 210 | } else { |
| 211 | $result = \openssl_csr_new($dn, $privkey); |
| 212 | } |
| 213 | if ($result === \false) { |
| 214 | throw OpensslException::createFromPhpError(); |
| 215 | } |
| 216 | return $result; |
| 217 | } |
| 218 | /** |
| 219 | * openssl_csr_sign generates an x509 certificate |
| 220 | * resource from the given CSR. |
| 221 | * |
| 222 | * @param string|resource $csr A CSR previously generated by openssl_csr_new. |
| 223 | * It can also be the path to a PEM encoded CSR when specified as |
| 224 | * file://path/to/csr or an exported string generated |
| 225 | * by openssl_csr_export. |
| 226 | * @param mixed $cacert The generated certificate will be signed by cacert. |
| 227 | * If cacert is NULL, the generated certificate |
| 228 | * will be a self-signed certificate. |
| 229 | * @param string|resource|array $priv_key priv_key is the private key that corresponds to |
| 230 | * cacert. |
| 231 | * @param int $days days specifies the length of time for which the |
| 232 | * generated certificate will be valid, in days. |
| 233 | * @param array $configargs You can finetune the CSR signing by configargs. |
| 234 | * See openssl_csr_new for more information about |
| 235 | * configargs. |
| 236 | * @param int $serial An optional the serial number of issued certificate. If not specified |
| 237 | * it will default to 0. |
| 238 | * @return resource Returns an x509 certificate resource on success, FALSE on failure. |
| 239 | * @throws OpensslException |
| 240 | * |
| 241 | */ |
| 242 | function openssl_csr_sign($csr, $cacert, $priv_key, int $days, array $configargs = null, int $serial = 0) |
| 243 | { |
| 244 | error_clear_last(); |
| 245 | if ($serial !== 0) { |
| 246 | $result = \openssl_csr_sign($csr, $cacert, $priv_key, $days, $configargs, $serial); |
| 247 | } elseif ($configargs !== null) { |
| 248 | $result = \openssl_csr_sign($csr, $cacert, $priv_key, $days, $configargs); |
| 249 | } else { |
| 250 | $result = \openssl_csr_sign($csr, $cacert, $priv_key, $days); |
| 251 | } |
| 252 | if ($result === \false) { |
| 253 | throw OpensslException::createFromPhpError(); |
| 254 | } |
| 255 | return $result; |
| 256 | } |
| 257 | /** |
| 258 | * Takes a raw or base64 encoded string and decrypts it using a given method and key. |
| 259 | * |
| 260 | * @param string $data The encrypted message to be decrypted. |
| 261 | * @param string $method The cipher method. For a list of available cipher methods, use |
| 262 | * openssl_get_cipher_methods. |
| 263 | * @param string $key The key. |
| 264 | * @param int $options options can be one of |
| 265 | * OPENSSL_RAW_DATA, |
| 266 | * OPENSSL_ZERO_PADDING. |
| 267 | * @param string $iv A non-NULL Initialization Vector. |
| 268 | * @param string $tag The authentication tag in AEAD cipher mode. If it is incorrect, the authentication fails and the function returns FALSE. |
| 269 | * @param string $aad Additional authentication data. |
| 270 | * @return string The decrypted string on success. |
| 271 | * @throws OpensslException |
| 272 | * |
| 273 | */ |
| 274 | function openssl_decrypt(string $data, string $method, string $key, int $options = 0, string $iv = "", string $tag = "", string $aad = ""): string |
| 275 | { |
| 276 | error_clear_last(); |
| 277 | $result = \openssl_decrypt($data, $method, $key, $options, $iv, $tag, $aad); |
| 278 | if ($result === \false) { |
| 279 | throw OpensslException::createFromPhpError(); |
| 280 | } |
| 281 | return $result; |
| 282 | } |
| 283 | /** |
| 284 | * The shared secret returned by openssl_dh_compute_key is |
| 285 | * often used as an encryption key to secretly communicate with a remote party. |
| 286 | * This is known as the Diffie-Hellman key exchange. |
| 287 | * |
| 288 | * @param string $pub_key DH Public key of the remote party. |
| 289 | * @param resource $dh_key A local DH private key, corresponding to the public key to be shared with the remote party. |
| 290 | * @return string Returns shared secret on success. |
| 291 | * @throws OpensslException |
| 292 | * |
| 293 | */ |
| 294 | function openssl_dh_compute_key(string $pub_key, $dh_key): string |
| 295 | { |
| 296 | error_clear_last(); |
| 297 | $result = \openssl_dh_compute_key($pub_key, $dh_key); |
| 298 | if ($result === \false) { |
| 299 | throw OpensslException::createFromPhpError(); |
| 300 | } |
| 301 | return $result; |
| 302 | } |
| 303 | /** |
| 304 | * Computes a digest hash value for the given data using a given method, |
| 305 | * and returns a raw or binhex encoded string. |
| 306 | * |
| 307 | * @param string $data The data. |
| 308 | * @param string $method The digest method to use, e.g. "sha256", see openssl_get_md_methods for a list of available digest methods. |
| 309 | * @param bool $raw_output Setting to TRUE will return as raw output data, otherwise the return |
| 310 | * value is binhex encoded. |
| 311 | * @return string Returns the digested hash value on success. |
| 312 | * @throws OpensslException |
| 313 | * |
| 314 | */ |
| 315 | function openssl_digest(string $data, string $method, bool $raw_output = \false): string |
| 316 | { |
| 317 | error_clear_last(); |
| 318 | $result = \openssl_digest($data, $method, $raw_output); |
| 319 | if ($result === \false) { |
| 320 | throw OpensslException::createFromPhpError(); |
| 321 | } |
| 322 | return $result; |
| 323 | } |
| 324 | /** |
| 325 | * openssl_open opens (decrypts) |
| 326 | * sealed_data using the private key associated with |
| 327 | * the key identifier priv_key_id and the envelope key |
| 328 | * env_key, and fills |
| 329 | * open_data with the decrypted data. |
| 330 | * The envelope key is generated when the |
| 331 | * data are sealed and can only be used by one specific private key. See |
| 332 | * openssl_seal for more information. |
| 333 | * |
| 334 | * @param string $sealed_data |
| 335 | * @param string|null $open_data If the call is successful the opened data is returned in this |
| 336 | * parameter. |
| 337 | * @param string $env_key |
| 338 | * @param string|array|resource $priv_key_id |
| 339 | * @param string $method The cipher method. |
| 340 | * @param string $iv The initialization vector. |
| 341 | * @throws OpensslException |
| 342 | * |
| 343 | */ |
| 344 | function openssl_open(string $sealed_data, ?string &$open_data, string $env_key, $priv_key_id, string $method = "RC4", string $iv = null): void |
| 345 | { |
| 346 | error_clear_last(); |
| 347 | if ($iv !== null) { |
| 348 | $result = \openssl_open($sealed_data, $open_data, $env_key, $priv_key_id, $method, $iv); |
| 349 | } else { |
| 350 | $result = \openssl_open($sealed_data, $open_data, $env_key, $priv_key_id, $method); |
| 351 | } |
| 352 | if ($result === \false) { |
| 353 | throw OpensslException::createFromPhpError(); |
| 354 | } |
| 355 | } |
| 356 | /** |
| 357 | * openssl_pbkdf2 computes PBKDF2 (Password-Based Key Derivation Function 2), |
| 358 | * a key derivation function defined in PKCS5 v2. |
| 359 | * |
| 360 | * @param string $password Password from which the derived key is generated. |
| 361 | * @param string $salt PBKDF2 recommends a crytographic salt of at least 64 bits (8 bytes). |
| 362 | * @param int $key_length Length of desired output key. |
| 363 | * @param int $iterations The number of iterations desired. NIST |
| 364 | * recommends at least 10,000. |
| 365 | * @param string $digest_algorithm Optional hash or digest algorithm from openssl_get_md_methods. Defaults to SHA-1. |
| 366 | * @return string Returns raw binary string. |
| 367 | * @throws OpensslException |
| 368 | * |
| 369 | */ |
| 370 | function openssl_pbkdf2(string $password, string $salt, int $key_length, int $iterations, string $digest_algorithm = "sha1"): string |
| 371 | { |
| 372 | error_clear_last(); |
| 373 | $result = \openssl_pbkdf2($password, $salt, $key_length, $iterations, $digest_algorithm); |
| 374 | if ($result === \false) { |
| 375 | throw OpensslException::createFromPhpError(); |
| 376 | } |
| 377 | return $result; |
| 378 | } |
| 379 | /** |
| 380 | * openssl_pkcs12_export_to_file stores |
| 381 | * x509 into a file named by |
| 382 | * filename in a PKCS#12 file format. |
| 383 | * |
| 384 | * @param string|resource $x509 See Key/Certificate parameters for a list of valid values. |
| 385 | * @param string $filename Path to the output file. |
| 386 | * @param string|array|resource $priv_key Private key component of PKCS#12 file. |
| 387 | * See Public/Private Key parameters for a list of valid values. |
| 388 | * @param string $pass Encryption password for unlocking the PKCS#12 file. |
| 389 | * @param array $args Optional array, other keys will be ignored. |
| 390 | * |
| 391 | * |
| 392 | * |
| 393 | * |
| 394 | * Key |
| 395 | * Description |
| 396 | * |
| 397 | * |
| 398 | * |
| 399 | * |
| 400 | * "extracerts" |
| 401 | * array of extra certificates or a single certificate to be included in the PKCS#12 file. |
| 402 | * |
| 403 | * |
| 404 | * "friendlyname" |
| 405 | * string to be used for the supplied certificate and key |
| 406 | * |
| 407 | * |
| 408 | * |
| 409 | * |
| 410 | * @throws OpensslException |
| 411 | * |
| 412 | */ |
| 413 | function openssl_pkcs12_export_to_file($x509, string $filename, $priv_key, string $pass, array $args = null): void |
| 414 | { |
| 415 | error_clear_last(); |
| 416 | if ($args !== null) { |
| 417 | $result = \openssl_pkcs12_export_to_file($x509, $filename, $priv_key, $pass, $args); |
| 418 | } else { |
| 419 | $result = \openssl_pkcs12_export_to_file($x509, $filename, $priv_key, $pass); |
| 420 | } |
| 421 | if ($result === \false) { |
| 422 | throw OpensslException::createFromPhpError(); |
| 423 | } |
| 424 | } |
| 425 | /** |
| 426 | * openssl_pkcs12_export stores |
| 427 | * x509 into a string named by |
| 428 | * out in a PKCS#12 file format. |
| 429 | * |
| 430 | * @param string|resource $x509 See Key/Certificate parameters for a list of valid values. |
| 431 | * @param string|null $out On success, this will hold the PKCS#12. |
| 432 | * @param string|array|resource $priv_key Private key component of PKCS#12 file. |
| 433 | * See Public/Private Key parameters for a list of valid values. |
| 434 | * @param string $pass Encryption password for unlocking the PKCS#12 file. |
| 435 | * @param array $args Optional array, other keys will be ignored. |
| 436 | * |
| 437 | * |
| 438 | * |
| 439 | * |
| 440 | * Key |
| 441 | * Description |
| 442 | * |
| 443 | * |
| 444 | * |
| 445 | * |
| 446 | * "extracerts" |
| 447 | * array of extra certificates or a single certificate to be included in the PKCS#12 file. |
| 448 | * |
| 449 | * |
| 450 | * "friendlyname" |
| 451 | * string to be used for the supplied certificate and key |
| 452 | * |
| 453 | * |
| 454 | * |
| 455 | * |
| 456 | * @throws OpensslException |
| 457 | * |
| 458 | */ |
| 459 | function openssl_pkcs12_export($x509, ?string &$out, $priv_key, string $pass, array $args = null): void |
| 460 | { |
| 461 | error_clear_last(); |
| 462 | if ($args !== null) { |
| 463 | $result = \openssl_pkcs12_export($x509, $out, $priv_key, $pass, $args); |
| 464 | } else { |
| 465 | $result = \openssl_pkcs12_export($x509, $out, $priv_key, $pass); |
| 466 | } |
| 467 | if ($result === \false) { |
| 468 | throw OpensslException::createFromPhpError(); |
| 469 | } |
| 470 | } |
| 471 | /** |
| 472 | * openssl_pkcs12_read parses the PKCS#12 certificate store supplied by |
| 473 | * pkcs12 into a array named |
| 474 | * certs. |
| 475 | * |
| 476 | * @param string $pkcs12 The certificate store contents, not its file name. |
| 477 | * @param array|null $certs On success, this will hold the Certificate Store Data. |
| 478 | * @param string $pass Encryption password for unlocking the PKCS#12 file. |
| 479 | * @throws OpensslException |
| 480 | * |
| 481 | */ |
| 482 | function openssl_pkcs12_read(string $pkcs12, ?array &$certs, string $pass): void |
| 483 | { |
| 484 | error_clear_last(); |
| 485 | $result = \openssl_pkcs12_read($pkcs12, $certs, $pass); |
| 486 | if ($result === \false) { |
| 487 | throw OpensslException::createFromPhpError(); |
| 488 | } |
| 489 | } |
| 490 | /** |
| 491 | * Decrypts the S/MIME encrypted message contained in the file specified by |
| 492 | * infilename using the certificate and its |
| 493 | * associated private key specified by recipcert and |
| 494 | * recipkey. |
| 495 | * |
| 496 | * @param string $infilename |
| 497 | * @param string $outfilename The decrypted message is written to the file specified by |
| 498 | * outfilename. |
| 499 | * @param string|resource $recipcert |
| 500 | * @param string|resource|array $recipkey |
| 501 | * @throws OpensslException |
| 502 | * |
| 503 | */ |
| 504 | function openssl_pkcs7_decrypt(string $infilename, string $outfilename, $recipcert, $recipkey = null): void |
| 505 | { |
| 506 | error_clear_last(); |
| 507 | if ($recipkey !== null) { |
| 508 | $result = \openssl_pkcs7_decrypt($infilename, $outfilename, $recipcert, $recipkey); |
| 509 | } else { |
| 510 | $result = \openssl_pkcs7_decrypt($infilename, $outfilename, $recipcert); |
| 511 | } |
| 512 | if ($result === \false) { |
| 513 | throw OpensslException::createFromPhpError(); |
| 514 | } |
| 515 | } |
| 516 | /** |
| 517 | * openssl_pkcs7_encrypt takes the contents of the |
| 518 | * file named infile and encrypts them using an RC2 |
| 519 | * 40-bit cipher so that they can only be read by the intended recipients |
| 520 | * specified by recipcerts. |
| 521 | * |
| 522 | * @param string $infile |
| 523 | * @param string $outfile |
| 524 | * @param string|resource|array $recipcerts Either a lone X.509 certificate, or an array of X.509 certificates. |
| 525 | * @param array $headers headers is an array of headers that |
| 526 | * will be prepended to the data after it has been encrypted. |
| 527 | * |
| 528 | * headers can be either an associative array |
| 529 | * keyed by header name, or an indexed array, where each element contains |
| 530 | * a single header line. |
| 531 | * @param int $flags flags can be used to specify options that affect |
| 532 | * the encoding process - see PKCS7 |
| 533 | * constants. |
| 534 | * @param int $cipherid One of cipher constants. |
| 535 | * @throws OpensslException |
| 536 | * |
| 537 | */ |
| 538 | function openssl_pkcs7_encrypt(string $infile, string $outfile, $recipcerts, array $headers, int $flags = 0, int $cipherid = \OPENSSL_CIPHER_RC2_40): void |
| 539 | { |
| 540 | error_clear_last(); |
| 541 | $result = \openssl_pkcs7_encrypt($infile, $outfile, $recipcerts, $headers, $flags, $cipherid); |
| 542 | if ($result === \false) { |
| 543 | throw OpensslException::createFromPhpError(); |
| 544 | } |
| 545 | } |
| 546 | /** |
| 547 | * |
| 548 | * |
| 549 | * @param string $infilename |
| 550 | * @param array|null $certs |
| 551 | * @throws OpensslException |
| 552 | * |
| 553 | */ |
| 554 | function openssl_pkcs7_read(string $infilename, ?array &$certs): void |
| 555 | { |
| 556 | error_clear_last(); |
| 557 | $result = \openssl_pkcs7_read($infilename, $certs); |
| 558 | if ($result === \false) { |
| 559 | throw OpensslException::createFromPhpError(); |
| 560 | } |
| 561 | } |
| 562 | /** |
| 563 | * openssl_pkcs7_sign takes the contents of the file |
| 564 | * named infilename and signs them using the |
| 565 | * certificate and its matching private key specified by |
| 566 | * signcert and privkey |
| 567 | * parameters. |
| 568 | * |
| 569 | * @param string $infilename The input file you are intending to digitally sign. |
| 570 | * @param string $outfilename The file which the digital signature will be written to. |
| 571 | * @param string|resource $signcert The X.509 certificate used to digitally sign infilename. |
| 572 | * See Key/Certificate parameters for a list of valid values. |
| 573 | * @param string|resource|array $privkey privkey is the private key corresponding to signcert. |
| 574 | * See Public/Private Key parameters for a list of valid values. |
| 575 | * @param array $headers headers is an array of headers that |
| 576 | * will be prepended to the data after it has been signed (see |
| 577 | * openssl_pkcs7_encrypt for more information about |
| 578 | * the format of this parameter). |
| 579 | * @param int $flags flags can be used to alter the output - see PKCS7 constants. |
| 580 | * @param string $extracerts extracerts specifies the name of a file containing |
| 581 | * a bunch of extra certificates to include in the signature which can for |
| 582 | * example be used to help the recipient to verify the certificate that you used. |
| 583 | * @throws OpensslException |
| 584 | * |
| 585 | */ |
| 586 | function openssl_pkcs7_sign(string $infilename, string $outfilename, $signcert, $privkey, array $headers, int $flags = \PKCS7_DETACHED, string $extracerts = null): void |
| 587 | { |
| 588 | error_clear_last(); |
| 589 | if ($extracerts !== null) { |
| 590 | $result = \openssl_pkcs7_sign($infilename, $outfilename, $signcert, $privkey, $headers, $flags, $extracerts); |
| 591 | } else { |
| 592 | $result = \openssl_pkcs7_sign($infilename, $outfilename, $signcert, $privkey, $headers, $flags); |
| 593 | } |
| 594 | if ($result === \false) { |
| 595 | throw OpensslException::createFromPhpError(); |
| 596 | } |
| 597 | } |
| 598 | /** |
| 599 | * openssl_pkey_export_to_file saves an ascii-armoured |
| 600 | * (PEM encoded) rendition of key into the file named |
| 601 | * by outfilename. |
| 602 | * |
| 603 | * @param resource|string|array $key |
| 604 | * @param string $outfilename Path to the output file. |
| 605 | * @param string $passphrase The key can be optionally protected by a |
| 606 | * passphrase. |
| 607 | * @param array $configargs configargs can be used to fine-tune the export |
| 608 | * process by specifying and/or overriding options for the openssl |
| 609 | * configuration file. See openssl_csr_new for more |
| 610 | * information about configargs. |
| 611 | * @throws OpensslException |
| 612 | * |
| 613 | */ |
| 614 | function openssl_pkey_export_to_file($key, string $outfilename, string $passphrase = null, array $configargs = null): void |
| 615 | { |
| 616 | error_clear_last(); |
| 617 | if ($configargs !== null) { |
| 618 | $result = \openssl_pkey_export_to_file($key, $outfilename, $passphrase, $configargs); |
| 619 | } elseif ($passphrase !== null) { |
| 620 | $result = \openssl_pkey_export_to_file($key, $outfilename, $passphrase); |
| 621 | } else { |
| 622 | $result = \openssl_pkey_export_to_file($key, $outfilename); |
| 623 | } |
| 624 | if ($result === \false) { |
| 625 | throw OpensslException::createFromPhpError(); |
| 626 | } |
| 627 | } |
| 628 | /** |
| 629 | * openssl_pkey_export exports |
| 630 | * key as a PEM encoded string and stores it into |
| 631 | * out (which is passed by reference). |
| 632 | * |
| 633 | * @param resource $key |
| 634 | * @param string|null $out |
| 635 | * @param string $passphrase The key is optionally protected by passphrase. |
| 636 | * @param array $configargs configargs can be used to fine-tune the export |
| 637 | * process by specifying and/or overriding options for the openssl |
| 638 | * configuration file. See openssl_csr_new for more |
| 639 | * information about configargs. |
| 640 | * @throws OpensslException |
| 641 | * |
| 642 | */ |
| 643 | function openssl_pkey_export($key, ?string &$out, string $passphrase = null, array $configargs = null): void |
| 644 | { |
| 645 | error_clear_last(); |
| 646 | if ($configargs !== null) { |
| 647 | $result = \openssl_pkey_export($key, $out, $passphrase, $configargs); |
| 648 | } elseif ($passphrase !== null) { |
| 649 | $result = \openssl_pkey_export($key, $out, $passphrase); |
| 650 | } else { |
| 651 | $result = \openssl_pkey_export($key, $out); |
| 652 | } |
| 653 | if ($result === \false) { |
| 654 | throw OpensslException::createFromPhpError(); |
| 655 | } |
| 656 | } |
| 657 | /** |
| 658 | * openssl_pkey_get_private parses |
| 659 | * key and prepares it for use by other functions. |
| 660 | * |
| 661 | * @param string $key key can be one of the following: |
| 662 | * |
| 663 | * a string having the format |
| 664 | * file://path/to/file.pem. The named file must |
| 665 | * contain a PEM encoded certificate/private key (it may contain both). |
| 666 | * |
| 667 | * |
| 668 | * A PEM formatted private key. |
| 669 | * |
| 670 | * @param string $passphrase The optional parameter passphrase must be used |
| 671 | * if the specified key is encrypted (protected by a passphrase). |
| 672 | * @return resource Returns a positive key resource identifier on success. |
| 673 | * @throws OpensslException |
| 674 | * |
| 675 | */ |
| 676 | function openssl_pkey_get_private(string $key, string $passphrase = "") |
| 677 | { |
| 678 | error_clear_last(); |
| 679 | $result = \openssl_pkey_get_private($key, $passphrase); |
| 680 | if ($result === \false) { |
| 681 | throw OpensslException::createFromPhpError(); |
| 682 | } |
| 683 | return $result; |
| 684 | } |
| 685 | /** |
| 686 | * openssl_pkey_get_public extracts the public key from |
| 687 | * certificate and prepares it for use by other |
| 688 | * functions. |
| 689 | * |
| 690 | * @param resource|string $certificate certificate can be one of the following: |
| 691 | * |
| 692 | * an X.509 certificate resource |
| 693 | * a string having the format |
| 694 | * file://path/to/file.pem. The named file must |
| 695 | * contain a PEM encoded certificate/public key (it may contain both). |
| 696 | * |
| 697 | * |
| 698 | * A PEM formatted public key. |
| 699 | * |
| 700 | * @return resource Returns a positive key resource identifier on success. |
| 701 | * @throws OpensslException |
| 702 | * |
| 703 | */ |
| 704 | function openssl_pkey_get_public($certificate) |
| 705 | { |
| 706 | error_clear_last(); |
| 707 | $result = \openssl_pkey_get_public($certificate); |
| 708 | if ($result === \false) { |
| 709 | throw OpensslException::createFromPhpError(); |
| 710 | } |
| 711 | return $result; |
| 712 | } |
| 713 | /** |
| 714 | * openssl_pkey_new generates a new private and public |
| 715 | * key pair. The public component of the key can be obtained using |
| 716 | * openssl_pkey_get_public. |
| 717 | * |
| 718 | * @param array $configargs You can finetune the key generation (such as specifying the number of |
| 719 | * bits) using configargs. See |
| 720 | * openssl_csr_new for more information about |
| 721 | * configargs. |
| 722 | * @return resource Returns a resource identifier for the pkey on success. |
| 723 | * @throws OpensslException |
| 724 | * |
| 725 | */ |
| 726 | function openssl_pkey_new(array $configargs = null) |
| 727 | { |
| 728 | error_clear_last(); |
| 729 | if ($configargs !== null) { |
| 730 | $result = \openssl_pkey_new($configargs); |
| 731 | } else { |
| 732 | $result = \openssl_pkey_new(); |
| 733 | } |
| 734 | if ($result === \false) { |
| 735 | throw OpensslException::createFromPhpError(); |
| 736 | } |
| 737 | return $result; |
| 738 | } |
| 739 | /** |
| 740 | * openssl_private_decrypt decrypts |
| 741 | * data that was previously encrypted via |
| 742 | * openssl_public_encrypt and stores the result into |
| 743 | * decrypted. |
| 744 | * |
| 745 | * You can use this function e.g. to decrypt data which is supposed to only be available to you. |
| 746 | * |
| 747 | * @param string $data |
| 748 | * @param string|null $decrypted |
| 749 | * @param string|resource|array $key key must be the private key corresponding that |
| 750 | * was used to encrypt the data. |
| 751 | * @param int $padding padding can be one of |
| 752 | * OPENSSL_PKCS1_PADDING, |
| 753 | * OPENSSL_SSLV23_PADDING, |
| 754 | * OPENSSL_PKCS1_OAEP_PADDING, |
| 755 | * OPENSSL_NO_PADDING. |
| 756 | * @throws OpensslException |
| 757 | * |
| 758 | */ |
| 759 | function openssl_private_decrypt(string $data, ?string &$decrypted, $key, int $padding = \OPENSSL_PKCS1_PADDING): void |
| 760 | { |
| 761 | error_clear_last(); |
| 762 | $result = \openssl_private_decrypt($data, $decrypted, $key, $padding); |
| 763 | if ($result === \false) { |
| 764 | throw OpensslException::createFromPhpError(); |
| 765 | } |
| 766 | } |
| 767 | /** |
| 768 | * openssl_private_encrypt encrypts data |
| 769 | * with private key and stores the result into |
| 770 | * crypted. Encrypted data can be decrypted via |
| 771 | * openssl_public_decrypt. |
| 772 | * |
| 773 | * This function can be used e.g. to sign data (or its hash) to prove that it |
| 774 | * is not written by someone else. |
| 775 | * |
| 776 | * @param string $data |
| 777 | * @param string|null $crypted |
| 778 | * @param string|resource|array $key |
| 779 | * @param int $padding padding can be one of |
| 780 | * OPENSSL_PKCS1_PADDING, |
| 781 | * OPENSSL_NO_PADDING. |
| 782 | * @throws OpensslException |
| 783 | * |
| 784 | */ |
| 785 | function openssl_private_encrypt(string $data, ?string &$crypted, $key, int $padding = \OPENSSL_PKCS1_PADDING): void |
| 786 | { |
| 787 | error_clear_last(); |
| 788 | $result = \openssl_private_encrypt($data, $crypted, $key, $padding); |
| 789 | if ($result === \false) { |
| 790 | throw OpensslException::createFromPhpError(); |
| 791 | } |
| 792 | } |
| 793 | /** |
| 794 | * openssl_public_decrypt decrypts |
| 795 | * data that was previous encrypted via |
| 796 | * openssl_private_encrypt and stores the result into |
| 797 | * decrypted. |
| 798 | * |
| 799 | * You can use this function e.g. to check if the message was written by the |
| 800 | * owner of the private key. |
| 801 | * |
| 802 | * @param string $data |
| 803 | * @param string|null $decrypted |
| 804 | * @param string|resource $key key must be the public key corresponding that |
| 805 | * was used to encrypt the data. |
| 806 | * @param int $padding padding can be one of |
| 807 | * OPENSSL_PKCS1_PADDING, |
| 808 | * OPENSSL_NO_PADDING. |
| 809 | * @throws OpensslException |
| 810 | * |
| 811 | */ |
| 812 | function openssl_public_decrypt(string $data, ?string &$decrypted, $key, int $padding = \OPENSSL_PKCS1_PADDING): void |
| 813 | { |
| 814 | error_clear_last(); |
| 815 | $result = \openssl_public_decrypt($data, $decrypted, $key, $padding); |
| 816 | if ($result === \false) { |
| 817 | throw OpensslException::createFromPhpError(); |
| 818 | } |
| 819 | } |
| 820 | /** |
| 821 | * openssl_public_encrypt encrypts data |
| 822 | * with public key and stores the result into |
| 823 | * crypted. Encrypted data can be decrypted via |
| 824 | * openssl_private_decrypt. |
| 825 | * |
| 826 | * This function can be used e.g. to encrypt message which can be then read |
| 827 | * only by owner of the private key. It can be also used to store secure data |
| 828 | * in database. |
| 829 | * |
| 830 | * @param string $data |
| 831 | * @param string|null $crypted This will hold the result of the encryption. |
| 832 | * @param string|resource $key The public key. |
| 833 | * @param int $padding padding can be one of |
| 834 | * OPENSSL_PKCS1_PADDING, |
| 835 | * OPENSSL_SSLV23_PADDING, |
| 836 | * OPENSSL_PKCS1_OAEP_PADDING, |
| 837 | * OPENSSL_NO_PADDING. |
| 838 | * @throws OpensslException |
| 839 | * |
| 840 | */ |
| 841 | function openssl_public_encrypt(string $data, ?string &$crypted, $key, int $padding = \OPENSSL_PKCS1_PADDING): void |
| 842 | { |
| 843 | error_clear_last(); |
| 844 | $result = \openssl_public_encrypt($data, $crypted, $key, $padding); |
| 845 | if ($result === \false) { |
| 846 | throw OpensslException::createFromPhpError(); |
| 847 | } |
| 848 | } |
| 849 | /** |
| 850 | * Generates a string of pseudo-random bytes, with the number of bytes |
| 851 | * determined by the length parameter. |
| 852 | * |
| 853 | * It also indicates if a cryptographically strong algorithm was used to produce the |
| 854 | * pseudo-random bytes, and does this via the optional crypto_strong |
| 855 | * parameter. It's rare for this to be FALSE, but some systems may be broken or old. |
| 856 | * |
| 857 | * @param int $length The length of the desired string of bytes. Must be a positive integer. PHP will |
| 858 | * try to cast this parameter to a non-null integer to use it. |
| 859 | * @param bool|null $crypto_strong If passed into the function, this will hold a boolean value that determines |
| 860 | * if the algorithm used was "cryptographically strong", e.g., safe for usage with GPG, |
| 861 | * passwords, etc. TRUE if it did, otherwise FALSE |
| 862 | * @return string Returns the generated string of bytes on success. |
| 863 | * @throws OpensslException |
| 864 | * |
| 865 | */ |
| 866 | function openssl_random_pseudo_bytes(int $length, ?bool &$crypto_strong = null): string |
| 867 | { |
| 868 | error_clear_last(); |
| 869 | $result = \openssl_random_pseudo_bytes($length, $crypto_strong); |
| 870 | if ($result === \false) { |
| 871 | throw OpensslException::createFromPhpError(); |
| 872 | } |
| 873 | return $result; |
| 874 | } |
| 875 | /** |
| 876 | * openssl_seal seals (encrypts) |
| 877 | * data by using the given method with a randomly generated |
| 878 | * secret key. The key is encrypted with each of the public keys |
| 879 | * associated with the identifiers in pub_key_ids |
| 880 | * and each encrypted key is returned |
| 881 | * in env_keys. This means that one can send |
| 882 | * sealed data to multiple recipients (provided one has obtained their |
| 883 | * public keys). Each recipient must receive both the sealed data and |
| 884 | * the envelope key that was encrypted with the recipient's public key. |
| 885 | * |
| 886 | * @param string $data The data to seal. |
| 887 | * @param string|null $sealed_data The sealed data. |
| 888 | * @param array $env_keys Array of encrypted keys. |
| 889 | * @param array $pub_key_ids Array of public key resource identifiers. |
| 890 | * @param string $method The cipher method. |
| 891 | * @param string $iv The initialization vector. |
| 892 | * @return int Returns the length of the sealed data on success. |
| 893 | * If successful the sealed data is returned in |
| 894 | * sealed_data, and the envelope keys in |
| 895 | * env_keys. |
| 896 | * @throws OpensslException |
| 897 | * |
| 898 | */ |
| 899 | function openssl_seal(string $data, ?string &$sealed_data, array &$env_keys, array $pub_key_ids, string $method = "RC4", string &$iv = null): int |
| 900 | { |
| 901 | error_clear_last(); |
| 902 | $result = \openssl_seal($data, $sealed_data, $env_keys, $pub_key_ids, $method, $iv); |
| 903 | if ($result === \false) { |
| 904 | throw OpensslException::createFromPhpError(); |
| 905 | } |
| 906 | return $result; |
| 907 | } |
| 908 | /** |
| 909 | * openssl_sign computes a signature for the |
| 910 | * specified data by generating a cryptographic |
| 911 | * digital signature using the private key associated with |
| 912 | * priv_key_id. Note that the data itself is |
| 913 | * not encrypted. |
| 914 | * |
| 915 | * @param string $data The string of data you wish to sign |
| 916 | * @param string|null $signature If the call was successful the signature is returned in |
| 917 | * signature. |
| 918 | * @param resource|string $priv_key_id resource - a key, returned by openssl_get_privatekey |
| 919 | * |
| 920 | * string - a PEM formatted key |
| 921 | * @param int|string $signature_alg int - one of these Signature Algorithms. |
| 922 | * |
| 923 | * string - a valid string returned by openssl_get_md_methods example, "sha256WithRSAEncryption" or "sha384". |
| 924 | * @throws OpensslException |
| 925 | * |
| 926 | */ |
| 927 | function openssl_sign(string $data, ?string &$signature, $priv_key_id, $signature_alg = \OPENSSL_ALGO_SHA1): void |
| 928 | { |
| 929 | error_clear_last(); |
| 930 | $result = \openssl_sign($data, $signature, $priv_key_id, $signature_alg); |
| 931 | if ($result === \false) { |
| 932 | throw OpensslException::createFromPhpError(); |
| 933 | } |
| 934 | } |
| 935 | /** |
| 936 | * openssl_x509_export_to_file stores |
| 937 | * x509 into a file named by |
| 938 | * outfilename in a PEM encoded format. |
| 939 | * |
| 940 | * @param string|resource $x509 See Key/Certificate parameters for a list of valid values. |
| 941 | * @param string $outfilename Path to the output file. |
| 942 | * @param bool $notext |
| 943 | * The optional parameter notext affects |
| 944 | * the verbosity of the output; if it is FALSE, then additional human-readable |
| 945 | * information is included in the output. The default value of |
| 946 | * notext is TRUE. |
| 947 | * @throws OpensslException |
| 948 | * |
| 949 | */ |
| 950 | function openssl_x509_export_to_file($x509, string $outfilename, bool $notext = \true): void |
| 951 | { |
| 952 | error_clear_last(); |
| 953 | $result = \openssl_x509_export_to_file($x509, $outfilename, $notext); |
| 954 | if ($result === \false) { |
| 955 | throw OpensslException::createFromPhpError(); |
| 956 | } |
| 957 | } |
| 958 | /** |
| 959 | * openssl_x509_export stores |
| 960 | * x509 into a string named by |
| 961 | * output in a PEM encoded format. |
| 962 | * |
| 963 | * @param string|resource $x509 See Key/Certificate parameters for a list of valid values. |
| 964 | * @param string|null $output On success, this will hold the PEM. |
| 965 | * @param bool $notext |
| 966 | * The optional parameter notext affects |
| 967 | * the verbosity of the output; if it is FALSE, then additional human-readable |
| 968 | * information is included in the output. The default value of |
| 969 | * notext is TRUE. |
| 970 | * @throws OpensslException |
| 971 | * |
| 972 | */ |
| 973 | function openssl_x509_export($x509, ?string &$output, bool $notext = \true): void |
| 974 | { |
| 975 | error_clear_last(); |
| 976 | $result = \openssl_x509_export($x509, $output, $notext); |
| 977 | if ($result === \false) { |
| 978 | throw OpensslException::createFromPhpError(); |
| 979 | } |
| 980 | } |
| 981 | /** |
| 982 | * openssl_x509_fingerprint returns the digest of |
| 983 | * x509 as a string. |
| 984 | * |
| 985 | * @param string|resource $x509 See Key/Certificate parameters for a list of valid values. |
| 986 | * @param string $hash_algorithm The digest method or hash algorithm to use, e.g. "sha256", one of openssl_get_md_methods. |
| 987 | * @param bool $raw_output When set to TRUE, outputs raw binary data. FALSE outputs lowercase hexits. |
| 988 | * @return string Returns a string containing the calculated certificate fingerprint as lowercase hexits unless raw_output is set to TRUE in which case the raw binary representation of the message digest is returned. |
| 989 | * |
| 990 | * Returns FALSE on failure. |
| 991 | * @throws OpensslException |
| 992 | * |
| 993 | */ |
| 994 | function openssl_x509_fingerprint($x509, string $hash_algorithm = "sha1", bool $raw_output = \false): string |
| 995 | { |
| 996 | error_clear_last(); |
| 997 | $result = \openssl_x509_fingerprint($x509, $hash_algorithm, $raw_output); |
| 998 | if ($result === \false) { |
| 999 | throw OpensslException::createFromPhpError(); |
| 1000 | } |
| 1001 | return $result; |
| 1002 | } |
| 1003 | /** |
| 1004 | * openssl_x509_read parses the certificate supplied by |
| 1005 | * x509certdata and returns a resource identifier for |
| 1006 | * it. |
| 1007 | * |
| 1008 | * @param string|resource $x509certdata X509 certificate. See Key/Certificate parameters for a list of valid values. |
| 1009 | * @return resource Returns a resource identifier on success. |
| 1010 | * @throws OpensslException |
| 1011 | * |
| 1012 | */ |
| 1013 | function openssl_x509_read($x509certdata) |
| 1014 | { |
| 1015 | error_clear_last(); |
| 1016 | $result = \openssl_x509_read($x509certdata); |
| 1017 | if ($result === \false) { |
| 1018 | throw OpensslException::createFromPhpError(); |
| 1019 | } |
| 1020 | return $result; |
| 1021 | } |
| 1022 |