Skip to content

Commit

Permalink
Revert "Test .NET Standard with no dependencies"
Browse files Browse the repository at this point in the history
  • Loading branch information
r-brown committed Sep 7, 2020
1 parent e1cea2c commit 4905533
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 2 deletions.
6 changes: 4 additions & 2 deletions NetLicensingClient/NetLicensingClient.csproj
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework>
<TargetFramework>netstandard2.0</TargetFramework>
<ReleaseVersion>2.x</ReleaseVersion>
<PackOnBuild>true</PackOnBuild>
<PackageId>NetLicensingClient-csharp</PackageId>
<PackageVersion>0.0.4</PackageVersion>
<PackageVersion>2.4.4</PackageVersion>
<Authors>Labs64 NetLicensing</Authors>
<Copyright>© 2010 Labs64 GmbH</Copyright>
<PackageIconUrl>https://netlicensing.io/img/labs64-avatar-200x200.png</PackageIconUrl>
Expand All @@ -22,5 +22,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="System.Security.Cryptography.Xml" Version="4.7.0" />
<PackageReference Include="Portable.BouncyCastle" Version="1.8.6.7" />
</ItemGroup>
</Project>
51 changes: 51 additions & 0 deletions NetLicensingClient/RestController/NetLicensingAPI.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@
using System.Web;
using NetLicensingClient.Entities;
using NetLicensingClient.Exceptions;
using Org.BouncyCastle.Crypto.Parameters;
using Org.BouncyCastle.OpenSsl;
using System.Security.Cryptography;
using System.Security.Cryptography.Xml;
using System.Xml;

namespace NetLicensingClient.RestController
{
Expand Down Expand Up @@ -132,6 +137,10 @@ public static netlicensing request(Context context, Method method, String path,
using (StreamReader reader = new StreamReader(memoryStream))
{
var responseString = reader.ReadToEnd();
if (!VerifyXmlSignature(responseString, context.publicKey))
{
throw new NetLicensingException("XML signature could not be verified");
}
}
}
memoryStream.Dispose();
Expand Down Expand Up @@ -198,6 +207,48 @@ private static netlicensing deserialize(Stream responseStream)
return NetLicensingSerializer.Deserialize(responseStream) as netlicensing;
}

private static bool VerifyXmlSignature(string xmlString, string publicKey)
{
using (var keyReader = new StringReader(publicKey))
{
var pemReader = new PemReader(keyReader);

RsaKeyParameters parameters = (RsaKeyParameters)pemReader.ReadObject();
RSAParameters rParams = new RSAParameters();
rParams.Modulus = parameters.Modulus.ToByteArray();
rParams.Exponent = parameters.Exponent.ToByteArray();

RSA rsaKey = RSA.Create();
rsaKey.ImportParameters(rParams);

XmlDocument xmlDoc = new XmlDocument();
xmlDoc.PreserveWhitespace = true;
xmlDoc.LoadXml(xmlString);

// Create a new SignedXml object and pass it the XML document class
SignedXml signedXml = new SignedXml(xmlDoc);
// Find the "Signature" node and create a new XmlNodeList object
XmlNodeList nodeList = xmlDoc.GetElementsByTagName("Signature");

// Throw an exception if no signature was found
if (nodeList.Count <= 0)
{
throw new CryptographicException("Verification failed: No Signature was found in the document.");
}

// Throw an exception if more than one signature was found
if (nodeList.Count >= 2)
{
throw new CryptographicException("Verification failed: More that one signature was found for the document.");
}

// Load the first <signature> node
signedXml.LoadXml((XmlElement)nodeList[0]);

// Check the signature and return the result
return signedXml.CheckSignature(rsaKey);
}
}
}

}

0 comments on commit 4905533

Please sign in to comment.