Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP gnupg_addsignkey Error: get_key failed

<?php

$res = gnupg_init();

gnupg_clearsignkeys($res);

$key = get_pub_key();// Value pasted below

$publicImp = gnupg_import($res, $key);

gnupg_addsignkey($res, $publicImp['fingerprint']);

echo gnupg_geterror($res); // Prints `get_key failed`

The key is generated from https://www.igolder.com/pgp/generate-key/

-----BEGIN PGP PUBLIC KEY BLOCK-----
Version: BCPG C# v1.6.1.0

mQENBF3FXV0BCACy/cvfn5oEZQG+Wz8803XvpHVP0NWwIDAuogOsSfDIZX3xBK4s
Ul8y/aXkhPeK4V+NwurNiGqWQXCg3tKX77pfFGo0iBwDQlACmNhxtga/bkDOROx+
O5Ete2olKJ0qVDJTIpA1XMTZK+dFor8oKDWGlYyZ7w1mVxit65fkmzxIdCKrKxGw
9KqHZDkzCwRlpyZ/cQYVPNrx+9GREal2b+XLN3Y9Lbx2OD5FTxO6tLee2XlQdaXJ
R/hetUnei0hoP3J1Zdp99Qh+SX7cGZ+jUcGSdrDEx8q066Q7473xVDJ3Lr0YG53K
EW3lA5z13IkMFn4Yn6taJgkyF/QJtMDZ0xlTABEBAAG0DEhpQGdtYWlsLmNvbYkB
HAQQAQIABgUCXcVdXQAKCRBYbc/NUpqITuYOB/9br/Oc2ahZrbE/GOUOWQc3Yp26
rE+gcp5OjX0WXHsqfxRpVgIb7Si0hr5PPPiCiahS7n44IfZ0CGFcenhv+Eb93h5t
UtC26LRfv1ACTF7tX0QZAeACvB/npB0MMTO/dlbl6eIBjCKHmGMBMBW8WHG39J0V
m5X4GwX6vdTPWQH5ZbpNQirvhLTdYBw6JGtkU++K1gW9kZcncV+3EsRnHIqF5B1G
LnGBYOm9bAdCz2b6J8dkPqjCA9Uw5mvBI5ikB0NruZH8kXynzibnwTpXTc+N3Dsl
5dlRVPh7bIIpGdOVKWqtdnLpNDiY/dQhhmNJ64ZkXOnMibx0qPNYmy3iIgN/
=pvzn
-----END PGP PUBLIC KEY BLOCK-----

The key info looks like this.

[fingerprint] => AF4A061F20FFFE141AA366FE586DCFCD529A884E
[keyid] => 586DCFCD529A884E
[timestamp] => 1573215581
[expires] => 0
[is_secret] =>
[invalid] =>
[can_encrypt] => 1
[can_sign] => 1
[disabled] =>
[expired] =>
[revoked] =>

Could not find any solution, kindly help.

like image 439
anwerj Avatar asked Nov 08 '19 13:11

anwerj


1 Answers

In order for this to work you need to import the private key also.

According to https://www.gnupg.org/gph/en/manual/c235.html:

Key Integrity

When you distribute your public key, you are distributing the public components of your master and subordinate keys as well as the user IDs. Distributing this material alone, however, is a security risk since it is possible for an attacker to tamper with the key. The public key can be modified by adding or substituting keys, or by adding or changing user IDs. By tampering with a user ID, the attacker could change the user ID's email address to have email redirected to himself. By changing one of the encryption keys, the attacker would also be able to decrypt the messages redirected to him.

Using digital signatures is a solution to this problem. When data is signed by a private key, the corresponding public key is bound to the signed data. In other words, only the corresponding public key can be used to verify the signature and ensure that the data has not been modified. A public key can be protected from tampering by using its corresponding private master key to sign the public key components and user IDs, thus binding the components to the public master key. Signing public key components with the corresponding private master signing key is called self-signing, and a public key that has self-signed user IDs bound to it is called a certificate.

Here is a sample working code:

<?php

$res = gnupg_init();

gnupg_clearsignkeys($res);

$publicKey = file_get_contents('public.key');
$privateKey = file_get_contents('private.key');

$publicImp = gnupg_import($res, $publicKey);
$privateImp = gnupg_import($res, $privateKey);

gnupg_addsignkey($res, $publicImp['fingerprint']);

echo gnupg_geterror($res);

The public.key content:

-----BEGIN PGP PUBLIC KEY BLOCK-----
Version: BCPG C# v1.6.1.0
mQENBF3TmV4BCACOy5NyL9OMpCR2T5JpuRnaIZCiQ4dyuEx5HikChBBOo6ttB9Pp
whM8kdZf2Xd7bF9SBX5ZEESNCx1aVvo5pKEh/ziRbx4OQINmVyKfhoG7OIWiXoSI
S8swRISmRteIsARPKn7yJfoxmrlCXwAURDOBfFYbmWDdKmeVSz/AS/tysqmPg12D
WJ7Z4NlNDy2Z0g3yS39xIbqi/tf23NmfCZ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/sQ+
mCyEAFy93kEzrp2m/Un/Nt/wJETWM2SCiKJyw4Rddu1gRPobqhrRK4aIW9Msn/UN
Ft0Eg4xy9PsB1jiL9CkMkaWKDUv8EoMYtjgN1JzrQWA= =BpXt
-----END PGP PRIVATE KEY BLOCK-----

I hope this helps.

like image 123
Armino Popp Avatar answered Nov 08 '22 19:11

Armino Popp