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 ShouldSetDerFormattedContentAsCommandResult(string content)
        {
            decoratedHandler.Setup(dh => dh.Execute(command))
            .Callback <ReadKeyFromFileCommand>(c => c.FileContent = encoding.GetBytes(content));

            decorator.Execute(command);
            Assert.AreEqual(resultingKey, command.Result);
        }
Esempio n. 3
0
        protected void SetupWithRsaKey()
        {
            keyPair = rsaKeyProvider.CreateKeyPair(2048);
            var privateRsaKey = keyPair.PrivateKey;

            privateKey = pkcs8PemFormatter.GetAsPem(privateRsaKey);

            file.Setup(f => f.ReadAllBytes("private.pem"))
            .Returns(encoding.GetBytes(privateKey));
        }
        public void ShouldSetResult()
        {
            var command = new ReadKeyFromFileCommand
            {
                FileContent = encoding.GetBytes("-----BEGIN EC PRIVATE KEY given content")
            };

            decorator.Execute(command);
            Assert.AreEqual(convertedKey, command.Result);
        }
Esempio n. 5
0
        public void Execute(T command)
        {
            var base64Formatted = base64.ToBase64String(command.Out.Content);

            command.FileContent = encoding.GetBytes(base64Formatted);
            decoratedCommandHandler.Execute(command);
        }
Esempio n. 6
0
        public void Execute(T command)
        {
            if (command.ContentType == ContentType.OpenSsh)
            {
                string sshFormattedKey = sshFormattingProvider.GetAsOpenSshPublicKey(command.Out, "openssh-key");
                command.FileContent = encoding.GetBytes(sshFormattedKey);
            }

            decoratedCommand.Execute(command);
        }
        public void Execute(T command)
        {
            if (command.Out.CipherType == CipherType.Ec && command.ContentType == ContentType.Sec1)
            {
                string keyContent = formattingProvider.GetAsPem((IEcKey)command.Out);
                command.FileContent = encoding.GetBytes(keyContent);
            }

            decoratedCommand.Execute(command);
        }
        public void Execute(T command)
        {
            if (command.ContentType == ContentType.Pem)
            {
                string pemFormatted = formattingProvider.GetAsPem(command.Out);
                command.FileContent = encoding.GetBytes(pemFormatted);
            }

            decoratedCommand.Execute(command);
        }
Esempio n. 9
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);
        }
Esempio n. 10
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);
        }
Esempio n. 11
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);
        }
Esempio n. 12
0
        public string GetAsSsh2PublicKey(IAsymmetricKey key, string comment)
        {
            if (key.IsPrivateKey)
            {
                throw new InvalidOperationException("Private key cannot be formatted as SSH2 public key.");
            }

            int commentLength = encoding.GetBytes(comment).Length;

            if (commentLength > 1024)
            {
                throw new ArgumentException("Comment is too long.");
            }

            string contentLine;

            switch (key.CipherType)
            {
            case CipherType.Rsa:
                contentLine = sshKeyProvider.GetRsaPublicKeyContent(key);
                break;

            case CipherType.Dsa:
                contentLine = sshKeyProvider.GetDsaPublicKeyContent(key);
                break;

            case CipherType.Ec:
                VerifyEcCurve(key);
                contentLine = sshKeyProvider.GetEcPublicKeyContent(key);
                break;

            default:
                throw new InvalidOperationException("Cipher type not supported for SSH2 key.");
            }

            string content          = ssh2ContentFormatter.FormatToSsh2KeyContentLength(contentLine);
            string formattedComment = ssh2ContentFormatter.FormatToSsh2HeaderLength(comment);

            return($"---- BEGIN SSH2 PUBLIC KEY ----{Environment.NewLine}" +
                   $"Comment: {formattedComment + Environment.NewLine}" +
                   $"{content + Environment.NewLine}" +
                   "---- END SSH2 PUBLIC KEY ----");
        }
Esempio n. 13
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")))));
        }
Esempio n. 14
0
 public void ShouldInvokeDecoratedHandlerWithOpenSshKey()
 {
     command.ContentType = ContentType.OpenSsh;
     openSshPublicKeyDecorator.Execute(command);
     decoratedCommandHandler.Verify(d => d.Execute(It.Is <WriteFileCommand <IAsymmetricKey> >(c => c.FileContent.SequenceEqual(encoding.GetBytes("openSshFormattedKey")))));
 }
        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")))));
        }
Esempio n. 16
0
        public string GetEcPublicKeyContent(IAsymmetricKey publicKey)
        {
            var ecKey = (IEcKey)publicKey;

            if (ecKey.IsCurve25519)
            {
                throw new InvalidOperationException("Curve25519 must be formatted in Edwards form when converted to Ssh key.");
            }

            var keyParameters = (ECPublicKeyParameters)PublicKeyFactory.CreateKey(publicKey.Content);

            string curve = sshSupportedCurves.Single(c => c.Contains(ecKey.Curve)).First();

            byte[] identifier = encoding.GetBytes(sshCurveHeaders[curve]);
            byte[] header     = encoding.GetBytes(sshCurveIdentifiers[curve]);
            byte[] q          = keyParameters.Q.GetEncoded(false);

            using (var stream = new MemoryStream())
            {
                stream.Write(LengthAsBytes(identifier.Length), 0, 4);
                stream.Write(identifier, 0, identifier.Length);
                stream.Write(LengthAsBytes(header.Length), 0, 4);
                stream.Write(header, 0, header.Length);
                stream.Write(LengthAsBytes(q.Length), 0, 4);
                stream.Write(q, 0, q.Length);
                return(base64.ToBase64String(stream.ToArray()));
            }
        }
Esempio n. 17
0
 public void ShouldSetFileContentToFromFileFieldOfGivenCommand()
 {
     Assert.AreEqual(encoding.GetBytes("file content"), command.FileContent);
 }