Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add some tests for #581, already fixed in #569 #584

Merged
merged 1 commit into from
Jul 21, 2024
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
13 changes: 13 additions & 0 deletions src/JsonWebToken/Cryptography/EllipticalCurves.cs
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,19 @@ public static EllipticalCurve FromString(string crv)
return curve;
}


/// <summary>Cast the <see cref="string"/> into its <see cref="EllipticalCurve"/> representation.</summary>
public static explicit operator EllipticalCurve?(string? value)
{
if (value is null)
{
return null;
}

return FromString(value); ;
}


/// <summary>Tries to parse a <see cref="string"/> into a <see cref="EllipticalCurve"/>.</summary>
public static bool TryParse(string crv, [NotNullWhen(true)] out EllipticalCurve? curve)
{
Expand Down
19 changes: 3 additions & 16 deletions src/JsonWebToken/Jwk.cs
Original file line number Diff line number Diff line change
Expand Up @@ -641,22 +641,9 @@ public bool TryGetKeyUnwrapper(EncryptionAlgorithm? encryptionAlgorithm, KeyMana
public byte[] Canonicalize()
{
int size = GetCanonicalizeSize();
byte[]? arrayToReturn = null;
try
{
Span<byte> buffer = size > Constants.MaxStackallocBytes
? (arrayToReturn = ArrayPool<byte>.Shared.Rent(size))
: stackalloc byte[size];
Canonicalize(buffer);
return buffer.Slice(0, size).ToArray();
}
finally
{
if (arrayToReturn != null)
{
ArrayPool<byte>.Shared.Return(arrayToReturn);
}
}
var buffer = new byte[size];
Canonicalize(buffer);
return buffer;
}

/// <summary>Compute the normal form, as defined by https://tools.ietf.org/html/rfc7638#section-3.2, and writes it to the <paramref name="buffer"/>.</summary>
Expand Down
2 changes: 1 addition & 1 deletion src/JsonWebToken/PasswordBasedJwk.cs
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ public static PasswordBasedJwk FromPassphrase(string passphrase, KeyManagementAl
ThrowHelper.ThrowArgumentNullException(ExceptionArgument.bytes);
}

var innerKey = SymmetricJwk.FromByteArray(Utf8.GetBytes(passphrase), algorithm, computeThumbprint);
var innerKey = SymmetricJwk.FromByteArray(Utf8.GetBytes(passphrase), computeThumbprint);
return new PasswordBasedJwk(innerKey, iterationCount, saltSizeInBytes, algorithm);
}

Expand Down
16 changes: 11 additions & 5 deletions test/JsonWebToken.Tests/ECJwkTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ public void Equal(EllipticalCurve crv)
key.Kid = JsonEncodedText.Encode("X");
copiedKey.Kid = JsonEncodedText.Encode("Y");
Assert.NotEqual(key, copiedKey);

Assert.NotEqual(key, Jwk.None);
}

Expand Down Expand Up @@ -180,15 +180,21 @@ public override Signer CreateSigner_Failed(Jwk key, SignatureAlgorithm alg)
return base.CreateSigner_Failed(key, alg);
}

[Fact]
public override void Canonicalize()
[Theory]
[InlineData("ES256")]
[InlineData("ES384")]
[InlineData("ES512")]
[InlineData("ES256K")]
public override void Canonicalize(string alg)
{
var jwk = ECJwk.GeneratePrivateKey(SignatureAlgorithm.ES256);
var jwk = ECJwk.GeneratePrivateKey((SignatureAlgorithm)alg);
var canonicalizedKey = (ECJwk)CanonicalizeKey(jwk);

Assert.True(canonicalizedKey.D.IsEmpty);
bool supported = EllipticalCurve.TryGetSupportedCurve((SignatureAlgorithm)alg, out var crv);

Assert.Equal(EllipticalCurve.P256.Id, canonicalizedKey.Crv.Id);
Assert.True(supported);
Assert.Equal(crv.Id, canonicalizedKey.Crv.Id);
Assert.False(canonicalizedKey.X.IsEmpty);
Assert.False(canonicalizedKey.Y.IsEmpty);
}
Expand Down
2 changes: 1 addition & 1 deletion test/JsonWebToken.Tests/JwkTestsBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public virtual Signer CreateSigner_Failed(Jwk key, SignatureAlgorithm alg)
return signer;
}

public abstract void Canonicalize();
public abstract void Canonicalize(string alg);

public Jwk CanonicalizeKey(Jwk key)
{
Expand Down
12 changes: 8 additions & 4 deletions test/JsonWebToken.Tests/PasswordBasedJwkTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public void Equal()
key.Kid = JsonEncodedText.Encode("X");
copiedKey.Kid = JsonEncodedText.Encode("Y");
Assert.NotEqual(key, copiedKey);

Assert.NotEqual(key, Jwk.None);
}

Expand All @@ -39,10 +39,14 @@ public override KeyWrapper CreateKeyWrapper_Succeed(Jwk key, EncryptionAlgorithm
return base.CreateKeyWrapper_Succeed(key, enc, alg);
}

