|
8 | 8 | using System.Web;
|
9 | 9 | using NetLicensingClient.Entities;
|
10 | 10 | 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; |
11 | 16 |
|
12 | 17 | namespace NetLicensingClient.RestController
|
13 | 18 | {
|
@@ -132,6 +137,10 @@ public static netlicensing request(Context context, Method method, String path,
|
132 | 137 | using (StreamReader reader = new StreamReader(memoryStream))
|
133 | 138 | {
|
134 | 139 | var responseString = reader.ReadToEnd();
|
| 140 | + if (!VerifyXmlSignature(responseString, context.publicKey)) |
| 141 | + { |
| 142 | + throw new NetLicensingException("XML signature could not be verified"); |
| 143 | + } |
135 | 144 | }
|
136 | 145 | }
|
137 | 146 | memoryStream.Dispose();
|
@@ -198,6 +207,48 @@ private static netlicensing deserialize(Stream responseStream)
|
198 | 207 | return NetLicensingSerializer.Deserialize(responseStream) as netlicensing;
|
199 | 208 | }
|
200 | 209 |
|
| 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 | + } |
201 | 252 | }
|
202 | 253 |
|
203 | 254 | }
|
0 commit comments