コード例 #1
0
        public void Should_Decrypt_Personal_Details_Element()
        {
            PassportData passportData = GetPassportData();

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

            EncryptedPassportElement element = Assert.Single(passportData.Data, el => el.Type == "personal_details");

            PersonalDetails personalDetails = decrypter.DecryptData <PersonalDetails>(
                encryptedData: element.Data,
                dataCredentials: credentials.SecureData.PersonalDetails.Data
                );

            Assert.Equal("Poulad", personalDetails.FirstName);
            Assert.Equal("Ashrafpour", personalDetails.LastName);
            Assert.Equal("پولاد", personalDetails.FirstNameNative);
            Assert.Equal("اشرف پور", personalDetails.LastNameNative);
            Assert.Empty(personalDetails.MiddleName);
            Assert.Empty(personalDetails.MiddleNameNative);
            Assert.Equal("male", personalDetails.Gender);
            Assert.Equal(PassportEnums.Gender.Male, personalDetails.Gender);
            Assert.Equal("US", personalDetails.CountryCode);          // U.S.A
            Assert.Equal("IR", personalDetails.ResidenceCountryCode); // Iran
            Assert.Equal("30.07.1990", personalDetails.BirthDate);
            Assert.InRange(personalDetails.Birthdate, new DateTime(1990, 7, 30), new DateTime(1990, 7, 30, 1, 0, 0));
        }
コード例 #2
0
        public async Task Should_Decrypt_Front_Side_File()
        {
            PassportData passportData = GetPassportData();

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

            byte[] encryptedContent = await System.IO.File.ReadAllBytesAsync("Files/driver_license-front_side.jpg.enc");

            byte[] content = decrypter.DecryptFile(
                encryptedContent,
                credentials.SecureData.DriverLicense.FrontSide
                );
            Assert.NotEmpty(content);
            await System.IO.File.WriteAllBytesAsync("Files/driver_license-front_side.jpg", content);

            using (System.IO.MemoryStream
                   encryptedFileStream = new System.IO.MemoryStream(encryptedContent),
                   decryptedFileStream = new System.IO.MemoryStream()
                   )
            {
                await decrypter.DecryptFileAsync(
                    encryptedFileStream,
                    credentials.SecureData.DriverLicense.FrontSide,
                    decryptedFileStream
                    );

                Assert.Equal(content, decryptedFileStream.ToArray());
            }
        }
コード例 #3
0
        public async Task Should_decrypt_utility_bill_element_translation()
        {
            Update                   update       = _classFixture.Entity;
            PassportData             passportData = update.Message.PassportData;
            RSA                      key          = EncryptionKey.ReadAsRsa();
            EncryptedPassportElement billElement  = Assert.Single(passportData.Data, el => el.Type == "utility_bill");

            PassportFile translationFile = Assert.Single(billElement.Translation);

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

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

            File encryptedFileInfo;

            using (System.IO.Stream decryptedFile = new System.IO.MemoryStream())
            {
                encryptedFileInfo = await BotClient.DownloadAndDecryptPassportFileAsync(
                    translationFile,
                    fileCredentials,
                    decryptedFile
                    );

                Assert.InRange(decryptedFile.Length, translationFile.FileSize - 256, translationFile.FileSize + 256);
            }

            Assert.NotEmpty(encryptedFileInfo.FilePath);
            Assert.NotEmpty(encryptedFileInfo.FileId);
            Assert.InRange(encryptedFileInfo.FileSize, 1_000, 50_000_000);
        }
        public void Should_Decrypt_Data()
        {
            Update                   update       = _classFixture.Entity;
            PassportData             passportData = update.Message.PassportData;
            EncryptedPassportElement element      = passportData.Data.Single();

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

            PersonalDetails personalDetails = decrypter.DecryptData <PersonalDetails>(
                element.Data,
                credentials.SecureData.PersonalDetails.Data
                );

            Assert.NotNull(personalDetails);
            Assert.NotEmpty(personalDetails.FirstName);
            Assert.NotEmpty(personalDetails.Gender);
            Assert.NotEmpty(personalDetails.CountryCode);
            Assert.Equal(2, personalDetails.CountryCode.Length);
            Assert.NotEmpty(personalDetails.ResidenceCountryCode);
            Assert.Equal(2, personalDetails.ResidenceCountryCode.Length);
            Assert.NotEmpty(personalDetails.BirthDate);
            Assert.InRange(personalDetails.Birthdate, new DateTime(1900, 1, 1), DateTime.Today);
        }
