2019-09-12 11:33:17 +02:00
|
|
|
package helper
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"testing"
|
|
|
|
|
|
2019-12-27 19:35:43 +01:00
|
|
|
"github.com/ProtonMail/gopenpgp/v2/constants"
|
|
|
|
|
"github.com/ProtonMail/gopenpgp/v2/crypto"
|
2019-09-12 11:33:17 +02:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
|
)
|
|
|
|
|
|
2020-01-06 04:21:44 -08:00
|
|
|
func TestMobileSignedMessageDecryption(t *testing.T) {
|
2019-12-27 19:35:43 +01:00
|
|
|
privateKey, _ := crypto.NewKeyFromArmored(readTestFile("keyring_privateKey", false))
|
2019-09-12 11:33:17 +02:00
|
|
|
// Password defined in base_test
|
2019-12-27 19:35:43 +01:00
|
|
|
privateKey, err := privateKey.Unlock(testMailboxPassword)
|
2019-09-12 11:33:17 +02:00
|
|
|
if err != nil {
|
|
|
|
|
t.Fatal("Expected no error unlocking privateKey, got:", err)
|
|
|
|
|
}
|
2019-12-27 19:35:43 +01:00
|
|
|
testPrivateKeyRing, _ := crypto.NewKeyRing(privateKey)
|
|
|
|
|
|
|
|
|
|
publicKey, _ := crypto.NewKeyFromArmored(readTestFile("mime_publicKey", false))
|
|
|
|
|
testPublicKeyRing, _ := crypto.NewKeyRing(publicKey)
|
2019-09-12 11:33:17 +02:00
|
|
|
|
|
|
|
|
pgpMessage, err := crypto.NewPGPMessageFromArmored(readTestFile("message_signed", false))
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatal("Expected no error when unarmoring, got:", err)
|
|
|
|
|
}
|
|
|
|
|
|
2019-10-22 18:44:45 +02:00
|
|
|
decrypted, err := DecryptExplicitVerify(pgpMessage, testPrivateKeyRing, testPublicKeyRing, crypto.GetUnixTime())
|
2019-09-12 11:33:17 +02:00
|
|
|
if err != nil {
|
|
|
|
|
t.Fatal("Expected no error when decrypting, got:", err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
assert.Exactly(t, constants.SIGNATURE_NO_VERIFIER, decrypted.SignatureVerificationError.Status)
|
|
|
|
|
assert.Exactly(t, readTestFile("message_plaintext", true), decrypted.Message.GetString())
|
|
|
|
|
|
2019-12-27 19:35:43 +01:00
|
|
|
publicKey, _ = crypto.NewKeyFromArmored(readTestFile("keyring_publicKey", false))
|
|
|
|
|
testPublicKeyRing, _ = crypto.NewKeyRing(publicKey)
|
2019-09-12 11:33:17 +02:00
|
|
|
|
|
|
|
|
pgpMessage, err = testPublicKeyRing.Encrypt(decrypted.Message, testPrivateKeyRing)
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatal("Expected no error when encrypting, got:", err)
|
|
|
|
|
}
|
|
|
|
|
|
2019-10-22 18:44:45 +02:00
|
|
|
decrypted, err = DecryptExplicitVerify(pgpMessage, testPrivateKeyRing, testPublicKeyRing, crypto.GetUnixTime())
|
2019-09-12 11:33:17 +02:00
|
|
|
if err != nil {
|
|
|
|
|
t.Fatal("Expected no error when decrypting, got:", err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
assert.Nil(t, decrypted.SignatureVerificationError)
|
|
|
|
|
assert.Exactly(t, readTestFile("message_plaintext", true), decrypted.Message.GetString())
|
|
|
|
|
|
2019-10-22 18:44:45 +02:00
|
|
|
decrypted, err = DecryptExplicitVerify(pgpMessage, testPublicKeyRing, testPublicKeyRing, crypto.GetUnixTime())
|
2019-09-12 11:33:17 +02:00
|
|
|
assert.NotNil(t, err)
|
|
|
|
|
assert.Nil(t, decrypted)
|
|
|
|
|
}
|
2020-04-07 14:59:25 +02:00
|
|
|
|
2021-04-27 17:38:25 +02:00
|
|
|
func TestMobileSignedMessageDecryptionWithSessionKey(t *testing.T) {
|
|
|
|
|
var message = crypto.NewPlainMessageFromString(
|
|
|
|
|
"The secret code is... 1, 2, 3, 4, 5. I repeat: the secret code is... 1, 2, 3, 4, 5",
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
privateKey, _ := crypto.NewKeyFromArmored(readTestFile("keyring_privateKey", false))
|
|
|
|
|
// Password defined in base_test
|
|
|
|
|
privateKey, err := privateKey.Unlock(testMailboxPassword)
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatal("Expected no error unlocking privateKey, got:", err)
|
|
|
|
|
}
|
|
|
|
|
testPrivateKeyRing, _ := crypto.NewKeyRing(privateKey)
|
|
|
|
|
|
|
|
|
|
publicKey, _ := crypto.NewKeyFromArmored(readTestFile("keyring_publicKey", false))
|
|
|
|
|
testPublicKeyRing, _ := crypto.NewKeyRing(publicKey)
|
|
|
|
|
|
|
|
|
|
sk, err := crypto.GenerateSessionKey()
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatal("Expected no error generating session key, got:", err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pgpMessage, err := sk.Encrypt(message)
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatal("Expected no error when unarmoring, got:", err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
decrypted, err := DecryptSessionKeyExplicitVerify(pgpMessage, sk, testPublicKeyRing, crypto.GetUnixTime())
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatal("Expected no error when decrypting, got:", err)
|
|
|
|
|
}
|
|
|
|
|
|
2021-07-15 18:56:15 +02:00
|
|
|
assert.Exactly(t, constants.SIGNATURE_NOT_SIGNED, decrypted.SignatureVerificationError.Status)
|
2021-04-27 17:38:25 +02:00
|
|
|
assert.Exactly(t, message.GetString(), decrypted.Message.GetString())
|
|
|
|
|
|
|
|
|
|
publicKey, _ = crypto.NewKeyFromArmored(readTestFile("keyring_publicKey", false))
|
|
|
|
|
testPublicKeyRing, _ = crypto.NewKeyRing(publicKey)
|
|
|
|
|
|
|
|
|
|
pgpMessage, err = sk.EncryptAndSign(message, testPrivateKeyRing)
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatal("Expected no error when encrypting, got:", err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
decrypted, err = DecryptSessionKeyExplicitVerify(pgpMessage, sk, testPublicKeyRing, crypto.GetUnixTime())
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatal("Expected no error when decrypting, got:", err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
assert.Nil(t, decrypted.SignatureVerificationError)
|
|
|
|
|
assert.Exactly(t, message.GetString(), decrypted.Message.GetString())
|
|
|
|
|
}
|
|
|
|
|
|
2020-04-07 14:59:25 +02:00
|
|
|
func TestGetJsonSHA256FingerprintsV4(t *testing.T) {
|
|
|
|
|
sha256Fingerprints, err := GetJsonSHA256Fingerprints(readTestFile("keyring_publicKey", false))
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatal("Cannot unarmor key:", err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
assert.Exactly(t, []byte("[\"d9ac0b857da6d2c8be985b251a9e3db31e7a1d2d832d1f07ebe838a9edce9c24\",\"203dfba1f8442c17e59214d9cd11985bfc5cc8721bb4a71740dd5507e58a1a0d\"]"), sha256Fingerprints)
|
|
|
|
|
}
|