diff --git a/src/Titano.Protocol/Client/CertificatePinning.cs b/src/Titano.Protocol/Client/CertificatePinning.cs
index f905b429..6c24e37a 100644
--- a/src/Titano.Protocol/Client/CertificatePinning.cs
+++ b/src/Titano.Protocol/Client/CertificatePinning.cs
@@ -6,25 +6,39 @@ namespace Titano.Protocol.Client;
///
/// Trust-on-first-use certificate validation: a certificate is accepted when it chains to a trusted root or when
-/// its SHA-256 fingerprint is in the caller's trusted set. The last unknown fingerprint is remembered so the UI can
-/// ask the user once and add it.
+/// its SHA-256 fingerprint is in the caller's trusted set. The fingerprint rejected by the latest handshake is
+/// remembered (and cleared by the next accepted one) so the UI can ask the user once and add it.
///
public sealed class CertificatePinning(IEnumerable trustedFingerprints)
{
private readonly HashSet _trusted = new(trustedFingerprints.Select(Normalize), StringComparer.Ordinal);
+ private readonly Lock _gate = new();
+ private string? _lastRejected;
- public string? LastRejectedFingerprint { get; private set; }
+ public string? LastRejectedFingerprint
+ {
+ get { lock (_gate) return _lastRejected; }
+ }
- public void Trust(string fingerprint) => _trusted.Add(Normalize(fingerprint));
+ public void Trust(string fingerprint)
+ {
+ lock (_gate)
+ {
+ _trusted.Add(Normalize(fingerprint));
+ _lastRejected = null;
+ }
+ }
public bool Validate(object sender, X509Certificate? certificate, X509Chain? chain, SslPolicyErrors errors)
{
if (certificate is null) return false;
- if (errors == SslPolicyErrors.None) return true;
- var fingerprint = Fingerprint(certificate);
- if (_trusted.Contains(fingerprint)) return true;
- LastRejectedFingerprint = fingerprint;
- return false;
+ var fingerprint = errors == SslPolicyErrors.None ? null : Fingerprint(certificate);
+ lock (_gate)
+ {
+ var accepted = fingerprint is null || _trusted.Contains(fingerprint);
+ _lastRejected = accepted ? null : fingerprint;
+ return accepted;
+ }
}
public static string Fingerprint(X509Certificate certificate) =>
diff --git a/tests/Titano.Testing/TestRunner.cs b/tests/Titano.Testing/TestRunner.cs
index 813b5f9e..3b0576f6 100644
--- a/tests/Titano.Testing/TestRunner.cs
+++ b/tests/Titano.Testing/TestRunner.cs
@@ -253,8 +253,8 @@ public static class TestRunner
foreach (var r in results.OrderBy(r => r.Test.FullName, StringComparer.Ordinal))
{
w.WriteStartElement("testcase");
- w.WriteAttributeString("classname", r.Test.Type.FullName);
- w.WriteAttributeString("name", r.Test.FullName[(r.Test.Type.FullName!.Length + 1)..]);
+ w.WriteAttributeString("classname", Clean(r.Test.Type.FullName!));
+ w.WriteAttributeString("name", Clean(r.Test.FullName[(r.Test.Type.FullName!.Length + 1)..]));
w.WriteAttributeString("time", r.Duration.TotalSeconds.ToString("F3", CultureInfo.InvariantCulture));
if (r.Outcome == TestOutcome.Failed)
{
@@ -276,7 +276,29 @@ public static class TestRunner
w.WriteEndDocument();
}
- private static string Clean(string s) => new(s.Where(XmlConvert.IsXmlChar).ToArray());
+ /// Escapes characters XML cannot carry (theory cases may contain control characters) as \uXXXX.
+ private static string Clean(string s)
+ {
+ StringBuilder? sb = null;
+ for (var i = 0; i < s.Length; i++)
+ {
+ var c = s[i];
+ if (XmlConvert.IsXmlChar(c))
+ {
+ sb?.Append(c);
+ continue;
+ }
+ if (char.IsHighSurrogate(c) && i + 1 < s.Length && XmlConvert.IsXmlSurrogatePair(s[i + 1], c))
+ {
+ sb?.Append(c).Append(s[i + 1]);
+ i++;
+ continue;
+ }
+ sb ??= new StringBuilder(s, 0, i, s.Length + 8);
+ sb.Append(CultureInfo.InvariantCulture, $"\\u{(int)c:X4}");
+ }
+ return sb?.ToString() ?? s;
+ }
private sealed class Options
{