* Change keyring unlock functionalities * Add keyring#Lock, keyring#CheckIntegrity, tests * Update helpers, fix bugs * Update go.mod with ProtonMail/crypto commit * Change key management system * Clear keys from memory + tests * Create SessionKey with direct encryption for datapackets. Move symmetrickey to password. * Fix upstream dependencies * Update module to V2, documentation * Add linter * Add v2 folder to .gitignore * Minor changes to KeyID getters * Remove old changelog * Improve docs, remove compilation script
132 lines
3.6 KiB
Go
132 lines
3.6 KiB
Go
package helper
|
|
|
|
import (
|
|
"testing"
|
|
|
|
"github.com/ProtonMail/gopenpgp/v2/crypto"
|
|
"github.com/stretchr/testify/assert"
|
|
)
|
|
|
|
func TestAESEncryption(t *testing.T) {
|
|
var plaintext = "Symmetric secret"
|
|
var passphrase = []byte("passphrase")
|
|
|
|
ciphertext, err := EncryptMessageWithPassword(passphrase, plaintext)
|
|
if err != nil {
|
|
t.Fatal("Expected no error when encrypting, got:", err)
|
|
}
|
|
|
|
_, err = DecryptMessageWithPassword([]byte("Wrong passphrase"), ciphertext)
|
|
assert.EqualError(t, err, "gopenpgp: wrong password in symmetric decryption")
|
|
|
|
decrypted, err := DecryptMessageWithPassword(passphrase, ciphertext)
|
|
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)
|
|
}
|
|
|
|
assert.Exactly(t, true, crypto.IsPGPMessage(armored))
|
|
|
|
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(
|
|
readTestFile("keyring_publicKey", false),
|
|
readTestFile("keyring_privateKey", false),
|
|
testMailboxPassword, // Password defined in base_test
|
|
plaintext,
|
|
)
|
|
if err != nil {
|
|
t.Fatal("Expected no error when encrypting, got:", err)
|
|
}
|
|
|
|
assert.Exactly(t, true, crypto.IsPGPMessage(armored))
|
|
|
|
_, err = DecryptVerifyMessageArmored(
|
|
readTestFile("mime_publicKey", false), // Wrong public key
|
|
readTestFile("keyring_privateKey", false),
|
|
testMailboxPassword, // Password defined in base_test
|
|
armored,
|
|
)
|
|
assert.EqualError(t, err, "Signature Verification Error: No matching signature")
|
|
|
|
decrypted, err := DecryptVerifyMessageArmored(
|
|
readTestFile("keyring_publicKey", false),
|
|
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 TestAttachmentEncryptionVerification(t *testing.T) {
|
|
var attachment = []byte("Secret file\r\nRoot password:hunter2")
|
|
|
|
keyPacket, dataPacket, signature, err := EncryptSignAttachment(
|
|
readTestFile("keyring_publicKey", false),
|
|
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(
|
|
readTestFile("mime_publicKey", false), // Wrong public key
|
|
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(
|
|
readTestFile("keyring_publicKey", false),
|
|
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)
|
|
}
|