Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -85,19 +85,24 @@ public class AuthTokenVersion11ValidatorTest : AbstractTestWithValidator
}

[TestCase("web-eid:1.1")]
public void WhenFormatIsV11ThenSupportsReturnsTrue(string format) =>
[TestCase("web-eid:1.2")]
[TestCase("web-eid:1.10")]
[TestCase("web-eid:1.999")]
public void WhenFormatIsV11OrHigherMinorVersionThenSupportsReturnsTrue(string format) =>
Assert.That(v11Validator.Supports(format), Is.True);

[TestCase(null)]
[TestCase("")]
[TestCase("web-eid:1")]
[TestCase("web-eid:1.0")]
[TestCase("web-eid:1.")]
[TestCase("web-eid:1.0TEST")]
[TestCase("web-eid:1.00")]
[TestCase("web-eid:1.1.0")]
[TestCase("web-eid:1.10")]
[TestCase("web-eid:1.2")]
[TestCase("web-eid:2")]
[TestCase("web-eid:0.9")]
[TestCase("webauthn:1.1")]
public void WhenFormatIsNullEmptyOrNotV11ThenSupportsReturnsFalse(string format) =>
public void WhenFormatIsNullEmptyMinorVersion0NonCanonicalOrMalformedThenSupportsReturnsFalse(string format) =>
Assert.That(v11Validator.Supports(format), Is.False);

