starekrow / lockbox

Encrypted storage with built-in key management facilities
MIT License
95 stars 6 forks source link

Fatal error: Uncaught Exception: Unknown algorithm #32

Closed cavo789 closed 6 years ago

cavo789 commented 6 years ago

Hi

I'm trying to use lockbox but it fails on my computer (localhost, Windows 10). I'm using the code below (a copy/paste from your readme.md) and I receive a fatal error when calling the Lock() method.

I'm under PHP 7.2.0 (it's working fine under PHP 5.6.25 or 7.0.10). I've just install PHP 7.2.1, same result : NOK.

Can you give advices please ? Any installation problem on my computer ?

Thanks.

define ('DS', DIRECTORY_SEPARATOR);

use starekrow\Lockbox\CryptoKey;
use starekrow\Lockbox\Secret;
use starekrow\Lockbox\Vault;

$lib = 'some_dir';

// Include Lockbox
require_once $lib."CryptoCore.php";
require_once $lib."CryptoCoreLoader.php";
require_once $lib."CryptoCoreFailed.php";
require_once $lib."CryptoCoreBuiltin.php";
require_once $lib."CryptoCoreOpenssl.php";
require_once $lib."Crypto.php";
require_once $lib."CryptoKey.php";
require_once $lib."Secret.php";
require_once $lib."Vault.php";

// CryptoKey defaults to AES-128-CBC encryption with a random key
$key = new CryptoKey();
$message = "You can't see me.";
echo $key->Lock( $message ).'<hr/>';

$key = new CryptoKey( "ILikeCheese", null, "AES-256-ECB" );
$no_see_um = $key->Lock( "This text is safe." );
echo $no_see_um.'<hr/>';
$see_um = $key->Unlock( $no_see_um );
echo $see_um.'<hr/>';

Full error message :

Fatal error: Uncaught Exception: Unknown algorithm in 
libs\lockbox\CryptoCoreBuiltin.php:86 Stack trace: 
#0 libs\lockbox\Crypto.php(84): starekrow\Lockbox\CryptoCoreBuiltin->ivlen('AES-128-CBC') 
#1 libs\lockbox\CryptoKey.php(129): starekrow\Lockbox\Crypto::ivlen('AES-128-CBC') 
#2 test.php(28): starekrow\Lockbox\CryptoKey->lock('You can't see m...') 
#3 {main} thrown in libs\lockbox\CryptoCoreBuiltin.php on line 86
KJLJon commented 6 years ago

Can you check your php.ini and verify you have openssl extension enabled.

See: https://github.com/php/php-src/blob/PHP-7.2.1/php.ini-production#L907

cavo789 commented 6 years ago

Thanks for your answer. Indeed, i was also searching in that direction.

OpenSSL seems to be not loaded ... but I've well uncomment extension=php_openssl.dll in my PHP.ini, for PHP 7.2.x .

I've the feeling that somethings goes wrong with that version : it's well OK for PHP 7.1 but I can't get it works with 7.2.

extension=php_openssl.dll is well uncommented...

If I find a solution, I'll post here back.

Thanks.

KJLJon commented 6 years ago

It looks like it is using CryptoCoreBuiltin (which is done when function_exists("openssl_encrypt") == false).

Can you test something like the following:

<?php
var_dump([
    //checks if openssl_encrypt function exists (like the code does)
    'function openssl_encrypt exists' => function_exists("openssl_encrypt"),
    //verify the php.ini file you updated is the one being loaded
    'php.ini file loaded' => php_ini_loaded_file(),
    //check the extensions being loaded (verify openssl is actually loaded in php)
    'loaded extensions' => get_loaded_extensions(),
]);
cavo789 commented 6 years ago

Here it is 👍

array(3) { ["function openssl_encrypt exists"]=> bool(false) ["php.ini file loaded"]=> string(62) "C:\Christophe\tools\wamp64\bin\apache\apache2.4.23\bin\php.ini" ["loaded extensions"]=> array(47) { [0]=> string(4) "Core" [1]=> string(6) "bcmath" [2]=> string(8) "calendar" [3]=> string(5) "ctype" [4]=> string(4) "date" [5]=> string(6) "filter" [6]=> string(4) "hash" [7]=> string(5) "iconv" [8]=> string(4) "json" [9]=> string(3) "SPL" [10]=> string(4) "pcre" [11]=> string(8) "readline" [12]=> string(10) "Reflection" [13]=> string(7) "session" [14]=> string(8) "standard" [15]=> string(7) "mysqlnd" [16]=> string(9) "tokenizer" [17]=> string(3) "zip" [18]=> string(4) "zlib" [19]=> string(6) "libxml" [20]=> string(3) "dom" [21]=> string(3) "PDO" [22]=> string(3) "bz2" [23]=> string(9) "SimpleXML" [24]=> string(3) "xml" [25]=> string(4) "wddx" [26]=> string(9) "xmlreader" [27]=> string(9) "xmlwriter" [28]=> string(14) "apache2handler" [29]=> string(4) "Phar" [30]=> string(10) "com_dotnet" [31]=> string(8) "fileinfo" [32]=> string(2) "gd" [33]=> string(7) "gettext" [34]=> string(3) "gmp" [35]=> string(4) "imap" [36]=> string(8) "mbstring" [37]=> string(4) "exif" [38]=> string(6) "mysqli" [39]=> string(9) "pdo_mysql" [40]=> string(10) "pdo_sqlite" [41]=> string(4) "soap" [42]=> string(7) "sockets" [43]=> string(7) "sqlite3" [44]=> string(6) "xmlrpc" [45]=> string(3) "xsl" [46]=> string(12) "Zend OPcache" } }

KJLJon commented 6 years ago

When you said you modified the php.ini file and uncommented the ; did you modify the php.ini that exists in your apache bin folder? Located: C:\Christophe\tools\wamp64\bin\apache\apache2.4.23\bin\php.ini

I am not 100% sure, but I think wamp might automatically generate that file from the phpForApache.ini (which is probably located in your php 7.2 folder) when you switch your php version inside of wamp

cavo789 commented 6 years ago

I am not 100% sure, but I think wamp might automatically generate that file from the phpForApache.ini (which is probably located in your php 7.2 folder) when you switch your php version inside of wamp

This is correct. With wamp, there are two files in the 7.2 folders that we need to modify (don't know why but I do the changes in the two) : php.ini and phpForApache.ini and yes, by switching from one version of PHP to an another, it seems that wamp copy the phpForApache.ini file from the 7.2 folder to C:\Christophe\tools\wamp64\bin\apache\apache2.4.23\bin\php.ini.

I've check that last file : references to the /ext folder is well to the 7.2 folder.

It seems that my problem isn't related to lockbox but well to wamp so don't loose anymore your time. I really appreciate your help KJLJon but here, I think it's on my side to understand why wamp isn't working as expected.

Perhaps a suggestion for lockbox : correctly handle the fact that openssl isn"t loaded... Perhaps something to do in CryptoCoreBuiltin in order to make lockbox working even when openssl isn't there.

Once again, many thanks for your help and your opensource code; it's really great (y)

KJLJon commented 6 years ago

@starekrow I think you can close this issue :)