Debian Codegraph @

lib_pypy/_cffi_ssl/tools/make_ssl_data.py

raw symbols SCIP
1 #! /usr/bin/env python3
2
3 """
4 This script should be called *manually* when we want to upgrade SSLError
5 `library` and `reason` mnemnonics to a more recent OpenSSL version.
6 It takes two arguments:
7 - the path to the OpenSSL source tree (e.g. git checkout)
8 - the path to the wrapper file to be generated
9 - error codes are version specific
10
11 Sample invocation (from folder containing this file):
12 python make_ssl_data.py ../openssl/src/ ../_stdssl/errorcodes.py
13 """
14
15 import argparse
16 import datetime
17 import operator
18 import os
19 import re
20 import sys
21
22
23 parser = argparse.ArgumentParser(
24 description="Generate ssl_data.h from OpenSSL sources"
25 )
26 parser.add_argument("srcdir", help="OpenSSL source directory")
27 parser.add_argument(
28 "output", nargs="?", type=argparse.FileType("w"), default=sys.stdout
29 )
30
31
32 def _file_search(fname, pat):
33 with open(fname, encoding="utf-8") as f:
34 for line in f:
35 match = pat.search(line)
36 if match is not None:
37 yield match
38
39
40 def parse_err_h(args):
41 """Parse err codes, e.g. ERR_LIB_X509: 11"""
42 pat = re.compile(r"#\s*define\W+ERR_LIB_(\w+)\s+(\d+)")
43 lib2errnum = {}
44 for match in _file_search(args.err_h, pat):
45 libname, num = match.groups()
46 lib2errnum[libname] = int(num)
47
48 return lib2errnum
49
50
51 def parse_openssl_error_text(args):
52 """Parse error reasons, X509_R_AKID_MISMATCH"""
53 # ignore backslash line continuation for now
54 pat = re.compile(r"^((\w+?)_R_(\w+)):(\d+):")
55 for match in _file_search(args.errtxt, pat):
56 reason, libname, errname, num = match.groups()
57 if "_F_" in reason:
58 # ignore function codes
59 continue
60 num = int(num)
61 yield reason, libname, errname, num
62
63
64 def parse_extra_reasons(args):
65 """Parse extra reasons from openssl.ec"""
66 pat = re.compile(r"^R\s+((\w+)_R_(\w+))\s+(\d+)")
67 for match in _file_search(args.errcodes, pat):
68 reason, libname, errname, num = match.groups()
69 num = int(num)
70 yield reason, libname, errname, num
71
72
73 def gen_library_codes(args):
74 """Generate table short libname to numeric code"""
75 yield "_lib_codes = ["
76 for libname in sorted(args.lib2errnum):
77 yield f' ("{libname}", lib.ERR_LIB_{libname}),'
78 yield "]"
79 yield ""
80
81
82 def gen_error_codes(args):
83 """Generate error code table for error reasons"""
84 yield "_error_codes = ["
85 for reason, libname, errname, num in args.reasons:
86 yield f' ("{errname}", lib.ERR_LIB_{libname}, {num}),'
87 yield "]"
88 yield ""
89
90
91 def main():
92 args = parser.parse_args()
93
94 args.err_h = os.path.join(args.srcdir, "include", "openssl", "err.h")
95 if not os.path.isfile(args.err_h):
96 # Fall back to infile for OpenSSL 3.0.0
97 args.err_h += ".in"
98 args.errcodes = os.path.join(args.srcdir, "crypto", "err", "openssl.ec")
99 args.errtxt = os.path.join(args.srcdir, "crypto", "err", "openssl.txt")
100
101 if not os.path.isfile(args.errtxt):
102 parser.error(f"File {args.errtxt} not found in srcdir\n.")
103
104 # {X509: 11, ...}
105 args.lib2errnum = parse_err_h(args)
106
107 # [('X509_R_AKID_MISMATCH', 'X509', 'AKID_MISMATCH', 110), ...]
108 reasons = []
109 reasons.extend(parse_openssl_error_text(args))
110 reasons.extend(parse_extra_reasons(args))
111 # sort by libname, numeric error code
112 args.reasons = sorted(reasons, key=operator.itemgetter(0, 3))
113
114 lines = [
115 "# File generated by lib_pypy/_cffi_ssl/tools/make_ssl_data.py",
116 f"# Generated on {datetime.datetime.utcnow().isoformat()}",
117 "",
118 "from _pypy_openssl import ffi, lib"
119 ]
120 lines.extend(gen_library_codes(args))
121 lines.append("")
122 lines.extend(gen_error_codes(args))
123
124 args.output.write("\n".join(lines))
125
126
127 if __name__ == "__main__":
128 main()