コード例 #5
0
        public async Task Should_Decrypt_Identity_Card_Element_Selfie()
        {
            Update                   update       = _classFixture.Entity;
            PassportData             passportData = update.Message.PassportData;
            RSA                      key          = EncryptionKey.ReadAsRsa();
            EncryptedPassportElement idCardEl     = Assert.Single(passportData.Data, el => el.Type == "identity_card");

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

            byte[] encryptedContent;
            using (System.IO.MemoryStream stream = new System.IO.MemoryStream(idCardEl.Selfie.FileSize))
            {
                await BotClient.GetInfoAndDownloadFileAsync(
                    idCardEl.Selfie.FileId,
                    stream
                    );

                encryptedContent = stream.ToArray();
            }

            byte[] content = decrypter.DecryptFile(
                encryptedContent,
                credentials.SecureData.IdentityCard.Selfie
                );

            Assert.NotEmpty(content);
        }
コード例 #6
0
        public async Task Should_Decrypt_Identity_Card_Element_Reverse_Side()
        {
            Update                   update       = _classFixture.Entity;
            PassportData             passportData = update.Message.PassportData;
            RSA                      key          = EncryptionKey.ReadAsRsa();
            EncryptedPassportElement idCardEl     = Assert.Single(passportData.Data, el => el.Type == "identity_card");

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

            string botToken    = ConfigurationProvider.TestConfigurations.ApiToken;
            File   encFileInfo = await BotClient.GetFileAsync(idCardEl.ReverseSide.FileId);

            HttpClient http = new HttpClient();

            System.IO.Stream encFileStream = await http.GetStreamAsync(
                $"https://api.telegram.org/file/bot{botToken}/{encFileInfo.FilePath}"
                );

            string destFilePath = System.IO.Path.GetTempFileName();

            using (encFileStream)
                using (System.IO.Stream reverseSideFile = System.IO.File.OpenWrite(destFilePath))
                {
                    await decrypter.DecryptFileAsync(
                        encFileStream,
                        credentials.SecureData.IdentityCard.ReverseSide,
                        reverseSideFile
                        );

                    Assert.InRange(reverseSideFile.Length, encFileInfo.FileSize - 256, encFileInfo.FileSize + 256);
                }

            _output.WriteLine("Reverse side photo is written to file \"{0}\".", destFilePath);
        }
コード例 #7
0
        public void Should_Decrypt_Identity_Card_Element_Document()
        {
            Update       update       = _classFixture.Entity;
            PassportData passportData = update.Message.PassportData;

            RSA key = EncryptionKey.ReadAsRsa();

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

            IdDocumentData documentData = decrypter.DecryptData <IdDocumentData>(
                idCardEl.Data,
                credentials.SecureData.IdentityCard.Data
                );

            Assert.NotEmpty(documentData.DocumentNo);
            if (string.IsNullOrEmpty(documentData.ExpiryDate))
            {
                Assert.Null(documentData.Expiry);
            }
            else
            {
                Assert.NotNull(documentData.Expiry);
            }
        }
        public async Task Should_Decrypt_Front_Side_File()
        {
            Update                   update       = _classFixture.Entity;
            PassportData             passportData = update.Message.PassportData;
            RSA                      key          = EncryptionKey.ReadAsRsa();
            EncryptedPassportElement element      = passportData.Data.Single();
            IDecrypter               decrypter    = new Decrypter();
            Credentials              credentials  = decrypter.DecryptCredentials(passportData.Credentials, key);

            File   encryptedFileInfo;
            string decryptedFilePath = System.IO.Path.GetTempFileName();

            using (System.IO.Stream decryptedFile = System.IO.File.OpenWrite(decryptedFilePath))
            {
                encryptedFileInfo = await BotClient.DownloadAndDecryptPassportFileAsync(
                    element.FrontSide,
                    credentials.SecureData.DriverLicense.FrontSide,
                    decryptedFile
                    );
            }

            _output.WriteLine("Front side JPEG file is written to \"{0}\".", decryptedFilePath);

            Assert.NotEmpty(encryptedFileInfo.FilePath);
            Assert.NotEmpty(encryptedFileInfo.FileId);
            Assert.InRange(encryptedFileInfo.FileSize, 1_000, 50_000_000);
        }
