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);
        }
        public void WriteFileCommand_Verify_1()
        {
            Mock <IIrbisConnection> mock       = GetConnectionMock();
            IIrbisConnection        connection = mock.Object;
            WriteFileCommand        command
                = new WriteFileCommand(connection);

            Assert.IsFalse(command.Verify(false));
        }
        public void WriteFileCommand_Construciton_1()
        {
            Mock <IIrbisConnection> mock       = GetConnectionMock();
            IIrbisConnection        connection = mock.Object;
            WriteFileCommand        command
                = new WriteFileCommand(connection);

            Assert.AreSame(connection, command.Connection);
        }
Exemple #4
0
        public void ShouldExecuteDecoratedCommandHandler()
        {
            var command = new WriteFileCommand <IAsymmetricKey>
            {
                ContentType = ContentType.Sec1
            };

            decorator.Execute(command);
            decoratedCommandHandler.Verify(ch => ch.Execute(command));
        }
        public void Setup()
        {
            decoratedHandler = new Mock <ICommandHandler <WriteFileCommand <IAsymmetricKey> > >();
            decorator        = new WriteKeyToFilePathValidationDecorator <WriteFileCommand <IAsymmetricKey> >(decoratedHandler.Object);

            command = new WriteFileCommand <IAsymmetricKey>
            {
                Out = Mock.Of <IAsymmetricKey>()
            };
        }
Exemple #6
0
        public void ShouldThrowWhenEncryptionIsAppliedToSec1Key()
        {
            var command = new WriteFileCommand <IAsymmetricKey>
            {
                ContentType    = ContentType.Sec1,
                EncryptionType = EncryptionType.Pkcs
            };

            Assert.Throws <InvalidOperationException>(() => decorator.Execute(command));
        }
        public void ShouldThrowWhenSec1IsAppliedForNonEcKey()
        {
            key = Mock.Of <IEcKey>(k => k.CipherType == CipherType.Rsa);
            var command = new WriteFileCommand <IAsymmetricKey>
            {
                Out         = key,
                ContentType = ContentType.Sec1
            };

            Assert.Throws <InvalidOperationException>(() => decorator.Execute(command));
        }
        public void ShouldSetFileContentWhenContentTypeIsSec1AndCipherTypeIsEc()
        {
            var command = new WriteFileCommand <IAsymmetricKey>
            {
                Out         = key,
                ContentType = ContentType.Sec1
            };

            decorator.Execute(command);
            decoratedCommandHandler.Verify(d => d.Execute(It.Is <WriteFileCommand <IAsymmetricKey> >(k => k.FileContent.SequenceEqual(encoding.GetBytes("EcPem")))));
        }
Exemple #9
0
        public void ShouldInvokeDecoratedCommandWithEmptyContentWhenContentTypeIsNotPem()
        {
            var command = new WriteFileCommand <IAsymmetricKey>
            {
                Out         = key,
                ContentType = ContentType.Der
            };

            decorator.Execute(command);
            decoratedCommand.Verify(d => d.Execute(It.Is <WriteFileCommand <IAsymmetricKey> >(k => k.FileContent == null)));
        }
        public void ShouldNotSetFileContentWhenContentTypeIsNotPem()
        {
            var command = new WriteFileCommand <IAsymmetricKey>
            {
                Out         = key,
                ContentType = ContentType.Ssh2
            };

            decorator.Execute(command);
            decoratedCommandHandler.Verify(d => d.Execute(It.Is <WriteFileCommand <IAsymmetricKey> >(k => k.FileContent == null)));
        }
Exemple #11
0
        public void ShouldInvokeDecoratedCommandWithPemFormattedContentWhenContentTypeIsPem()
        {
            var command = new WriteFileCommand <IAsymmetricKey>
            {
                Out         = key,
                ContentType = ContentType.Pem
            };

            decorator.Execute(command);
            decoratedCommand.Verify(d => d.Execute(It.Is <WriteFileCommand <IAsymmetricKey> >(k => k.FileContent.SequenceEqual(encoding.GetBytes("pemFormattedFoo")))));
        }
