-
Notifications
You must be signed in to change notification settings - Fork 0
/
BouncyCastleTsaClient.cs
131 lines (117 loc) · 5.71 KB
/
BouncyCastleTsaClient.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
119
120
121
122
123
124
125
126
127
128
129
130
131
using DevExpress.Pdf;
using DevExpress.Office.Tsp;
using Org.BouncyCastle.Asn1.Cmp;
using Org.BouncyCastle.Crypto;
using Org.BouncyCastle.Crypto.Digests;
using Org.BouncyCastle.Math;
using Org.BouncyCastle.Security;
using Org.BouncyCastle.Tsp;
using System;
using System.IO;
using System.Net;
using System.Security.Cryptography;
using System.Diagnostics;
namespace CustomTsaClient
{
public class BouncyCastleTsaClient : ITsaClient
{
readonly Uri tsaServerURI;
readonly IDigest hashCalculator;
public BouncyCastleTsaClient(Uri tsaServerURI, IDigest hashCalculator)
{
this.tsaServerURI = tsaServerURI;
this.hashCalculator = hashCalculator;
}
byte[] CalculateDigest(Stream stream)
{
byte[] buffer = new byte[81920];
hashCalculator.Reset();
int bytesRead;
while ((bytesRead = stream.Read(buffer, 0, buffer.Length)) > 0)
hashCalculator.BlockUpdate(buffer, 0, bytesRead);
byte[] result = new byte[hashCalculator.GetDigestSize()];
hashCalculator.DoFinal(result, 0);
return result;
}
public byte[] GenerateTimeStamp(Stream stream)
{
//Generate a timestamp request:
TimeStampRequestGenerator tsqGenerator = new TimeStampRequestGenerator();
tsqGenerator.SetCertReq(true);
BigInteger nonce;
using (RandomNumberGenerator generator = RandomNumberGenerator.Create())
{
byte[] nonceValue = new byte[10];
generator.GetBytes(nonceValue);
nonce = new BigInteger(nonceValue);
}
string algorithmOid = DigestUtilities.GetObjectIdentifier(hashCalculator.AlgorithmName).Id;
TimeStampRequest request = tsqGenerator.Generate(algorithmOid, CalculateDigest(stream), nonce);
byte[] requestBytes = request.GetEncoded();
//Send the request to a server:
HttpWebRequest httpRequest = (HttpWebRequest)WebRequest.Create(tsaServerURI);
httpRequest.Method = "POST";
httpRequest.ContentType = "application/timestamp-query";
httpRequest.ContentLength = requestBytes.Length;
using (Stream requestStream = httpRequest.GetRequestStream())
requestStream.Write(requestBytes, 0, requestBytes.Length);
//Get a responce from the server:
HttpWebResponse httpResponse = (HttpWebResponse)httpRequest.GetResponse();
using (Stream respStream = new BufferedStream(httpResponse.GetResponseStream()))
{
//Read the response:
TimeStampResponse response = new TimeStampResponse(respStream);
response.Validate(request);
PkiFailureInfo failure = response.GetFailInfo();
//Throw an exception if the responce returned an error:
if (failure != null)
throw new Exception($"TimeStamp request to the \"{tsaServerURI}\" failed.");
TimeStampToken token = response.TimeStampToken;
//Throw an exception if the responce doesn't contain the timestamp:
if (token == null)
throw new Exception($"TimeStamp request to the \"{tsaServerURI}\" failed.");
return token.GetEncoded();
}
}
public byte[] GenerateTimeStamp(byte[] digest, string digestAlgorithmOID)
{
//Generate a timestamp request:
TimeStampRequestGenerator tsqGenerator = new TimeStampRequestGenerator();
tsqGenerator.SetCertReq(true);
BigInteger nonce;
using (RandomNumberGenerator generator = RandomNumberGenerator.Create())
{
byte[] nonceValue = new byte[10];
generator.GetBytes(nonceValue);
nonce = new BigInteger(nonceValue);
}
string algorithmOid = DigestUtilities.GetObjectIdentifier(hashCalculator.AlgorithmName).Id;
TimeStampRequest request = tsqGenerator.Generate(algorithmOid, digest, nonce);
byte[] requestBytes = request.GetEncoded();
//Send the request to a server:
HttpWebRequest httpRequest = (HttpWebRequest)WebRequest.Create(tsaServerURI);
httpRequest.Method = "POST";
httpRequest.ContentType = "application/timestamp-query";
httpRequest.ContentLength = requestBytes.Length;
using (Stream requestStream = httpRequest.GetRequestStream())
requestStream.Write(requestBytes, 0, requestBytes.Length);
//Get a responce from the server:
HttpWebResponse httpResponse = (HttpWebResponse)httpRequest.GetResponse();
using (Stream respStream = new BufferedStream(httpResponse.GetResponseStream()))
{
//Read the response:
TimeStampResponse response = new TimeStampResponse(respStream);
response.Validate(request);
PkiFailureInfo failure = response.GetFailInfo();
//Throw an exception if the responce returned an error:
if (failure != null)
throw new Exception($"TimeStamp request to the \"{tsaServerURI}\" failed.");
TimeStampToken token = response.TimeStampToken;
//Throw an exception if the responce doesn't contain the timestamp:
if (token == null)
throw new Exception($"TimeStamp request to the \"{tsaServerURI}\" failed.");
return token.GetEncoded();
}
}
}
}