コード例 #9
0
        public void Should_Decrypt_Credentials()
        {
            Update       update       = _classFixture.Entity;
            PassportData passportData = update.Message.PassportData;

            RSA key = EncryptionKey.ReadAsRsa();

            IDecrypter decrypter = new Decrypter();

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

            Assert.NotNull(credentials);
            Assert.NotNull(credentials.SecureData);
            Assert.Equal("Test nonce for id card & utility bill", credentials.Nonce);

            // decryption of document data in 'identity_card' element requires accompanying DataCredentials
            Assert.NotNull(credentials.SecureData.IdentityCard);
            Assert.NotNull(credentials.SecureData.IdentityCard.Data);
            Assert.NotEmpty(credentials.SecureData.IdentityCard.Data.Secret);
            Assert.NotEmpty(credentials.SecureData.IdentityCard.Data.DataHash);

            // decryption of front side of 'identity_card' element requires accompanying FileCredentials
            Assert.NotNull(credentials.SecureData.IdentityCard.FrontSide);
            Assert.NotEmpty(credentials.SecureData.IdentityCard.FrontSide.Secret);
            Assert.NotEmpty(credentials.SecureData.IdentityCard.FrontSide.FileHash);

            // decryption of reverse side of 'identity_card' element requires accompanying FileCredentials
            Assert.NotNull(credentials.SecureData.IdentityCard.ReverseSide);
            Assert.NotEmpty(credentials.SecureData.IdentityCard.ReverseSide.Secret);
            Assert.NotEmpty(credentials.SecureData.IdentityCard.ReverseSide.FileHash);

            // decryption of selfie of 'identity_card' element requires accompanying FileCredentials
            Assert.NotNull(credentials.SecureData.IdentityCard.Selfie);
            Assert.NotEmpty(credentials.SecureData.IdentityCard.Selfie.Secret);
            Assert.NotEmpty(credentials.SecureData.IdentityCard.Selfie.FileHash);

            Assert.Null(credentials.SecureData.IdentityCard.Translation);
            Assert.Null(credentials.SecureData.IdentityCard.Files);

            // decryption of file scan in 'utility_bill' element requires accompanying FileCredentials
            Assert.NotNull(credentials.SecureData.UtilityBill.Files);
            FileCredentials billCredentials = Assert.Single(credentials.SecureData.UtilityBill.Files);

            Assert.NotEmpty(billCredentials.Secret);
            Assert.NotEmpty(billCredentials.FileHash);

            // decryption of translation file scan in 'utility_bill' element requires accompanying FileCredentials
            Assert.NotNull(credentials.SecureData.UtilityBill.Files);
            FileCredentials billTranslationFileCredentials =
                Assert.Single(credentials.SecureData.UtilityBill.Translation);

            Assert.NotEmpty(billTranslationFileCredentials.Secret);
            Assert.NotEmpty(billTranslationFileCredentials.FileHash);
        }
        public void Should_Decrypt_Credentials()
        {
            RSA          key      = EncryptionKey.RsaPrivateKey;
            PassportData passData = GetPassportData();

            IDecrypter decrypter = new Decrypter();

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

            Assert.NotNull(credentials);
            Assert.NotNull(credentials.SecureData);
            Assert.Equal("TEST", credentials.Nonce);

            // decryption of document data in 'identity_card' element requires accompanying DataCredentials
            Assert.NotNull(credentials.SecureData.IdentityCard);
            Assert.NotNull(credentials.SecureData.IdentityCard.Data);
            Assert.NotEmpty(credentials.SecureData.IdentityCard.Data.Secret);
            Assert.NotEmpty(credentials.SecureData.IdentityCard.Data.DataHash);

            // decryption of front side of 'identity_card' element requires accompanying FileCredentials
            Assert.NotNull(credentials.SecureData.IdentityCard.FrontSide);
            Assert.NotEmpty(credentials.SecureData.IdentityCard.FrontSide.Secret);
            Assert.NotEmpty(credentials.SecureData.IdentityCard.FrontSide.FileHash);

            // decryption of reverse side of 'identity_card' element requires accompanying FileCredentials
            Assert.NotNull(credentials.SecureData.IdentityCard.ReverseSide);
            Assert.NotEmpty(credentials.SecureData.IdentityCard.ReverseSide.Secret);
            Assert.NotEmpty(credentials.SecureData.IdentityCard.ReverseSide.FileHash);

            // decryption of selfie of 'identity_card' element requires accompanying FileCredentials
            Assert.NotNull(credentials.SecureData.IdentityCard.Selfie);
            Assert.NotEmpty(credentials.SecureData.IdentityCard.Selfie.Secret);
            Assert.NotEmpty(credentials.SecureData.IdentityCard.Selfie.FileHash);

            Assert.Null(credentials.SecureData.IdentityCard.Translation);
            Assert.Null(credentials.SecureData.IdentityCard.Files);

            // decryption of file scan in 'utility_bill' element requires accompanying FileCredentials
            Assert.NotNull(credentials.SecureData.UtilityBill.Files);
            FileCredentials billFileCredentials = Assert.Single(credentials.SecureData.UtilityBill.Files);

            Assert.NotEmpty(billFileCredentials.Secret);
            Assert.NotEmpty(billFileCredentials.FileHash);

            // decryption of translation file scan in 'utility_bill' element requires accompanying FileCredentials
            Assert.NotNull(credentials.SecureData.UtilityBill.Files);
            FileCredentials billTranslationFileCredentials =
                Assert.Single(credentials.SecureData.UtilityBill.Translation);

            Assert.NotEmpty(billTranslationFileCredentials.Secret);
            Assert.NotEmpty(billTranslationFileCredentials.FileHash);
        }
        public void Should_Throw_If_Null_Credentials_Data()
        {
            IDecrypter decrypter = new Decrypter();

            Exception exception = Assert.ThrowsAny <Exception>(() =>
                                                               decrypter.DecryptCredentials(new EncryptedCredentials(), RSA.Create())
                                                               );

            Assert.Matches(@"^Value cannot be null\.\s+Parameter name: Data$", exception.Message);
            Assert.IsType <ArgumentNullException>(exception);
        }
        public void Should_Decrypt_Credentials()
        {
            Update       update       = _classFixture.Entity;
            PassportData passportData = update.Message.PassportData;

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

            Assert.NotNull(credentials);
            Assert.Equal("Test nonce for address", credentials.Nonce);
            Assert.NotNull(credentials.SecureData);
        }
        public async Task Should_Generate_Auth_Link()
        {
            const string publicKey = "-----BEGIN PUBLIC KEY-----\n" +
                                     "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA0VElWoQA2SK1csG2/sY/\n" +
                                     "wlssO1bjXRx+t+JlIgS6jLPCefyCAcZBv7ElcSPJQIPEXNwN2XdnTc2wEIjZ8bTg\n" +
                                     "BlBqXppj471bJeX8Mi2uAxAqOUDuvGuqth+mq7DMqol3MNH5P9FO6li7nZxI1FX3\n" +
                                     "9u2r/4H4PXRiWx13gsVQRL6Clq2jcXFHc9CvNaCQEJX95jgQFAybal216EwlnnVV\n" +
                                     "giT/TNsfFjW41XJZsHUny9k+dAfyPzqAk54cgrvjgAHJayDWjapq90Fm/+e/DVQ6\n" +
                                     "BHGkV0POQMkkBrvvhAIQu222j+03frm9b2yZrhX/qS01lyjW4VaQytGV0wlewV6B\n" +
                                     "FwIDAQAB\n" +
                                     "-----END PUBLIC KEY-----";
            PassportScope scope = new PassportScope(new[]
            {
                new PassportScopeElementOne(PassportEnums.Scope.Passport)
                {
                    Selfie      = true,
                    Translation = true,
                },
            });
            AuthorizationRequestParameters authReq = new AuthorizationRequestParameters(
                botId: _fixture.BotUser.Id,
                publicKey: publicKey,
                nonce: "Test nonce for passport",
                scope: scope
                );

            await BotClient.SendTextMessageAsync(
                _fixture.SupergroupChat,
                "Share your *passport* with its *translation* and a *selfie* using Telegram Passport.\n\n" +
                "1. Click inline button\n" +
                "2. Open link in browser to redirect you back to Telegram passport\n" +
                "3. Authorize bot to access the info",
                ParseMode.Markdown,
                replyMarkup : (InlineKeyboardMarkup)InlineKeyboardButton.WithUrl(
                    "Share via Passport",
                    $"https://telegrambots.github.io/Telegram.Bot.Extensions.Passport/redirect.html?{authReq.Query}"
                    )
                );

            Update passportUpdate = await _fixture.UpdateReceiver.GetPassportUpdate();

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

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

            _classFixture.AuthorizationRequestParameters = authReq;
            _classFixture.Credentials = credentials;
            _classFixture.Message     = passportUpdate.Message;
        }
        public void Should_Throw_If_Invalid_Credentials_Hash(string hash)
        {
            EncryptedCredentials encryptedCredentials = new EncryptedCredentials
            {
                Data   = "dGV4dCBvZiAxNiBjaGFycw==",
                Secret = "",
                Hash   = hash,
            };

            IDecrypter decrypter = new Decrypter();

            Assert.ThrowsAny <FormatException>(() =>
                                               decrypter.DecryptCredentials(encryptedCredentials, RSA.Create())
                                               );
        }
        public void Should_Decrypt_Credentials_Personal_Details()
        {
            EncryptedCredentials encryptedCredentials = new EncryptedCredentials
            {
                Data = "tHcn5IEhx7REdkb/C9BTEJW7Ftob4UFzl/vWQXADBqfTCG05OvvgMn6GYZQpi8qW92tREsju35adGvzX6+lJrcSZYPr3" +
                       "sRbok+2lBIBs/tIeGWl39HpTTHhQsMTCILnOsuqpJzYAq0TvbTcaq2rkD8qTG30fxbVNWpQbRJCvFkLH3ueuJfMHs/ig" +
                       "P85QsO1sjz4915ZOPbsh9VR3x3dS+pKM+LCB4sQs2/o8Qy6jES1ZIHckTRNHNBfKeMnzlOPbTZHjJvAJ4B0P8sCpbzKQ" +
                       "M/buRZhpLRsv5Pe9U61UNALSg/Vq98st41WKH35CaLME+dwHvO4a+xCO78GnySjNjCPsCjCqEWHEXtUtbodZsMw4sdje" +
                       "rwfC3LBpPJygjn8pAwyt2LjqRSjtwxqW86AdkkFpAW4qJJ2Uy70onxtY2M97yYXRkizIt6y/sLvkh2mRWW917lUhdTf/" +
                       "M3YaTK6kiQXhWPTX/78U8AtXvhiw07iMRxVwRmHKyAVyI334C3ZKiY0rscRAVwYlrCHFtVcxMQ==",
                Hash   = "QN3IRzvFR9k/yvDfi9ChnQtIHo8No2SZm3iGwwj4NVk=",
                Secret = "Sr/17/6JrtKCP7X/e9c7XIMAdigeI1QO6u43prhnS9wuNralsZhvPnKIc3qL7A2jcgML273TM2blHywbzt6cAqLxjC" +
                         "ntyjSay0FyMnctarY3soCkCZsUynMsPC9g39CTVBCUXbZZ6tWZ8mgQ9WDXeMVTRaLXLBr9EZdICauFGsln/LaopfU9" +
                         "CvdYXQ0PcdhCFNbisuPwXOqd5jUu0x49+sPAc4V68TsnWRUC3CYEhEfqkRmtomM8UV+/JyHk0zYdiRxarGzAXfgdXJ" +
                         "wjfjXARhERA/hZRYKH+w9vsPpZWdqQg7zSi5EU8Fr2Cs3IzAes+txLUekFprWsKff7j21KXg=="
            };

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

            Assert.NotNull(credentials);
            Assert.Equal("TEST", credentials.Nonce);
            Assert.NotNull(credentials.SecureData);
            Assert.NotNull(credentials.SecureData.PersonalDetails);

            Assert.NotNull(credentials.SecureData.PersonalDetails.Data);
            Assert.Equal(
                "v9Hx0oaQHLxyGuZdiYmszC3gTGyfYnc57zWLg+WaMus=",
                credentials.SecureData.PersonalDetails.Data.Secret
                );
            Assert.Equal(
                "IeyXbEWBTXXQYG+O0vv7munuGs0H0S4Jr7jzYV1ltCk=",
                credentials.SecureData.PersonalDetails.Data.DataHash
                );

            Assert.Null(credentials.SecureData.DriverLicense);
            Assert.Null(credentials.SecureData.Address);
            Assert.Null(credentials.SecureData.Passport);
            Assert.Null(credentials.SecureData.BankStatement);
            Assert.Null(credentials.SecureData.IdentityCard);
            Assert.Null(credentials.SecureData.InternalPassport);
            Assert.Null(credentials.SecureData.PassportRegistration);
            Assert.Null(credentials.SecureData.RentalAgreement);
            Assert.Null(credentials.SecureData.TemporaryRegistration);
            Assert.Null(credentials.SecureData.UtilityBill);
        }
        public void Should_Throw_If_Null_Credentials_Hash()
        {
            EncryptedCredentials encryptedCredentials = new EncryptedCredentials
            {
                Data   = "",
                Secret = "",
            };

            IDecrypter decrypter = new Decrypter();

            Exception exception = Assert.ThrowsAny <Exception>(() =>
                                                               decrypter.DecryptCredentials(encryptedCredentials, RSA.Create())
                                                               );

            Assert.Matches(@"^Value cannot be null\.\s+Parameter name: Hash$", exception.Message);
            Assert.IsType <ArgumentNullException>(exception);
        }
        public void Should_decrypt_credentials()
        {
            Update       update       = _classFixture.Entity;
            PassportData passportData = update.Message.PassportData;
            RSA          key          = EncryptionKey.ReadAsRsa();

            IDecrypter decrypter = new Decrypter();

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

            Assert.NotNull(credentials);
            Assert.NotNull(credentials.SecureData);
            Assert.Equal("Test nonce for phone and email", credentials.Nonce);
        }
        public void Should_Throw_If_Invalid_Credentials_Hash_Length(string hash)
        {
            EncryptedCredentials encryptedCredentials = new EncryptedCredentials
            {
                Data   = "dGV4dCBvZiAxNiBjaGFycw==",
                Secret = "",
                Hash   = hash,
            };

            IDecrypter decrypter = new Decrypter();

            Exception exception = Assert.ThrowsAny <Exception>(() =>
                                                               decrypter.DecryptCredentials(encryptedCredentials, RSA.Create())
                                                               );

            Assert.Matches(@"^Hash length is not 32: \d+\.$", exception.Message);
            Assert.IsType <PassportDataDecryptionException>(exception);
        }
        public void Should_Throw_If_Invalid_Credentials_Data_String_Length()
        {
            EncryptedCredentials encryptedCredentials = new EncryptedCredentials
            {
                Data   = "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA==",
                Secret = "",
                Hash   = "",
            };

            IDecrypter decrypter = new Decrypter();

            Exception exception = Assert.ThrowsAny <Exception>(() =>
                                                               decrypter.DecryptCredentials(encryptedCredentials, RSA.Create())
                                                               );

            Assert.Equal("Data length is not divisible by 16: 31.", exception.Message);
            Assert.IsType <PassportDataDecryptionException>(exception);
        }
        public void Should_Throw_If_Empty_Credentials_Data_String()
        {
            EncryptedCredentials encryptedCredentials = new EncryptedCredentials
            {
                Data   = "",
                Secret = "",
                Hash   = "",
            };

            IDecrypter decrypter = new Decrypter();

            Exception exception = Assert.ThrowsAny <Exception>(() =>
                                                               decrypter.DecryptCredentials(encryptedCredentials, RSA.Create())
                                                               );

            Assert.Matches(@"^Data is empty\.\s+Parameter name: Data$", exception.Message);
            Assert.IsType <ArgumentException>(exception);
        }
        public async Task Should_Decrypt_Translation_File()
        {
            Update                   update       = _classFixture.Entity;
            PassportData             passportData = update.Message.PassportData;
            RSA                      key          = EncryptionKey.ReadAsRsa();
            EncryptedPassportElement element      = passportData.Data.Single();

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

            for (int i = 0; i < element.Translation.Length; i++)
            {
                PassportFile    passportFile    = element.Translation[i];
                FileCredentials fileCredentials = credentials.SecureData.DriverLicense.Translation[i];

                byte[] encryptedContent;
                {
                    File encryptedFileInfo = await BotClient.GetFileAsync(passportFile.FileId);

                    Assert.NotEmpty(encryptedFileInfo.FilePath);
                    Assert.NotEmpty(encryptedFileInfo.FileId);
                    Assert.InRange(encryptedFileInfo.FileSize, 1_000, 50_000_000);

                    using (System.IO.MemoryStream stream = new System.IO.MemoryStream(encryptedFileInfo.FileSize))
                    {
                        await BotClient.DownloadFileAsync(encryptedFileInfo.FilePath, stream);

                        encryptedContent = stream.ToArray();
                    }
                }

                byte[] translationContent = decrypter.DecryptFile(
                    encryptedContent,
                    fileCredentials
                    );

                Assert.NotEmpty(translationContent);

                string decryptedFilePath = System.IO.Path.GetTempFileName();
                await System.IO.File.WriteAllBytesAsync(decryptedFilePath, translationContent);

                _output.WriteLine("Translation JPEG file is written to \"{0}\".", decryptedFilePath);
            }
        }
        public async Task Should_Decrypt_Utility_Bill_Element_Translation()
        {
            PassportData             passportData = GetPassportData();
            EncryptedPassportElement billElement  = Assert.Single(passportData.Data, el => el.Type == "utility_bill");

            Assert.NotNull(billElement.Translation);
            PassportFile translationFile = Assert.Single(billElement.Translation);

            Assert.Equal("DgADAQADOwADGV9BRP4b7RLGAtUKAg", translationFile.FileId);
            Assert.InRange(translationFile.FileDate, new DateTime(2018, 8, 30), new DateTime(2018, 8, 31));
            Assert.Equal(0, translationFile.FileSize);

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

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

            byte[] encryptedContent = await System.IO.File.ReadAllBytesAsync("Files/utility_bill-translation.jpg.enc");

            byte[] content = decrypter.DecryptFile(
                encryptedContent,
                translationFileCredentials
                );

            Assert.NotEmpty(content);

            await System.IO.File.WriteAllBytesAsync("Files/utility_bill-translation.jpg", content);

            using (System.IO.MemoryStream
                   encryptedFileStream = new System.IO.MemoryStream(encryptedContent),
                   decryptedFileStream = new System.IO.MemoryStream()
                   )
            {
                await decrypter.DecryptFileAsync(
                    encryptedFileStream,
                    translationFileCredentials,
                    decryptedFileStream
                    );

                Assert.Equal(content, decryptedFileStream.ToArray());
            }
        }
        public void Should_Decrypt_Element_Document()
        {
            PassportData passportData = GetPassportData();

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

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

            IdDocumentData documentData = decrypter.DecryptData <IdDocumentData>(
                idCardEl.Data,
                credentials.SecureData.IdentityCard.Data
                );

            Assert.Equal("9999R", documentData.DocumentNo);
            Assert.Empty(documentData.ExpiryDate);
            Assert.Null(documentData.Expiry);
        }
        public async Task Should_Decrypt_Selfie_File()
        {
            Update                   update       = _classFixture.Entity;
            PassportData             passportData = update.Message.PassportData;
            RSA                      key          = EncryptionKey.ReadAsRsa();
            EncryptedPassportElement element      = passportData.Data.Single();
            IDecrypter               decrypter    = new Decrypter();
            Credentials              credentials  = decrypter.DecryptCredentials(passportData.Credentials, key);

            byte[] encryptedContent;
            {
                File encryptedFileInfo = await BotClient.GetFileAsync(element.Selfie.FileId);

                Assert.NotEmpty(encryptedFileInfo.FilePath);
                Assert.NotEmpty(encryptedFileInfo.FileId);
                Assert.InRange(encryptedFileInfo.FileSize, 1_000, 50_000_000);

                using (System.IO.MemoryStream stream = new System.IO.MemoryStream(encryptedFileInfo.FileSize))
                {
                    await BotClient.DownloadFileAsync(encryptedFileInfo.FilePath, stream);

                    encryptedContent = stream.ToArray();
                }
            }

            byte[] selfieContent = decrypter.DecryptFile(
                encryptedContent,
                credentials.SecureData.DriverLicense.Selfie
                );

            Assert.NotEmpty(selfieContent);

            using (System.IO.Stream stream = new System.IO.MemoryStream(selfieContent))
            {
                await BotClient.SendPhotoAsync(
                    _fixture.SupergroupChat,
                    stream,
                    "selfie with driver license",
                    replyToMessageId : update.Message.MessageId
                    );
            }
        }
