test_17_ssl_use.py 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  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. #
  27. import difflib
  28. import filecmp
  29. import json
  30. import logging
  31. import os
  32. from datetime import timedelta
  33. import pytest
  34. from testenv import Env, CurlClient, LocalClient, ExecResult
  35. log = logging.getLogger(__name__)
  36. class TestSSLUse:
  37. @pytest.fixture(autouse=True, scope='class')
  38. def _class_scope(self, env, httpd, nghttpx):
  39. if env.have_h3():
  40. nghttpx.start_if_needed()
  41. httpd.clear_extra_configs()
  42. httpd.reload()
  43. def test_17_01_sslinfo_plain(self, env: Env, httpd, nghttpx, repeat):
  44. proto = 'http/1.1'
  45. curl = CurlClient(env=env)
  46. url = f'https://{env.authority_for(env.domain1, proto)}/curltest/sslinfo'
  47. r = curl.http_get(url=url, alpn_proto=proto)
  48. assert r.json['HTTPS'] == 'on', f'{r.json}'
  49. assert 'SSL_SESSION_ID' in r.json, f'{r.json}'
  50. assert 'SSL_SESSION_RESUMED' in r.json, f'{r.json}'
  51. assert r.json['SSL_SESSION_RESUMED'] == 'Initial', f'{r.json}'
  52. @pytest.mark.parametrize("tls_max", ['1.2', '1.3'])
  53. def test_17_02_sslinfo_reconnect(self, env: Env, httpd, nghttpx, tls_max, repeat):
  54. proto = 'http/1.1'
  55. count = 3
  56. exp_resumed = 'Resumed'
  57. xargs = ['--sessionid', '--tls-max', tls_max, f'--tlsv{tls_max}']
  58. if env.curl_uses_lib('gnutls'):
  59. if tls_max == '1.3':
  60. exp_resumed = 'Initial' # 1.2 works in gnutls, but 1.3 does not, TODO
  61. if env.curl_uses_lib('libressl'):
  62. if tls_max == '1.3':
  63. exp_resumed = 'Initial' # 1.2 works in libressl, but 1.3 does not, TODO
  64. if env.curl_uses_lib('wolfssl'):
  65. xargs = ['--sessionid', f'--tlsv{tls_max}']
  66. if tls_max == '1.3':
  67. exp_resumed = 'Initial' # 1.2 works in wolfssl, but 1.3 does not, TODO
  68. if env.curl_uses_lib('rustls-ffi'):
  69. exp_resumed = 'Initial' # rustls does not support sessions, TODO
  70. if env.curl_uses_lib('bearssl') and tls_max == '1.3':
  71. pytest.skip('BearSSL does not support TLSv1.3')
  72. if env.curl_uses_lib('mbedtls') and tls_max == '1.3':
  73. pytest.skip('mbedtls TLSv1.3 session resume not working in 3.6.0')
  74. curl = CurlClient(env=env)
  75. # tell the server to close the connection after each request
  76. urln = f'https://{env.authority_for(env.domain1, proto)}/curltest/sslinfo?'\
  77. f'id=[0-{count-1}]&close'
  78. r = curl.http_download(urls=[urln], alpn_proto=proto, with_stats=True,
  79. extra_args=xargs)
  80. r.check_response(count=count, http_status=200)
  81. # should have used one connection for each request, sessions after
  82. # first should have been resumed
  83. assert r.total_connects == count, r.dump_logs()
  84. for i in range(count):
  85. dfile = curl.download_file(i)
  86. assert os.path.exists(dfile)
  87. with open(dfile) as f:
  88. djson = json.load(f)
  89. assert djson['HTTPS'] == 'on', f'{i}: {djson}'
  90. if i == 0:
  91. assert djson['SSL_SESSION_RESUMED'] == 'Initial', f'{i}: {djson}'
  92. else:
  93. assert djson['SSL_SESSION_RESUMED'] == exp_resumed, f'{i}: {djson}'
  94. # use host name with trailing dot, verify handshake
  95. @pytest.mark.parametrize("proto", ['http/1.1', 'h2', 'h3'])
  96. def test_17_03_trailing_dot(self, env: Env, httpd, nghttpx, repeat, proto):
  97. if proto == 'h3' and not env.have_h3():
  98. pytest.skip("h3 not supported")
  99. curl = CurlClient(env=env)
  100. domain = f'{env.domain1}.'
  101. url = f'https://{env.authority_for(domain, proto)}/curltest/sslinfo'
  102. r = curl.http_get(url=url, alpn_proto=proto)
  103. assert r.exit_code == 0, f'{r}'
  104. assert r.json, f'{r}'
  105. if proto != 'h3': # we proxy h3
  106. # the SNI the server received is without trailing dot
  107. assert r.json['SSL_TLS_SNI'] == env.domain1, f'{r.json}'
  108. # use host name with double trailing dot, verify handshake
  109. @pytest.mark.parametrize("proto", ['http/1.1', 'h2', 'h3'])
  110. def test_17_04_double_dot(self, env: Env, httpd, nghttpx, repeat, proto):
  111. if proto == 'h3' and not env.have_h3():
  112. pytest.skip("h3 not supported")
  113. if proto == 'h3' and env.curl_uses_lib('wolfssl'):
  114. pytest.skip("wolfSSL HTTP/3 peer verification does not properly check")
  115. curl = CurlClient(env=env)
  116. domain = f'{env.domain1}..'
  117. url = f'https://{env.authority_for(domain, proto)}/curltest/sslinfo'
  118. r = curl.http_get(url=url, alpn_proto=proto, extra_args=[
  119. '-H', f'Host: {env.domain1}',
  120. ])
  121. if r.exit_code == 0:
  122. assert r.json, f'{r.stdout}'
  123. # the SNI the server received is without trailing dot
  124. if proto != 'h3': # we proxy h3
  125. assert r.json['SSL_TLS_SNI'] == env.domain1, f'{r.json}'
  126. assert False, f'should not have succeeded: {r.json}'
  127. # 7 - rustls rejects a servername with .. during setup
  128. # 35 - libressl rejects setting an SNI name with trailing dot
  129. # 60 - peer name matching failed against certificate
  130. assert r.exit_code in [7, 35, 60], f'{r}'
  131. # use ip address for connect
  132. @pytest.mark.parametrize("proto", ['http/1.1', 'h2', 'h3'])
  133. def test_17_05_ip_addr(self, env: Env, httpd, nghttpx, repeat, proto):
  134. if env.curl_uses_lib('bearssl'):
  135. pytest.skip("bearssl does not support cert verification with IP addresses")
  136. if env.curl_uses_lib('mbedtls'):
  137. pytest.skip("mbedtls does not support cert verification with IP addresses")
  138. if proto == 'h3' and not env.have_h3():
  139. pytest.skip("h3 not supported")
  140. curl = CurlClient(env=env)
  141. domain = f'127.0.0.1'
  142. url = f'https://{env.authority_for(domain, proto)}/curltest/sslinfo'
  143. r = curl.http_get(url=url, alpn_proto=proto)
  144. assert r.exit_code == 0, f'{r}'
  145. assert r.json, f'{r}'
  146. if proto != 'h3': # we proxy h3
  147. # the SNI should not have been used
  148. assert 'SSL_TLS_SNI' not in r.json, f'{r.json}'
  149. # use localhost for connect
  150. @pytest.mark.parametrize("proto", ['http/1.1', 'h2', 'h3'])
  151. def test_17_06_localhost(self, env: Env, httpd, nghttpx, repeat, proto):
  152. if proto == 'h3' and not env.have_h3():
  153. pytest.skip("h3 not supported")
  154. curl = CurlClient(env=env)
  155. domain = f'localhost'
  156. url = f'https://{env.authority_for(domain, proto)}/curltest/sslinfo'
  157. r = curl.http_get(url=url, alpn_proto=proto)
  158. assert r.exit_code == 0, f'{r}'
  159. assert r.json, f'{r}'
  160. if proto != 'h3': # we proxy h3
  161. assert r.json['SSL_TLS_SNI'] == domain, f'{r.json}'
  162. # test setting cipher suites, the AES 256 ciphers are disabled in the test server
  163. @pytest.mark.parametrize("ciphers, succeed", [
  164. [[0x1301], True],
  165. [[0x1302], False],
  166. [[0x1303], True],
  167. [[0x1302, 0x1303], True],
  168. [[0xC02B, 0xC02F], True],
  169. [[0xC02C, 0xC030], False],
  170. [[0xCCA9, 0xCCA8], True],
  171. [[0xC02C, 0xC030, 0xCCA9, 0xCCA8], True],
  172. ])
  173. def test_17_07_ssl_ciphers(self, env: Env, httpd, nghttpx, ciphers, succeed, repeat):
  174. cipher_table = {
  175. 0x1301: 'TLS_AES_128_GCM_SHA256',
  176. 0x1302: 'TLS_AES_256_GCM_SHA384',
  177. 0x1303: 'TLS_CHACHA20_POLY1305_SHA256',
  178. 0xC02B: 'ECDHE-ECDSA-AES128-GCM-SHA256',
  179. 0xC02F: 'ECDHE-RSA-AES128-GCM-SHA256',
  180. 0xC02C: 'ECDHE-ECDSA-AES256-GCM-SHA384',
  181. 0xC030: 'ECDHE-RSA-AES256-GCM-SHA384',
  182. 0xCCA9: 'ECDHE-ECDSA-CHACHA20-POLY1305',
  183. 0xCCA8: 'ECDHE-RSA-CHACHA20-POLY1305',
  184. }
  185. cipher_names = list(map(cipher_table.get, ciphers))
  186. proto = 'http/1.1'
  187. curl = CurlClient(env=env)
  188. url = f'https://{env.authority_for(env.domain1, proto)}/curltest/sslinfo'
  189. extra_args = []
  190. if env.curl_uses_lib('gnutls'):
  191. pytest.skip('gnutls does not support setting ciphers by name')
  192. if env.curl_uses_lib('rustls-ffi'):
  193. pytest.skip('rustls-ffi does not support setting ciphers')
  194. if ciphers[0] & 0xFF00 == 0x1300:
  195. # test setting TLSv1.3 ciphers
  196. if env.curl_uses_lib('bearssl'):
  197. pytest.skip('bearssl does not support TLSv1.3')
  198. elif env.curl_uses_lib('sectransp'):
  199. pytest.skip('sectransp does not support TLSv1.3')
  200. elif env.curl_uses_lib('boringssl'):
  201. pytest.skip('boringssl does not support setting TLSv1.3 ciphers')
  202. elif env.curl_uses_lib('mbedtls'):
  203. if not env.curl_lib_version_at_least('mbedtls', '3.6.0'):
  204. pytest.skip('mbedtls TLSv1.3 support requires at least 3.6.0')
  205. extra_args = ['--ciphers', ':'.join(cipher_names)]
  206. elif env.curl_uses_lib('wolfssl'):
  207. extra_args = ['--ciphers', ':'.join(cipher_names)]
  208. else:
  209. extra_args = ['--tls13-ciphers', ':'.join(cipher_names)]
  210. else:
  211. # test setting TLSv1.2 ciphers
  212. if env.curl_uses_lib('schannel'):
  213. pytest.skip('schannel does not support setting TLSv1.2 ciphers by name')
  214. elif env.curl_uses_lib('wolfssl'):
  215. # setting tls version is botched with wolfssl: setting max (--tls-max)
  216. # is not supported, setting min (--tlsv1.*) actually also sets max
  217. extra_args = ['--tlsv1.2', '--ciphers', ':'.join(cipher_names)]
  218. else:
  219. # the server supports TLSv1.3, so to test TLSv1.2 ciphers we set tls-max
  220. extra_args = ['--tls-max', '1.2', '--ciphers', ':'.join(cipher_names)]
  221. r = curl.http_get(url=url, alpn_proto=proto, extra_args=extra_args)
  222. if succeed:
  223. assert r.exit_code == 0, f'{r}'
  224. assert r.json['HTTPS'] == 'on', f'{r.json}'
  225. assert 'SSL_CIPHER' in r.json, f'{r.json}'
  226. assert r.json['SSL_CIPHER'] in cipher_names, f'{r.json}'
  227. else:
  228. assert r.exit_code != 0, f'{r}'
  229. @pytest.mark.parametrize("proto", ['http/1.1', 'h2', 'h3'])
  230. def test_17_08_cert_status(self, env: Env, httpd, nghttpx, repeat, proto):
  231. if proto == 'h3' and not env.have_h3():
  232. pytest.skip("h3 not supported")
  233. if not env.curl_uses_lib('openssl') and \
  234. not env.curl_uses_lib('gnutls') and \
  235. not env.curl_uses_lib('quictls'):
  236. pytest.skip("tls library does not support --cert-status")
  237. curl = CurlClient(env=env)
  238. domain = f'localhost'
  239. url = f'https://{env.authority_for(domain, proto)}/'
  240. r = curl.http_get(url=url, alpn_proto=proto, extra_args=[
  241. '--cert-status'
  242. ])
  243. # CURLE_SSL_INVALIDCERTSTATUS, our certs have no OCSP info
  244. assert r.exit_code == 91, f'{r}'