Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix PGP/MIME sending and viewing #10195

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions lib/Controller/DraftsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ public function __construct(string $appName,
* @param string $body
* @param string $editorBody
* @param bool $isHtml
* @param bool $isPgpMime
* @param bool $smimeSign
* @param bool $smimeEncrypt
* @param array<int, string[]> $to i. e. [['label' => 'Linus', 'email' => '[email protected]'], ['label' => 'Pierre', 'email' => '[email protected]']]
Expand Down Expand Up @@ -89,7 +90,8 @@ public function create(
?int $smimeCertificateId = null,
?int $sendAt = null,
?int $draftId = null,
bool $requestMdn = false) : JsonResponse {
bool $requestMdn = false,
bool $isPgpMime = false) : JsonResponse {
$account = $this->accountService->find($this->userId, $accountId);
if ($draftId !== null) {
$this->service->handleDraft($account, $draftId);
Expand All @@ -108,6 +110,7 @@ public function create(
$message->setSmimeSign($smimeSign);
$message->setSmimeEncrypt($smimeEncrypt);
$message->setRequestMdn($requestMdn);
$message->setPgpMime($isPgpMime);

if (!empty($smimeCertificateId)) {
$smimeCertificate = $this->smimeService->findCertificate($smimeCertificateId, $this->userId);
Expand All @@ -128,6 +131,7 @@ public function create(
* @param string $body
* @param string $editorBody
* @param bool $isHtml
* @param bool $isPgpMime
* @param bool $failed
* @param array<int, string[]> $to i. e. [['label' => 'Linus', 'email' => '[email protected]'], ['label' => 'Pierre', 'email' => '[email protected]']]
* @param array<int, string[]> $cc
Expand Down Expand Up @@ -156,7 +160,8 @@ public function update(int $id,
?string $inReplyToMessageId = null,
?int $smimeCertificateId = null,
?int $sendAt = null,
bool $requestMdn = false): JsonResponse {
bool $requestMdn = false,
bool $isPgpMime = false): JsonResponse {
$message = $this->service->getMessage($id, $this->userId);
$account = $this->accountService->find($this->userId, $accountId);

Expand All @@ -174,6 +179,7 @@ public function update(int $id,
$message->setSmimeSign($smimeSign);
$message->setSmimeEncrypt($smimeEncrypt);
$message->setRequestMdn($requestMdn);
$message->setPgpMime($isPgpMime);

if (!empty($smimeCertificateId)) {
$smimeCertificate = $this->smimeService->findCertificate($smimeCertificateId, $this->userId);
Expand Down
4 changes: 4 additions & 0 deletions lib/Controller/OutboxController.php
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ public function create(
?int $smimeCertificateId = null,
?int $sendAt = null,
bool $requestMdn = false,
bool $isPgpMime = false,
): JsonResponse {
$account = $this->accountService->find($this->userId, $accountId);

Expand All @@ -122,6 +123,7 @@ public function create(
$message->setHtml($isHtml);
$message->setInReplyToMessageId($inReplyToMessageId);
$message->setSendAt($sendAt);
$message->setPgpMime($isPgpMime);
$message->setSmimeSign($smimeSign);
$message->setSmimeEncrypt($smimeEncrypt);
$message->setRequestMdn($requestMdn);
Expand Down Expand Up @@ -194,6 +196,7 @@ public function update(
?int $smimeCertificateId = null,
?int $sendAt = null,
bool $requestMdn = false,
bool $isPgpMime = false,
): JsonResponse {
$message = $this->service->getMessage($id, $this->userId);
if ($message->getStatus() === LocalMessage::STATUS_PROCESSED) {
Expand All @@ -209,6 +212,7 @@ public function update(
$message->setHtml($isHtml);
$message->setInReplyToMessageId($inReplyToMessageId);
$message->setSendAt($sendAt);
$message->setPgpMime($isPgpMime);
$message->setSmimeSign($smimeSign);
$message->setSmimeEncrypt($smimeEncrypt);
$message->setRequestMdn($requestMdn);
Expand Down
7 changes: 7 additions & 0 deletions lib/Db/LocalMessage.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@
* @method void setInReplyToMessageId(?string $inReplyToId)
* @method int|null getUpdatedAt()
* @method setUpdatedAt(?int $updatedAt)
* @method bool|null isPgpMime()
* @method setPgpMime(bool $pgpMime)
* @method bool|null getSmimeSign()
* @method setSmimeSign(bool $smimeSign)
* @method int|null getSmimeCertificateId()
Expand Down Expand Up @@ -110,6 +112,9 @@ class LocalMessage extends Entity implements JsonSerializable {
/** @var int|null */
protected $updatedAt;

/** @var bool */
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
/** @var bool */
/** @var bool|null */

protected $pgpMime;

/** @var bool|null */
protected $smimeSign;

Expand Down Expand Up @@ -139,6 +144,7 @@ public function __construct() {
$this->addType('html', 'boolean');
$this->addType('failed', 'boolean');
$this->addType('updatedAt', 'integer');
$this->addType('pgpMime', 'boolean');
$this->addType('smimeSign', 'boolean');
$this->addType('smimeCertificateId', 'integer');
$this->addType('smimeEncrypt', 'boolean');
Expand All @@ -160,6 +166,7 @@ public function jsonSerialize() {
'body' => $this->getBody(),
'editorBody' => $this->getEditorBody(),
'isHtml' => ($this->isHtml() === true),
'isPgpMime' => ($this->isPgpMime() === true),
'inReplyToMessageId' => $this->getInReplyToMessageId(),
'attachments' => $this->getAttachments(),
'from' => array_values(
Expand Down
9 changes: 9 additions & 0 deletions lib/IMAP/ImapMessageFetcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ class ImapMessageFetcher {
private ?string $unsubscribeUrl = null;
private bool $isOneClickUnsubscribe = false;
private ?string $unsubscribeMailto = null;
private bool $isPgpMimeEncrypted = false;

public function __construct(int $uid,
string $mailbox,
Expand Down Expand Up @@ -148,6 +149,13 @@ public function fetchMessage(?Horde_Imap_Client_Data_Fetch $fetch = null): IMAPM
// analyse the body part
$structure = $fetch->getStructure();

$this->isPgpMimeEncrypted = ($structure->getType() === 'multipart/encrypted'
&& $structure->getContentTypeParameter('protocol') === 'application/pgp-encrypted');
if ($this->isPgpMimeEncrypted) {
$this->plainMessage = $this->loadBodyData($structure, '2', false);
$this->attachmentsToIgnore[] = $structure->getPartByIndex(1)->getName();
}

$this->hasAnyAttachment = $this->hasAttachments($structure);

$isEncrypted = $this->smimeService->isEncrypted($fetch);
Expand Down Expand Up @@ -267,6 +275,7 @@ public function fetchMessage(?Horde_Imap_Client_Data_Fetch $fetch = null): IMAPM
$isSigned,
$signatureIsValid,
$this->htmlService, // TODO: drop the html service dependency
$this->isPgpMimeEncrypted,
);
}

Expand Down
38 changes: 38 additions & 0 deletions lib/Migration/Version4100Date20240916174827.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

declare(strict_types=1);

/**
* SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/

namespace OCA\Mail\Migration;

use Closure;
use OCP\DB\ISchemaWrapper;
use OCP\DB\Types;
use OCP\Migration\IOutput;
use OCP\Migration\SimpleMigrationStep;

class Version4100Date20240916174827 extends SimpleMigrationStep {

/**
* @param IOutput $output
* @param Closure(): ISchemaWrapper $schemaClosure
* @param array $options
* @return null|ISchemaWrapper
*/
public function changeSchema(IOutput $output, Closure $schemaClosure, array $options): ?ISchemaWrapper {
$schema = $schemaClosure();

$outboxTable = $schema->getTable('mail_local_messages');
if (!$outboxTable->hasColumn('pgp_mime')) {
$outboxTable->addColumn('pgp_mime', Types::BOOLEAN, [
'notnull' => false,
'default' => false,
]);
}
return $schema;
}
}
10 changes: 9 additions & 1 deletion lib/Model/IMAPMessage.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ class IMAPMessage implements IMessage, JsonSerializable {
private bool $isEncrypted;
private bool $isSigned;
private bool $signatureIsValid;
private bool $isPgpMimeEncrypted;

public function __construct(int $uid,
string $messageId,
Expand Down Expand Up @@ -98,7 +99,8 @@ public function __construct(int $uid,
bool $isEncrypted,
bool $isSigned,
bool $signatureIsValid,
Html $htmlService) {
Html $htmlService,
bool $isPgpMimeEncrypted) {
$this->messageId = $uid;
$this->realMessageId = $messageId;
$this->flags = $flags;
Expand Down Expand Up @@ -128,6 +130,7 @@ public function __construct(int $uid,
$this->isSigned = $isSigned;
$this->signatureIsValid = $signatureIsValid;
$this->htmlService = $htmlService;
$this->isPgpMimeEncrypted = $isPgpMimeEncrypted;
}

public static function generateMessageId(): string {
Expand Down Expand Up @@ -317,6 +320,7 @@ public function jsonSerialize() {
'isOneClickUnsubscribe' => $this->isOneClickUnsubscribe,
'unsubscribeMailto' => $this->unsubscribeMailto,
'scheduling' => $this->scheduling,
'isPgpMimeEncrypted' => $this->isPgpMimeEncrypted,
];
}

Expand Down Expand Up @@ -459,6 +463,10 @@ public function isOneClickUnsubscribe(): bool {
return $this->isOneClickUnsubscribe;
}

public function isPgpMimeEncrypted(): bool {
return $this->isPgpMimeEncrypted;
}

/**
* Cast all values from an IMAP message into the correct DB format
*
Expand Down
3 changes: 2 additions & 1 deletion lib/Service/MailTransmission.php
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,8 @@ public function sendMessage(Account $account, LocalMessage $localMessage): void
$mimePart = $mimeMessage->build(
$localMessage->isHtml(),
$localMessage->getBody(),
$attachmentParts
$attachmentParts,
$localMessage->isPgpMime() === true
);

// TODO: add smimeEncrypt check if implemented
Expand Down
23 changes: 22 additions & 1 deletion lib/Service/MimeMessage.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public function __construct(DataUriParser $uriParser) {
* @param Horde_Mime_Part[] $attachments
* @return Horde_Mime_Part
*/
public function build(bool $isHtml, string $content, array $attachments): Horde_Mime_Part {
public function build(bool $isHtml, string $content, array $attachments, bool $isPgpMime = false): Horde_Mime_Part {
if ($isHtml) {
$imageParts = [];
if (empty($content)) {
Expand Down Expand Up @@ -115,6 +115,27 @@ public function build(bool $isHtml, string $content, array $attachments): Horde_
} else {
$bodyPart = $alternativePart;
}
} elseif ($isPgpMime) {
$contentPart = new Horde_Mime_Part();
$contentPart->setType('application/octet-stream');
$contentPart->setContentTypeParameter('name', 'encrypted.asc');
$contentPart->setTransferEncoding('7bit');
$contentPart->setDisposition('inline');
$contentPart->setDispositionParameter('filename', 'encrypted.asc');
$contentPart->setDescription('OpenPGP encrypted message');
$contentPart->setContents($content);

$pgpIdentPart = new Horde_Mime_Part();
$pgpIdentPart->setType('application/pgp-encrypted');
$pgpIdentPart->setTransferEncoding('7bit');
$pgpIdentPart->setDescription('PGP/MIME Versions Identification');
$pgpIdentPart->setContents('Version: 1');

$bodyPart = new Horde_Mime_Part();
$bodyPart->setType('multipart/encrypted');
$bodyPart->setContentTypeParameter('protocol', 'application/pgp-encrypted');
$bodyPart[] = $pgpIdentPart;
$bodyPart[] = $contentPart;
} else {
$bodyPart = new Horde_Mime_Part();
$bodyPart->setType('text/plain');
Expand Down
1 change: 1 addition & 0 deletions src/components/Composer.vue
Original file line number Diff line number Diff line change
Expand Up @@ -1096,6 +1096,7 @@ export default {
smimeSign: this.shouldSmimeSign,
smimeEncrypt: this.shouldSmimeEncrypt,
smimeCertificateId: this.smimeCertificateForCurrentAlias?.id,
isPgpMime: this.encrypt,
}
},
saveDraft() {
Expand Down
5 changes: 4 additions & 1 deletion src/components/Message.vue
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
:message="message"
:full-height="fullHeight"
@load="$emit('load', $event)" />
<MessageEncryptedBody v-else-if="isEncrypted"
<MessageEncryptedBody v-else-if="isEncrypted || isPgpMimeEncrypted"
:body="message.body"
:from="from"
:message="message" />
Expand Down Expand Up @@ -128,6 +128,9 @@ export default {
isEncrypted() {
return isPgpgMessage(this.message.hasHtmlBody ? html(this.message.body) : plain(this.message.body))
},
isPgpMimeEncrypted() {
return this.message.isPgpMimeEncrypted
},
itineraries() {
return this.message.itineraries ?? []
},
Expand Down
3 changes: 3 additions & 0 deletions tests/Unit/Controller/DraftsControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,7 @@ public function testCreate(): void {
$message->setSendAt(null);
$message->setUpdatedAt(123456);
$message->setRequestMdn(false);
$message->setPgpMime(false);
$to = [['label' => 'Lewis', 'email' => '[email protected]']];
$cc = [['label' => 'Pierre', 'email' => '[email protected]']];

Expand Down Expand Up @@ -250,6 +251,7 @@ public function testCreateFromDraft(): void {
$message->setSendAt(null);
$message->setUpdatedAt(123456);
$message->setRequestMdn(false);
$message->setPgpMime(false);
$to = [['label' => 'Lewis', 'email' => '[email protected]']];
$cc = [['label' => 'Pierre', 'email' => '[email protected]']];

Expand Down Expand Up @@ -304,6 +306,7 @@ public function testCreateWithEmptyRecipients(): void {
$message->setSendAt(null);
$message->setUpdatedAt(123456);
$message->setRequestMdn(false);
$message->setPgpMime(false);

$account = new Account(new MailAccount());
$this->accountService->expects(self::once())
Expand Down
2 changes: 2 additions & 0 deletions tests/Unit/Controller/ListControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@ public function testUnsupportedMessage(): void {
false,
false,
$this->createMock(Html::class),
false,
);
$this->serviceMock->getParameter('mailManager')
->expects(self::once())
Expand Down Expand Up @@ -206,6 +207,7 @@ public function testUnsubscribe(): void {
false,
false,
$this->createMock(Html::class),
false,
);
$this->serviceMock->getParameter('mailManager')
->expects(self::once())
Expand Down
1 change: 1 addition & 0 deletions tests/Unit/Controller/OutboxControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,7 @@ public function testCreate(): void {
$message->setInReplyToMessageId('abc');
$message->setType(LocalMessage::TYPE_OUTGOING);
$message->setRequestMdn(false);
$message->setPgpMime(false);
$to = [['label' => 'Lewis', 'email' => '[email protected]']];
$cc = [['label' => 'Pierre', 'email' => '[email protected]']];

Expand Down
3 changes: 3 additions & 0 deletions tests/Unit/Model/IMAPMessageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ public function testIconvHtmlMessage() {
false,
false,
$htmlService,
false,
);

$actualHtmlBody = $message->getHtmlBody(123);
Expand Down Expand Up @@ -123,6 +124,7 @@ public function testSerialize() {
false,
false,
$this->htmlService,
false,
);

$json = $m->jsonSerialize();
Expand Down Expand Up @@ -156,6 +158,7 @@ public function testSerialize() {
'hasDkimSignature' => false,
'phishingDetails' => [],
'scheduling' => [],
'isPgpMimeEncrypted' => false,
], $json);
$this->assertEquals(1234, $json['uid']);
}
Expand Down
Loading