2018-11-05 23:01:53 +01:00
|
|
|
package crypto
|
|
|
|
|
|
|
|
|
|
import (
|
2019-05-08 13:04:22 +02:00
|
|
|
"encoding/base64"
|
2018-11-05 23:01:53 +01:00
|
|
|
"io/ioutil"
|
|
|
|
|
"strings"
|
|
|
|
|
"testing"
|
2019-05-14 14:42:38 +00:00
|
|
|
|
|
|
|
|
"golang.org/x/crypto/openpgp/armor"
|
|
|
|
|
|
2019-05-13 14:07:18 +02:00
|
|
|
"github.com/ProtonMail/gopenpgp/constants"
|
2019-05-14 14:42:38 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
2018-11-05 23:01:53 +01:00
|
|
|
)
|
|
|
|
|
|
2019-05-14 14:42:38 +00:00
|
|
|
var decodedSymmetricKey, _ = base64.StdEncoding.DecodeString("ExXmnSiQ2QCey20YLH6qlLhkY3xnIBC1AwlIXwK/HvY=")
|
2019-05-08 13:04:22 +02:00
|
|
|
|
2019-01-11 00:23:00 +01:00
|
|
|
var testSymmetricKey = &SymmetricKey{
|
2019-05-08 13:04:22 +02:00
|
|
|
Key: decodedSymmetricKey,
|
2019-05-14 14:42:38 +00:00
|
|
|
Algo: constants.AES256,
|
2019-01-11 00:23:00 +01:00
|
|
|
}
|
|
|
|
|
|
2019-06-03 17:00:01 +02:00
|
|
|
var testWrongSymmetricKey = &SymmetricKey{
|
|
|
|
|
Key: []byte("WrongPass"),
|
|
|
|
|
Algo: constants.AES256,
|
|
|
|
|
}
|
|
|
|
|
|
2019-05-08 13:04:22 +02:00
|
|
|
// Corresponding key in testdata/keyring_privateKey
|
2018-11-05 23:01:53 +01:00
|
|
|
const testMailboxPassword = "apple"
|
2019-05-08 13:04:22 +02:00
|
|
|
|
|
|
|
|
// Corresponding key in testdata/keyring_privateKeyLegacy
|
2019-05-14 14:42:38 +00:00
|
|
|
// const testMailboxPasswordLegacy = "123"
|
2018-11-05 23:01:53 +01:00
|
|
|
|
|
|
|
|
var (
|
|
|
|
|
testPrivateKeyRing *KeyRing
|
|
|
|
|
testPublicKeyRing *KeyRing
|
|
|
|
|
)
|
|
|
|
|
|
2019-05-15 13:48:47 +02:00
|
|
|
var testIdentity = &Identity{
|
|
|
|
|
Name: "UserID",
|
|
|
|
|
Email: "",
|
|
|
|
|
}
|
2018-11-05 23:01:53 +01:00
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
|
var err error
|
2019-05-14 16:08:25 +00:00
|
|
|
|
|
|
|
|
testPrivateKeyRing, err = ReadArmoredKeyRing(strings.NewReader(readTestFile("keyring_privateKey", false)))
|
2019-05-14 14:42:38 +00:00
|
|
|
if err != nil {
|
2018-11-05 23:01:53 +01:00
|
|
|
panic(err)
|
|
|
|
|
}
|
|
|
|
|
|
2019-05-14 16:08:25 +00:00
|
|
|
testPublicKeyRing, err = ReadArmoredKeyRing(strings.NewReader(readTestFile("keyring_publicKey", false)))
|
2019-05-14 14:42:38 +00:00
|
|
|
if err != nil {
|
2018-11-05 23:01:53 +01:00
|
|
|
panic(err)
|
|
|
|
|
}
|
|
|
|
|
|
2019-06-03 17:00:01 +02:00
|
|
|
err = testPrivateKeyRing.UnlockWithPassphrase(testMailboxPassword)
|
2019-05-14 14:42:38 +00:00
|
|
|
if err != nil {
|
2018-11-05 23:01:53 +01:00
|
|
|
panic(err)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestKeyRing_ArmoredPublicKeyString(t *testing.T) {
|
2019-05-14 08:07:49 +00:00
|
|
|
s, err := testPrivateKeyRing.GetArmoredPublicKey()
|
2018-11-05 23:01:53 +01:00
|
|
|
if err != nil {
|
|
|
|
|
t.Fatal("Expected no error while getting armored public key, got:", err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Decode armored keys
|
|
|
|
|
block, err := armor.Decode(strings.NewReader(s))
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatal("Expected no error while decoding armored public key, got:", err)
|
|
|
|
|
}
|
2019-05-08 13:04:22 +02:00
|
|
|
|
2019-05-14 16:08:25 +00:00
|
|
|
expected, err := armor.Decode(strings.NewReader(readTestFile("keyring_publicKey", false)))
|
2018-11-05 23:01:53 +01:00
|
|
|
if err != nil {
|
|
|
|
|
t.Fatal("Expected no error while decoding expected armored public key, got:", err)
|
|
|
|
|
}
|
|
|
|
|
|
2019-05-08 13:04:22 +02:00
|
|
|
assert.Exactly(t, expected.Type, block.Type)
|
2018-11-05 23:01:53 +01:00
|
|
|
|
|
|
|
|
b, err := ioutil.ReadAll(block.Body)
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatal("Expected no error while reading armored public key body, got:", err)
|
|
|
|
|
}
|
2019-05-08 13:04:22 +02:00
|
|
|
|
2018-11-05 23:01:53 +01:00
|
|
|
eb, err := ioutil.ReadAll(expected.Body)
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatal("Expected no error while reading expected armored public key body, got:", err)
|
|
|
|
|
}
|
|
|
|
|
|
2019-05-08 13:04:22 +02:00
|
|
|
assert.Exactly(t, eb, b)
|
2018-11-05 23:01:53 +01:00
|
|
|
}
|
2019-05-15 13:48:47 +02:00
|
|
|
|
|
|
|
|
func TestCheckPassphrase(t *testing.T) {
|
|
|
|
|
encryptedKeyRing, _ := ReadArmoredKeyRing(strings.NewReader(readTestFile("keyring_privateKey", false)))
|
2019-05-15 13:40:19 +02:00
|
|
|
isCorrect := encryptedKeyRing.CheckPassphrase("Wrong password")
|
|
|
|
|
assert.Exactly(t, false, isCorrect)
|
2019-05-15 13:48:47 +02:00
|
|
|
|
2019-05-15 13:40:19 +02:00
|
|
|
isCorrect = encryptedKeyRing.CheckPassphrase(testMailboxPassword)
|
|
|
|
|
assert.Exactly(t, true, isCorrect)
|
2019-05-15 13:48:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestIdentities(t *testing.T) {
|
|
|
|
|
identities := testPrivateKeyRing.Identities()
|
|
|
|
|
assert.Len(t, identities, 1)
|
|
|
|
|
assert.Exactly(t, identities[0], testIdentity)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestFilterExpiredKeys(t *testing.T) {
|
|
|
|
|
expiredKey, _ := ReadArmoredKeyRing(strings.NewReader(readTestFile("key_expiredKey", false)))
|
2019-06-03 17:00:01 +02:00
|
|
|
keys := []*KeyRing{testPrivateKeyRing, expiredKey}
|
2019-05-15 13:48:47 +02:00
|
|
|
unexpired, err := FilterExpiredKeys(keys)
|
|
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatal("Expected no error while filtering expired keyrings, got:", err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
assert.Len(t, unexpired, 1)
|
|
|
|
|
assert.Exactly(t, unexpired[0], testPrivateKeyRing)
|
|
|
|
|
}
|
2019-06-03 17:00:01 +02:00
|
|
|
|
|
|
|
|
func TestGetPublicKey(t *testing.T) {
|
|
|
|
|
publicKey, err := testPrivateKeyRing.GetPublicKey()
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatal("Expected no error while obtaining public key, got:", err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
publicKeyRing, err := pgp.BuildKeyRing(publicKey)
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatal("Expected no error while creating public key ring, got:", err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
privateFingerprint, err := testPrivateKeyRing.GetFingerprint()
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatal("Expected no error while extracting private fingerprint, got:", err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
publicFingerprint, err := publicKeyRing.GetFingerprint()
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatal("Expected no error while extracting public fingerprint, got:", err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
assert.Exactly(t, privateFingerprint, publicFingerprint)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestKeyIds(t *testing.T) {
|
|
|
|
|
keyIDs := testPrivateKeyRing.KeyIds()
|
|
|
|
|
var assertKeyIDs = []uint64{4518840640391470884}
|
|
|
|
|
assert.Exactly(t, assertKeyIDs, keyIDs)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestReadFromJson(t *testing.T) {
|
|
|
|
|
decodedKeyRing := &KeyRing{}
|
|
|
|
|
err = decodedKeyRing.ReadFromJSON([]byte(readTestFile("keyring_jsonKeys", false)))
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatal("Expected no error while reading JSON, got:", err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fingerprint, err := decodedKeyRing.GetFingerprint()
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatal("Expected no error while extracting fingerprint, got:", err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
assert.Exactly(t, "91eacacca6837890efa7000470e569d5c182bef6", fingerprint)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestUnlockJson(t *testing.T) {
|
|
|
|
|
userKeyRing, err := ReadArmoredKeyRing(strings.NewReader(readTestFile("keyring_userKey", false)))
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatal("Expected no error while creating keyring, got:", err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
err = userKeyRing.UnlockWithPassphrase("testpassphrase")
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatal("Expected no error while creating keyring, got:", err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
addressKeyRing, err := userKeyRing.UnlockJSONKeyRing([]byte(readTestFile("keyring_newJSONKeys", false)))
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatal("Expected no error while reading and decrypting JSON, got:", err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for _, e := range addressKeyRing.entities {
|
|
|
|
|
assert.Exactly(t, false, e.PrivateKey.Encrypted)
|
|
|
|
|
}
|
|
|
|
|
}
|