PluginProbe
Defender Security – Malware Scanner, Login Security & Firewall / 6.1.0
Defender Security – Malware Scanner, Login Security & Firewall v6.1.0
6.3.0 6.2.3 6.2.4 6.2.0 6.2.1 6.2.2 6.1.0 5.3.1 5.4.0 5.4.1 5.5.0 5.5.1 5.6.0 5.6.1 5.6.2 5.7.0 5.7.1 5.7.2 5.8.0 5.8.1 5.9.0 6.0.0 6.0.1 3.0.1 3.1.0 All 141 releases
defender-security / vendor / thecodingmachine / safe / generated / openssl.php

openssl.php in Defender Security – Malware Scanner, Login Security & Firewall 6.1.0, at vendor/thecodingmachine/safe/generated/openssl.php

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