| 1 |
#! /usr/bin/env python3 |
| 2 |
|
| 3 |
""" |
| 4 |
This script should be called *manually* when we want to upgrade SSLError |
| 5 |
`library` and `reason` mnemonics to a more recent OpenSSL version. |
| 6 |
|
| 7 |
It takes two arguments: |
| •8 |
- the path to the OpenSSL git checkout |
| 9 |
- the path to the header file to be generated Modules/_ssl_data_{version}.h |
| 10 |
- error codes are version specific |
| •11 |
|
| 12 |
The OpenSSL git checkout should be at a specific tag, using commands like: |
| 13 |
git tag --list 'openssl-*' |
| 14 |
git switch --detach openssl-3.4.0 |
| 15 |
|
| 16 |
|
| 17 |
After generating the definitions, compare the result with newest pre-existing file. |
| 18 |
You can use a command like: |
| 19 |
|
| 20 |
git diff --no-index Modules/_ssl_data_31.h Modules/_ssl_data_34.h |
| 21 |
|
| 22 |
- If the new version *only* adds new definitions, remove the pre-existing file |
| 23 |
and adjust the #include in _ssl.c to point to the new version. |
| 24 |
- If the new version removes or renumbers some definitions, keep both files and |
| 25 |
add a new #include in _ssl.c. |
| 26 |
|
| 27 |
A newly supported OpenSSL version should also be added to: |
| 28 |
- Tools/ssl/multissltests.py |
| 29 |
- .github/workflows/build.yml |
| 30 |
""" |
| 31 |
|
| 32 |
import argparse |
| 33 |
import datetime |
| 34 |
import operator |
| 35 |
import os |
| 36 |
import re |
| 37 |
import sys |
| •38 |
import subprocess |
| 39 |
|
| 40 |
|
| 41 |
parser = argparse.ArgumentParser( |
| 42 |
description="Generate ssl_data.h from OpenSSL sources" |
| 43 |
) |
| 44 |
parser.add_argument("srcdir", help="OpenSSL source directory") |
| 45 |
parser.add_argument( |
| 46 |
"output", nargs="?", default=None |
| 47 |
) |
| 48 |
|
| 49 |
|
| 50 |
def _file_search(fname, pat): |
| 51 |
with open(fname, encoding="utf-8") as f: |
| 52 |
for line in f: |
| 53 |
match = pat.search(line) |
| 54 |
if match is not None: |
| 55 |
yield match |
| 56 |
|
| 57 |
|
| 58 |
def parse_err_h(args): |
| 59 |
"""Parse err codes, e.g. ERR_LIB_X509: 11""" |
| 60 |
pat = re.compile(r"#\s*define\W+ERR_LIB_(\w+)\s+(\d+)") |
| 61 |
lib2errnum = {} |
| 62 |
for match in _file_search(args.err_h, pat): |
| 63 |
libname, num = match.groups() |
| 64 |
lib2errnum[libname] = int(num) |
| 65 |
|
| 66 |
return lib2errnum |
| 67 |
|
| 68 |
|
| 69 |
def parse_openssl_error_text(args): |
| 70 |
"""Parse error reasons, X509_R_AKID_MISMATCH""" |
| 71 |
# ignore backslash line continuation for now |
| 72 |
pat = re.compile(r"^((\w+?)_R_(\w+)):(\d+):") |
| 73 |
for match in _file_search(args.errtxt, pat): |
| 74 |
reason, libname, errname, num = match.groups() |
| 75 |
if "_F_" in reason: |
| 76 |
# ignore function codes |
| 77 |
continue |
| 78 |
num = int(num) |
| 79 |
yield reason, libname, errname, num |
| 80 |
|
| 81 |
|
| 82 |
def parse_extra_reasons(args): |
| 83 |
"""Parse extra reasons from openssl.ec""" |
| 84 |
pat = re.compile(r"^R\s+((\w+)_R_(\w+))\s+(\d+)") |
| 85 |
for match in _file_search(args.errcodes, pat): |
| 86 |
reason, libname, errname, num = match.groups() |
| 87 |
num = int(num) |
| 88 |
yield reason, libname, errname, num |
| 89 |
|
| 90 |
|
| 91 |
def gen_library_codes(args): |
| 92 |
"""Generate table short libname to numeric code""" |
| 93 |
yield "static struct py_ssl_library_code library_codes[] = {" |
| 94 |
for libname in sorted(args.lib2errnum): |
| 95 |
yield f"#ifdef ERR_LIB_{libname}" |
| 96 |
yield f' {{"{libname}", ERR_LIB_{libname}}},' |
| 97 |
yield "#endif" |
| 98 |
yield " { NULL }" |
| 99 |
yield "};" |
| 100 |
yield "" |
| 101 |
|
| 102 |
|
| 103 |
def gen_error_codes(args): |
| 104 |
"""Generate error code table for error reasons""" |
| 105 |
yield "static struct py_ssl_error_code error_codes[] = {" |
| 106 |
for reason, libname, errname, num in args.reasons: |
| 107 |
yield f" #ifdef {reason}" |
| 108 |
yield f' {{"{errname}", ERR_LIB_{libname}, {reason}}},' |
| 109 |
yield " #else" |
| 110 |
yield f' {{"{errname}", {args.lib2errnum[libname]}, {num}}},' |
| 111 |
yield " #endif" |
| 112 |
|
| 113 |
yield " { NULL }" |
| 114 |
yield "};" |
| 115 |
yield "" |
| 116 |
|
| 117 |
|
| 118 |
def main(): |
| 119 |
args = parser.parse_args() |
| 120 |
|
| 121 |
args.err_h = os.path.join(args.srcdir, "include", "openssl", "err.h") |
| 122 |
if not os.path.isfile(args.err_h): |
| 123 |
# Fall back to infile for OpenSSL 3.0.0 |
| 124 |
args.err_h += ".in" |
| 125 |
args.errcodes = os.path.join(args.srcdir, "crypto", "err", "openssl.ec") |
| 126 |
args.errtxt = os.path.join(args.srcdir, "crypto", "err", "openssl.txt") |
| 127 |
|
| 128 |
if not os.path.isfile(args.errtxt): |
| 129 |
parser.error(f"File {args.errtxt} not found in srcdir\n.") |
| 130 |
|
| 131 |
# {X509: 11, ...} |
| 132 |
args.lib2errnum = parse_err_h(args) |
| 133 |
|
| 134 |
# [('X509_R_AKID_MISMATCH', 'X509', 'AKID_MISMATCH', 110), ...] |
| 135 |
reasons = [] |
| 136 |
reasons.extend(parse_openssl_error_text(args)) |
| 137 |
reasons.extend(parse_extra_reasons(args)) |
| 138 |
# sort by libname, numeric error code |
| 139 |
args.reasons = sorted(reasons, key=operator.itemgetter(0, 3)) |
| 140 |
|
| •141 |
git_describe = subprocess.run( |
| 142 |
['git', 'describe', '--long', '--dirty'], |
| 143 |
cwd=args.srcdir, |
| 144 |
capture_output=True, |
| 145 |
encoding='utf-8', |
| 146 |
check=True, |
| 147 |
) |
| 148 |
lines = [ |
| •149 |
"/* File generated by Tools/ssl/make_ssl_data.py */", |
| 150 |
f"/* Generated on {datetime.datetime.now(datetime.UTC).isoformat()} */", |
| 151 |
f"/* Generated from Git commit {git_describe.stdout.strip()} */", |
| 152 |
] |
| 153 |
lines.extend(gen_library_codes(args)) |
| 154 |
lines.append("") |
| 155 |
lines.extend(gen_error_codes(args)) |
| 156 |
|
| 157 |
if args.output is None: |
| 158 |
for line in lines: |
| 159 |
print(line) |
| 160 |
else: |
| 161 |
with open(args.output, 'w') as output: |
| 162 |
for line in lines: |
| 163 |
print(line, file=output) |
| 164 |
|
| 165 |
|
| 166 |
if __name__ == "__main__": |
| 167 |
main() |