Coverage for Lib/asyncio/sslproto.py: 87%

555 statements  

« prev     ^ index     » next       coverage.py v7.17.3, created at 2026-11-20 05:46 +0000

1# Contains code from https://github.com/MagicStack/uvloop/tree/v0.16.0 

2# SPDX-License-Identifier: PSF-2.0 AND (MIT OR Apache-2.0) 

3# SPDX-FileCopyrightText: Copyright (c) 2015-2021 MagicStack Inc. http://magic.io 

4 

5import collections 

6import enum 

7import warnings 

8try: 

9 import ssl 

10except ImportError: # pragma: no cover 

11 ssl = None 

12 

13from . import constants 

14from . import exceptions 

15from . import protocols 

16from . import transports 

17from .log import logger 

18 

19if ssl is not None: 19 ↛ 23line 19 didn't jump to line 23 because the condition on line 19 was always true

20 SSLAgainErrors = (ssl.SSLWantReadError, ssl.SSLSyscallError) 

21 

22 

23class SSLProtocolState(enum.Enum): 

24 UNWRAPPED = "UNWRAPPED" 

25 DO_HANDSHAKE = "DO_HANDSHAKE" 

26 WRAPPED = "WRAPPED" 

27 FLUSHING = "FLUSHING" 

28 SHUTDOWN = "SHUTDOWN" 

29 

30 

31class AppProtocolState(enum.Enum): 

32 # This tracks the state of app protocol (https://git.io/fj59P): 

33 # 

34 # INIT -cm-> CON_MADE [-dr*->] [-er-> EOF?] -cl-> CON_LOST 

35 # 

36 # * cm: connection_made() 

37 # * dr: data_received() 

38 # * er: eof_received() 

39 # * cl: connection_lost() 

40 

41 STATE_INIT = "STATE_INIT" 

42 STATE_CON_MADE = "STATE_CON_MADE" 

43 STATE_EOF = "STATE_EOF" 

44 STATE_CON_LOST = "STATE_CON_LOST" 

45 

46 

47def _create_transport_context(server_side, server_hostname): 

48 if server_side: 48 ↛ 49line 48 didn't jump to line 49 because the condition on line 48 was never true

49 raise ValueError('Server side SSL needs a valid SSLContext') 

50 

51 # Client side may pass ssl=True to use a default 

52 # context; in that case the sslcontext passed is None. 

53 # The default is secure for client connections. 

54 # Python 3.4+: use up-to-date strong settings. 

55 sslcontext = ssl.create_default_context() 

56 if not server_hostname: 56 ↛ 57line 56 didn't jump to line 57 because the condition on line 56 was never true

57 sslcontext.check_hostname = False 

58 return sslcontext 

59 

60 

61def add_flowcontrol_defaults(high, low, kb): 

62 if high is None: 

63 if low is None: 63 ↛ 66line 63 didn't jump to line 66 because the condition on line 63 was always true

64 hi = kb * 1024 

65 else: 

66 lo = low 

67 hi = 4 * lo 

68 else: 

69 hi = high 

70 if low is None: 70 ↛ 73line 70 didn't jump to line 73 because the condition on line 70 was always true

71 lo = hi // 4 

72 else: 

73 lo = low 

74 

75 if not hi >= lo >= 0: 75 ↛ 76line 75 didn't jump to line 76 because the condition on line 75 was never true

76 raise ValueError('high (%r) must be >= low (%r) must be >= 0' % 

77 (hi, lo)) 

78 

79 return hi, lo 

80 

81 

82class _SSLProtocolTransport(transports._FlowControlMixin, 

83 transports.Transport): 

84 

85 _start_tls_compatible = True 

86 _sendfile_compatible = constants._SendfileMode.FALLBACK 

87 

88 def __init__(self, loop, ssl_protocol): 

89 self._loop = loop 

90 self._ssl_protocol = ssl_protocol 

91 self._closed = False 

92 

93 def get_extra_info(self, name, default=None): 

94 """Get optional transport information.""" 

95 return self._ssl_protocol._get_extra_info(name, default) 

96 

97 def set_protocol(self, protocol): 

98 self._ssl_protocol._set_app_protocol(protocol) 

99 

100 def get_protocol(self): 

101 return self._ssl_protocol._app_protocol 

102 

103 def is_closing(self): 

104 return self._closed or self._ssl_protocol._is_transport_closing() 

105 

106 def close(self): 

107 """Close the transport. 

108 

109 Buffered data will be flushed asynchronously. No more data 

110 will be received. After all buffered data is flushed, the 

111 protocol's connection_lost() method will (eventually) called 

112 with None as its argument. 

113 """ 

114 if not self._closed: 

115 self._closed = True 

116 self._ssl_protocol._start_shutdown() 

117 

118 def __del__(self, _warnings=warnings): 

119 if not self._closed: 119 ↛ 120line 119 didn't jump to line 120 because the condition on line 119 was never true

120 self._closed = True 

