I'm trying to use the PHP GnuPG extension to perform PGP encryption in my PHP script. The script works as expected when running with the built-in test server (php -S), but it fails to import a PGP public key when served through Nginx. I'm encountering the "Error importing public key" issue.
Here's an overview of the situation:
PHP Version: 8.1.24 Nginx Version: nginx/1.18.0 (Ubuntu)
putenv("GNUPGHOME=/tmp");
$pubkey = "-----BEGIN PGP PUBLIC KEY BLOCK-----
Version: GnuPG v1.2.6 (GNU/Linux)
[KEY HERE]
-----END PGP PUBLIC KEY BLOCK-----";
$enc = (null);
$res = gnupg_init();
var_dump($res);
$rtv = gnupg_import($res, $pubkey);
$rtv = gnupg_addencryptkey($res, "C25F29936D9046D73A77DCF8244F423AED8F1481");
var_dump($rtv);
$enc = gnupg_encrypt($res, "just a test to see if anything works");
var_dump($enc);
echo "Encrypted Data: " . $enc . "<br/>";
I tested the script with nginx and the php test server. While using the php server I get a output like this:
resource(2) of type (ctx)
array(9) { ["imported"]=> int(0) ["unchanged"]=> int(1) ["newuserids"]=> int(0)
["newsubkeys"]=> int(0)
["secretimported"]=> int(0)
["secretunchanged"]=> int(0)
["newsignatures"]=> int(0)
["skippedkeys"]=> int(0)
["fingerprint"]=> string(40) "C25F29936D9046D73A77DCF8244F423AED8F1481"
}
bool(true)
Encrypted Data: -----BEGIN PGP MESSAGE----- [MESSAGE]-----END PGP MESSAGE-----
But with nginx the output is only:
resource(2) of type (ctx)
array(9) {
["imported"]=> int(0)
["unchanged"]=> int(1)
["newuserids"]=> int(0)
["newsubkeys"]=> int(0)
["secretimported"]=> int(0)
["secretunchanged"]=> int(0)
["newsignatures"]=> int(0)
["skippedkeys"]=> int(0)
["fingerprint"]=> string(40) "C25F29936D9046D73A77DCF8244F423AED8F1481" }
bool(false)
Encrypted Data:
I added the line extension = gnupg.so to these files:
/etc/php/8.1/cli/php.ini
/etc/php/8.1/fpm/php.ini
I also added a file to the conf.d folder called gnupg.ini
I've noticed that when I run phpinfo() on the test server, there's a PATH environment variable that includes GnuPG, but this variable is not present when running the same script through Nginx.
Any suggestions or insights would be greatly appreciated. Thank you!