Issues with OpenSSL on PHP - different behaviour for different versions - php

(This question was originally posted on ServerFault - I have deleted it there and moved it here.)
I have a development machine running PHP 5.3.5 and a production machine running PHP 5.3.8.
The following code runs on the development machine:
<?php
$key = "-----BEGIN PUBLIC KEY-----
MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQC0x+2RiQ+LCZNAUcl/Ecf1NrTr
lhjOiHaVC+w/y+UJevqVcDstD22OJGwT13B9T47OuQG9BmzcZQYLcShUMhVD/Owu
9+8PcK51EnBd0lym6+z/WixpnqfQonyKiqq5ytmYKUlUv39J8QQUI2geyvY9VpWS
wyNcFUs7wPl2zsLCPQIDAQAB
-----END PUBLIC KEY-----";
$data = "Hello, world!";
$key1 = openssl_get_publickey($key);
print_r ($key1);
echo "<p>";
$res = openssl_public_encrypt($data, $encrypted_data, $key1, OPENSSL_PKCS1_PADDING);
echo base64_encode($encrypted_data);
On my development machine, this code outputs a resource and an encoded string. I would copy it here, but of course it changes each time. On the production machine, this code produces the resource number and the following PHP errors:
PHP Warning: openssl_public_encrypt(): Don't know how to get public key from this private key in C:\xxx\test.php on line 15
PHP Warning: openssl_public_encrypt(): key parameter is not a valid public key in C:\xxx\test.php on line 15
Unfortunately, installing an older version of PHP on the production machine is not an option at the moment because of other applications that are running on it which require 5.3.8 as a minimum.
Would it help if I upgraded to 5.4.x?
I do know that the version of OpenSSL on 5.3.5 is 0.9.8 whereas the version in 5.3.8 is 1.0.0. I imagine that there might be a problem there. Is there any way to work around that?
I have tried to find out as much as I can from the OpenSSL.org site, and the PHP bug tracker, but I don't know what I'm looking for.
Regards,
Philip

According to this post, the issue is related to different OpenSSL versions of Apache and PHP in the XAMPP/Windows installation. I had a similar issue with openssl_verify. I solved it by using the same OpenSSL version for both Apache and PHP (replacing DLLs). Here's a link to the solution.
"I found a solution for the problem, it seems there are 2 wrong files in /apache/bin/ in the default 1.7.7 installation that need to be replaced by the files existing in /php/ (libeay32.ddl and ssleay32.dll)"

You passed the wrong parameter of openssl_public_encrypt
openssl_public_encrypt($data, $encrypted_data, $key1, OPENSSL_PKCS1_PADDING)
where $key1 is the resource id use $key instead of $key1. You can use
openssl_public_encrypt($data, $encrypted_data, $key, OPENSSL_PKCS1_PADDING)

Related

PHP openssl_pkcs12_read "error:0308010C:digital envelope routines::unsupported"

I'm getting this error after upgrading from php5.6 to php8.2 on windows. Looks like the new openssl dll does not support splitting of my apple push notification certificate pk12 file into separate cert and key array. Any ideas how to get it to do that ? There is another similar question on stackoverflow but it only discusses ubuntu, not windows. I tried to ask a question to the contributors of the ubuntu related article but i had insufficient permissions.
$p12_filename = "..\\path\\to\\pass.com.testpass.p12";
$p12data = file_get_contents($p12_filename);
$p12Password = 'MyPassword';
$rp12 = array();
// following function works in php5.6 but fails in php8.2
$rc = openssl_pkcs12_read($p12data, $rp12, $p12Password);
$cert_data = $rp12['cert'];
$cert_key = $rp12['pkey'];
So according to the link above supplied by Jacob Mulquin
, the openssl component supplied in PHP8.2 has a legacy function removed which prevents it from parsing certain types of certificate that were previously supported. As i don't need the latest language enhancements of PHP8.2, the quickest solution for me ( on windows ) was to replace PHP8.2 with the next most recent release, PHP8.1.13.

Gnupg gives "get_key failed" despite using a valid key