121 _warnings.warn( 

122 "unclosed transport <asyncio._SSLProtocolTransport " 

123 "object>", ResourceWarning) 

124 

125 def is_reading(self): 

126 return not self._ssl_protocol._app_reading_paused 

127 

128 def pause_reading(self): 

129 """Pause the receiving end. 

130 

131 No data will be passed to the protocol's data_received() 

132 method until resume_reading() is called. 

133 """ 

134 self._ssl_protocol._pause_reading() 

135 

136 def resume_reading(self): 

137 """Resume the receiving end. 

138 

139 Data received will once again be passed to the protocol's 

140 data_received() method. 

141 """ 

142 self._ssl_protocol._resume_reading() 

143 

144 def set_write_buffer_limits(self, high=None, low=None): 

145 """Set the high- and low-water limits for write flow control. 

146 

147 These two values control when to call the protocol's 

148 pause_writing() and resume_writing() methods. If specified, 

149 the low-water limit must be less than or equal to the 

150 high-water limit. Neither value can be negative. 

151 

152 The defaults are implementation-specific. If only the 

153 high-water limit is given, the low-water limit defaults to an 

154 implementation-specific value less than or equal to the 

155 high-water limit. Setting high to zero forces low to zero as 

156 well, and causes pause_writing() to be called whenever the 

157 buffer becomes non-empty. Setting low to zero causes 

158 resume_writing() to be called only once the buffer is empty. 

159 Use of zero for either limit is generally sub-optimal as it 

160 reduces opportunities for doing I/O and computation 

161 concurrently. 

162 """ 

163 self._ssl_protocol._set_write_buffer_limits(high, low) 

164 self._ssl_protocol._control_app_writing() 

165 

166 def get_write_buffer_limits(self): 

167 return (self._ssl_protocol._outgoing_low_water, 

168 self._ssl_protocol._outgoing_high_water) 

169 

170 def get_write_buffer_size(self): 

171 """Return the current size of the write buffers.""" 

172 return self._ssl_protocol._get_write_buffer_size() 

173 

174 def set_read_buffer_limits(self, high=None, low=None): 

175 """Set the high- and low-water limits for read flow control. 

176 

177 These two values control when to call the upstream transport's 

178 pause_reading() and resume_reading() methods. If specified, 

179 the low-water limit must be less than or equal to the 

180 high-water limit. Neither value can be negative. 

181 

182 The defaults are implementation-specific. If only the 

183 high-water limit is given, the low-water limit defaults to an 

184 implementation-specific value less than or equal to the 

185 high-water limit. Setting high to zero forces low to zero as 

186 well, and causes pause_reading() to be called whenever the 

187 buffer becomes non-empty. Setting low to zero causes 

188 resume_reading() to be called only once the buffer is empty. 

189 Use of zero for either limit is generally sub-optimal as it 

190 reduces opportunities for doing I/O and computation 

191 concurrently. 

192 """ 

193 self._ssl_protocol._set_read_buffer_limits(high, low) 

194 self._ssl_protocol._control_ssl_reading() 

195 

196 def get_read_buffer_limits(self): 

197 return (self._ssl_protocol._incoming_low_water, 

198 self._ssl_protocol._incoming_high_water) 

199 

200 def get_read_buffer_size(self): 

201 """Return the current size of the read buffer.""" 

202 return self._ssl_protocol._get_read_buffer_size() 

203 

204 @property 

205 def _protocol_paused(self): 

206 # Required for sendfile fallback pause_writing/resume_writing logic 

207 return self._ssl_protocol._app_writing_paused 

208 

209 def write(self, data): 

210 """Write some data bytes to the transport. 

211 

212 This does not block; it buffers the data and arranges for it 

213 to be sent out asynchronously. 

214 """ 

215 if not isinstance(data, (bytes, bytearray, memoryview)): 215 ↛ 216line 215 didn't jump to line 216 because the condition on line 215 was never true

216 raise TypeError(f"data: expecting a bytes-like instance, " 

217 f"got {type(data).__name__}") 

218 if not data: 218 ↛ 219line 218 didn't jump to line 219 because the condition on line 218 was never true

219 return 

220 self._ssl_protocol._write_appdata((data,)) 

221 

222 def writelines(self, list_of_data): 

223 """Write a list (or any iterable) of data bytes to the transport. 

224 

225 The default implementation concatenates the arguments and 

226 calls write() on the result. 

227 """ 

228 self._ssl_protocol._write_appdata(list_of_data) 

229 

230 def write_eof(self): 

231 """Close the write end after flushing buffered data. 

232 

233 This raises :exc:`NotImplementedError` right now. 

234 """ 

235 raise NotImplementedError 

236 

237 def can_write_eof(self): 

238 """Return True if this transport supports write_eof(), False if not.""" 

239 return False 

240 

241 def abort(self): 

242 """Close the transport immediately. 

243 

244 Buffered data will be lost. No more data will be received. 

245 The protocol's connection_lost() method will (eventually) be 

246 called with None as its argument. 

247 """ 

