public void HandshakeCompletionShouldBeLoggedWithCipherDetails() { var smtpClientSimulator = new TcpConnection(); smtpClientSimulator.Connect(25002); var banner = smtpClientSimulator.Receive(); var capabilities1 = smtpClientSimulator.SendAndReceive("EHLO example.com\r\n"); Assert.IsTrue(capabilities1.Contains("STARTTLS")); smtpClientSimulator.SendAndReceive("STARTTLS\r\n"); smtpClientSimulator.HandshakeAsClient(); var capabilities2 = smtpClientSimulator.SendAndReceive("EHLO example.com\r\n"); var default_log = LogHandler.ReadCurrentDefaultLog(); Assert.IsTrue(default_log.Contains("Version: TLS")); Assert.IsTrue(default_log.Contains("Cipher: ")); Assert.IsTrue(default_log.Contains("Bits: ")); }
public void Send(bool useStartTls, string username, string password, string sFrom, string sTo, string sSubject, string sBody, out string errorMessage) { string sData; _tcpConnection.Connect(_ipaddress, _port); // Receive welcome message. var welcomeMessage = _tcpConnection.Receive(); if (!welcomeMessage.StartsWith("220") || !welcomeMessage.EndsWith("\r\n")) { throw new Exception("Received unexpected welcome message: " + welcomeMessage); } if (useStartTls) { var capabilities1 = SendAndReceive("EHLO example.com\r\n"); if (!capabilities1.Contains("STARTTLS")) { throw new DeliveryFailedException("Server does not support STARTTLS."); } SendAndReceive("STARTTLS\r\n"); _tcpConnection.HandshakeAsClient(); } if (!string.IsNullOrEmpty(username)) { if (!Logon(EncodeBase64(username), EncodeBase64(password), out errorMessage)) { throw new DeliveryFailedException("Login failed: " + errorMessage); } } _tcpConnection.Send("HELO example.com\r\n"); var helloResponse = _tcpConnection.Receive(); if (helloResponse != "250 Hello.\r\n") { throw new DeliveryFailedException("Unexpected response to HELO from server: " + helloResponse); } // User _tcpConnection.Send("MAIL FROM:<" + sFrom + ">\r\n"); var mailFromResponse = _tcpConnection.Receive(); if (mailFromResponse != "250 OK\r\n") { throw new DeliveryFailedException("Unexpected response to HELO from server: " + helloResponse); } _tcpConnection.Send("RCPT TO:<" + sTo + ">\r\n"); var rcptToResponse = _tcpConnection.Receive(); if (!rcptToResponse.StartsWith("2") || !rcptToResponse.EndsWith("\r\n")) { errorMessage = TrimNewlline(rcptToResponse); throw new DeliveryFailedException("Unexpected response from server: " + errorMessage); } // Select inbox _tcpConnection.Send("DATA\r\n"); var dataResponse = _tcpConnection.Receive(); if (!dataResponse.StartsWith("354") || !dataResponse.EndsWith("\r\n")) { errorMessage = TrimNewlline(dataResponse); throw new DeliveryFailedException("Unexpected response from server: " + errorMessage); } _tcpConnection.Send("From: " + sFrom + "\r\n"); _tcpConnection.Send("To: " + sTo + "\r\n"); _tcpConnection.Send("Subject: " + sSubject + "\r\n"); _tcpConnection.Send("\r\n"); // Send body _tcpConnection.Send(sBody); _tcpConnection.Send("\r\n"); _tcpConnection.Send(".\r\n"); // Wait for OK. sData = _tcpConnection.Receive(); if (sData.Substring(0, 3) != "250") { errorMessage = TrimNewlline(sData); throw new DeliveryFailedException("Unexpected response from server: " + errorMessage); } // Quit again _tcpConnection.Send("QUIT\r\n"); _tcpConnection.Receive(); _tcpConnection.Disconnect(); errorMessage = ""; }
public void Handshake() { _tcpConnection.HandshakeAsClient(); }
public void IfStlsRequiredLogonShouldSucceedIfStls() { var smtpClientSimulator = new TcpConnection(); smtpClientSimulator.Connect(25003); var banner = smtpClientSimulator.Receive(); var capabilities1 = smtpClientSimulator.SendAndReceive("EHLO example.com\r\n"); Assert.IsTrue(capabilities1.Contains("STARTTLS")); smtpClientSimulator.SendAndReceive("STARTTLS\r\n"); smtpClientSimulator.HandshakeAsClient(); var loginResult = smtpClientSimulator.SendAndReceive("AUTH LOGIN\r\n"); Assert.IsTrue(loginResult.StartsWith("334")); }
public void StartTlsCommandShouldSwithToTls() { var smtpClientSimulator = new TcpConnection(); smtpClientSimulator.Connect(25002); var banner = smtpClientSimulator.Receive(); var capabilities1 = smtpClientSimulator.SendAndReceive("EHLO example.com\r\n"); Assert.IsTrue(capabilities1.Contains("STARTTLS")); smtpClientSimulator.SendAndReceive("STARTTLS\r\n"); smtpClientSimulator.HandshakeAsClient(); // Send a command over TLS. var capabilities2 = smtpClientSimulator.SendAndReceive("EHLO example.com\r\n"); Assert.IsFalse(capabilities2.Contains("STARTTLS")); // We're now on SSL. }
public void TestPlaintextCommandInjection() { var smtpClientSimulator = new TcpConnection(); smtpClientSimulator.Connect(25002); var banner = smtpClientSimulator.Receive(); var capabilities1 = smtpClientSimulator.SendAndReceive("EHLO example.com\r\n"); Assert.IsTrue(capabilities1.Contains("STARTTLS")); var resp = smtpClientSimulator.SendAndReceive("STARTTLS\r\nRSET\r\n"); Assert.AreEqual("220 Ready to start TLS\r\n", resp); smtpClientSimulator.HandshakeAsClient(); var quitResponse = smtpClientSimulator.SendAndReceive("QUIT\r\n"); Assert.AreEqual(quitResponse, "221 goodbye\r\n"); var default_log = LogHandler.ReadCurrentDefaultLog(); Assert.IsFalse(default_log.Contains("RSET")); }
public bool Send(bool useStartTls, string username, string password, string sFrom, string sTo, string sSubject, string sBody, out string result) { string sData; _tcpConnection.Connect(_ipaddress, _port); // Receive welcome message. _tcpConnection.Receive(); if (useStartTls) { var capabilities1 = SendAndReceive("EHLO example.com\r\n"); CustomAssert.IsTrue(capabilities1.Contains("STARTTLS")); SendAndReceive("STARTTLS\r\n"); CustomAssert.IsTrue(_tcpConnection.HandshakeAsClient()); } if (!string.IsNullOrEmpty(username)) { if (!Logon(EncodeBase64(username), EncodeBase64(password), out result)) { return(false); } } _tcpConnection.Send("HELO example.com\r\n"); sData = _tcpConnection.Receive(); // User _tcpConnection.Send("MAIL FROM:<" + sFrom + ">\r\n"); sData = _tcpConnection.Receive(); _tcpConnection.Send("RCPT TO:<" + sTo + ">\r\n"); sData = _tcpConnection.Receive(); if (sData.StartsWith("2") == false) { result = TrimNewlline(sData); return(false); } // Select inbox _tcpConnection.Send("DATA\r\n"); _tcpConnection.Receive(); _tcpConnection.Send("From: " + sFrom + "\r\n"); _tcpConnection.Send("To: " + sTo + "\r\n"); _tcpConnection.Send("Subject: " + sSubject + "\r\n"); _tcpConnection.Send("\r\n"); // Send body _tcpConnection.Send(sBody); _tcpConnection.Send("\r\n"); _tcpConnection.Send(".\r\n"); // Wait for OK. sData = _tcpConnection.Receive(); if (sData.Substring(0, 3) != "250") { result = TrimNewlline(sData); return(false); } // Quit again _tcpConnection.Send("QUIT\r\n"); _tcpConnection.Receive(); _tcpConnection.Disconnect(); result = ""; return(true); }