runtime: System.Security.Cryptography. ECDsaOpenSsl throw NullReferenceException after using construction.

Issue Title

ECDsaOpenSsl from System.Security.Cryptography throw NullReferenceException after using construction.

General

I have .NET Core 2.2 and Linux container in docker. When I create JWT token with ES256 algorithm JwtSecurityTokenHandler.CreateEncodedJwt() throw exception.

token

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>netcoreapp2.2</TargetFramework>
    <DockerDefaultTargetOS>Linux</DockerDefaultTargetOS>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="Microsoft.VisualStudio.Azure.Containers.Tools.Targets" Version="1.9.5" />
    <PackageReference Include="Portable.BouncyCastle" Version="1.8.5.2" />
    <PackageReference Include="System.IdentityModel.Tokens.Jwt" Version="5.6.0" />
  </ItemGroup>

</Project>
FROM mcr.microsoft.com/dotnet/core/runtime:2.2-stretch-slim AS base
WORKDIR /app

FROM mcr.microsoft.com/dotnet/core/sdk:2.2-stretch AS build
WORKDIR /src
COPY ["AppleMusic/AppleMusic.csproj", "AppleMusic/"]
RUN dotnet restore "AppleMusic/AppleMusic.csproj"
COPY . .
WORKDIR "/src/AppleMusic"
RUN dotnet build "AppleMusic.csproj" -c Release -o /app/build

FROM build AS publish
RUN dotnet publish "AppleMusic.csproj" -c Release -o /app/publish

FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "AppleMusic.dll"]
using System;
using System.IdentityModel.Tokens.Jwt;
using System.Security.Claims;
using System.Security.Cryptography;
using Microsoft.IdentityModel.Tokens;
using Org.BouncyCastle.Crypto.Parameters;
using Org.BouncyCastle.Security;

namespace AppleMusic
{
	class Program
	{
		static void Main(string[] args)
		{
			var key = "";
			
			var iat = (int)DateTime.UtcNow.Subtract(new DateTime(1970, 1, 1)).TotalSeconds;
			var exp = (int)DateTime.UtcNow.AddMonths(5).Subtract(new DateTime(1970, 1, 1)).TotalSeconds;

			var tokenHandler = new JwtSecurityTokenHandler();

			var tokenDescriptor = new SecurityTokenDescriptor
			{
				Subject = new ClaimsIdentity(new Claim[]
				{
					new Claim("iat", iat.ToString()),
					new Claim("exp", exp.ToString()),
				}),
				Issuer = ""
			};

			for (var i = 0; i < 100; i++)
			{
				using (var algorithm = GetEllipticCurveAlgorithm(key)) // this code doesn't work
				{
					tokenDescriptor.SigningCredentials = new SigningCredentials(new ECDsaSecurityKey(algorithm)
					{
						KeyId = ""
					}, SecurityAlgorithms.EcdsaSha256);
					var token = tokenHandler.CreateEncodedJwt(tokenDescriptor);
					Console.WriteLine(token);
				}
			}
		}

		private static ECDsa GetEllipticCurveAlgorithm(string privateKey)
		{
			var keyParams = (ECPrivateKeyParameters)PrivateKeyFactory
				.CreateKey(Convert.FromBase64String(privateKey));

			var q = keyParams.Parameters.G.Multiply(keyParams.D).Normalize();

			return ECDsa.Create(new ECParameters
			{
				Curve = ECCurve.CreateFromValue(keyParams.PublicKeyParamSet.Id),
				D = keyParams.D.ToByteArrayUnsigned(),
				Q =
				{
					X = q.XCoord.GetEncoded(),
					Y = q.YCoord.GetEncoded()
				}
			});
		}
	}
}

version 2:

using System.IdentityModel.Tokens.Jwt;
using System.Security.Cryptography;
using Microsoft.IdentityModel.Tokens;

namespace AppleMusic
{
	class Program
	{
		static void Main(string[] args)
		{
			var tokenHandler = new JwtSecurityTokenHandler();

			var tokenDescriptor = new SecurityTokenDescriptor
			{
				Issuer = "A"
			};

			for (var i = 0; i < 100; i++)
			{
				using (var algorithm = ECDsa.Create()) // this code doesn't work correct
				{
					tokenDescriptor.SigningCredentials = new SigningCredentials(new ECDsaSecurityKey(algorithm)
					{
						KeyId = "A"
					}, SecurityAlgorithms.EcdsaSha256);
					var token = tokenHandler.CreateEncodedJwt(tokenDescriptor);
				}
			}
		}
	}
}

About this issue

  • Original URL
  • State: closed
  • Created 5 years ago
  • Comments: 37 (15 by maintainers)

Most upvoted comments