248 self._force_close(None) 

249 

250 def _force_close(self, exc): 

251 self._closed = True 

252 if self._ssl_protocol is not None: 252 ↛ exitline 252 didn't return from function '_force_close' because the condition on line 252 was always true

253 self._ssl_protocol._abort(exc) 

254 

255 def _test__append_write_backlog(self, data): 

256 # for test only 

257 self._ssl_protocol._write_backlog.append(data) 

258 self._ssl_protocol._write_buffer_size += len(data) 

259 

260 

261class SSLProtocol(protocols.BufferedProtocol): 

262 max_size = 256 * 1024 # Buffer size passed to read() 

263 

264 _handshake_start_time = None 

265 _handshake_timeout_handle = None 

266 _shutdown_timeout_handle = None 

267 

268 def __init__(self, loop, app_protocol, sslcontext, waiter, 

269 server_side=False, server_hostname=None, 

270 call_connection_made=True, 

271 ssl_handshake_timeout=None, 

272 ssl_shutdown_timeout=None): 

273 if ssl is None: 

274 raise RuntimeError("stdlib ssl module not available") 

275 

276 self._ssl_buffer = bytearray(self.max_size) 

277 self._ssl_buffer_view = memoryview(self._ssl_buffer) 

278 

279 if ssl_handshake_timeout is None: 

280 ssl_handshake_timeout = constants.SSL_HANDSHAKE_TIMEOUT 

281 elif ssl_handshake_timeout <= 0: 

282 raise ValueError( 

283 f"ssl_handshake_timeout should be a positive number, " 

284 f"got {ssl_handshake_timeout}") 

285 if ssl_shutdown_timeout is None: 

286 ssl_shutdown_timeout = constants.SSL_SHUTDOWN_TIMEOUT 

287 elif ssl_shutdown_timeout <= 0: 287 ↛ 288line 287 didn't jump to line 288 because the condition on line 287 was never true

288 raise ValueError( 

289 f"ssl_shutdown_timeout should be a positive number, " 

290 f"got {ssl_shutdown_timeout}") 

291 

292 if not sslcontext: 

293 sslcontext = _create_transport_context( 

294 server_side, server_hostname) 

295 

296 self._server_side = server_side 

297 if server_hostname and not server_side: 

298 self._server_hostname = server_hostname 

299 else: 

300 self._server_hostname = None 

301 self._sslcontext = sslcontext 

302 # SSL-specific extra info. More info are set when the handshake 

303 # completes. 

304 self._extra = dict(sslcontext=sslcontext) 

305 

306 # App data write buffering 

307 self._write_backlog = collections.deque() 

308 self._write_buffer_size = 0 

309 # How much of self._outgoing was produced by _do_write(): encrypted 

310 # app data, plus anything else OpenSSL flushed along with it 

311 self._outgoing_app_size = 0 

312 

313 self._waiter = waiter 

314 self._loop = loop 

315 self._set_app_protocol(app_protocol) 

316 self._app_transport = None 

317 self._app_transport_created = False 

318 # transport, ex: SelectorSocketTransport 

319 self._transport = None 

320 self._ssl_handshake_timeout = ssl_handshake_timeout 

321 self._ssl_shutdown_timeout = ssl_shutdown_timeout 

322 # SSL and state machine 

323 self._incoming = ssl.MemoryBIO() 

324 self._outgoing = ssl.MemoryBIO() 

325 self._state = SSLProtocolState.UNWRAPPED 

326 self._conn_lost = 0 # Set when connection_lost called 

327 if call_connection_made: 

328 self._app_state = AppProtocolState.STATE_INIT 

329 else: 

330 self._app_state = AppProtocolState.STATE_CON_MADE 

331 self._sslobj = self._sslcontext.wrap_bio( 

332 self._incoming, self._outgoing, 

333 server_side=self._server_side, 

334 server_hostname=self._server_hostname) 

335 

336 # Flow Control 

337 

338 self._ssl_writing_paused = False 

339 

340 self._app_reading_paused = False 

341 

342 self._ssl_reading_paused = False 

343 self._incoming_high_water = 0 

344 self._incoming_low_water = 0 

345 self._set_read_buffer_limits() 

346 self._eof_received = False 

347 

348 self._app_writing_paused = False 

349 self._outgoing_high_water = 0 

350 self._outgoing_low_water = 0 

351 self._set_write_buffer_limits() 

352 self._get_app_transport() 

353 

354 def _set_app_protocol(self, app_protocol): 

355 self._app_protocol = app_protocol 

356 # Make fast hasattr check first 

357 if (hasattr(app_protocol, 'get_buffer') and 

358 isinstance(app_protocol, protocols.BufferedProtocol)): 

359 self._app_protocol_get_buffer = app_protocol.get_buffer 

360 self._app_protocol_buffer_updated = app_protocol.buffer_updated 

