Skip to content

Commit

Permalink
Revert change making it possible to change FormatOptions.Default prop…
Browse files Browse the repository at this point in the history
…erty values

This reverts the following commits:
* 4b77a9f: Allow changing the FormatOptions.Default values
* e3bab7b: Improve Header.GetRawValue() logic
* 22881de: Cache more of the FormatOptions in each Header
* 9570ebd: Added FormatOptions.ReformatHeaders and fixed unit tests

...And reopens issue #993.

I will be revisiting this set of changes for v5.0 when I'll have more flexibility with
behavioral changes. The problem with this set was not that it "didn't work", but rather
that it changed expected behavior. I also wasn't sure if I really liked the new
FormatOptions.ReformatHeaders property naming.
  • Loading branch information
jstedfast committed Feb 7, 2024
1 parent 9570ebd commit d858945
Show file tree
Hide file tree
Showing 7 changed files with 131 additions and 236 deletions.
11 changes: 8 additions & 3 deletions MimeKit/Cryptography/ArcVerifier.cs
Original file line number Diff line number Diff line change
Expand Up @@ -396,6 +396,9 @@ async Task<bool> VerifyArcMessageSignatureAsync (FormatOptions options, MimeMess
if (!IsEnabled (signatureAlgorithm))
return false;

options = options.Clone ();
options.NewLineFormat = NewLineFormat.Dos;

// first check the body hash (if that's invalid, then the entire signature is invalid)
if (!VerifyBodyHash (options, message, signatureAlgorithm, bodyAlgorithm, maxLength, bh))
return false;
Expand Down Expand Up @@ -430,6 +433,9 @@ async Task<bool> VerifyArcSealAsync (FormatOptions options, ArcHeaderSet[] sets,
if ((key is RsaKeyParameters rsa) && rsa.Modulus.BitLength < MinimumRsaKeyLength)
return false;

options = options.Clone ();
options.NewLineFormat = NewLineFormat.Dos;

using (var stream = new DkimSignatureStream (CreateVerifyContext (algorithm, key))) {
using (var filtered = new FilteredStream (stream)) {
filtered.Add (options.CreateNewLineFilter ());
Expand Down Expand Up @@ -667,7 +673,6 @@ async Task<ArcValidationResult> VerifyAsync (FormatOptions options, MimeMessage
int newest = count - 1;

result.Seals = new ArcHeaderValidationResult[count];
options = GetVerifyOptions (options);

// validate the most recent Arc-Message-Signature
try {
Expand Down Expand Up @@ -783,7 +788,7 @@ public Task<ArcValidationResult> VerifyAsync (FormatOptions options, MimeMessage
/// </exception>
public ArcValidationResult Verify (MimeMessage message, CancellationToken cancellationToken = default)
{
return Verify (FormatOptions.VerifySignature, message, cancellationToken);
return Verify (FormatOptions.Default, message, cancellationToken);
}

/// <summary>
Expand All @@ -806,7 +811,7 @@ public ArcValidationResult Verify (MimeMessage message, CancellationToken cancel
/// </exception>
public Task<ArcValidationResult> VerifyAsync (MimeMessage message, CancellationToken cancellationToken = default)
{
return VerifyAsync (FormatOptions.VerifySignature, message, cancellationToken);
return VerifyAsync (FormatOptions.Default, message, cancellationToken);
}
}
}
7 changes: 4 additions & 3 deletions MimeKit/Cryptography/DkimVerifier.cs
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,8 @@ async Task<bool> VerifyAsync (FormatOptions options, MimeMessage message, Header
if (!IsEnabled (signatureAlgorithm))
return false;

options = GetVerifyOptions (options);
options = options.Clone ();
options.NewLineFormat = NewLineFormat.Dos;

// first check the body hash (if that's invalid, then the entire signature is invalid)
if (!VerifyBodyHash (options, message, signatureAlgorithm, bodyAlgorithm, maxLength, bh))
Expand Down Expand Up @@ -241,7 +242,7 @@ public Task<bool> VerifyAsync (FormatOptions options, MimeMessage message, Heade
/// </exception>
public bool Verify (MimeMessage message, Header dkimSignature, CancellationToken cancellationToken = default)
{
return Verify (FormatOptions.VerifySignature, message, dkimSignature, cancellationToken);
return Verify (FormatOptions.Default, message, dkimSignature, cancellationToken);
}

/// <summary>
Expand Down Expand Up @@ -273,7 +274,7 @@ public bool Verify (MimeMessage message, Header dkimSignature, CancellationToken
/// </exception>
public Task<bool> VerifyAsync (MimeMessage message, Header dkimSignature, CancellationToken cancellationToken = default)
{
return VerifyAsync (FormatOptions.VerifySignature, message, dkimSignature, cancellationToken);
return VerifyAsync (FormatOptions.Default, message, dkimSignature, cancellationToken);
}
}
}
16 changes: 0 additions & 16 deletions MimeKit/Cryptography/DkimVerifierBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -150,22 +150,6 @@ public bool IsEnabled (DkimSignatureAlgorithm algorithm)
return (enabledSignatureAlgorithms & (1 << (int) algorithm)) != 0;
}

/// <summary>
/// Get the formatting options for use with verifying DKIM signatures.
/// </summary>
/// <param name="format">The user-requested formatting options.</param>
/// <returns>The formatting options.</returns>
internal static FormatOptions GetVerifyOptions (FormatOptions format)
{
var options = format.Clone ();
options.NewLineFormat = NewLineFormat.Dos;
options.VerifyingSignature = true;
options.ReformatHeaders = false;
options.HiddenHeaders.Clear ();
options.EnsureNewLine = false;
return options;
}

static bool IsWhiteSpace (char c)
{
return c == ' ' || c == '\t';
Expand Down
2 changes: 0 additions & 2 deletions MimeKit/Cryptography/MultipartSigned.cs
Original file line number Diff line number Diff line change
Expand Up @@ -767,7 +767,6 @@ public DigitalSignatureCollection Verify (CryptographyContext ctx, CancellationT
var options = FormatOptions.Default.Clone ();
options.NewLineFormat = NewLineFormat.Dos;
options.VerifyingSignature = true;
options.ReformatHeaders = false;

this[0].WriteTo (options, cleartext, cancellationToken);
cleartext.Position = 0;
Expand Down Expand Up @@ -838,7 +837,6 @@ public async Task<DigitalSignatureCollection> VerifyAsync (CryptographyContext c
var options = FormatOptions.Default.Clone ();
options.NewLineFormat = NewLineFormat.Dos;
options.VerifyingSignature = true;
options.ReformatHeaders = false;

await this[0].WriteToAsync (options, cleartext, cancellationToken);
cleartext.Position = 0;
Expand Down
Loading

0 comments on commit d858945

Please sign in to comment.