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

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.

Related

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.

SOAP-ERROR: Parsing WSDL: Couldn't load from (PHP 5.6 Certificate checking)

I have been fighting the following error for longer than I would like to admit. The solutions I was finding were not related to what the actual problem was.
PHP Fatal error: SOAP-ERROR: Parsing WSDL: Couldn't load from 'https://x.x.x.x/iControl/iControlPortal.cgi?WSDL=LocalLB.Class' : failed to load external entity "https://x.x.x.x/iControl/iControlPortal.cgi?WSDL=LocalLB.Class"
My prod environment is CentOS 6 which is packaged with PHP 5.3 which has been EOL since August 2014. From my understanding CentOS backports security fixes, I still wanted to do some testing with a newer version and there was some other features not available in 5.3 that I could possibly leverage.
I built out a new DEV environment with PHP 5.6 and all seemed to be working until I tried a SOAP call and got the message above. After a lot of searching I finally found that in PHP 5.6 SSL Certificate check is enabled by default. In my DEV environment it is completely contained and the application requires SSL. I do not have a valid certificate. I have not tested in my PROD environment which does have valid certificates.
To get around this I added:
'stream_context'=>stream_context_create
(
array
(
'ssl'=>array
(
'verify_peer'=>false,
'verify_peer_name'=>false
)
)
)
Here it is put together in the code. This is kind of messy just threw it together for testing.
<?php
$device_ip = "x.x.x.x";
$user = "username";
$pw = "password";
$location = "https://" . $device_ip . "/iControl/iControlPortal.cgi?";
$wsdl_class = "https://" . $device_ip . "/iControl/iControlPortal.cgi?WSDL=LocalLB.Class";
$soap_class = new SoapClient($wsdl_class, array('location'=>$location, 'login'=>$user, 'password'=>$pw, 'stream_context'=>stream_context_create(array('ssl'=>array('verify_peer'=>false, 'verify_peer_name'=>false)))));
$response = $soap_class->get_string_class(array("SomeName"));
var_dump($response);
?>
although I was able to find a workaround for not having a valid certificate. Is there a way to import the self-signed certificate so that I would not have use the workaround in my DEV environment?

Call to undefined method mysqli_stmt::get_result() AND mysqlnd installed

