summaryrefslogtreecommitdiff
path: root/Tools/ssl
diff options
context:
space:
mode:
authorAntoine Pitrou <solipsis@pitrou.net>2015-01-18 17:40:17 +0100
committerAntoine Pitrou <solipsis@pitrou.net>2015-01-18 17:40:17 +0100
commit3b225d8bfb748a02d4bada14d4498e9b6aa4e162 (patch)
tree5299d2fda426e0aa04047c783226087d42e209fd /Tools/ssl
parente6f250ed90d0efed83f9e3beef97e5f9e6398d00 (diff)
parent173ad83b074b3bf0c9e86eb8bd101c2841f74297 (diff)
downloadcpython-git-3b225d8bfb748a02d4bada14d4498e9b6aa4e162.tar.gz
Issue #23248: Update ssl error codes from latest OpenSSL git master.
Diffstat (limited to 'Tools/ssl')
-rwxr-xr-xTools/ssl/make_ssl_data.py57
1 files changed, 38 insertions, 19 deletions
diff --git a/Tools/ssl/make_ssl_data.py b/Tools/ssl/make_ssl_data.py
index 10244d106f..3fb49852f4 100755
--- a/Tools/ssl/make_ssl_data.py
+++ b/Tools/ssl/make_ssl_data.py
@@ -5,8 +5,7 @@ This script should be called *manually* when we want to upgrade SSLError
`library` and `reason` mnemnonics to a more recent OpenSSL version.
It takes two arguments:
-- the path to the OpenSSL include files' directory
- (e.g. openssl-1.0.1-beta3/include/openssl/)
+- the path to the OpenSSL source tree (e.g. git checkout)
- the path to the C file to be generated
(probably Modules/_ssl_data.h)
"""
@@ -15,9 +14,10 @@ import datetime
import os
import re
import sys
+import _ssl
-def parse_error_codes(h_file, prefix):
+def parse_error_codes(h_file, prefix, libcode):
pat = re.compile(r"#define\W+(%s([\w]+))\W+(\d+)\b" % re.escape(prefix))
codes = []
with open(h_file, "r", encoding="latin1") as f:
@@ -26,7 +26,8 @@ def parse_error_codes(h_file, prefix):
if match:
code, name, num = match.groups()
num = int(num)
- codes.append((code, name, num))
+ # e.g. ("SSL_R_BAD_DATA", ("ERR_LIB_SSL", "BAD_DATA", 390))
+ codes.append((code, (libcode, name, num)))
return codes
if __name__ == "__main__":
@@ -34,12 +35,32 @@ if __name__ == "__main__":
outfile = sys.argv[2]
use_stdout = outfile == '-'
f = sys.stdout if use_stdout else open(outfile, "w")
- error_libraries = (
- # (library code, mnemonic, error prefix, header file)
- ('ERR_LIB_PEM', 'PEM', 'PEM_R_', 'pem.h'),
- ('ERR_LIB_SSL', 'SSL', 'SSL_R_', 'ssl.h'),
- ('ERR_LIB_X509', 'X509', 'X509_R_', 'x509.h'),
- )
+ error_libraries = {
+ # mnemonic -> (library code, error prefix, header file)
+ 'PEM': ('ERR_LIB_PEM', 'PEM_R_', 'crypto/pem/pem.h'),
+ 'SSL': ('ERR_LIB_SSL', 'SSL_R_', 'ssl/ssl.h'),
+ 'X509': ('ERR_LIB_X509', 'X509_R_', 'crypto/x509/x509.h'),
+ }
+
+ # Read codes from libraries
+ new_codes = []
+ for libcode, prefix, h_file in sorted(error_libraries.values()):
+ new_codes += parse_error_codes(os.path.join(openssl_inc, h_file),
+ prefix, libcode)
+ new_code_nums = set((libcode, num)
+ for (code, (libcode, name, num)) in new_codes)
+
+ # Merge with existing codes (in case some old codes disappeared).
+ codes = {}
+ for errname, (libnum, errnum) in _ssl.err_names_to_codes.items():
+ lib = error_libraries[_ssl.lib_codes_to_names[libnum]]
+ libcode = lib[0] # e.g. ERR_LIB_PEM
+ errcode = lib[1] + errname # e.g. SSL_R_BAD_SSL_SESSION_ID_LENGTH
+ # Only keep it if the numeric codes weren't reused
+ if (libcode, errnum) not in new_code_nums:
+ codes[errcode] = libcode, errname, errnum
+ codes.update(dict(new_codes))
+
def w(l):
f.write(l + "\n")
w("/* File generated by Tools/ssl/make_ssl_data.py */")
@@ -47,21 +68,19 @@ if __name__ == "__main__":
w("")
w("static struct py_ssl_library_code library_codes[] = {")
- for libcode, mnemo, _, _ in error_libraries:
+ for mnemo, (libcode, _, _) in sorted(error_libraries.items()):
w(' {"%s", %s},' % (mnemo, libcode))
w(' { NULL }')
w('};')
w("")
w("static struct py_ssl_error_code error_codes[] = {")
- for libcode, _, prefix, h_file in error_libraries:
- codes = parse_error_codes(os.path.join(openssl_inc, h_file), prefix)
- for code, name, num in sorted(codes):
- w(' #ifdef %s' % (code))
- w(' {"%s", %s, %s},' % (name, libcode, code))
- w(' #else')
- w(' {"%s", %s, %d},' % (name, libcode, num))
- w(' #endif')
+ for errcode, (libcode, name, num) in sorted(codes.items()):
+ w(' #ifdef %s' % (errcode))
+ w(' {"%s", %s, %s},' % (name, libcode, errcode))
+ w(' #else')
+ w(' {"%s", %s, %d},' % (name, libcode, num))
+ w(' #endif')
w(' { NULL }')
w('};')
if not use_stdout: