Skip to content

Commit

Permalink
Checking for public and private key not to be null in RS256Algorithm (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
abatishchev authored Jun 24, 2019
1 parent 050b24b commit 16ef475
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 10 deletions.
28 changes: 19 additions & 9 deletions src/JWT/Algorithms/RS256Algorithm.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Security.Cryptography;
using System;
using System.Security.Cryptography;
using System.Security.Cryptography.X509Certificates;

namespace JWT.Algorithms
Expand All @@ -18,8 +19,17 @@ public sealed class RS256Algorithm : IJwtAlgorithm
/// <param name="privateKey">The RSA key for signing the data.</param>
public RS256Algorithm(RSACryptoServiceProvider publicKey, RSA privateKey)
{
_publicKey = publicKey;
_privateKey = privateKey;
_publicKey = publicKey ?? throw new InvalidOperationException("Private key is null");
_privateKey = privateKey ?? throw new InvalidOperationException("Public key is null");
}

/// <summary>
/// Creates an instance using the provided pair of public and private keys.
/// </summary>
/// <param name="publicKey">The RSA service provider for verifying the data.</param>
public RS256Algorithm(RSACryptoServiceProvider publicKey)
{
_publicKey = publicKey ?? throw new InvalidOperationException("Private key is null");
}

/// <summary>
Expand Down Expand Up @@ -52,14 +62,14 @@ public byte[] Sign(byte[] bytesToSign) =>
/// <summary>
/// Verifies provided byte array with provided signature.
/// </summary>
/// <remarks>
/// 2.16.840.1.101.3.4.2.1 is the object id for the sha256NoSign algorithm.
/// See https://docs.microsoft.com/en-us/openspecs/windows_protocols/ms-gpnap/a48b02b2-2a10-4eb0-bed4-1807a6d2f5ad for further details.
/// </remarks>
/// <param name="bytesToSign">The data to verify</param>
/// <param name="signature">The signature to verify with</param>
public bool Verify(byte[] bytesToSign, byte[] signature)
{
// 2.16.840.1.101.3.4.2.1 is the object id for the sha256NoSign algorithm.
// See https://docs.microsoft.com/en-us/openspecs/windows_protocols/ms-gpnap/a48b02b2-2a10-4eb0-bed4-1807a6d2f5ad for further details.
return _publicKey.VerifyData(bytesToSign, "2.16.840.1.101.3.4.2.1", signature);
}
public bool Verify(byte[] bytesToSign, byte[] signature) =>
_publicKey.VerifyData(bytesToSign, "2.16.840.1.101.3.4.2.1", signature);

private static RSA GetPrivateKey(X509Certificate2 cert)
{
Expand Down
2 changes: 1 addition & 1 deletion src/JWT/JWT.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
<PackageProjectUrl>https://github.com/jwt-dotnet/jwt</PackageProjectUrl>
<Authors>Alexander Batishchev, John Sheehan, Michael Lehenbauer</Authors>
<PackageLicenseUrl>https://creativecommons.org/publicdomain/zero/1.0/</PackageLicenseUrl>
<Version>5.2.0</Version>
<Version>5.2.1</Version>
<PackageTags>jwt json</PackageTags>
<FileVersion>5.0.0.0</FileVersion>
<AssemblyVersion>5.0.0.0</AssemblyVersion>
Expand Down

0 comments on commit 16ef475

Please sign in to comment.