2020-01-06 04:21:44 -08:00
|
|
|
package helper
|
|
|
|
|
|
|
|
|
|
import (
|
2020-04-07 14:59:25 +02:00
|
|
|
"encoding/json"
|
2020-10-29 12:42:32 +01:00
|
|
|
goerrors "errors"
|
2020-12-08 17:52:50 +01:00
|
|
|
"runtime/debug"
|
2020-04-07 14:59:25 +02:00
|
|
|
|
2020-01-06 04:21:44 -08:00
|
|
|
"github.com/ProtonMail/gopenpgp/v2/crypto"
|
2020-10-29 12:42:32 +01:00
|
|
|
"github.com/pkg/errors"
|
2020-01-06 04:21:44 -08:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
type ExplicitVerifyMessage struct {
|
|
|
|
|
Message *crypto.PlainMessage
|
|
|
|
|
SignatureVerificationError *crypto.SignatureVerificationError
|
|
|
|
|
}
|
|
|
|
|
|
2020-04-28 13:55:36 +02:00
|
|
|
// DecryptExplicitVerify decrypts an armored PGP message given a private key
|
|
|
|
|
// and its passphrase and verifies the embedded signature. Returns the plain
|
|
|
|
|
// data or an error on signature verification failure.
|
2020-01-06 04:21:44 -08:00
|
|
|
func DecryptExplicitVerify(
|
|
|
|
|
pgpMessage *crypto.PGPMessage,
|
|
|
|
|
privateKeyRing, publicKeyRing *crypto.KeyRing,
|
|
|
|
|
verifyTime int64,
|
|
|
|
|
) (*ExplicitVerifyMessage, error) {
|
|
|
|
|
var explicitVerify *ExplicitVerifyMessage
|
|
|
|
|
|
|
|
|
|
message, err := privateKeyRing.Decrypt(pgpMessage, publicKeyRing, verifyTime)
|
|
|
|
|
|
|
|
|
|
if err != nil {
|
2020-10-29 12:42:32 +01:00
|
|
|
castedErr := &crypto.SignatureVerificationError{}
|
|
|
|
|
isType := goerrors.As(err, castedErr)
|
2020-01-06 04:21:44 -08:00
|
|
|
if !isType {
|
2020-10-29 12:42:32 +01:00
|
|
|
return nil, errors.Wrap(err, "gopenpgp: unable to decrypt message")
|
2020-01-06 04:21:44 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
explicitVerify = &ExplicitVerifyMessage{
|
|
|
|
|
Message: message,
|
2020-10-29 12:42:32 +01:00
|
|
|
SignatureVerificationError: castedErr,
|
2020-01-06 04:21:44 -08:00
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
explicitVerify = &ExplicitVerifyMessage{
|
|
|
|
|
Message: message,
|
|
|
|
|
SignatureVerificationError: nil,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return explicitVerify, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// DecryptAttachment takes a keypacket and datpacket
|
|
|
|
|
// and returns a decrypted PlainMessage
|
|
|
|
|
// Specifically designed for attachments rather than text messages.
|
|
|
|
|
func DecryptAttachment(keyPacket []byte, dataPacket []byte, keyRing *crypto.KeyRing) (*crypto.PlainMessage, error) {
|
|
|
|
|
splitMessage := crypto.NewPGPSplitMessage(keyPacket, dataPacket)
|
|
|
|
|
|
|
|
|
|
decrypted, err := keyRing.DecryptAttachment(splitMessage)
|
|
|
|
|
if err != nil {
|
2020-10-29 12:42:32 +01:00
|
|
|
return nil, errors.Wrap(err, "gopenpgp: unable to decrypt attachment")
|
2020-01-06 04:21:44 -08:00
|
|
|
}
|
|
|
|
|
return decrypted, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// EncryptAttachment encrypts a file given a plainData and a fileName.
|
2020-04-28 13:55:36 +02:00
|
|
|
// Returns a PGPSplitMessage containing a session key packet and symmetrically
|
|
|
|
|
// encrypted data. Specifically designed for attachments rather than text
|
|
|
|
|
// messages.
|
2020-10-12 18:45:57 +02:00
|
|
|
func EncryptAttachment(plainData []byte, filename string, keyRing *crypto.KeyRing) (*crypto.PGPSplitMessage, error) {
|
|
|
|
|
plainMessage := crypto.NewPlainMessageFromFile(plainData, filename, uint32(crypto.GetUnixTime()))
|
|
|
|
|
decrypted, err := keyRing.EncryptAttachment(plainMessage, "")
|
2020-01-06 04:21:44 -08:00
|
|
|
if err != nil {
|
2020-10-29 12:42:32 +01:00
|
|
|
return nil, errors.Wrap(err, "gopenpgp: unable to encrypt attachment")
|
2020-01-06 04:21:44 -08:00
|
|
|
}
|
|
|
|
|
return decrypted, nil
|
|
|
|
|
}
|
2020-04-07 14:59:25 +02:00
|
|
|
|
2020-04-28 13:55:36 +02:00
|
|
|
// GetJsonSHA256Fingerprints returns the SHA256 fingeprints of key and subkeys,
|
|
|
|
|
// encoded in JSON, since gomobile can not handle arrays.
|
2020-04-07 14:59:25 +02:00
|
|
|
func GetJsonSHA256Fingerprints(publicKey string) ([]byte, error) {
|
|
|
|
|
key, err := crypto.NewKeyFromArmored(publicKey)
|
|
|
|
|
if err != nil {
|
2020-10-29 12:42:32 +01:00
|
|
|
return nil, errors.Wrap(err, "gopenpgp: unable to parse key")
|
2020-04-07 14:59:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return json.Marshal(key.GetSHA256Fingerprints())
|
|
|
|
|
}
|
2020-08-27 17:34:46 +02:00
|
|
|
|
|
|
|
|
type EncryptSignArmoredDetachedMobileResult struct {
|
2020-10-29 06:20:39 -07:00
|
|
|
CiphertextArmored, EncryptedSignatureArmored string
|
2020-08-27 17:34:46 +02:00
|
|
|
}
|
|
|
|
|
|
2020-10-29 06:20:39 -07:00
|
|
|
// EncryptSignArmoredDetachedMobile wraps the encryptSignArmoredDetached method
|
2020-09-11 15:35:22 +02:00
|
|
|
// to have only one return argument for mobile.
|
2020-08-27 17:34:46 +02:00
|
|
|
func EncryptSignArmoredDetachedMobile(
|
|
|
|
|
publicKey, privateKey string,
|
|
|
|
|
passphrase, plainData []byte,
|
|
|
|
|
) (wrappedTuple *EncryptSignArmoredDetachedMobileResult, err error) {
|
2020-10-07 15:26:36 +02:00
|
|
|
ciphertext, encryptedSignature, err := encryptSignArmoredDetached(publicKey, privateKey, passphrase, plainData)
|
2020-08-27 17:34:46 +02:00
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
2020-10-29 12:42:32 +01:00
|
|
|
|
|
|
|
|
return &EncryptSignArmoredDetachedMobileResult{
|
2020-10-29 06:20:39 -07:00
|
|
|
CiphertextArmored: ciphertext,
|
|
|
|
|
EncryptedSignatureArmored: encryptedSignature,
|
|
|
|
|
}, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type EncryptSignBinaryDetachedMobileResult struct {
|
|
|
|
|
EncryptedData []byte
|
|
|
|
|
EncryptedSignatureArmored string
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// EncryptSignBinaryDetachedMobile wraps the encryptSignBinaryDetached method
|
|
|
|
|
// to have only one return argument for mobile.
|
|
|
|
|
func EncryptSignBinaryDetachedMobile(
|
|
|
|
|
publicKey, privateKey string,
|
|
|
|
|
passphrase, plainData []byte,
|
|
|
|
|
) (wrappedTuple *EncryptSignBinaryDetachedMobileResult, err error) {
|
|
|
|
|
ciphertext, encryptedSignature, err := encryptSignBinaryDetached(publicKey, privateKey, passphrase, plainData)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
return &EncryptSignBinaryDetachedMobileResult{
|
|
|
|
|
EncryptedData: ciphertext,
|
|
|
|
|
EncryptedSignatureArmored: encryptedSignature,
|
2020-10-29 12:42:32 +01:00
|
|
|
}, nil
|
2020-08-27 17:34:46 +02:00
|
|
|
}
|
2020-12-08 17:52:50 +01:00
|
|
|
|
|
|
|
|
// FreeOSMemory can be used to explicitly
|
|
|
|
|
// call the garbage collector and
|
|
|
|
|
// return the unused memory to the OS.
|
|
|
|
|
func FreeOSMemory() {
|
|
|
|
|
debug.FreeOSMemory()
|
|
|
|
|
}
|