public byte[] SignRequest(ISshKey aKey, byte[] aSignData) { BlobBuilder builder = new BlobBuilder(); switch (aKey.Version) { case SshVersion.SSH1: builder.AddBytes(aKey.GetPublicKeyBlob()); var engine = new Pkcs1Encoding(new RsaEngine()); engine.Init(true /* encrypt */, aKey.GetPublicKeyParameters()); var encryptedData = engine.ProcessBlock(aSignData, 0, aSignData.Length); var challenge = new BigInteger(encryptedData); builder.AddSsh1BigIntBlob(challenge); builder.AddBytes(SessionId); builder.AddInt(1); // response type - must be 1 builder.InsertHeader(Agent.Message.SSH1_AGENTC_RSA_CHALLENGE); break; case SshVersion.SSH2: builder.AddBlob(aKey.GetPublicKeyBlob()); builder.AddBlob(aSignData); builder.InsertHeader(Agent.Message.SSH2_AGENTC_SIGN_REQUEST); break; default: throw new Exception(cUnsupportedSshVersion); } BlobParser replyParser = SendMessage(builder); var header = replyParser.ReadHeader(); switch (aKey.Version) { case SshVersion.SSH1: if (header.Message != Agent.Message.SSH1_AGENT_RSA_RESPONSE) { throw new AgentFailureException(); } byte[] response = new byte[16]; for (int i = 0; i < 16; i++) { response[i] = replyParser.ReadUInt8(); } return(response); case SshVersion.SSH2: if (header.Message != Agent.Message.SSH2_AGENT_SIGN_RESPONSE) { throw new AgentFailureException(); } return(replyParser.ReadBlob()); default: throw new Exception(cUnsupportedSshVersion); } }
public static byte[] FormatSignature(this ISshKey key, byte[] signature) { AsymmetricKeyParameter publicKey = key.GetPublicKeyParameters(); if (publicKey is DsaPublicKeyParameters || publicKey is ECPublicKeyParameters) { Asn1Sequence seq = (Asn1Sequence)Asn1Object.FromByteArray(signature); BigInteger r = ((DerInteger)seq[0]).PositiveValue; BigInteger s = ((DerInteger)seq[1]).PositiveValue; BlobBuilder formatedSignature = new BlobBuilder(); if (publicKey is ECPublicKeyParameters) { var bytes = r.ToByteArray().ToList(); while (bytes.Count < 20) { bytes.Insert(0, 0); } formatedSignature.AddBlob(bytes.ToArray()); bytes = s.ToByteArray().ToList(); while (bytes.Count < 20) { bytes.Insert(0, 0); } formatedSignature.AddBlob(bytes.ToArray()); } else { var bytes = r.ToByteArrayUnsigned().ToList(); while (bytes.Count < 20) { bytes.Insert(0, 0); } formatedSignature.AddBytes(bytes.ToArray()); bytes = s.ToByteArrayUnsigned().ToList(); while (bytes.Count < 20) { bytes.Insert(0, 0); } formatedSignature.AddBytes(bytes.ToArray()); } return(formatedSignature.GetBlob()); } else if (publicKey is RsaKeyParameters || publicKey is Ed25519PublicKeyParameter) { return(signature); } throw new Exception("Unsupported algorithm"); }
public void TestAddBytes() { BlobBuilder builder = new BlobBuilder(); byte[] value = { 0, 1, 2, 3, 4, 5 }; builder.AddBytes(value); Assert.That(builder.GetBlob(), Is.EqualTo(value)); }
private BlobBuilder CreatePublicKeyBlob(ISshKey aKey) { var builder = new BlobBuilder(); switch (aKey.Version) { case SshVersion.SSH1: builder.AddBytes(aKey.GetPublicKeyBlob()); break; case SshVersion.SSH2: builder.AddBlob(aKey.GetPublicKeyBlob()); break; } return(builder); }
/// <summary> /// Answers the message. /// </summary> /// <param name='messageStream'>Message stream.</param> /// <param name="process">The calling process or <c>null</c> if the process /// could not be obtained.</param> /// <remarks>code based on winpgnt.c from PuTTY source code</remarks> public void AnswerMessage(Stream messageStream, Process process = null) { if (messageStream.CanTimeout) { messageStream.ReadTimeout = 5000; } var messageParser = new BlobParser(messageStream); var responseBuilder = new BlobBuilder(); BlobHeader header; try { header = messageParser.ReadHeader(); if (MessageReceived != null) { var eventArgs = new MessageReceivedEventArgs(header); MessageReceived(this, eventArgs); if (eventArgs.Fail) { throw new Exception (); } } } catch (Exception) { header = new BlobHeader(); header.Message = Message.UNKNOWN; // this will cause the switch statement below to use the default case // which returns an error to the stream. } switch (header.Message) { case Message.SSH1_AGENTC_REQUEST_RSA_IDENTITIES: /* * Reply with SSH1_AGENT_RSA_IDENTITIES_ANSWER. */ try { if (header.BlobLength > 1) { // ruby net-ssh tries to send a SSH2_AGENT_REQUEST_VERSION message // which has the same id number as SSH1_AGENTC_REQUEST_RSA_IDENTITIES // with a string tacked on. We need to read the string from the // stream, but it is not used for anything. messageParser.ReadString (); } var keyList = ListKeys(SshVersion.SSH1); if (FilterKeyListCallback != null) { keyList = FilterKeyListCallback(keyList); } foreach (SshKey key in keyList) { responseBuilder.AddBytes(key.GetPublicKeyBlob()); responseBuilder.AddStringBlob(key.Comment); } responseBuilder.InsertHeader(Message.SSH1_AGENT_RSA_IDENTITIES_ANSWER, keyList.Count); // TODO may want to check that there is enough room in the message stream break; // succeeded } catch (Exception ex) { Debug.Fail(ex.ToString()); } goto default; // failed case Message.SSH2_AGENTC_REQUEST_IDENTITIES: /* * Reply with SSH2_AGENT_IDENTITIES_ANSWER. */ try { var keyList = ListKeys(SshVersion.SSH2); if (FilterKeyListCallback != null) { keyList = FilterKeyListCallback(keyList); } foreach (SshKey key in keyList) { responseBuilder.AddBlob(key.GetPublicKeyBlob()); responseBuilder.AddStringBlob(key.Comment); } responseBuilder.InsertHeader(Message.SSH2_AGENT_IDENTITIES_ANSWER, keyList.Count); // TODO may want to check that there is enough room in the message stream break; // succeeded } catch (Exception ex) { Debug.Fail(ex.ToString()); } goto default; // failed case Message.SSH1_AGENTC_RSA_CHALLENGE: /* * Reply with either SSH1_AGENT_RSA_RESPONSE or * SSH_AGENT_FAILURE, depending on whether we have that key * or not. */ try { //Reading publicKey information var publicKeyParams = messageParser.ReadSsh1PublicKeyData(true); //Searching for Key here var matchingKey = mKeyList.Where(key => key.Version == SshVersion.SSH1 && (key.GetPublicKeyParameters().Equals(publicKeyParams))).Single(); //Reading challenge var encryptedChallenge = messageParser.ReadSsh1BigIntBlob(); var sessionId = messageParser.ReadBytes(16); //Checking responseType field if (messageParser.ReadInt() != 1) { goto default; //responseType !=1 is not longer supported } //Answering to the challenge var engine = new Pkcs1Encoding(new RsaEngine()); engine.Init(false /* decrypt */, matchingKey.GetPrivateKeyParameters()); var decryptedChallenge = engine.ProcessBlock(encryptedChallenge, 0, encryptedChallenge.Length); using (MD5 md5 = MD5.Create()) { var md5Buffer = new byte[48]; decryptedChallenge.CopyTo(md5Buffer, 0); sessionId.CopyTo(md5Buffer, 32); responseBuilder.AddBytes(md5.ComputeHash(md5Buffer)); responseBuilder.InsertHeader(Message.SSH1_AGENT_RSA_RESPONSE); break; } } catch (InvalidOperationException) { // this is expected if there is not a matching key } catch (Exception ex) { Debug.Fail(ex.ToString()); } goto default; // failed case Message.SSH2_AGENTC_SIGN_REQUEST: /* * Reply with either SSH2_AGENT_SIGN_RESPONSE or SSH_AGENT_FAILURE, * depending on whether we have that key or not. */ try { var keyBlob = messageParser.ReadBlob(); var reqData = messageParser.ReadBlob(); var flags = new SignRequestFlags(); try { // usually, there are no flags, so parser will throw flags = (SignRequestFlags)messageParser.ReadInt(); } catch { } var matchingKey = mKeyList.Where(key => key.Version == SshVersion.SSH2 && key.GetPublicKeyBlob().SequenceEqual(keyBlob)).First(); var confirmConstraints = matchingKey.Constraints .Where(constraint => constraint.Type == KeyConstraintType.SSH_AGENT_CONSTRAIN_CONFIRM); if (confirmConstraints.Count() > 0) { if (!ConfirmUserPermissionCallback.Invoke(matchingKey, process)) { goto default; } } /* create signature */ var signKey = matchingKey; var signer = signKey.GetSigner(); var algName = signKey.Algorithm.GetIdentifierString(); signer.Init(true, signKey.GetPrivateKeyParameters()); signer.BlockUpdate(reqData, 0, reqData.Length); byte[] signature = signer.GenerateSignature(); signature = signKey.FormatSignature(signature); BlobBuilder signatureBuilder = new BlobBuilder(); if (!flags.HasFlag(SignRequestFlags.SSH_AGENT_OLD_SIGNATURE)) { signatureBuilder.AddStringBlob(algName); } signatureBuilder.AddBlob(signature); responseBuilder.AddBlob(signatureBuilder.GetBlob()); responseBuilder.InsertHeader(Message.SSH2_AGENT_SIGN_RESPONSE); try { KeyUsed(this, new KeyUsedEventArgs(signKey, process)); } catch { } break; // succeeded } catch (InvalidOperationException) { // this is expected if there is not a matching key } catch (Exception ex) { Debug.Fail(ex.ToString()); } goto default; // failure case Message.SSH1_AGENTC_ADD_RSA_IDENTITY: case Message.SSH1_AGENTC_ADD_RSA_ID_CONSTRAINED: /* * Add to the list and return SSH_AGENT_SUCCESS, or * SSH_AGENT_FAILURE if the key was malformed. */ if (IsLocked) { goto default; } bool ssh1constrained = (header.Message == Message.SSH1_AGENTC_ADD_RSA_ID_CONSTRAINED); try { var publicKeyParams = messageParser.ReadSsh1PublicKeyData(false); var keyPair = messageParser.ReadSsh1KeyData(publicKeyParams); SshKey key = new SshKey(SshVersion.SSH1, keyPair); key.Comment = messageParser.ReadString(); key.Source = "External client"; if (ssh1constrained) { while (messageStream.Position < header.BlobLength + 4) { KeyConstraint constraint = new KeyConstraint(); constraint.Type = (KeyConstraintType)messageParser.ReadByte(); if (constraint.Type == KeyConstraintType.SSH_AGENT_CONSTRAIN_LIFETIME) { constraint.Data = messageParser.ReadInt(); } key.AddConstraint(constraint); } } AddKey(key); responseBuilder.InsertHeader(Message.SSH_AGENT_SUCCESS); break; } catch (CallbackNullException) { // this is expected } catch (Exception ex) { Debug.Fail(ex.ToString()); } goto default; // failed case Message.SSH2_AGENTC_ADD_IDENTITY: case Message.SSH2_AGENTC_ADD_ID_CONSTRAINED: /* * Add to the list and return SSH_AGENT_SUCCESS, or * SSH_AGENT_FAILURE if the key was malformed. */ if (IsLocked) { goto default; } bool constrained = (header.Message == Message.SSH2_AGENTC_ADD_ID_CONSTRAINED); try { var publicKeyParams = messageParser.ReadSsh2PublicKeyData(); var keyPair = messageParser.ReadSsh2KeyData(publicKeyParams); SshKey key = new SshKey(SshVersion.SSH2, keyPair); key.Comment = messageParser.ReadString(); key.Source = "External client"; if (constrained) { while (messageStream.Position < header.BlobLength + 4) { KeyConstraint constraint = new KeyConstraint(); constraint.Type = (KeyConstraintType)messageParser.ReadByte(); if (constraint.Type == KeyConstraintType.SSH_AGENT_CONSTRAIN_LIFETIME) { constraint.Data = messageParser.ReadInt(); } key.AddConstraint(constraint); } } AddKey(key); responseBuilder.InsertHeader(Message.SSH_AGENT_SUCCESS); break; // success! } catch (CallbackNullException) { // this is expected } catch (Exception ex) { Debug.Fail(ex.ToString()); } goto default; // failed case Message.SSH1_AGENTC_REMOVE_RSA_IDENTITY: case Message.SSH2_AGENTC_REMOVE_IDENTITY: /* * Remove from the list and return SSH_AGENT_SUCCESS, or * perhaps SSH_AGENT_FAILURE if it wasn't in the list to * start with. */ if (IsLocked) { goto default; } SshVersion removeVersion; byte[] rKeyBlob; if (header.Message == Message.SSH1_AGENTC_REMOVE_RSA_IDENTITY) { removeVersion = SshVersion.SSH1; rKeyBlob = messageParser.ReadBytes(header.BlobLength - 1); } else if (header.Message == Message.SSH2_AGENTC_REMOVE_IDENTITY) { removeVersion = SshVersion.SSH2; rKeyBlob = messageParser.ReadBlob(); } else { Debug.Fail("Should not get here."); goto default; } try { var matchingKey = mKeyList.Get(removeVersion, rKeyBlob); var startKeyListLength = mKeyList.Count; RemoveKey(matchingKey); // only succeed if key was removed if (mKeyList.Count == startKeyListLength - 1) { responseBuilder.InsertHeader(Message.SSH_AGENT_SUCCESS); break; //success! } } catch (Exception ex) { Debug.Fail(ex.ToString()); } goto default; // failed case Message.SSH1_AGENTC_REMOVE_ALL_RSA_IDENTITIES: case Message.SSH2_AGENTC_REMOVE_ALL_IDENTITIES: /* * Remove all SSH-1 or SSH-2 keys. */ if (IsLocked) { goto default; } SshVersion removeAllVersion; if (header.Message == Message.SSH1_AGENTC_REMOVE_ALL_RSA_IDENTITIES) { removeAllVersion = SshVersion.SSH1; } else if (header.Message == Message.SSH2_AGENTC_REMOVE_ALL_IDENTITIES) { removeAllVersion = SshVersion.SSH2; } else { Debug.Fail("Should not get here."); goto default; } try { RemoveAllKeys(removeAllVersion); responseBuilder.InsertHeader(Message.SSH_AGENT_SUCCESS); break; //success! } catch (Exception ex) { Debug.Fail(ex.ToString()); } goto default; // failed case Message.SSH_AGENTC_LOCK: try { var passphrase = new PinnedArray<byte>(messageParser.ReadBlob()); try { Lock(passphrase.Data); } finally { passphrase.Clear(); } if (IsLocked) { responseBuilder.InsertHeader(Message.SSH_AGENT_SUCCESS); break; } } catch (AgentLockedException) { // This is expected } catch (Exception ex) { Debug.Fail(ex.ToString()); } goto default; case Message.SSH_AGENTC_UNLOCK: try { var passphrase = new PinnedArray<byte>(messageParser.ReadBlob()); try { Unlock(passphrase.Data); } finally { passphrase.Clear(); } if (!IsLocked) { responseBuilder.InsertHeader(Message.SSH_AGENT_SUCCESS); break; } } catch (AgentLockedException) { // This is expected } catch (PassphraseException) { // This is expected } catch (Exception ex) { Debug.Fail(ex.ToString()); } goto default; default: responseBuilder.Clear(); responseBuilder.InsertHeader(Message.SSH_AGENT_FAILURE); break; } /* write response to stream */ if (messageStream.CanSeek) messageStream.Position = 0; messageStream.Write(responseBuilder.GetBlob(), 0, responseBuilder.Length); messageStream.Flush(); }
public static byte[] FormatSignature(this ISshKey key, byte[] signature) { AsymmetricKeyParameter publicKey = key.GetPublicKeyParameters(); if (publicKey is DsaPublicKeyParameters || publicKey is ECPublicKeyParameters) { Asn1Sequence seq = (Asn1Sequence)Asn1Object.FromByteArray(signature); BigInteger r = ((DerInteger)seq[0]).PositiveValue; BigInteger s = ((DerInteger)seq[1]).PositiveValue; BlobBuilder formatedSignature = new BlobBuilder(); if (publicKey is ECPublicKeyParameters) { var bytes = r.ToByteArray().ToList (); while (bytes.Count < 20) bytes.Insert(0, 0); formatedSignature.AddBlob(bytes.ToArray()); bytes = s.ToByteArray().ToList(); while (bytes.Count < 20) bytes.Insert(0, 0); formatedSignature.AddBlob(bytes.ToArray()); } else { var bytes = r.ToByteArrayUnsigned().ToList(); while (bytes.Count < 20) bytes.Insert(0, 0); formatedSignature.AddBytes(bytes.ToArray()); bytes = s.ToByteArrayUnsigned().ToList(); while (bytes.Count < 20) bytes.Insert(0, 0); formatedSignature.AddBytes(bytes.ToArray()); } return formatedSignature.GetBlob(); } else if (publicKey is RsaKeyParameters || publicKey is Ed25519PublicKeyParameter) { return signature; } throw new Exception("Unsupported algorithm"); }
public void TestAnswerSSH2_AGENTC_ADD_ID_CONSTRAINED() { /* most code is shared with SSH2_AGENTC_ADD_IDENTITY, so we just * need to test the differences */ Agent.ConfirmUserPermissionDelegate confirmCallback = delegate(ISshKey k, Process p) { return true; }; Agent agent = new TestAgent(); /* test that no confirmation callback returns failure */ BlobBuilder builder = new BlobBuilder(); RsaPrivateCrtKeyParameters rsaParameters = (RsaPrivateCrtKeyParameters)rsaKey.GetPrivateKeyParameters(); builder.AddStringBlob(rsaKey.Algorithm.GetIdentifierString()); builder.AddBigIntBlob(rsaParameters.Modulus); builder.AddBigIntBlob(rsaParameters.PublicExponent); builder.AddBigIntBlob(rsaParameters.Exponent); builder.AddBigIntBlob(rsaParameters.QInv); builder.AddBigIntBlob(rsaParameters.P); builder.AddBigIntBlob(rsaParameters.Q); builder.AddStringBlob(rsaKey.Comment); //save blob so far so we don't have to repeat later. byte[] commonBlob = builder.GetBlob(); builder.AddByte((byte)Agent.KeyConstraintType.SSH_AGENT_CONSTRAIN_CONFIRM); builder.InsertHeader(Agent.Message.SSH2_AGENTC_ADD_ID_CONSTRAINED); PrepareMessage(builder); agent.AnswerMessage(stream); RewindStream(); Agent.BlobHeader header = parser.ReadHeader(); Assert.That(header.BlobLength, Is.EqualTo(1)); Assert.That(header.Message, Is.EqualTo(Agent.Message.SSH_AGENT_FAILURE)); /* test adding key with confirm constraint */ agent = new TestAgent(); agent.ConfirmUserPermissionCallback = confirmCallback; PrepareMessage(builder); agent.AnswerMessage(stream); RewindStream(); header = parser.ReadHeader(); Assert.That(header.BlobLength, Is.EqualTo(1)); Assert.That(header.Message, Is.EqualTo(Agent.Message.SSH_AGENT_SUCCESS)); ISshKey returnedKey = agent.GetAllKeys().First(); Assert.That(returnedKey.Constraints.Count(), Is.EqualTo(1)); Assert.That(returnedKey.Constraints[0].Type, Is.EqualTo(Agent.KeyConstraintType.SSH_AGENT_CONSTRAIN_CONFIRM)); Assert.That(returnedKey.Constraints[0].Data, Is.Null); /* test adding key with lifetime constraint */ agent = new TestAgent(); builder.Clear(); builder.AddBytes(commonBlob); builder.AddByte((byte)Agent.KeyConstraintType.SSH_AGENT_CONSTRAIN_LIFETIME); builder.AddInt(10); builder.InsertHeader(Agent.Message.SSH2_AGENTC_ADD_ID_CONSTRAINED); PrepareMessage(builder); agent.AnswerMessage(stream); RewindStream(); header = parser.ReadHeader(); Assert.That(header.BlobLength, Is.EqualTo(1)); Assert.That(header.Message, Is.EqualTo(Agent.Message.SSH_AGENT_SUCCESS)); returnedKey = agent.GetAllKeys().First(); Assert.That(returnedKey.Constraints.Count(), Is.EqualTo(1)); Assert.That(returnedKey.Constraints[0].Type, Is.EqualTo(Agent.KeyConstraintType.SSH_AGENT_CONSTRAIN_LIFETIME)); Assert.That(returnedKey.Constraints[0].Data.GetType(), Is.EqualTo(Agent.KeyConstraintType.SSH_AGENT_CONSTRAIN_LIFETIME.GetDataType())); Assert.That(returnedKey.Constraints[0].Data, Is.EqualTo(10)); /* test adding key with multiple constraints */ agent = new TestAgent(); agent.ConfirmUserPermissionCallback = confirmCallback; builder.Clear(); builder.AddBytes(commonBlob); builder.AddByte((byte)Agent.KeyConstraintType.SSH_AGENT_CONSTRAIN_CONFIRM); builder.AddByte((byte)Agent.KeyConstraintType.SSH_AGENT_CONSTRAIN_LIFETIME); builder.AddInt(10); builder.InsertHeader(Agent.Message.SSH2_AGENTC_ADD_ID_CONSTRAINED); PrepareMessage(builder); agent.AnswerMessage(stream); RewindStream(); header = parser.ReadHeader(); Assert.That(header.BlobLength, Is.EqualTo(1)); Assert.That(header.Message, Is.EqualTo(Agent.Message.SSH_AGENT_SUCCESS)); returnedKey = agent.GetAllKeys().First(); Assert.That(returnedKey.Constraints.Count(), Is.EqualTo(2)); Assert.That(returnedKey.Constraints[0].Type, Is.EqualTo(Agent.KeyConstraintType.SSH_AGENT_CONSTRAIN_CONFIRM)); Assert.That(returnedKey.Constraints[0].Data, Is.Null); Assert.That(returnedKey.Constraints[1].Type, Is.EqualTo(Agent.KeyConstraintType.SSH_AGENT_CONSTRAIN_LIFETIME)); Assert.That(returnedKey.Constraints[1].Data, Is.EqualTo(10)); /* test adding key with multiple constraints in different order */ agent = new TestAgent(); agent.ConfirmUserPermissionCallback = confirmCallback; builder.Clear(); builder.AddBytes(commonBlob); builder.AddByte((byte)Agent.KeyConstraintType.SSH_AGENT_CONSTRAIN_LIFETIME); builder.AddInt(10); builder.AddByte((byte)Agent.KeyConstraintType.SSH_AGENT_CONSTRAIN_CONFIRM); builder.InsertHeader(Agent.Message.SSH2_AGENTC_ADD_ID_CONSTRAINED); PrepareMessage(builder); agent.AnswerMessage(stream); RewindStream(); header = parser.ReadHeader(); Assert.That(header.BlobLength, Is.EqualTo(1)); Assert.That(header.Message, Is.EqualTo(Agent.Message.SSH_AGENT_SUCCESS)); returnedKey = agent.GetAllKeys().First(); Assert.That(returnedKey.Constraints.Count(), Is.EqualTo(2)); Assert.That(returnedKey.Constraints[0].Type, Is.EqualTo(Agent.KeyConstraintType.SSH_AGENT_CONSTRAIN_LIFETIME)); Assert.That(returnedKey.Constraints[0].Data, Is.EqualTo(10)); Assert.That(returnedKey.Constraints[1].Type, Is.EqualTo(Agent.KeyConstraintType.SSH_AGENT_CONSTRAIN_CONFIRM)); Assert.That(returnedKey.Constraints[1].Data, Is.Null); }
public override void Serialize(Stream stream, object obj) { /* check for required parameters */ if (stream == null) { throw new ArgumentNullException("stream"); } if (obj == null) { throw new ArgumentNullException("obj"); } PinnedArray <char> passphrase = null; string ciphername; if (passphrase == null || passphrase.Data.Length == 0) { ciphername = KDFNAME_NONE; } else { ciphername = KDFNAME_BCRYPT; } var builder = new BlobBuilder(); ISshKey sshKey = obj as ISshKey; if (sshKey == null) { throw new ArgumentException("Expected ISshKey", "obj"); } var publicKeyParams = sshKey.GetPublicKeyParameters() as Ed25519PublicKeyParameter; var privateKeyParams = sshKey.GetPrivateKeyParameters() as Ed25519PrivateKeyParameter; /* writing info headers */ builder.AddBytes(Encoding.ASCII.GetBytes(AUTH_MAGIC)); builder.AddStringBlob(ciphername); builder.AddStringBlob(ciphername); //kdfname builder.AddBlob(new byte[0]); // kdfoptions /* writing public key */ builder.AddInt(1); // number of keys N var publicKeyBuilder = new BlobBuilder(); publicKeyBuilder.AddStringBlob(PublicKeyAlgorithm.ED25519.GetIdentifierString()); publicKeyBuilder.AddBlob(publicKeyParams.Key); builder.AddBlob(publicKeyBuilder.GetBlob()); /* writing private key */ BlobBuilder privateKeyBuilder = new BlobBuilder(); var checkint = new SecureRandom().NextInt(); privateKeyBuilder.AddInt(checkint); privateKeyBuilder.AddInt(checkint); privateKeyBuilder.AddStringBlob(PublicKeyAlgorithm.ED25519.GetIdentifierString()); privateKeyBuilder.AddBlob(publicKeyParams.Key); privateKeyBuilder.AddBlob(privateKeyParams.Signature); privateKeyBuilder.AddStringBlob(sshKey.Comment); if (ciphername == KDFNAME_NONE) { /* plain-text */ builder.AddBlob(privateKeyBuilder.GetBlobAsPinnedByteArray().Data); } else { byte[] keydata; using (MD5 md5 = MD5.Create()) { keydata = md5.ComputeHash(Encoding.ASCII.GetBytes(passphrase.Data)); } passphrase.Dispose(); } /* writing result to file */ var builderOutput = builder.GetBlobAsPinnedByteArray(); using (var writer = new StreamWriter(stream)) { writer.NewLine = "\n"; writer.WriteLine(MARK_BEGIN); var base64Data = Util.ToBase64(builderOutput.Data); var base64String = Encoding.UTF8.GetString(base64Data); var offset = 0; while (offset < base64String.Length) { const int maxLineLength = 70; if (offset + maxLineLength > base64String.Length) { writer.WriteLine(base64String.Substring(offset)); } else { writer.WriteLine(base64String.Substring(offset, maxLineLength)); } offset += maxLineLength; } writer.WriteLine(MARK_END); } }
public void TestInsertHeader() { BlobBuilder builder = new BlobBuilder(); builder.InsertHeader(Agent.Message.SSH_AGENT_SUCCESS); byte[] expected = { 0, 0, 0, 1, (byte)Agent.Message.SSH_AGENT_SUCCESS }; Assert.That(builder.GetBlob(), Is.EqualTo(expected)); builder = new BlobBuilder(); int value1 = 12345; builder.InsertHeader(Agent.Message.SSH_AGENT_SUCCESS, value1); expected = new byte[9]; Array.Copy((5).ToBytes(), expected, 4); expected[4] = (byte)Agent.Message.SSH_AGENT_SUCCESS; Array.Copy(value1.ToBytes(), 0, expected, 5, 4); Assert.That(builder.GetBlob(), Is.EqualTo(expected)); builder = new BlobBuilder(); byte[] value2 = { 1, 2, 3, 4, 5 }; builder.AddBytes(value2); builder.InsertHeader(Agent.Message.SSH_AGENT_SUCCESS); expected = new byte[5 + value2.Length]; int length = value2.Length + 1; Array.Copy(length.ToBytes(), expected, 4); expected[4] = (byte)Agent.Message.SSH_AGENT_SUCCESS; Array.Copy(value2, 0, expected, 5, value2.Length); Assert.That(builder.GetBlob(), Is.EqualTo(expected)); builder = new BlobBuilder(); builder.AddBytes(value2); builder.InsertHeader(Agent.Message.SSH_AGENT_SUCCESS, value1); expected = new byte[9 + value2.Length]; Array.Copy((5 + value2.Length).ToBytes(), expected, 4); expected[4] = (byte)Agent.Message.SSH_AGENT_SUCCESS; Array.Copy(value1.ToBytes(), 0, expected, 5, 4); Array.Copy(value2, 0, expected, 9, value2.Length); Assert.That(builder.GetBlob(), Is.EqualTo(expected)); }
public override void Serialize(Stream aStream, object aObject) { /* check for required parameters */ if (aStream == null) { throw new ArgumentNullException("aStream"); } if (aObject == null) { throw new ArgumentNullException("aObject"); } PasswordFinder pwFinder = null; if (GetPassphraseCallbackMethod != null) { pwFinder = new PasswordFinder(GetPassphraseCallbackMethod); } PinnedArray <char> passphrase = null; if (pwFinder != null) { passphrase = new PinnedArray <char>(0); passphrase.Data = pwFinder.GetPassword(); } byte cipherType; if (passphrase == null || passphrase.Data.Length == 0) { cipherType = SSH_CIPHER_NONE; } else { cipherType = SSH_CIPHER_3DES; } BlobBuilder builder = new BlobBuilder(); ISshKey sshKey = aObject as ISshKey; RsaKeyParameters publicKeyParams = sshKey.GetPublicKeyParameters() as RsaKeyParameters; RsaPrivateCrtKeyParameters privateKeyParams = sshKey.GetPrivateKeyParameters() as RsaPrivateCrtKeyParameters; /* writing info headers */ builder.AddBytes(Encoding.ASCII.GetBytes(FILE_HEADER_LINE + "\n")); builder.AddUInt8(0); //end of string builder.AddUInt8(cipherType); //cipher builder.AddInt(0); //reserved /* writing public key */ builder.AddInt(sshKey.Size); builder.AddSsh1BigIntBlob(publicKeyParams.Modulus); builder.AddSsh1BigIntBlob(publicKeyParams.Exponent); builder.AddStringBlob(sshKey.Comment); /* writing private key */ BlobBuilder privateKeyBuilder = new BlobBuilder(); /* adding some control values */ Random random = new Random(); byte[] resultCheck = new byte[2]; random.NextBytes(resultCheck); privateKeyBuilder.AddUInt8(resultCheck[0]); privateKeyBuilder.AddUInt8(resultCheck[1]); privateKeyBuilder.AddUInt8(resultCheck[0]); privateKeyBuilder.AddUInt8(resultCheck[1]); privateKeyBuilder.AddSsh1BigIntBlob(privateKeyParams.Exponent); privateKeyBuilder.AddSsh1BigIntBlob(privateKeyParams.DQ); privateKeyBuilder.AddSsh1BigIntBlob(privateKeyParams.P); privateKeyBuilder.AddSsh1BigIntBlob(privateKeyParams.Q); if (cipherType == SSH_CIPHER_NONE) { /* plain-text */ builder.AddBytes(privateKeyBuilder.GetBlobAsPinnedByteArray().Data); } else { byte[] keydata; using (MD5 md5 = MD5.Create()) { keydata = md5.ComputeHash(Encoding.ASCII.GetBytes(passphrase.Data)); } /* encryption */ DesSsh1Engine desEngine = new DesSsh1Engine(); desEngine.Init(true, new KeyParameter(keydata)); BufferedBlockCipher bufferedBlockCipher = new BufferedBlockCipher(desEngine); byte[] ouputBuffer = bufferedBlockCipher.ProcessBytes( privateKeyBuilder.GetBlobAsPinnedByteArray().Data); builder.AddBytes(ouputBuffer); passphrase.Dispose(); } /* writing result to file */ var builderOutput = builder.GetBlobAsPinnedByteArray(); aStream.Write(builderOutput.Data, 0, builderOutput.Data.Length); aStream.Close(); }
private BlobBuilder CreatePublicKeyBlob(ISshKey aKey) { var builder = new BlobBuilder(); switch (aKey.Version) { case SshVersion.SSH1: builder.AddBytes(aKey.GetPublicKeyBlob()); break; case SshVersion.SSH2: builder.AddBlob(aKey.GetPublicKeyBlob()); break; } return builder; }
public byte[] SignRequest(ISshKey aKey, byte[] aSignData) { BlobBuilder builder = new BlobBuilder(); switch (aKey.Version) { case SshVersion.SSH1: builder.AddBytes(aKey.GetPublicKeyBlob()); var engine = new Pkcs1Encoding(new RsaEngine()); engine.Init(true /* encrypt */, aKey.GetPublicKeyParameters()); var encryptedData = engine.ProcessBlock(aSignData, 0, aSignData.Length); var challenge = new BigInteger(encryptedData); builder.AddSsh1BigIntBlob(challenge); builder.AddBytes(SessionId); builder.AddInt(1); // response type - must be 1 builder.InsertHeader(Agent.Message.SSH1_AGENTC_RSA_CHALLENGE); break; case SshVersion.SSH2: builder.AddBlob(aKey.GetPublicKeyBlob()); builder.AddBlob(aSignData); builder.InsertHeader(Agent.Message.SSH2_AGENTC_SIGN_REQUEST); break; default: throw new Exception(cUnsupportedSshVersion); } BlobParser replyParser = SendMessage(builder); var header = replyParser.ReadHeader(); switch (aKey.Version) { case SshVersion.SSH1: if (header.Message != Agent.Message.SSH1_AGENT_RSA_RESPONSE) { throw new AgentFailureException(); } byte[] response = new byte[16]; for (int i = 0; i < 16; i++) { response[i] = replyParser.ReadByte(); } return response; case SshVersion.SSH2: if (header.Message != Agent.Message.SSH2_AGENT_SIGN_RESPONSE) { throw new AgentFailureException(); } return replyParser.ReadBlob(); default: throw new Exception(cUnsupportedSshVersion); } }
public void TestAnswerSSH1_AGENTC_REMOVE_RSA_IDENTITY() { Agent agent = new TestAgent(allKeys); BlobBuilder builder = new BlobBuilder(); /* test remove key returns success when key is removed */ builder.AddBytes(rsa1Key.GetPublicKeyBlob()); builder.InsertHeader(Agent.Message.SSH1_AGENTC_REMOVE_RSA_IDENTITY); PrepareMessage(builder); agent.AnswerMessage(stream); RewindStream(); Agent.BlobHeader header = parser.ReadHeader(); Assert.That(header.BlobLength, Is.EqualTo(1)); Assert.That(header.Message, Is.EqualTo(Agent.Message.SSH_AGENT_SUCCESS)); Assert.That(agent.GetAllKeys() .SequenceEqual(allKeys.Where(key => key != rsa1Key))); /* test remove key returns failure when key does not exist */ int startCount = agent.GetAllKeys().Count(); PrepareMessage(builder); agent.AnswerMessage(stream); RewindStream(); header = parser.ReadHeader(); Assert.That(header.BlobLength, Is.EqualTo(1)); Assert.That(header.Message, Is.EqualTo(Agent.Message.SSH_AGENT_FAILURE)); Assert.That(agent.GetAllKeys().Count(), Is.EqualTo(startCount)); /* test returns failure when locked */ agent.Lock(new byte[0]); startCount = agent.GetAllKeys().Count(); builder.AddBlob(dsaKey.GetPublicKeyBlob()); builder.InsertHeader(Agent.Message.SSH2_AGENTC_REMOVE_IDENTITY); PrepareMessage(builder); agent.AnswerMessage(stream); RewindStream(); header = parser.ReadHeader(); Assert.That(header.BlobLength, Is.EqualTo(1)); Assert.That(header.Message, Is.EqualTo(Agent.Message.SSH_AGENT_FAILURE)); Assert.That(agent.GetAllKeys().Count(), Is.EqualTo(startCount)); }
public void TestAnswerSSH1_AGENTC_RSA_CHALLENGE() { Agent agent = new TestAgent(allKeys); /* test answering to RSA challenge */ BlobBuilder builder = new BlobBuilder(); RsaPrivateCrtKeyParameters rsaParameters = (RsaPrivateCrtKeyParameters)rsa1Key.GetPrivateKeyParameters(); builder.AddInt(rsa1Key.Size); builder.AddSsh1BigIntBlob(rsaParameters.PublicExponent); builder.AddSsh1BigIntBlob(rsaParameters.Modulus); byte[] decryptedChallenge = new byte[8]; byte[] sessionId = new byte[16]; Random random = new Random(); random.NextBytes(decryptedChallenge); random.NextBytes(sessionId); IAsymmetricBlockCipher engine = new Pkcs1Encoding(new RsaEngine()); engine.Init(true, rsa1Key.GetPublicKeyParameters()); byte[] encryptedChallenge = engine.ProcessBlock(decryptedChallenge, 0, decryptedChallenge.Length); BigInteger chal = new BigInteger(encryptedChallenge); builder.AddSsh1BigIntBlob(chal); builder.AddBytes(sessionId); builder.AddInt(1); builder.InsertHeader(Agent.Message.SSH1_AGENTC_RSA_CHALLENGE); PrepareMessage(builder); agent.AnswerMessage(stream); RewindStream(); Agent.BlobHeader header = parser.ReadHeader(); byte[] md5Received = parser.ReadBytes(16); /* check that proper response type was received */ Assert.That(header.Message, Is.EqualTo(Agent.Message.SSH1_AGENT_RSA_RESPONSE)); using (MD5 md5 = MD5.Create()) { byte[] md5Buffer = new byte[48]; decryptedChallenge.CopyTo(md5Buffer, 0); sessionId.CopyTo(md5Buffer, 32); byte[] md5Expected = md5.ComputeHash(md5Buffer); /* check the encrypted challenge was successfully read */ Assert.That(md5Received, Is.EqualTo(md5Expected)); } }
public override void Serialize(Stream stream, object obj) { /* check for required parameters */ if (stream == null) { throw new ArgumentNullException("stream"); } if (obj == null) { throw new ArgumentNullException("obj"); } PinnedArray<char> passphrase = null; string ciphername; if (passphrase == null || passphrase.Data.Length == 0) { ciphername = KDFNAME_NONE; } else { ciphername = KDFNAME_BCRYPT; } var builder = new BlobBuilder(); ISshKey sshKey = obj as ISshKey; if (sshKey == null) { throw new ArgumentException("Expected ISshKey", "obj"); } var publicKeyParams = sshKey.GetPublicKeyParameters() as Ed25519PublicKeyParameter; var privateKeyParams = sshKey.GetPrivateKeyParameters() as Ed25519PrivateKeyParameter; /* writing info headers */ builder.AddBytes(Encoding.ASCII.GetBytes(AUTH_MAGIC)); builder.AddStringBlob(ciphername); builder.AddStringBlob(ciphername); //kdfname builder.AddBlob(new byte[0]); // kdfoptions /* writing public key */ builder.AddInt(1); // number of keys N var publicKeyBuilder = new BlobBuilder(); publicKeyBuilder.AddStringBlob(PublicKeyAlgorithm.ED25519.GetIdentifierString()); publicKeyBuilder.AddBlob(publicKeyParams.Key); builder.AddBlob(publicKeyBuilder.GetBlob()); /* writing private key */ BlobBuilder privateKeyBuilder = new BlobBuilder(); var checkint = new SecureRandom().NextInt(); privateKeyBuilder.AddInt(checkint); privateKeyBuilder.AddInt(checkint); privateKeyBuilder.AddStringBlob(PublicKeyAlgorithm.ED25519.GetIdentifierString()); privateKeyBuilder.AddBlob(publicKeyParams.Key); privateKeyBuilder.AddBlob(privateKeyParams.Signature); privateKeyBuilder.AddStringBlob(sshKey.Comment); if (ciphername == KDFNAME_NONE) { /* plain-text */ builder.AddBlob(privateKeyBuilder.GetBlobAsPinnedByteArray().Data); } else { byte[] keydata; using (MD5 md5 = MD5.Create()) { keydata = md5.ComputeHash(Encoding.ASCII.GetBytes(passphrase.Data)); } passphrase.Dispose(); } /* writing result to file */ var builderOutput = builder.GetBlobAsPinnedByteArray(); using (var writer = new StreamWriter(stream)) { writer.NewLine = "\n"; writer.WriteLine(MARK_BEGIN); var base64Data = Util.ToBase64(builderOutput.Data); var base64String = Encoding.UTF8.GetString(base64Data); var offset = 0; while (offset < base64String.Length) { const int maxLineLength = 70; if (offset + maxLineLength > base64String.Length) { writer.WriteLine(base64String.Substring(offset)); } else { writer.WriteLine(base64String.Substring(offset, maxLineLength)); } offset += maxLineLength; } writer.WriteLine(MARK_END); } }
/// <summary> /// Answers the message. /// </summary> /// <param name='messageStream'>Message stream.</param> /// <param name="process">The calling process or <c>null</c> if the process /// could not be obtained.</param> /// <remarks>code based on winpgnt.c from PuTTY source code</remarks> public void AnswerMessage(Stream messageStream, Process process = null) { if (messageStream.CanTimeout) { messageStream.ReadTimeout = 5000; } var messageParser = new BlobParser(messageStream); var responseBuilder = new BlobBuilder(); BlobHeader header; try { header = messageParser.ReadHeader(); if (MessageReceived != null) { var eventArgs = new MessageReceivedEventArgs(header); MessageReceived(this, eventArgs); if (eventArgs.Fail) { throw new Exception(); } } } catch (Exception) { header = new BlobHeader(); header.Message = Message.UNKNOWN; // this will cause the switch statement below to use the default case // which returns an error to the stream. } switch (header.Message) { case Message.SSH1_AGENTC_REQUEST_RSA_IDENTITIES: /* * Reply with SSH1_AGENT_RSA_IDENTITIES_ANSWER. */ try { if (header.BlobLength > 1) { // ruby net-ssh tries to send a SSH2_AGENT_REQUEST_VERSION message // which has the same id number as SSH1_AGENTC_REQUEST_RSA_IDENTITIES // with a string tacked on. We need to read the string from the // stream, but it is not used for anything. messageParser.ReadString(); } var keyList = ListKeys(SshVersion.SSH1); if (FilterKeyListCallback != null) { keyList = FilterKeyListCallback(keyList); } foreach (SshKey key in keyList) { responseBuilder.AddBytes(key.GetPublicKeyBlob()); responseBuilder.AddStringBlob(key.Comment); } responseBuilder.InsertHeader(Message.SSH1_AGENT_RSA_IDENTITIES_ANSWER, keyList.Count); // TODO may want to check that there is enough room in the message stream break; // succeeded } catch (Exception ex) { Debug.Fail(ex.ToString()); } goto default; // failed case Message.SSH2_AGENTC_REQUEST_IDENTITIES: /* * Reply with SSH2_AGENT_IDENTITIES_ANSWER. */ try { var keyList = ListKeys(SshVersion.SSH2); if (FilterKeyListCallback != null) { keyList = FilterKeyListCallback(keyList); } foreach (SshKey key in keyList) { responseBuilder.AddBlob(key.GetPublicKeyBlob()); responseBuilder.AddStringBlob(key.Comment); } responseBuilder.InsertHeader(Message.SSH2_AGENT_IDENTITIES_ANSWER, keyList.Count); // TODO may want to check that there is enough room in the message stream break; // succeeded } catch (Exception ex) { Debug.Fail(ex.ToString()); } goto default; // failed case Message.SSH1_AGENTC_RSA_CHALLENGE: /* * Reply with either SSH1_AGENT_RSA_RESPONSE or * SSH_AGENT_FAILURE, depending on whether we have that key * or not. */ try { //Reading publicKey information var publicKeyParams = messageParser.ReadSsh1PublicKeyData(true); //Searching for Key here var matchingKey = mKeyList.Where(key => key.Version == SshVersion.SSH1 && (key.GetPublicKeyParameters().Equals(publicKeyParams))).Single(); //Reading challenge var encryptedChallenge = messageParser.ReadSsh1BigIntBlob(); var sessionId = messageParser.ReadBytes(16); //Checking responseType field if (messageParser.ReadUInt32() != 1) { goto default; //responseType !=1 is not longer supported } //Answering to the challenge var engine = new Pkcs1Encoding(new RsaEngine()); engine.Init(false /* decrypt */, matchingKey.GetPrivateKeyParameters()); var decryptedChallenge = engine.ProcessBlock(encryptedChallenge, 0, encryptedChallenge.Length); using (MD5 md5 = MD5.Create()) { var md5Buffer = new byte[48]; decryptedChallenge.CopyTo(md5Buffer, 0); sessionId.CopyTo(md5Buffer, 32); responseBuilder.AddBytes(md5.ComputeHash(md5Buffer)); responseBuilder.InsertHeader(Message.SSH1_AGENT_RSA_RESPONSE); break; } } catch (InvalidOperationException) { // this is expected if there is not a matching key } catch (Exception ex) { Debug.Fail(ex.ToString()); } goto default; // failed case Message.SSH2_AGENTC_SIGN_REQUEST: /* * Reply with either SSH2_AGENT_SIGN_RESPONSE or SSH_AGENT_FAILURE, * depending on whether we have that key or not. */ try { var keyBlob = messageParser.ReadBlob(); var reqData = messageParser.ReadBlob(); var flags = new SignRequestFlags(); try { // usually, there are no flags, so parser will throw flags = (SignRequestFlags)messageParser.ReadUInt32(); } catch { } var matchingKey = mKeyList.Where(key => key.Version == SshVersion.SSH2 && key.GetPublicKeyBlob().SequenceEqual(keyBlob)).First(); var confirmConstraints = matchingKey.Constraints .Where(constraint => constraint.Type == KeyConstraintType.SSH_AGENT_CONSTRAIN_CONFIRM); if (confirmConstraints.Count() > 0) { if (!ConfirmUserPermissionCallback.Invoke(matchingKey, process)) { goto default; } } /* create signature */ var signKey = matchingKey; var signer = signKey.GetSigner(); var algName = signKey.Algorithm.GetIdentifierString(); signer.Init(true, signKey.GetPrivateKeyParameters()); signer.BlockUpdate(reqData, 0, reqData.Length); byte[] signature = signer.GenerateSignature(); signature = signKey.FormatSignature(signature); BlobBuilder signatureBuilder = new BlobBuilder(); if (!flags.HasFlag(SignRequestFlags.SSH_AGENT_OLD_SIGNATURE)) { signatureBuilder.AddStringBlob(algName); } signatureBuilder.AddBlob(signature); responseBuilder.AddBlob(signatureBuilder.GetBlob()); responseBuilder.InsertHeader(Message.SSH2_AGENT_SIGN_RESPONSE); try { KeyUsed(this, new KeyUsedEventArgs(signKey, process)); } catch { } break; // succeeded } catch (InvalidOperationException) { // this is expected if there is not a matching key } catch (Exception ex) { Debug.Fail(ex.ToString()); } goto default; // failure case Message.SSH1_AGENTC_ADD_RSA_IDENTITY: case Message.SSH1_AGENTC_ADD_RSA_ID_CONSTRAINED: /* * Add to the list and return SSH_AGENT_SUCCESS, or * SSH_AGENT_FAILURE if the key was malformed. */ if (IsLocked) { goto default; } bool ssh1constrained = (header.Message == Message.SSH1_AGENTC_ADD_RSA_ID_CONSTRAINED); try { var publicKeyParams = messageParser.ReadSsh1PublicKeyData(false); var keyPair = messageParser.ReadSsh1KeyData(publicKeyParams); SshKey key = new SshKey(SshVersion.SSH1, keyPair); key.Comment = messageParser.ReadString(); key.Source = "External client"; if (ssh1constrained) { while (messageStream.Position < header.BlobLength + 4) { KeyConstraint constraint = new KeyConstraint(); constraint.Type = (KeyConstraintType)messageParser.ReadUInt8(); if (constraint.Type == KeyConstraintType.SSH_AGENT_CONSTRAIN_LIFETIME) { constraint.Data = messageParser.ReadUInt32(); } key.AddConstraint(constraint); } } AddKey(key); responseBuilder.InsertHeader(Message.SSH_AGENT_SUCCESS); break; } catch (CallbackNullException) { // this is expected } catch (Exception ex) { Debug.Fail(ex.ToString()); } goto default; // failed case Message.SSH2_AGENTC_ADD_IDENTITY: case Message.SSH2_AGENTC_ADD_ID_CONSTRAINED: /* * Add to the list and return SSH_AGENT_SUCCESS, or * SSH_AGENT_FAILURE if the key was malformed. */ if (IsLocked) { goto default; } bool constrained = (header.Message == Message.SSH2_AGENTC_ADD_ID_CONSTRAINED); try { OpensshCertificate cert; var publicKeyParams = messageParser.ReadSsh2PublicKeyData(out cert); var keyPair = messageParser.ReadSsh2KeyData(publicKeyParams); SshKey key = new SshKey(SshVersion.SSH2, keyPair, null, cert); key.Comment = messageParser.ReadString(); key.Source = "External client"; if (constrained) { while (messageStream.Position < header.BlobLength + 4) { KeyConstraint constraint = new KeyConstraint(); constraint.Type = (KeyConstraintType)messageParser.ReadUInt8(); if (constraint.Type == KeyConstraintType.SSH_AGENT_CONSTRAIN_LIFETIME) { constraint.Data = messageParser.ReadUInt32(); } key.AddConstraint(constraint); } } AddKey(key); responseBuilder.InsertHeader(Message.SSH_AGENT_SUCCESS); break; // success! } catch (CallbackNullException) { // this is expected } catch (Exception ex) { Debug.Fail(ex.ToString()); } goto default; // failed case Message.SSH1_AGENTC_REMOVE_RSA_IDENTITY: case Message.SSH2_AGENTC_REMOVE_IDENTITY: /* * Remove from the list and return SSH_AGENT_SUCCESS, or * perhaps SSH_AGENT_FAILURE if it wasn't in the list to * start with. */ if (IsLocked) { goto default; } SshVersion removeVersion; byte[] rKeyBlob; if (header.Message == Message.SSH1_AGENTC_REMOVE_RSA_IDENTITY) { removeVersion = SshVersion.SSH1; rKeyBlob = messageParser.ReadBytes(header.BlobLength - 1); } else if (header.Message == Message.SSH2_AGENTC_REMOVE_IDENTITY) { removeVersion = SshVersion.SSH2; rKeyBlob = messageParser.ReadBlob(); } else { Debug.Fail("Should not get here."); goto default; } try { var matchingKey = mKeyList.Get(removeVersion, rKeyBlob); var startKeyListLength = mKeyList.Count; RemoveKey(matchingKey); // only succeed if key was removed if (mKeyList.Count == startKeyListLength - 1) { responseBuilder.InsertHeader(Message.SSH_AGENT_SUCCESS); break; //success! } } catch (Exception ex) { Debug.Fail(ex.ToString()); } goto default; // failed case Message.SSH1_AGENTC_REMOVE_ALL_RSA_IDENTITIES: case Message.SSH2_AGENTC_REMOVE_ALL_IDENTITIES: /* * Remove all SSH-1 or SSH-2 keys. */ if (IsLocked) { goto default; } SshVersion removeAllVersion; if (header.Message == Message.SSH1_AGENTC_REMOVE_ALL_RSA_IDENTITIES) { removeAllVersion = SshVersion.SSH1; } else if (header.Message == Message.SSH2_AGENTC_REMOVE_ALL_IDENTITIES) { removeAllVersion = SshVersion.SSH2; } else { Debug.Fail("Should not get here."); goto default; } try { RemoveAllKeys(removeAllVersion); responseBuilder.InsertHeader(Message.SSH_AGENT_SUCCESS); break; //success! } catch (Exception ex) { Debug.Fail(ex.ToString()); } goto default; // failed case Message.SSH_AGENTC_LOCK: try { var passphrase = new PinnedArray <byte>(messageParser.ReadBlob()); try { Lock(passphrase.Data); } finally { passphrase.Clear(); } if (IsLocked) { responseBuilder.InsertHeader(Message.SSH_AGENT_SUCCESS); break; } } catch (AgentLockedException) { // This is expected } catch (Exception ex) { Debug.Fail(ex.ToString()); } goto default; case Message.SSH_AGENTC_UNLOCK: try { var passphrase = new PinnedArray <byte>(messageParser.ReadBlob()); try { Unlock(passphrase.Data); } finally { passphrase.Clear(); } if (!IsLocked) { responseBuilder.InsertHeader(Message.SSH_AGENT_SUCCESS); break; } } catch (AgentLockedException) { // This is expected } catch (PassphraseException) { // This is expected } catch (Exception ex) { Debug.Fail(ex.ToString()); } goto default; default: responseBuilder.Clear(); responseBuilder.InsertHeader(Message.SSH_AGENT_FAILURE); break; } /* write response to stream */ if (messageStream.CanSeek) { messageStream.Position = 0; } messageStream.Write(responseBuilder.GetBlob(), 0, responseBuilder.Length); messageStream.Flush(); }
private static void VerifyIntegrity(FileData fileData) { BlobBuilder builder = new BlobBuilder(); if (fileData.ppkFileVersion != Version.V1) { builder.AddStringBlob(fileData.publicKeyAlgorithm.GetIdentifierString()); builder.AddStringBlob(fileData.privateKeyAlgorithm.GetIdentifierString()); builder.AddBlob(Encoding.GetEncoding(1252).GetBytes(fileData.comment)); builder.AddBlob(fileData.publicKeyBlob); builder.AddInt(fileData.privateKeyBlob.Data.Length); } builder.AddBytes(fileData.privateKeyBlob.Data); byte[] computedHash; SHA1 sha = SHA1.Create(); if (fileData.isHMAC) { HMAC hmac = HMACSHA1.Create(); if (fileData.passphrase != null) { using (PinnedArray<byte> hashData = new PinnedArray<byte>(cMACKeySalt.Length + fileData.passphrase.Length)) { Array.Copy(Encoding.UTF8.GetBytes(cMACKeySalt), hashData.Data, cMACKeySalt.Length); IntPtr passphrasePtr = Marshal.SecureStringToGlobalAllocUnicode(fileData.passphrase); for (int i = 0; i < fileData.passphrase.Length; i++) { int unicodeChar = Marshal.ReadInt16(passphrasePtr + i * 2); byte ansiChar = Util.UnicodeToAnsi(unicodeChar); hashData.Data[cMACKeySalt.Length + i] = ansiChar; Marshal.WriteByte(passphrasePtr, i * 2, 0); } Marshal.ZeroFreeGlobalAllocUnicode(passphrasePtr); hmac.Key = sha.ComputeHash(hashData.Data); } } else { hmac.Key = sha.ComputeHash(Encoding.UTF8.GetBytes(cMACKeySalt)); } computedHash = hmac.ComputeHash(builder.GetBlob()); hmac.Clear(); } else { computedHash = sha.ComputeHash(builder.GetBlob()); } sha.Clear(); builder.Clear(); try { int macLength = computedHash.Length; bool failed = false; if (fileData.privateMAC.Length == macLength) { for (int i = 0; i < macLength; i++) { if (fileData.privateMAC[i] != computedHash[i]) { failed = true; break; } } } else { failed = true; } if (failed) { // private key data should start with 3 bytes with value 0 if it was // properly decrypted or does not require decryption if ((fileData.privateKeyBlob.Data[0] == 0) && (fileData.privateKeyBlob.Data[1] == 0) && (fileData.privateKeyBlob.Data[2] == 0)) { // so if they bytes are there, passphrase decrypted properly and // something else is wrong with the file contents throw new PpkFormatterException(PpkFormatterException.PpkErrorType.FileCorrupt); } else { // if the bytes are not zeros, we assume that the data was not // properly decrypted because the passphrase was incorrect. throw new PpkFormatterException(PpkFormatterException.PpkErrorType.BadPassphrase); } } } catch { throw; } finally { Array.Clear(computedHash, 0, computedHash.Length); } }
public override void Serialize(Stream aStream, object aObject) { /* check for required parameters */ if (aStream == null) { throw new ArgumentNullException("aStream"); } if (aObject == null) { throw new ArgumentNullException("aObject"); } PasswordFinder pwFinder = null; if (GetPassphraseCallbackMethod != null) { pwFinder = new PasswordFinder(GetPassphraseCallbackMethod); } PinnedArray<char> passphrase = null; if (pwFinder != null) { passphrase = new PinnedArray<char>(0); passphrase.Data = pwFinder.GetPassword(); } byte cipherType; if (passphrase == null || passphrase.Data.Length == 0) { cipherType = SSH_CIPHER_NONE; } else { cipherType = SSH_CIPHER_3DES; } BlobBuilder builder = new BlobBuilder(); ISshKey sshKey = aObject as ISshKey; RsaKeyParameters publicKeyParams = sshKey.GetPublicKeyParameters() as RsaKeyParameters; RsaPrivateCrtKeyParameters privateKeyParams = sshKey.GetPrivateKeyParameters() as RsaPrivateCrtKeyParameters; /* writing info headers */ builder.AddBytes(Encoding.ASCII.GetBytes(FILE_HEADER_LINE + "\n")); builder.AddByte(0); //end of string builder.AddByte(cipherType); //cipher builder.AddInt(0); //reserved /* writing public key */ builder.AddInt(sshKey.Size); builder.AddSsh1BigIntBlob(publicKeyParams.Modulus); builder.AddSsh1BigIntBlob(publicKeyParams.Exponent); builder.AddStringBlob(sshKey.Comment); /* writing private key */ BlobBuilder privateKeyBuilder = new BlobBuilder(); /* adding some control values */ Random random = new Random(); byte[] resultCheck = new byte[2]; random.NextBytes(resultCheck); privateKeyBuilder.AddByte(resultCheck[0]); privateKeyBuilder.AddByte(resultCheck[1]); privateKeyBuilder.AddByte(resultCheck[0]); privateKeyBuilder.AddByte(resultCheck[1]); privateKeyBuilder.AddSsh1BigIntBlob(privateKeyParams.Exponent); privateKeyBuilder.AddSsh1BigIntBlob(privateKeyParams.DQ); privateKeyBuilder.AddSsh1BigIntBlob(privateKeyParams.P); privateKeyBuilder.AddSsh1BigIntBlob(privateKeyParams.Q); if (cipherType == SSH_CIPHER_NONE) { /* plain-text */ builder.AddBytes(privateKeyBuilder.GetBlobAsPinnedByteArray().Data); } else { byte[] keydata; using (MD5 md5 = MD5.Create()) { keydata = md5.ComputeHash(Encoding.ASCII.GetBytes(passphrase.Data)); } /* encryption */ DesSsh1Engine desEngine = new DesSsh1Engine(); desEngine.Init(true, new KeyParameter(keydata)); BufferedBlockCipher bufferedBlockCipher = new BufferedBlockCipher(desEngine); byte[] ouputBuffer = bufferedBlockCipher.ProcessBytes( privateKeyBuilder.GetBlobAsPinnedByteArray().Data); builder.AddBytes(ouputBuffer); passphrase.Dispose(); } /* writing result to file */ var builderOutput = builder.GetBlobAsPinnedByteArray(); aStream.Write(builderOutput.Data, 0, builderOutput.Data.Length); aStream.Close(); }
private static void VerifyIntegrity(FileData fileData) { BlobBuilder builder = new BlobBuilder(); if (fileData.ppkFileVersion != Version.V1) { builder.AddStringBlob(fileData.publicKeyAlgorithm.GetIdentifierString()); builder.AddStringBlob(fileData.privateKeyAlgorithm.GetIdentifierString()); builder.AddBlob(Encoding.GetEncoding(1252).GetBytes(fileData.comment)); builder.AddBlob(fileData.publicKeyBlob); builder.AddInt(fileData.privateKeyBlob.Data.Length); } builder.AddBytes(fileData.privateKeyBlob.Data); byte[] computedHash; SHA1 sha = SHA1.Create(); if (fileData.isHMAC) { HMAC hmac = HMACSHA1.Create(); if (fileData.passphrase != null) { using (PinnedArray <byte> hashData = new PinnedArray <byte>(cMACKeySalt.Length + fileData.passphrase.Length)) { Array.Copy(Encoding.UTF8.GetBytes(cMACKeySalt), hashData.Data, cMACKeySalt.Length); IntPtr passphrasePtr = Marshal.SecureStringToGlobalAllocUnicode(fileData.passphrase); for (int i = 0; i < fileData.passphrase.Length; i++) { int unicodeChar = Marshal.ReadInt16(passphrasePtr + i * 2); byte ansiChar = Util.UnicodeToAnsi(unicodeChar); hashData.Data[cMACKeySalt.Length + i] = ansiChar; Marshal.WriteByte(passphrasePtr, i * 2, 0); } Marshal.ZeroFreeGlobalAllocUnicode(passphrasePtr); hmac.Key = sha.ComputeHash(hashData.Data); } } else { hmac.Key = sha.ComputeHash(Encoding.UTF8.GetBytes(cMACKeySalt)); } computedHash = hmac.ComputeHash(builder.GetBlob()); hmac.Clear(); } else { computedHash = sha.ComputeHash(builder.GetBlob()); } sha.Clear(); builder.Clear(); try { int macLength = computedHash.Length; bool failed = false; if (fileData.privateMAC.Length == macLength) { for (int i = 0; i < macLength; i++) { if (fileData.privateMAC[i] != computedHash[i]) { failed = true; break; } } } else { failed = true; } if (failed) { // private key data should start with 3 bytes with value 0 if it was // properly decrypted or does not require decryption if ((fileData.privateKeyBlob.Data[0] == 0) && (fileData.privateKeyBlob.Data[1] == 0) && (fileData.privateKeyBlob.Data[2] == 0)) { // so if they bytes are there, passphrase decrypted properly and // something else is wrong with the file contents throw new PpkFormatterException(PpkFormatterException.PpkErrorType.FileCorrupt); } else { // if the bytes are not zeros, we assume that the data was not // properly decrypted because the passphrase was incorrect. throw new PpkFormatterException(PpkFormatterException.PpkErrorType.BadPassphrase); } } } catch { throw; } finally { Array.Clear(computedHash, 0, computedHash.Length); } }