Beispiel #1
0
        public void ShouldSetPublicKeyAsResultWhenWhenPublicKeyIsRead()
        {
            var command = new ReadKeyFromFileCommand();

            decorator.Execute(command);
            Assert.AreSame(publicKey, command.Result);
        }
        public void ConvertKeyPair(ApplicationArguments arguments)
        {
            if (arguments.ContentType == ContentType.NotSpecified)
            {
                throw new ArgumentException("Target type for key conversion was not specified.");
            }

            if (!arguments.HasPrivateKey && !arguments.HasPublicKey)
            {
                throw new ArgumentException("No keys were specified for conversion.");
            }

            if (arguments.HasPublicKey)
            {
                ReadKeyFromFileCommand readPublicKeyFromFile = fileCommandProvider.GetReadPublicKeyFromFileCommand(arguments.PublicKeyPath);
                ConvertKey(readPublicKeyFromFile, arguments);
            }

            if (!arguments.HasPrivateKey)
            {
                return;
            }

            ReadKeyFromFileCommand readPrivateKeyFromFile = fileCommandProvider.GetReadPrivateKeyFromFileCommand(arguments.PrivateKeyPath, arguments.Password);

            ConvertKey(readPrivateKeyFromFile, arguments);
        }
Beispiel #3
0
 public void SetupPkcsKeyDecryptionDecoratorTest()
 {
     decoratedCommandHandler = new Mock <ICommandHandler <ReadKeyFromFileCommand> >();
     keyEncryptionProvider   = new Mock <IKeyEncryptionProvider>();
     decorator = new PkcsKeyDecryptionDecorator <ReadKeyFromFileCommand>(decoratedCommandHandler.Object, keyEncryptionProvider.Object);
     command   = new ReadKeyFromFileCommand();
 }
        public void CreateSignature(ApplicationArguments arguments)
        {
            ReadKeyFromFileCommand readPrivateKeyFromFile = fileCommandProvider.GetReadPrivateKeyFromFileCommand(arguments.PrivateKeyPath, arguments.Password);

            commandExecutor.Execute(readPrivateKeyFromFile);

            byte[] contentToSign;
            if (arguments.HasFileInput)
            {
                ReadFileCommand <byte[]> readFileToSign = fileCommandProvider.GetReadFileCommand <byte[]>(arguments.FileInput);
                commandExecutor.Execute(readFileToSign);
                contentToSign = readFileToSign.Result;
            }
            else
            {
                contentToSign = encoding.GetBytes(arguments.Input);
            }

            CreateSignatureCommand createSignature = signatureCommandProvider.GetCreateSignatureCommand(readPrivateKeyFromFile.Result, contentToSign);

            commandExecutor.Execute(createSignature);

            if (arguments.HasFileOutput)
            {
                WriteFileCommand <Signature> writeSignatureTofile = fileCommandProvider.GetWriteToFileCommand(createSignature.Result, arguments.FileOutput);
                commandExecutor.Execute(writeSignatureTofile);
                return;
            }

            WriteToStdOutCommand <Signature> writeSignatureToStdOut = fileCommandProvider.GetWriteToStdOutCommand <Signature>(createSignature.Result);

            commandExecutor.Execute(writeSignatureToStdOut);
        }
Beispiel #5
0
        public void ShouldSetOriginalContentType()
        {
            var command = new ReadKeyFromFileCommand();

            decorator.Execute(command);
            Assert.AreEqual(ContentType.Der, command.OriginalContentType);
        }
Beispiel #6
0
        public void ShouldExecuteDecoratedCommandHandler()
        {
            var command = new ReadKeyFromFileCommand();

            decorator.Execute(command);

            decoratedHandler.Verify(d => d.Execute(command));
        }
            public void FilePathIsNull()
            {
                var command = new ReadKeyFromFileCommand();

                Assert.Throws <ArgumentException>(() =>
                {
                    decorator.Execute(command);
                });
            }
