diff --git a/src/ScottBrady.IdentityModel/Crypto/ExtendedJsonWebAlgorithmsKeyTypes.cs b/src/ScottBrady.IdentityModel/Crypto/ExtendedJsonWebAlgorithmsKeyTypes.cs new file mode 100644 index 0000000..83fd3b0 --- /dev/null +++ b/src/ScottBrady.IdentityModel/Crypto/ExtendedJsonWebAlgorithmsKeyTypes.cs @@ -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"; + } +} diff --git a/src/ScottBrady.IdentityModel/Extensions/ExtendedJsonWebKeyConverter.cs b/src/ScottBrady.IdentityModel/Extensions/ExtendedJsonWebKeyConverter.cs new file mode 100644 index 0000000..57d6305 --- /dev/null +++ b/src/ScottBrady.IdentityModel/Extensions/ExtendedJsonWebKeyConverter.cs @@ -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, + }; + } + } +} diff --git a/src/ScottBrady.IdentityModel/Tokens/EdDsa.cs b/src/ScottBrady.IdentityModel/Tokens/EdDsa.cs index 3e554af..4b01b31 100644 --- a/src/ScottBrady.IdentityModel/Tokens/EdDsa.cs +++ b/src/ScottBrady.IdentityModel/Tokens/EdDsa.cs @@ -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; @@ -9,7 +10,7 @@ namespace ScottBrady.IdentityModel.Tokens; -public class EdDsa +public class EdDsa: AsymmetricAlgorithm { internal EdDsaParameters Parameters { get; private init; } @@ -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)}; } @@ -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)); diff --git a/src/ScottBrady.IdentityModel/Tokens/EdDsaSecurityKey.cs b/src/ScottBrady.IdentityModel/Tokens/EdDsaSecurityKey.cs index 80b6299..b8cc3ab 100644 --- a/src/ScottBrady.IdentityModel/Tokens/EdDsaSecurityKey.cs +++ b/src/ScottBrady.IdentityModel/Tokens/EdDsaSecurityKey.cs @@ -35,9 +35,9 @@ public EdDsaSecurityKey(Ed25519PublicKeyParameters keyParameters) : this() if (keyParameters == null) throw new ArgumentNullException(nameof(keyParameters)); 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; diff --git a/test/ScottBrady.IdentityModel.Tests/Tokens/ExtendedJsonWebKeyConverterTests.cs b/test/ScottBrady.IdentityModel.Tests/Tokens/ExtendedJsonWebKeyConverterTests.cs new file mode 100644 index 0000000..80cc635 --- /dev/null +++ b/test/ScottBrady.IdentityModel.Tests/Tokens/ExtendedJsonWebKeyConverterTests.cs @@ -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); + } + } +}