/
X509Chain.cs
192 lines (163 loc) · 6.2 KB
/
X509Chain.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
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
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System.Diagnostics;
using System.Runtime.Versioning;
using SafeX509ChainHandle = Microsoft.Win32.SafeHandles.SafeX509ChainHandle;
namespace System.Security.Cryptography.X509Certificates
{
public class X509Chain : IDisposable
{
private X509ChainPolicy? _chainPolicy;
private volatile X509ChainStatus[]? _lazyChainStatus;
private X509ChainElementCollection? _chainElements;
private IChainPal? _pal;
private bool _useMachineContext;
private readonly object _syncRoot = new object();
public X509Chain() { }
public X509Chain(bool useMachineContext)
{
_useMachineContext = useMachineContext;
}
[SupportedOSPlatform("windows")]
public X509Chain(IntPtr chainContext)
{
_pal = ChainPal.FromHandle(chainContext);
Debug.Assert(_pal != null);
_chainElements = new X509ChainElementCollection(_pal.ChainElements!);
}
public static X509Chain Create()
{
return new X509Chain();
}
public X509ChainElementCollection ChainElements => _chainElements ??= new X509ChainElementCollection();
public X509ChainPolicy ChainPolicy
{
get => _chainPolicy ??= new X509ChainPolicy();
set
{
ArgumentNullException.ThrowIfNull(value);
_chainPolicy = value;
}
}
public X509ChainStatus[] ChainStatus
{
get
{
// We give the user a reference to the array since we'll never access it.
return _lazyChainStatus ??= (_pal == null ? Array.Empty<X509ChainStatus>() : _pal.ChainStatus!);
}
}
public IntPtr ChainContext
{
get
{
SafeX509ChainHandle? handle = SafeHandle;
if (handle == null)
{
// This case will only exist for Unix
return IntPtr.Zero;
}
// For .NET Framework compat, we may return an invalid handle here (IntPtr.Zero)
return handle.DangerousGetHandle();
}
}
public SafeX509ChainHandle? SafeHandle
{
get
{
if (_pal == null)
return SafeX509ChainHandle.InvalidHandle;
return _pal.SafeHandle;
}
}
[UnsupportedOSPlatform("browser")]
public bool Build(X509Certificate2 certificate)
{
return Build(certificate, true);
}
internal bool Build(X509Certificate2 certificate, bool throwOnException)
{
lock (_syncRoot)
{
if (certificate == null || certificate.Pal == null)
throw new ArgumentException(SR.Cryptography_InvalidContextHandle, nameof(certificate));
if (_chainPolicy != null)
{
if (_chainPolicy._customTrustStore != null)
{
if (_chainPolicy.TrustMode == X509ChainTrustMode.System && _chainPolicy.CustomTrustStore.Count > 0)
throw new CryptographicException(SR.Cryptography_CustomTrustCertsInSystemMode);
foreach (X509Certificate2 customCertificate in _chainPolicy.CustomTrustStore)
{
if (customCertificate == null || customCertificate.Handle == IntPtr.Zero)
{
throw new CryptographicException(SR.Cryptography_InvalidTrustCertificate);
}
}
}
if (_chainPolicy.TrustMode == X509ChainTrustMode.CustomRootTrust && _chainPolicy._customTrustStore == null)
{
_chainPolicy._customTrustStore = new X509Certificate2Collection();
}
}
Reset();
X509ChainPolicy chainPolicy = ChainPolicy;
_pal = ChainPal.BuildChain(
_useMachineContext,
certificate.Pal,
chainPolicy._extraStore,
chainPolicy._applicationPolicy!,
chainPolicy._certificatePolicy!,
chainPolicy.RevocationMode,
chainPolicy.RevocationFlag,
chainPolicy._customTrustStore,
chainPolicy.TrustMode,
chainPolicy.VerificationTimeIgnored ? DateTime.Now : chainPolicy.VerificationTime,
chainPolicy.UrlRetrievalTimeout,
chainPolicy.DisableCertificateDownloads);
if (_pal == null)
return false;
_chainElements = new X509ChainElementCollection(_pal.ChainElements!);
Exception? verificationException;
bool? verified = _pal.Verify(chainPolicy.VerificationFlags, out verificationException);
if (!verified.HasValue)
{
if (throwOnException)
{
throw verificationException!;
}
else
{
verified = false;
}
}
return verified.Value;
}
}
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
protected virtual void Dispose(bool disposing)
{
if (disposing)
{
Reset();
}
}
public void Reset()
{
// _chainPolicy is not reset for .NET Framework compat
_lazyChainStatus = null;
_chainElements = null;
_useMachineContext = false;
IChainPal? pal = _pal;
if (pal != null)
{
_pal = null;
pal.Dispose();
}
}
}
}