public async Task Should_Decrypt_From_Seekable_Stream()
        {
            FileCredentials fileCredentials = new FileCredentials
            {
                FileHash = "v3q47iscI6TS94CMo7HGQUOxw28LIf82NJBkImzP57c=",
                Secret   = "vF7nut7clg/H/pEaTJigo4mQJ0s8B+HGCWKTWtOTIdo=",
            };

            IDecrypter decrypter = new Decrypter();

            Stream encContentStream = new MemoryStream();

            using (Stream encFileStream = new NonSeekableFileReadStream("Files/s_dec1.driver_license-selfie.jpg.enc"))
            {
                await encFileStream.CopyToAsync(encContentStream);
            }

            using (encContentStream)
                using (Stream contentStream = new MemoryStream())
                {
                    encContentStream.Position = 0; // Ensure method starts reading the content fro the beginning

                    await decrypter.DecryptFileAsync(
                        encContentStream,
                        fileCredentials,
                        contentStream
                        );
                }
        }
Ejemplo n.º 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());
            }
        }
        public async Task Should_Throw_Decrypting_Unencrypted_File()
        {
            FileCredentials fileCredentials = new FileCredentials
            {
                FileHash = "v3q47iscI6TS94CMo7HGQUOxw28LIf82NJBkImzP57c=",
                Secret   = "vF7nut7clg/H/pEaTJigo4mQJ0s8B+HGCWKTWtOTIdo=",
            };

            IDecrypter decrypter = new Decrypter();

            Exception exception;

            using (Stream
                   encFileStream = new NonSeekableFileReadStream("Files/s_dec3.driver_license-selfie.jpg"),
                   fileStream = new MemoryStream()
                   )
            {
                exception = await Assert.ThrowsAnyAsync <Exception>(() =>
                                                                    decrypter.DecryptFileAsync(
                                                                        encFileStream,
                                                                        fileCredentials,
                                                                        fileStream
                                                                        )
                                                                    );
            }

            Assert.Equal("The input data is not a complete block.", exception.Message);
            Assert.IsType <CryptographicException>(exception);
        }
        public async Task Should_Throw_Decrypting_Stream_With_Wrong_FileCredentials()
        {
            FileCredentials wrongFileCredentials = new FileCredentials
            {
                FileHash = "THTjgv2FU7kff/29Vty/IcqKPmOGkL7F35fAzmkfZdI=",
                Secret   = "a+jxJoKPEaz77VCjRvDVcYHfIO3+h+oI+ruZh+KkYa0=",
            };

            IDecrypter decrypter = new Decrypter();

            Exception exception;

            using (Stream
                   encFileStream = new NonSeekableFileReadStream("Files/s_dec5.driver_license-selfie.jpg.enc"),
                   fileStream = new MemoryStream()
                   )
            {
                exception = await Assert.ThrowsAnyAsync <Exception>(() =>
                                                                    decrypter.DecryptFileAsync(
                                                                        encFileStream,
                                                                        wrongFileCredentials,
                                                                        fileStream
                                                                        )
                                                                    );
            }

            Assert.Matches(@"^Data hash mismatch at position \d+\.$", exception.Message);
            Assert.IsType <PassportDataDecryptionException>(exception);
        }