Beispiel #8
0
        public void ShouldSetPrivateKeyAsResultWhenPrivateKeyIsRead()
        {
            var command = new ReadKeyFromFileCommand
            {
                IsPrivateKey = true
            };

            decorator.Execute(command);
            Assert.AreSame(privateKey, command.Result);
        }
        public void ShouldInvokeDecoratedCommandHandler()
        {
            var command = new ReadKeyFromFileCommand
            {
                Result = Mock.Of <IEcKey>()
            };

            decorator.Execute(command);
            decoratedCommandHandler.Verify(dch => dch.Execute(command));
        }
        public void ShouldSetResult()
        {
            var command = new ReadKeyFromFileCommand
            {
                FileContent = encoding.GetBytes("-----BEGIN EC PRIVATE KEY given content")
            };

            decorator.Execute(command);
            Assert.AreEqual(convertedKey, command.Result);
        }
        public void ShouldSetOriginalContentType()
        {
            var command = new ReadKeyFromFileCommand
            {
                FileContent = encoding.GetBytes("-----BEGIN EC PRIVATE KEY given content")
            };

            decorator.Execute(command);
            Assert.AreEqual(ContentType.Sec1, command.OriginalContentType);
        }
        public void ShouldNotSetResultWhenKeyIsNotEcSec1Formatted()
        {
            var command = new ReadKeyFromFileCommand
            {
                FileContent = encoding.GetBytes("-----BEGIN PRIVATE KEY given content")
            };

            decorator.Execute(command);
            Assert.IsNull(command.Result);
        }
        public void ShouldIndicatePublicKeyInExceptionWhenCommandIsNotForPrivateKey()
        {
            var command   = new ReadKeyFromFileCommand();
            var exception = Assert.Throws <ArgumentException>(() =>
            {
                decorator.Execute(command);
            });

            Assert.AreEqual("Public key file or path is required.", exception.Message);
        }
        public void ShouldExecuteDecoratedHandler()
        {
            var command = new ReadKeyFromFileCommand
            {
                FilePath = "foo"
            };

            decorator.Execute(command);
            decoratedHandler.Verify(h => h.Execute(command));
        }
Beispiel #15
0
        public void ShouldNotSetOriginalContentTypeWhenResultIsNotSet()
        {
            var command = new ReadKeyFromFileCommand
            {
                Result = Mock.Of <IAsymmetricKey>()
            };

            decorator.Execute(command);
            Assert.AreEqual(ContentType.NotSpecified, command.OriginalContentType);
        }
        public void VerifyKeyPair(ApplicationArguments arguments)
        {
            ReadKeyFromFileCommand readPublicKeyFromFile  = fileCommandProvider.GetReadPublicKeyFromFileCommand(arguments.PublicKeyPath);
            ReadKeyFromFileCommand readPrivateKeyFromFile = fileCommandProvider.GetReadPrivateKeyFromFileCommand(arguments.PrivateKeyPath, arguments.Password);

            commandExecutor.ExecuteSequence(new [] { readPrivateKeyFromFile, readPublicKeyFromFile });

            IVerifyKeyPairCommand verifyKeyPairCommand = keyCommandProvider.GetVerifyKeyPairCommand(readPublicKeyFromFile.Result, readPrivateKeyFromFile.Result);

            commandExecutor.Execute(verifyKeyPairCommand);
        }
Beispiel #17
0
        public void ShouldNotSetResultWhenItExists()
        {
            var key     = Mock.Of <IAsymmetricKey>();
            var command = new ReadKeyFromFileCommand
            {
                Result = key
            };

            decorator.Execute(command);
            Assert.AreSame(key, command.Result);
        }
        public void ShouldNotSetOriginalContentTypeWhenKeyIsNotEcSec1Formatted()
        {
            var command = new ReadKeyFromFileCommand
            {
                FileContent         = encoding.GetBytes("-----BEGIN PRIVATE KEY given content"),
                OriginalContentType = ContentType.Der
            };

            decorator.Execute(command);
            Assert.AreEqual(ContentType.Der, command.OriginalContentType);
        }
            public void FilePathIsWhiteSpace()
            {
                var command = new ReadKeyFromFileCommand
                {
                    FilePath = "   "
                };

                Assert.Throws <ArgumentException>(() =>
                {
                    decorator.Execute(command);
                });
            }
Beispiel #20
0
        public void ShouldSetEncryptedPrivateKeyAsResultWhenReadingPrivateKeyCausesCryptographicException()
        {
            asymmetricKeyProvider.Setup(akp => akp.GetPrivateKey(It.IsAny <byte[]>()))
            .Throws <CryptographicException>();

            var command = new ReadKeyFromFileCommand
            {
                IsPrivateKey = true
            };

            decorator.Execute(command);
            Assert.AreSame(encryptedPrivateKey, command.Result);
        }