361 self._app_protocol_is_buffer = True 

362 else: 

363 self._app_protocol_is_buffer = False 

364 

365 def _wakeup_waiter(self, exc=None): 

366 if self._waiter is None: 

367 return 

368 if not self._waiter.cancelled(): 

369 if exc is not None: 

370 self._waiter.set_exception(exc) 

371 else: 

372 self._waiter.set_result(None) 

373 self._waiter = None 

374 

375 def _get_app_transport(self): 

376 if self._app_transport is None: 

377 if self._app_transport_created: 377 ↛ 378line 377 didn't jump to line 378 because the condition on line 377 was never true

378 raise RuntimeError('Creating _SSLProtocolTransport twice') 

379 self._app_transport = _SSLProtocolTransport(self._loop, self) 

380 self._app_transport_created = True 

381 return self._app_transport 

382 

383 def _is_transport_closing(self): 

384 return self._transport is not None and self._transport.is_closing() 

385 

386 def connection_made(self, transport): 

387 """Called when the low-level connection is made. 

388 

389 Start the SSL handshake. 

390 """ 

391 self._transport = transport 

392 self._start_handshake() 

393 

394 def connection_lost(self, exc): 

395 """Called when the low-level connection is lost or closed. 

396 

397 The argument is an exception object or None (the latter 

398 meaning a regular EOF is received or the connection was 

399 aborted or closed). 

400 """ 

401 self._write_backlog.clear() 

402 self._outgoing.read() 

403 self._conn_lost += 1 

404 

405 # Just mark the app transport as closed so that its __dealloc__ 

406 # doesn't complain. 

407 if self._app_transport is not None: 407 ↛ 413line 407 didn't jump to line 413 because the condition on line 407 was always true

408 self._app_transport._closed = True 

409 

410 # The app protocol gets connection_lost() only if it got 

411 # connection_made(). After start_tls() that is already the case 

412 # while the handshake is in progress. 

413 app_notified = False 

414 if ( 

415 self._app_state == AppProtocolState.STATE_CON_MADE or 

416 self._app_state == AppProtocolState.STATE_EOF 

417 ): 

418 self._app_state = AppProtocolState.STATE_CON_LOST 

419 self._loop.call_soon(self._app_protocol.connection_lost, exc) 

420 app_notified = True 

421 self._set_state(SSLProtocolState.UNWRAPPED) 

422 self._transport = None 

423 self._app_transport = None 

424 self._app_protocol = None 

425 self._wakeup_waiter(exc) 

426 

427 if self._shutdown_timeout_handle: 

428 self._shutdown_timeout_handle.cancel() 

429 self._shutdown_timeout_handle = None 

430 if self._handshake_timeout_handle: 

431 self._handshake_timeout_handle.cancel() 

432 self._handshake_timeout_handle = None 

433 if app_notified: 

434 # After the app protocol's connection_lost() (scheduled above), 

435 # so that it can still read the TLS extra info. 

436 self._loop.call_soon(self._release_tls_state) 

437 else: 

438 self._release_tls_state() 

439 

440 def _release_tls_state(self): 

441 # The connection is gone: drop everything that refers to the 

442 # SSLContext, so that an application holding the transport does 

443 # not keep the context alive. 

444 self._sslobj = None 

445 self._sslcontext = None 

446 self._extra.pop('sslcontext', None) 

447 self._extra.pop('ssl_object', None) 

448 

449 def get_buffer(self, n): 

450 want = n 

451 if want <= 0 or want > self.max_size: 451 ↛ 453line 451 didn't jump to line 453 because the condition on line 451 was always true

452 want = self.max_size 

453 if len(self._ssl_buffer) < want: 453 ↛ 454line 453 didn't jump to line 454 because the condition on line 453 was never true

454 self._ssl_buffer = bytearray(want) 

455 self._ssl_buffer_view = memoryview(self._ssl_buffer) 

456 return self._ssl_buffer_view 

457 

458 def buffer_updated(self, nbytes): 

459 self._incoming.write(self._ssl_buffer_view[:nbytes]) 

460 

461 if self._state == SSLProtocolState.DO_HANDSHAKE: 

462 self._do_handshake() 

463 

464 elif self._state == SSLProtocolState.WRAPPED: 

465 self._do_read() 

466 

467 elif self._state == SSLProtocolState.FLUSHING: 467 ↛ 468line 467 didn't jump to line 468 because the condition on line 467 was never true

468 self._do_flush() 

469 

470 elif self._state == SSLProtocolState.SHUTDOWN: 470 ↛ exitline 470 didn't return from function 'buffer_updated' because the condition on line 470 was always true

471 self._do_shutdown() 

472 

473 def eof_received(self): 

474 """Called when the other end of the low-level stream 

475 is half-closed. 

476 

477 If this returns a false value (including None), the transport 

478 will close itself. If it returns a true value, closing the 

479 transport is up to the protocol. 

480 """ 

481 self._eof_received = True 

