GnuPG returns false in PHP

1

I'm using the GnuPG module in PHP and tested it exactly with this code:

$keydata = '-----BEGIN PGP PRIVATE KEY BLOCK-----
Version: BCPG C# v1.6.1.0

lQOsBFh5ZewBCACBqYoooH9AFQ2WMhhaJKD0+6eQnYQKroA5zH8xNFp+PTg7WXSR
HJaISLto48Zrl4HQtSM6eOpxiL3M5qc+gchueWxhc2ds4XYrDosrN2gejZfjfsII
bAqHjz8RIAORaIAlPalToKcnU/75x8e0Rh05n5rjp43zl1gsAFhCs+YhnJE1sQBk
rpuN3cxMeIiJVOS03O6iJKXVxZwsLSraGslC1MVOY5emAfsBpS4KBf281ZcLVdq5
/DkUS5RWXLFOl73M3Wq0PQb85/VFlRdwiBTckzepF7EecY36MV+ExfVIl3ItcLSo
JZxY8uvYhgDjLnlv1YkcqK/sE30vzOKVMM+tABEBAAH/AwMC4YF5S+ay4apgajcm
C4RRYUj4ZEpa2maY/fJVS3S25jBJJlx2w873nqVvQR+4Zjp9PyDuA6XH/MFQBYLu
f3OVu5dLFa/VTwm7fA9dHskFE8GYPPvXKbJxsF33ABEFLaVJ2UGX+qluZ9Ux9Cc6
JZ4X1F/dzeq5abK24+cPIta5CTE1ZnZ3gSVthKMrEZ/S11l+czgIrgYcBcl7XRL4
r93bi+KJGKPgpq/eQ7UPhuGTMgKV62JITOxbkGCHBej6XOiz6q+fu9Y6VdyAW5Yt
7Bk2VP7bsOVRBLnZ+WKkAcxA3xno5zv88x0nCiFUFnnboo/WM2PY7S8oMVhL+EhG
xLztXE8mVVutcV/NVVpUWOJH1j0RsrzCYiilIiZW86q4WyKfImg2o6OZiSILqaHT
sDU8XX9jyEu0GD5U8ZVK/bkI59pnZrNToZJqqaUMnRTHIDnUqiXMbSd6SJEgEPMG
sJYXBykvPoPIN0A0PaxhbK+jQ6sSZoWIITL3HQQcvabDzl0TqPm3dclgrqBg8g6x
h31R+iNVF+Itf/QBBBWSklAVCKWb3BQrfKEdddxi0FaqF26A11FOzIwO6cFKJK+/
tZY8dwmzh/FlsmtGztmPbbaYKQ/KafJJXpS1B3NkjdrnWMlcmPlrzX5dk6M0PfQU
VKiNi994C2npPvQJtXzNYWqlTBG85Iy9jbTFTjAatP0ira1zib+5QAAJNMbXSGBN
lCP1KCOR8bhJjEGe5ANP8jI1iPNsTsYh8EbmCHmjfNgMtCiGvNgGmmbnI/KwoeM9
AwCXMwhcvTuLL2CX6g1Q9MtE09u30u1eFKaCwXpsyMP4uITmaye320Y+jvhfYYE5
XVSQTSZQIaVhp/P/Y7s1mIZ+dAEuCR+riaXXzumWbLQNdGVzdEB0ZXN0LmNvbYkB
HAQQAQIABgUCWHll7AAKCRDjfnMbSIMMA3GFB/4oBCm4n+NRGJ5vlkNZdNE1SSDl
uxMFKx7MSGoitupuCa1hTGTb6RVaHLkmrT/P6KLOjnKdp/lSDo0xd1C00hYhUP2C
NjecvhgzStHPRlw85+2fVDQEni9+pe6zZp1MDztGeHwZm4kBFx6ZittnBIWdET56
RlBzeu04lRffCdoPRiyBqG/nT7v8uNswsWUDNb7O9QiS+30FXOq19KVhAtl1zyp4
i3CHAsPzAcYYWP+wCyd5FGohdzfjug5zB2xUzD6xzyklhACFljgyepUjhEXI/2at
qBNP7buMVimrscjuB5jUzSfYI1V1E4VN/5Ib5QurzbQ35KoRY9ISgVXFHVIF
=p4Wt
-----END PGP PRIVATE KEY BLOCK-----';

$res = gnupg_init();
$info = gnupg_import($res,$keydata);
var_dump($info);
  

This private key was generated for testing!

Result:

bool(false)

I tested with this key above and also with the "original" key, both have $info as false , which is not expected. Second the documentation (and the comment 9 years ago) on link was to be displayed information about the key that was imported.

No error is reported in the logs and the module is installed properly and with all the necessary dependencies, apparently.

Does this extension no longer work or is something wrong?

    
asked by anonymous 14.01.2017 / 00:51

1 answer

0

I have found a solution. : D

Problem Cause:

PHP / NGINX was not able to access the folder where the file was imported and so false was displayed.

Try running the same script as root using PHP-CLI and you probably will not have the problem.

Solution:

Enter a folder outside the web access:

cd /usr

Create a folder for example:

mkdir .nomelegal

Give NGINX permission:

chown -R nginx .nomelegal
chgrp -R nginx .nomelegal

If you're using Apache, change nginx to www-data , I believe that's it.

Then determine the location in PHP by adding this:

<?php

putenv("GNUPGHOME=/usr/.nomelegal");

$keydata = '...';

$res = gnupg_init();
$info = gnupg_import($res,$keydata);
var_dump($info);

If everything is right now PHP is able to read and write inside the folder so you can import public and private keys!

Note:

I do not know if it is necessary, but Centos does not run gpg-agent automatically, so you should run:

gpg-agent --daemon --use-standard-socket

This was used to generate keys, I do not know if it's necessary for GnuPG in PHP.

    
14.01.2017 / 03:18