2019-05-14 15:39:35 +02:00
|
|
|
package crypto
|
|
|
|
|
|
|
|
|
|
import (
|
2019-08-29 17:45:13 +02:00
|
|
|
"bytes"
|
2019-06-03 17:00:01 +02:00
|
|
|
"encoding/base64"
|
2019-08-29 17:45:13 +02:00
|
|
|
"io"
|
2019-05-14 15:39:35 +02:00
|
|
|
"strings"
|
|
|
|
|
"testing"
|
2019-06-03 17:00:01 +02:00
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
2019-08-29 17:45:13 +02:00
|
|
|
"golang.org/x/crypto/openpgp/packet"
|
2019-05-14 15:39:35 +02:00
|
|
|
)
|
|
|
|
|
|
2019-06-03 17:00:01 +02:00
|
|
|
func TestTextMessageEncryptionWithSymmetricKey(t *testing.T) {
|
|
|
|
|
var message = NewPlainMessageFromString("The secret code is... 1, 2, 3, 4, 5")
|
2019-05-14 15:39:35 +02:00
|
|
|
|
2019-06-03 17:00:01 +02:00
|
|
|
// Encrypt data with password
|
|
|
|
|
encrypted, err := testSymmetricKey.Encrypt(message)
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatal("Expected no error when encrypting, got:", err)
|
|
|
|
|
}
|
|
|
|
|
// Decrypt data with wrong password
|
|
|
|
|
_, err = testWrongSymmetricKey.Decrypt(encrypted)
|
|
|
|
|
assert.NotNil(t, err)
|
|
|
|
|
|
|
|
|
|
// Decrypt data with the good password
|
|
|
|
|
decrypted, err := testSymmetricKey.Decrypt(encrypted)
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatal("Expected no error when decrypting, got:", err)
|
|
|
|
|
}
|
|
|
|
|
assert.Exactly(t, message.GetString(), decrypted.GetString())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestBinaryMessageEncryptionWithSymmetricKey(t *testing.T) {
|
|
|
|
|
binData, _ := base64.StdEncoding.DecodeString("ExXmnSiQ2QCey20YLH6qlLhkY3xnIBC1AwlIXwK/HvY=")
|
|
|
|
|
var message = NewPlainMessage(binData)
|
2019-05-14 15:39:35 +02:00
|
|
|
|
|
|
|
|
// Encrypt data with password
|
2019-06-03 17:00:01 +02:00
|
|
|
encrypted, err := testSymmetricKey.Encrypt(message)
|
2019-05-14 15:39:35 +02:00
|
|
|
if err != nil {
|
|
|
|
|
t.Fatal("Expected no error when encrypting, got:", err)
|
|
|
|
|
}
|
|
|
|
|
// Decrypt data with wrong password
|
2019-06-03 17:00:01 +02:00
|
|
|
_, err = testWrongSymmetricKey.Decrypt(encrypted)
|
2019-05-14 15:39:35 +02:00
|
|
|
assert.NotNil(t, err)
|
2019-06-03 17:00:01 +02:00
|
|
|
|
2019-05-14 15:39:35 +02:00
|
|
|
// Decrypt data with the good password
|
2019-06-03 17:00:01 +02:00
|
|
|
decrypted, err := testSymmetricKey.Decrypt(encrypted)
|
2019-05-14 15:39:35 +02:00
|
|
|
if err != nil {
|
|
|
|
|
t.Fatal("Expected no error when decrypting, got:", err)
|
|
|
|
|
}
|
2019-06-03 17:00:01 +02:00
|
|
|
assert.Exactly(t, message, decrypted)
|
2019-05-14 15:39:35 +02:00
|
|
|
}
|
|
|
|
|
|
2019-06-03 17:00:01 +02:00
|
|
|
func TestTextMessageEncryption(t *testing.T) {
|
|
|
|
|
var message = NewPlainMessageFromString("plain text")
|
2019-05-14 15:39:35 +02:00
|
|
|
|
2019-10-22 18:44:45 +02:00
|
|
|
testPublicKeyRing, _ = BuildKeyRingArmored(readTestFile("keyring_publicKey", false))
|
|
|
|
|
testPrivateKeyRing, err = BuildKeyRingArmored(readTestFile("keyring_privateKey", false))
|
2019-06-03 17:00:01 +02:00
|
|
|
|
|
|
|
|
// Password defined in keyring_test
|
|
|
|
|
err = testPrivateKeyRing.UnlockWithPassphrase(testMailboxPassword)
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatal("Expected no error unlocking privateKey, got:", err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ciphertext, err := testPublicKeyRing.Encrypt(message, testPrivateKeyRing)
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatal("Expected no error when encrypting, got:", err)
|
|
|
|
|
}
|
|
|
|
|
|
2019-10-22 18:44:45 +02:00
|
|
|
decrypted, err := testPrivateKeyRing.Decrypt(ciphertext, testPublicKeyRing, GetUnixTime())
|
2019-06-03 17:00:01 +02:00
|
|
|
if err != nil {
|
|
|
|
|
t.Fatal("Expected no error when decrypting, got:", err)
|
|
|
|
|
}
|
|
|
|
|
assert.Exactly(t, message.GetString(), decrypted.GetString())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestBinaryMessageEncryption(t *testing.T) {
|
|
|
|
|
binData, _ := base64.StdEncoding.DecodeString("ExXmnSiQ2QCey20YLH6qlLhkY3xnIBC1AwlIXwK/HvY=")
|
|
|
|
|
var message = NewPlainMessage(binData)
|
|
|
|
|
|
2019-10-22 18:44:45 +02:00
|
|
|
testPublicKeyRing, _ = BuildKeyRingArmored(readTestFile("keyring_publicKey", false))
|
|
|
|
|
testPrivateKeyRing, err = BuildKeyRingArmored(readTestFile("keyring_privateKey", false))
|
2019-05-14 15:39:35 +02:00
|
|
|
|
2019-06-03 17:00:01 +02:00
|
|
|
// Password defined in keyring_test
|
|
|
|
|
err = testPrivateKeyRing.UnlockWithPassphrase(testMailboxPassword)
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatal("Expected no error unlocking privateKey, got:", err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ciphertext, err := testPublicKeyRing.Encrypt(message, testPrivateKeyRing)
|
2019-05-14 15:39:35 +02:00
|
|
|
if err != nil {
|
|
|
|
|
t.Fatal("Expected no error when encrypting, got:", err)
|
|
|
|
|
}
|
2019-06-03 17:00:01 +02:00
|
|
|
|
2019-10-22 18:44:45 +02:00
|
|
|
decrypted, err := testPrivateKeyRing.Decrypt(ciphertext, testPublicKeyRing, GetUnixTime())
|
2019-05-14 15:39:35 +02:00
|
|
|
if err != nil {
|
|
|
|
|
t.Fatal("Expected no error when decrypting, got:", err)
|
|
|
|
|
}
|
2019-06-03 17:00:01 +02:00
|
|
|
assert.Exactly(t, message.GetBinary(), decrypted.GetBinary())
|
2019-08-29 17:45:13 +02:00
|
|
|
|
|
|
|
|
// Decrypt without verifying
|
|
|
|
|
decrypted, err = testPrivateKeyRing.Decrypt(ciphertext, nil, 0)
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatal("Expected no error when decrypting, got:", err)
|
|
|
|
|
}
|
|
|
|
|
assert.Exactly(t, message.GetString(), decrypted.GetString())
|
2019-05-14 15:39:35 +02:00
|
|
|
}
|
2019-06-04 18:10:31 +02:00
|
|
|
|
|
|
|
|
func TestIssue11(t *testing.T) {
|
2019-10-22 18:44:45 +02:00
|
|
|
myKeyring, err := BuildKeyRingArmored(readTestFile("issue11_privatekey", false))
|
2019-06-04 18:10:31 +02:00
|
|
|
if err != nil {
|
|
|
|
|
t.Fatal("Expected no error while bulding private keyring, got:", err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
err = myKeyring.UnlockWithPassphrase("1234");
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatal("Expected no error while unlocking private keyring, got:", err)
|
|
|
|
|
}
|
|
|
|
|
|
2019-10-22 18:44:45 +02:00
|
|
|
senderKeyring, err := BuildKeyRingArmored(readTestFile("issue11_publickey", false))
|
2019-06-04 18:10:31 +02:00
|
|
|
if err != nil {
|
|
|
|
|
t.Fatal("Expected no error while building public keyring, got:", err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
assert.Exactly(t, []uint64{0x643b3595e6ee4fdf}, senderKeyring.KeyIds())
|
|
|
|
|
|
|
|
|
|
pgpMessage, err := NewPGPMessageFromArmored(readTestFile("issue11_message", false))
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatal("Expected no error while unlocking private keyring, got:", err)
|
|
|
|
|
}
|
|
|
|
|
|
2019-07-02 07:36:02 -07:00
|
|
|
plainMessage, err := myKeyring.Decrypt(pgpMessage, senderKeyring, 0)
|
2019-06-04 18:10:31 +02:00
|
|
|
if err != nil {
|
|
|
|
|
t.Fatal("Expected no error while decrypting/verifying, got:", err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
assert.Exactly(t, "message from sender", plainMessage.GetString())
|
|
|
|
|
}
|
2019-08-29 17:45:13 +02:00
|
|
|
|
|
|
|
|
func TestSignedMessageDecryption(t *testing.T) {
|
2019-10-22 18:44:45 +02:00
|
|
|
testPrivateKeyRing, err = BuildKeyRingArmored(readTestFile("keyring_privateKey", false))
|
2019-08-29 17:45:13 +02:00
|
|
|
|
|
|
|
|
// Password defined in keyring_test
|
|
|
|
|
err = testPrivateKeyRing.UnlockWithPassphrase(testMailboxPassword)
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatal("Expected no error unlocking privateKey, got:", err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pgpMessage, err := NewPGPMessageFromArmored(readTestFile("message_signed", false))
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatal("Expected no error when unarmoring, got:", err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
decrypted, err := testPrivateKeyRing.Decrypt(pgpMessage, nil, 0)
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatal("Expected no error when decrypting, got:", err)
|
|
|
|
|
}
|
|
|
|
|
assert.Exactly(t, readTestFile("message_plaintext", true), decrypted.GetString())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestMultipleKeyMessageEncryption(t *testing.T) {
|
|
|
|
|
var message = NewPlainMessageFromString("plain text")
|
|
|
|
|
|
2019-10-22 18:44:45 +02:00
|
|
|
testPublicKeyRing, _ = BuildKeyRingArmored(readTestFile("keyring_publicKey", false))
|
2019-09-03 17:42:10 +02:00
|
|
|
err = testPublicKeyRing.ReadFrom(strings.NewReader(readTestFile("mime_publicKey", false)), true)
|
2019-08-29 17:45:13 +02:00
|
|
|
if err != nil {
|
|
|
|
|
t.Fatal("Expected no error adding second public key, got:", err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
assert.Exactly(t, 2, len(testPublicKeyRing.entities))
|
|
|
|
|
|
2019-10-22 18:44:45 +02:00
|
|
|
testPrivateKeyRing, err = BuildKeyRingArmored(readTestFile("keyring_privateKey", false))
|
2019-08-29 17:45:13 +02:00
|
|
|
|
|
|
|
|
// Password defined in keyring_test
|
|
|
|
|
err = testPrivateKeyRing.UnlockWithPassphrase(testMailboxPassword)
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatal("Expected no error unlocking privateKey, got:", err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ciphertext, err := testPublicKeyRing.Encrypt(message, testPrivateKeyRing)
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatal("Expected no error when encrypting, got:", err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
numKeyPackets := 0
|
|
|
|
|
packets := packet.NewReader(bytes.NewReader(ciphertext.Data))
|
|
|
|
|
for {
|
|
|
|
|
var p packet.Packet
|
|
|
|
|
if p, err = packets.Next(); err == io.EOF {
|
|
|
|
|
err = nil
|
|
|
|
|
break
|
|
|
|
|
}
|
|
|
|
|
switch p.(type) {
|
|
|
|
|
case *packet.EncryptedKey:
|
|
|
|
|
numKeyPackets++
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
assert.Exactly(t, 2, numKeyPackets)
|
|
|
|
|
|
2019-10-22 18:44:45 +02:00
|
|
|
decrypted, err := testPrivateKeyRing.Decrypt(ciphertext, testPublicKeyRing, GetUnixTime())
|
2019-08-29 17:45:13 +02:00
|
|
|
if err != nil {
|
|
|
|
|
t.Fatal("Expected no error when decrypting, got:", err)
|
|
|
|
|
}
|
|
|
|
|
assert.Exactly(t, message.GetString(), decrypted.GetString())
|
|
|
|
|
}
|