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

Feature: Add extension method to convert EdDsaSecurityKey to JsonWebKey #13

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
@@ -0,0 +1,8 @@
namespace ScottBrady.IdentityModel.Crypto
{
public static class ExtendedJsonWebAlgorithmsKeyTypes
{
// https://datatracker.ietf.org/doc/html/draft-ietf-jose-cfrg-curves-06#section-2
public const string ECDH = "OKP";
scottbrady91 marked this conversation as resolved.
Show resolved Hide resolved
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
using Microsoft.IdentityModel.Tokens;
using ScottBrady.IdentityModel.Crypto;
using ScottBrady.IdentityModel.Tokens;

namespace ScottBrady.IdentityModel.Extensions
{
public static class ExtendedJsonWebKeyConverter
{
public static JsonWebKey ConvertFromEdDsaSecurityKey(EdDsaSecurityKey securityKey)
{
var parameters = securityKey.EdDsa.Parameters;
return new JsonWebKey
{
Crv = parameters.Curve,
X = parameters.X != null ? Base64UrlEncoder.Encode(parameters.X) : null,
D = parameters.D != null ? Base64UrlEncoder.Encode(parameters.D) : null,
Kty = ExtendedJsonWebAlgorithmsKeyTypes.ECDH,
Alg = ExtendedSecurityAlgorithms.EdDsa,
CryptoProviderFactory = securityKey.CryptoProviderFactory,
};
}
}
}
6 changes: 3 additions & 3 deletions src/ScottBrady.IdentityModel/Tokens/EdDsa.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Linq;
using System.Security.Cryptography;
using Org.BouncyCastle.Crypto;
using Org.BouncyCastle.Crypto.Generators;
using Org.BouncyCastle.Crypto.Parameters;
Expand All @@ -9,7 +10,7 @@

namespace ScottBrady.IdentityModel.Tokens;

public class EdDsa
public class EdDsa: AsymmetricAlgorithm
scottbrady91 marked this conversation as resolved.
Show resolved Hide resolved
{
internal EdDsaParameters Parameters { get; private init; }

Expand All @@ -36,7 +37,6 @@ public static EdDsa Create(string curve)
var generator = new Ed25519KeyPairGenerator();
generator.Init(new Ed25519KeyGenerationParameters(new SecureRandom()));
var keyPair = generator.GenerateKeyPair();

return new EdDsa {Parameters = new EdDsaParameters(keyPair, curve)};
}

Expand All @@ -60,7 +60,7 @@ public static EdDsa CreateFromJwk(string jwk)
{
throw new NotImplementedException();
}

public byte[] Sign(byte[] input)
{
if (input == null) throw new ArgumentNullException(nameof(input));
Expand Down
2 changes: 1 addition & 1 deletion src/ScottBrady.IdentityModel/Tokens/EdDsaSecurityKey.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public EdDsaSecurityKey(Ed25519PublicKeyParameters keyParameters) : this()
EdDsa = EdDsa.Create(new EdDsaParameters(ExtendedSecurityAlgorithms.Curves.Ed25519) {X = keyParameters.GetEncoded()});
}

public override int KeySize => throw new NotImplementedException();
public override int KeySize => EdDsa.KeySize;

[Obsolete("HasPrivateKey method is deprecated, please use PrivateKeyStatus.")]
public override bool HasPrivateKey => EdDsa.Parameters.D != null;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
using ScottBrady.IdentityModel.Crypto;
using ScottBrady.IdentityModel.Extensions;
using ScottBrady.IdentityModel.Tokens;
using Xunit;

namespace ScottBrady.IdentityModel.Tests.Tokens
{
public class ExtendedJsonWebKeyConverterTests
{
[Fact]
public void JsonWebKeyConverter_ConvertFromEdDsaSecurityKey()
{
var originKey = new EdDsaSecurityKey(EdDsa.Create(ExtendedSecurityAlgorithms.Curves.Ed25519));
var jwk = ExtendedJsonWebKeyConverter.ConvertFromEdDsaSecurityKey(originKey);
Assert.NotNull(jwk);
Assert.Equal(ExtendedSecurityAlgorithms.Curves.Ed25519, jwk.Crv);
Assert.Equal(ExtendedJsonWebAlgorithmsKeyTypes.ECDH, jwk.Kty);
Assert.Equal(ExtendedSecurityAlgorithms.EdDsa, jwk.Alg);
Assert.NotNull(jwk.D);
Assert.NotNull(jwk.X);
}
}
}