Exemple #12
0
        public void ShouldInvokeDecoratedCommandWithKeyContentWhenContentTypeIsNotSpecified()
        {
            var command = new WriteFileCommand <IAsymmetricKey>
            {
                Out         = key,
                ContentType = ContentType.NotSpecified
            };

            decorator.Execute(command);
            decoratedCommand.Verify(d => d.Execute(It.Is <WriteFileCommand <IAsymmetricKey> >(k => k.FileContent.SequenceEqual(keyContent))));
        }
            public void Setup()
            {
                privateKey          = Mock.Of <IAsymmetricKey>(k => k.IsPrivateKey);
                encryptedPrivateKey = Mock.Of <IAsymmetricKey>(k => k.IsPrivateKey && k.IsEncrypted);

                keyEncryptionProvider.Setup(kep => kep.EncryptPrivateKey(privateKey, "fooPassword", EncryptionType.Aes))
                .Returns(encryptedPrivateKey);

                command = Mock.Of <WriteFileCommand <IAsymmetricKey> >(c => c.EncryptionType == EncryptionType.Aes &&
                                                                       c.Password == "fooPassword" &&
                                                                       c.Out == privateKey);
            }
        public void ShouldNotSetFileContentWhenCipherTypeIsNotEc()
        {
            key = Mock.Of <IEcKey>(k => k.CipherType == CipherType.Rsa);
            var command = new WriteFileCommand <IAsymmetricKey>
            {
                Out         = key,
                ContentType = ContentType.Sec1
            };

            decorator.Execute(command);
            decoratedCommandHandler.Verify(d => d.Execute(It.Is <WriteFileCommand <IAsymmetricKey> >(k => k.FileContent == null)));
        }
        public void ShouldExecuteDecoratedCommandHandler()
        {
            key = Mock.Of <IEcKey>(k => k.CipherType == CipherType.Ec);
            var command = new WriteFileCommand <IAsymmetricKey>
            {
                Out         = key,
                ContentType = ContentType.Sec1
            };

            decorator.Execute(command);
            decoratedCommandHandler.Verify(ch => ch.Execute(command));
        }
Exemple #16
0
        public void ShouldSetSec1FormattedPrivateKeyContentToCommand()
        {
            var key     = Mock.Of <IEcKey>(k => k.CipherType == CipherType.Ec && k.IsPrivateKey);
            var command = new WriteFileCommand <IAsymmetricKey>
            {
                Out         = key,
                ContentType = ContentType.Sec1
            };

            decorator.Execute(command);
            decoratedCommandHandler.Verify(dc => dc.Execute(It.Is <WriteFileCommand <IAsymmetricKey> >(c => c.Out == formattedKey)));
        }
Exemple #17
0
        public void ShouldNotSetSec1ContentWhenContentTypeIsNotSec1()
        {
            var key     = Mock.Of <IAsymmetricKey>(k => k.CipherType == CipherType.Ec && k.IsPrivateKey);
            var command = new WriteFileCommand <IAsymmetricKey>
            {
                Out         = key,
                ContentType = ContentType.Pem
            };

            decorator.Execute(command);
            decoratedCommandHandler.Verify(dc => dc.Execute(It.Is <WriteFileCommand <IAsymmetricKey> >(c => c.Out == key)));
        }
        public void WriteFile(TsFile file)
        {
            var astPayload = JsonConvert.SerializeObject(file.Statements.ToArray());

            var command = new WriteFileCommand()
            {
                AstPayload = astPayload,
                FileName   = file.FileName,
                Path       = file.Path
            };

            this.ExecuteCommand(command);
        }
