-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBouncyCastleTsaClient.cs
More file actions
125 lines (111 loc) · 5.25 KB
/
BouncyCastleTsaClient.cs
File metadata and controls
125 lines (111 loc) · 5.25 KB
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
using DevExpress.Office.Tsp;
using Org.BouncyCastle.Asn1.Cmp;
using Org.BouncyCastle.Crypto;
using Org.BouncyCastle.Math;
using Org.BouncyCastle.Security;
using Org.BouncyCastle.Tsp;
using System;
using System.IO;
using System.Net.Http;
using System.Security.Cryptography;
public class BouncyCastleTsaClient : ITsaClient
{
readonly Uri tsaServerURI;
readonly IDigest hashCalculator;
readonly HttpClient httpClient;
public BouncyCastleTsaClient(Uri tsaServerURI, IDigest hashCalculator, HttpClient httpClient)
{
this.tsaServerURI = tsaServerURI;
this.hashCalculator = hashCalculator;
this.httpClient = httpClient;
}
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:
HttpClient httpClient = new HttpClient();
using var content = new ByteArrayContent(requestBytes);
content.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/timestamp-query");
//Get a responce from the server:
using HttpResponseMessage responseMessage = httpClient.PostAsync(tsaServerURI, content).Result;
if (!responseMessage.IsSuccessStatusCode)
{
throw new Exception($"TimeStamp request to the \"{tsaServerURI}\" failed with status code {responseMessage.StatusCode}.");
}
byte[] responseBytes = responseMessage.Content.ReadAsByteArrayAsync().Result;
//Read the response:
TimeStampResponse response = new TimeStampResponse(responseBytes);
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:
HttpClient httpClient = new HttpClient();
using var content = new ByteArrayContent(requestBytes);
content.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/timestamp-query");
//Get a responce from the server:
using HttpResponseMessage responseMessage = httpClient.PostAsync(tsaServerURI, content).Result;
if (!responseMessage.IsSuccessStatusCode)
{
throw new Exception($"TimeStamp request to the \"{tsaServerURI}\" failed with status code {responseMessage.StatusCode}.");
}
byte[] responseBytes = responseMessage.Content.ReadAsByteArrayAsync().Result;
//Read the response:
TimeStampResponse response = new TimeStampResponse(responseBytes);
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();
}
}