0

I've just installed gnupg pecl extension and required libraries, gnupg seems to be enabled in my phpinfo();

php -i | grep gnupg

/etc/php/7.3/cli/conf.d/20-gnupg.ini,
gnupg
gnupg support => enabled

when i try to call gnupg_init() it's succeed and give me resource id but then i'm trying gnupg_import and it always fails and return false

when i try to execute command in shell: gpg --import somekey.txt it works here is my test test.key:

-----BEGIN PGP PUBLIC KEY BLOCK-----

mQINBFxmhuYBEADLObpGfH0eQmrhF9Rs4FrYZ2D/QbiDk0TRY/M3aop2oYL8sjCE
hgIvQ9iKAkGydT0bY4pZpRZj5+HQVvDg6ps/7iXPX6RQUsS4SufvoAFUZLIXTla8
WTXC1fY+ZLV29H2NdzT1ZIh72xKquFV+hh0DuI16J1AYSoCrqx4FDZ9vfivCa3Qb
Xyao3B6BHdF9GqvjFWAjp45Fbg4voEPcbPvhoD40ilsO9PDx4/LTQu7afs8HKlAh
XtiyCEJBq/hyiRagLmt81ZpEbQXJiTkOylas1WsJdQpMfb9fTmwDSE0baqwoQUS0
y20w0+3+BKaIrDGu9ErSqG3qUO5sfolczvZZK7ccqfgYTBgvBECL3fA/5kMLh7/X
wgn7Hjw8NqpfwvuPCwQOlI4ovpGSsJceF9IqGauwQUFFHaHqRtah7lXHcw8od0iG
QIT+EIqvYOV19/g0QfZzFQePMSU/+ApkqcIL902TVMYPRcP/1CqcmQMIDdFFycdZ
AG27nDw514uJEh0x7HV9JdI3ZUWrwlMLq9wnVVwMgKGSfVJ/bp+irwydfZfcFmHo
5hvQDcOrM0xMYch8JZ47ZN+u91KjBQMO0QAz1RWYXFhqit9gHm4lIQ65P86VhbnB
ChWa/rYH8/d29lX69TR4IBitBcyaZwJfZuxPFvKrrG3ljt4RWEdVc/AOmQARAQAB
tAt0ZXN0aW5nIGtleYkCNgQTAQoAIAUCXGaG5gIbAwULCQgHAwUVCgkICwQWAgEA
Ah4BAheAAAoJEBeRbnwEwXF5sWQP/iQT4AQDVdyjfk+XyaFx2HITBvaM0SytIThj
qFzChBw7KB/W7MV957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UEjoUMMh1
U+dS1+D49nMrdR+APsu1ZNYecVvky7//DgZMjUblPYfHs3kbhganyupHFPLCxSEO
gTe4zU9QnGOaoO6+sUBPhZyj8HLLwW5tiGQxhwRv46aSAvCBJvEw26bd43p8q+M0
ZhokekucpgaBqPAZ9CyOweIQ5UVOdwDiTDAzIfi73Zf7w+pt2J/qNKWirR21n0/J
mBZPqLLWpRRt3E9GHZUrxeht8qQGmDb1akDJWCNQjdxhhASSgN1CJyIOieUDud3/
WQARAQABiQIfBBgBCgAJBQJcZobmAhsMAAoJEBeRbnwEwXF5PJcP/3sZZinBNCtE
wfWe+vQ9raGO/wvqkBk/NMHx67Qqj90rbCHlERGcNdcq0ssz9HlPPCJIr51vDKJ0
2aPfWjJp/L7M+HmFJafOpyLeJq3F5CZ8QsfQ0g6hq/TiBilhDiL4xOSmpnarR8By
jHJ2KchLjuuxOfiNj3S95CrxV0RspssWNwl2zHHeDoYCvLI+4vSTwg9TD7wTvUbn
BQBh/o7tx+lCD/BQrjrXCHp7OQT9sHemkNB08M8qLCg6xA60F8JuJMTd24gkaXwE
/0cltdxExjJLPDyREnUVzfjNN3fi/fK2ujjmFJtixsjoqXWME2zmDT83Y5O7+qJy
D7t6DyaIc4y978xQUaey4KYo5zAVzB/GkOr1nqYbTiKb4vqpQXKEGviMH/Qjendm
1O75GbXlPu6zbZk4pbhEeXo2RAKfWWqzpy+9rH249ecfNixBavRe+bdk3W8L9Izi
vwY3kJO7DNRqAvmIjA+AgxNtCgRgGFEmwKfWB5q0NjCIxmiOGZKhpSOO+NPPqut8
rW/dglT0SccTE95Y4UDkS6MdM7W66306+6d78zTneglnqx/z8RPlLsJfzKicmOeY
jWqp2SlPkn0nF1d+NXUepjF3/NWjvKpfeQfZIiOXZKdfuPVaUcjVbpBCuxQnUcLg
Epd4OakW3tIP+3vqfv0nJ+weizFNq5Tj
=wIHb
-----END PGP PUBLIC KEY BLOCK-----