Beispiel #21
0
        public void Setup()
        {
            encoding = new EncodingWrapper();
            file     = new Mock <FileWrapper>();
            file.Setup(f => f.ReadAllBytes("fromFile"))
            .Returns(encoding.GetBytes("file content"));

            commandHandler = new ReadKeyFromFileCommandHandler(file.Object);
            command        = new ReadKeyFromFileCommand
            {
                FilePath = "fromFile"
            };

            commandHandler.Execute(command);
        }
        public void Setup()
        {
            encoding           = new EncodingWrapper();
            decoratedHandler   = new Mock <ICommandHandler <ReadKeyFromFileCommand> >();
            formattingProvider = new Mock <IPemFormattingProvider <IAsymmetricKey> >();
            decorator          = new Pkcs8PemReadFormattingDecorator <ReadKeyFromFileCommand>(decoratedHandler.Object, formattingProvider.Object, encoding);

            resultingKey = new EncryptedKey(null, CipherType.Pkcs12Encrypted);

            formattingProvider.Setup(fp => fp.GetAsDer("-----BEGIN PRIVATE fileContent"))
            .Returns(resultingKey);
            formattingProvider.Setup(fp => fp.GetAsDer("-----BEGIN PUBLIC fileContent"))
            .Returns(resultingKey);
            formattingProvider.Setup(fp => fp.GetAsDer("-----BEGIN ENCRYPTED fileContent"))
            .Returns(resultingKey);

            command = new ReadKeyFromFileCommand();
        }
Beispiel #23
0
        public void Setup()
        {
            encoding           = new EncodingWrapper();
            decoratedHandler   = new Mock <ICommandHandler <ReadKeyFromFileCommand> >();
            formattingProvider = new Mock <ISshFormattingProvider>();
            decorator          = new SshReadFormattingDecorator <ReadKeyFromFileCommand>(decoratedHandler.Object, formattingProvider.Object, encoding);

            key     = Mock.Of <IAsymmetricKey>();
            command = new ReadKeyFromFileCommand();

            decoratedHandler.Setup(dh => dh.Execute(command))
            .Callback <ReadKeyFromFileCommand>(c => c.FileContent = encoding.GetBytes("foobar"));

            formattingProvider.Setup(fp => fp.IsSshKey("foobar"))
            .Returns(true);
            formattingProvider.Setup(fp => fp.GetAsDer("foobar"))
            .Returns(key);
        }
        private void ConvertKey(ReadKeyFromFileCommand readKeyFromFile, ApplicationArguments arguments)
        {
            commandExecutor.Execute(readKeyFromFile);
            string fileExtension = arguments.ContentType
                                   .ToString()
                                   .ToLower();

            if (readKeyFromFile.OriginalContentType == arguments.ContentType)
            {
                throw new InvalidOperationException($"The given key {readKeyFromFile.FilePath} is already in {fileExtension} format.");
            }

            if (arguments.IsContentTypeSsh && readKeyFromFile.Result.IsPrivateKey)
            {
                throw new InvalidOperationException("Private key cannot be converted to SSH format.");
            }

            string keyPath = $"{readKeyFromFile.FilePath}.{fileExtension}";
            WriteFileCommand <IAsymmetricKey> writeKeyToFile = fileCommandProvider.GetWriteKeyToFileCommand(readKeyFromFile.Result, keyPath, arguments.ContentType, readKeyFromFile.OriginalEncryptionType, arguments.Password);

            commandExecutor.Execute(writeKeyToFile);
        }
        public void VerifySignature(ApplicationArguments arguments)
        {
            ReadKeyFromFileCommand readPublicKeyFromFile = fileCommandProvider.GetReadPublicKeyFromFileCommand(arguments.PublicKeyPath);

            commandExecutor.Execute(readPublicKeyFromFile);

            byte[] contentToVerify;
            if (arguments.HasFileInput)
            {
                ReadFileCommand <byte[]> readFileToVerify = fileCommandProvider.GetReadFileCommand <byte[]>(arguments.FileInput);
                commandExecutor.Execute(readFileToVerify);
                contentToVerify = readFileToVerify.Result;
            }
            else
            {
                contentToVerify = encoding.GetBytes(arguments.Input);
            }

            byte[] signatureToVerify;
            if (base64.IsBase64(arguments.Signature))
            {
                signatureToVerify = base64.FromBase64String(arguments.Signature);
            }
            else
            {
                ReadFileCommand <byte[]> readSignatureToVerify = fileCommandProvider.GetReadFileCommand <byte[]>(arguments.Signature);
                commandExecutor.Execute(readSignatureToVerify);

                string base64Signature = encoding.GetString(readSignatureToVerify.Result);
                signatureToVerify = base64.FromBase64String(base64Signature);
            }

            VerifySignatureCommand verifySignature = signatureCommandProvider.GetVerifySignatureCommand(readPublicKeyFromFile.Result, contentToVerify, signatureToVerify);

            commandExecutor.Execute(verifySignature);
        }
Beispiel #26
0
 public void Setup()
 {
     result = provider.GetReadPublicKeyFromFileCommand("foo");
 }
Beispiel #27
0
 public void Setup()
 {
     result = provider.GetReadPrivateKeyFromFileCommand("barPath", "fooPassword");
 }