|
| 1 | +// Copyright (c) 2026 Christopher Holloway / Progressive Robot Ltd |
| 2 | +// http://www.hmailserver.com |
| 3 | + |
| 4 | +using System; |
| 5 | +using System.Security.Cryptography; |
| 6 | +using System.Security.Cryptography.X509Certificates; |
| 7 | +using NUnit.Framework; |
| 8 | +using RegressionTests.POP3; |
| 9 | +using RegressionTests.Shared; |
| 10 | + |
| 11 | +namespace RegressionTests.SSL |
| 12 | +{ |
| 13 | + /// <summary> |
| 14 | + /// SCRAM-SHA-256-PLUS (RFC 5802 / RFC 7677) with tls-server-end-point channel |
| 15 | + /// binding (RFC 5929) over a real TLS POP3 connection (RFC 5034). These tests |
| 16 | + /// require TLS, so they live with the SSL suite and configure the SSL ports in |
| 17 | + /// each test. |
| 18 | + /// </summary> |
| 19 | + [TestFixture] |
| 20 | + public class ScramPlusPop3 : TestFixtureBase |
| 21 | + { |
| 22 | + // Ports created by SslSetup.SetupSSLPorts. |
| 23 | + private const int Pop3PlainPort = 11000; // eCSNone |
| 24 | + private const int Pop3TlsPort = 11001; // eCSTLS (implicit TLS) |
| 25 | + |
| 26 | + [Test] |
| 27 | + [Description("RFC 5802/5929 (SCRAM-SHA-256-PLUS over POP3): CAPA advertises SASL " + |
| 28 | + "SCRAM-SHA-256-PLUS over TLS (where channel binding is possible) but never on a " + |
| 29 | + "plain connection.")] |
| 30 | + public void TestScramPlusAdvertisedOnTlsOnly() |
| 31 | + { |
| 32 | + SslSetup.SetupSSLPorts(_application); |
| 33 | + SingletonProvider<TestSetup>.Instance.AddAccount(_domain, "pluspop3cap@example.test", "test"); |
| 34 | + |
| 35 | + using (var con = new TcpConnection(true)) |
| 36 | + { |
| 37 | + Assert.IsTrue(con.Connect(Pop3TlsPort), "Could not connect to TLS POP3."); |
| 38 | + con.ReadUntil("+OK"); // banner |
| 39 | + con.Send("CAPA\r\n"); |
| 40 | + string caps = con.Receive(); |
| 41 | + Assert.IsTrue(caps.Contains("SCRAM-SHA-256-PLUS"), |
| 42 | + "TLS CAPA must advertise SASL SCRAM-SHA-256-PLUS. " + caps); |
| 43 | + } |
| 44 | + |
| 45 | + using (var con = new TcpConnection()) |
| 46 | + { |
| 47 | + Assert.IsTrue(con.Connect(Pop3PlainPort), "Could not connect to plaintext POP3."); |
| 48 | + con.ReadUntil("+OK"); // banner |
| 49 | + con.Send("CAPA\r\n"); |
| 50 | + string caps = con.Receive(); |
| 51 | + Assert.IsFalse(caps.Contains("SCRAM-SHA-256-PLUS"), |
| 52 | + "A plain connection must not advertise SCRAM-SHA-256-PLUS. " + caps); |
| 53 | + Assert.IsTrue(caps.Contains("SCRAM-SHA-256"), |
| 54 | + "A plain connection should still advertise the non-PLUS SCRAM-SHA-256. " + caps); |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + [Test] |
| 59 | + [Description("RFC 5802/5929 (SCRAM-SHA-256-PLUS over POP3): a full channel-bound exchange " + |
| 60 | + "authenticates a PBKDF2 account over TLS and the server proves it knows the key.")] |
| 61 | + public void TestScramPlusAuthenticates() |
| 62 | + { |
| 63 | + SslSetup.SetupSSLPorts(_application); |
| 64 | + SingletonProvider<TestSetup>.Instance.AddAccount(_domain, "pluspop3ok@example.test", "SeC-r3t Pass!"); |
| 65 | + |
| 66 | + using (var con = new TcpConnection(true)) |
| 67 | + { |
| 68 | + Assert.IsTrue(con.Connect(Pop3TlsPort), "Could not connect to TLS POP3."); |
| 69 | + con.ReadUntil("+OK"); // banner |
| 70 | + |
| 71 | + byte[] cbind = TlsServerEndPoint(con.RemoteCertificate); |
| 72 | + string final = Pop3SaslTestClient.AuthenticateScramPlus(con, "pluspop3ok@example.test", "SeC-r3t Pass!", cbind); |
| 73 | + Assert.IsTrue(final.StartsWith("+OK"), |
| 74 | + "SCRAM-SHA-256-PLUS authentication should succeed (+OK). Got: " + final); |
| 75 | + |
| 76 | + // The session must be authenticated and usable afterwards. |
| 77 | + con.Send("STAT\r\n"); |
| 78 | + Assert.IsTrue(con.Receive().StartsWith("+OK"), |
| 79 | + "Session should be usable after a PLUS logon."); |
| 80 | + con.Send("QUIT\r\n"); |
| 81 | + con.Disconnect(); |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + [Test] |
| 86 | + [Description("RFC 5929 (SCRAM-SHA-256-PLUS over POP3): a channel binding that does not match the " + |
| 87 | + "server certificate is rejected even with the correct password, which is exactly the " + |
| 88 | + "man-in-the-middle case channel binding defends against.")] |
| 89 | + public void TestScramPlusWrongBindingFails() |
| 90 | + { |
| 91 | + SslSetup.SetupSSLPorts(_application); |
| 92 | + bool autoBan = _settings.AutoBanOnLogonFailure; |
| 93 | + _settings.AutoBanOnLogonFailure = false; |
| 94 | + _settings.ClearLogonFailureList(); |
| 95 | + try |
| 96 | + { |
| 97 | + SingletonProvider<TestSetup>.Instance.AddAccount(_domain, "pluspop3mitm@example.test", "correct horse"); |
| 98 | + |
| 99 | + using (var con = new TcpConnection(true)) |
| 100 | + { |
| 101 | + Assert.IsTrue(con.Connect(Pop3TlsPort), "Could not connect to TLS POP3."); |
| 102 | + con.ReadUntil("+OK"); // banner |
| 103 | + |
| 104 | + byte[] cbind = TlsServerEndPoint(con.RemoteCertificate); |
| 105 | + // Simulate an attacker on a different TLS channel: the binding no longer |
| 106 | + // matches the certificate the server presents. |
| 107 | + cbind[0] ^= 0xFF; |
| 108 | + |
| 109 | + string final = Pop3SaslTestClient.AuthenticateScramPlus(con, "pluspop3mitm@example.test", "correct horse", cbind); |
| 110 | + Assert.IsTrue(final.StartsWith("-ERR"), |
| 111 | + "A mismatched channel binding must be rejected even with the right password. Got: " + final); |
| 112 | + } |
| 113 | + } |
| 114 | + finally |
| 115 | + { |
| 116 | + _settings.AutoBanOnLogonFailure = autoBan; |
| 117 | + _settings.ClearLogonFailureList(); |
| 118 | + } |
| 119 | + } |
| 120 | + |
| 121 | + [Test] |
| 122 | + [Description("SCRAM-SHA-256-PLUS over POP3 requires a TLS channel: the mechanism is refused on a " + |
| 123 | + "plain connection where no channel binding is available.")] |
| 124 | + public void TestScramPlusRejectedWithoutTls() |
| 125 | + { |
| 126 | + SslSetup.SetupSSLPorts(_application); |
| 127 | + bool autoBan = _settings.AutoBanOnLogonFailure; |
| 128 | + _settings.AutoBanOnLogonFailure = false; |
| 129 | + _settings.ClearLogonFailureList(); |
| 130 | + try |
| 131 | + { |
| 132 | + using (var con = new TcpConnection()) |
| 133 | + { |
| 134 | + Assert.IsTrue(con.Connect(Pop3PlainPort), "Could not connect to plaintext POP3."); |
| 135 | + con.ReadUntil("+OK"); // banner |
| 136 | + |
| 137 | + con.Send("AUTH SCRAM-SHA-256-PLUS\r\n"); |
| 138 | + string resp = con.Receive(); |
| 139 | + Assert.IsTrue(resp.StartsWith("-ERR"), |
| 140 | + "SCRAM-SHA-256-PLUS must be refused without TLS. Got: " + resp); |
| 141 | + } |
| 142 | + } |
| 143 | + finally |
| 144 | + { |
| 145 | + _settings.AutoBanOnLogonFailure = autoBan; |
| 146 | + _settings.ClearLogonFailureList(); |
| 147 | + } |
| 148 | + } |
| 149 | + |
| 150 | + /// <summary> |
| 151 | + /// Computes the RFC 5929 'tls-server-end-point' channel binding for a server |
| 152 | + /// certificate: the hash of the DER certificate using the certificate's |
| 153 | + /// signature hash, with MD5/SHA-1 substituted by SHA-256. |
| 154 | + /// </summary> |
| 155 | + private static byte[] TlsServerEndPoint(X509Certificate cert) |
| 156 | + { |
| 157 | + Assert.IsNotNull(cert, "No server certificate was negotiated."); |
| 158 | + |
| 159 | + var cert2 = new X509Certificate2(cert); |
| 160 | + HashAlgorithm hash; |
| 161 | + switch (cert2.SignatureAlgorithm.Value) |
| 162 | + { |
| 163 | + case "1.2.840.113549.1.1.12": // sha384RSA |
| 164 | + case "1.2.840.10045.4.3.3": // ecdsa-with-SHA384 |
| 165 | + hash = SHA384.Create(); |
| 166 | + break; |
| 167 | + case "1.2.840.113549.1.1.13": // sha512RSA |
| 168 | + case "1.2.840.10045.4.3.4": // ecdsa-with-SHA512 |
| 169 | + hash = SHA512.Create(); |
| 170 | + break; |
| 171 | + default: // sha256RSA / ecdsa-with-SHA256 and the MD5/SHA-1 -> SHA-256 substitution |
| 172 | + hash = SHA256.Create(); |
| 173 | + break; |
| 174 | + } |
| 175 | + |
| 176 | + using (hash) |
| 177 | + return hash.ComputeHash(cert.GetRawCertData()); |
| 178 | + } |
| 179 | + } |
| 180 | +} |
0 commit comments