Ejemplo n.º 5
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);
        }
        public async Task Should_Throw_Decrypting_Mispositioned_Stream()
        {
            FileCredentials fileCredentials = new FileCredentials
            {
                FileHash = "v3q47iscI6TS94CMo7HGQUOxw28LIf82NJBkImzP57c=",
                Secret   = "vF7nut7clg/H/pEaTJigo4mQJ0s8B+HGCWKTWtOTIdo=",
            };

            IDecrypter decrypter = new Decrypter();

            Stream encContentStream = new MemoryStream();

            using (Stream encFileStream = new NonSeekableFileReadStream("Files/s_dec4.driver_license-selfie.jpg.enc"))
            {
                await encFileStream.CopyToAsync(encContentStream);
            }

            Exception exception;

            using (encContentStream) // Stream position is at the end and not at 0 position
                using (Stream contentStream = new MemoryStream())
                {
                    exception = await Assert.ThrowsAnyAsync <Exception>(() =>
                                                                        decrypter.DecryptFileAsync(
                                                                            encContentStream,
                                                                            fileCredentials,
                                                                            contentStream
                                                                            )
                                                                        );
                }

            Assert.Matches(@"^Data padding length is invalid: \d+\.", exception.Message);
            Assert.IsType <PassportDataDecryptionException>(exception);
        }
        public async Task Should_Throw_If_Null_Secret()
        {
            IDecrypter decrypter = new Decrypter();
            Exception  exception = await Assert.ThrowsAnyAsync <Exception>(() =>
                                                                           decrypter.DecryptFileAsync(new MemoryStream(), new FileCredentials(), new MemoryStream())
                                                                           );

            Assert.Matches(@"^Value cannot be null\.\s+Parameter name: Secret$", exception.Message);
            Assert.IsType <ArgumentNullException>(exception);
        }
        public async Task Should_Throw_If_Null_EncryptedContent()
        {
            IDecrypter decrypter = new Decrypter();

            Exception exception = await Assert.ThrowsAnyAsync <Exception>(() =>
                                                                          decrypter.DecryptFileAsync(null, null, null)
                                                                          );

            Assert.Matches(@"^Value cannot be null\.\s+Parameter name: encryptedContent$", exception.Message);
            Assert.IsType <ArgumentNullException>(exception);
        }
        public async Task Should_Throw_If_Invalid_Hash(string fileHash)
        {
            FileCredentials fileCredentials = new FileCredentials {
                Secret = "", FileHash = fileHash
            };
            IDecrypter decrypter = new Decrypter();

            await Assert.ThrowsAnyAsync <FormatException>(() =>
                                                          decrypter.DecryptFileAsync(new MemoryStream(new byte[16]), fileCredentials, new MemoryStream())
                                                          );
        }
        public async Task Should_Throw_If_Invalid_Hash_Length(string fileHash)
        {
            FileCredentials fileCredentials = new FileCredentials {
                Secret = "", FileHash = fileHash
            };
            IDecrypter decrypter = new Decrypter();

            Exception exception = await Assert.ThrowsAnyAsync <Exception>(() =>
                                                                          decrypter.DecryptFileAsync(new MemoryStream(new byte[16]), fileCredentials, new MemoryStream())
                                                                          );

            Assert.Matches(@"^Hash length is not 32: \d+\.$", exception.Message);
            Assert.IsType <PassportDataDecryptionException>(exception);
        }
        public async Task Should_Throw_If_Empty_Data_Stream()
        {
            FileCredentials fileCredentials = new FileCredentials {
                Secret = "", FileHash = ""
            };
            IDecrypter decrypter = new Decrypter();

            Exception exception = await Assert.ThrowsAnyAsync <Exception>(() =>
                                                                          decrypter.DecryptFileAsync(new MemoryStream(), fileCredentials, new MemoryStream())
                                                                          );

            Assert.Matches(@"^Stream is empty\.\s+Parameter name: encryptedContent$", exception.Message);
            Assert.IsType <ArgumentException>(exception);
        }
        public async Task Should_Throw_If_Null_Destination()
        {
            IDecrypter      decrypter       = new Decrypter();
            FileCredentials fileCredentials = new FileCredentials {
                Secret = "", FileHash = ""
            };

            Exception exception = await Assert.ThrowsAnyAsync <Exception>(() =>
                                                                          decrypter.DecryptFileAsync(new MemoryStream(), fileCredentials, null)
                                                                          );

            Assert.Matches(@"^Value cannot be null\.\s+Parameter name: destination$", exception.Message);
            Assert.IsType <ArgumentNullException>(exception);
        }
        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 async Task Should_Throw_If_Invalid_Seekable_Data_Stream_Length()
        {
            IDecrypter      decrypter       = new Decrypter();
            FileCredentials fileCredentials = new FileCredentials {
                Secret = "", FileHash = ""
            };

            Exception exception;

            using (Stream encStream = new MemoryStream(new byte[16 - 1]))
            {
                exception = await Assert.ThrowsAnyAsync <Exception>(() =>
                                                                    decrypter.DecryptFileAsync(encStream, fileCredentials, new MemoryStream())
                                                                    );
            }

            Assert.Equal("Data length is not divisible by 16: 15.", exception.Message);
            Assert.IsType <PassportDataDecryptionException>(exception);
        }
        public async Task Should_Throw_If_NonWritable_Destination()
        {
            IDecrypter      decrypter       = new Decrypter();
            FileCredentials fileCredentials = new FileCredentials {
                Secret = "", FileHash = ""
            };

            Exception exception;

            using (Stream destStream = File.OpenRead("Files/s_dec7.driver_license-selfie.jpg"))
            {
                exception = await Assert.ThrowsAnyAsync <Exception>(() =>
                                                                    decrypter.DecryptFileAsync(new MemoryStream(new byte[16]), fileCredentials, destStream)
                                                                    );
            }

            Assert.Matches(@"^Stream does not support writing\.\s+Parameter name: destination$", exception.Message);
            Assert.IsType <ArgumentException>(exception);
        }
