2020-01-06 04:21:44 -08:00
|
|
|
// +build !ios
|
|
|
|
|
// +build !android
|
|
|
|
|
|
|
|
|
|
package helper
|
|
|
|
|
|
2020-10-12 18:45:57 +02:00
|
|
|
import (
|
|
|
|
|
"github.com/ProtonMail/gopenpgp/v2/crypto"
|
|
|
|
|
)
|
2020-01-06 04:21:44 -08:00
|
|
|
|
|
|
|
|
// EncryptSignAttachment encrypts an attachment using a detached signature, given a publicKey, a privateKey
|
|
|
|
|
// and its passphrase, the filename, and the unencrypted file data.
|
|
|
|
|
// Returns keypacket, dataPacket and unarmored (!) signature separate.
|
|
|
|
|
func EncryptSignAttachment(
|
2020-10-12 18:45:57 +02:00
|
|
|
publicKey, privateKey string, passphrase []byte, filename string, plainData []byte,
|
2020-01-06 04:21:44 -08:00
|
|
|
) (keyPacket, dataPacket, signature []byte, err error) {
|
|
|
|
|
var publicKeyObj, privateKeyObj, unlockedKeyObj *crypto.Key
|
|
|
|
|
var publicKeyRing, privateKeyRing *crypto.KeyRing
|
|
|
|
|
var packets *crypto.PGPSplitMessage
|
|
|
|
|
var signatureObj *crypto.PGPSignature
|
|
|
|
|
|
2020-10-12 18:45:57 +02:00
|
|
|
var binMessage = crypto.NewPlainMessageFromFile(plainData, filename, uint32(crypto.GetUnixTime()))
|
2020-01-06 04:21:44 -08:00
|
|
|
|
|
|
|
|
if publicKeyObj, err = crypto.NewKeyFromArmored(publicKey); err != nil {
|
|
|
|
|
return nil, nil, nil, err
|
|
|
|
|
}
|
2020-08-04 10:04:40 +02:00
|
|
|
if publicKeyObj.IsPrivate() {
|
|
|
|
|
publicKeyObj, err = publicKeyObj.ToPublic()
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, nil, nil, err
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-01-06 04:21:44 -08:00
|
|
|
|
|
|
|
|
if publicKeyRing, err = crypto.NewKeyRing(publicKeyObj); err != nil {
|
|
|
|
|
return nil, nil, nil, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if privateKeyObj, err = crypto.NewKeyFromArmored(privateKey); err != nil {
|
|
|
|
|
return nil, nil, nil, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if unlockedKeyObj, err = privateKeyObj.Unlock(passphrase); err != nil {
|
|
|
|
|
return nil, nil, nil, err
|
|
|
|
|
}
|
2020-04-27 21:01:23 +02:00
|
|
|
defer unlockedKeyObj.ClearPrivateParams()
|
2020-01-06 04:21:44 -08:00
|
|
|
|
|
|
|
|
if privateKeyRing, err = crypto.NewKeyRing(unlockedKeyObj); err != nil {
|
|
|
|
|
return nil, nil, nil, err
|
|
|
|
|
}
|
|
|
|
|
|
2020-10-12 18:45:57 +02:00
|
|
|
if packets, err = publicKeyRing.EncryptAttachment(binMessage, ""); err != nil {
|
2020-01-06 04:21:44 -08:00
|
|
|
return nil, nil, nil, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if signatureObj, err = privateKeyRing.SignDetached(binMessage); err != nil {
|
|
|
|
|
return nil, nil, nil, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return packets.GetBinaryKeyPacket(), packets.GetBinaryDataPacket(), signatureObj.GetBinary(), nil
|
|
|
|
|
}
|
2020-09-14 09:19:33 +02:00
|
|
|
|
|
|
|
|
// EncryptSignArmoredDetached takes a public key for encryption,
|
|
|
|
|
// a private key and its passphrase for signature, and the plaintext data
|
|
|
|
|
// Returns an armored ciphertext and a detached armored signature.
|
|
|
|
|
func EncryptSignArmoredDetached(
|
|
|
|
|
publicKey, privateKey string,
|
|
|
|
|
passphrase, plainData []byte,
|
2020-10-07 15:26:36 +02:00
|
|
|
) (ciphertext, encryptedSignature string, err error) {
|
2020-09-28 18:26:31 +02:00
|
|
|
return encryptSignArmoredDetached(publicKey, privateKey, passphrase, plainData)
|
2020-09-14 09:19:33 +02:00
|
|
|
}
|