Skip to content

Commit

Permalink
Merge pull request #13 from thomas-tran/feature/add-jsonwebkey-converter
Browse files Browse the repository at this point in the history
Feature: Add extension method to convert EdDsaSecurityKey to JsonWebKey
  • Loading branch information
scottbrady91 committed Mar 30, 2024
2 parents 7c36b09 + 753c2d3 commit af06726
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 6 deletions.
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";
}
}
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
{
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
6 changes: 3 additions & 3 deletions src/ScottBrady.IdentityModel/Tokens/EdDsaSecurityKey.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

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);
}
}
}

0 comments on commit af06726

Please sign in to comment.