master
1#***************************************************************************
2# _ _ ____ _
3# Project ___| | | | _ \| |
4# / __| | | | |_) | |
5# | (__| |_| | _ <| |___
6# \___|\___/|_| \_\_____|
7#
8# Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
9#
10# This software is licensed as described in the file COPYING, which
11# you should have received as part of this distribution. The terms
12# are also available at https://curl.se/docs/copyright.html.
13#
14# You may opt to use, copy, modify, merge, publish, distribute and/or sell
15# copies of the Software, and permit persons to whom the Software is
16# furnished to do so, under the terms of the COPYING file.
17#
18# This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19# KIND, either express or implied.
20#
21# SPDX-License-Identifier: curl
22#
23###########################################################################
24#
25import logging
26import os
27import shutil
28import socket
29import subprocess
30from typing import Dict
32import pytest
33from testenv import CurlClient, Env
34from testenv.ports import alloc_ports_and_do
36log = logging.getLogger(__name__)
39class OsslServer:
41 def __init__(self, env, domain, ocsp_case):
42 self.name = 'osslserver'
43 self.env = env
44 self.run_dir = os.path.join(env.gen_dir, self.name)
45 self.err_file = os.path.join(self.run_dir, 'stderr')
46 self._rmrf(self.run_dir)
47 self._mkpath(self.run_dir)
48 self.domain = domain
49 self.ocsp_case = ocsp_case
50 self.sproc = None
51 self.cerr = None
52 self.port = 0
54 def _mkpath(self, path):
55 if not os.path.exists(path):
56 os.makedirs(path)
58 def _rmrf(self, path):
59 if os.path.exists(path):
60 shutil.rmtree(path)
62 def _mk_ocsp_stat(self, ocsp_file, creds, index_content):
63 # make an OCSP status file for certificate 'creds' with
64 # content 'index_content'
65 ocsp_index = os.path.join(self.env.gen_dir, 'ocsp-index.der')
66 with open(ocsp_index, 'w') as fd:
67 fd.write(index_content)
68 ocsp_req = os.path.join(self.env.gen_dir, 'ocsp-req.der')
69 s = subprocess.run([
70 self.env.CONFIG.openssl, 'ocsp',
71 '-issuer', self.env.ca.cert_file,
72 '-cert', creds.cert_file,
73 '-no_nonce',
74 '-reqout', ocsp_req
75 ], stderr=self.cerr, stdout=self.cerr, check=True)
76 assert s.returncode == 0
77 s = subprocess.run([
78 self.env.CONFIG.openssl, 'ocsp',
79 '-index', ocsp_index,
80 '-rsigner', self.env.ca.cert_file,
81 '-rkey', self.env.ca.pkey_file,
82 '-CA', self.env.ca.cert_file,
83 '-reqin', ocsp_req,
84 '-respout', ocsp_file,
85 '-ndays', '1'
86 ], stderr=self.cerr, stdout=self.cerr, check=True)
87 assert s.returncode == 0
89 def startup(self):
90 self.cerr = open(self.err_file, 'w') # noqa: SIM115
91 creds = self.env.get_credentials(self.domain)
92 assert creds # convince pytype this is not None
93 ocsp_file = os.path.join(self.env.gen_dir, 'ocsp-stat.der')
94 if self.ocsp_case == 'trylater':
95 with open(ocsp_file, 'wb') as fd:
96 fd.write(b'\x30\x03\x0a\x01\x03')
97 elif self.ocsp_case == 'unauth':
98 with open(ocsp_file, 'wb') as fd:
99 fd.write(b'\x30\x03\x0a\x01\x06')
100 elif self.ocsp_case == 'interror':
101 with open(ocsp_file, 'wb') as fd:
102 fd.write(b'\x30\x03\x0a\x01\x02')
103 elif self.ocsp_case == 'unknown':
104 self._mk_ocsp_stat(ocsp_file, creds, '')
105 elif self.ocsp_case == 'revoked':
106 index_file = os.path.join(self.env.gen_dir, 'ocsp-index.txt')
107 # openssl wants hex numbers as complete bytes
108 serial = hex(creds.serial_number)[2:].upper()
109 serial = f'0{serial}' if len(serial) % 2 else serial
110 index = 'R\t301231000000Z\t240101000000Z,keyCompromise' \
111 f'\t{serial}\tunknown\t/{creds.subject.rfc4514_string()}\n'
112 with open(index_file, 'wb') as fd:
113 fd.write(index.encode())
114 self._mk_ocsp_stat(ocsp_file, creds, index)
115 elif self.ocsp_case == 'valid':
116 index_file = os.path.join(self.env.gen_dir, 'ocsp-index.txt')
117 # openssl wants hex numbers as complete bytes
118 serial = hex(creds.serial_number)[2:].upper()
119 serial = f'0{serial}' if len(serial) % 2 else serial
120 index = 'V\t301231000000Z\t240101000000Z,valid' \
121 f'\t{serial}\tunknown\t/{creds.subject.rfc4514_string()}\n'
122 with open(index_file, 'wb') as fd:
123 fd.write(index.encode())
124 self._mk_ocsp_stat(ocsp_file, creds, index)
125 elif self.ocsp_case:
126 assert False, f'unknown ocsp case: {self.ocsp_case}'
128 def startup(ports: Dict[str, int]) -> bool:
129 self.port = ports[self.name]
130 sargs = [
131 self.env.CONFIG.openssl, 's_server', '-www',
132 '-accept', f'{self.port}', '-tls1_2',
133 '-cert', creds.cert_file, '-key', creds.pkey_file,
134 ]
135 if self.ocsp_case is not None:
136 sargs.extend(['-status_file', ocsp_file])
137 log.info(f'start_ {sargs}')
138 self.sproc = subprocess.Popen(args=sargs,
139 cwd=self.run_dir,
140 stderr=self.cerr,
141 stdout=self.cerr)
142 self.env.update_ports(ports)
143 return True
145 port_spec = {
146 self.name: socket.SOCK_STREAM
147 }
148 assert alloc_ports_and_do(port_spec, startup,
149 self.env.gen_root, max_tries=3)
150 assert self.sproc
152 def shutdown(self):
153 self.sproc.terminate()
154 self.cerr.close()
156 def set_ocsp_case(self, ocsp_case):
157 if self.ocsp_case != ocsp_case:
158 self.shutdown()
159 self.ocsp_case = ocsp_case
160 self.startup()
163@pytest.mark.skipif(condition=not Env.have_openssl_ech(), reason="no modern openssl available")
164@pytest.mark.skipif(condition=not Env.curl_can_cert_status(), reason="no cert-status in curl")
165class TestOCSP:
167 @pytest.fixture(autouse=True, scope='class')
168 @classmethod
169 def osslserver(cls, env):
170 server = OsslServer(env, env.domain1, None)
171 server.startup()
172 yield server
173 server.shutdown()
175 @pytest.mark.parametrize("cert_status, exp_ok, ocsp_case", [
176 [False, True, None],
177 [True, False, None], # CURLE_SSL_INVALIDCERTSTATUS
178 [True, False, 'trylater'],
179 [True, False, 'unauth'],
180 [True, False, 'interror'],
181 [True, False, 'unknown'],
182 [True, False, 'revoked'],
183 [True, True, 'valid'],
184 ])
185 def test_24_01_verify_status(self, env: Env, osslserver, cert_status, exp_ok, ocsp_case):
186 osslserver.set_ocsp_case(ocsp_case)
187 run_env = os.environ.copy()
188 xargs = []
189 if cert_status:
190 xargs.append('--cert-status')
191 curl = CurlClient(env=env, run_env=run_env)
192 url = f'https://{env.domain1}:{osslserver.port}/data.json'
193 r = curl.http_download(urls=[url], with_stats=True, extra_args=xargs)
194 if exp_ok:
195 r.check_exit_code(0)
196 elif ocsp_case == 'revoked':
197 # gnutls fails this with 60 as its internal checks fail early
198 assert r.exit_code in [60, 91], f'{r}'
199 else:
200 r.check_exit_code(91)
Diagnostics4 git calls · 140 ms
CallStatusSizeTime0140 ms
discover200152 B3 ms
git.upload-pack discover http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=2 git.response.bytes=152 git.server.agent=gitty/1.0 git.server.object_format=sha1 git.server.caps=agent ls-refs fetch object-format git.server.fetch=shallow filter sideband-all git.protocol.version=2 git.pkt.frames=8 git.pkt.data=6 git.pkt.flush=2 git.pkt.delim=0 git.pkt.bytes=152 git.pkt.payload.max=34
ls-refs200149 B · hit3 ms
git.upload-pack ls-refs http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=ls-refs git.upstream.dur_ms=3 git.response.bytes=149 git.response.cache=HIT git.response.duration_ms=3 git.protocol.version=2 git.pkt.frames=3 git.pkt.data=2 git.pkt.flush=1 git.pkt.delim=0 git.pkt.bytes=149 git.pkt.payload.max=78 git.ls_refs.count=2 git.ls_refs.head=refs/heads/master
fetchblob:none · deepen 10200287 KiB · hit2 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=135 git.response.bytes=293470 git.response.cache=HIT git.response.duration_ms=2 git.fetch.filter=blob:none git.fetch.filter.requested=tree:0 git.fetch.filter.fallback=blob:none git.fetch.filter.learned=false git.fetch.wants=31c911efe5652725e930f4d2e60c9060e5f6646d git.fetch.haves.count=0 git.fetch.deepen=10 git.fetch.thin_pack=false git.fetch.ofs_delta=true git.protocol.version=2 git.pkt.frames=10 git.pkt.data=8 git.pkt.flush=1 git.pkt.delim=1 git.pkt.bytes=293470 git.pkt.payload.max=65516 git.sideband.band1.bytes=293354 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0 git.pack.objects=87 git.pack.bytes=293354 git.pack.ofs_delta=0 git.pack.ref_delta=1 git.pack.chain.max=1 git.pack.trailer=6a646bcda6740bab42e321b51998b52ea15146c0 git.pack.full=86Open pack map
fetch2002.4 KiB135 ms
git.upload-pack fetch http.response.status_code=200 git.response.bytes=2448 git.response.cache=MISS git.response.duration_ms=135 git.fetch.wants=e37bd20b3b8bd631c3951244c6afff5dedf17ed4 git.fetch.haves.count=0 git.fetch.deepen=0 git.fetch.thin_pack=false git.fetch.ofs_delta=true git.protocol.version=2 git.pkt.frames=3 git.pkt.data=2 git.pkt.flush=1 git.pkt.delim=0 git.pkt.bytes=2448 git.pkt.payload.max=2427 git.sideband.band1.bytes=2426 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0
Each row is one HTTP round trip ply made to the git server for this page, on one timeline. discover asks what the server speaks, ls-refs lists branch tips, fetch brings objects by id. A hit was served by Workers Cache. Tap a row for its wire details.
- Remote
- https://5ad2236b39de4c8bd8f6270ea63f3ea9.artifacts.cloudflare.net/git/public-v0/curl.git
- Ref
- refs/heads/master
- Commit
- 31c911efe5652725e930f4d2e60c9060e5f6646d
- cf-ray
- a5e2eb1c6d6a4bc2
- Request
- 472dca55…
- Protocol
- v2 · sha1
- Agent
- gitty/1.0
- Filter
- advertised
- Fetch
- shallow filter sideband-all
- Capabilities
- agent ls-refs fetch object-format