xref: /aosp_15_r20/external/tink/kokoro/testutils/install_openssl.sh (revision e7b1675dde1b92d52ec075b0a92829627f2c52a5)
1#!/bin/bash
2# Copyright 2022 Google LLC
3#
4# Licensed under the Apache License, Version 2.0 (the "License");
5# you may not use this file except in compliance with the License.
6# You may obtain a copy of the License at
7#
8#      http://www.apache.org/licenses/LICENSE-2.0
9#
10# Unless required by applicable law or agreed to in writing, software
11# distributed under the License is distributed on an "AS IS" BASIS,
12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13# See the License for the specific language governing permissions and
14# limitations under the License.
15################################################################################
16
17# This scripts installs OpenSSL of a given version and SHA256. If the version is
18# not specified, DEFAULT_OPENSSL_VERSION is used; similarly the digest is by
19# default DEFAULT_OPENSSL_SHA256.
20#
21# NOTEs:
22#   * If not running on Kokoro, this script will do nothing.
23#   * This script MUST be sourced to update the environment of the calling
24#     script.
25#   * If a custom version is passed, the corresponding digest should be passed
26#     too.
27#
28# Usage:
29#   source ./kokoro/testutils/install_openssl.sh [version] [sha256]
30
31readonly DEFAULT_OPENSSL_VERSION="1.1.1l"
32readonly DEFAULT_OPENSSL_SHA256="0b7a3e5e59c34827fe0c3a74b7ec8baef302b98fa80088d7f9153aa16fa76bd1"
33readonly PLATFORM="$(uname | tr '[:upper:]' '[:lower:]')"
34
35install_openssl() {
36  local openssl_version="${1:-${DEFAULT_OPENSSL_VERSION}}"
37  local openssl_sha256="${2:-${DEFAULT_OPENSSL_SHA256}}"
38
39  local openssl_name="openssl-${openssl_version}"
40  local openssl_archive="${openssl_name}.tar.gz"
41  local openssl_url="https://www.openssl.org/source/${openssl_archive}"
42
43  local openssl_tmpdir="$(mktemp -dt tink-openssl-${openssl_version}.XXXXXX)"
44  echo "Building and installing OpensSSL ${openssl_version} to \
45${openssl_tmpdir}..."
46  (
47    cd "${openssl_tmpdir}"
48    curl -OLsS "${openssl_url}"
49    echo "${openssl_sha256} ${openssl_archive}" | sha256sum -c
50
51    tar xzf "${openssl_archive}"
52    cd "${openssl_name}"
53    ./config --prefix="${openssl_tmpdir}" --openssldir="${openssl_tmpdir}"
54    if [[ "${PLATFORM}" == "darwin" ]]; then
55      make -j "$(sysctl -n hw.ncpu)" > /dev/null
56    else
57      make -j "$(nproc)" > /dev/null
58    fi
59    make install_sw > /dev/null
60  )
61  echo "Done"
62  export OPENSSL_ROOT_DIR="${openssl_tmpdir}"
63  export PATH="${openssl_tmpdir}/bin:${PATH}"
64}
65
66if [[ -n "${KOKORO_ARTIFACTS_DIR:-}" ]]; then
67  # If specifying the version, users must also specify the digest.
68  if (( "$#" == 1 )); then
69    echo \
70      "The SHA256 digest must be provided too when specifying OpenSSL's version" \
71      >&2
72    exit 1
73  fi
74  install_openssl "$@"
75fi
76