I want to integrate PGP encryption into my web application, after looking for what to use(extensions, libraries, etc.) I decided to go with the gnupg extension for php. Now, I do have a PGP key in one of my desktop folders and I've tried to use it's fingerprint as a string for addencryptkey, the error I receive is get_key failed which I don't understand why, my PGP key is valid.
There are two very similar questions on SO:
php gnupg get_key failed error ,
gnupg get_key failed in php ,
Based on these, I've updated my code somewhat to no success, here's what it currently looks like:
putenv("GNUPGHOME=/home/user/Desktop/Keys/.gnupg/");
$pgp = new gnupg();
$pgp->addencryptkey("F0E2DF9C82ECE67935171F4939D8599A923820D9");
echo $pgp->geterror();
In the folder specified in putenv, I have my public key saved in a .asc file. I can't see what the problem really is, unless it only works with keys stored on the server?
I just wanted to share my fix for this issue. Given that this is one of the more recent questions on this topic I thought it best to share it here.
At the time I was able to encrypt messages fine (PHP 7.4 with the GNUPG PECL extension).
To address the get_key_failed error, after setting up/importing my keys I copied my entire .gnupg directory to the root of my webserver (/var/www/html in my case) and updated its permissions so that it was accessible by the webserver.
putenv("GNUPGHOME=/var/www/html/.gnupg");
I assumed that this would fix it, however I then encountered a new error when attemping to decrypt a message:
Uncaught Exception: decrypt failed
The only way I could resolve this was by ensuring my key pair did not have a passphrase. Some comments on the PHP GNUPG docs suggest that that passphrase which is the second argument on adddecryptkey() is ignored regardless. However, in my case decryption only worked with a private key that didn't have a passphrase set.
This worked on my local instance (Ubuntu 18) and when deployed to an EC2 instance running Amazon Linux 2.

My wiki was working fine until now, suddenly this error is occouring, What can be done?

This is the error
Warning: hash(): Unknown hashing algorithm: fnv132 in
C:\wamp64\www\wiki\includes\resourceloader\ResourceLoader.php on line
624
Version info of my wiki
MediaWiki 1.32.2
PHP 7.2.18 (apache2handler)
MySQL 5.7.26
ICU 63.1
This is the part of code from resourceloader.php
623: public static function makeHash( $value ) {
624: $hash = hash( 'fnv132', $value );
625: return Wikimedia\base_convert( $hash, 16, 36, 7 );
626: }
My pages are loading fine in the background but this error banner is appearing over them, blocking half the page.
Probably all you have to do is reboot the server, and restart all the services in wamp. This should fix the problem.
If not, add this line to localSettings.php file in the root directory, then run the website via the browser. It should tell you if this hashing algorithm is enabled or not.
print_r(hash_algos());

Symfony 4 : Default value for parameters with a class type can only be

I am currently working on an application in symfony 4. I encounter a problem for deploy in prod. when I get to the navigator I get this error :
Fatal error: Default value for parameters with a class type can only be NULL in /var/www/html/project/vendor/symfony/config/ResourceCheckerConfigCache.php on line 40.
public function __construct(string $file, iterable $resourceCheckers = array())
{
$this->file = $file;
$this->resourceCheckers = $resourceCheckers;
}
I specify that the site works perfectly with the dev server (php bin /console server:start)
The problem must come from the environment. in the .env file, when I set APP_ENV=dev , in the browser there is only a blank page with a status 500 (no message in the logs)
I can not find any documentation on the internet and I am starting to lack ideas. If someone has a solution I am interested.
Thank you.
Solution by OP.
The problem was that the PHP version was PHP 7.0 and not 7.1. Though in my shell the php -v command was fine with 7.1, I noticed the wrong version used by phpinfo ();

PHP7.1 and Laravel 5.3: Function mcrypt_get_iv_size() is deprecated

I'm using Laravel 5.0 for all my projects. Yesterday, I've updated my PHP version from 7.0.x to version 7.1.0. Once updated, I tried opening my Laravel project and saw this message below:
ErrorException in Encrypter.php line 303:
Function mcrypt_get_iv_size() is deprecated
in Encrypter.php line 303
at HandleExceptions->handleError('8192', 'Function mcrypt_get_iv_size() is deprecated', 'C:\wamp64\www\project1\vendor\laravel\framework\src\Illuminate\Encryption\Encrypter.php', '303', array()) in Encrypter.php line 303
May I know how can I solve this ? Does using Laravel 5.3 solve the problem? I don't feel like updating my Laravel to 5.3 because it's a huge project and it will takes a long time to update. There are too much differences between these two versions. Lots of codes need to be modified.
Is there an easier way to solve this issue?
Add this to the beginning of the config/app.php:
error_reporting(E_ALL ^ E_DEPRECATED);
Source: https://stackoverflow.com/a/42515505/225790
This error occurs because you probably have something other than AES-256-CBC as your cipher in your config/app.php file that depends on the mcrypt extension. Perhaps you are using MCRYPT_RIJNDAEL_256 or MCRYPT_RIJNDAEL_128?
The best thing you can do without a full-blown Laravel upgrade is install the legacy encrypter and use it to update all your encrypted data to use the AES-256-CBC cipher which has been the default cipher since Laravel 5.1, I believe. Once you do this, you should be able to use PHP 7.1 for your Laravel application.
In your config/app.php configuration file, you should update the cipher to "AES-256-CBC" and set your key to a random 32 byte string which may be securely generated using php artisan key:generate
to solve this just change cipher in app.php from 'MCRYPT_RIJNDAEL_128' to 'AES-256-CBC'

Categories