public override byte[] HostKeyMD5FingerPrint() { SSH1DataWriter wr = new SSH1DataWriter(); //RSA only for SSH1 RSAPublicKey rsa = (RSAPublicKey)_hostkey; wr.WriteBigInteger(rsa.Exponent); wr.WriteBigInteger(rsa.Modulus); return(new MD5CryptoServiceProvider().ComputeHash(wr.ToByteArray())); }
public override string DumpHostKeyInKnownHostsStyle() { StringBuilder bld = new StringBuilder(); bld.Append("ssh1 "); SSH1DataWriter wr = new SSH1DataWriter(); //RSA only for SSH1 RSAPublicKey rsa = (RSAPublicKey)_hostkey; wr.WriteBigInteger(rsa.Exponent); wr.WriteBigInteger(rsa.Modulus); bld.Append(Encoding.ASCII.GetString(Base64.Encode(wr.ToByteArray()))); return(bld.ToString()); }
//RSA authentication private void DoRSAChallengeResponse() { //read key SSH1UserAuthKey key = new SSH1UserAuthKey(_param.IdentityFile, _param.Password); SSH1DataWriter w = new SSH1DataWriter(); w.WriteBigInteger(key.PublicModulus); SSH1Packet p = SSH1Packet.FromPlainPayload(PacketType.SSH_CMSG_AUTH_RSA, w.ToByteArray()); p.WriteTo(_stream, _tCipher); TraceTransmissionEvent(PacketType.SSH_CMSG_AUTH_RSA, "RSA challenge-reponse"); DataFragment response = ReceivePacket(); SSH1DataReader reader = new SSH1DataReader(response); PacketType pt = reader.ReadPacketType(); if (pt == PacketType.SSH_SMSG_FAILURE) { throw new SSHException(Strings.GetString("ServerRefusedRSA")); } else if (pt != PacketType.SSH_SMSG_AUTH_RSA_CHALLENGE) { throw new SSHException(String.Format(Strings.GetString("UnexpectedResponse"), pt)); } TraceReceptionEvent(PacketType.SSH_SMSG_AUTH_RSA_CHALLENGE, "received challenge"); //creating challenge BigInteger challenge = key.decryptChallenge(reader.ReadMPInt()); byte[] rawchallenge = RSAUtil.StripPKCS1Pad(challenge, 2).getBytes(); //building response MemoryStream bos = new MemoryStream(); bos.Write(rawchallenge, 0, rawchallenge.Length); //!!mindtermでは頭が0かどうかで変なハンドリングがあった bos.Write(_sessionID, 0, _sessionID.Length); byte[] reply = new MD5CryptoServiceProvider().ComputeHash(bos.ToArray()); w = new SSH1DataWriter(); w.Write(reply); p = SSH1Packet.FromPlainPayload(PacketType.SSH_CMSG_AUTH_RSA_RESPONSE, w.ToByteArray()); p.WriteTo(_stream, _tCipher); TraceReceptionEvent(PacketType.SSH_CMSG_AUTH_RSA_RESPONSE, "received response"); }
private void SendSessionKey(byte[] session_key) { try { //step1 XOR with session_id byte[] working_data = new byte[session_key.Length]; byte[] session_id = CalcSessionID(); Array.Copy(session_key, 0, working_data, 0, session_key.Length); for (int i = 0; i < session_id.Length; i++) { working_data[i] ^= session_id[i]; } //step2 decrypts with RSA RSAPublicKey first_encryption; RSAPublicKey second_encryption; SSHServerInfo si = _cInfo._serverinfo; int first_key_bytelen, second_key_bytelen; if (si.server_key_bits < si.host_key_bits) { first_encryption = new RSAPublicKey(si.server_key_public_exponent, si.server_key_public_modulus); second_encryption = new RSAPublicKey(si.host_key_public_exponent, si.host_key_public_modulus); first_key_bytelen = (si.server_key_bits + 7) / 8; second_key_bytelen = (si.host_key_bits + 7) / 8; } else { first_encryption = new RSAPublicKey(si.host_key_public_exponent, si.host_key_public_modulus); second_encryption = new RSAPublicKey(si.server_key_public_exponent, si.server_key_public_modulus); first_key_bytelen = (si.host_key_bits + 7) / 8; second_key_bytelen = (si.server_key_bits + 7) / 8; } Rng rng = RngManager.GetSecureRng(); BigInteger first_result = RSAUtil.PKCS1PadType2(new BigInteger(working_data), first_key_bytelen, rng).modPow(first_encryption.Exponent, first_encryption.Modulus); BigInteger second_result = RSAUtil.PKCS1PadType2(first_result, second_key_bytelen, rng).modPow(second_encryption.Exponent, second_encryption.Modulus); //output SSH1DataWriter writer = new SSH1DataWriter(); writer.WriteByte((byte)_cInfo._algorithmForTransmittion); writer.Write(si.anti_spoofing_cookie); writer.WriteBigInteger(second_result); writer.WriteInt32(0); //protocol flags //send TraceTransmissionEvent(PacketType.SSH_CMSG_SESSION_KEY, "sent encrypted session-keys"); SSH1Packet packet = SSH1Packet.FromPlainPayload(PacketType.SSH_CMSG_SESSION_KEY, writer.ToByteArray()); packet.WriteTo(_stream); _sessionID = session_id; } catch (Exception e) { if (e is IOException) { throw (IOException)e; } else { string t = e.StackTrace; throw new SSHException(e.Message); //IOException以外はみなSSHExceptionにしてしまう } } }