mirrored from https://www.bouncycastle.org/repositories/bc-csharp
-
Notifications
You must be signed in to change notification settings - Fork 602
Expand file tree
/
Copy pathBasicOCSPRespGenerator.cs
More file actions
204 lines (174 loc) · 8.62 KB
/
Copy pathBasicOCSPRespGenerator.cs
File metadata and controls
204 lines (174 loc) · 8.62 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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
using System;
using System.Collections.Generic;
using Org.BouncyCastle.Asn1;
using Org.BouncyCastle.Asn1.Ocsp;
using Org.BouncyCastle.Asn1.X509;
using Org.BouncyCastle.Crypto;
using Org.BouncyCastle.Crypto.Operators;
using Org.BouncyCastle.Security;
using Org.BouncyCastle.Utilities;
using Org.BouncyCastle.X509;
namespace Org.BouncyCastle.Ocsp
{
/// <summary>Generator for basic OCSP response objects.</summary>
public class BasicOcspRespGenerator
{
private readonly List<ResponseObject> m_list = new List<ResponseObject>();
private X509Extensions m_responseExtensions;
private RespID m_responderID;
private class ResponseObject
{
internal CertificateID certId;
internal CertStatus certStatus;
internal DerGeneralizedTime thisUpdate;
internal DerGeneralizedTime nextUpdate;
internal X509Extensions extensions;
internal ResponseObject(
CertificateID certId,
CertificateStatus certStatus,
DateTime thisUpdate,
DateTime? nextUpdate,
X509Extensions extensions)
{
this.certId = certId;
if (certStatus == null)
{
this.certStatus = new CertStatus();
}
else if (certStatus is UnknownStatus)
{
this.certStatus = new CertStatus(2, DerNull.Instance);
}
else
{
RevokedStatus rs = (RevokedStatus)certStatus;
CrlReason revocationReason = rs.HasRevocationReason
? new CrlReason(rs.RevocationReason)
: null;
var revocationTime = Rfc5280Asn1Utilities.CreateGeneralizedTime(rs.RevocationTime);
var revokedInfo = new RevokedInfo(revocationTime, revocationReason);
this.certStatus = new CertStatus(revokedInfo);
}
this.thisUpdate = Rfc5280Asn1Utilities.CreateGeneralizedTime(thisUpdate);
this.nextUpdate = nextUpdate.HasValue ? Rfc5280Asn1Utilities.CreateGeneralizedTime(nextUpdate.Value) : null;
this.extensions = extensions;
}
public SingleResponse ToResponse()
{
return new SingleResponse(certId.ToAsn1Object(), certStatus, thisUpdate, nextUpdate, extensions);
}
}
/// <summary>Basic constructor.</summary>
public BasicOcspRespGenerator(RespID responderID)
{
m_responderID = responderID;
}
/// <summary>Construct with the responderID as the SHA-1 keyHash of the passed in public key.</summary>
public BasicOcspRespGenerator(AsymmetricKeyParameter publicKey)
{
m_responderID = new RespID(publicKey);
}
/// <summary>Add a response for a particular Certificate ID.</summary>
/// <param name="certID">Certificate ID details.</param>
/// <param name="certStatus">Status of the certificate - null if okay.</param>
public void AddResponse(CertificateID certID, CertificateStatus certStatus)
{
m_list.Add(new ResponseObject(certID, certStatus, DateTime.UtcNow, null, null));
}
/// <summary>Add a response for a particular Certificate ID.</summary>
/// <param name="certID">Certificate ID details.</param>
/// <param name="certStatus">Status of the certificate - null if okay.</param>
/// <param name="singleExtensions">Optional extensions.</param>
public void AddResponse(CertificateID certID, CertificateStatus certStatus, X509Extensions singleExtensions)
{
m_list.Add(new ResponseObject(certID, certStatus, DateTime.UtcNow, null, singleExtensions));
}
/// <summary>Add a response for a particular Certificate ID.</summary>
/// <param name="certID">Certificate ID details.</param>
/// <param name="nextUpdate">The date when next update should be requested.</param>
/// <param name="certStatus">Status of the certificate - null if okay.</param>
/// <param name="singleExtensions">Optional extensions.</param>
public void AddResponse(CertificateID certID, CertificateStatus certStatus, DateTime? nextUpdate,
X509Extensions singleExtensions)
{
m_list.Add(new ResponseObject(certID, certStatus, DateTime.UtcNow, nextUpdate, singleExtensions));
}
/// <summary>Add a response for a particular Certificate ID.</summary>
/// <param name="certID">Certificate ID details.</param>
/// <param name="thisUpdate">The date this response was valid on.</param>
/// <param name="nextUpdate">The date when next update should be requested.</param>
/// <param name="certStatus">Status of the certificate - null if okay.</param>
/// <param name="singleExtensions">Optional extensions.</param>
public void AddResponse(CertificateID certID, CertificateStatus certStatus, DateTime thisUpdate,
DateTime? nextUpdate, X509Extensions singleExtensions)
{
m_list.Add(new ResponseObject(certID, certStatus, thisUpdate, nextUpdate, singleExtensions));
}
/// <summary>Set the extensions for the response.</summary>
public void SetResponseExtensions(X509Extensions responseExtensions)
{
m_responseExtensions = responseExtensions;
}
private BasicOcspResp GenerateResponse(ISignatureFactory signatureFactory, X509Certificate[] chain,
DateTime producedAt)
{
AlgorithmIdentifier sigAlgID = (AlgorithmIdentifier)signatureFactory.AlgorithmDetails;
DerSequence responses;
try
{
responses = DerSequence.Map(m_list, ro => ro.ToResponse());
}
catch (Exception e)
{
throw new OcspException("exception creating Request", e);
}
var responseData = new ResponseData(m_responderID.ToAsn1Object(),
Rfc5280Asn1Utilities.CreateGeneralizedTime(producedAt), responses, m_responseExtensions);
DerBitString signature;
try
{
signature = X509.X509Utilities.GenerateSignature(signatureFactory, responseData);
}
catch (Exception e)
{
throw new OcspException("exception processing ResponseData", e);
}
DerSequence certs = null;
if (!Arrays.IsNullOrEmpty(chain))
{
certs = DerSequence.Map(chain, c => c.CertificateStructure);
}
return new BasicOcspResp(new BasicOcspResponse(responseData, sigAlgID, signature, certs));
}
// TODO[api] Rename 'thisUpdate' to 'producedAt'
public BasicOcspResp Generate(string signingAlgorithm, AsymmetricKeyParameter privateKey,
X509Certificate[] chain, DateTime thisUpdate)
{
return Generate(signingAlgorithm, privateKey, chain, thisUpdate, null);
}
public BasicOcspResp Generate(string signingAlgorithm, AsymmetricKeyParameter privateKey,
X509Certificate[] chain, DateTime producedAt, SecureRandom random)
{
if (signingAlgorithm == null)
throw new ArgumentNullException(nameof(signingAlgorithm));
return GenerateResponse(new Asn1SignatureFactory(signingAlgorithm, privateKey, random), chain, producedAt);
}
/// <summary>
/// Generate the signed response using the passed in signature calculator.
/// </summary>
/// <param name="signatureCalculatorFactory">Implementation of signing calculator factory.</param>
/// <param name="chain">The certificate chain associated with the response signer.</param>
/// <param name="producedAt">"produced at" date.</param>
/// <returns></returns>
// TODO[api] Rename 'signatureCalculatorFactory' to 'signatureFactory'
public BasicOcspResp Generate(ISignatureFactory signatureCalculatorFactory, X509Certificate[] chain,
DateTime producedAt)
{
if (signatureCalculatorFactory == null)
throw new ArgumentNullException(nameof(signatureCalculatorFactory));
return GenerateResponse(signatureCalculatorFactory, chain, producedAt);
}
/// <summary>Return an IEnumerable of the signature names supported by the generator.</summary>
public IEnumerable<string> SignatureAlgNames => Asn1SignatureFactory.SignatureAlgNames;
}
}