Exemple #19
0
        public void SetupSshWriteFormattingDecoratorTest()
        {
            decoratedCommandHandler = new Mock <ICommandHandler <WriteFileCommand <IAsymmetricKey> > >();
            formattingProvider      = new Mock <ISshFormattingProvider>();
            encoding = new EncodingWrapper();
            openSshPublicKeyDecorator = new OpenSshPublicKeyWriteFormattingDecorator <WriteFileCommand <IAsymmetricKey> >(decoratedCommandHandler.Object, formattingProvider.Object, encoding);

            key     = Mock.Of <IAsymmetricKey>(k => k.Content == new byte[] { 0x07 });
            command = new WriteFileCommand <IAsymmetricKey>
            {
                Out = key
            };

            formattingProvider.Setup(fp => fp.GetAsOpenSshPublicKey(key, "openssh-key")).Returns("openSshFormattedKey");
        }
Exemple #20
0
        public void SetupSshWriteFormattingDecoratorTest()
        {
            decoratedCommandHandler = new Mock <ICommandHandler <WriteFileCommand <IAsymmetricKeyPair> > >();
            formattingProvider      = new Mock <ISshFormattingProvider>();
            encoding = new EncodingWrapper();
            openSshPrivateKeyDecorator = new OpenSshPrivateKeyWriteFormattingDecorator <WriteFileCommand <IAsymmetricKeyPair> >(decoratedCommandHandler.Object, formattingProvider.Object, encoding);

            keyPair = Mock.Of <IAsymmetricKeyPair>();
            command = new WriteFileCommand <IAsymmetricKeyPair>
            {
                Out = keyPair
            };

            formattingProvider.Setup(fp => fp.GetAsOpenSshPrivateKey(keyPair, "openssh-key")).Returns("openSshFormattedKey");
        }
Exemple #21
0
        public void Setup()
        {
            file           = new Mock <FileWrapper>();
            commandHandler = new WriteToFileCommandHandler <object>(file.Object);

            var encoding = new EncodingWrapper();

            fileContent = encoding.GetBytes("barContent");

            var command = new WriteFileCommand <object>
            {
                FilePath    = "fooDestination",
                FileContent = fileContent
            };

            commandHandler.Execute(command);
        }
        public void CreateKeyPair(ApplicationArguments arguments)
        {
            ICreateAsymmetricKeyCommand createKeyCommand;

            switch (arguments.KeyType)
            {
            case CipherType.Rsa:
                createKeyCommand = keyCommandProvider.GetCreateKeyCommand <RsaKey>(arguments.KeySize);
                break;

            case CipherType.Dsa:
                createKeyCommand = keyCommandProvider.GetCreateKeyCommand <DsaKey>(arguments.KeySize);
                break;

            case CipherType.Ec:
                createKeyCommand = keyCommandProvider.GetCreateKeyCommand <EcKey>(arguments.Curve);
                break;

            case CipherType.ElGamal:
                createKeyCommand = keyCommandProvider.GetCreateKeyCommand <ElGamalKey>(arguments.KeySize, arguments.UseRfc3526Prime);
                break;

            default:
                throw new ArgumentException("Key type not supported.");
            }

            commandExecutor.Execute(createKeyCommand);

            if (createKeyCommand.Curve == "curve25519" && arguments.ContentType == ContentType.OpenSsh)
            {
                WriteFileCommand <IAsymmetricKeyPair> writeOpenSshCurve25519PrivateKey = fileCommandProvider.GetWriteToFileCommand <IAsymmetricKeyPair>(createKeyCommand.Result, arguments.PrivateKeyPath, arguments.ContentType, arguments.EncryptionType, arguments.Password);
                WriteFileCommand <IAsymmetricKey>     writeOpenSshCurve25519PublicKey  = fileCommandProvider.GetWriteKeyToFileCommand(createKeyCommand.Result.PrivateKey, arguments.PublicKeyPath, arguments.ContentType);
                commandExecutor.Execute(writeOpenSshCurve25519PrivateKey);
                commandExecutor.Execute(writeOpenSshCurve25519PublicKey);
                return;
            }

            WriteFileCommand <IAsymmetricKey> writePublicKeyToFile = fileCommandProvider.GetWriteKeyToFileCommand(createKeyCommand.Result.PublicKey, arguments.PublicKeyPath, arguments.ContentType);

            commandExecutor.Execute(writePublicKeyToFile);

            WriteFileCommand <IAsymmetricKey> writePrivateKeyToFile = fileCommandProvider.GetWriteKeyToFileCommand(createKeyCommand.Result.PrivateKey, arguments.PrivateKeyPath, arguments.ContentType, arguments.EncryptionType, arguments.Password);

            commandExecutor.Execute(writePrivateKeyToFile);
        }
        public void Setup()
        {
            decoratedCommandHandler = new Mock <ICommandHandler <WriteFileCommand <Signature> > >();
            base64    = new Base64Wrapper();
            decorator = new WriteToFileBase64FormattingDecorator <WriteFileCommand <Signature> >(decoratedCommandHandler.Object, base64, new EncodingWrapper());

            signature = new Signature
            {
                Content = Encoding.Default.GetBytes("foobar")
            };

            command = new WriteFileCommand <Signature>
            {
                Out = signature
            };

            decorator.Execute(command);
        }
        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);
        }
        static void Main(string[] args)
        {
            var fileSystemReceiver = FileSystemReceiverUtil.GetFileSystemReceiver();

            var openFileCommand = new OpenFileCommand(fileSystemReceiver);

            var closeFileCommand = new CloseFileCommand(fileSystemReceiver);

            var readFileCommand = new ReadFileCommand(fileSystemReceiver);

            var writeFileCommand = new WriteFileCommand(fileSystemReceiver);

            var invoker = new FileInvoker();

            invoker.Execute(openFileCommand);

            invoker.Execute(closeFileCommand);

            invoker.Execute(new ICommand[] { openFileCommand, readFileCommand, writeFileCommand, closeFileCommand });

            Console.Read();
        }
        static void CommandTest()
        {
            //Creating the receiver object
            IFileSystemReceiver fs = FileSystemReceiverUtil.getUnderlyingFileSystem();

            //creating command and associating with receiver
            OpenFileCommand openFileCommand = new OpenFileCommand(fs);

            //Creating invoker and associating with Command
            FileInvoker file = new FileInvoker(openFileCommand);

            //perform action on invoker object
            file.execute();

            WriteFileCommand writeFileCommand = new WriteFileCommand(fs);

            file = new FileInvoker(writeFileCommand);
            file.execute();

            CloseFileCommand closeFileCommand = new CloseFileCommand(fs);

            file = new FileInvoker(closeFileCommand);
            file.execute();
        }
