-
Notifications
You must be signed in to change notification settings - Fork 0
/
Program.cs
118 lines (100 loc) · 4.93 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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
using DevExpress.Office.DigitalSignatures;
using DevExpress.Office.Tsp;
using System;
using System.Diagnostics;
using System.Net;
using System.Security.Cryptography.X509Certificates;
namespace WorkbookDigitalSignatureSample
{
class Program
{
static string input = "Template.xlsx";
static string output = "Template_signed.xlsx";
static void Main(string[] args)
{
//Enable all security protocols:
//ServicePointManager.SecurityProtocol |= SecurityProtocolType.Tls12
// | SecurityProtocolType.Ssl3 | SecurityProtocolType.Tls | SecurityProtocolType.Tls11;
SignDocument(input);
ValidateSignature(output);
}
static void SignDocument(string path)
{
DocumentSigner documentSigner = new DocumentSigner();
//Sign a document and save the result:
documentSigner.Sign(path, output, CreateSignatureOptions(), CreateSignatureInfo());
}
//Specify signature options:
static SignatureOptions CreateSignatureOptions()
{
X509Certificate2 certificate = new X509Certificate2("Certificate/SignDemo.pfx", "dxdemo");
Uri tsaServer = new Uri("https://freetsa.org/tsr");
SignatureOptions options = new SignatureOptions();
options.Certificate = certificate;
if (tsaServer != null)
options.TsaClient = new TsaClient(tsaServer, HashAlgorithmType.SHA256);
//In this example, certificate validation is skipped
options.SignatureFlags &= ~SignatureFlags.ValidateCertificate;
options.CertificateKeyUsageFlags = X509KeyUsageFlags.None;
options.DigestMethod = HashAlgorithmType.SHA256;
X509ChainPolicy policy = new X509ChainPolicy();
policy.RevocationMode = X509RevocationMode.NoCheck;
policy.RevocationFlag = X509RevocationFlag.ExcludeRoot;
policy.VerificationFlags |= X509VerificationFlags.AllowUnknownCertificateAuthority |
X509VerificationFlags.IgnoreCertificateAuthorityRevocationUnknown;
options.CertificatePolicy = policy;
options.TimestampCertificatePolicy = policy;
return options;
}
static SignatureInfo CreateSignatureInfo()
{
SignatureInfo signatureInfo = new SignatureInfo();
signatureInfo.CommitmentType = CommitmentType.ProofOfApproval;
signatureInfo.Time = DateTime.UtcNow;
signatureInfo.ClaimedRoles.Clear();
signatureInfo.ClaimedRoles.Add("Sales Representative");
signatureInfo.Country = "USA";
signatureInfo.City = "Seattle";
signatureInfo.StateOrProvince = "WA";
signatureInfo.Address1 = "507 - 20th Ave. E.";
signatureInfo.Address2 = "Apt. 2A";
signatureInfo.PostalCode = "98122";
signatureInfo.Comments = "Demo Digital Signature";
return signatureInfo;
}
private static void ValidateSignature(string path)
{
DocumentSigner validator = new DocumentSigner();
SignatureValidationOptions validationOptions = new SignatureValidationOptions();
//In this example, signature and timestamp certificate validation is skipped
validationOptions.ValidationFlags = ~ValidationFlags.ValidateSignatureCertificate & ~ValidationFlags.ValidateTimestampCertificate;
//Validate the signature:
PackageSignatureValidation signatureValidation = validator.Validate(path, validationOptions);
string validationMessage = signatureValidation.ResultMessage;
//Check validation result and show information in the console:
switch (signatureValidation.Result)
{
case PackageSignatureValidationResult.Valid:
Console.WriteLine(validationMessage); Console.ReadKey();
Process.Start(path);
break;
case PackageSignatureValidationResult.SignaturesNotFound:
Console.WriteLine(validationMessage);
break;
case PackageSignatureValidationResult.Invalid:
case PackageSignatureValidationResult.PartiallyValid:
var failedCheckDetails = signatureValidation.Items[0].FailedCheckDetails;
Console.WriteLine(validationMessage);
int i = 1;
foreach (SignatureCheckResult checkResult in failedCheckDetails)
{
Console.WriteLine(String.Format("Validation details {0}: \r\n" +
"{1} failed, Info: {2} \r\n", i, checkResult.CheckType, checkResult.Info));
i++;
}
Console.ReadKey();
break;
}
}
}
}