|
| 1 | +#!/usr/bin/env bash |
| 2 | +set -e |
| 3 | + |
| 4 | +PROJECT_ROOT_DIR=$(pwd) |
| 5 | +OPENSSL_DIR="${PROJECT_ROOT_DIR}/deps/openssl" |
| 6 | +OUTPUT_DIR="${PROJECT_ROOT_DIR}/kern" |
| 7 | + |
| 8 | +if [[ ! -f "go.mod" ]]; then |
| 9 | + echo "Run the script from the project root directory" |
| 10 | + exit 1 |
| 11 | +fi |
| 12 | + |
| 13 | +echo "check file exists: ${OPENSSL_DIR}/.git" |
| 14 | +# skip cloning if the header file of the max supported version is already generated |
| 15 | +if [[ ! -f "${OPENSSL_DIR}/.git" ]]; then |
| 16 | + echo "check directory exists: ${OPENSSL_DIR}" |
| 17 | + # skip cloning if the openssl directory already exists |
| 18 | + if [[ ! -d "${OPENSSL_DIR}" ]]; then |
| 19 | + echo "git clone openssl to ${OPENSSL_DIR}" |
| 20 | + git clone https://github.com/openssl/openssl.git ${OPENSSL_DIR} |
| 21 | + fi |
| 22 | +fi |
| 23 | + |
| 24 | +# openssl 3.3.* 跟 3.2.* 的offset一致,故这里采用 3.2的文件名。 |
| 25 | +function run() { |
| 26 | + git fetch --tags |
| 27 | + cp -f ${PROJECT_ROOT_DIR}/utils/openssl_3_2_0_offset.c ${OPENSSL_DIR}/offset.c |
| 28 | + declare -A sslVerMap=() |
| 29 | + sslVerMap["0"]="0" |
| 30 | + sslVerMap["1"]="1" |
| 31 | + |
| 32 | + # shellcheck disable=SC2068 |
| 33 | + for ver in ${!sslVerMap[@]}; do |
| 34 | + tag="openssl-3.3.${ver}" |
| 35 | + val=${sslVerMap[$ver]} |
| 36 | + header_file="${OUTPUT_DIR}/openssl_3_2_${val}_kern.c" |
| 37 | + header_define="OPENSSL_3_2_$(echo ${val} | tr "[:lower:]" "[:upper:]")_KERN_H" |
| 38 | + |
| 39 | + if [[ -f ${header_file} ]]; then |
| 40 | + echo "Skip ${header_file}" |
| 41 | + continue |
| 42 | + fi |
| 43 | + echo "git checkout ${tag}" |
| 44 | + git checkout ${tag} |
| 45 | + echo "Generating ${header_file}" |
| 46 | + |
| 47 | + |
| 48 | + # ./Configure and make openssl/opensslconf.h |
| 49 | + ./Configure |
| 50 | + make clean |
| 51 | + make build_generated |
| 52 | + |
| 53 | + |
| 54 | + clang -I include/ -I . offset.c -o offset |
| 55 | + |
| 56 | + echo -e "#ifndef ECAPTURE_${header_define}" >${header_file} |
| 57 | + echo -e "#define ECAPTURE_${header_define}\n" >>${header_file} |
| 58 | + ./offset >>${header_file} |
| 59 | + echo -e "#define SSL_ST_VERSION SSL_CONNECTION_ST_VERSION\n" >>${header_file} |
| 60 | + echo -e "#define SSL_ST_WBIO SSL_CONNECTION_ST_WBIO\n" >>${header_file} |
| 61 | + echo -e "#define SSL_ST_RBIO SSL_CONNECTION_ST_RBIO\n" >>${header_file} |
| 62 | + echo -e "\n#include \"openssl.h\"" >>${header_file} |
| 63 | + echo -e "#include \"openssl_masterkey_3.3.h\"" >>${header_file} |
| 64 | + echo -e "\n#endif" >>${header_file} |
| 65 | + |
| 66 | + # clean up |
| 67 | + make clean |
| 68 | + |
| 69 | + done |
| 70 | + |
| 71 | + rm offset.c |
| 72 | +} |
| 73 | + |
| 74 | +# TODO Check if the directory for OpenSSL exists |
| 75 | +pushd ${OPENSSL_DIR} |
| 76 | +(run) |
| 77 | +[[ "$?" != 0 ]] && popd |
| 78 | +popd |
0 commit comments