|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2010-07-16 10:06 UTC] p dot vanbrouwershaven at networking4all dot com
Description: ------------ The PHP functions openssl_pkcs7_(sign|encrypt|decrypt|verify) do require files to be executed. In many cases this will create the unintended requirement of temporary files. In compare with openssl_(sign|encrypt|decrypt|verify|...) which are doing almost the same thing this is a strange behavior. When we look at the purpose of openssl_pkcs7_* (working with digital signatures in mail), you would not expect to work with files instead of strings for this few data. Patchesopenssl_pkcs7_mem_functions (last revision 2014-01-13 15:05 UTC by php at kriegt dot es)openssl_pkcs7_sign.patch (last revision 2010-07-16 08:10 UTC by jille at quis dot cx) Pull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Wed Oct 29 15:00:02 2025 UTC |
First exmaple, signing mail with the current PHP version, content is located in file unsigned.txt, strangely this file needs to start with an empty line to get the signature recognized. <?php if (openssl_pkcs7_sign("unsigned.txt", "signed.txt", "file://public.cer", array("file://private.key", "password"), array("To" => "me@example.com", // keyed syntax "From: Me <me@example.com>", // indexed syntax "Subject" => "This is my subject"), PKCS7_DETACHED, "intermediate.cer" )) { // message signed - send it! exec(ini_get("sendmail_path") . " < signed.txt"); } ?> A second example that runs with this patch, please not the linefeed "\n", without this linefeed the signature will not be recognized. <?php if (openssl_pkcs7_sign("\nunsigned.txt", "signed.txt", "file://public.cer", array("file://private.key", "password"), array("To" => "me@example.com", // keyed syntax "From: Me <me@example.com>", // indexed syntax "Subject" => "This is my subject"), PKCS7_DETACHED, "intermediate.cer" )) { // message signed - send it! exec(ini_get("sendmail_path") . " < signed.txt"); } ?> Please not this proof of concept does only changes the infilename and not the other files like the outfilename, signcert, privkey & extracerts.Since I see no chance to change the current functions (openssl_pkcs7_encrypt/decrypt) to the schema I would need it, I just added two new functions named: - openssl_pkcs7_mem_encrypt - openssl_pkcs7_mem_decrypt These functions use BIO_s_mem instead of BIO_s_file to create the necessary BIO data handled by the PKCS7 functions. I tested this with the following skript and it worked as it should: <?php $message = "hey there, this is top secret message which gets encrypted by memory soon"; #$infile = tempnam(sys_get_temp_dir(),'smime'); #$tmpfile = fopen( $infile, 'w+' ); #fwrite($tmpfile,$message,strlen($message)); #echo "Raw Message in $infile\n"; #$outfile = tempnam(sys_get_temp_dir(),'smime'); $certfile = "/path/to/just/the/certificate.pem"; $encrypted = ""; if( openssl_pkcs7_mem_encrypt( $message, $encrypted, file_get_contents($certfile), array() ) ) { var_dump( $encrypted ); } $p12key = "/my/path/to/related/p12file.p12" $password = "mysecretpasswordforkey"; openssl_pkcs12_read(file_get_contents($p12key), $certdata, $password); $key = $certdata['pkey']; $cert = $certdata['cert']; if( strlen($key) != 0 ) { # echo "Key okay!\n"; } if( trim($encrypted) != "" ) { if( openssl_pkcs7_mem_decrypt( $encrypted, $decrypted, $cert, array( $key, $password ) ) ) { var_dump( $decrypted ); } } ################# First var_dump returns encrypted data Second var_dump returns content of $message