482 try: 

483 if self._loop.get_debug(): 483 ↛ 484line 483 didn't jump to line 484 because the condition on line 483 was never true

484 logger.debug("%r received EOF", self) 

485 

486 if self._state == SSLProtocolState.DO_HANDSHAKE: 

487 self._on_handshake_complete(ConnectionResetError) 

488 

489 elif self._state == SSLProtocolState.WRAPPED: 

490 self._set_state(SSLProtocolState.FLUSHING) 

491 if self._app_reading_paused: 

492 return True 

493 else: 

494 self._do_flush() 

495 

496 elif self._state == SSLProtocolState.FLUSHING: 496 ↛ 497line 496 didn't jump to line 497 because the condition on line 496 was never true

497 self._do_write() 

498 self._set_state(SSLProtocolState.SHUTDOWN) 

499 self._do_shutdown() 

500 

501 elif self._state == SSLProtocolState.SHUTDOWN: 501 ↛ exitline 501 didn't return from function 'eof_received' because the condition on line 501 was always true

502 self._do_shutdown() 

503 

504 except Exception: 

505 self._transport.close() 

506 raise 

507 

508 def _get_extra_info(self, name, default=None): 

509 if name in self._extra: 

510 return self._extra[name] 

511 elif self._transport is not None: 

512 return self._transport.get_extra_info(name, default) 

513 else: 

514 return default 

515 

516 def _set_state(self, new_state): 

517 allowed = False 

518 

519 if new_state == SSLProtocolState.UNWRAPPED: 

520 allowed = True 

521 

522 elif ( 

523 self._state == SSLProtocolState.UNWRAPPED and 

524 new_state == SSLProtocolState.DO_HANDSHAKE 

525 ): 

526 allowed = True 

527 

528 elif ( 

529 self._state == SSLProtocolState.DO_HANDSHAKE and 

530 new_state == SSLProtocolState.WRAPPED 

531 ): 

532 allowed = True 

533 

534 elif ( 

535 self._state == SSLProtocolState.WRAPPED and 

536 new_state == SSLProtocolState.FLUSHING 

537 ): 

538 allowed = True 

539 

540 elif ( 540 ↛ 546line 540 didn't jump to line 546 because the condition on line 540 was always true

541 self._state == SSLProtocolState.FLUSHING and 

542 new_state == SSLProtocolState.SHUTDOWN 

543 ): 

544 allowed = True 

545 

546 if allowed: 546 ↛ 550line 546 didn't jump to line 550 because the condition on line 546 was always true

547 self._state = new_state 

548 

549 else: 

550 raise RuntimeError( 

551 'cannot switch state from {} to {}'.format( 

552 self._state, new_state)) 

553 

554 # Handshake flow 

555 

556 def _start_handshake(self): 

557 if self._loop.get_debug(): 557 ↛ 558line 557 didn't jump to line 558 because the condition on line 557 was never true

558 logger.debug("%r starts SSL handshake", self) 

559 self._handshake_start_time = self._loop.time() 

560 else: 

561 self._handshake_start_time = None 

562 

563 self._set_state(SSLProtocolState.DO_HANDSHAKE) 

564 

565 # start handshake timeout count down 

566 self._handshake_timeout_handle = \ 

567 self._loop.call_later(self._ssl_handshake_timeout, 

568 self._check_handshake_timeout) 

569 

570 self._do_handshake() 

571 

572 def _check_handshake_timeout(self): 

573 if self._state == SSLProtocolState.DO_HANDSHAKE: 573 ↛ exitline 573 didn't return from function '_check_handshake_timeout' because the condition on line 573 was always true

574 msg = ( 

575 f"SSL handshake is taking longer than " 

576 f"{self._ssl_handshake_timeout} seconds: " 

577 f"aborting the connection" 

578 ) 

579 self._fatal_error(ConnectionAbortedError(msg)) 

580 

581 def _do_handshake(self): 

582 try: 

583 self._sslobj.do_handshake() 

584 except SSLAgainErrors: 

585 self._process_outgoing() 

586 except ssl.SSLError as exc: 

587 self._on_handshake_complete(exc) 

588 else: 

589 self._on_handshake_complete(None) 

590 

591 def _on_handshake_complete(self, handshake_exc): 

592 if self._handshake_timeout_handle is not None: 592 ↛ 596line 592 didn't jump to line 596 because the condition on line 592 was always true

593 self._handshake_timeout_handle.cancel() 

594 self._handshake_timeout_handle = None 

595 

596 sslobj = self._sslobj 

597 try: 

598 if handshake_exc is None: 

599 self._set_state(SSLProtocolState.WRAPPED) 

600 else: 

601 raise handshake_exc 

602 

603 peercert = sslobj.getpeercert() 

604 except Exception as exc: 

605 handshake_exc = None 

606 self._set_state(SSLProtocolState.UNWRAPPED) 

607 if isinstance(exc, ssl.CertificateError): 

