Debian Codegraph @

crt/s2n/codebuild/bin/test_libcrypto_interning.sh

raw symbols SCIP
1 #!/usr/bin/envscip-shell filesystem . . `/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.shscip-shell source . . `codebuild/bin/s2n_setup_env.sh`.
19 source codebuild/bin/jobs.shscip-shell 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=$(echoscip-shell system . . 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 [scip-shell system . . `[`. ! -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 [scip-shell system . . `[`. ! -f $TARGET_LIBCRYPTO_PATH/lib/libcrypto.a ]; then
31 if [scip-shell system . . `[`. "$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 [scip-shell system . . `[`. -x "$(command -v gcc-ar)" ]; then
43 LTO_FLAGS+=" -DCMAKE_AR=$(whichscip-shell system . . which. gcc-ar) -DCMAKE_NM=$(whichscip-shell system . . which. gcc-nm) -DCMAKE_RANLIB=$(whichscip-shell system . . which. gcc-ranlib)"
44 fi
45
46 function failscip-shell shell shell-project 0.0.0 fail().() {
47 echoscip-shell system . . echo. "test failure: $1"
48 exit 1
49 }
50
51 function write_appscip-shell shell shell-project 0.0.0 write_app().() {
52 catscip-shell system . . 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 buildscip-shell shell shell-project 0.0.0 build().() {
65 echoscip-shell system . . echo. "=== BUILDING $1 ==="
66 cmakescip-shell system . . cmake. . -B$1 $COMMON_FLAGS ${@:2}
67 cmakescip-shell system . . cmake. --build $1 -- -j $JOBS
68 }
69
70 function testsscip-shell shell shell-project 0.0.0 tests().() {
71 echoscip-shell system . . echo. "=== TESTING $1 ==="
72 makescip-shell system . . 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 buildscip-shell shell shell-project 0.0.0 build(). build/shared-default -DBUILD_SHARED_LIBS=on -DBUILD_TESTING=on
81 lddscip-shell system . . ldd. ./build/shared-default/lib/libs2n.so | grepscip-shell system . . grep. -q libcrypto || failscip-shell shell shell-project 0.0.0 fail(). "shared-default: libcrypto was not linked"
82
83 # ensure libcrypto interning works with shared libs and no testing
84 buildscip-shell shell shell-project 0.0.0 build(). build/shared -DBUILD_SHARED_LIBS=on -DBUILD_TESTING=off -DS2N_INTERN_LIBCRYPTO=on
85 # s2n should not publicly depend on libcrypto
86 lddscip-shell system . . ldd. ./build/shared/lib/libs2n.so | grepscip-shell system . . grep. -q libcrypto && failscip-shell shell shell-project 0.0.0 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 buildscip-shell shell shell-project 0.0.0 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 lddscip-shell system . . ldd. ./build/shared-lto/lib/libs2n.so | grepscip-shell system . . grep. -q libcrypto && failscip-shell shell shell-project 0.0.0 fail(). "shared-lto: libcrypto was not interned"
93
94 # ensure libcrypto interning works with shared libs and testing
95 buildscip-shell shell shell-project 0.0.0 build(). build/shared-testing -DBUILD_SHARED_LIBS=on -DBUILD_TESTING=on -DS2N_INTERN_LIBCRYPTO=on
96 # s2n should not publicly depend on libcrypto
97 lddscip-shell system . . ldd. ./build/shared-testing/lib/libs2n.so | grepscip-shell system . . grep. -q libcrypto && failscip-shell shell shell-project 0.0.0 fail(). "shared-testing: libcrypto was not interned"
98 # run the tests and make sure they all pass with the prefixed version
99 testsscip-shell shell shell-project 0.0.0 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 testsscip-shell shell shell-project 0.0.0 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_appscip-shell shell shell-project 0.0.0 write_app(). build/$build/app.c
107
108 for target in $OPENSSL_1_0 $TARGET_LIBCRYPTO_PATH; do
109 echoscip-shell system . . echo. "testing $build linking with $target"
110 mkdirscip-shell system . . mkdir. -p $target/bin
111 ccscip-shell system . . 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 buildscip-shell shell shell-project 0.0.0 build(). build/static -DBUILD_SHARED_LIBS=off -DBUILD_TESTING=on -DS2N_INTERN_LIBCRYPTO=on
124 testsscip-shell shell shell-project 0.0.0 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_appscip-shell shell shell-project 0.0.0 write_app(). build/$build/app.c
132
133 for target in $OPENSSL_1_0 $TARGET_LIBCRYPTO_PATH; do
134 echoscip-shell system . . echo. "testing $build linking with $target"
135 mkdirscip-shell system . . mkdir. -p $target/bin
136 ccscip-shell system . . 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 nmscip-shell system . . nm. $target/bin/test-app | grepscip-shell system . . grep. -q 'T s2n$BN_CTX_new' || failscip-shell shell shell-project 0.0.0 fail(). "$target: libcrypto symbols were not prefixed"
138 nmscip-shell system . . nm. $target/bin/test-app | grepscip-shell system . . grep. -q 'T BN_CTX_new' || failscip-shell shell shell-project 0.0.0 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_testscip-shell shell shell-project 0.0.0 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/nullscip-shell filesystem . . `/dev/null`. &
152 local SERVER_PID=$!
153
154 # Wait for the server to start up before connecting
155 sleepscip-shell system . . sleep. 5s
156
157 LD_PRELOAD=$OPENSSL_1_0/lib/libcrypto.so ./build/$TARGET/bin/s2nc -i -c default_tls13 localhost 4433 | teescip-shell system . . tee. build/client.log
158 killscip-shell system . . kill. $SERVER_PID &> /dev/nullscip-shell filesystem . . `/dev/null`. || truescip-shell system . . true.
159
160 # ensure a TLS 1.3 session was negotiated
161 echoscip-shell system . . echo. "checking for TLS 1.3"
162 grepscip-shell system . . 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 echoscip-shell system . . echo. "Running test: attempt TLS1.3 handshake without interning"
167 run_connection_testscip-shell shell shell-project 0.0.0 run_connection_test(). shared-default && failscip-shell shell shell-project 0.0.0 fail(). "TLS 1.3 handshake was expected to fail"
168 echoscip-shell system . . echo. "TLS1.3 handshake failed as expected"
169 echoscip-shell system . . echo. ""
170
171 # with interning, the connection should succeed even though we've linked the wrong version of libcrypto
172 echoscip-shell system . . echo. "Running test: attempt TLS1.3 handshake with interning"
173 run_connection_testscip-shell shell shell-project 0.0.0 run_connection_test(). shared-testing || failscip-shell shell shell-project 0.0.0 fail(). "TLS 1.3 handshake was expected to succeed"
174 echoscip-shell system . . echo. "TLS1.3 handshake succeeded as expected"
175
176 echoscip-shell system . . echo. "SUCCESS!"