do i have to initialize gpg somehow before using it in my web application? point me into the right direction pls

exported previously imported key by fingerprint:

gpg --export --armor B4A57058391D51C5853BBFA317916E7C04C17179 > /var/www/test/test.key

below is my test.php:

<?php
$key=file_get_contents('/var/www/test/test.key');
$gpg=gnupg_init();
print_r($gpg); //returns Resource id #7 
$res=gnupg_import($gpg, $key);
print_r($res); //returns empty string
var_dump($res); //returns (bool)false
$er=gnupg_geterror($gpg);
var_dump($er); //return (bool)false
?>

p.s. using nginx+php7.3-fpm and gpg 1.4.23 my gnupg homedir is in /var/www/.gnupg belongs to nginx/php user and has required previleges 600/700

  • Are there any Warnings in your PHP error log, or with `ini_set('display_errors', 'on'); error_reporting(E_ALL);`? Often, functions that return false on failure will raise warnings describing the specific error. – IMSoP Feb 15 '19 at 10:20
  • i got this warning: php-fpm7.3[31083]: [15-Feb-2019 05:02:50] NOTICE: PHP message: PHP Warning: Module 'gnupg' already loaded in Unknown on line 0. error_reporting is E_ALL display_errors=On – Olympic Bear Feb 15 '19 at 10:56
  • @МишкаОлимпийский What is the return value of `gnupg_geterror()` after your `gnupg_import()` call failed? – Progman Feb 15 '19 at 13:21
  • @Progman (bool)false – Olympic Bear Feb 15 '19 at 16:15
  • @МишкаОлимпийский Please edit your question to include the source code you have now with the added `gnupg_geterror()` check. – Progman Feb 15 '19 at 16:20

1 Answers1

0

There might be newlines at fault here, because you're posting the key contents instead of the actual key file. Please replace that field with upload-file field and it should cause no newline issues (as long as you didn't edit the exported keys, but I'd export them once again, just to make sure)

I just tried and both armored and normal public exported files work fine with the gnupg_import. Code I tried:

//gpg --export --armor 4E8D5B22A24A1CFFA5BD2E771A9A0D703BDDF81B > public-armor.key
$PublicData = file_get_contents('public-armor.key');
$res=gnupg_import($gpg, $PublicData);
var_dump($res); 
$er=gnupg_geterror($gpg);
var_dump($er); 

My output:

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) "4E8D5B22A24A1CFFA5BD2E771A9A0D703BDDF81B"
}
/tmp/so_5469155777_test_php_gnupg.php:12:
bool(false)

EDIT: I just gave it a spin on DO droplet to confirm the bug and while it's weird it's really there. Please modify the beginning of your key from:

-----BEGIN PGP PUBLIC KEY BLOCK----- Version: GnuPG v1

to:

-----BEGIN PGP PUBLIC KEY BLOCK----- 

and it will all work nicely. (yes, just remove the version info from it)

Jan Myszkier
  • 2,714
  • 1
  • 16
  • 23
  • Hey, please see my edit, it should solve the issue for you – Jan Myszkier Feb 17 '19 at 10:31
  • thanks for your time, but i forgot to update that part of question because armored export already stripped key of the version, and it did not make any changes – Olympic Bear Feb 17 '19 at 12:11
  • did you also made sure the key file is split by newlines? Because it's not if you copypaste SO key into a file. (if you did, please update the SO question so the key includes the newlines and the `=wIHb` should be in its own line, too) – Jan Myszkier Feb 17 '19 at 13:58
  • Hi, man! the "=wIHb" already on it's own line. Since i've exported key that have already been imported to the keyring, so there should be no issues with formatting. I'm incline to conclusion that pecl gnupg extension or gpg itself installed wrong. may be do i have to initialize it somehow before using? – Olympic Bear Feb 18 '19 at 05:54
  • you don't have to initialize it. on "clean" server I installed only php-gnupg + php7.3 (from ondrej PPA) and that was all I needed to do to make it work. Key and code works fine there. Have you tried to use CLI ? or are you simply encountering nginx permission issues which I described on https://stackoverflow.com/a/54732387/2129097 ? – Jan Myszkier Feb 18 '19 at 09:10