xref: /curl/tests/http/test_17_ssl_use.py (revision 701813b2)
1#!/usr/bin/env python3
2# -*- coding: utf-8 -*-
3#***************************************************************************
4#                                  _   _ ____  _
5#  Project                     ___| | | |  _ \| |
6#                             / __| | | | |_) | |
7#                            | (__| |_| |  _ <| |___
8#                             \___|\___/|_| \_\_____|
9#
10# Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
11#
12# This software is licensed as described in the file COPYING, which
13# you should have received as part of this distribution. The terms
14# are also available at https://curl.se/docs/copyright.html.
15#
16# You may opt to use, copy, modify, merge, publish, distribute and/or sell
17# copies of the Software, and permit persons to whom the Software is
18# furnished to do so, under the terms of the COPYING file.
19#
20# This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
21# KIND, either express or implied.
22#
23# SPDX-License-Identifier: curl
24#
25###########################################################################
26#
27import json
28import logging
29import os
30import re
31import pytest
32
33from testenv import Env, CurlClient, LocalClient
34
35
36log = logging.getLogger(__name__)
37
38
39class TestSSLUse:
40
41    @pytest.fixture(autouse=True, scope='class')
42    def _class_scope(self, env, httpd, nghttpx):
43        env.make_data_file(indir=httpd.docs_dir, fname="data-10k", fsize=10*1024)
44        if env.have_h3():
45            nghttpx.start_if_needed()
46
47    @pytest.fixture(autouse=True, scope='function')
48    def _function_scope(self, request, env, httpd):
49        httpd.clear_extra_configs()
50        if 'httpd' not in request.node._fixtureinfo.argnames:
51            httpd.reload_if_config_changed()
52
53    def test_17_01_sslinfo_plain(self, env: Env, nghttpx, repeat):
54        proto = 'http/1.1'
55        curl = CurlClient(env=env)
56        url = f'https://{env.authority_for(env.domain1, proto)}/curltest/sslinfo'
57        r = curl.http_get(url=url, alpn_proto=proto)
58        assert r.json['HTTPS'] == 'on', f'{r.json}'
59        assert 'SSL_SESSION_ID' in r.json, f'{r.json}'
60        assert 'SSL_SESSION_RESUMED' in r.json, f'{r.json}'
61        assert r.json['SSL_SESSION_RESUMED'] == 'Initial', f'{r.json}'
62
63    @pytest.mark.parametrize("tls_max", ['1.2', '1.3'])
64    def test_17_02_sslinfo_reconnect(self, env: Env, tls_max):
65        proto = 'http/1.1'
66        count = 3
67        exp_resumed = 'Resumed'
68        xargs = ['--sessionid', '--tls-max', tls_max, f'--tlsv{tls_max}']
69        if env.curl_uses_lib('libressl'):
70            if tls_max == '1.3':
71                exp_resumed = 'Initial'  # 1.2 works in LibreSSL, but 1.3 does not, TODO
72        if env.curl_uses_lib('rustls-ffi'):
73            exp_resumed = 'Initial'  # Rustls does not support sessions, TODO
74        if env.curl_uses_lib('bearssl') and tls_max == '1.3':
75            pytest.skip('BearSSL does not support TLSv1.3')
76        if env.curl_uses_lib('mbedtls') and tls_max == '1.3' and \
77           not env.curl_lib_version_at_least('mbedtls', '3.6.0'):
78            pytest.skip('mbedtls TLSv1.3 session resume not working in 3.6.0')
79
80        run_env = os.environ.copy()
81        run_env['CURL_DEBUG'] = 'ssl'
82        curl = CurlClient(env=env, run_env=run_env)
83        # tell the server to close the connection after each request
84        urln = f'https://{env.authority_for(env.domain1, proto)}/curltest/sslinfo?'\
85               f'id=[0-{count-1}]&close'
86        r = curl.http_download(urls=[urln], alpn_proto=proto, with_stats=True,
87                               extra_args=xargs)
88        r.check_response(count=count, http_status=200)
89        # should have used one connection for each request, sessions after
90        # first should have been resumed
91        assert r.total_connects == count, r.dump_logs()
92        for i in range(count):
93            dfile = curl.download_file(i)
94            assert os.path.exists(dfile)
95            with open(dfile) as f:
96                djson = json.load(f)
97            assert djson['HTTPS'] == 'on', f'{i}: {djson}'
98            if i == 0:
99                assert djson['SSL_SESSION_RESUMED'] == 'Initial', f'{i}: {djson}\n{r.dump_logs()}'
100            else:
101                assert djson['SSL_SESSION_RESUMED'] == exp_resumed, f'{i}: {djson}\n{r.dump_logs()}'
102
103    # use host name with trailing dot, verify handshake
104    @pytest.mark.parametrize("proto", ['http/1.1', 'h2', 'h3'])
105    def test_17_03_trailing_dot(self, env: Env, proto):
106        if proto == 'h3' and not env.have_h3():
107            pytest.skip("h3 not supported")
108        curl = CurlClient(env=env)
109        domain = f'{env.domain1}.'
110        url = f'https://{env.authority_for(domain, proto)}/curltest/sslinfo'
111        r = curl.http_get(url=url, alpn_proto=proto)
112        assert r.exit_code == 0, f'{r}'
113        assert r.json, f'{r}'
114        if proto != 'h3':  # we proxy h3
115            # the SNI the server received is without trailing dot
116            assert r.json['SSL_TLS_SNI'] == env.domain1, f'{r.json}'
117
118    # use host name with double trailing dot, verify handshake
119    @pytest.mark.parametrize("proto", ['http/1.1', 'h2', 'h3'])
120    def test_17_04_double_dot(self, env: Env, proto):
121        if proto == 'h3' and not env.have_h3():
122            pytest.skip("h3 not supported")
123        curl = CurlClient(env=env)
124        domain = f'{env.domain1}..'
125        url = f'https://{env.authority_for(domain, proto)}/curltest/sslinfo'
126        r = curl.http_get(url=url, alpn_proto=proto, extra_args=[
127            '-H', f'Host: {env.domain1}',
128        ])
129        if r.exit_code == 0:
130            assert r.json, f'{r.stdout}'
131            # the SNI the server received is without trailing dot
132            if proto != 'h3':  # we proxy h3
133                assert r.json['SSL_TLS_SNI'] == env.domain1, f'{r.json}'
134            assert False, f'should not have succeeded: {r.json}'
135        # 7 - Rustls rejects a servername with .. during setup
136        # 35 - LibreSSL rejects setting an SNI name with trailing dot
137        # 60 - peer name matching failed against certificate
138        assert r.exit_code in [7, 35, 60], f'{r}'
139
140    # use ip address for connect
141    @pytest.mark.parametrize("proto", ['http/1.1', 'h2', 'h3'])
142    def test_17_05_ip_addr(self, env: Env, proto):
143        if env.curl_uses_lib('bearssl'):
144            pytest.skip("BearSSL does not support cert verification with IP addresses")
145        if env.curl_uses_lib('mbedtls'):
146            pytest.skip("mbedTLS does use IP addresses in SNI")
147        if proto == 'h3' and not env.have_h3():
148            pytest.skip("h3 not supported")
149        curl = CurlClient(env=env)
150        domain = '127.0.0.1'
151        url = f'https://{env.authority_for(domain, proto)}/curltest/sslinfo'
152        r = curl.http_get(url=url, alpn_proto=proto)
153        assert r.exit_code == 0, f'{r}'
154        assert r.json, f'{r}'
155        if proto != 'h3':  # we proxy h3
156            # the SNI should not have been used
157            assert 'SSL_TLS_SNI' not in r.json, f'{r.json}'
158
159    # use localhost for connect
160    @pytest.mark.parametrize("proto", ['http/1.1', 'h2', 'h3'])
161    def test_17_06_localhost(self, env: Env, proto):
162        if proto == 'h3' and not env.have_h3():
163            pytest.skip("h3 not supported")
164        curl = CurlClient(env=env)
165        domain = 'localhost'
166        url = f'https://{env.authority_for(domain, proto)}/curltest/sslinfo'
167        r = curl.http_get(url=url, alpn_proto=proto)
168        assert r.exit_code == 0, f'{r}'
169        assert r.json, f'{r}'
170        if proto != 'h3':  # we proxy h3
171            assert r.json['SSL_TLS_SNI'] == domain, f'{r.json}'
172
173    @staticmethod
174    def gen_test_17_07_list():
175        tls13_tests = [
176            [None, True],
177            [['TLS_AES_128_GCM_SHA256'], True],
178            [['TLS_AES_256_GCM_SHA384'], False],
179            [['TLS_CHACHA20_POLY1305_SHA256'], True],
180            [['TLS_AES_256_GCM_SHA384',
181              'TLS_CHACHA20_POLY1305_SHA256'], True],
182        ]
183        tls12_tests = [
184            [None, True],
185            [['ECDHE-ECDSA-AES128-GCM-SHA256', 'ECDHE-RSA-AES128-GCM-SHA256'], True],
186            [['ECDHE-ECDSA-AES256-GCM-SHA384', 'ECDHE-RSA-AES256-GCM-SHA384'], False],
187            [['ECDHE-ECDSA-CHACHA20-POLY1305', 'ECDHE-RSA-CHACHA20-POLY1305'], True],
188            [['ECDHE-ECDSA-AES256-GCM-SHA384', 'ECDHE-RSA-AES256-GCM-SHA384',
189              'ECDHE-ECDSA-CHACHA20-POLY1305', 'ECDHE-RSA-CHACHA20-POLY1305'], True],
190        ]
191        ret = []
192        for tls_proto in ['TLSv1.3 +TLSv1.2', 'TLSv1.3', 'TLSv1.2']:
193            for [ciphers13, succeed13] in tls13_tests:
194                for [ciphers12, succeed12] in tls12_tests:
195                    ret.append([tls_proto, ciphers13, ciphers12, succeed13, succeed12])
196        return ret
197
198    @pytest.mark.parametrize("tls_proto, ciphers13, ciphers12, succeed13, succeed12", gen_test_17_07_list())
199    def test_17_07_ssl_ciphers(self, env: Env, httpd, tls_proto, ciphers13, ciphers12, succeed13, succeed12):
200        # to test setting cipher suites, the AES 256 ciphers are disabled in the test server
201        httpd.set_extra_config('base', [
202            'SSLCipherSuite SSL'
203                ' ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256'
204                ':ECDHE-ECDSA-CHACHA20-POLY1305:ECDHE-RSA-CHACHA20-POLY1305',
205            'SSLCipherSuite TLSv1.3'
206                ' TLS_AES_128_GCM_SHA256:TLS_CHACHA20_POLY1305_SHA256',
207            f'SSLProtocol {tls_proto}'
208        ])
209        httpd.reload_if_config_changed()
210        proto = 'http/1.1'
211        curl = CurlClient(env=env)
212        url = f'https://{env.authority_for(env.domain1, proto)}/curltest/sslinfo'
213        # SSL backend specifics
214        if env.curl_uses_lib('gnutls'):
215            pytest.skip('GnuTLS does not support setting ciphers')
216        elif env.curl_uses_lib('boringssl'):
217            if ciphers13 is not None:
218                pytest.skip('BoringSSL does not support setting TLSv1.3 ciphers')
219        elif env.curl_uses_lib('schannel'):  # not in CI, so untested
220            if ciphers12 is not None:
221                pytest.skip('Schannel does not support setting TLSv1.2 ciphers by name')
222        elif env.curl_uses_lib('bearssl'):
223            if tls_proto == 'TLSv1.3':
224                pytest.skip('BearSSL does not support TLSv1.3')
225            tls_proto = 'TLSv1.2'
226        elif env.curl_uses_lib('mbedtls') and not env.curl_lib_version_at_least('mbedtls', '3.6.0'):
227            if tls_proto == 'TLSv1.3':
228                pytest.skip('mbedTLS < 3.6.0 does not support TLSv1.3')
229        elif env.curl_uses_lib('sectransp'):  # not in CI, so untested
230            if tls_proto == 'TLSv1.3':
231                pytest.skip('Secure Transport does not support TLSv1.3')
232            tls_proto = 'TLSv1.2'
233        # test
234        extra_args = ['--tls13-ciphers', ':'.join(ciphers13)] if ciphers13 else []
235        extra_args += ['--ciphers', ':'.join(ciphers12)] if ciphers12 else []
236        r = curl.http_get(url=url, alpn_proto=proto, extra_args=extra_args)
237        if tls_proto != 'TLSv1.2' and succeed13:
238            assert r.exit_code == 0, r.dump_logs()
239            assert r.json['HTTPS'] == 'on', r.dump_logs()
240            assert r.json['SSL_PROTOCOL'] == 'TLSv1.3', r.dump_logs()
241            assert ciphers13 is None or r.json['SSL_CIPHER'] in ciphers13, r.dump_logs()
242        elif tls_proto == 'TLSv1.2' and succeed12:
243            assert r.exit_code == 0, r.dump_logs()
244            assert r.json['HTTPS'] == 'on', r.dump_logs()
245            assert r.json['SSL_PROTOCOL'] == 'TLSv1.2', r.dump_logs()
246            assert ciphers12 is None or r.json['SSL_CIPHER'] in ciphers12, r.dump_logs()
247        else:
248            assert r.exit_code != 0, r.dump_logs()
249
250    @pytest.mark.parametrize("proto", ['http/1.1', 'h2', 'h3'])
251    def test_17_08_cert_status(self, env: Env, proto):
252        if proto == 'h3' and not env.have_h3():
253            pytest.skip("h3 not supported")
254        if not env.curl_uses_lib('openssl') and \
255            not env.curl_uses_lib('gnutls') and \
256            not env.curl_uses_lib('quictls'):
257            pytest.skip("TLS library does not support --cert-status")
258        curl = CurlClient(env=env)
259        domain = 'localhost'
260        url = f'https://{env.authority_for(domain, proto)}/'
261        r = curl.http_get(url=url, alpn_proto=proto, extra_args=[
262            '--cert-status'
263        ])
264        # CURLE_SSL_INVALIDCERTSTATUS, our certs have no OCSP info
265        assert r.exit_code == 91, f'{r}'
266
267    @staticmethod
268    def gen_test_17_09_list():
269        return [[tls_proto, max_ver, min_ver]
270                for tls_proto in ['TLSv1', 'TLSv1.1', 'TLSv1.2', 'TLSv1.3']
271                for max_ver in range(5)
272                for min_ver in range(-2, 4)]
273
274    @pytest.mark.parametrize("tls_proto, max_ver, min_ver", gen_test_17_09_list())
275    def test_17_09_ssl_min_max(self, env: Env, httpd, tls_proto, max_ver, min_ver):
276        httpd.set_extra_config('base', [
277            f'SSLProtocol {tls_proto}',
278            'SSLCipherSuite ALL:@SECLEVEL=0',
279        ])
280        httpd.reload_if_config_changed()
281        proto = 'http/1.1'
282        run_env = os.environ.copy()
283        if env.curl_uses_lib('gnutls'):
284            # we need to override any default system configuration since
285            # we want to test all protocol versions. Ubuntu (or the GH image)
286            # disable TSL1.0 and TLS1.1 system wide. We do not want.
287            our_config = os.path.join(env.gen_dir, 'gnutls_config')
288            if not os.path.exists(our_config):
289                with open(our_config, 'w') as fd:
290                    fd.write('# empty\n')
291            run_env['GNUTLS_SYSTEM_PRIORITY_FILE'] = our_config
292        curl = CurlClient(env=env, run_env=run_env)
293        url = f'https://{env.authority_for(env.domain1, proto)}/curltest/sslinfo'
294        # SSL backend specifics
295        if env.curl_uses_lib('bearssl'):
296            supported = ['TLSv1', 'TLSv1.1', 'TLSv1.2', None]
297        elif env.curl_uses_lib('sectransp'):  # not in CI, so untested
298            supported = ['TLSv1', 'TLSv1.1', 'TLSv1.2', None]
299        elif env.curl_uses_lib('gnutls'):
300            supported = ['TLSv1', 'TLSv1.1', 'TLSv1.2', 'TLSv1.3']
301        elif env.curl_uses_lib('quiche'):
302            supported = ['TLSv1', 'TLSv1.1', 'TLSv1.2', 'TLSv1.3']
303        else:  # most SSL backends dropped support for TLSv1.0, TLSv1.1
304            supported = [None, None, 'TLSv1.2', 'TLSv1.3']
305        # test
306        extra_args = [[], ['--tlsv1'], ['--tlsv1.0'], ['--tlsv1.1'], ['--tlsv1.2'], ['--tlsv1.3']][min_ver+2] + \
307            [['--tls-max', '1.0'], ['--tls-max', '1.1'], ['--tls-max', '1.2'], ['--tls-max', '1.3'], []][max_ver]
308        extra_args.extend(['--trace-config', 'ssl'])
309        r = curl.http_get(url=url, alpn_proto=proto, extra_args=extra_args)
310        if max_ver >= min_ver and tls_proto in supported[max(0, min_ver):min(max_ver, 3)+1]:
311            assert r.exit_code == 0, f'extra_args={extra_args}\n{r.dump_logs()}'
312            assert r.json['HTTPS'] == 'on', r.dump_logs()
313            assert r.json['SSL_PROTOCOL'] == tls_proto, r.dump_logs()
314        else:
315            assert r.exit_code != 0, f'extra_args={extra_args}\n{r.dump_logs()}'
316
317    def test_17_10_h3_session_reuse(self, env: Env, httpd, nghttpx):
318        if not env.have_h3():
319            pytest.skip("h3 not supported")
320        if not env.curl_uses_lib('quictls') and \
321            not env.curl_uses_lib('gnutls') and \
322            not env.curl_uses_lib('wolfssl'):
323            pytest.skip("QUIC session reuse not implemented")
324        count = 2
325        docname = 'data-10k'
326        url = f'https://localhost:{env.https_port}/{docname}'
327        client = LocalClient(name='hx-download', env=env)
328        if not client.exists():
329            pytest.skip(f'example client not built: {client.name}')
330        r = client.run(args=[
331             '-n', f'{count}',
332             '-f',  # forbid reuse of connections
333             '-r', f'{env.domain1}:{env.port_for("h3")}:127.0.0.1',
334             '-V', 'h3', url
335        ])
336        r.check_exit_code(0)
337        # check that TLS session was reused as expected
338        reused_session = False
339        for line in r.trace_lines:
340            m = re.match(r'\[1-1] \* SSL reusing session.*', line)
341            if m:
342                reused_session = True
343        assert reused_session, f'{r}\n{r.dump_logs()}'
344
345    # use host name server has no certificate for
346    @pytest.mark.parametrize("proto", ['http/1.1', 'h2', 'h3'])
347    def test_17_11_wrong_host(self, env: Env, proto):
348        if proto == 'h3' and not env.have_h3():
349            pytest.skip("h3 not supported")
350        curl = CurlClient(env=env)
351        domain = f'insecure.{env.tld}'
352        url = f'https://{domain}:{env.port_for(proto)}/curltest/sslinfo'
353        r = curl.http_get(url=url, alpn_proto=proto)
354        assert r.exit_code == 60, f'{r}'
355
356    # use host name server has no cert for with --insecure
357    @pytest.mark.parametrize("proto", ['http/1.1', 'h2', 'h3'])
358    def test_17_12_insecure(self, env: Env, proto):
359        if proto == 'h3' and not env.have_h3():
360            pytest.skip("h3 not supported")
361        curl = CurlClient(env=env)
362        domain = f'insecure.{env.tld}'
363        url = f'https://{domain}:{env.port_for(proto)}/curltest/sslinfo'
364        r = curl.http_get(url=url, alpn_proto=proto, extra_args=[
365            '--insecure'
366        ])
367        assert r.exit_code == 0, f'{r}'
368        assert r.json, f'{r}'
369