コード例 #25
0
        public void Should_decrypt_credentials()
        {
            PassportData passportData = GetPassportData();

            IDecrypter decrypter = new Decrypter();

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

            Assert.NotNull(credentials);
            Assert.NotNull(credentials.SecureData);
            Assert.NotEmpty(credentials.Nonce);
            Assert.Equal("TEST", credentials.Nonce);

            // 'address' element decryption needs accompanying DataCredentials
            Assert.NotNull(credentials.SecureData.Address);
            Assert.NotNull(credentials.SecureData.Address.Data);
            Assert.NotEmpty(credentials.SecureData.Address.Data.Secret);
            Assert.NotEmpty(credentials.SecureData.Address.Data.DataHash);
        }
        public void Should_Decrypt_Data()
        {
            Update                   update       = _classFixture.Entity;
            PassportData             passportData = update.Message.PassportData;
            EncryptedPassportElement element      = passportData.Data.Single();

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

            ResidentialAddress residentialAddress = decrypter.DecryptData <ResidentialAddress>(
                element.Data,
                credentials.SecureData.Address.Data
                );

            Assert.NotNull(residentialAddress);
            Assert.NotEmpty(residentialAddress.StreetLine1);
            Assert.NotEmpty(residentialAddress.City);
            Assert.NotEmpty(residentialAddress.PostCode);
            Assert.Equal(2, residentialAddress.CountryCode.Length);
        }