Ejemplo n.º 16
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
                    );
            }
        }
        public async Task Should_Decrypt_Identity_Card_Element_Reverse_Side()
        {
            PassportData             passportData = GetPassportData();
            EncryptedPassportElement idCardEl     = Assert.Single(passportData.Data, el => el.Type == "identity_card");

            Assert.NotNull(idCardEl.ReverseSide);
            Assert.Equal("DgADAQADKAADNfRARK9jbzh5AAFqvAI", idCardEl.ReverseSide.FileId);
            Assert.InRange(idCardEl.ReverseSide.FileDate, new DateTime(2018, 8, 30), new DateTime(2018, 8, 31));
            Assert.Equal(0, idCardEl.ReverseSide.FileSize);

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

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

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

            Assert.NotEmpty(content);

            await System.IO.File.WriteAllBytesAsync("Files/identity_card-reverse_side.jpg", encryptedContent);

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

                Assert.Equal(content, decryptedFileStream.ToArray());
            }
        }
        public async Task Should_Throw_If_NonReadable_Data_Stream()
        {
            IDecrypter      decrypter       = new Decrypter();
            FileCredentials fileCredentials = new FileCredentials {
                Secret = "", FileHash = ""
            };

            Exception exception;

            using (Stream encStream = File.OpenWrite("Files/s_dec6.driver_license-selfie.jpg"))
            {
                exception = await Assert.ThrowsAnyAsync <Exception>(() =>
                                                                    decrypter.DecryptFileAsync(encStream, fileCredentials, new MemoryStream())
                                                                    );
            }

            Assert.Matches(
                @"^Stream does not support reading\.\s+Parameter name: encryptedContent$",
                exception.Message
                );
            Assert.IsType <ArgumentException>(exception);
        }
        public async Task Should_Decrypt_From_NonSeekable_Stream()
        {
            FileCredentials fileCredentials = new FileCredentials
            {
                FileHash = "v3q47iscI6TS94CMo7HGQUOxw28LIf82NJBkImzP57c=",
                Secret   = "vF7nut7clg/H/pEaTJigo4mQJ0s8B+HGCWKTWtOTIdo=",
            };

            IDecrypter decrypter = new Decrypter();

            using (Stream
                   encFileStream = new NonSeekableFileReadStream("Files/s_dec2.driver_license-selfie.jpg.enc"),
                   fileStream = new MemoryStream()
                   )
            {
                await decrypter.DecryptFileAsync(
                    encFileStream,
                    fileCredentials,
                    fileStream
                    );
            }
        }
        public async Task Should_Decrypt_Reverse_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
                   encryptedContent = new System.IO.MemoryStream(element.ReverseSide.FileSize),
                   decryptedFile = System.IO.File.OpenWrite(decryptedFilePath)
                   )
            {
                encryptedFileInfo = await BotClient.GetInfoAndDownloadFileAsync(
                    element.ReverseSide.FileId,
                    encryptedContent
                    );

                encryptedContent.Position = 0;

                await decrypter.DecryptFileAsync(
                    encryptedContent,
                    credentials.SecureData.DriverLicense.ReverseSide,
                    decryptedFile
                    );
            }

            _output.WriteLine("Reverse 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);
        }