Exemple #27
0
 public void ShouldNotMapPrivateKeyContentTypeToPemWhenContentIsOpenSshAndPrivateKeyIsCurve25519()
 {
     key    = Mock.Of <IEcKey>(k => k.IsPrivateKey && k.CipherType == CipherType.Ec && k.IsCurve25519);
     result = provider.GetWriteKeyToFileCommand(key, "key", ContentType.OpenSsh);
     Assert.AreEqual(ContentType.OpenSsh, result.ContentType);
 }
Exemple #28
0
 public void ShouldMapPrivateKeyContentTypeToPemWhenContentTypeForPublicKeyIsSec1()
 {
     key    = Mock.Of <IAsymmetricKey>(k => !k.IsPrivateKey);
     result = provider.GetWriteKeyToFileCommand(key, "key", ContentType.Sec1);
     Assert.AreEqual(ContentType.Pem, result.ContentType);
 }
Exemple #29
0
 public void ShouldMapPrivateKeyContentTypeToPemWhenContentIsOpenSshAndPrivateKeyIsNotEcKey()
 {
     key    = Mock.Of <IAsymmetricKey>(k => k.IsPrivateKey && k.CipherType == CipherType.Rsa);
     result = provider.GetWriteKeyToFileCommand(key, "key", ContentType.OpenSsh);
     Assert.AreEqual(ContentType.Pem, result.ContentType);
 }
Exemple #30
0
 public void Setup()
 {
     signature = Mock.Of <Signature>();
     result    = provider.GetWriteToFileCommand(signature, "signedfile.extension", ContentType.Der, EncryptionType.Pkcs, "foopassword");
 }