From 3c002e993d21278c7f1b02c5c84a1167f20ed76e Mon Sep 17 00:00:00 2001 From: emersion Date: Thu, 11 Jan 2018 00:13:28 +0100 Subject: [PATCH] protonmail: use symetricallyEncrypt in Attachment.Encrypt --- protonmail/attachments.go | 18 ++++++------------ protonmail/crypto.go | 13 ++++++++++--- protonmail/messages.go | 2 +- 3 files changed, 17 insertions(+), 16 deletions(-) diff --git a/protonmail/attachments.go b/protonmail/attachments.go index ab6f013..6427c16 100644 --- a/protonmail/attachments.go +++ b/protonmail/attachments.go @@ -82,19 +82,13 @@ func (att *Attachment) Encrypt(ciphertext io.Writer, signed *openpgp.Entity) (cl return nil, errors.New("cannot encrypt attachment: no attachment key available") } - encryptedData, err := packet.SerializeSymmetricallyEncrypted(ciphertext, att.unencryptedKey.CipherFunc, att.unencryptedKey.Key, config) - if err != nil { - return nil, err + // TODO: sign and store signature in att.Signature + + hints := &openpgp.FileHints{ + IsBinary: true, + FileName: att.Name, } - - // TODO: sign, see https://github.com/golang/crypto/blob/master/openpgp/write.go#L287 - - literalData, err := packet.SerializeLiteral(encryptedData, true, att.Name, 0) - if err != nil { - return nil, err - } - - return literalData, nil + return symetricallyEncrypt(ciphertext, att.unencryptedKey, nil, hints, config) } // GetAttachment downloads an attachment's payload. The returned io.ReadCloser diff --git a/protonmail/crypto.go b/protonmail/crypto.go index ed17baf..7c76510 100644 --- a/protonmail/crypto.go +++ b/protonmail/crypto.go @@ -106,7 +106,7 @@ func generateUnencryptedKey(cipher packet.CipherFunction, config *packet.Config) }, nil } -func symetricallyEncrypt(ciphertext io.Writer, symKey *packet.EncryptedKey, signer *packet.PrivateKey, config *packet.Config) (plaintext io.WriteCloser, err error) { +func symetricallyEncrypt(ciphertext io.Writer, symKey *packet.EncryptedKey, signer *packet.PrivateKey, hints *openpgp.FileHints, config *packet.Config) (plaintext io.WriteCloser, err error) { // From https://github.com/golang/crypto/blob/master/openpgp/write.go#L172 encryptedData, err := packet.SerializeSymmetricallyEncrypted(ciphertext, symKey.CipherFunc, symKey.Key, config) @@ -129,6 +129,10 @@ func symetricallyEncrypt(ciphertext io.Writer, symKey *packet.EncryptedKey, sign } } + if hints == nil { + hints = &openpgp.FileHints{} + } + w := encryptedData if signer != nil { // If we need to write a signature packet after the literal @@ -136,8 +140,11 @@ func symetricallyEncrypt(ciphertext io.Writer, symKey *packet.EncryptedKey, sign // encryptedData. w = noOpCloser{encryptedData} } - - literalData, err := packet.SerializeLiteral(w, false, "", 0) + var epochSeconds uint32 + if !hints.ModTime.IsZero() { + epochSeconds = uint32(hints.ModTime.Unix()) + } + literalData, err := packet.SerializeLiteral(w, hints.IsBinary, hints.FileName, epochSeconds) if err != nil { return nil, err } diff --git a/protonmail/messages.go b/protonmail/messages.go index b7550c4..fc9766b 100644 --- a/protonmail/messages.go +++ b/protonmail/messages.go @@ -302,7 +302,7 @@ func (set *MessagePackageSet) Encrypt(mimeType string, signed *openpgp.Entity) ( encoded := new(bytes.Buffer) ciphertext := base64.NewEncoder(base64.StdEncoding, encoded) - cleartext, err := symetricallyEncrypt(ciphertext, key, signer, config) + cleartext, err := symetricallyEncrypt(ciphertext, key, signer, nil, config) if err != nil { return nil, err }