As the title suggests, I have mysqlnd available on my shared hosting server running PHP version 5.4. When I attempt to call the mysqli get_result() function, I get this error.
I have spoken several times with the hosting provider, and most recently they told me to try running
# /opt/ntphp/php54/bin/php -i | grep -i mysqlnd
I hopped on ssh and ran this command which gave this:
mysqlnd
mysqlnd => enabled
Version => mysqlnd 5.0.10 - 20111026 - $Id: c85105d7c6f7d70d609bb4c000257868a40840ab $
Loaded plugins => mysqlnd,example,debug_trace,auth_plugin_mysql_native_password,auth_plugin_mysql_clear_password
mysqlnd statistics =>
Client API version => mysqlnd 5.0.10 - 20111026 - $Id: c85105d7c6f7d70d609bb4c000257868a40840ab $
So, that appears to me as I would expect.
I found another piece of PHP code on another forum post that suggests running:
$hasMySQL = false; $hasMySQLi = false; $withMySQLnd = false; $sentence = '';
if (function_exists('mysql_connect')) {
$hasMySQL = true;
$sentence.= "(Deprecated) MySQL <b>is installed</b> "; } else
$sentence.= "(Deprecated) MySQL <b>is not</b> installed ";
if (function_exists('mysqli_connect')) {
$hasMySQLi = true;
$sentence.= "and the new (improved) MySQL <b>is installed</b>. "; } else
$sentence.= "and the new (improved) MySQL <b>is not installed</b>. ";
if (function_exists('mysqli_get_client_stats')) {
$withMySQLnd = true;
$sentence.= "This server is using MySQLnd as the driver."; } else
$sentence.= "This server is using libmysqlclient as the driver.";
echo $sentence;
I did this and got the result:
(Deprecated) MySQL is installed and the new (improved) MySQL is installed. This server is using libmysqlclient as the driver.
I'm running my hosting with Arvixe, and they had a blog post that basically said "Run PHP 5.4 and this will work". It's clear to me that they think this function should run, but it's giving me a fatal error instead.
Side note - the code runs perfectly on my local machine, and I only get an error with the call to get_result().
EDITED:
Here is how the PHP is set up:
$stmt = $con->prepare("SELECT * FROM User_Details WHERE LCASE(username) = LCASE(?) LIMIT 1");
$stmt->bind_param("s", $username);
$stmt->execute();
$result = $stmt->get_result(); // This line throws the ugly error
For anybody wondering what's going on with this, and using Arvixe. Here is the response I received from the staff.
This is some confusion around MySQLND which was caused by an old
platform and its something I'll be communicating to my staff.
We used to run a system which allowed us to have individual PHP
installs and at that point 5.4 and 5.5 both had MysqlND running. The
new PHP system we have in place runs all PHP installs on a consistent
config and therefore with our PHP 5.3 install (the base install) not
using MySQLND nor does our 5.4 or 5.5 install.
This will be reviewed in future as PHP 5.6 is going to have MySQLND as
a default.
Currently the only way we can support MySQLND is via a VPS / dedicated
server.
So, if you're on Arvixe and not a VPS, you're out of luck for using a super common and recommended convention for retrieving data from your database. There are workarounds for this using a ton of different methods. They either aren't feasible for my project or I couldn't get them to work, but for smaller queries it seems that using $stmt->bind_result() is one of the more popular methods.
http://php.net/manual/en/mysqli-stmt.bind-result.php
For me, I'm taking my business back to GoDaddy. I spent over 10 hours trying to get a solution for this with no resolution offered except "We have a 60 day money back guarantee if you're not satisfied."
Thanks everyone for helping out with this. Frustrating as it's been, I learned a lot in the process and from these boards... As is usually the case.
When you ran the code that inspected the PHP environment was that through the webserver? I ask this because it sounds like you haven't configured your webserver to use PHP 5.4 (which allegedly has the version of mysqli you're after).
You might want to refer to this article from Arvixe.
Interestingly, it seems the function that's causing trouble is available since PHP 5.3.
You might also try a simple script to view the environment from the web
<?php
phpinfo();
Hit that from the web and check for a couple things
PHP version 5.4
mysqlnd
Personally, I'm not sure mysqlnd is required to provide the get_results function (but would need to dig deeper to determine that).
Another test you can do to see if in fact Arvixe's PHP 5.4 environment provides the function of interest is test the CLI environment which you know has mysqlnd
/opt/ntphp/php54/bin/php -r 'echo method_exists("mysqli_stmt", "get_result") . PHP_EOL;'
If that spits out a 1, you almost certainly just need to follow that guide I linked to earlier to get your webserver environment running PHP 5.4.
Remove the existing php version and install the new version of php 5.5 or later
and verify by doing on root terminal $ php -v

PC/SC with PHP5 in windows with wampServer

I'm trying to read public data from smartCard with wampServer, PHP 5.4, winSCard and php_pcsc.dll for PHP5 (http://pecl.php.net/package/pcsc/0.2/windows).
I found this project http://hanez.org/pcsc-for-php.html (code is for php4 but my dll is for php5) and tried to do in localhost but php throws me this error "Call to undefined function SCardListReaders()".
When I see de phpinfo I can see the extension enabled but it hasn't any information (I can't put images because I haven't points), only has table headers
PC/SC
| PC/SC support | enabled |
Thank you
You may be getting an error because the function is actually scard_list_readers() not SCardListReaders() as the error shows in your post.
But before you can use most of the functions, you need to establish a context first.
Try:
$context = scard_establish_context();
$readers = scard_list_readers($context);
print_r($readers);
// then when you have your card reader's identity from $readers
$connection = scard_connect($context, 'YOUR CARD READER HERE');
$response = scard_transmit($connection, 'pdu string');

Issues with OpenSSL on PHP - different behaviour for different versions

(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)

Categories