2018-09-11 11:09:28 +02:00
|
|
|
package crypto
|
2018-06-04 16:05:14 -07:00
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"bytes"
|
2019-05-13 12:42:29 +00:00
|
|
|
"fmt"
|
2018-06-04 16:05:14 -07:00
|
|
|
"io"
|
|
|
|
|
"io/ioutil"
|
2018-11-22 10:53:14 +01:00
|
|
|
"runtime"
|
|
|
|
|
"sync"
|
2018-06-04 16:05:14 -07:00
|
|
|
|
2019-05-13 14:07:18 +02:00
|
|
|
armorUtils "github.com/ProtonMail/gopenpgp/armor"
|
|
|
|
|
"github.com/ProtonMail/gopenpgp/constants"
|
|
|
|
|
"github.com/ProtonMail/gopenpgp/models"
|
2018-06-04 16:05:14 -07:00
|
|
|
"golang.org/x/crypto/openpgp"
|
2018-09-19 11:52:14 +02:00
|
|
|
"golang.org/x/crypto/openpgp/packet"
|
2018-06-04 16:05:14 -07:00
|
|
|
)
|
|
|
|
|
|
2019-05-15 13:40:19 +02:00
|
|
|
// AttachmentProcessor keeps track of the progress of encrypting an attachment
|
|
|
|
|
// (optimized for encrypting large files).
|
2018-11-22 10:53:14 +01:00
|
|
|
type AttachmentProcessor struct {
|
|
|
|
|
w *io.WriteCloser
|
|
|
|
|
pipe *io.PipeWriter
|
|
|
|
|
done sync.WaitGroup
|
|
|
|
|
split *models.EncryptedSplit
|
|
|
|
|
garbageCollector int
|
|
|
|
|
err error
|
|
|
|
|
}
|
|
|
|
|
|
2019-05-15 13:40:19 +02:00
|
|
|
// Process writes attachment data to be encrypted
|
2018-11-22 10:53:14 +01:00
|
|
|
func (ap *AttachmentProcessor) Process(plainData []byte) {
|
2019-05-14 14:42:38 +00:00
|
|
|
if _, err := (*ap.w).Write(plainData); err != nil {
|
|
|
|
|
panic(err)
|
|
|
|
|
}
|
2018-11-22 10:53:14 +01:00
|
|
|
}
|
|
|
|
|
|
2019-05-15 13:40:19 +02:00
|
|
|
// Finish closes the attachment and returns the encrypted data
|
2018-11-22 10:53:14 +01:00
|
|
|
func (ap *AttachmentProcessor) Finish() (*models.EncryptedSplit, error) {
|
|
|
|
|
if ap.err != nil {
|
|
|
|
|
return nil, ap.err
|
|
|
|
|
}
|
|
|
|
|
(*ap.w).Close()
|
|
|
|
|
(*ap.pipe).Close()
|
|
|
|
|
ap.done.Wait()
|
|
|
|
|
if ap.garbageCollector > 0 {
|
|
|
|
|
runtime.GC()
|
2018-06-04 16:05:14 -07:00
|
|
|
}
|
2018-11-22 10:53:14 +01:00
|
|
|
return ap.split, nil
|
|
|
|
|
}
|
|
|
|
|
|
2019-05-15 13:40:19 +02:00
|
|
|
// encryptAttachment creates an AttachmentProcessor which can be used to encrypt
|
|
|
|
|
// a file. It takes an estimatedSize and fileName as hints about the file.
|
2019-05-14 18:05:01 +02:00
|
|
|
func (pgp *GopenPGP) encryptAttachment(
|
2019-05-14 14:42:38 +00:00
|
|
|
estimatedSize int, fileName string, publicKey *KeyRing, garbageCollector int,
|
|
|
|
|
) (*AttachmentProcessor, error) {
|
2018-11-22 10:53:14 +01:00
|
|
|
attachmentProc := &AttachmentProcessor{}
|
2019-05-15 13:40:19 +02:00
|
|
|
// You could also add these one at a time if needed.
|
2018-11-22 10:53:14 +01:00
|
|
|
attachmentProc.done.Add(1)
|
|
|
|
|
attachmentProc.garbageCollector = garbageCollector
|
2018-06-04 16:05:14 -07:00
|
|
|
|
|
|
|
|
hints := &openpgp.FileHints{
|
|
|
|
|
FileName: fileName,
|
|
|
|
|
}
|
2018-06-22 16:04:20 +02:00
|
|
|
|
|
|
|
|
config := &packet.Config{
|
|
|
|
|
DefaultCipher: packet.CipherAES256,
|
2019-05-14 18:05:01 +02:00
|
|
|
Time: pgp.getTimeGenerator(),
|
2018-06-22 16:04:20 +02:00
|
|
|
}
|
2018-06-04 16:05:14 -07:00
|
|
|
|
2018-11-22 10:53:14 +01:00
|
|
|
reader, writer := io.Pipe()
|
|
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
|
defer attachmentProc.done.Done()
|
|
|
|
|
split, splitError := SeparateKeyAndData(nil, reader, estimatedSize, garbageCollector)
|
|
|
|
|
if attachmentProc.err != nil {
|
|
|
|
|
attachmentProc.err = splitError
|
|
|
|
|
}
|
2019-05-14 14:42:38 +00:00
|
|
|
split.Algo = constants.AES256
|
2018-11-22 10:53:14 +01:00
|
|
|
attachmentProc.split = split
|
|
|
|
|
}()
|
|
|
|
|
|
|
|
|
|
var ew io.WriteCloser
|
|
|
|
|
var encryptErr error
|
|
|
|
|
ew, encryptErr = openpgp.Encrypt(writer, publicKey.entities, nil, hints, config)
|
2019-04-27 07:31:29 +02:00
|
|
|
if encryptErr != nil {
|
|
|
|
|
return nil, encryptErr
|
|
|
|
|
}
|
2018-11-22 10:53:14 +01:00
|
|
|
attachmentProc.w = &ew
|
|
|
|
|
attachmentProc.pipe = writer
|
|
|
|
|
|
|
|
|
|
return attachmentProc, nil
|
|
|
|
|
}
|
2018-06-04 16:05:14 -07:00
|
|
|
|
2019-05-15 13:40:19 +02:00
|
|
|
// EncryptAttachment encrypts a file. fileName
|
2019-05-14 18:05:01 +02:00
|
|
|
func (pgp *GopenPGP) EncryptAttachment(
|
2019-05-14 14:42:38 +00:00
|
|
|
plainData []byte, fileName string, publicKey *KeyRing,
|
|
|
|
|
) (*models.EncryptedSplit, error) {
|
2019-05-14 18:05:01 +02:00
|
|
|
ap, err := pgp.encryptAttachment(len(plainData), fileName, publicKey, -1)
|
2018-11-22 10:53:14 +01:00
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
ap.Process(plainData)
|
|
|
|
|
split, err := ap.Finish()
|
2018-06-04 16:05:14 -07:00
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
2018-06-04 17:50:26 -07:00
|
|
|
return split, nil
|
2018-11-22 10:53:14 +01:00
|
|
|
}
|
|
|
|
|
|
2019-05-15 13:40:19 +02:00
|
|
|
// EncryptAttachmentLowMemory creates an AttachmentProcessor which can be used
|
|
|
|
|
// to encrypt a file. It takes an estimatedSize and fileName as hints about the
|
|
|
|
|
// file. It is optimized for low-memory environments and collects garbage every
|
|
|
|
|
// megabyte.
|
2019-05-14 18:05:01 +02:00
|
|
|
func (pgp *GopenPGP) EncryptAttachmentLowMemory(
|
2019-05-14 14:42:38 +00:00
|
|
|
estimatedSize int, fileName string, publicKey *KeyRing,
|
|
|
|
|
) (*AttachmentProcessor, error) {
|
2019-05-14 18:05:01 +02:00
|
|
|
return pgp.encryptAttachment(estimatedSize, fileName, publicKey, 1<<20)
|
2018-06-04 16:05:14 -07:00
|
|
|
}
|
|
|
|
|
|
2019-05-15 13:40:19 +02:00
|
|
|
// SplitArmor is a helper method which splits an armored message into its
|
|
|
|
|
// session key packet and symmetrically encrypted data packet.
|
2018-11-05 22:55:45 +01:00
|
|
|
func SplitArmor(encrypted string) (*models.EncryptedSplit, error) {
|
|
|
|
|
var err error
|
|
|
|
|
|
|
|
|
|
encryptedRaw, err := armorUtils.Unarmor(encrypted)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
encryptedReader := bytes.NewReader(encryptedRaw)
|
|
|
|
|
|
2018-11-22 10:53:14 +01:00
|
|
|
return SeparateKeyAndData(nil, encryptedReader, len(encrypted), -1)
|
2018-11-05 22:55:45 +01:00
|
|
|
}
|
|
|
|
|
|
2019-05-15 13:40:19 +02:00
|
|
|
// DecryptAttachment takes a session key packet and symmetrically encrypted data
|
|
|
|
|
// packet. privateKeys is a KeyRing that can contain multiple keys. The
|
|
|
|
|
// passphrase is used to unlock keys in privateKeys.
|
2019-05-14 18:05:01 +02:00
|
|
|
func (pgp *GopenPGP) DecryptAttachment(
|
2019-05-14 14:42:38 +00:00
|
|
|
keyPacket, dataPacket []byte,
|
|
|
|
|
kr *KeyRing, passphrase string,
|
|
|
|
|
) ([]byte, error) {
|
2018-11-09 02:03:19 +01:00
|
|
|
privKeyEntries := kr.entities
|
2018-06-04 16:05:14 -07:00
|
|
|
|
2019-05-13 12:42:29 +00:00
|
|
|
if err := kr.Unlock([]byte(passphrase)); err != nil {
|
2019-05-15 13:40:19 +02:00
|
|
|
err = fmt.Errorf("gopenpgp: cannot decrypt attachment: %v", err)
|
2019-05-13 12:42:29 +00:00
|
|
|
return nil, err
|
|
|
|
|
}
|
2018-06-04 16:05:14 -07:00
|
|
|
|
|
|
|
|
keyReader := bytes.NewReader(keyPacket)
|
|
|
|
|
dataReader := bytes.NewReader(dataPacket)
|
|
|
|
|
|
|
|
|
|
encryptedReader := io.MultiReader(keyReader, dataReader)
|
|
|
|
|
|
2019-05-14 18:05:01 +02:00
|
|
|
config := &packet.Config{Time: pgp.getTimeGenerator()}
|
2018-06-22 16:04:20 +02:00
|
|
|
|
|
|
|
|
md, err := openpgp.ReadMessage(encryptedReader, privKeyEntries, nil, config)
|
2018-06-04 16:05:14 -07:00
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
decrypted := md.UnverifiedBody
|
|
|
|
|
b, err := ioutil.ReadAll(decrypted)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return b, nil
|
|
|
|
|
}
|