Esempio n. 1
0
        public void SetupPkcsEncryptionProviderTest()
        {
            configuration = Mock.Of <IConfiguration>(c => c.Get <int>("SaltLengthInBytes") == 100 &&
                                                     c.Get <int>("KeyDerivationIterationCount") == 10);

            secureRandom            = new Mock <SecureRandomGenerator>();
            rsaKeyProvider          = new Mock <IKeyProvider <RsaKey> >();
            asymmetricKeyProvider   = new AsymmetricKeyProvider(new OidToCipherTypeMapper(), new KeyInfoWrapper(), rsaKeyProvider.Object, null, null, null);
            pkcsEncryptionGenerator = new Mock <Pkcs12KeyEncryptionGenerator>();
            pkcsEncryptionGenerator.Setup(e => e.Encrypt(It.IsAny <string>(), It.IsAny <byte[]>(), It.IsAny <int>(), It.IsAny <byte[]>()))
            .Returns <string, byte[], int, byte[]>((password, salt, iterationCount, content) =>
            {
                var generator = new Pkcs12KeyEncryptionGenerator();
                return(generator.Encrypt(password, salt, iterationCount, content));
            });

            aesEncryptionGenerator = new Mock <AesKeyEncryptionGenerator>();
            aesEncryptionGenerator.Setup(e => e.Encrypt(It.IsAny <string>(), It.IsAny <byte[]>(), It.IsAny <int>(), It.IsAny <byte[]>()))
            .Returns <string, byte[], int, byte[]>((password, salt, iterationCount, content) =>
            {
                var generator = new AesKeyEncryptionGenerator();
                return(generator.Encrypt(password, salt, iterationCount, content));
            });

            encryptionProvider = new KeyEncryptionProvider(configuration, secureRandom.Object, asymmetricKeyProvider, pkcsEncryptionGenerator.Object, aesEncryptionGenerator.Object);
        }
Esempio n. 2
0
            public void ShouldThrowExceptionWhenKeyTypeIsNotSupported(CipherType cipherType)
            {
                var typeMapperMock = new Mock <OidToCipherTypeMapper>();

                typeMapperMock.Setup(ctm => ctm.MapOidToCipherType(It.IsAny <string>()))
                .Returns(cipherType);

                keyProvider = new AsymmetricKeyProvider(typeMapperMock.Object, keyInfoWrapper.Object, rsaKeyProvider, dsaKeyProvider, ecKeyProvider, elGamalKeyProvider);
                Assert.Throws <ArgumentException>(() =>
                {
                    keyProvider.GetPublicKey(rsaKeyPair.PublicKey.Content);
                });
            }
        public void SetupFormattingProviderTest()
        {
            var secureRandom = new SecureRandomGenerator();

            rsaKeyProvider = new RsaKeyProvider(new AsymmetricKeyPairGenerator(secureRandom));
            keyPair        = rsaKeyProvider.CreateKeyPair(2048);

            var oidMapper = new OidToCipherTypeMapper();
            var asymmetricKeyConverter = new AsymmetricKeyProvider(oidMapper, new KeyInfoWrapper(), rsaKeyProvider, null, null, null);

            pkcs8PemFormattingProvider = new Pkcs8PemFormattingProvider(asymmetricKeyConverter);

            var configuration = Mock.Of <IConfiguration>(m => m.Get <int>("SaltLengthInBytes") == 100 &&
                                                         m.Get <int>("KeyDerivationIterationCount") == 1);

            var encryptionProvider = new KeyEncryptionProvider(configuration, secureRandom, asymmetricKeyConverter, new Pkcs12KeyEncryptionGenerator(), new AesKeyEncryptionGenerator());

            pkcsEncryptedKey = encryptionProvider.EncryptPrivateKey(keyPair.PrivateKey, "password", EncryptionType.Pkcs);
            aesEncryptedKey  = encryptionProvider.EncryptPrivateKey(keyPair.PrivateKey, "password", EncryptionType.Aes);
        }
Esempio n. 4
0
        public void SetupCreateSignatureTest()
        {
            encoding = new EncodingWrapper();
            var random = new SecureRandomGenerator();

            fileContent = random.NextBytes(10000);

            file       = new Mock <FileWrapper>();
            fileOutput = new Dictionary <string, byte[]>();
            file.Setup(f => f.WriteAllBytes(It.IsAny <string>(), It.IsAny <byte[]>()))
            .Callback <string, byte[]>((path, content) =>
            {
                fileOutput.Add(path, content);
            });

            file.Setup(f => f.ReadAllBytes("foo.file"))
            .Returns(fileContent);

            console       = new Mock <ConsoleWrapper>();
            consoleOutput = new List <string>();

            console.Setup(c => c.WriteLine(It.IsAny <string>()))
            .Callback <string>(input => consoleOutput.Add(input));

            Container container = ContainerProvider.GetContainer();

            container.Register <FileWrapper>(() => file.Object);
            container.Register <ConsoleWrapper>(() => console.Object);

            var asymmetricKeyPairGenerator = new AsymmetricKeyPairGenerator(new SecureRandomGenerator());

            rsaKeyProvider = new RsaKeyProvider(asymmetricKeyPairGenerator);
            dsaKeyProvider = new DsaKeyProvider(asymmetricKeyPairGenerator);
            ecKeyProvider  = new EcKeyProvider(asymmetricKeyPairGenerator, new FieldToCurveNameMapper());

            var asymmetricKeyProvider = new AsymmetricKeyProvider(new OidToCipherTypeMapper(), new KeyInfoWrapper(), rsaKeyProvider, dsaKeyProvider, null, null);

            pkcs8PemFormatter = new Pkcs8PemFormattingProvider(asymmetricKeyProvider);
        }
Esempio n. 5
0
                public void Setup()
                {
                    var asymmetricKeyProvider = new AsymmetricKeyProvider(new OidToCipherTypeMapper(), new KeyInfoWrapper(), null, null, null, null);

                    encryptionProvider = new KeyEncryptionProvider(new PbeConfiguration(), new SecureRandomGenerator(), asymmetricKeyProvider, new Pkcs12KeyEncryptionGenerator(), new AesKeyEncryptionGenerator());
                }
Esempio n. 6
0
 private void SetupValidKeyProvider()
 {
     keyProvider = new AsymmetricKeyProvider(cipherTypeMapper, keyInfoWrapper.Object, rsaKeyProvider, dsaKeyProvider, ecKeyProvider, elGamalKeyProvider);
 }
Esempio n. 7
0
 public void SetupGetEncryptedPrivateKey()
 {
     keyProvider = new AsymmetricKeyProvider(new OidToCipherTypeMapper(), new KeyInfoWrapper(), rsaKeyProvider, dsaKeyProvider, ecKeyProvider, elGamalKeyProvider);
 }