-
Notifications
You must be signed in to change notification settings - Fork 37
/
Copy pathMainForm.cs
305 lines (228 loc) · 18.6 KB
/
MainForm.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
using System;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Mono.Math;
namespace Moserware.TlsAnalyzer
{
public partial class MainForm : Form
{
public MainForm()
{
InitializeComponent();
}
private void btnGo_Click(object sender, EventArgs e)
{
try
{
byte[] preMasterSecret = FirefoxSslDebugFileUtilities.GetPremasterSecretKey(txtPremasterSecret.Text);
string label = txtMasterSecretLabel.Text;
byte[] serverHelloRandom = txtServerRandomBytes.Text.FromWireshark();
byte[] clientHelloRandom = txtClientRandomBytes.Text.FromWireshark();
byte[] clientHelloAndServerHello = ByteUtilities.ConcatBytes(clientHelloRandom, serverHelloRandom);
byte[] masterSecret = Prf10.GenerateBytes(preMasterSecret, label, clientHelloAndServerHello, 48);
txtMasterSecret.Text = masterSecret.ToDisplayByteString();
byte[] serverHelloAndClientHello = ByteUtilities.ConcatBytes(serverHelloRandom, clientHelloRandom);
byte[] keyBlock = Prf10.GenerateBytes(masterSecret, txtKeyExpansionLabel.Text, serverHelloAndClientHello, 96);
byte[] client_write_MAC_secret = new byte[16];
byte[] server_write_MAC_secret = new byte[16];
byte[] client_write_key = new byte[16];
byte[] server_write_key = new byte[16];
byte[] client_write_IV = new byte[16];
byte[] server_write_IV = new byte[16];
Buffer.BlockCopy(keyBlock, 0, client_write_MAC_secret, 0, 16);
txtClientWriteMacKey.Text = client_write_MAC_secret.ToDisplayByteString();
Buffer.BlockCopy(keyBlock, 16, server_write_MAC_secret, 0, 16);
txtServerWriteMacKey.Text = server_write_MAC_secret.ToDisplayByteString();
Buffer.BlockCopy(keyBlock, 32, client_write_key, 0, 16);
txtClientWriteKey.Text = client_write_key.ToDisplayByteString();
Buffer.BlockCopy(keyBlock, 48, server_write_key, 0, 16);
txtServerWriteKey.Text = server_write_key.ToDisplayByteString();
Buffer.BlockCopy(keyBlock, 64, client_write_IV, 0, 16);
txtClientIV.Text = client_write_IV.ToDisplayByteString();
Buffer.BlockCopy(keyBlock, 80, server_write_IV, 0, 16);
txtServerIV.Text = server_write_IV.ToDisplayByteString();
byte[] clientHello = txtClientHello.Text.FromWireshark();
byte[] serverHello = txtServerHello.Text.FromWireshark();
byte[] certificate = txtServerHelloCertificate.Text.FromWireshark();
byte[] serverHelloDone = txtServerHelloDone.Text.FromWireshark();
byte[] clientKeyExchangeEncrypted = txtClientKeyExchange.Text.FromWireshark();
byte[] handshakeMessages = ByteUtilities.ConcatBytes(clientHello, serverHello, certificate, serverHelloDone, clientKeyExchangeEncrypted);
txtHandshakeMessages.Text = handshakeMessages.ToDisplayByteString(16);
var md5Handshake = Hasher.ComputeMD5(handshakeMessages);
txtMd5HandshakeMessages.Text = md5Handshake.ToDisplayByteString();
var sha1Handshake = Hasher.ComputeSHA1Hash(handshakeMessages);
txtSha1HandshakeMessages.Text = sha1Handshake.ToDisplayByteString();
byte[] clientVerifyData = Prf10.GenerateBytes(masterSecret, txtClientFinishedLabel.Text, ByteUtilities.ConcatBytes(md5Handshake, sha1Handshake), 12);
txtClientFinishedVerifyData.Text = clientVerifyData.ToDisplayByteString();
var clientFinishedHeaderBytes = txtClientFinishedHeader.Text.FromWireshark();
var clientFinishedHash = Hasher.ComputeTlsMD5Hmac(client_write_MAC_secret, 0x16, 0, ByteUtilities.ConcatBytes(clientFinishedHeaderBytes, clientVerifyData));
txtClientFinishedHmacMd5.Text = clientFinishedHash.ToDisplayByteString();
var clientFinishedHeaderAndVerify = ByteUtilities.ConcatBytes(clientFinishedHeaderBytes, clientVerifyData);
var clientFinishedDecrypted = ByteUtilities.ConcatBytes(clientFinishedHeaderBytes, clientVerifyData, clientFinishedHash);
Arc4 clientWriteArc4 = new Arc4(client_write_key);
var clientFinishedEncrypted = clientWriteArc4.Encrypt(clientFinishedDecrypted);
var expectedClientFinishedEncrypted = txtClientEncryptedFinishedMessage.Text.FromWireshark();
Debug.Assert(ByteUtilities.AreEqual(expectedClientFinishedEncrypted, clientFinishedEncrypted));
byte[] clientApplicationData = txtClientApplicationDataInput.Text.FromWireshark();
byte[] decryptedBytes = clientWriteArc4.Encrypt(clientApplicationData);
byte[] plainTextBytes = new byte[decryptedBytes.Length - 16];
Buffer.BlockCopy(decryptedBytes, 0, plainTextBytes, 0, plainTextBytes.Length);
string plainText = ASCIIEncoding.ASCII.GetString(plainTextBytes);
txtDecryptedClientApplicationData.Text = plainText;
byte[] hmacClientBytesReceived = new byte[16];
Buffer.BlockCopy(decryptedBytes, plainTextBytes.Length, hmacClientBytesReceived, 0, 16);
txtClientApplicationDataHmac.Text = hmacClientBytesReceived.ToDisplayByteString();
var hmacFirstClientPacket = Hasher.ComputeTlsMD5Hmac(client_write_MAC_secret, 23, 1, plainTextBytes);
Debug.Assert(ByteUtilities.AreEqual(hmacFirstClientPacket, hmacClientBytesReceived));
// get server reply
var serverHandshakeMessages = ByteUtilities.ConcatBytes(handshakeMessages, clientFinishedHeaderAndVerify);
var serverFinishedHeader = txtServerFinishedHeader.Text.FromWireshark();
md5Handshake = Hasher.ComputeMD5(serverHandshakeMessages);
sha1Handshake = Hasher.ComputeSHA1Hash(serverHandshakeMessages);
var serverVerifyData = Prf10.GenerateBytes(masterSecret, txtServerFinishedLabel.Text, ByteUtilities.ConcatBytes(md5Handshake, sha1Handshake), 12);
txtServerFinishedVerifyData.Text = serverVerifyData.ToDisplayByteString();
var serverFirstHash = Hasher.ComputeTlsMD5Hmac(server_write_MAC_secret, 0x16, 0, ByteUtilities.ConcatBytes(serverFinishedHeader, serverVerifyData));
txtServerFinishedHmacMd5.Text = serverFirstHash.ToDisplayByteString();
var serverArc4 = new Arc4(server_write_key);
var serverFinishedMessage = ByteUtilities.ConcatBytes(serverFinishedHeader, serverVerifyData, serverFirstHash);
var encryptedServerFinishedMessage = serverArc4.Encrypt(serverFinishedMessage);
Debug.Assert(ByteUtilities.AreEqual(encryptedServerFinishedMessage, txtServerEncryptedHandshakeMessage.Text.FromWireshark()));
var serverApplicationDataBytes = txtServerApplicationDataInput.Text.FromWireshark();
var decryptedServerApplicationDataBytes = serverArc4.Encrypt(serverApplicationDataBytes);
var serverPlainTextBytes = new byte[decryptedServerApplicationDataBytes.Length - 16];
Buffer.BlockCopy(decryptedServerApplicationDataBytes, 0, serverPlainTextBytes, 0, serverPlainTextBytes.Length);
var hmacServerFirstPacketReceived = new byte[16];
Buffer.BlockCopy(decryptedServerApplicationDataBytes, serverPlainTextBytes.Length, hmacServerFirstPacketReceived, 0, 16);
txtDecryptedServerApplicationData.Text = ASCIIEncoding.ASCII.GetString(serverPlainTextBytes);
var hmacServerFirstPacketComputed = Hasher.ComputeTlsMD5Hmac(server_write_MAC_secret, 23, 1, serverPlainTextBytes);
txtServerApplicationDataHmac.Text = hmacServerFirstPacketComputed.ToDisplayByteString();
Debug.Assert(ByteUtilities.AreEqual(hmacServerFirstPacketComputed, hmacServerFirstPacketComputed));
}
catch (Exception ex)
{
MessageBox.Show("Error calculating derived handshake info: " + ex.Message);
}
}
private void btnPrfGenerate_Click(object sender, EventArgs e)
{
try
{
byte[] secretBytes = txtPrfSecretBytes.Text.FromWireshark();
string prfLabel = txtPrfLabel.Text;
byte[] seedBytes = txtPrfSeedBytes.Text.FromWireshark();
int bytesToGenerate = (int)nudBytesToGenerate.Value;
txtPrfAsciiLabelBytes.Text = prfLabel.ToAsciiBytes().ToDisplayByteString();
byte[] prfBytes = Prf10.GenerateBytes(secretBytes, prfLabel, seedBytes, bytesToGenerate);
txtPrfOutput.Text = prfBytes.ToDisplayByteString();
txtPrfMD5Output.Text = Hasher.ComputeMD5(prfBytes).ToDisplayByteString();
}
catch (Exception ex)
{
MessageBox.Show("Error generating PRF: " + ex.Message);
}
}
private void btnGenerateHmac_Click(object sender, EventArgs e)
{
try
{
byte[] keyBytes = txtHmacKeyString.Text.ToAsciiBytes();
txtHmacKeyAsciiBytes.Text = keyBytes.ToDisplayByteString();
byte[] dataBytes = txtHmacDataString.Text.ToAsciiBytes();
txtHmacDataAsciiBytes.Text = dataBytes.ToDisplayByteString();
const int blockSize = 64;
Func<byte, byte[]> getPad = padByte => Enumerable.Range(1, blockSize).Select(n=>padByte).ToArray();
// SHA-1
byte[] sha1Key = keyBytes.Length > blockSize ? Hasher.ComputeSHA1Hash(keyBytes) : keyBytes;
byte[] sha1Opad = getPad(0x5c);
txtHmacSha1Opad.Text = sha1Opad.ToDisplayByteString();
byte[] sha1KeyXorOpad = sha1Key.Xor(sha1Opad);
txtHmacSha1KeyXorOpad.Text = sha1KeyXorOpad.ToDisplayByteString();
byte[] sha1Ipad = getPad(0x36);
txtHmacSha1IpadBytes.Text = sha1Ipad.ToDisplayByteString();
byte[] sha1KeyXorIpad = sha1Key.Xor(sha1Ipad);
txtHmacSha1KeyXorIpad.Text = sha1KeyXorIpad.ToDisplayByteString();
byte[] sha1TotalInnerToHash = ByteUtilities.ConcatBytes(sha1KeyXorIpad, dataBytes);
byte[] sha1InnerHash = Hasher.ComputeSHA1Hash(sha1TotalInnerToHash);
txtHmacSha1InnerHash.Text = sha1InnerHash.ToDisplayByteString();
byte[] sha1Hmac = Hasher.ComputeSHA1Hash(ByteUtilities.ConcatBytes(sha1KeyXorOpad, sha1InnerHash));
byte[] sha1ExpectedHmac = Hasher.ComputeSHA1Hmac(keyBytes, dataBytes);
Debug.Assert(ByteUtilities.AreEqual(sha1ExpectedHmac, sha1Hmac));
txtHmacSha1Result.Text = sha1Hmac.ToDisplayByteString();
// MD5
byte[] md5Key = keyBytes.Length > blockSize ? Hasher.ComputeMD5(keyBytes) : keyBytes;
byte[] md5Opad = getPad(0x5c);
txtHmacMd5Opad.Text = md5Opad.ToDisplayByteString();
byte[] md5KeyXorOpad = md5Key.Xor(md5Opad);
txtHmacMd5KeyXorOpad.Text = md5KeyXorOpad.ToDisplayByteString();
byte[] md5Ipad = getPad(0x36);
txtHmacMd5IpadBytes.Text = md5Ipad.ToDisplayByteString();
byte[] md5KeyXorIpad = md5Key.Xor(md5Ipad);
txtHmacMd5KeyXorIpad.Text = md5KeyXorIpad.ToDisplayByteString();
byte[] md5TotalInnerToHash = ByteUtilities.ConcatBytes(md5KeyXorIpad, dataBytes);
byte[] md5InnerHash = Hasher.ComputeMD5(md5TotalInnerToHash);
txtHmacMd5InnerHash.Text = md5InnerHash.ToDisplayByteString();
byte[] md5Hmac = Hasher.ComputeMD5(ByteUtilities.ConcatBytes(md5KeyXorOpad, md5InnerHash));
byte[] md5ExpectedHmac = Hasher.ComputeMD5Hmac(keyBytes, dataBytes);
Debug.Assert(ByteUtilities.AreEqual(md5ExpectedHmac, md5Hmac));
txtHmacMd5Result.Text = md5Hmac.ToDisplayByteString();
}
catch (Exception ex)
{
MessageBox.Show("Error: " + ex.Message);
}
}
private void btnCalculateCertificateInformation_Click(object sender, EventArgs e)
{
try
{
// Get moduli ahead of time since they'll be needed in a chained fashion
byte[] amazonModulusBytes = txtAmazonModulus.Text.FromWireshark();
BigInteger amazonModulus = new BigInteger(amazonModulusBytes);
txtAmazonModulusBase10.Text = amazonModulus.ToDisplayString();
byte[] amazonPublicExponentBytes = txtAmazonPublicExponent.Text.FromWireshark();
byte[] verisignClass3SecureServerModulusBytes = txtVerisignClass3SecureServerModulus.Text.FromWireshark();
BigInteger verisignClass3SecureServerModulus = new BigInteger(verisignClass3SecureServerModulusBytes);
txtVerisignClass3SecureServerModulusBase10.Text = verisignClass3SecureServerModulus.ToDisplayString();
byte[] verisignClass3SecureServerPublicExponentBytes = txtVerisignClass3SecureServerPublicExponent.Text.FromWireshark();
byte[] verisignClass3PrimaryCertificationAuthorityModulusBytes = txtVerisignClass3PrimaryCertificationAuthorityModulus.Text.FromWireshark();
BigInteger verisignClass3PrimaryCertificationAuthorityModulus = new BigInteger(verisignClass3PrimaryCertificationAuthorityModulusBytes);
txtVerisignClass3PrimaryCertificationAuthorityModulusBase10.Text = verisignClass3PrimaryCertificationAuthorityModulus.ToDisplayString();
byte[] verisignClass3PrimaryCertificationAuthorityPublicExponentBytes = txtVerisignClass3PrimaryCertificationAuthorityPublicExponent.Text.FromWireshark();
byte[] amazonSignedCertificateBytes = txtAmazonSignatureValue.Text.FromWireshark();
byte[] amazonDecryptedSignatureBytes = RsaUtilities.GetSignedOriginalValue(amazonSignedCertificateBytes, verisignClass3SecureServerPublicExponentBytes, verisignClass3SecureServerModulusBytes);
txtAmazonDecryptedSignature.Text = amazonDecryptedSignatureBytes.ToDisplayByteString(16);
const int sha1HashSize = 20; // bytes
byte[] amazonHashValueBytes = amazonDecryptedSignatureBytes.SubBytes(amazonDecryptedSignatureBytes.Length - sha1HashSize);
Debug.Assert(ByteUtilities.AreEqual(Hasher.ComputeSHA1Hash(txtAmazonSignedCertificate.Text.FromWireshark()), amazonHashValueBytes));
txtAmazonHashValue.Text = amazonHashValueBytes.ToDisplayByteString();
// For algorithm info, see http://tools.ietf.org/html/rfc3447#page-43
const int algorithmIdSize = 15; // bytes
byte[] amazonAlgorithmIdBytes = amazonDecryptedSignatureBytes.SubBytes(amazonDecryptedSignatureBytes.Length - sha1HashSize - algorithmIdSize, algorithmIdSize);
txtAmazonHashAlgorithmId.Text = amazonAlgorithmIdBytes.ToDisplayByteString();
byte[] verisignClass3SecureServerSignatureValueBytes = txtVerisignClass3SecureServerSignatureValue.Text.FromWireshark();
byte[] verisignClass3SecureServerDecryptedSignatureBytes = RsaUtilities.GetSignedOriginalValue(verisignClass3SecureServerSignatureValueBytes, verisignClass3PrimaryCertificationAuthorityPublicExponentBytes, verisignClass3PrimaryCertificationAuthorityModulusBytes);
txtVerisignClass3SecureServerDecryptedSignature.Text = verisignClass3SecureServerDecryptedSignatureBytes.ToDisplayByteString(16);
byte[] verisignClass3SecureServerHashValueBytes = verisignClass3SecureServerDecryptedSignatureBytes.SubBytes(verisignClass3SecureServerDecryptedSignatureBytes.Length - sha1HashSize);
Debug.Assert(ByteUtilities.AreEqual(Hasher.ComputeSHA1Hash(txtVersignClass3SecureServerSignedCertificate.Text.FromWireshark()), verisignClass3SecureServerHashValueBytes));
txtVerisignClass3SecureServerHashValue.Text = verisignClass3SecureServerHashValueBytes.ToDisplayByteString();
byte[] verisignClass3SecureServerAlgorithmIdBytes = verisignClass3SecureServerDecryptedSignatureBytes.SubBytes(verisignClass3SecureServerDecryptedSignatureBytes.Length - sha1HashSize - algorithmIdSize, algorithmIdSize);
txtVerisignClass3SecureServerHashAlgorithmId.Text = verisignClass3SecureServerAlgorithmIdBytes.ToDisplayByteString();
byte[] verisignClass3PrimaryCertificationAuthoritySignatureValueBytes = txtVerisignClass3PrimaryCertificationAuthoritySignatureValue.Text.FromWireshark();
byte[] verisignClass3PrimaryCertificationAuthorityDecryptedSignatureBytes = RsaUtilities.GetSignedOriginalValue(verisignClass3PrimaryCertificationAuthoritySignatureValueBytes, verisignClass3PrimaryCertificationAuthorityPublicExponentBytes, verisignClass3PrimaryCertificationAuthorityModulusBytes);
txtVerisignClass3PrimaryCertificationAuthorityDecryptedSignature.Text = verisignClass3PrimaryCertificationAuthorityDecryptedSignatureBytes.ToDisplayByteString(16);
const int md2HashSize = 16; // bytes
int md2AlgorithmIdSize = algorithmIdSize + 3;
byte[] verisignClass3PrimaryCertificationAuthorityHashValueBytes = verisignClass3SecureServerDecryptedSignatureBytes.SubBytes(verisignClass3SecureServerDecryptedSignatureBytes.Length - md2HashSize);
txtVerisignClass3PrimaryCertificationAuthorityHashValue.Text = verisignClass3PrimaryCertificationAuthorityHashValueBytes.ToDisplayByteString();
byte[] verisignClass3PrimaryCertificationAuthorityAlgorithmIdBytes = verisignClass3PrimaryCertificationAuthorityDecryptedSignatureBytes.SubBytes(verisignClass3PrimaryCertificationAuthorityDecryptedSignatureBytes.Length - md2HashSize - md2AlgorithmIdSize, md2AlgorithmIdSize);
txtVerisignClass3PrimaryCertificationAuthorityHashAlgorithmId.Text = verisignClass3PrimaryCertificationAuthorityAlgorithmIdBytes.ToDisplayByteString();
}
catch (Exception ex)
{
MessageBox.Show("Error: " + ex.Message);
}
}
}
}