608 msg = 'SSL handshake failed on verifying the certificate' 

609 else: 

610 msg = 'SSL handshake failed' 

611 # gh-98078: When the handshake fails, OpenSSL leaves the fatal 

612 # TLS alert (for example "bad certificate" or "protocol 

613 # version") in the outgoing BIO. Send it to the peer before 

614 # closing the transport so that it knows why the handshake 

615 # failed. 

616 self._process_outgoing() 

617 self._fatal_error(exc, msg) 

618 self._wakeup_waiter(exc) 

619 return 

620 

621 if self._loop.get_debug(): 621 ↛ 622line 621 didn't jump to line 622 because the condition on line 621 was never true

622 dt = self._loop.time() - self._handshake_start_time 

623 logger.debug("%r: SSL handshake took %.1f ms", self, dt * 1e3) 

624 

625 # Add extra info that becomes available after handshake. 

626 self._extra.update(peercert=peercert, 

627 cipher=sslobj.cipher(), 

628 compression=sslobj.compression(), 

629 ssl_object=sslobj) 

630 if self._app_state == AppProtocolState.STATE_INIT: 

631 self._app_state = AppProtocolState.STATE_CON_MADE 

632 self._app_protocol.connection_made(self._get_app_transport()) 

633 self._wakeup_waiter() 

634 self._do_read() 

635 

636 # Shutdown flow 

637 

638 def _start_shutdown(self): 

639 if ( 

640 self._state in ( 

641 SSLProtocolState.FLUSHING, 

642 SSLProtocolState.SHUTDOWN, 

643 SSLProtocolState.UNWRAPPED 

644 ) 

645 ): 

646 return 

647 if self._app_transport is not None: 647 ↛ 649line 647 didn't jump to line 649 because the condition on line 647 was always true

648 self._app_transport._closed = True 

649 if self._state == SSLProtocolState.DO_HANDSHAKE: 

650 self._abort(None) 

651 else: 

652 self._set_state(SSLProtocolState.FLUSHING) 

653 self._shutdown_timeout_handle = self._loop.call_later( 

654 self._ssl_shutdown_timeout, 

655 self._check_shutdown_timeout 

656 ) 

657 self._do_flush() 

658 

659 def _check_shutdown_timeout(self): 

660 if ( 660 ↛ exitline 660 didn't return from function '_check_shutdown_timeout' because the condition on line 660 was always true

661 self._state in ( 

662 SSLProtocolState.FLUSHING, 

663 SSLProtocolState.SHUTDOWN 

664 ) 

665 ): 

666 self._transport._force_close( 

667 exceptions.TimeoutError('SSL shutdown timed out')) 

668 

669 def _do_flush(self): 

670 self._do_read() 

671 self._set_state(SSLProtocolState.SHUTDOWN) 

672 self._do_shutdown() 

673 

674 def _do_shutdown(self): 

675 try: 

676 if not self._eof_received: 

677 self._sslobj.unwrap() 

678 except SSLAgainErrors: 

679 self._process_outgoing() 

680 except ssl.SSLError as exc: 

681 # gh-98078: send what OpenSSL left in the outgoing BIO, e.g. 

682 # the close_notify alert, to the peer before closing (see 

683 # _on_handshake_complete()). 

684 self._process_outgoing() 

685 self._on_shutdown_complete(exc) 

686 else: 

687 self._process_outgoing() 

688 self._call_eof_received() 

689 self._on_shutdown_complete(None) 

690 

691 def _on_shutdown_complete(self, shutdown_exc): 

692 if shutdown_exc: 

693 if self._shutdown_timeout_handle is not None: 693 ↛ 696line 693 didn't jump to line 696 because the condition on line 693 was always true

694 self._shutdown_timeout_handle.cancel() 

695 self._shutdown_timeout_handle = None 

696 self._fatal_error(shutdown_exc) 

697 else: 

698 # transport.close() flushes its write buffer first, which a peer 

699 # that does not read can delay forever. Keep the timeout (or 

700 # start it, if the peer initiated the shutdown) until 

701 # connection_lost(). 

702 if self._shutdown_timeout_handle is None: 

703 self._shutdown_timeout_handle = self._loop.call_later( 

704 self._ssl_shutdown_timeout, 

705 self._check_shutdown_timeout 

706 ) 

707 self._loop.call_soon(self._transport.close) 

708 

709 def _abort(self, exc): 

710 self._set_state(SSLProtocolState.UNWRAPPED) 

711 if self._transport is not None: 

712 self._transport._force_close(exc) 

713 

714 # Outgoing flow 

715 

716 def _write_appdata(self, list_of_data): 

717 if ( 

718 self._state in ( 

719 SSLProtocolState.FLUSHING, 

720 SSLProtocolState.SHUTDOWN, 

721 SSLProtocolState.UNWRAPPED 

722 ) 

723 ): 

724 if self._conn_lost >= constants.LOG_THRESHOLD_FOR_CONNLOST_WRITES: 724 ↛ 725line 724 didn't jump to line 725 because the condition on line 724 was never true

