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 pathCertificateStatus.cs
More file actions
198 lines (167 loc) · 7.21 KB
/
Copy pathCertificateStatus.cs
File metadata and controls
198 lines (167 loc) · 7.21 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
using System;
using System.Collections.Generic;
using System.IO;
using Org.BouncyCastle.Asn1;
using Org.BouncyCastle.Asn1.Ocsp;
namespace Org.BouncyCastle.Tls
{
public sealed class CertificateStatus
{
private readonly short m_statusType;
private readonly object m_response;
public CertificateStatus(short statusType, object response)
{
if (!IsCorrectType(statusType, response))
throw new ArgumentException("not an instance of the correct type", "response");
m_statusType = statusType;
m_response = response;
}
public short StatusType => m_statusType;
public object Response => m_response;
public OcspResponse OcspResponse
{
get
{
if (!IsCorrectType(CertificateStatusType.ocsp, m_response))
throw new InvalidOperationException("'response' is not an OCSPResponse");
return (OcspResponse)m_response;
}
}
/// <summary>an <see cref="IList{T}"/> of (possibly null) <see cref="Asn1.Ocsp.OcspResponse"/>.</summary>
public IList<OcspResponse> OcspResponseList
{
get
{
if (!IsCorrectType(CertificateStatusType.ocsp_multi, m_response))
throw new InvalidOperationException("'response' is not an OCSPResponseList");
return (IList<OcspResponse>)m_response;
}
}
/// <summary>Encode this <see cref="CertificateStatus"/> to a <see cref="Stream"/>.</summary>
/// <param name="output">the <see cref="Stream"/> to encode to.</param>
/// <exception cref="IOException"/>
public void Encode(Stream output)
{
TlsUtilities.WriteUint8(m_statusType, output);
switch (m_statusType)
{
case CertificateStatusType.ocsp:
{
OcspResponse ocspResponse = (OcspResponse)m_response;
byte[] derEncoding = ocspResponse.GetEncoded(Asn1Encodable.Der);
TlsUtilities.WriteOpaque24(derEncoding, output);
break;
}
case CertificateStatusType.ocsp_multi:
{
var ocspResponseList = (IList<OcspResponse>)m_response;
int count = ocspResponseList.Count;
var derEncodings = new List<byte[]>(count);
long totalLength = 0;
foreach (OcspResponse ocspResponse in ocspResponseList)
{
if (ocspResponse == null)
{
derEncodings.Add(TlsUtilities.EmptyBytes);
}
else
{
byte[] derEncoding = ocspResponse.GetEncoded(Asn1Encodable.Der);
derEncodings.Add(derEncoding);
totalLength += derEncoding.Length;
}
totalLength += 3;
}
TlsUtilities.CheckUint24(totalLength);
TlsUtilities.WriteUint24((int)totalLength, output);
foreach (byte[] derEncoding in derEncodings)
{
TlsUtilities.WriteOpaque24(derEncoding, output);
}
break;
}
default:
throw new TlsFatalAlert(AlertDescription.internal_error);
}
}
/// <summary>Parse a <see cref="CertificateStatus"/> from a <see cref="Stream"/>.</summary>
/// <param name="context">the <see cref="TlsContext"/> of the current connection.</param>
/// <param name="input">the <see cref="Stream"/> to parse from.</param>
/// <returns>a <see cref="CertificateStatus"/> object.</returns>
/// <exception cref="IOException"/>
public static CertificateStatus Parse(TlsContext context, Stream input)
{
SecurityParameters securityParameters = context.SecurityParameters;
Certificate peerCertificate = securityParameters.PeerCertificate;
if (null == peerCertificate || peerCertificate.IsEmpty
|| CertificateType.X509 != peerCertificate.CertificateType)
{
throw new TlsFatalAlert(AlertDescription.internal_error);
}
int certificateCount = peerCertificate.Length;
int statusRequestVersion = securityParameters.StatusRequestVersion;
short status_type = TlsUtilities.ReadUint8(input);
object response;
switch (status_type)
{
case CertificateStatusType.ocsp:
{
RequireStatusRequestVersion(1, statusRequestVersion);
byte[] derEncoding = TlsUtilities.ReadOpaque24(input, 1);
response = ParseOcspResponse(derEncoding);
break;
}
case CertificateStatusType.ocsp_multi:
{
RequireStatusRequestVersion(2, statusRequestVersion);
byte[] ocsp_response_list = TlsUtilities.ReadOpaque24(input, 1);
MemoryStream buf = new MemoryStream(ocsp_response_list, false);
var ocspResponseList = new List<OcspResponse>();
while (buf.Position < buf.Length)
{
if (ocspResponseList.Count >= certificateCount)
throw new TlsFatalAlert(AlertDescription.illegal_parameter);
int length = TlsUtilities.ReadUint24(buf);
if (length < 1)
{
ocspResponseList.Add(null);
}
else
{
byte[] derEncoding = TlsUtilities.ReadFully(length, buf);
ocspResponseList.Add(ParseOcspResponse(derEncoding));
}
}
response = ocspResponseList;
break;
}
default:
throw new TlsFatalAlert(AlertDescription.decode_error);
}
return new CertificateStatus(status_type, response);
}
private static bool IsCorrectType(short statusType, object response)
{
switch (statusType)
{
case CertificateStatusType.ocsp:
return response is OcspResponse;
case CertificateStatusType.ocsp_multi:
return IsOcspResponseList(response);
default:
throw new ArgumentException("unsupported CertificateStatusType", "statusType");
}
}
private static bool IsOcspResponseList(object response) =>
response is IList<OcspResponse> v && v.Count > 0;
/// <exception cref="IOException"/>
private static OcspResponse ParseOcspResponse(byte[] derEncoding) =>
TlsUtilities.ReadDerEncoding(derEncoding, OcspResponse.GetOptional);
/// <exception cref="IOException"/>
private static void RequireStatusRequestVersion(int minVersion, int statusRequestVersion)
{
if (statusRequestVersion < minVersion)
throw new TlsFatalAlert(AlertDescription.decode_error);
}
}
}