#89 added view GnuPG armored keys

This commit is contained in:
the-djmaze 2022-01-28 17:33:52 +01:00
parent 70f315e76e
commit fe078174ab
6 changed files with 71 additions and 20 deletions

View file

@ -187,7 +187,24 @@ export const PgpUserStore = new class {
}
);
}
}
};
key.view = () => {
let pass = isPrivate ? prompt('Passphrase') : true;
if (pass) {
Remote.request('GnupgExportKey',
(iError, oData) => {
if (oData && oData.Result) {
key.armor = oData.Result;
showScreenPopup(ViewOpenPgpKeyPopupView, [key]);
}
}, {
KeyId: key.id,
isPrivate: isPrivate,
Passphrase: isPrivate ? pass : ''
}
);
}
};
return key;
};
this.gnupgPublicKeys(oData.Result.public.map(key => initKey(key, 0)));

View file

@ -460,7 +460,7 @@ class ComposePopupView extends AbstractViewPopup {
try {
const keys = PgpUserStore.getOpenPGPPrivateKeyFor(this.currentIdentity().email());
if (keys[0]) {
keys[0].decrypt(window.prompt('Password', ''));
keys[0].decrypt(window.prompt('Passphrase'));
cfg.privateKey = privateKey = keys[0];
}
} catch (e) {

View file

@ -98,6 +98,15 @@ trait Pgp
return $this->DefaultResponse(__FUNCTION__, $GPG ? $GPG->keyInfo('') : false);
}
public function DoGnupgExportKey() : array
{
$GPG = $this->GnuPG();
return $this->DefaultResponse(__FUNCTION__, $GPG ? $GPG->export(
$this->GetActionParam('KeyId', ''),
$this->GetActionParam('Passphrase', '')
) : false);
}
public function DoGnupgGenerateKey() : array
{
$fingerprint = false;

View file

@ -38,8 +38,7 @@ class GnuPG
public static function isSupported() : bool
{
return \class_exists('gnupg')
|| \SnappyMail\PGP\GPG::isSupported();
return \class_exists('gnupg') || GPG::isSupported();
}
private static $instance;
@ -56,13 +55,14 @@ class GnuPG
return $this->GnuPG ?: $this->GPG;
}
public function getGPG()
public function getGPG(bool $throw = true) : ?GPG
{
if (!$this->GPG) {
if (!\SnappyMail\PGP\GPG::isSupported()) {
if (GPG::isSupported()) {
$this->GPG = new GPG($this->homedir);
} else if ($throw) {
throw new \Exception('GnuPG not supported');
}
$this->GPG = new \SnappyMail\PGP\GPG($this->homedir);
}
return $this->GPG;
}
@ -214,8 +214,13 @@ class GnuPG
/**
* Exports a key
*/
public function export(string $fingerprint) /*: string|false*/
public function export(string $fingerprint, string $passphrase = '') /*: string|false*/
{
if ($passphrase) {
return $this->getGPG()
->addPassphrase($fingerprint, $passphrase)
->exportPrivateKey($fingerprint);
}
return $this->GnuPG
? $this->GnuPG->export($fingerprint)
: $this->GPG->export($fingerprint);
@ -258,13 +263,8 @@ class GnuPG
*/
public function generateKey(string $uid, string $passphrase) /*: string|false*/
{
if (!$this->GPG) {
if (!\SnappyMail\PGP\GPG::isSupported()) {
return false;
}
$this->GPG = new \SnappyMail\PGP\GPG($homedir);
}
return $this->GPG->generateKey($uid, $passphrase);
$GPG = $this->getGPG(false);
return $GPG ? $GPG->generateKey($uid, $passphrase) : false;
}
/**

View file

@ -341,12 +341,37 @@ class GPG
return false;
}
protected function _exportKey($keyId, $private = false)
{
$keys = $this->keyInfo($keyId, $private ? 1 : 0);
if (!$keys) {
throw new \Exception(($private ? 'Private' : 'Public') . ' key not found: ' . $keyId);
}
if ($private && $this->passphrases) {
$_ENV['PINENTRY_USER_DATA'] = \json_encode($this->passphrases);
}
$result = $this->exec([
$private ? '--export-secret-keys' : '--export',
'--armor',
\escapeshellarg($keys[0]['subkeys'][0]['fingerprint']),
]);
return $result['output'];
}
/**
* Exports a key
* Exports a public key
*/
public function export(string $fingerprint) /*: string|false*/
{
return false;
return $this->_exportKey($fingerprint);
}
/**
* Exports a private key
*/
public function exportPrivateKey(string $fingerprint) /*: string|false*/
{
return $this->_exportKey($fingerprint, true);
}
/**
@ -388,7 +413,7 @@ class GPG
public function addPassphrase($keyId, $passphrase)
{
$this->passphrases[$key] = $passphrase;
$this->passphrases[$keyId] = $passphrase;
return $this;
}

View file

@ -24,7 +24,7 @@
<tbody><tr><th colspan="4" data-i18n="SETTINGS_OPEN_PGP/TITLE_PRIVATE">Private keys</th></tr></tbody>
<tbody data-bind="foreach: gnupgPrivateKeys, i18nUpdate: gnupgPrivateKeys">
<tr>
<td>
<td data-bind="click: view">
<span data-bind="visible: can_sign" class="fontastic"></span>
<span data-bind="visible: can_decrypt" class="fontastic">🔓</span>
<span class="key-id" data-bind="text: id"></span>
@ -44,7 +44,7 @@
<tbody><tr><th colspan="4" data-i18n="SETTINGS_OPEN_PGP/TITLE_PUBLIC">Public keys</th></tr></tbody>
<tbody data-bind="foreach: gnupgPublicKeys, i18nUpdate: gnupgPublicKeys">
<tr>
<td>
<td data-bind="click: view">
<span data-bind="visible: can_verify" class="fontastic"></span>
<span data-bind="visible: can_encrypt" class="fontastic">🔒</span>
<span class="key-id" data-bind="text: id"></span>