2012-06-22 16:12:59 -03:00
|
|
|
#! /usr/bin/env python3
|
|
|
|
|
2012-06-22 17:07:01 -03:00
|
|
|
"""
|
|
|
|
This script should be called *manually* when we want to upgrade SSLError
|
2019-07-30 19:16:13 -03:00
|
|
|
`library` and `reason` mnemonics to a more recent OpenSSL version.
|
2012-06-22 17:07:01 -03:00
|
|
|
|
|
|
|
It takes two arguments:
|
2015-01-18 12:39:32 -04:00
|
|
|
- the path to the OpenSSL source tree (e.g. git checkout)
|
2012-06-22 17:07:01 -03:00
|
|
|
- the path to the C file to be generated
|
|
|
|
(probably Modules/_ssl_data.h)
|
|
|
|
"""
|
|
|
|
|
2012-06-22 16:12:59 -03:00
|
|
|
import datetime
|
2020-04-11 17:36:12 -03:00
|
|
|
import glob
|
2012-06-22 16:12:59 -03:00
|
|
|
import os
|
|
|
|
import re
|
|
|
|
import sys
|
2015-01-18 12:39:32 -04:00
|
|
|
import _ssl
|
2012-06-22 16:12:59 -03:00
|
|
|
|
|
|
|
|
2015-01-18 12:39:32 -04:00
|
|
|
def parse_error_codes(h_file, prefix, libcode):
|
2020-04-11 17:36:12 -03:00
|
|
|
pat = re.compile(r"#\s*define\W+(%s([\w]+))\W+(\d+)\b" % re.escape(prefix))
|
2012-06-22 16:12:59 -03:00
|
|
|
codes = []
|
|
|
|
with open(h_file, "r", encoding="latin1") as f:
|
|
|
|
for line in f:
|
|
|
|
match = pat.search(line)
|
|
|
|
if match:
|
|
|
|
code, name, num = match.groups()
|
|
|
|
num = int(num)
|
2015-01-18 12:39:32 -04:00
|
|
|
# e.g. ("SSL_R_BAD_DATA", ("ERR_LIB_SSL", "BAD_DATA", 390))
|
|
|
|
codes.append((code, (libcode, name, num)))
|
2020-04-11 17:36:12 -03:00
|
|
|
assert codes, f"no codes found in {h_file}"
|
2012-06-22 16:12:59 -03:00
|
|
|
return codes
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
openssl_inc = sys.argv[1]
|
|
|
|
outfile = sys.argv[2]
|
|
|
|
use_stdout = outfile == '-'
|
|
|
|
f = sys.stdout if use_stdout else open(outfile, "w")
|
2020-04-11 17:36:12 -03:00
|
|
|
# mnemonic -> (library code, error prefix, header file)
|
|
|
|
error_libraries = {}
|
2020-06-20 05:10:31 -03:00
|
|
|
for error_header in glob.glob(os.path.join(glob.escape(openssl_inc), 'include/openssl/*err.h')):
|
2020-04-11 17:36:12 -03:00
|
|
|
base = os.path.basename(error_header)
|
|
|
|
if base in ('buffererr.h', 'objectserr.h', 'storeerr.h'):
|
|
|
|
# Deprecated in 3.0.
|
|
|
|
continue
|
|
|
|
mnemonic = base[:-5].upper()
|
|
|
|
if mnemonic == "":
|
2020-04-14 00:11:40 -03:00
|
|
|
# err.h
|
|
|
|
lib_codes = {
|
|
|
|
code: num
|
|
|
|
for (code, (_, _, num)) in parse_error_codes(error_header, 'ERR_LIB_', None)
|
|
|
|
}
|
|
|
|
else:
|
|
|
|
error_libraries[mnemonic] = (f'ERR_LIB_{mnemonic}', f'{mnemonic}_R_', error_header)
|
2015-01-18 12:39:32 -04:00
|
|
|
|
|
|
|
# Read codes from libraries
|
|
|
|
new_codes = []
|
|
|
|
for libcode, prefix, h_file in sorted(error_libraries.values()):
|
2020-04-11 17:36:12 -03:00
|
|
|
new_codes += parse_error_codes(h_file, prefix, libcode)
|
2015-01-18 12:39:32 -04:00
|
|
|
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))
|
|
|
|
|
2012-06-22 16:12:59 -03:00
|
|
|
def w(l):
|
|
|
|
f.write(l + "\n")
|
|
|
|
w("/* File generated by Tools/ssl/make_ssl_data.py */")
|
|
|
|
w("/* Generated on %s */" % datetime.datetime.now().isoformat())
|
|
|
|
w("")
|
|
|
|
|
|
|
|
w("static struct py_ssl_library_code library_codes[] = {")
|
2015-01-18 12:39:32 -04:00
|
|
|
for mnemo, (libcode, _, _) in sorted(error_libraries.items()):
|
2020-04-12 15:59:31 -03:00
|
|
|
w(f'#ifdef {libcode}')
|
2012-06-22 16:12:59 -03:00
|
|
|
w(' {"%s", %s},' % (mnemo, libcode))
|
2020-04-12 15:59:31 -03:00
|
|
|
w('#endif')
|
2012-06-22 16:12:59 -03:00
|
|
|
w(' { NULL }')
|
|
|
|
w('};')
|
|
|
|
w("")
|
|
|
|
|
|
|
|
w("static struct py_ssl_error_code error_codes[] = {")
|
2015-01-18 12:39:32 -04:00
|
|
|
for errcode, (libcode, name, num) in sorted(codes.items()):
|
|
|
|
w(' #ifdef %s' % (errcode))
|
|
|
|
w(' {"%s", %s, %s},' % (name, libcode, errcode))
|
|
|
|
w(' #else')
|
2020-04-14 00:11:40 -03:00
|
|
|
w(' {"%s", %s, %d},' % (name, lib_codes[libcode], num))
|
2015-01-18 12:39:32 -04:00
|
|
|
w(' #endif')
|
2012-06-22 16:12:59 -03:00
|
|
|
w(' { NULL }')
|
|
|
|
w('};')
|
|
|
|
if not use_stdout:
|
|
|
|
f.close()
|