725 logger.warning('SSL connection is closed') 

726 self._conn_lost += 1 

727 return 

728 

729 for data in list_of_data: 

730 self._write_backlog.append(data) 

731 self._write_buffer_size += len(data) 

732 

733 try: 

734 if self._state == SSLProtocolState.WRAPPED: 734 ↛ exitline 734 didn't return from function '_write_appdata' because the condition on line 734 was always true

735 self._do_write() 

736 

737 except Exception as ex: 

738 self._fatal_error(ex, 'Fatal error on SSL protocol') 

739 

740 def _do_write(self): 

741 pending = self._outgoing.pending 

742 try: 

743 while self._write_backlog: 

744 data = self._write_backlog[0] 

745 count = self._sslobj.write(data) 

746 data_len = len(data) 

747 if count < data_len: 747 ↛ 748line 747 didn't jump to line 748 because the condition on line 747 was never true

748 self._write_backlog[0] = data[count:] 

749 self._write_buffer_size -= count 

750 else: 

751 del self._write_backlog[0] 

752 self._write_buffer_size -= data_len 

753 except SSLAgainErrors: 

754 pass 

755 self._outgoing_app_size += self._outgoing.pending - pending 

756 self._process_outgoing() 

757 

758 def _process_outgoing(self): 

759 if not self._ssl_writing_paused: 

760 data = self._outgoing.read() 

761 self._outgoing_app_size = 0 

762 if len(data): 

763 self._transport.write(data) 

764 self._control_app_writing() 

765 

766 # Incoming flow 

767 

768 def _do_read(self): 

769 if ( 

770 self._state not in ( 

771 SSLProtocolState.WRAPPED, 

772 SSLProtocolState.FLUSHING, 

773 ) 

774 ): 

775 return 

776 try: 

777 if not self._app_reading_paused: 

778 if self._app_protocol_is_buffer: 

779 self._do_read__buffered() 

780 else: 

781 self._do_read__copied() 

782 if self._write_backlog: 

783 self._do_write() 

784 else: 

785 self._process_outgoing() 

786 self._control_ssl_reading() 

787 except ssl.SSLError as ex: 

788 # gh-98078: send the fatal TLS alert left in the outgoing 

789 # BIO to the peer (see _on_handshake_complete()). 

790 self._process_outgoing() 

791 self._fatal_error(ex, 'Fatal error on SSL protocol') 

792 except Exception as ex: 

793 self._fatal_error(ex, 'Fatal error on SSL protocol') 

794 

795 def _do_read__buffered(self): 

796 offset = 0 

797 count = 1 

798 

799 buf = self._app_protocol_get_buffer(self._get_read_buffer_size()) 

800 wants = len(buf) 

801 

802 try: 

803 count = self._sslobj.read(wants, buf) 

804 

805 if count > 0: 805 ↛ 817line 805 didn't jump to line 817 because the condition on line 805 was always true

806 offset = count 

807 while offset < wants: 807 ↛ 808line 807 didn't jump to line 808 because the condition on line 807 was never true

808 count = self._sslobj.read(wants - offset, buf[offset:]) 

809 if count > 0: 

810 offset += count 

811 else: 

812 break 

813 else: 

814 self._loop.call_soon(self._do_read) 

815 except SSLAgainErrors: 

816 pass 

817 if offset > 0: 

818 self._app_protocol_buffer_updated(offset) 

819 if not count: 819 ↛ 821line 819 didn't jump to line 821 because the condition on line 819 was never true

820 # close_notify 

821 self._call_eof_received() 

822 self._start_shutdown() 

823 

824 def _do_read__copied(self): 

825 chunk = b'1' 

826 zero = True 

827 one = False 

828 

829 try: 

830 while True: 

831 chunk = self._sslobj.read(self.max_size) 

832 if not chunk: 

833 break 

834 if zero: 

835 zero = False 

836 one = True 

837 first = chunk 

838 elif one: 

839 one = False 

840 data = [first, chunk] 

841 else: 

842 data.append(chunk) 

843 except SSLAgainErrors: 

844 pass 

845 if one: 

846 self._app_protocol.data_received(first) 

847 elif not zero: 

848 self._app_protocol.data_received(b''.join(data)) 

849 if not chunk: 

850 # close_notify 

851 self._call_eof_received() 

852 self._start_shutdown() 

853 

854 def _call_eof_received(self): 

855 try: 

856 if self._app_state == AppProtocolState.STATE_CON_MADE: 

857 self._app_state = AppProtocolState.STATE_EOF 

858 keep_open = self._app_protocol.eof_received() 

859 if keep_open: 859 ↛ 860line 859 didn't jump to line 860 because the condition on line 859 was never true

860 logger.warning('returning true from eof_received() ' 

861 'has no effect when using ssl') 

862 except (KeyboardInterrupt, SystemExit): 

863 raise 