コード例 #27
0
        public async Task Should_Decrypt_Selfie_File()
        {
            PassportData passportData = GetPassportData();

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

            using (System.IO.Stream
                   encryptedFileStream = System.IO.File.OpenRead("Files/driver_license-selfie.jpg.enc"),
                   decryptedFileStream = System.IO.File.OpenWrite("Files/driver_license-selfie.jpg")
                   )
            {
                await decrypter.DecryptFileAsync(
                    encryptedFileStream,
                    credentials.SecureData.DriverLicense.Selfie,
                    decryptedFileStream
                    );
            }
        }
コード例 #28
0
        public void Should_Decrypt_Document_Data()
        {
            PassportData passportData = GetPassportData();

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

            EncryptedPassportElement licenseEl = Assert.Single(passportData.Data, el => el.Type == "driver_license");

            IdDocumentData licenseDoc = decrypter.DecryptData <IdDocumentData>(
                encryptedData: licenseEl.Data,
                dataCredentials: credentials.SecureData.DriverLicense.Data
                );

            Assert.Equal("G544-061", licenseDoc.DocumentNo);
            Assert.Equal("26.11.2022", licenseDoc.ExpiryDate);
            Assert.NotNull(licenseDoc.Expiry);
            Assert.InRange(licenseDoc.Expiry.Value, new DateTime(2022, 11, 26), new DateTime(2022, 11, 26, 0, 0, 1));
        }
        public void Should_Throw_Decrypting_Unencrypted_Credentials_Data_Invalid_Length()
        {
            EncryptedCredentials encryptedCredentials = new EncryptedCredentials
            {
                Data   = "dGV4dCBvZiAxNiBjaGFycw==", // unencrypted data
                Hash   = "QN3IRzvFR9k/yvDfi9ChnQtIHo8No2SZm3iGwwj4NVk=",
                Secret = "Sr/17/6JrtKCP7X/e9c7XIMAdigeI1QO6u43prhnS9wuNralsZhvPnKIc3qL7A2jcgML273TM2blHywbzt6cAqLxjC" +
                         "ntyjSay0FyMnctarY3soCkCZsUynMsPC9g39CTVBCUXbZZ6tWZ8mgQ9WDXeMVTRaLXLBr9EZdICauFGsln/LaopfU9" +
                         "CvdYXQ0PcdhCFNbisuPwXOqd5jUu0x49+sPAc4V68TsnWRUC3CYEhEfqkRmtomM8UV+/JyHk0zYdiRxarGzAXfgdXJ" +
                         "wjfjXARhERA/hZRYKH+w9vsPpZWdqQg7zSi5EU8Fr2Cs3IzAes+txLUekFprWsKff7j21KXg=="
            };

            IDecrypter decrypter = new Decrypter();

            Exception exception = Assert.ThrowsAny <Exception>(() =>
                                                               decrypter.DecryptCredentials(encryptedCredentials, EncryptionKey.RsaPrivateKey)
                                                               );

            Assert.Matches(@"^Data hash mismatch at position \d+\.$", exception.Message);
            Assert.IsType <PassportDataDecryptionException>(exception);
        }
