Skip to content

Commit 4905533

Browse files
committed
Revert "Test .NET Standard with no dependencies"
1 parent e1cea2c commit 4905533

File tree

2 files changed

+55
-2
lines changed

2 files changed

+55
-2
lines changed

NetLicensingClient/NetLicensingClient.csproj

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
4-
<TargetFramework>netcoreapp3.1</TargetFramework>
4+
<TargetFramework>netstandard2.0</TargetFramework>
55
<ReleaseVersion>2.x</ReleaseVersion>
66
<PackOnBuild>true</PackOnBuild>
77
<PackageId>NetLicensingClient-csharp</PackageId>
8-
<PackageVersion>0.0.4</PackageVersion>
8+
<PackageVersion>2.4.4</PackageVersion>
99
<Authors>Labs64 NetLicensing</Authors>
1010
<Copyright>© 2010 Labs64 GmbH</Copyright>
1111
<PackageIconUrl>https://netlicensing.io/img/labs64-avatar-200x200.png</PackageIconUrl>
@@ -22,5 +22,7 @@
2222
</PropertyGroup>
2323

2424
<ItemGroup>
25+
<PackageReference Include="System.Security.Cryptography.Xml" Version="4.7.0" />
26+
<PackageReference Include="Portable.BouncyCastle" Version="1.8.6.7" />
2527
</ItemGroup>
2628
</Project>

NetLicensingClient/RestController/NetLicensingAPI.cs

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,11 @@
88
using System.Web;
99
using NetLicensingClient.Entities;
1010
using NetLicensingClient.Exceptions;
11+
using Org.BouncyCastle.Crypto.Parameters;
12+
using Org.BouncyCastle.OpenSsl;
13+
using System.Security.Cryptography;
14+
using System.Security.Cryptography.Xml;
15+
using System.Xml;
1116

1217
namespace NetLicensingClient.RestController
1318
{
@@ -132,6 +137,10 @@ public static netlicensing request(Context context, Method method, String path,
132137
using (StreamReader reader = new StreamReader(memoryStream))
133138
{
134139
var responseString = reader.ReadToEnd();
140+
if (!VerifyXmlSignature(responseString, context.publicKey))
141+
{
142+
throw new NetLicensingException("XML signature could not be verified");
143+
}
135144
}
136145
}
137146
memoryStream.Dispose();
@@ -198,6 +207,48 @@ private static netlicensing deserialize(Stream responseStream)
198207
return NetLicensingSerializer.Deserialize(responseStream) as netlicensing;
199208
}
200209

210+
private static bool VerifyXmlSignature(string xmlString, string publicKey)
211+
{
212+
using (var keyReader = new StringReader(publicKey))
213+
{
214+
var pemReader = new PemReader(keyReader);
215+
216+
RsaKeyParameters parameters = (RsaKeyParameters)pemReader.ReadObject();
217+
RSAParameters rParams = new RSAParameters();
218+
rParams.Modulus = parameters.Modulus.ToByteArray();
219+
rParams.Exponent = parameters.Exponent.ToByteArray();
220+
221+
RSA rsaKey = RSA.Create();
222+
rsaKey.ImportParameters(rParams);
223+
224+
XmlDocument xmlDoc = new XmlDocument();
225+
xmlDoc.PreserveWhitespace = true;
226+
xmlDoc.LoadXml(xmlString);
227+
228+
// Create a new SignedXml object and pass it the XML document class
229+
SignedXml signedXml = new SignedXml(xmlDoc);
230+
// Find the "Signature" node and create a new XmlNodeList object
231+
XmlNodeList nodeList = xmlDoc.GetElementsByTagName("Signature");
232+
233+
// Throw an exception if no signature was found
234+
if (nodeList.Count <= 0)
235+
{
236+
throw new CryptographicException("Verification failed: No Signature was found in the document.");
237+
}
238+
239+
// Throw an exception if more than one signature was found
240+
if (nodeList.Count >= 2)
241+
{
242+
throw new CryptographicException("Verification failed: More that one signature was found for the document.");
243+
}
244+
245+
// Load the first <signature> node
246+
signedXml.LoadXml((XmlElement)nodeList[0]);
247+
248+
// Check the signature and return the result
249+
return signedXml.CheckSignature(rsaKey);
250+
}
251+
}
201252
}
202253

203254
}

0 commit comments

Comments
 (0)