-
Notifications
You must be signed in to change notification settings - Fork 37
/
FirefoxSslDebugFileUtilities.cs
65 lines (59 loc) · 2.29 KB
/
FirefoxSslDebugFileUtilities.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
using System;
using System.IO;
namespace Moserware.TlsAnalyzer
{
/// <summary>
/// Utilities to work with Firefox's debug files.
/// </summary>
public static class FirefoxSslDebugFileUtilities
{
/// <summary>
/// Helper function for things dumped to SSLDEBUGFILE with appropriate SSLTRACE levels.
/// </summary>
/// <param name="input">Raw input that has the pre-master secret.</param>
/// <returns>The pre-master secret key bytes.</returns>
public static byte[] GetPremasterSecretKey(string input)
{
// Looks like
// 5140: SSL[75821480]: Pre-Master Secret [Len: 48]
// 03 01 97 01 9e aa 3c 3c e1 ef 7f 39 5d be 88 1e ......<<...9]...
// 60 51 e7 f5 94 db fd 62 b2 b5 26 be b5 3d 7c 16 `Q.....b..&..=|.
// 4d ff 79 73 8e cb c8 aa 9c 70 f2 5d 29 91 72 50 M.ys.....p.]).rP
using (var sr = new StringReader(input))
{
bool foundHeader = false;
while (sr.Peek() >= 0)
{
string currentLine = sr.ReadLine().Trim();
if (!foundHeader)
{
if (!currentLine.Contains("Pre-Master Secret"))
{
continue;
}
foundHeader = true;
break;
}
}
if (!foundHeader)
{
throw new InvalidDataException();
}
// reading in secret bytes
using (var ms = new MemoryStream())
{
for (int ixCurrentLine = 0; ixCurrentLine < 3; ixCurrentLine++)
{
string currentLine = sr.ReadLine().Trim();
for (int ixCurrentByte = 0; ixCurrentByte < 16; ixCurrentByte++)
{
string byteText = currentLine.Substring(3 * ixCurrentByte, 2);
ms.WriteByte(Convert.ToByte(byteText, 16));
}
}
return ms.ToArray();
}
}
}
}
}