Skip to content

Commit

Permalink
Change method signature on decrypter
Browse files Browse the repository at this point in the history
  • Loading branch information
poulad committed Sep 3, 2018
1 parent bcb77e1 commit b5c45db
Show file tree
Hide file tree
Showing 15 changed files with 74 additions and 69 deletions.
4 changes: 2 additions & 2 deletions src/Quickstart/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,8 @@ static async Task DecryptPassportDataAsync(Message message)

// Step 1: Decrypt credentials
Credentials credentials = decrypter.DecryptCredentials(
GetRsaPrivateKey(),
message.PassportData.Credentials
message.PassportData.Credentials,
GetRsaPrivateKey()
);

// Step 2: Validate nonce
Expand Down
8 changes: 4 additions & 4 deletions src/Telegram.Bot.Extensions.Passport/Decryption/Decrypter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,14 @@ public class Decrypter : IDecrypter
{
/// <inheritdoc />
public Credentials DecryptCredentials(
RSA key,
EncryptedCredentials encryptedCredentials
EncryptedCredentials encryptedCredentials,
RSA key
)
{
if (key is null)
throw new ArgumentNullException(nameof(key));
if (encryptedCredentials is null)
throw new ArgumentNullException(nameof(encryptedCredentials));
if (key is null)
throw new ArgumentNullException(nameof(key));

byte[] data = Convert.FromBase64String(encryptedCredentials.Data);
if (data.Length % 16 != 0)
Expand Down
4 changes: 2 additions & 2 deletions src/Telegram.Bot.Extensions.Passport/Decryption/IDecrypter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ namespace Telegram.Bot.Passport
public interface IDecrypter
{
Credentials DecryptCredentials(
RSA key,
EncryptedCredentials encryptedCredentials
EncryptedCredentials encryptedCredentials,
RSA key
);

TValue DecryptData<TValue>(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ public void Should_Decrypt_Identity_Card_Element_Document()
RSA key = EncryptionKey.ReadAsRsa();

IDecrypter decrypter = new Decrypter();
Credentials credentials = decrypter.DecryptCredentials(key, passportData.Credentials);
Credentials credentials = decrypter.DecryptCredentials(passportData.Credentials, key);
EncryptedPassportElement idCardEl = Assert.Single(passportData.Data, el => el.Type == "identity_card");

IdDocumentData documentData = decrypter.DecryptData<IdDocumentData>(
Expand Down Expand Up @@ -248,7 +248,7 @@ public async Task Should_Decrypt_Identity_Card_Element_Front_Side()
EncryptedPassportElement idCardEl = Assert.Single(passportData.Data, el => el.Type == "identity_card");

IDecrypter decrypter = new Decrypter();
Credentials credentials = decrypter.DecryptCredentials(key, passportData.Credentials);
Credentials credentials = decrypter.DecryptCredentials(passportData.Credentials, key);

byte[] encryptedContent;
using (System.IO.MemoryStream stream = new System.IO.MemoryStream(idCardEl.FrontSide.FileSize))
Expand Down Expand Up @@ -278,7 +278,7 @@ public async Task Should_Decrypt_Identity_Card_Element_Reverse_Side()
EncryptedPassportElement idCardEl = Assert.Single(passportData.Data, el => el.Type == "identity_card");

IDecrypter decrypter = new Decrypter();
Credentials credentials = decrypter.DecryptCredentials(key, passportData.Credentials);
Credentials credentials = decrypter.DecryptCredentials(passportData.Credentials, key);

string botToken = ConfigurationProvider.TestConfigurations.ApiToken;
File encFileInfo = await BotClient.GetFileAsync(idCardEl.ReverseSide.FileId);
Expand Down Expand Up @@ -313,7 +313,7 @@ public async Task Should_Decrypt_Identity_Card_Element_Selfie()
EncryptedPassportElement idCardEl = Assert.Single(passportData.Data, el => el.Type == "identity_card");

IDecrypter decrypter = new Decrypter();
Credentials credentials = decrypter.DecryptCredentials(key, passportData.Credentials);
Credentials credentials = decrypter.DecryptCredentials(passportData.Credentials, key);

byte[] encryptedContent;
using (System.IO.MemoryStream stream = new System.IO.MemoryStream(idCardEl.Selfie.FileSize))
Expand Down Expand Up @@ -344,7 +344,7 @@ public async Task Should_Decrypt_Utility_Bill_Element_File()
PassportFile billScanFile = Assert.Single(billElement.Files);

IDecrypter decrypter = new Decrypter();
Credentials credentials = decrypter.DecryptCredentials(key, passportData.Credentials);
Credentials credentials = decrypter.DecryptCredentials(passportData.Credentials, key);

FileCredentials fileCredentials = Assert.Single(credentials.SecureData.UtilityBill.Files);

Expand Down Expand Up @@ -375,7 +375,7 @@ public async Task Should_decrypt_utility_bill_element_translation()
PassportFile translationFile = Assert.Single(billElement.Translation);

IDecrypter decrypter = new Decrypter();
Credentials credentials = decrypter.DecryptCredentials(key, passportData.Credentials);
Credentials credentials = decrypter.DecryptCredentials(passportData.Credentials, key);

FileCredentials fileCredentials = Assert.Single(credentials.SecureData.UtilityBill.Translation);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// ReSharper disable PossibleNullReferenceException
// ReSharper disable CheckNamespace
// ReSharper disable StringLiteralTypo

using System.Security.Cryptography;
using System.Threading.Tasks;
Expand Down Expand Up @@ -76,7 +77,7 @@ await BotClient.SendTextMessageAsync(
RSA key = EncryptionKey.ReadAsRsa();
IDecrypter decrypter = new Decrypter();
Credentials credentials =
decrypter.DecryptCredentials(key, passportUpdate.Message.PassportData.Credentials);
decrypter.DecryptCredentials(passportUpdate.Message.PassportData.Credentials, key);

Assert.Equal("Test nonce for identity card", credentials.Nonce);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// ReSharper disable PossibleNullReferenceException
// ReSharper disable CheckNamespace
// ReSharper disable StringLiteralTypo

using System.Linq;
using System.Security.Cryptography;
Expand Down Expand Up @@ -76,7 +77,7 @@ await BotClient.SendTextMessageAsync(
RSA key = EncryptionKey.ReadAsRsa();
IDecrypter decrypter = new Decrypter();
Credentials credentials =
decrypter.DecryptCredentials(key, passportUpdate.Message.PassportData.Credentials);
decrypter.DecryptCredentials(passportUpdate.Message.PassportData.Credentials, key);

Assert.Equal("Test nonce for passport registration", credentials.Nonce);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// ReSharper disable PossibleNullReferenceException
// ReSharper disable CheckNamespace
// ReSharper disable StringLiteralTypo

using System.Linq;
using System.Threading.Tasks;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ public void Should_Decrypt_Credentials()

IDecrypter decrypter = new Decrypter();

Credentials credentials = decrypter.DecryptCredentials(key, passportData.Credentials);
Credentials credentials = decrypter.DecryptCredentials(passportData.Credentials, key);

Assert.NotNull(credentials);
Assert.Equal("Test nonce for driver license", credentials.Nonce);
Expand Down Expand Up @@ -175,7 +175,7 @@ public void Should_Decrypt_Document_Data()
EncryptedPassportElement element = passportData.Data.Single();
RSA key = EncryptionKey.ReadAsRsa();
IDecrypter decrypter = new Decrypter();
Credentials credentials = decrypter.DecryptCredentials(key, passportData.Credentials);
Credentials credentials = decrypter.DecryptCredentials(passportData.Credentials, key);

IdDocumentData licenseDoc = decrypter.DecryptData<IdDocumentData>(
encryptedData: element.Data,
Expand All @@ -201,7 +201,7 @@ public async Task Should_Decrypt_Front_Side_File()
RSA key = EncryptionKey.ReadAsRsa();
EncryptedPassportElement element = passportData.Data.Single();
IDecrypter decrypter = new Decrypter();
Credentials credentials = decrypter.DecryptCredentials(key, passportData.Credentials);
Credentials credentials = decrypter.DecryptCredentials(passportData.Credentials, key);

File encryptedFileInfo;
string decryptedFilePath = System.IO.Path.GetTempFileName();
Expand Down Expand Up @@ -229,7 +229,7 @@ public async Task Should_Decrypt_Reverse_Side_File()
RSA key = EncryptionKey.ReadAsRsa();
EncryptedPassportElement element = passportData.Data.Single();
IDecrypter decrypter = new Decrypter();
Credentials credentials = decrypter.DecryptCredentials(key, passportData.Credentials);
Credentials credentials = decrypter.DecryptCredentials(passportData.Credentials, key);

File encryptedFileInfo;
string decryptedFilePath = System.IO.Path.GetTempFileName();
Expand Down Expand Up @@ -266,7 +266,7 @@ public async Task Should_Decrypt_Selfie_File()
RSA key = EncryptionKey.ReadAsRsa();
EncryptedPassportElement element = passportData.Data.Single();
IDecrypter decrypter = new Decrypter();
Credentials credentials = decrypter.DecryptCredentials(key, passportData.Credentials);
Credentials credentials = decrypter.DecryptCredentials(passportData.Credentials, key);

byte[] encryptedContent;
{
Expand Down Expand Up @@ -310,7 +310,7 @@ public async Task Should_Decrypt_Translation_File()
EncryptedPassportElement element = passportData.Data.Single();

IDecrypter decrypter = new Decrypter();
Credentials credentials = decrypter.DecryptCredentials(key, passportData.Credentials);
Credentials credentials = decrypter.DecryptCredentials(passportData.Credentials, key);

for (int i = 0; i < element.Translation.Length; i++)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ public void Should_Decrypt_Credentials()
RSA key = EncryptionKey.ReadAsRsa();

IDecrypter decrypter = new Decrypter();
Credentials credentials = decrypter.DecryptCredentials(key, passportData.Credentials);
Credentials credentials = decrypter.DecryptCredentials(passportData.Credentials, key);

Assert.NotNull(credentials);
Assert.NotEmpty(credentials.Nonce);
Expand All @@ -124,7 +124,7 @@ public void Should_Decrypt_Data()

RSA key = EncryptionKey.ReadAsRsa();
IDecrypter decrypter = new Decrypter();
Credentials credentials = decrypter.DecryptCredentials(key, passportData.Credentials);
Credentials credentials = decrypter.DecryptCredentials(passportData.Credentials, key);

PersonalDetails personalDetails = decrypter.DecryptData<PersonalDetails>(
element.Data,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ public void Should_Decrypt_Credentials()

IDecrypter decrypter = new Decrypter();

Credentials credentials = decrypter.DecryptCredentials(key, passportData.Credentials);
Credentials credentials = decrypter.DecryptCredentials(passportData.Credentials,EncryptionKey.ReadAsRsa());

Assert.NotNull(credentials);
Assert.Equal("Test nonce for address", credentials.Nonce);
Expand All @@ -122,7 +122,7 @@ public void Should_Decrypt_Data()

RSA key = EncryptionKey.ReadAsRsa();
IDecrypter decrypter = new Decrypter();
Credentials credentials = decrypter.DecryptCredentials(key, passportData.Credentials);
Credentials credentials = decrypter.DecryptCredentials(passportData.Credentials,EncryptionKey.ReadAsRsa());

ResidentialAddress residentialAddress = decrypter.DecryptData<ResidentialAddress>(
element.Data,
Expand Down
18 changes: 11 additions & 7 deletions test/UnitTests/Encryption Key.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,11 @@ namespace UnitTests
{
internal static class EncryptionKey
{
public static RSA GetRsaPrivateKey()
public static RSA RsaPrivateKey
{
string privateKeyPem = @"-----BEGIN RSA PRIVATE KEY-----
get
{
string privateKeyPem = @"-----BEGIN RSA PRIVATE KEY-----
MIIEpAIBAAKCAQEA0VElWoQA2SK1csG2/sY/wlssO1bjXRx+t+JlIgS6jLPCefyC
AcZBv7ElcSPJQIPEXNwN2XdnTc2wEIjZ8bTgBlBqXppj471bJeX8Mi2uAxAqOUDu
vGuqth+mq7DMqol3MNH5P9FO6li7nZxI1FX39u2r/4H4PXRiWx13gsVQRL6Clq2j
Expand Down Expand Up @@ -39,11 +41,13 @@ public static RSA GetRsaPrivateKey()
gaZk6+H62W5zGnIbtzodB2n7JasK561Ic/QcrEtheC4Qmr+RXe03pg==
-----END RSA PRIVATE KEY-----
";
PemReader pemReader = new PemReader(new StringReader(privateKeyPem));
AsymmetricCipherKeyPair keyPair = (AsymmetricCipherKeyPair) pemReader.ReadObject();
RSAParameters parameters = DotNetUtilities.ToRSAParameters(keyPair.Private as RsaPrivateCrtKeyParameters);
RSA rsa = RSA.Create(parameters);
return rsa;
PemReader pemReader = new PemReader(new StringReader(privateKeyPem));
AsymmetricCipherKeyPair keyPair = (AsymmetricCipherKeyPair) pemReader.ReadObject();
RSAParameters parameters =
DotNetUtilities.ToRSAParameters(keyPair.Private as RsaPrivateCrtKeyParameters);
RSA rsa = RSA.Create(parameters);
return rsa;
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,12 @@ public class IdentityCardAndUtilityBillTests
[Fact(DisplayName = "Should decrypt 'passport_data.credentials'")]
public void Should_Decrypt_Credentials()
{
RSA key = EncryptionKey.GetRsaPrivateKey();
RSA key = EncryptionKey.RsaPrivateKey;
PassportData passData = GetPassportData();

IDecrypter decrypter = new Decrypter();

Credentials credentials = decrypter.DecryptCredentials(key, encryptedCredentials: passData.Credentials);
Credentials credentials = decrypter.DecryptCredentials(encryptedCredentials: passData.Credentials, key);

Assert.NotNull(credentials);
Assert.NotNull(credentials.SecureData);
Expand Down Expand Up @@ -73,11 +73,11 @@ public void Should_Decrypt_Credentials()
[Fact(DisplayName = "Should decrypt docuemnt data in 'identity_card' element")]
public void Should_Decrypt_Element_Document()
{
RSA key = EncryptionKey.GetRsaPrivateKey();
PassportData passportData = GetPassportData();

IDecrypter decrypter = new Decrypter();
Credentials credentials = decrypter.DecryptCredentials(key, passportData.Credentials);
Credentials credentials =
decrypter.DecryptCredentials(passportData.Credentials, EncryptionKey.RsaPrivateKey);

EncryptedPassportElement idCardEl = Assert.Single(passportData.Data, el => el.Type == "identity_card");

Expand All @@ -94,7 +94,6 @@ public void Should_Decrypt_Element_Document()
[Fact(DisplayName = "Should decrypt front side photo in 'identity_card' element")]
public async Task Should_Decrypt_Identity_Card_Element_Front_Side()
{
RSA key = EncryptionKey.GetRsaPrivateKey();
PassportData passportData = GetPassportData();
EncryptedPassportElement idCardEl = Assert.Single(passportData.Data, el => el.Type == "identity_card");

Expand All @@ -104,7 +103,8 @@ public async Task Should_Decrypt_Identity_Card_Element_Front_Side()
Assert.Equal(0, idCardEl.FrontSide.FileSize);

IDecrypter decrypter = new Decrypter();
Credentials credentials = decrypter.DecryptCredentials(key, passportData.Credentials);
Credentials credentials =
decrypter.DecryptCredentials(passportData.Credentials, EncryptionKey.RsaPrivateKey);

byte[] encryptedContent = await System.IO.File.ReadAllBytesAsync("Files/identity_card-front_side.jpg.enc");
byte[] content = decrypter.DecryptFile(
Expand Down Expand Up @@ -134,7 +134,6 @@ await decrypter.DecryptFileAsync(
[Fact(DisplayName = "Should decrypt reverse side photo in 'identity_card' element")]
public async Task Should_Decrypt_Identity_Card_Element_Reverse_Side()
{
RSA key = EncryptionKey.GetRsaPrivateKey();
PassportData passportData = GetPassportData();
EncryptedPassportElement idCardEl = Assert.Single(passportData.Data, el => el.Type == "identity_card");

Expand All @@ -144,7 +143,8 @@ public async Task Should_Decrypt_Identity_Card_Element_Reverse_Side()
Assert.Equal(0, idCardEl.ReverseSide.FileSize);

IDecrypter decrypter = new Decrypter();
Credentials credentials = decrypter.DecryptCredentials(key, passportData.Credentials);
Credentials credentials =
decrypter.DecryptCredentials(passportData.Credentials, EncryptionKey.RsaPrivateKey);

byte[] encryptedContent =
await System.IO.File.ReadAllBytesAsync("Files/identity_card-reverse_side.jpg.enc");
Expand Down Expand Up @@ -175,7 +175,6 @@ await decrypter.DecryptFileAsync(
[Fact(DisplayName = "Should decrypt selfie photo in 'identity_card' element")]
public async Task Should_decrypt_identity_card_element_selfie()
{
RSA key = EncryptionKey.GetRsaPrivateKey();
PassportData passportData = GetPassportData();
EncryptedPassportElement idCardEl = Assert.Single(passportData.Data, el => el.Type == "identity_card");

Expand All @@ -185,7 +184,8 @@ public async Task Should_decrypt_identity_card_element_selfie()
Assert.Equal(0, idCardEl.Selfie.FileSize);

IDecrypter decrypter = new Decrypter();
Credentials credentials = decrypter.DecryptCredentials(key, passportData.Credentials);
Credentials credentials =
decrypter.DecryptCredentials(passportData.Credentials, EncryptionKey.RsaPrivateKey);

byte[] encryptedContent = await System.IO.File.ReadAllBytesAsync("Files/identity_card-selfie.jpg.enc");
byte[] content = decrypter.DecryptFile(
Expand Down Expand Up @@ -215,7 +215,6 @@ await decrypter.DecryptFileAsync(
[Fact(DisplayName = "Should decrypt the single file in 'utility_bill' element")]
public async Task Should_Decrypt_Utility_Bill_Element_File()
{
RSA key = EncryptionKey.GetRsaPrivateKey();
PassportData passportData = GetPassportData();
EncryptedPassportElement billElement = Assert.Single(passportData.Data, el => el.Type == "utility_bill");

Expand All @@ -227,7 +226,8 @@ public async Task Should_Decrypt_Utility_Bill_Element_File()
Assert.Equal(0, scanFile.FileSize);

IDecrypter decrypter = new Decrypter();
Credentials credentials = decrypter.DecryptCredentials(key, passportData.Credentials);
Credentials credentials =
decrypter.DecryptCredentials(passportData.Credentials, EncryptionKey.RsaPrivateKey);

FileCredentials billFileCredentials = Assert.Single(credentials.SecureData.UtilityBill.Files);

Expand Down Expand Up @@ -259,7 +259,6 @@ await decrypter.DecryptFileAsync(
[Fact(DisplayName = "Should decrypt the single translation file in 'utility_bill' element")]
public async Task Should_Decrypt_Utility_Bill_Element_Translation()
{
RSA key = EncryptionKey.GetRsaPrivateKey();
PassportData passportData = GetPassportData();
EncryptedPassportElement billElement = Assert.Single(passportData.Data, el => el.Type == "utility_bill");

Expand All @@ -271,7 +270,8 @@ public async Task Should_Decrypt_Utility_Bill_Element_Translation()
Assert.Equal(0, translationFile.FileSize);

IDecrypter decrypter = new Decrypter();
Credentials credentials = decrypter.DecryptCredentials(key, passportData.Credentials);
Credentials credentials =
decrypter.DecryptCredentials(passportData.Credentials, EncryptionKey.RsaPrivateKey);

FileCredentials translationFileCredentials = Assert.Single(credentials.SecureData.UtilityBill.Translation);

Expand Down
Loading

0 comments on commit b5c45db

Please sign in to comment.