1 | n/a | #! /usr/bin/env python3 |
---|
2 | n/a | |
---|
3 | n/a | """ |
---|
4 | n/a | This script should be called *manually* when we want to upgrade SSLError |
---|
5 | n/a | `library` and `reason` mnemnonics to a more recent OpenSSL version. |
---|
6 | n/a | |
---|
7 | n/a | It takes two arguments: |
---|
8 | n/a | - the path to the OpenSSL source tree (e.g. git checkout) |
---|
9 | n/a | - the path to the C file to be generated |
---|
10 | n/a | (probably Modules/_ssl_data.h) |
---|
11 | n/a | """ |
---|
12 | n/a | |
---|
13 | n/a | import datetime |
---|
14 | n/a | import os |
---|
15 | n/a | import re |
---|
16 | n/a | import sys |
---|
17 | n/a | import _ssl |
---|
18 | n/a | |
---|
19 | n/a | |
---|
20 | n/a | def parse_error_codes(h_file, prefix, libcode): |
---|
21 | n/a | pat = re.compile(r"#define\W+(%s([\w]+))\W+(\d+)\b" % re.escape(prefix)) |
---|
22 | n/a | codes = [] |
---|
23 | n/a | with open(h_file, "r", encoding="latin1") as f: |
---|
24 | n/a | for line in f: |
---|
25 | n/a | match = pat.search(line) |
---|
26 | n/a | if match: |
---|
27 | n/a | code, name, num = match.groups() |
---|
28 | n/a | num = int(num) |
---|
29 | n/a | # e.g. ("SSL_R_BAD_DATA", ("ERR_LIB_SSL", "BAD_DATA", 390)) |
---|
30 | n/a | codes.append((code, (libcode, name, num))) |
---|
31 | n/a | return codes |
---|
32 | n/a | |
---|
33 | n/a | if __name__ == "__main__": |
---|
34 | n/a | openssl_inc = sys.argv[1] |
---|
35 | n/a | outfile = sys.argv[2] |
---|
36 | n/a | use_stdout = outfile == '-' |
---|
37 | n/a | f = sys.stdout if use_stdout else open(outfile, "w") |
---|
38 | n/a | error_libraries = { |
---|
39 | n/a | # mnemonic -> (library code, error prefix, header file) |
---|
40 | n/a | 'PEM': ('ERR_LIB_PEM', 'PEM_R_', 'crypto/pem/pem.h'), |
---|
41 | n/a | 'SSL': ('ERR_LIB_SSL', 'SSL_R_', 'ssl/ssl.h'), |
---|
42 | n/a | 'X509': ('ERR_LIB_X509', 'X509_R_', 'crypto/x509/x509.h'), |
---|
43 | n/a | } |
---|
44 | n/a | |
---|
45 | n/a | # Read codes from libraries |
---|
46 | n/a | new_codes = [] |
---|
47 | n/a | for libcode, prefix, h_file in sorted(error_libraries.values()): |
---|
48 | n/a | new_codes += parse_error_codes(os.path.join(openssl_inc, h_file), |
---|
49 | n/a | prefix, libcode) |
---|
50 | n/a | new_code_nums = set((libcode, num) |
---|
51 | n/a | for (code, (libcode, name, num)) in new_codes) |
---|
52 | n/a | |
---|
53 | n/a | # Merge with existing codes (in case some old codes disappeared). |
---|
54 | n/a | codes = {} |
---|
55 | n/a | for errname, (libnum, errnum) in _ssl.err_names_to_codes.items(): |
---|
56 | n/a | lib = error_libraries[_ssl.lib_codes_to_names[libnum]] |
---|
57 | n/a | libcode = lib[0] # e.g. ERR_LIB_PEM |
---|
58 | n/a | errcode = lib[1] + errname # e.g. SSL_R_BAD_SSL_SESSION_ID_LENGTH |
---|
59 | n/a | # Only keep it if the numeric codes weren't reused |
---|
60 | n/a | if (libcode, errnum) not in new_code_nums: |
---|
61 | n/a | codes[errcode] = libcode, errname, errnum |
---|
62 | n/a | codes.update(dict(new_codes)) |
---|
63 | n/a | |
---|
64 | n/a | def w(l): |
---|
65 | n/a | f.write(l + "\n") |
---|
66 | n/a | w("/* File generated by Tools/ssl/make_ssl_data.py */") |
---|
67 | n/a | w("/* Generated on %s */" % datetime.datetime.now().isoformat()) |
---|
68 | n/a | w("") |
---|
69 | n/a | |
---|
70 | n/a | w("static struct py_ssl_library_code library_codes[] = {") |
---|
71 | n/a | for mnemo, (libcode, _, _) in sorted(error_libraries.items()): |
---|
72 | n/a | w(' {"%s", %s},' % (mnemo, libcode)) |
---|
73 | n/a | w(' { NULL }') |
---|
74 | n/a | w('};') |
---|
75 | n/a | w("") |
---|
76 | n/a | |
---|
77 | n/a | w("static struct py_ssl_error_code error_codes[] = {") |
---|
78 | n/a | for errcode, (libcode, name, num) in sorted(codes.items()): |
---|
79 | n/a | w(' #ifdef %s' % (errcode)) |
---|
80 | n/a | w(' {"%s", %s, %s},' % (name, libcode, errcode)) |
---|
81 | n/a | w(' #else') |
---|
82 | n/a | w(' {"%s", %s, %d},' % (name, libcode, num)) |
---|
83 | n/a | w(' #endif') |
---|
84 | n/a | w(' { NULL }') |
---|
85 | n/a | w('};') |
---|
86 | n/a | if not use_stdout: |
---|
87 | n/a | f.close() |
---|