#89 Added signing using GnuPG

This commit is contained in:
the-djmaze 2022-02-09 22:58:47 +01:00
parent 8d0154530c
commit 2c5322129c
5 changed files with 59 additions and 28 deletions

View file

@ -114,17 +114,16 @@ export const PgpUserStore = new class {
* Returns the first library that can.
*/
async getKeyForSigning(email) {
/* // TODO: sign in PHP fails
let key = GnuPGUserStore.getPrivateKeyFor(email, 1);
if (key) {
return ['gnupg', key];
}
*/
let key = OpenPGPUserStore.getPrivateKeyFor(email, 1);
if (key) {
return ['openpgp', key];
}
key = GnuPGUserStore.getPrivateKeyFor(email, 1);
if (key) {
return ['gnupg', key];
}
// return await this.getMailvelopePrivateKeyFor(email, 1);
}

View file

@ -431,6 +431,7 @@ class ComposePopupView extends AbstractViewPopup {
if (sign && !draft && sign[1]) {
if ('openpgp' == sign[0]) {
// Doesn't sign attachments
params.Html = params.Text = '';
let signed = new MimePart;
signed.headers['Content-Type'] =
'multipart/signed; micalg="pgp-sha256"; protocol="application/pgp-signature"';
@ -441,19 +442,17 @@ class ComposePopupView extends AbstractViewPopup {
signature.headers['Content-Transfer-Encoding'] = '7Bit';
signature.body = await OpenPGPUserStore.sign(data.toString(), sign[1], 1);
signed.children.push(signature);
params.Signed = signed.toString();
params.Boundary = signed.boundary;
data = signed;
/*
} else if ('gnupg' == sign[0])) {
} else if ('gnupg' == sign[0]) {
// TODO: sign in PHP fails
// params.SignData = data.toString();
params.SignFingerprint = sign[1].fingerprint;
params.SignPassphrase = await GnuPGUserStore.sign(sign[1]);
*/
} else {
throw 'Signing with ' + sign[0] + ' not yet implemented';
}
params.Signed = data.toString();
params.Boundary = data.boundary;
params.Html = params.Text = '';
}
if (encrypt) {
if ('openpgp' == encrypt) {

View file

@ -0,0 +1,34 @@
<?php
namespace MailSo\Base\StreamFilters;
class LineEndings extends \php_user_filter
{
public function filter($in, $out, &$consumed, $closing)
{
while ($bucket = \stream_bucket_make_writeable($in)) {
$bucket->data = \preg_replace('/\R/s', "\r\n", \rtrim($bucket->data, "\r"));
// $bucket->data = \preg_replace('/\R/s', "\n", \rtrim($bucket->data, "\r"));
$consumed += $bucket->datalen;
\stream_bucket_append($out, $bucket);
}
/*
private $buffer = '';
while ($bucket = \stream_bucket_make_writeable($in)) {
$this->buffer += $bucket->data;
$consumed += $bucket->datalen;
}
$this->buffer = \preg_replace('/\R/s', "\r\n", $this->buffer);
\stream_bucket_append($out, \stream_bucket_new($this->stream, $this->buffer));
$this->buffer = '';
*/
return PSFS_PASS_ON;
}
public static function appendTo($fp)
{
\stream_filter_append($fp, 'crlf', STREAM_FILTER_ALL);
}
}
\stream_filter_register('crlf', 'MailSo\\Base\\StreamFilters\\LineEndings');

View file

@ -54,7 +54,7 @@ class PartCollection extends \MailSo\Base\Collection
$aResult[] = "\r\n--{$this->sBoundary}\r\n";
$aResult[] = $oPart->ToStream();
}
$aResult[] = "\r\n--{$this->sBoundary}--";
$aResult[] = "\r\n--{$this->sBoundary}--\r\n";
return \MailSo\Base\StreamWrappers\SubStreams::CreateStream($aResult);
}
return null;

View file

@ -1271,21 +1271,23 @@ trait Messages
$sFingerprint = $this->GetActionParam('SignFingerprint', '');
$sPassphrase = $this->GetActionParam('SignPassphrase', '');
if ($sFingerprint) {
// TODO: but verify is still invalid
throw new \Exception('Sign using GnuPG not working yet');
/*
$GPG = $this->GnuPG();
$oBody = $oMessage->GetRootPart();
$fp = \fopen('php://memory', 'r+b');
$resource = $oBody->ToStream();
// \MailSo\Base\StreamFilters\LineEndings::appendTo($resource);
$oBody->Body = null;
$oBody->SubParts->Clear();
$oMessage->SubParts->Clear();
$oMessage->Attachments()->Clear();
\MailSo\Base\StreamFilters\LineEndings::appendTo($resource);
\stream_copy_to_stream($resource, $fp);
$GPG->addSignKey($sFingerprint, $sPassphrase);
$GPG->setsignmode(GNUPG_SIG_MODE_DETACH);
$sSignature = $GPG->signStream($fp);
$oMessage->SubParts->Clear();
$oMessage->Attachments()->Clear();
if (!$sSignature) {
throw new \Exception('GnuPG sign() failed');
}
$oPart = new MimePart;
$oPart->Headers->AddByName(
@ -1296,16 +1298,13 @@ trait Messages
\rewind($fp);
$oBody->Raw = $fp;
$oBody->Body = null;
$oBody->SubParts->Clear();
$oPart->SubParts->append($oBody);
$oAlternativePart = new MimePart;
$oAlternativePart->Headers->AddByName(\MailSo\Mime\Enumerations\Header::CONTENT_TYPE, 'application/pgp-signature; name="signature.asc"');
$oAlternativePart->Headers->AddByName(\MailSo\Mime\Enumerations\Header::CONTENT_TRANSFER_ENCODING, '7Bit');
$oAlternativePart->Body = $sSignature;
$oPart->SubParts->append($oAlternativePart);
*/
$oSignaturePart = new MimePart;
$oSignaturePart->Headers->AddByName(\MailSo\Mime\Enumerations\Header::CONTENT_TYPE, 'application/pgp-signature; name="signature.asc"');
$oSignaturePart->Headers->AddByName(\MailSo\Mime\Enumerations\Header::CONTENT_TRANSFER_ENCODING, '7Bit');
$oSignaturePart->Body = $sSignature;
$oPart->SubParts->append($oSignaturePart);
}
$aFingerprints = \json_decode($this->GetActionParam('EncryptFingerprints', ''), true);