get_mozilla_ciphers.py 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. #!/usr/bin/python
  2. # coding=utf-8
  3. # Copyright 2011, The Tor Project, Inc
  4. # original version by Arturo Filastò
  5. # See LICENSE for licensing information
  6. # This script parses Firefox and OpenSSL sources, and uses this information
  7. # to generate a ciphers.inc file.
  8. #
  9. # It takes two arguments: the location of a firefox source directory, and the
  10. # location of an openssl source directory.
  11. import os
  12. import re
  13. import sys
  14. if len(sys.argv) != 3:
  15. print >>sys.stderr, "Syntax: get_mozilla_ciphers.py <firefox-source-dir> <openssl-source-dir>"
  16. sys.exit(1)
  17. ff_root = sys.argv[1]
  18. ossl_root = sys.argv[2]
  19. def ff(s):
  20. return os.path.join(ff_root, s)
  21. def ossl(s):
  22. return os.path.join(ossl_root, s)
  23. #####
  24. # Read the cpp file to understand what Ciphers map to what name :
  25. # Make "ciphers" a map from name used in the javascript to a cipher macro name
  26. fileA = open(ff('security/manager/ssl/src/nsNSSComponent.cpp'),'r')
  27. # The input format is a file containing exactly one section of the form:
  28. # static CipherPref CipherPrefs[] = {
  29. # {"name", MACRO_NAME}, // comment
  30. # ...
  31. # {NULL, 0}
  32. # }
  33. inCipherSection = False
  34. cipherLines = []
  35. for line in fileA:
  36. if line.startswith('static CipherPref CipherPrefs'):
  37. # Get the starting boundary of the Cipher Preferences
  38. inCipherSection = True
  39. elif inCipherSection:
  40. line = line.strip()
  41. if line.startswith('{NULL, 0}'):
  42. # At the ending boundary of the Cipher Prefs
  43. break
  44. else:
  45. cipherLines.append(line)
  46. fileA.close()
  47. # Parse the lines and put them into a dict
  48. ciphers = {}
  49. cipher_pref = {}
  50. for line in cipherLines:
  51. m = re.search(r'^{\s*\"([^\"]+)\",\s*(\S*)\s*}', line)
  52. if m:
  53. key,value = m.groups()
  54. ciphers[key] = value
  55. cipher_pref[value] = key
  56. ####
  57. # Now find the correct order for the ciphers
  58. fileC = open(ff('security/nss/lib/ssl/ssl3con.c'), 'r')
  59. firefox_ciphers = []
  60. inEnum=False
  61. for line in fileC:
  62. if not inEnum:
  63. if "ssl3CipherSuiteCfg cipherSuites[" in line:
  64. inEnum = True
  65. continue
  66. if line.startswith("};"):
  67. break
  68. m = re.match(r'^\s*\{\s*([A-Z_0-9]+),', line)
  69. if m:
  70. firefox_ciphers.append(m.group(1))
  71. fileC.close()
  72. #####
  73. # Read the JS file to understand what ciphers are enabled. The format is
  74. # pref("name", true/false);
  75. # Build a map enabled_ciphers from javascript name to "true" or "false",
  76. # and an (unordered!) list of the macro names for those ciphers that are
  77. # enabled.
  78. fileB = open(ff('netwerk/base/public/security-prefs.js'), 'r')
  79. enabled_ciphers = {}
  80. for line in fileB:
  81. m = re.match(r'pref\(\"([^\"]+)\"\s*,\s*(\S*)\s*\)', line)
  82. if not m:
  83. continue
  84. key, val = m.groups()
  85. if key.startswith("security.ssl3"):
  86. enabled_ciphers[key] = val
  87. fileB.close()
  88. used_ciphers = []
  89. for k, v in enabled_ciphers.items():
  90. if v == "true":
  91. used_ciphers.append(ciphers[k])
  92. #oSSLinclude = ('/usr/include/openssl/ssl3.h', '/usr/include/openssl/ssl.h',
  93. # '/usr/include/openssl/ssl2.h', '/usr/include/openssl/ssl23.h',
  94. # '/usr/include/openssl/tls1.h')
  95. oSSLinclude = ('ssl/ssl3.h', 'ssl/ssl.h',
  96. 'ssl/ssl2.h', 'ssl/ssl23.h',
  97. 'ssl/tls1.h')
  98. #####
  99. # This reads the hex code for the ciphers that are used by firefox.
  100. # sslProtoD is set to a map from macro name to macro value in sslproto.h;
  101. # cipher_codes is set to an (unordered!) list of these hex values.
  102. sslProto = open(ff('security/nss/lib/ssl/sslproto.h'), 'r')
  103. sslProtoD = {}
  104. for line in sslProto:
  105. m = re.match('#define\s+(\S+)\s+(\S+)', line)
  106. if m:
  107. key, value = m.groups()
  108. sslProtoD[key] = value
  109. sslProto.close()
  110. cipher_codes = []
  111. for x in used_ciphers:
  112. cipher_codes.append(sslProtoD[x].lower())
  113. ####
  114. # Now read through all the openssl include files, and try to find the openssl
  115. # macro names for those files.
  116. openssl_macro_by_hex = {}
  117. all_openssl_macros = {}
  118. for fl in oSSLinclude:
  119. fp = open(ossl(fl), 'r')
  120. for line in fp.readlines():
  121. m = re.match('#define\s+(\S+)\s+(\S+)', line)
  122. if m:
  123. value,key = m.groups()
  124. if key.startswith('0x') and "_CK_" in value:
  125. key = key.replace('0x0300','0x').lower()
  126. #print "%s %s" % (key, value)
  127. openssl_macro_by_hex[key] = value
  128. all_openssl_macros[value]=key
  129. fp.close()
  130. # Now generate the output.
  131. print """\
  132. /* This is an include file used to define the list of ciphers clients should
  133. * advertise. Before including it, you should define the CIPHER and XCIPHER
  134. * macros.
  135. *
  136. * This file was automatically generated by get_mozilla_ciphers.py.
  137. */"""
  138. # Go in order by the order in CipherPrefs
  139. for firefox_macro in firefox_ciphers:
  140. try:
  141. js_cipher_name = cipher_pref[firefox_macro]
  142. except KeyError:
  143. # This one has no javascript preference.
  144. continue
  145. # The cipher needs to be enabled in security-prefs.js
  146. if enabled_ciphers.get(js_cipher_name, 'false') != 'true':
  147. continue
  148. hexval = sslProtoD[firefox_macro].lower()
  149. try:
  150. openssl_macro = openssl_macro_by_hex[hexval.lower()]
  151. openssl_macro = openssl_macro.replace("_CK_", "_TXT_")
  152. if openssl_macro not in all_openssl_macros:
  153. raise KeyError()
  154. format = {'hex':hexval, 'macro':openssl_macro, 'note':""}
  155. except KeyError:
  156. # openssl doesn't have a macro for this.
  157. format = {'hex':hexval, 'macro':firefox_macro,
  158. 'note':"/* No openssl macro found for "+hexval+" */\n"}
  159. res = """\
  160. %(note)s#ifdef %(macro)s
  161. CIPHER(%(hex)s, %(macro)s)
  162. #else
  163. XCIPHER(%(hex)s, %(macro)s)
  164. #endif""" % format
  165. print res