-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProgram.cs
55 lines (45 loc) · 1.54 KB
/
Program.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
using System.Xml;
using System.Security.Cryptography.Xml;
using System.Security.Cryptography;
if (args.Length < 1)
{
Console.WriteLine("Usage: Verifier <xml-file> [<private-key-file>] [--attach]");
}
if (args.Length > 2 && args[2] == "--attach")
{
System.Diagnostics.Debugger.Launch();
}
// load document
var xmlDoc = new XmlDocument();
var xmlContent = File.ReadAllText(args[0]);
xmlDoc.LoadXml(xmlContent);
// get ds:Signature
var signedXml = new SignedXml(xmlDoc);
var signature = xmlDoc.GetElementsByTagName("Signature", SignedXml.XmlDsigNamespaceUrl).OfType<XmlElement>().Single();
signedXml.LoadXml(signature);
// verify signature using embedded keyinfo
var result = true;
// just for padding rsaKey
result = PrintInfo(signedXml.CheckSignature(/* */));
// verify signature using private key from pem
if (args.Length > 1)
{
var privateKey = File.ReadAllText(args[1]);
using var rsaKey = RSA.Create();
rsaKey.ImportFromPem(privateKey.ToCharArray());
result = PrintInfo(signedXml.CheckSignature(rsaKey)) && result;
}
if (!result)
{
System.Environment.Exit(1);
}
static bool PrintInfo(bool result, [System.Runtime.CompilerServices.CallerArgumentExpression(nameof(result))] string? caller = null)
{
Console.WriteLine(
"{0}: {1} (System.Security.Cryptography.Xml: {2}, .NET: {3})",
caller,
result ? "PASS" : "FAIL",
System.Diagnostics.FileVersionInfo.GetVersionInfo(typeof(SignedXml).Assembly.Location).ProductVersion,
Environment.Version);
return result;
}