[Test]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ public void SetUp()
[TestCase("web-eid:1")]
[TestCase("web-eid:1.0")]
[TestCase("web-eid:1.1")]
[TestCase("web-eid:1.2")]
[TestCase("web-eid:1.10")]
[TestCase("web-eid:1.999")]
public void WhenFormatIsValidMajorV1FormatThenSupportsReturnsTrue(string format) =>
Comment thread
aarmam marked this conversation as resolved.
Expand All @@ -95,6 +96,9 @@ public void WhenFormatIsValidMajorV1FormatThenSupportsReturnsTrue(string format)
[TestCase("web-eid")]
[TestCase("web-eid:1.")]
[TestCase("web-eid:1.0TEST")]
[TestCase("web-eid:1.00")]
[TestCase("web-eid:1.000")]
[TestCase("web-eid:01")]
[TestCase("web-eid:1.1.0")]
[TestCase("web-eid:0.9")]
[TestCase("web-eid:2")]
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
/*
* Copyright © 2025-2025 Estonian Information System Authority
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
namespace WebEid.Security.Tests.Validator.VersionValidators
{
using NUnit.Framework;
using WebEid.Security.Validator.VersionValidators;

public class AuthTokenVersionTest
{
[TestCase("web-eid:1", 1, 0, true)]
[TestCase("web-eid:1.0", 1, 0, true)]
[TestCase("web-eid:1.1", 1, 0, true)]
[TestCase("web-eid:1.1", 1, 1, true)]
[TestCase("web-eid:1.999", 1, 1, true)]
[TestCase("web-eid:2.0", 2, 0, true)]
[TestCase("web-eid:2.3", 2, 1, true)]
[TestCase("web-eid:1.0", 1, 1, false)]
[TestCase("web-eid:1", 1, 1, false)]
[TestCase("web-eid:2", 1, 0, false)]
[TestCase("web-eid:1.5", 2, 0, false)]
[TestCase("web-eid:1.00", 1, 0, false)]
[TestCase("web-eid:1.000", 1, 0, false)]
[TestCase("web-eid:01", 1, 0, false)]
[TestCase("web-eid:1.", 1, 0, false)]
[TestCase("web-eid:1.1.0", 1, 0, false)]
[TestCase("web-eid:0.9", 1, 0, false)]
[TestCase("webauthn:1", 1, 0, false)]
public void WhenFormatMatchesRequiredMajorAndAtLeastRequiredMinorThenSupportsReturnsExpected(
string format, int requiredMajorVersion, int requiredMinorVersion, bool expected) =>
Assert.That(
AuthTokenVersion.Supports(format, requiredMajorVersion, requiredMinorVersion),
Is.EqualTo(expected));

[Test]
public void WhenFormatIsNullThenSupportsReturnsFalse() =>
Assert.That(AuthTokenVersion.Supports(null, 1, 0), Is.False);

[TestCase("web-eid:1", 1, 0, true)]
[TestCase("web-eid:1.0", 1, 0, true)]
[TestCase("web-eid:1.1", 1, 1, true)]
[TestCase("web-eid:2.0", 2, 0, true)]
[TestCase("web-eid:1.1", 1, 0, false)]
[TestCase("web-eid:1.2", 1, 1, false)]
[TestCase("web-eid:1.0", 1, 1, false)]
[TestCase("web-eid:1", 2, 0, false)]
[TestCase("web-eid:1.00", 1, 0, false)]
[TestCase("web-eid:01", 1, 0, false)]
[TestCase("webauthn:1", 1, 0, false)]
public void WhenFormatMatchesRequiredMajorAndExactMinorThenSupportsExactlyReturnsExpected(
string format, int requiredMajorVersion, int requiredMinorVersion, bool expected) =>
Assert.That(
AuthTokenVersion.SupportsExactly(format, requiredMajorVersion, requiredMinorVersion),
Is.EqualTo(expected));

[Test]
public void WhenFormatIsNullThenSupportsExactlyReturnsFalse() =>
Assert.That(AuthTokenVersion.SupportsExactly(null, 1, 0), Is.False);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
/*
* Copyright © 2025-2025 Estonian Information System Authority
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
namespace WebEid.Security.Validator.VersionValidators
{
using System.Globalization;
using System.Text.RegularExpressions;

/// <summary>
/// Utility for matching Web eID authentication token format version strings of the form
/// <c>web-eid:&lt;major&gt;[.&lt;minor&gt;]</c>, e.g. <c>web-eid:1</c> or <c>web-eid:1.1</c>.
/// </summary>
internal static partial class AuthTokenVersion
{
// Matches 'web-eid:<major>' with an optional canonical '.<minor>', where both numbers have no leading
// zeros ('0' or '[1-9]\d*') and are at most 9 digits so that they always fit in an int. Non-canonical
// spellings such as 'web-eid:1.00' or 'web-eid:01' are rejected so that ambiguous version numbers cannot
// bypass the more specific validators.
[GeneratedRegex(@"^web-eid:(0|[1-9]\d{0,8})(?:\.(0|[1-9]\d{0,8}))?$")]
private static partial Regex TokenFormatRegex();

/// <summary>
/// Returns whether the given token format has exactly the required major version and a minor version that
/// is greater than or equal to the required minor version. A missing minor version is treated as 0.
/// Backwards-compatible minor version changes are supported within the same major version, while an
/// incompatible major version change is not.
/// </summary>
internal static bool Supports(string format, int requiredExactMajorVersion, int requiredMinimalMinorVersion)
{
var version = Parse(format);
return version.HasValue &&
version.Value.Major == requiredExactMajorVersion &&
version.Value.Minor >= requiredMinimalMinorVersion;
}

/// <summary>
/// Returns whether the given token format has exactly the required major version and exactly the required
/// minor version. A missing minor version is treated as 0.
/// </summary>
internal static bool SupportsExactly(string format, int requiredExactMajorVersion, int requiredExactMinorVersion)
{
var version = Parse(format);
return version.HasValue &&
version.Value.Major == requiredExactMajorVersion &&
version.Value.Minor == requiredExactMinorVersion;
}

private static (int Major, int Minor)? Parse(string format)
{
if (format == null)
{
return null;
}

var match = TokenFormatRegex().Match(format);
if (!match.Success)
{
return null;
}

var major = int.Parse(match.Groups[1].Value, CultureInfo.InvariantCulture);
var minor = match.Groups[2].Success
? int.Parse(match.Groups[2].Value, CultureInfo.InvariantCulture)
: 0;

return (major, minor);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ namespace WebEid.Security.Validator.VersionValidators
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography.X509Certificates;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using AuthToken;
using CertValidators;
Expand All @@ -37,13 +36,12 @@ namespace WebEid.Security.Validator.VersionValidators
using Util;

/// <summary>
/// Validator for token format web-eid:1.1.
/// Validator for token formats with major version 1 and minor version 1 or higher, e.g. web-eid:1.1.
/// Extends V1 validator with additional checks for signing certificate + supported algorithms.
/// </summary>
public sealed partial class AuthTokenVersion11Validator : AuthTokenVersion1Validator
public sealed class AuthTokenVersion11Validator : AuthTokenVersion1Validator
{
[GeneratedRegex(@"^web-eid:1\.1$", RegexOptions.IgnoreCase)]
private static partial Regex V11SupportedTokenFormatPattern();
private const int SupportedMinimalMinorVersion = 1;

private static readonly HashSet<string> SupportedSigningCryptoAlgorithms =
new(StringComparer.OrdinalIgnoreCase)
Expand Down Expand Up @@ -79,7 +77,8 @@ public sealed partial class AuthTokenVersion11Validator : AuthTokenVersion1Valid
private readonly ILogger logger;

/// <summary>
/// Initializes a validator for Web eID authentication tokens in format <c>web-eid:1.1</c>.
/// Initializes a validator for Web eID authentication tokens with token format major version 1
/// and minor version 1 or higher.
/// </summary>
internal AuthTokenVersion11Validator(
SubjectCertificateValidatorBatch simpleSubjectCertificateValidators,
Expand All @@ -97,13 +96,15 @@ internal AuthTokenVersion11Validator(
this.logger = logger;
}

/// <inheritdoc />
protected override Regex GetSupportedFormatPattern() =>
V11SupportedTokenFormatPattern();
/// <summary>
/// Determines whether this validator supports the specified token format.
/// </summary>
public override bool Supports(string format) =>
AuthTokenVersion.Supports(format, SupportedExactMajorVersion, SupportedMinimalMinorVersion);

/// <summary>
/// Validates a Web eID authentication token in format <c>web-eid:1.1</c>
/// and returns the authenticated user's certificate.
/// Validates a Web eID authentication token with token format major version 1
/// and minor version 1 or higher, and returns the authenticated user's certificate.
/// </summary>
public override async Task<X509Certificate2> Validate(WebEidAuthToken authToken, string currentChallengeNonce)
{
Expand Down Expand Up @@ -154,7 +155,7 @@ private static List<X509Certificate2> ValidateSigningCertificates(WebEidAuthToke
if (signingCertificates == null || signingCertificates.Count == 0)
{
throw new AuthTokenParseException(
"'unverifiedSigningCertificates' field is missing, null or empty for format 'web-eid:1.1'");
$"'unverifiedSigningCertificates' field is missing, null or empty for format '{token.Format}'");
}

var result = new List<X509Certificate2>();
Expand All @@ -164,7 +165,7 @@ private static List<X509Certificate2> ValidateSigningCertificates(WebEidAuthToke
if (certificate == null || string.IsNullOrEmpty(certificate.Certificate))
{
throw new AuthTokenParseException(
"'unverifiedSigningCertificates' contains a null or empty entry for format 'web-eid:1.1'");
$"'unverifiedSigningCertificates' contains a null or empty entry for format '{token.Format}'");
}

ValidateSupportedSignatureAlgorithms(certificate);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,7 @@
*/
namespace WebEid.Security.Validator.VersionValidators
{
using System;
using System.Security.Cryptography.X509Certificates;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using AuthToken;
using CertValidators;
Expand All @@ -33,14 +31,12 @@ namespace WebEid.Security.Validator.VersionValidators
using Util;

/// <summary>
/// Validator for token format web-eid:1.0.
/// Validator for token formats with major version 1, e.g. web-eid:1 and web-eid:1.0.
/// </summary>
public partial class AuthTokenVersion1Validator : IAuthTokenVersionValidator
{
private const string V1_SUPPORTED_TOKEN_FORMAT_PREFIX = "web-eid:1";

[GeneratedRegex(@"^web-eid:1(?:\.\d+)?$", RegexOptions.IgnoreCase)]
private static partial Regex V1SupportedTokenFormatPattern();
internal const int SupportedExactMajorVersion = 1;
private const int SupportedMinimalMinorVersion = 0;

private readonly SubjectCertificateValidatorBatch simpleSubjectCertificateValidators;
private readonly AuthTokenSignatureValidator signatureValidator;
Expand All @@ -50,7 +46,7 @@ public partial class AuthTokenVersion1Validator : IAuthTokenVersionValidator
private readonly ILogger logger;

/// <summary>
/// Initializes a validator for Web eID authentication tokens in format <c>web-eid:1.0</c>.
/// Initializes a validator for Web eID authentication tokens with token format major version 1.
/// </summary>
internal AuthTokenVersion1Validator(
SubjectCertificateValidatorBatch simpleSubjectCertificateValidators,
Expand All @@ -72,21 +68,15 @@ internal AuthTokenVersion1Validator(
/// Determines whether this validator supports the specified token format.
/// </summary>
public virtual bool Supports(string format) =>
format != null &&
GetSupportedFormatPattern().IsMatch(format);

/// <summary>
/// Gets the supported token format pattern for this validator.
/// </summary>
protected virtual Regex GetSupportedFormatPattern() =>
V1SupportedTokenFormatPattern();
AuthTokenVersion.Supports(format, SupportedExactMajorVersion, SupportedMinimalMinorVersion);

/// <summary>
/// Validates a Web eID authentication token and returns the authenticated user's certificate.
/// </summary>
public virtual async Task<X509Certificate2> Validate(WebEidAuthToken authToken, string currentChallengeNonce)
{
if (IsExactV10Format(authToken.Format) && authToken.UnverifiedSigningCertificates != null)
if (AuthTokenVersion.SupportsExactly(authToken.Format, SupportedExactMajorVersion, SupportedMinimalMinorVersion) &&
authToken.UnverifiedSigningCertificates != null)
{
throw new AuthTokenParseException($"'unverifiedSigningCertificates' field is not allowed for format '{authToken.Format}'");
}
Expand Down Expand Up @@ -123,9 +113,5 @@ public virtual async Task<X509Certificate2> Validate(WebEidAuthToken authToken,

return subjectCertificate;
}

private static bool IsExactV10Format(string format) =>
V1_SUPPORTED_TOKEN_FORMAT_PREFIX.Equals(format, StringComparison.OrdinalIgnoreCase) ||
"web-eid:1.0".Equals(format, StringComparison.OrdinalIgnoreCase);
}
}
Loading