[Fact]
public override void Canonicalize()
[Theory]
[InlineData("PBES2-HS256+A128KW")]
[InlineData("PBES2-HS384+A192KW")]
[InlineData("PBES2-HS512+A256KW")]
public override void Canonicalize(string alg)
{
var jwk = PasswordBasedJwk.FromPassphrase("Thus from my lips, by yours, my sin is purged.");
var jwk = PasswordBasedJwk.FromPassphrase("Thus from my lips, by yours, my sin is purged.", (KeyManagementAlgorithm)alg); // something is wrong here... how to provide the PBES alg ?
//var jwk = PasswordBasedJwk.FromPassphrase("Thus from my lips, by yours, my sin is purged."); // something is wrong here... how to provide the PBES alg ?
var canonicalizedKey = (SymmetricJwk)CanonicalizeKey(jwk);
Assert.NotEmpty(canonicalizedKey.ToArray());
}
Expand Down
13 changes: 10 additions & 3 deletions test/JsonWebToken.Tests/RsaJwkTests.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Diagnostics.Contracts;
using System.Numerics;
using System.Security.Cryptography;
using System.Text;
Expand Down Expand Up @@ -286,10 +287,16 @@ public override Signer CreateSigner_Succeed(Jwk key, SignatureAlgorithm alg)
return base.CreateSigner_Succeed(key, alg);
}

[Fact]
public override void Canonicalize()
[Theory]
[InlineData("RS256")]
[InlineData("RS384")]
[InlineData("RS512")]
[InlineData("PS256")]
[InlineData("PS384")]
[InlineData("PS512")]
public override void Canonicalize(string alg)
{
var jwk = RsaJwk.GeneratePrivateKey(2048, SignatureAlgorithm.RS256);
var jwk = RsaJwk.GeneratePrivateKey(2048, (SignatureAlgorithm)alg);
var canonicalizedKey = (RsaJwk)CanonicalizeKey(jwk);
Assert.False(canonicalizedKey.E.IsEmpty);
Assert.False(canonicalizedKey.N.IsEmpty);
Expand Down
7 changes: 5 additions & 2 deletions test/JsonWebToken.Tests/SymmetricJwkTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,11 @@
return base.CreateSigner_Succeed(key, alg);
}

[Fact]
public override void Canonicalize()
[Theory]
[InlineData("HS256")]
[InlineData("HS384")]
[InlineData("HS512")]
public override void Canonicalize(string alg)

Check warning on line 53 in test/JsonWebToken.Tests/SymmetricJwkTests.cs

View workflow job for this annotation

GitHub Actions / Build & Test (ubuntu-latest, Debug)

Theory method 'Canonicalize' on test class 'SymmetricJwkTests' does not use parameter 'alg'.

Check warning on line 53 in test/JsonWebToken.Tests/SymmetricJwkTests.cs

View workflow job for this annotation

GitHub Actions / Build & Test (ubuntu-latest, Debug)

Theory method 'Canonicalize' on test class 'SymmetricJwkTests' does not use parameter 'alg'.

Check warning on line 53 in test/JsonWebToken.Tests/SymmetricJwkTests.cs

View workflow job for this annotation

GitHub Actions / Build & Test (ubuntu-latest, Release)

Theory method 'Canonicalize' on test class 'SymmetricJwkTests' does not use parameter 'alg'.

Check warning on line 53 in test/JsonWebToken.Tests/SymmetricJwkTests.cs

View workflow job for this annotation

GitHub Actions / Build & Test (ubuntu-latest, Release)

Theory method 'Canonicalize' on test class 'SymmetricJwkTests' does not use parameter 'alg'.

Check warning on line 53 in test/JsonWebToken.Tests/SymmetricJwkTests.cs

View workflow job for this annotation

GitHub Actions / Build & Test (windows-latest, Debug)

Theory method 'Canonicalize' on test class 'SymmetricJwkTests' does not use parameter 'alg'.

Check warning on line 53 in test/JsonWebToken.Tests/SymmetricJwkTests.cs

View workflow job for this annotation

GitHub Actions / Build & Test (windows-latest, Debug)

Theory method 'Canonicalize' on test class 'SymmetricJwkTests' does not use parameter 'alg'.

Check warning on line 53 in test/JsonWebToken.Tests/SymmetricJwkTests.cs

View workflow job for this annotation

GitHub Actions / Build & Test (windows-latest, Release)

Theory method 'Canonicalize' on test class 'SymmetricJwkTests' does not use parameter 'alg'.

Check warning on line 53 in test/JsonWebToken.Tests/SymmetricJwkTests.cs

View workflow job for this annotation

GitHub Actions / Build & Test (windows-latest, Release)

Theory method 'Canonicalize' on test class 'SymmetricJwkTests' does not use parameter 'alg'.
{
var jwk = SymmetricJwk.GenerateKey(SignatureAlgorithm.HS256);
var canonicalizedKey = (SymmetricJwk)CanonicalizeKey(jwk);
Expand Down
Loading