| 1 | #!/usr/bin/env bash |
| 2 | # Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 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 | # A copy of the License is located at |
| 7 | # |
| 8 | # http://aws.amazon.com/apache2.0 |
| 9 | # |
| 10 | # or in the "license" file accompanying this file. This file is distributed |
| 11 | # on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either |
| 12 | # express or implied. See the License for the specific language governing |
| 13 | # permissions and limitations under the License. |
| 14 | # |
| 15 | |
| 16 | set -e |
| 17 | |
| 18 | source codebuild/bin/s2n_setup_env.sh |
| 19 | source codebuild/bin/jobs.sh |
| 20 | |
| 21 | # build 2 different version of libcrypto to make it easy to break the application if |
| 22 | # interning doesn't work as expected |
| 23 | WHICH_LIBCRYPTO=$(echo "${S2N_LIBCRYPTO:-"openssl-1.1.1"}") |
| 24 | TARGET_LIBCRYPTO="${WHICH_LIBCRYPTO//[-.]/_}" |
| 25 | TARGET_LIBCRYPTO_PATH="${TEST_DEPS_DIR}/${WHICH_LIBCRYPTO}" |
| 26 | OPENSSL_1_0="$OPENSSL_1_0_2_INSTALL_DIR" |
| 27 | if [ ! -f $OPENSSL_1_0/lib/libcrypto.a ]; then |
| 28 | ./codebuild/bin/install_openssl_1_0_2.sh $OPENSSL_1_0/src $OPENSSL_1_0 linux |
| 29 | fi |
| 30 | if [ ! -f $TARGET_LIBCRYPTO_PATH/lib/libcrypto.a ]; then |
| 31 | if [ "$TARGET_LIBCRYPTO" == "awslc" ]; then |
| 32 | ./codebuild/bin/install_${TARGET_LIBCRYPTO}.sh $TARGET_LIBCRYPTO_PATH/src $TARGET_LIBCRYPTO_PATH 0 |
| 33 | else |
| 34 | ./codebuild/bin/install_${TARGET_LIBCRYPTO}.sh $TARGET_LIBCRYPTO_PATH/src $TARGET_LIBCRYPTO_PATH linux |
| 35 | fi |
| 36 | fi |
| 37 | |
| 38 | COMMON_FLAGS="-DCMAKE_PREFIX_PATH=$TARGET_LIBCRYPTO_PATH -DCMAKE_BUILD_TYPE=RelWithDebInfo" |
| 39 | LTO_FLAGS="-DS2N_LTO=on" |
| 40 | |
| 41 | # use LTO-aware commands if possible |
| 42 | if [ -x "$(command -v gcc-ar)" ]; then |
| 43 | LTO_FLAGS+=" -DCMAKE_AR=$(which gcc-ar) -DCMAKE_NM=$(which gcc-nm) -DCMAKE_RANLIB=$(which gcc-ranlib)" |
| 44 | fi |
| 45 | |
| 46 | function fail() { |
| 47 | echo "test failure: $1" |
| 48 | exit 1 |
| 49 | } |
| 50 | |
| 51 | function write_app() { |
| 52 | cat <<EOF > $1 |
| 53 | #include <s2n.h> |
| 54 | #include <openssl/bn.h> |
| 55 | |
| 56 | int main() { |
| 57 | s2n_init(); |
| 58 | BN_CTX_new(); |
| 59 | return 0; |
| 60 | } |
| 61 | EOF |
| 62 | } |
| 63 | |
| 64 | function build() { |
| 65 | echo "=== BUILDING $1 ===" |
| 66 | cmake . -B$1 $COMMON_FLAGS ${@:2} |
| 67 | cmake --build $1 -- -j $JOBS |
| 68 | } |
| 69 | |
| 70 | function tests() { |
| 71 | echo "=== TESTING $1 ===" |
| 72 | make -C $1 test ARGS="-j $JOBS -L unit" |
| 73 | } |
| 74 | |
| 75 | ################## |
| 76 | # Dynamic builds # |
| 77 | ################## |
| 78 | |
| 79 | # build a default version to test what happens without interning |
| 80 | build build/shared-default -DBUILD_SHARED_LIBS=on -DBUILD_TESTING=on |
| 81 | ldd ./build/shared-default/lib/libs2n.so | grep -q libcrypto || fail "shared-default: libcrypto was not linked" |
| 82 | |
| 83 | # ensure libcrypto interning works with shared libs and no testing |
| 84 | build build/shared -DBUILD_SHARED_LIBS=on -DBUILD_TESTING=off -DS2N_INTERN_LIBCRYPTO=on |
| 85 | # s2n should not publicly depend on libcrypto |
| 86 | ldd ./build/shared/lib/libs2n.so | grep -q libcrypto && fail "shared: libcrypto was not interned" |
| 87 | |
| 88 | # ensure libcrypto interning works with shared libs, LTO and no testing |
| 89 | # NOTE: interning+LTO+testing doesn't currently work |
| 90 | build build/shared-lto -DBUILD_SHARED_LIBS=on -DBUILD_TESTING=off -DS2N_INTERN_LIBCRYPTO=on $LTO_FLAGS |
| 91 | # s2n should not publicly depend on libcrypto |
| 92 | ldd ./build/shared-lto/lib/libs2n.so | grep -q libcrypto && fail "shared-lto: libcrypto was not interned" |
| 93 | |
| 94 | # ensure libcrypto interning works with shared libs and testing |
| 95 | build build/shared-testing -DBUILD_SHARED_LIBS=on -DBUILD_TESTING=on -DS2N_INTERN_LIBCRYPTO=on |
| 96 | # s2n should not publicly depend on libcrypto |
| 97 | ldd ./build/shared-testing/lib/libs2n.so | grep -q libcrypto && fail "shared-testing: libcrypto was not interned" |
| 98 | # run the tests and make sure they all pass with the prefixed version |
| 99 | tests build/shared-testing |
| 100 | # load the wrong version of libcrypto and the tests should still pass |
| 101 | LD_PRELOAD=$OPENSSL_1_0/lib/libcrypto.so tests build/shared-testing |
| 102 | |
| 103 | # ensure the small app will compile with both versions of openssl without any linking issues |
| 104 | for build in shared shared-lto; do |
| 105 | # create a small app that links against both s2n and libcrypto |
| 106 | write_app build/$build/app.c |
| 107 | |
| 108 | for target in $OPENSSL_1_0 $TARGET_LIBCRYPTO_PATH; do |
| 109 | echo "testing $build linking with $target" |
| 110 | mkdir -p $target/bin |
| 111 | cc -fPIE -Iapi -I$target/include build/$build/app.c build/$build/lib/libs2n.so $target/lib/libcrypto.a -lpthread -ldl -o $target/bin/test-app |
| 112 | # make sure the app doesn't crash |
| 113 | LD_LIBRARY_PATH="build/$build/lib:$target/lib:$LD_LIBRARY_PATH" $target/bin/test-app |
| 114 | done |
| 115 | done |
| 116 | |
| 117 | ################## |
| 118 | # Static builds # |
| 119 | ################## |
| 120 | |
| 121 | # ensure libcrypto interning works with static libs |
| 122 | # NOTE: static builds don't vary based on testing being enabled |
| 123 | build build/static -DBUILD_SHARED_LIBS=off -DBUILD_TESTING=on -DS2N_INTERN_LIBCRYPTO=on |
| 124 | tests build/static |
| 125 | |
| 126 | # TODO figure out how to get static-lto+interning builds working |
| 127 | |
| 128 | # ensure the small app will compile with both versions of openssl without any linking issues |
| 129 | for build in static; do |
| 130 | # create a small app that links against both s2n and libcrypto |
| 131 | write_app build/$build/app.c |
| 132 | |
| 133 | for target in $OPENSSL_1_0 $TARGET_LIBCRYPTO_PATH; do |
| 134 | echo "testing $build linking with $target" |
| 135 | mkdir -p $target/bin |
| 136 | cc -fPIE -Iapi -I$target/include build/$build/app.c build/$build/lib/libs2n.a $target/lib/libcrypto.a -lpthread -ldl -o $target/bin/test-app |
| 137 | nm $target/bin/test-app | grep -q 'T s2n$BN_CTX_new' || fail "$target: libcrypto symbols were not prefixed" |
| 138 | nm $target/bin/test-app | grep -q 'T BN_CTX_new' || fail "$target: libcrypto was not linked in application" |
| 139 | # make sure the app doesn't crash |
| 140 | $target/bin/test-app |
| 141 | done |
| 142 | done |
| 143 | |
| 144 | ################## |
| 145 | # Runtime tests # |
| 146 | ################## |
| 147 | |
| 148 | run_connection_test() { |
| 149 | local TARGET="$1" |
| 150 | |
| 151 | LD_PRELOAD=$OPENSSL_1_0/lib/libcrypto.so ./build/$TARGET/bin/s2nd -c default_tls13 localhost 4433 &> /dev/null & |
| 152 | local SERVER_PID=$! |
| 153 | |
| 154 | # Wait for the server to start up before connecting |
| 155 | sleep 5s |
| 156 | |
| 157 | LD_PRELOAD=$OPENSSL_1_0/lib/libcrypto.so ./build/$TARGET/bin/s2nc -i -c default_tls13 localhost 4433 | tee build/client.log |
| 158 | kill $SERVER_PID &> /dev/null || true |
| 159 | |
| 160 | # ensure a TLS 1.3 session was negotiated |
| 161 | echo "checking for TLS 1.3" |
| 162 | grep -q "Actual protocol version: 34" build/client.log |
| 163 | } |
| 164 | |
| 165 | # without interning, the connection should fail when linking the wrong version of libcrypto |
| 166 | echo "Running test: attempt TLS1.3 handshake without interning" |
| 167 | run_connection_test shared-default && fail "TLS 1.3 handshake was expected to fail" |
| 168 | echo "TLS1.3 handshake failed as expected" |
| 169 | echo "" |
| 170 | |
| 171 | # with interning, the connection should succeed even though we've linked the wrong version of libcrypto |
| 172 | echo "Running test: attempt TLS1.3 handshake with interning" |
| 173 | run_connection_test shared-testing || fail "TLS 1.3 handshake was expected to succeed" |
| 174 | echo "TLS1.3 handshake succeeded as expected" |
| 175 | |
| 176 | echo "SUCCESS!" |