864 except BaseException as ex: 

865 self._fatal_error(ex, 'Error calling eof_received()') 

866 

867 # Flow control for writes from APP socket 

868 

869 def _control_app_writing(self): 

870 size = self._get_write_buffer_size() 

871 if size >= self._outgoing_high_water and not self._app_writing_paused: 

872 self._app_writing_paused = True 

873 try: 

874 self._app_protocol.pause_writing() 

875 except (KeyboardInterrupt, SystemExit): 

876 raise 

877 except BaseException as exc: 

878 self._loop.call_exception_handler({ 

879 'message': 'protocol.pause_writing() failed', 

880 'exception': exc, 

881 'transport': self._app_transport, 

882 'protocol': self, 

883 }) 

884 elif size <= self._outgoing_low_water and self._app_writing_paused: 

885 self._app_writing_paused = False 

886 try: 

887 self._app_protocol.resume_writing() 

888 except (KeyboardInterrupt, SystemExit): 

889 raise 

890 except BaseException as exc: 

891 self._loop.call_exception_handler({ 

892 'message': 'protocol.resume_writing() failed', 

893 'exception': exc, 

894 'transport': self._app_transport, 

895 'protocol': self, 

896 }) 

897 

898 def _get_write_buffer_size(self): 

899 return self._outgoing.pending + self._write_buffer_size 

900 

901 def _set_write_buffer_limits(self, high=None, low=None): 

902 high, low = add_flowcontrol_defaults( 

903 high, low, constants.FLOW_CONTROL_HIGH_WATER_SSL_WRITE) 

904 self._outgoing_high_water = high 

905 self._outgoing_low_water = low 

906 

907 # Flow control for reads to APP socket 

908 

909 def _pause_reading(self): 

910 self._app_reading_paused = True 

911 

912 def _resume_reading(self): 

913 if self._app_reading_paused: 913 ↛ exitline 913 didn't return from function '_resume_reading' because the condition on line 913 was always true

914 self._app_reading_paused = False 

915 

916 def resume(): 

917 if self._state == SSLProtocolState.WRAPPED: 

918 self._do_read() 

919 elif self._state == SSLProtocolState.FLUSHING: 

920 self._do_flush() 

921 elif self._state == SSLProtocolState.SHUTDOWN: 

922 self._do_shutdown() 

923 self._loop.call_soon(resume) 

924 

925 # Flow control for reads from SSL socket 

926 

927 def _control_ssl_reading(self): 

928 # Also count the records OpenSSL writes on its own while reading 

929 # (alerts, KeyUpdate replies, ...) which the peer does not read: 

930 # the app is not involved in producing them, so pausing it does 

931 # not bound them; only pausing reads from the peer does. Whatever 

932 # was produced while writing app data (_outgoing_app_size) is left 

933 # out so that two peers which both write before they read cannot 

934 # deadlock. 

935 size = (self._get_read_buffer_size() + 

936 self._outgoing.pending - self._outgoing_app_size) 

937 if size >= self._incoming_high_water and not self._ssl_reading_paused: 

938 self._ssl_reading_paused = True 

939 self._transport.pause_reading() 

940 elif size <= self._incoming_low_water and self._ssl_reading_paused: 

941 self._ssl_reading_paused = False 

942 self._transport.resume_reading() 

943 

944 def _set_read_buffer_limits(self, high=None, low=None): 

945 high, low = add_flowcontrol_defaults( 

946 high, low, constants.FLOW_CONTROL_HIGH_WATER_SSL_READ) 

947 self._incoming_high_water = high 

948 self._incoming_low_water = low 

949 

950 def _get_read_buffer_size(self): 

951 return self._incoming.pending 

952 

953 # Flow control for writes to SSL socket 

954 

955 def pause_writing(self): 

956 """Called when the low-level transport's buffer goes over 

957 the high-water mark. 

958 """ 

959 assert not self._ssl_writing_paused 

960 self._ssl_writing_paused = True 

961 

962 def resume_writing(self): 

963 """Called when the low-level transport's buffer drains below 

964 the low-water mark. 

965 """ 

966 assert self._ssl_writing_paused 

967 self._ssl_writing_paused = False 

968 self._process_outgoing() 

969 self._control_ssl_reading() 

970 

971 def _fatal_error(self, exc, message='Fatal error on transport'): 

972 if self._transport: 

973 self._transport._force_close(exc) 

974 

975 if isinstance(exc, OSError): 

976 if self._loop.get_debug(): 976 ↛ 977line 976 didn't jump to line 977 because the condition on line 976 was never true

977 logger.debug("%r: %s", self, message, exc_info=True) 

978 elif not isinstance(exc, exceptions.CancelledError): 978 ↛ exitline 978 didn't return from function '_fatal_error' because the condition on line 978 was always true

979 self._loop.call_exception_handler({ 

980 'message': message, 

981 'exception': exc, 

982 'transport': self._transport, 

983 'protocol': self, 

984 })