Ticket #4744: get_mozilla_ciphers.py

File get_mozilla_ciphers.py, 3.1 KB (added by hellais, 8 years ago)

Get the default ciphers of Mozilla Firefox

Line 
1# Read the cpp file to understand what Ciphers map to what name
2fileA = open('security/manager/ssl/src/nsNSSComponent.cpp','r')
3
4start = None
5lines = fileA.readlines()
6for i, line in enumerate(lines):
7    if line.strip().startswith('static CipherPref CipherPrefs'):
8        # Get the starting boundary of the Cipher Preferences
9        start = i
10
11    if start and line.strip().startswith('{NULL, 0}'):
12        # Get the ending boundary of the Cipher Prefs
13        end = i
14        break
15fileA.close()
16
17# Parse the lines and put them into a dict
18ciphers = {}
19for x in lines[start:end]:
20    line = x.strip()
21    if line.startswith('{'):
22        for i, y in enumerate(line):
23            if y == '}':
24                parsed = line[1:i]
25        key, value = parsed.split(',')
26        ciphers[key.replace("\"","")] = value.strip()
27
28# Read the JS file to understand what ciphers are enabled
29fileB = open('netwerk/base/public/security-prefs.js', 'r')
30
31enabled_ciphers = {}
32for x in fileB.readlines():
33    start = None
34    line = x.strip()
35    for i, y in enumerate(line):
36        if y == "(":
37            start = i
38        if start and y == ")":
39            end = i
40    if start:
41        inner = line[start+1:end]
42        key, value = inner.replace("\"","").split(",")
43        if key.startswith('security.ssl3'):
44            enabled_ciphers[key.strip()] = value.strip()
45fileB.close()
46
47used_ciphers = []
48for k, v in enabled_ciphers.items():
49    if v == "true":
50        used_ciphers.append(ciphers[k])
51
52
53oSSLinclude = ('/usr/include/openssl/ssl3.h', '/usr/include/openssl/ssl.h',
54               '/usr/include/openssl/ssl2.h', '/usr/include/openssl/ssl23.h',
55               '/usr/include/openssl/tls1.h')
56
57sslProto = open('security/nss/lib/ssl/sslproto.h', 'r')
58sslProtoD = {}
59
60# This reads the HEX code for the ciphers that are used
61# by firefox.
62for x in sslProto.readlines():
63    line = x.strip()
64    if line.lower().startswith("#define"):
65        # If I ever see somebody putting mixed tab
66        # and spaces I will cut their fingers :)
67        line = line.replace('\t', ' ')
68        key = line.split(' ')[1]
69        value = line.split(' ')[-1]
70        key = key.strip()
71        value = value.strip()
72        print "%s %s\n\n" % (key, value)
73        sslProtoD[key] = value
74sslProto.close()
75
76cipher_codes = []
77for x in used_ciphers:
78    cipher_codes.append(sslProtoD[x].lower())
79
80cipher_hex = {}
81for fl in oSSLinclude:
82    fp = open(fl, 'r')
83    for x in fp.readlines():
84        line = x.strip()
85        if line.lower().startswith("#define"):
86            line = line.replace('\t', ' ')
87            value = line.split(' ')[1]
88            key = line.split(' ')[-1]
89            key = key.strip()
90            value = value.strip()
91            if key.startswith('0x'):
92                key = key.replace('0x0300','0x').lower()
93                #print "%s %s" % (key, value)
94                cipher_hex[key] = value
95    fp.close()
96
97for x in cipher_codes:
98    try:
99        res = """#ifdef %s
100        CIPHER(%s, %s)
101    #else
102       XCIPHER(%s, %s)
103    #endif""" % (cipher_hex[x], x, cipher_hex[x], x, cipher_hex[x])
104        print res
105    except:
106        print "Not found %s" % x
107
108
109#print enabled_ciphers
110#print ciphers