コード例 #30
0
        public void Should_Decrypt_Credentials()
        {
            PassportData passportData = GetPassportData();

            IDecrypter decrypter = new Decrypter();

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

            Assert.NotNull(credentials);
            Assert.NotNull(credentials.SecureData);
            Assert.NotEmpty(credentials.Nonce);
            Assert.Equal("TEST", credentials.Nonce);

            // decryption of document data in 'driver_license' element requires accompanying DataCredentials
            Assert.NotNull(credentials.SecureData.DriverLicense);
            Assert.NotNull(credentials.SecureData.DriverLicense.Data);
            Assert.NotEmpty(credentials.SecureData.DriverLicense.Data.Secret);
            Assert.NotEmpty(credentials.SecureData.DriverLicense.Data.DataHash);

            // decryption of front side file in 'driver_license' element requires accompanying FileCredentials
            Assert.NotNull(credentials.SecureData.DriverLicense.FrontSide);
            Assert.NotEmpty(credentials.SecureData.DriverLicense.FrontSide.Secret);
            Assert.NotEmpty(credentials.SecureData.DriverLicense.FrontSide.FileHash);

            // decryption of selfie file in 'driver_license' element requires accompanying FileCredentials
            Assert.NotNull(credentials.SecureData.DriverLicense.Selfie);
            Assert.NotEmpty(credentials.SecureData.DriverLicense.Selfie.Secret);
            Assert.NotEmpty(credentials.SecureData.DriverLicense.Selfie.FileHash);

            // decryption of translation file in 'driver_license' element requires accompanying FileCredentials
            Assert.NotEmpty(credentials.SecureData.DriverLicense.Translation);
            FileCredentials translationFileCredentials = Assert.Single(
                credentials.SecureData.DriverLicense.Translation
                );

            Assert.NotNull(translationFileCredentials);
            Assert.NotEmpty(translationFileCredentials.Secret);
            Assert.NotEmpty(translationFileCredentials.FileHash);
        }