passforios-gopenpgp/crypto/attachment.go

148 lines
3.9 KiB
Go
Raw Normal View History

package crypto
2018-06-04 16:05:14 -07:00
import (
"bytes"
"io"
"io/ioutil"
2018-11-22 10:53:14 +01:00
"runtime"
"sync"
2018-06-04 16:05:14 -07:00
2018-11-01 17:03:43 +01:00
armorUtils "github.com/ProtonMail/go-pm-crypto/armor"
"github.com/ProtonMail/go-pm-crypto/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-13 12:33:01 +00:00
// EncryptedSplit when encrypt attachment
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-13 12:33:01 +00:00
// Process allows the attachment processor to write the encrypted attachment
2018-11-22 10:53:14 +01:00
func (ap *AttachmentProcessor) Process(plainData []byte) {
(*ap.w).Write(plainData)
}
2019-05-13 12:33:01 +00:00
// Finish attachment process
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-13 12:33:01 +00:00
// Encrypts attachment. Takes input data and key data in binary form
2018-11-22 10:53:14 +01:00
func (pm *PmCrypto) encryptAttachment(estimatedSize int, fileName string, publicKey *KeyRing, garbageCollector int) (*AttachmentProcessor, error) {
attachmentProc := &AttachmentProcessor{}
// you can also add these one at
// a time if you need to
attachmentProc.done.Add(1)
attachmentProc.garbageCollector = garbageCollector
2018-06-04 16:05:14 -07:00
hints := &openpgp.FileHints{
FileName: fileName,
}
config := &packet.Config{
DefaultCipher: packet.CipherAES256,
Time: pm.getTimeGenerator(),
}
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
}
split.Algo = "aes256"
attachmentProc.split = split
}()
var ew io.WriteCloser
var encryptErr error
ew, encryptErr = openpgp.Encrypt(writer, publicKey.entities, nil, hints, config)
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-13 12:33:01 +00:00
// EncryptAttachment encrypts attachment. Takes input data and key data in binary form
2018-11-22 10:53:14 +01:00
func (pm *PmCrypto) EncryptAttachment(plainData []byte, fileName string, publicKey *KeyRing) (*models.EncryptedSplit, error) {
ap, err := pm.encryptAttachment(len(plainData), fileName, publicKey, -1)
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-13 12:33:01 +00:00
// EncryptAttachmentLowMemory ...
2018-11-22 10:53:14 +01:00
func (pm *PmCrypto) EncryptAttachmentLowMemory(estimatedSize int, fileName string, publicKey *KeyRing) (*AttachmentProcessor, error) {
// Garbage collect every megabyte
return pm.encryptAttachment(estimatedSize, fileName, publicKey, 1<<20)
2018-06-04 16:05:14 -07:00
}
2019-05-13 12:33:01 +00:00
// SplitArmor is a Helper method. Splits armored pgp session into key and packet data
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)
}
// Decrypt attachment. Takes input data and key data in binary form. privateKeys can contains more keys. passphrase is used to unlock keys
2018-11-09 02:03:19 +01:00
func (pm *PmCrypto) DecryptAttachment(keyPacket []byte, dataPacket []byte, kr *KeyRing, passphrase string) ([]byte, error) {
privKeyEntries := kr.entities
2018-06-04 16:05:14 -07:00
kr.Unlock([]byte(passphrase))
2018-06-04 16:05:14 -07:00
keyReader := bytes.NewReader(keyPacket)
dataReader := bytes.NewReader(dataPacket)
encryptedReader := io.MultiReader(keyReader, dataReader)
2018-09-19 11:52:14 +02:00
config := &packet.Config{Time: pm.getTimeGenerator()}
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
}