|
| 1 | +#!/bin/sh |
| 2 | +set -e |
| 3 | +# Shell script to update OpenSSL in the source tree to a specific version |
| 4 | +# Based on https://github.com/nodejs/node/blob/main/doc/contributing/maintaining/maintaining-openssl.md |
| 5 | + |
| 6 | +cleanup() { |
| 7 | + EXIT_CODE=$? |
| 8 | + [ -d "$WORKSPACE" ] && rm -rf "$WORKSPACE" |
| 9 | + exit $EXIT_CODE |
| 10 | +} |
| 11 | + |
| 12 | +download_v1() { |
| 13 | + LATEST_V1_TAG_NAME="$("$NODE" --input-type=module --experimental-fetch <<'EOF' |
| 14 | +const res = await fetch('https://api.github.com/repos/quictls/openssl/git/matching-refs/tags/OpenSSL_1'); |
| 15 | +if (!res.ok) throw new Error(`FetchError: ${res.status} ${res.statusText}`, { cause: res }); |
| 16 | +const releases = await res.json() |
| 17 | +const latest = releases.reverse().find(({ ref }) => ref.includes('quic')); |
| 18 | +if(!latest) throw new Error(`Could not find latest release for v1`); |
| 19 | +console.log(latest.ref.replace('refs/tags/','')); |
| 20 | +EOF |
| 21 | +)" |
| 22 | + |
| 23 | + NEW_VERSION_V1=$(echo "$LATEST_V1_TAG_NAME" | sed 's/OpenSSL_//;s/_/./g;s/-/+/g') |
| 24 | + |
| 25 | + case "$NEW_VERSION_V1" in |
| 26 | + *quic1) NEW_VERSION_V1_NO_RELEASE="${NEW_VERSION_V1%1}" ;; |
| 27 | + *) NEW_VERSION_V1_NO_RELEASE="$NEW_VERSION_V1" ;; |
| 28 | + esac |
| 29 | + |
| 30 | + VERSION_H="$DEPS_DIR/openssl/openssl/include/openssl/opensslv.h" |
| 31 | + CURRENT_VERSION=$(grep "OPENSSL_VERSION_TEXT" "$VERSION_H" | sed -n "s/.*OpenSSL \([^\"]*\).*/\1/p" | cut -d ' ' -f 1) |
| 32 | + |
| 33 | + # This function exit with 0 if new version and current version are the same |
| 34 | + compare_dependency_version "openssl" "$NEW_VERSION_V1_NO_RELEASE" "$CURRENT_VERSION" |
| 35 | + |
| 36 | + echo "Making temporary workspace..." |
| 37 | + WORKSPACE=$(mktemp -d 2> /dev/null || mktemp -d -t 'tmp') |
| 38 | + cd "$WORKSPACE" |
| 39 | + |
| 40 | + echo "Fetching OpenSSL source archive..." |
| 41 | + OPENSSL_TARBALL="openssl.tar.gz" |
| 42 | + curl -sL -o "$OPENSSL_TARBALL" "https://api.github.com/repos/quictls/openssl/tarball/$LATEST_V1_TAG_NAME" |
| 43 | + log_and_verify_sha256sum "openssl" "$OPENSSL_TARBALL" |
| 44 | + gzip -dc "$OPENSSL_TARBALL" | tar xf - |
| 45 | + rm "$OPENSSL_TARBALL" |
| 46 | + |
| 47 | + mv quictls-openssl-* openssl |
| 48 | + |
| 49 | + echo "Replacing existing OpenSSL..." |
| 50 | + rm -rf "$DEPS_DIR/openssl/openssl" |
| 51 | + mv "$WORKSPACE/openssl" "$DEPS_DIR/openssl/" |
| 52 | + |
| 53 | + echo "All done!" |
| 54 | + echo "" |
| 55 | + echo "Please git add openssl, and commit the new version:" |
| 56 | + echo "" |
| 57 | + echo "$ git add -A deps/openssl/openssl" |
| 58 | + echo "$ git add doc/contributing/maintaining/maintaining-dependencies.md" |
| 59 | + echo "$ git commit -m \"deps: upgrade openssl sources to quictls/openssl-$NEW_VERSION_V1\"" |
| 60 | + echo "" |
| 61 | + # The last line of the script should always print the new version, |
| 62 | + # as we need to add it to $GITHUB_ENV variable. |
| 63 | + echo "NEW_VERSION=$NEW_VERSION_V1" |
| 64 | +} |
| 65 | + |
| 66 | +download_v3() { |
| 67 | + LATEST_V3_TAG_NAME="$("$NODE" --input-type=module <<'EOF' |
| 68 | +const res = await fetch('https://api.github.com/repos/quictls/openssl/git/matching-refs/tags/openssl-3.0'); |
| 69 | +if (!res.ok) throw new Error(`FetchError: ${res.status} ${res.statusText}`, { cause: res }); |
| 70 | +const releases = await res.json() |
| 71 | +const latest = releases.findLast(({ ref }) => ref.includes('quic')); |
| 72 | +if(!latest) throw new Error(`Could not find latest release for v3.0`); |
| 73 | +console.log(latest.ref.replace('refs/tags/','')); |
| 74 | +EOF |
| 75 | +)" |
| 76 | + NEW_VERSION_V3=$(echo "$LATEST_V3_TAG_NAME" | sed 's/openssl-//;s/-/+/g') |
| 77 | + |
| 78 | + case "$NEW_VERSION_V3" in |
| 79 | + *quic1) NEW_VERSION_V3_NO_RELEASE="${NEW_VERSION_V3%1}" ;; |
| 80 | + *) NEW_VERSION_V3_NO_RELEASE="$NEW_VERSION_V3" ;; |
| 81 | + esac |
| 82 | + VERSION_H="./deps/openssl/config/archs/linux-x86_64/asm/include/openssl/opensslv.h" |
| 83 | + CURRENT_VERSION=$(grep "OPENSSL_FULL_VERSION_STR" $VERSION_H | sed -n "s/^.*VERSION_STR \"\(.*\)\"/\1/p") |
| 84 | + # This function exit with 0 if new version and current version are the same |
| 85 | + compare_dependency_version "openssl" "$NEW_VERSION_V3_NO_RELEASE" "$CURRENT_VERSION" |
| 86 | + |
| 87 | + echo "Making temporary workspace..." |
| 88 | + |
| 89 | + WORKSPACE=$(mktemp -d 2> /dev/null || mktemp -d -t 'tmp') |
| 90 | + |
| 91 | + cd "$WORKSPACE" |
| 92 | + echo "Fetching OpenSSL source archive..." |
| 93 | + |
| 94 | + OPENSSL_TARBALL="openssl.tar.gz" |
| 95 | + |
| 96 | + curl -sL -o "$OPENSSL_TARBALL" "https://api.github.com/repos/quictls/openssl/tarball/$LATEST_V3_TAG_NAME" |
| 97 | + |
| 98 | + log_and_verify_sha256sum "openssl" "$OPENSSL_TARBALL" |
| 99 | + |
| 100 | + gzip -dc "$OPENSSL_TARBALL" | tar xf - |
| 101 | + |
| 102 | + rm "$OPENSSL_TARBALL" |
| 103 | + mv quictls-openssl-* openssl |
| 104 | + echo "Replacing existing OpenSSL..." |
| 105 | + rm -rf "$DEPS_DIR/openssl/openssl" |
| 106 | + mv "$WORKSPACE/openssl" "$DEPS_DIR/openssl/" |
| 107 | + |
| 108 | + # Update the version number |
| 109 | + update_dependency_version "openssl" "$NEW_VERSION_V3" |
| 110 | + echo "All done!" |
| 111 | + echo "" |
| 112 | + echo "Please git add openssl, and commit the new version:" |
| 113 | + echo "" |
| 114 | + echo "$ git add -A deps/openssl/openssl" |
| 115 | + echo "$ git add doc/contributing/maintaining/maintaining-dependencies.md" |
| 116 | + echo "$ git commit -m \"deps: upgrade openssl sources to quictls/openssl-$NEW_VERSION_V3\"" |
| 117 | + echo "" |
| 118 | + # The last line of the script should always print the new version, |
| 119 | + # as we need to add it to $GITHUB_ENV variable. |
| 120 | + echo "NEW_VERSION=$NEW_VERSION_V3" |
| 121 | +} |
| 122 | + |
| 123 | +regenerate() { |
| 124 | + command -v perl >/dev/null 2>&1 || { echo >&2 "Error: 'Perl' required but not installed."; exit 1; } |
| 125 | + command -v nasm >/dev/null 2>&1 || { echo >&2 "Error: 'nasm' required but not installed."; exit 1; } |
| 126 | + command -v as >/dev/null 2>&1 || { echo >&2 "Error: 'GNU as' required but not installed."; exit 1; } |
| 127 | + perl -e "use Text::Template">/dev/null 2>&1 || { echo >&2 "Error: 'Text::Template' Perl module required but not installed."; exit 1; } |
| 128 | + |
| 129 | + echo "Regenerating platform-dependent files..." |
| 130 | + |
| 131 | + make -C "$DEPS_DIR/openssl/config" clean |
| 132 | + # Needed for compatibility with nasm on 32-bit Windows |
| 133 | + # See https://github.com/nodejs/node/blob/main/doc/contributing/maintaining/maintaining-openssl.md#2-execute-make-in-depsopensslconfig-directory |
| 134 | + sed -i 's/#ifdef/%ifdef/g' "$DEPS_DIR/openssl/openssl/crypto/perlasm/x86asm.pl" |
| 135 | + sed -i 's/#endif/%endif/g' "$DEPS_DIR/openssl/openssl/crypto/perlasm/x86asm.pl" |
| 136 | + make -C "$DEPS_DIR/openssl/config" |
| 137 | + |
| 138 | + echo "All done!" |
| 139 | + echo "" |
| 140 | + echo "Please commit the regenerated files:" |
| 141 | + echo "" |
| 142 | + echo "$ git add -A deps/openssl/config/archs deps/openssl/openssl" |
| 143 | + echo "$ git commit -m \"deps: update archs files for openssl\"" |
| 144 | + echo "" |
| 145 | +} |
| 146 | + |
| 147 | +help() { |
| 148 | + echo "Shell script to update OpenSSL in the source tree to a specific version" |
| 149 | + echo "Sub-commands:" |
| 150 | + printf "%-23s %s\n" "help" "show help menu and commands" |
| 151 | + printf "%-23s %s\n" "download" "download and replace OpenSSL source code with new version" |
| 152 | + printf "%-23s %s\n" "regenerate" "regenerate platform-specific files" |
| 153 | + echo "" |
| 154 | + exit "${1:-0}" |
| 155 | +} |
| 156 | + |
| 157 | +main() { |
| 158 | + if [ ${#} -eq 0 ]; then |
| 159 | + help 0 |
| 160 | + fi |
| 161 | + |
| 162 | + trap cleanup INT TERM EXIT |
| 163 | + |
| 164 | + BASE_DIR=$(cd "$(dirname "$0")/../.." && pwd) |
| 165 | + DEPS_DIR="$BASE_DIR/deps" |
| 166 | + |
| 167 | + [ -z "$NODE" ] && NODE="$BASE_DIR/out/Release/node" |
| 168 | + [ -x "$NODE" ] || NODE=$(command -v node) |
| 169 | + |
| 170 | + # shellcheck disable=SC1091 |
| 171 | + . "$BASE_DIR/tools/dep_updaters/utils.sh" |
| 172 | + |
| 173 | + case ${1} in |
| 174 | + help | regenerate | download_v1 | download_v3 ) |
| 175 | + $1 "${2}" |
| 176 | + ;; |
| 177 | + * ) |
| 178 | + echo "unknown command: $1" |
| 179 | + help 1 |
| 180 | + exit 1 |
| 181 | + ;; |
| 182 | + esac |
| 183 | +} |
| 184 | + |
| 185 | +main "$@" |
0 commit comments