-
Notifications
You must be signed in to change notification settings - Fork 18
/
CertInspector.cs
335 lines (309 loc) · 14.9 KB
/
CertInspector.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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
using System;
using System.Collections.ObjectModel;
using System.Linq;
using System.Security.Cryptography;
using System.Windows.Forms;
using Fiddler;
using System.Security.Cryptography.X509Certificates;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Forms.Integration;
using System.Windows.Media;
using System.Diagnostics;
using System.Windows.Interop;
namespace VCSJones.FiddlerCert
{
public class CertInspector : Inspector2, IResponseInspector2
{
private readonly Grid _panel;
private readonly X509Store _rootStore = new X509Store(StoreName.Root, StoreLocation.CurrentUser);
private readonly X509Store _userStore = new X509Store(StoreName.Root, StoreLocation.LocalMachine);
private readonly ElementHost _host;
private readonly AskUpdateBarModel _askUpdateBarModel;
public CertInspector()
{
_askUpdateBarModel = new AskUpdateBarModel();
_rootStore.Open(OpenFlags.ReadOnly);
_userStore.Open(OpenFlags.ReadOnly);
_host = new ElementHost { Dock = DockStyle.Fill };
_panel = new Grid();
_host.Child = _panel;
FiddlerApplication.Prefs.AddWatcher("fiddler.ui.font", FontChanged);
FiddlerApplication.Prefs.AddWatcher(PreferenceNames.ASK_CHECK_FOR_UPDATES_PREF, AskUpdateChanged);
}
private void AskUpdateChanged(object sender, PrefChangeEventArgs e)
{
if (e.PrefName == PreferenceNames.ASK_CHECK_FOR_UPDATES_PREF)
{
_askUpdateBarModel.AskRequired = !e.ValueBool;
}
}
private void FontChanged(object sender, PrefChangeEventArgs e)
{
if (e.PrefName == "fiddler.ui.font.face")
{
var font = e.ValueString;
var fontFamily = new FontFamily(font);
_panel.Dispatcher.BeginInvoke((Action)(() =>
{
var style = new Style(_panel.GetType(), _panel.Style);
style.Setters.Add(new Setter(TextBlock.FontFamilyProperty, fontFamily));
style.Seal();
_panel.Style = style;
}));
}
if (e.PrefName == "fiddler.ui.font.size")
{
double value;
if (double.TryParse(e.ValueString, out value))
{
_panel.Dispatcher.BeginInvoke((Action)(() =>
{
var fontSizeInPoints = value * 96d / 72d;
var style = new Style(_panel.GetType(), _panel.Style);
style.Setters.Add(new Setter(TextBlock.FontSizeProperty, fontSizeInPoints));
style.Seal();
_panel.Style = style;
}));
}
}
}
public override void AddToTab(TabPage o)
{
o.Text = "Certificates";
o.Controls.Add(_host);
}
public override int GetOrder() => 10_000;
public void Clear() => _panel.Children.Clear();
public HTTPResponseHeaders headers
{
// We don't allow editing, and look only at Session flags
get => null;
set { }
}
public byte[] body
{
// We don't allow editing, and look only at Session flags
get => null;
set { }
}
// We are never dirty; we don't allow editing
public bool bDirty => false;
public bool bReadOnly
{
// We don't allow editing
get => true;
set { }
}
public override void AssignSession(Session oS)
{
Clear();
if (!CertificateInspector.HttpsDecryptionEnabledOnStartup || !CONFIG.bCaptureCONNECT || !CONFIG.bMITM_HTTPS)
{
_panel.Children.Add(new System.Windows.Controls.Label {
Content = "Fiddler Cert Inspector requires enabling decryption of HTTPS traffic, and restarting Fiddler."
});
return;
}
if (!CertificateInspector.IsSupportedOperatingSystem)
{
_panel.Children.Add(new System.Windows.Controls.Label
{
Content = "Fiddler Cert Inspector requires Windows Vista or Windows Server 2008 or later."
});
return;
}
var control = new WpfCertificateControl();
var masterModel = new CertInspectorModel();
masterModel.UpdateBarModel = new UpdateBarModel(CertificateInspector.LatestVersion?.Item1, CertificateInspector.LatestVersion?.Item2);
masterModel.AskUpdateBarModel = _askUpdateBarModel;
masterModel.AskUpdateBarModel.AskRequired = !FiddlerApplication.Prefs.GetBoolPref(PreferenceNames.ASK_CHECK_FOR_UPDATES_PREF, false);
masterModel.SettingsCommand = new RelayCommand(_ =>
{
var window = new SettingsWindow();
var helper = new WindowInteropHelper(window);
helper.Owner = FiddlerApplication.UI.Handle;
window.ShowDialog();
});
masterModel.HttpSecurityModel = new HttpSecurityModel
{
IsNotTunnel = (oS.BitFlags & SessionFlags.IsDecryptingTunnel) != SessionFlags.IsDecryptingTunnel,
ContentChain = new AsyncProperty<ObservableCollection<CertificateModel>>(Task.Factory.StartNew(() =>
{
if (!oS.bHasResponse)
{
return null;
}
var contentChain = ChainForContent(oS.ResponseBody);
if (contentChain == null)
{
return null;
}
var chainItems = contentChain.ChainElements.Cast<X509ChainElement>().Select((t, i) => AssignCertificate(t, false, null, contentChain, i)).ToList();
return new ObservableCollection<CertificateModel>(chainItems);
}))
};
if (oS.isHTTPS || (oS.BitFlags & SessionFlags.IsDecryptingTunnel) == SessionFlags.IsDecryptingTunnel)
{
if (CertificateInspector.ServerCertificates.TryGetValue(Tuple.Create(oS.hostname, oS.port), out var cert))
{
var pkp = oS.ResponseHeaders.Exists("public-key-pins") ? oS.ResponseHeaders["public-key-pins"] : null;
var pkpReportOnly = oS.ResponseHeaders.Exists("public-key-pins-report-only") ? oS.ResponseHeaders["public-key-pins-report-only"] : null;
var pinnedKeys = pkp == null && pkpReportOnly == null ? null : PublicKeyPinsParser.Parse(pkp ?? pkpReportOnly);
var reportOnly = pkpReportOnly != null;
var chain = cert.Item1;
masterModel.HttpSecurityModel.CertificateChain = new AsyncProperty<ObservableCollection<CertificateModel>>(Task.Factory.StartNew(() =>
{
var chainItems = chain.ChainElements.Cast<X509ChainElement>().Select((t, i) => AssignCertificate(t, reportOnly, pinnedKeys, chain, i)).ToList();
return new ObservableCollection<CertificateModel>(chainItems);
}));
masterModel.HttpSecurityModel.Hpkp = new HpkpModel
{
HasHpkpHeaders = pinnedKeys != null,
RawHpkpHeader = pkp ?? pkpReportOnly,
PinDirectives =
pinnedKeys == null ? null
: new ObservableCollection<HpkpHashModel>(pinnedKeys.PinnedKeys.Select(pk => new HpkpHashModel { Algorithm = pk.Algorithm, HashBase64 = pk.FingerprintBase64 }).ToArray())
};
}
}
control.DataContext = masterModel;
_panel.Children.Add(control);
}
private static X509Chain ChainForContent(byte[] content)
{
if (content == null || content.Length < 2)
{
return null;
}
try
{
var cert = new X509Certificate2(content);
var chain = new X509Chain();
//Revocation is checked async when the view is build.
//Don't do it here.
chain.ChainPolicy.RevocationMode = X509RevocationMode.NoCheck;
chain.Build(cert);
return chain;
}
catch (Exception)
{
return null;
}
}
private CertificateModel AssignCertificate(X509ChainElement chainElement, bool reportOnly, PublicPinnedKeys pinnedKey, X509Chain chain, int index)
{
var certificate = chainElement.Certificate;
var algorithmBits = BitStrengthCalculator.CalculateStrength(certificate);
var dn = DistinguishedNameParser.Parse(certificate.Subject);
return new CertificateModel
{
CommonName = dn.ContainsKey("cn") ? dn["cn"].FirstOrDefault() ?? certificate.Thumbprint : certificate.Thumbprint,
Thumbprint = CertificateHashBuilder.BuildHashForCertificateHex<SHA256Cng>(certificate),
DistinguishedName = dn,
SubjectAlternativeName = certificate.Extensions[KnownOids.X509Extensions.SubjectAltNameExtension]?.Format(false) ?? "None",
PublicKey = new PublicKeyModel
{
Algorithm = algorithmBits.AlgorithmName,
KeySizeBits = algorithmBits.BitSize,
PublicKey = certificate.PublicKey.EncodedKeyValue.RawData
},
BeginDate = certificate.NotBefore,
EndDate = certificate.NotAfter,
SerialNumber = certificate.SerialNumber ?? "None",
SignatureAlgorithm = new SignatureAlgorithmModel
{
SignatureAlgorithm = certificate.SignatureAlgorithm,
IsTrustedRoot = _rootStore.Certificates.Contains(certificate) || _userStore.Certificates.Contains(certificate)
},
CertificateType = index == 0 ? GetCertificateType(certificate, chain) : CertificateType.None,
CertificateCtModel = new AsyncProperty<CertificateCtModel>(Task.Factory.StartNew(() => GetCtModel(certificate))),
Errors = new AsyncProperty<CertificateErrors>(Task.Factory.StartNew(() => CertificateErrorsCalculator.GetCertificateErrors(chainElement))),
SpkiHashes = new AsyncProperty<SpkiHashesModel>(Task.Factory.StartNew(() => CalculateHashes(chainElement.Certificate, reportOnly, pinnedKey))),
InstallCommand = new RelayCommand(parameter => CertificateUI.ShowImportCertificate(chainElement.Certificate, FiddlerApplication.UI)),
ViewCommand = new RelayCommand(parameter => CertificateUI.ShowCertificate(chainElement.Certificate, FiddlerApplication.UI)),
BrowseCommand = new RelayCommand(parameter =>
{
var uri = parameter as Uri;
if (uri?.Scheme == Uri.UriSchemeHttps)
{
Process.Start(uri.AbsoluteUri);
}
})
};
}
private static CertificateCtModel GetCtModel(X509Certificate2 certificate)
{
var model = new CertificateCtModel();
var extension = certificate.Extensions[KnownOids.X509Extensions.CertificateTimeStampListCT];
if (extension != null)
{
var scts = SctDecoder.DecodeData(extension);
foreach (var sct in scts)
{
var log = CtLogs.FindByLogId(sct.LogId);
model.Signatures.Add(
new SctSignatureModel
{
HashAlgorithm = sct.HashAlgorithm,
SignatureAlgorithm = sct.SignatureAlgorithm,
LogIdHex = BitConverter.ToString(sct.LogId).Replace("-", ""),
Timestamp = sct.Timestamp,
LogName = log?.Name ?? "Unknown",
LogUrl = log?.Url ?? "Unknown",
Index = sct.Index,
SignatureHex = BitConverter.ToString(sct.Signature).Replace("-", ""),
RevocationEffective = CtLogs.RevokedCtLogs.FirstOrDefault(l => l.LogId.MemoryCompare(sct.LogId))?.RevocationEffective
}
);
}
}
return model;
}
private static SpkiHashesModel CalculateHashes(X509Certificate2 certificate, bool reportOnly, PublicPinnedKeys pinnedKeys)
{
var sha256 = CertificateHashBuilder.BuildHashForPublicKeyBinary<SHA256CryptoServiceProvider>(certificate);
var model = new SpkiHashesModel
{
Hashes = new ObservableCollection<SpkiHashModel>
{
new SpkiHashModel
{
ReportOnly = reportOnly,
Algorithm = PinAlgorithm.SHA256,
Hash = sha256,
IsPinned = pinnedKeys?.PinnedKeys?.Any(pk => pk.Fingerprint.SequenceEqual(sha256)) ?? false
},
}
};
return model;
}
private static CertificateType GetCertificateType(X509Certificate2 certificate, X509Chain chain)
{
if (CertificateValidatedChecker.IsCertificateExtendedValidation(certificate, chain))
{
return CertificateType.EV;
}
if (CertificateValidatedChecker.IsCertificateIndividualValidated(certificate))
{
return CertificateType.IV;
}
if (CertificateValidatedChecker.IsCertificateOrganizationValidated(certificate))
{
return CertificateType.OV;
}
if (CertificateValidatedChecker.IsCertificateDomainValidated(certificate))
{
return CertificateType.DV;
}
return CertificateType.None;
}
public override InspectorFlags GetFlags()
{
// We don't make sense in the AutoResponder and
// wouldn't work anyway (no Session object)
return InspectorFlags.HideInAutoResponder;
}
}
}