2019-06-03 17:00:01 +02:00
|
|
|
package helper
|
|
|
|
|
|
|
|
|
|
import (
|
2020-08-27 17:34:46 +02:00
|
|
|
"bytes"
|
2019-06-03 17:00:01 +02:00
|
|
|
"testing"
|
|
|
|
|
|
2019-12-27 19:35:43 +01:00
|
|
|
"github.com/ProtonMail/gopenpgp/v2/crypto"
|
2019-06-03 17:00:01 +02:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
func TestAESEncryption(t *testing.T) {
|
|
|
|
|
var plaintext = "Symmetric secret"
|
2019-12-27 19:35:43 +01:00
|
|
|
var passphrase = []byte("passphrase")
|
2019-06-03 17:00:01 +02:00
|
|
|
|
2019-12-27 19:35:43 +01:00
|
|
|
ciphertext, err := EncryptMessageWithPassword(passphrase, plaintext)
|
2019-06-03 17:00:01 +02:00
|
|
|
if err != nil {
|
|
|
|
|
t.Fatal("Expected no error when encrypting, got:", err)
|
|
|
|
|
}
|
|
|
|
|
|
2019-12-27 19:35:43 +01:00
|
|
|
_, err = DecryptMessageWithPassword([]byte("Wrong passphrase"), ciphertext)
|
2020-10-29 12:42:32 +01:00
|
|
|
assert.EqualError(t, err, "gopenpgp: unable to decrypt message with password: "+
|
|
|
|
|
"gopenpgp: error in reading password protected message: gopenpgp: wrong password in symmetric decryption")
|
2019-06-03 17:00:01 +02:00
|
|
|
|
2019-12-27 19:35:43 +01:00
|
|
|
decrypted, err := DecryptMessageWithPassword(passphrase, ciphertext)
|
2019-06-03 17:00:01 +02:00
|
|
|
if err != nil {
|
|
|
|
|
t.Fatal("Expected no error when decrypting, got:", err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
assert.Exactly(t, plaintext, decrypted)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestArmoredTextMessageEncryption(t *testing.T) {
|
|
|
|
|
var plaintext = "Secret message"
|
|
|
|
|
|
|
|
|
|
armored, err := EncryptMessageArmored(readTestFile("keyring_publicKey", false), plaintext)
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatal("Expected no error when encrypting, got:", err)
|
|
|
|
|
}
|
|
|
|
|
|
2019-10-22 18:44:45 +02:00
|
|
|
assert.Exactly(t, true, crypto.IsPGPMessage(armored))
|
2019-06-03 17:00:01 +02:00
|
|
|
|
|
|
|
|
decrypted, err := DecryptMessageArmored(
|
|
|
|
|
readTestFile("keyring_privateKey", false),
|
|
|
|
|
testMailboxPassword, // Password defined in base_test
|
|
|
|
|
armored,
|
|
|
|
|
)
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatal("Expected no error when decrypting, got:", err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
assert.Exactly(t, plaintext, decrypted)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestArmoredTextMessageEncryptionVerification(t *testing.T) {
|
|
|
|
|
var plaintext = "Secret message"
|
|
|
|
|
|
|
|
|
|
armored, err := EncryptSignMessageArmored(
|
2020-08-04 10:04:40 +02:00
|
|
|
readTestFile("keyring_privateKey", false),
|
2019-06-03 17:00:01 +02:00
|
|
|
readTestFile("keyring_privateKey", false),
|
|
|
|
|
testMailboxPassword, // Password defined in base_test
|
|
|
|
|
plaintext,
|
|
|
|
|
)
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatal("Expected no error when encrypting, got:", err)
|
|
|
|
|
}
|
|
|
|
|
|
2019-10-22 18:44:45 +02:00
|
|
|
assert.Exactly(t, true, crypto.IsPGPMessage(armored))
|
2019-06-03 17:00:01 +02:00
|
|
|
|
|
|
|
|
_, err = DecryptVerifyMessageArmored(
|
2020-08-04 10:04:40 +02:00
|
|
|
readTestFile("mime_privateKey", false), // Wrong public key
|
2019-06-03 17:00:01 +02:00
|
|
|
readTestFile("keyring_privateKey", false),
|
|
|
|
|
testMailboxPassword, // Password defined in base_test
|
|
|
|
|
armored,
|
|
|
|
|
)
|
2020-10-29 12:42:32 +01:00
|
|
|
assert.EqualError(t, err, "gopenpgp: unable to decrypt message: Signature Verification Error: No matching signature")
|
2019-06-03 17:00:01 +02:00
|
|
|
|
|
|
|
|
decrypted, err := DecryptVerifyMessageArmored(
|
2020-08-04 10:04:40 +02:00
|
|
|
readTestFile("keyring_privateKey", false),
|
2019-06-03 17:00:01 +02:00
|
|
|
readTestFile("keyring_privateKey", false),
|
|
|
|
|
testMailboxPassword, // Password defined in base_test
|
|
|
|
|
armored,
|
|
|
|
|
)
|
2019-07-02 07:36:02 -07:00
|
|
|
|
2019-06-03 17:00:01 +02:00
|
|
|
if err != nil {
|
|
|
|
|
t.Fatal("Expected no error when decrypting, got:", err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
assert.Exactly(t, plaintext, decrypted)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestAttachmentEncryptionVerification(t *testing.T) {
|
|
|
|
|
var attachment = []byte("Secret file\r\nRoot password:hunter2")
|
|
|
|
|
|
|
|
|
|
keyPacket, dataPacket, signature, err := EncryptSignAttachment(
|
2020-08-04 10:04:40 +02:00
|
|
|
readTestFile("keyring_privateKey", false),
|
2019-06-03 17:00:01 +02:00
|
|
|
readTestFile("keyring_privateKey", false),
|
|
|
|
|
testMailboxPassword, // Password defined in base_test
|
|
|
|
|
"password.txt",
|
|
|
|
|
attachment,
|
|
|
|
|
)
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatal("Expected no error when encrypting, got:", err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
sig := crypto.NewPGPSignature(signature)
|
|
|
|
|
armoredSig, err := sig.GetArmored()
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatal("Expected no error when armoring signature, got:", err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_, err = DecryptVerifyAttachment(
|
2020-08-04 10:04:40 +02:00
|
|
|
readTestFile("mime_privateKey", false), // Wrong public key
|
2019-06-03 17:00:01 +02:00
|
|
|
readTestFile("keyring_privateKey", false),
|
|
|
|
|
testMailboxPassword, // Password defined in base_test
|
|
|
|
|
keyPacket,
|
|
|
|
|
dataPacket,
|
|
|
|
|
armoredSig,
|
|
|
|
|
)
|
|
|
|
|
assert.EqualError(t, err, "gopenpgp: unable to verify attachment")
|
|
|
|
|
|
|
|
|
|
decrypted, err := DecryptVerifyAttachment(
|
2020-08-04 10:04:40 +02:00
|
|
|
readTestFile("keyring_privateKey", false),
|
2019-06-03 17:00:01 +02:00
|
|
|
readTestFile("keyring_privateKey", false),
|
|
|
|
|
testMailboxPassword, // Password defined in base_test
|
|
|
|
|
keyPacket,
|
|
|
|
|
dataPacket,
|
|
|
|
|
armoredSig,
|
|
|
|
|
)
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatal("Expected no error when decrypting, got:", err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
assert.Exactly(t, attachment, decrypted)
|
|
|
|
|
}
|
2020-07-17 15:02:39 +08:00
|
|
|
|
|
|
|
|
func TestArmoredBinaryMessageEncryption(t *testing.T) {
|
|
|
|
|
plainData := []byte("Secret message")
|
|
|
|
|
|
2020-08-04 10:04:40 +02:00
|
|
|
armored, err := EncryptBinaryMessageArmored(readTestFile("keyring_privateKey", false), plainData)
|
2020-07-17 15:02:39 +08:00
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatal("Expected no error when encrypting, got:", err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
assert.Exactly(t, true, crypto.IsPGPMessage(armored))
|
|
|
|
|
|
|
|
|
|
decrypted, err := DecryptBinaryMessageArmored(
|
|
|
|
|
readTestFile("keyring_privateKey", false),
|
|
|
|
|
testMailboxPassword, // Password defined in base_test
|
|
|
|
|
armored,
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatal("Expected no error when decrypting, got:", err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
assert.Exactly(t, plainData, decrypted)
|
|
|
|
|
}
|
2020-08-27 17:34:46 +02:00
|
|
|
|
|
|
|
|
func TestEncryptSignArmoredDetached(t *testing.T) {
|
|
|
|
|
plainData := []byte("Secret message")
|
|
|
|
|
privateKeyString := readTestFile("keyring_privateKey", false)
|
|
|
|
|
privateKey, err := crypto.NewKeyFromArmored(privateKeyString)
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatal("Error reading the test private key: ", err)
|
|
|
|
|
}
|
|
|
|
|
publicKeyString, err := privateKey.GetArmoredPublicKey()
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatal("Error reading the test public key: ", err)
|
|
|
|
|
}
|
|
|
|
|
armoredCiphertext, armoredSignature, err := EncryptSignArmoredDetached(
|
|
|
|
|
publicKeyString,
|
|
|
|
|
privateKeyString,
|
|
|
|
|
testMailboxPassword, // Password defined in base_test
|
|
|
|
|
plainData,
|
|
|
|
|
)
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatal("Expected no error while encrypting and signing, got:", err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
decrypted, err := DecryptVerifyArmoredDetached(
|
|
|
|
|
publicKeyString,
|
|
|
|
|
privateKeyString,
|
|
|
|
|
testMailboxPassword,
|
|
|
|
|
armoredCiphertext,
|
|
|
|
|
armoredSignature,
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatal("Expected no error while decrypting and verifying, got:", err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if !bytes.Equal(decrypted, plainData) {
|
|
|
|
|
t.Error("Decrypted is not equal to the plaintext")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_, modifiedSignature, err := EncryptSignArmoredDetached(
|
|
|
|
|
publicKeyString,
|
|
|
|
|
privateKeyString,
|
|
|
|
|
testMailboxPassword, // Password defined in base_test
|
|
|
|
|
[]byte("Different message"),
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatal("Expected no error while encrypting and signing, got:", err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_, err = DecryptVerifyArmoredDetached(
|
|
|
|
|
publicKeyString,
|
|
|
|
|
privateKeyString,
|
|
|
|
|
testMailboxPassword,
|
|
|
|
|
armoredCiphertext,
|
|
|
|
|
modifiedSignature,
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
if err == nil {
|
|
|
|
|
t.Fatal("Expected an error while decrypting and verifying with a wrong signature")
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-09-11 15:35:22 +02:00
|
|
|
|
|
|
|
|
func TestEncryptDecryptAttachmenWithKey(t *testing.T) {
|
|
|
|
|
plainData := []byte("Secret message")
|
|
|
|
|
privateKeyString := readTestFile("keyring_privateKey", false)
|
|
|
|
|
privateKey, err := crypto.NewKeyFromArmored(privateKeyString)
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatal("Error reading the test private key: ", err)
|
|
|
|
|
}
|
|
|
|
|
publicKeyString, err := privateKey.GetArmoredPublicKey()
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatal("Error reading the test public key: ", err)
|
|
|
|
|
}
|
|
|
|
|
pgpSplitMessage, err := EncryptAttachmentWithKey(
|
|
|
|
|
publicKeyString,
|
|
|
|
|
"test_filename",
|
|
|
|
|
plainData,
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatal("Expected no error while encrypting, got:", err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
decrypted, err := DecryptAttachmentWithKey(
|
|
|
|
|
privateKeyString,
|
|
|
|
|
testMailboxPassword,
|
|
|
|
|
pgpSplitMessage.KeyPacket,
|
|
|
|
|
pgpSplitMessage.DataPacket,
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatal("Expected no error while decrypting, got:", err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if !bytes.Equal(decrypted, plainData) {
|
|
|
|
|
t.Error("Decrypted attachment is not equal to the original attachment")
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestEncryptDecryptSessionKey(t *testing.T) {
|
|
|
|
|
privateKeyString := readTestFile("keyring_privateKey", false)
|
|
|
|
|
privateKey, err := crypto.NewKeyFromArmored(privateKeyString)
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatal("Error reading the test private key: ", err)
|
|
|
|
|
}
|
|
|
|
|
publicKeyString, err := privateKey.GetArmoredPublicKey()
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatal("Error reading the test public key: ", err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
sessionKey, err := crypto.GenerateSessionKeyAlgo("aes256")
|
|
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatal("Expected no error while generating the session key, got:", err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
encrypted, err := EncryptSessionKey(publicKeyString, sessionKey)
|
|
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatal("Expected no error while encrypting session key, got:", err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
decryptedSessionKey, err := DecryptSessionKey(
|
|
|
|
|
privateKeyString,
|
|
|
|
|
testMailboxPassword,
|
|
|
|
|
encrypted,
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatal("Expected no error while decrypting session key, got:", err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if decryptedSessionKey.GetBase64Key() != sessionKey.GetBase64Key() {
|
|
|
|
|
t.Error("Decrypted session key is not equal to the original session key")
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-10-29 06:20:39 -07:00
|
|
|
|
|
|
|
|
func TestEncryptSignBinaryDetached(t *testing.T) {
|
|
|
|
|
plainData := []byte("Secret message")
|
|
|
|
|
privateKeyString := readTestFile("keyring_privateKey", false)
|
|
|
|
|
privateKey, err := crypto.NewKeyFromArmored(privateKeyString)
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatal("Error reading the test private key: ", err)
|
|
|
|
|
}
|
|
|
|
|
publicKeyString, err := privateKey.GetArmoredPublicKey()
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatal("Error reading the test public key: ", err)
|
|
|
|
|
}
|
|
|
|
|
encryptedData, armoredSignature, err := EncryptSignBinaryDetached(
|
|
|
|
|
publicKeyString,
|
|
|
|
|
privateKeyString,
|
|
|
|
|
testMailboxPassword, // Password defined in base_test
|
|
|
|
|
plainData,
|
|
|
|
|
)
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatal("Expected no error while encrypting and signing, got:", err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
decrypted, err := DecryptVerifyBinaryDetached(
|
|
|
|
|
publicKeyString,
|
|
|
|
|
privateKeyString,
|
|
|
|
|
testMailboxPassword,
|
|
|
|
|
encryptedData,
|
|
|
|
|
armoredSignature,
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatal("Expected no error while decrypting and verifying, got:", err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if !bytes.Equal(decrypted, plainData) {
|
|
|
|
|
t.Error("Decrypted is not equal to the plaintext")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_, modifiedSignature, err := EncryptSignBinaryDetached(
|
|
|
|
|
publicKeyString,
|
|
|
|
|
privateKeyString,
|
|
|
|
|
testMailboxPassword, // Password defined in base_test
|
|
|
|
|
[]byte("Different message"),
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatal("Expected no error while encrypting and signing, got:", err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_, err = DecryptVerifyBinaryDetached(
|
|
|
|
|
publicKeyString,
|
|
|
|
|
privateKeyString,
|
|
|
|
|
testMailboxPassword,
|
|
|
|
|
encryptedData,
|
|
|
|
|
modifiedSignature,
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
if err == nil {
|
|
|
|
|
t.Fatal("Expected an error while decrypting and verifying with a wrong signature")
|
|
|
|
|
}
|
|
|
|
|
}
|