| 1 |
""" |
| 2 |
TLS and STARTTLS handling. |
| 3 |
""" |
| 4 |
|
| 5 |
import copy |
| 6 |
import ssl |
| 7 |
|
| 8 |
import pytest |
| 9 |
|
| 10 |
from aiosmtplib import ( |
| 11 |
SMTP, |
| 12 |
SMTPConnectError, |
| 13 |
SMTPException, |
| 14 |
SMTPResponseException, |
| 15 |
SMTPServerDisconnected, |
| 16 |
SMTPStatus, |
| 17 |
) |
| 18 |
|
| 19 |
from .smtpd import ( |
| 20 |
mock_response_ehlo_minimal, |
| 21 |
mock_response_tls_not_available, |
| 22 |
mock_response_tls_ready_disconnect, |
| 23 |
mock_response_unrecognized_command, |
| 24 |
) |
| 25 |
|
| 26 |
|
| 27 |
@pytest.mark.smtpd_options(tls=True) |
| 28 |
async def test_tls_connection(smtp_client: SMTP) -> None: |
| 29 |
""" |
| 30 |
Use an explicit connect/quit here, as other tests use the context manager. |
| 31 |
""" |
| 32 |
await smtp_client.connect(use_tls=True) |
| 33 |
assert smtp_client.is_connected |
| 34 |
|
| 35 |
await smtp_client.quit() |
| 36 |
assert not smtp_client.is_connected |
| 37 |
|
| 38 |
|
| 39 |
@pytest.mark.smtpd_options(tls=False) |
| 40 |
async def test_starttls(smtp_client: SMTP) -> None: |
| 41 |
async with smtp_client: |
| 42 |
response = await smtp_client.starttls() |
| 43 |
|
| 44 |
assert response.code == SMTPStatus.ready |
| 45 |
|
| 46 |
# Make sure our state has been cleared |
| 47 |
assert not smtp_client.esmtp_extensions |
| 48 |
assert not smtp_client.supported_auth_methods |
| 49 |
assert not smtp_client.supports_esmtp |
| 50 |
|
| 51 |
# Make sure our connection was actually upgraded. ssl protocol transport is |
| 52 |
# private in UVloop, so just check the class name. |
| 53 |
assert "SSL" in type(smtp_client.transport).__name__ |
| 54 |
|
| 55 |
response = await smtp_client.ehlo() |
| 56 |
assert response.code == SMTPStatus.completed |
| 57 |
|
| 58 |
|
| 59 |
async def test_starttls_init_kwarg( |
| 60 |
hostname: str, smtpd_server_port: int, client_tls_context: ssl.SSLContext |
| 61 |
) -> None: |
| 62 |
smtp_client = SMTP( |
| 63 |
hostname=hostname, |
| 64 |
port=smtpd_server_port, |
| 65 |
start_tls=True, |
| 66 |
tls_context=client_tls_context, |
| 67 |
timeout=1.0, |
| 68 |
) |
| 69 |
|
| 70 |
async with smtp_client: |
| 71 |
# Make sure our connection was actually upgraded. ssl protocol transport is |
| 72 |
# private in UVloop, so just check the class name. |
| 73 |
assert "SSL" in type(smtp_client.transport).__name__ |
| 74 |
|
| 75 |
|
| 76 |
@pytest.mark.smtpd_options(tls=False) |
| 77 |
async def test_starttls_connect_kwarg(smtp_client: SMTP) -> None: |
| 78 |
await smtp_client.connect(start_tls=True) |
| 79 |
|
| 80 |
# Make sure our connection was actually upgraded. ssl protocol transport is |
| 81 |
# private in UVloop, so just check the class name. |
| 82 |
assert "SSL" in type(smtp_client.transport).__name__ |
| 83 |
|
| 84 |
await smtp_client.quit() |
| 85 |
|
| 86 |
|
| 87 |
async def test_starttls_auto( |
| 88 |
hostname: str, smtpd_server_port: int, client_tls_context: ssl.SSLContext |
| 89 |
) -> None: |
| 90 |
smtp_client = SMTP( |
| 91 |
hostname=hostname, |
| 92 |
port=smtpd_server_port, |
| 93 |
start_tls=None, |
| 94 |
tls_context=client_tls_context, |
| 95 |
timeout=1.0, |
| 96 |
) |
| 97 |
|
| 98 |
async with smtp_client: |
| 99 |
# Make sure our connection was actually upgraded. ssl protocol transport is |
| 100 |
# private in UVloop, so just check the class name. |
| 101 |
assert "SSL" in type(smtp_client.transport).__name__ |
| 102 |
|
| 103 |
|
| 104 |
async def test_starttls_auto_connect_kwarg( |
| 105 |
hostname: str, |
| 106 |
smtpd_server_port: int, |
| 107 |
client_tls_context: ssl.SSLContext, |
| 108 |
) -> None: |
| 109 |
smtp_client = SMTP( |
| 110 |
hostname=hostname, |
| 111 |
port=smtpd_server_port, |
| 112 |
start_tls=False, |
| 113 |
tls_context=client_tls_context, |
| 114 |
timeout=1.0, |
| 115 |
) |
| 116 |
|
| 117 |
await smtp_client.connect(start_tls=None) |
| 118 |
|
| 119 |
# Make sure our connection was actually upgraded. ssl protocol transport is |
| 120 |
# private in UVloop, so just check the class name. |
| 121 |
assert "SSL" in type(smtp_client.transport).__name__ |
| 122 |
|
| 123 |
await smtp_client.quit() |
| 124 |
|
| 125 |
|
| 126 |
@pytest.mark.smtp_client_options(start_tls=None) |
| 127 |
@pytest.mark.smtpd_options(tls=False) |
| 128 |
@pytest.mark.smtpd_mocks(smtp_EHLO=mock_response_ehlo_minimal) |
| 129 |
async def test_starttls_auto_connect_not_supported(smtp_client: SMTP) -> None: |
| 130 |
async with smtp_client: |
| 131 |
await smtp_client.ehlo() |
| 132 |
|
| 133 |
# Make sure our connection was nul upgraded. ssl protocol transport is |
| 134 |
# private in UVloop, so just check the class name. |
| 135 |
assert "SSL" not in type(smtp_client.transport).__name__ |
| 136 |
|
| 137 |
|
| 138 |
@pytest.mark.smtpd_options(tls=False) |
| 139 |
async def test_starttls_with_explicit_server_hostname( |
| 140 |
smtp_client: SMTP, |
| 141 |
hostname: str, |
| 142 |
) -> None: |
| 143 |
async with smtp_client: |
| 144 |
await smtp_client.ehlo() |
| 145 |
|
| 146 |
await smtp_client.starttls(server_hostname=hostname) |
| 147 |
|
| 148 |
|
| 149 |
@pytest.mark.smtpd_options(tls=False) |
| 150 |
@pytest.mark.smtpd_mocks(smtp_EHLO=mock_response_ehlo_minimal) |
| 151 |
async def test_starttls_not_supported(smtp_client: SMTP) -> None: |
| 152 |
async with smtp_client: |
| 153 |
await smtp_client.ehlo() |
| 154 |
|
| 155 |
with pytest.raises(SMTPException, match="STARTTLS extension not supported"): |
| 156 |
await smtp_client.starttls() |
| 157 |
|
| 158 |
|
| 159 |
@pytest.mark.smtpd_options(tls=False) |
| 160 |
@pytest.mark.smtpd_mocks(smtp_STARTTLS=mock_response_tls_not_available) |
| 161 |
async def test_starttls_advertised_but_not_supported(smtp_client: SMTP) -> None: |
| 162 |
async with smtp_client: |
| 163 |
await smtp_client.ehlo() |
| 164 |
|
| 165 |
with pytest.raises(SMTPException): |
| 166 |
await smtp_client.starttls() |
| 167 |
|
| 168 |
|
| 169 |
@pytest.mark.smtpd_options(tls=False) |
| 170 |
@pytest.mark.smtpd_mocks(smtp_STARTTLS=mock_response_tls_ready_disconnect) |
| 171 |
async def test_starttls_disconnect_before_upgrade(smtp_client: SMTP) -> None: |
| 172 |
async with smtp_client: |
| 173 |
with pytest.raises(SMTPServerDisconnected): |
| 174 |
await smtp_client.starttls() |
| 175 |
|
| 176 |
|
| 177 |
@pytest.mark.smtpd_options(tls=False) |
| 178 |
@pytest.mark.smtpd_mocks(smtp_STARTTLS=mock_response_unrecognized_command) |
| 179 |
async def test_starttls_invalid_responses(smtp_client: SMTP) -> None: |
| 180 |
async with smtp_client: |
| 181 |
await smtp_client.ehlo() |
| 182 |
|
| 183 |
old_extensions = copy.copy(smtp_client.esmtp_extensions) |
| 184 |
|
| 185 |
with pytest.raises(SMTPResponseException) as exception_info: |
| 186 |
await smtp_client.starttls() |
| 187 |
|
| 188 |
assert exception_info.value.code == SMTPStatus.unrecognized_command |
| 189 |
# Make sure our state has been _not_ been cleared |
| 190 |
assert smtp_client.esmtp_extensions == old_extensions |
| 191 |
assert smtp_client.supports_esmtp is True |
| 192 |
|
| 193 |
# Make sure our connection was not upgraded. ssl protocol transport is |
| 194 |
# private in UVloop, so just check the class name. |
| 195 |
assert "SSL" not in type(smtp_client.transport).__name__ |
| 196 |
|
| 197 |
|
| 198 |
@pytest.mark.smtpd_options(tls=False) |
| 199 |
async def test_starttls_with_client_cert( |
| 200 |
hostname: str, |
| 201 |
smtpd_server_port: int, |
| 202 |
ca_cert_path: str, |
| 203 |
valid_cert_path: str, |
| 204 |
valid_key_path: str, |
| 205 |
) -> None: |
| 206 |
smtp_client = SMTP( |
| 207 |
hostname=hostname, port=smtpd_server_port, start_tls=False, timeout=1.0 |
| 208 |
) |
| 209 |
async with smtp_client: |
| 210 |
response = await smtp_client.starttls( |
| 211 |
client_cert=valid_cert_path, |
| 212 |
client_key=valid_key_path, |
| 213 |
cert_bundle=ca_cert_path, |
| 214 |
) |
| 215 |
|
| 216 |
assert response.code == SMTPStatus.ready |
| 217 |
assert smtp_client.client_cert == valid_cert_path |
| 218 |
assert smtp_client.client_key == valid_key_path |
| 219 |
assert smtp_client.cert_bundle == ca_cert_path |
| 220 |
|
| 221 |
|
| 222 |
@pytest.mark.smtpd_options(tls=False) |
| 223 |
async def test_starttls_with_invalid_client_cert( |
| 224 |
hostname: str, |
| 225 |
smtpd_server_port: int, |
| 226 |
invalid_cert_path: str, |
| 227 |
invalid_key_path: str, |
| 228 |
) -> None: |
| 229 |
smtp_client = SMTP( |
| 230 |
hostname=hostname, port=smtpd_server_port, start_tls=False, timeout=1.0 |
| 231 |
) |
| 232 |
async with smtp_client: |
| 233 |
with pytest.raises(ssl.SSLError): |
| 234 |
await smtp_client.starttls( |
| 235 |
client_cert=invalid_cert_path, |
| 236 |
client_key=invalid_key_path, |
| 237 |
cert_bundle=invalid_cert_path, |
| 238 |
) |
| 239 |
|
| 240 |
|
| 241 |
@pytest.mark.smtpd_options(tls=False) |
| 242 |
async def test_starttls_with_invalid_client_cert_no_validate( |
| 243 |
hostname: str, |
| 244 |
smtpd_server_port: int, |
| 245 |
invalid_cert_path: str, |
| 246 |
invalid_key_path: str, |
| 247 |
) -> None: |
| 248 |
smtp_client = SMTP( |
| 249 |
hostname=hostname, port=smtpd_server_port, start_tls=False, timeout=1.0 |
| 250 |
) |
| 251 |
async with smtp_client: |
| 252 |
response = await smtp_client.starttls( |
| 253 |
client_cert=invalid_cert_path, |
| 254 |
client_key=invalid_key_path, |
| 255 |
cert_bundle=invalid_cert_path, |
| 256 |
validate_certs=False, |
| 257 |
timeout=1.0, |
| 258 |
) |
| 259 |
|
| 260 |
assert response.code == SMTPStatus.ready |
| 261 |
assert smtp_client.client_cert == invalid_cert_path |
| 262 |
assert smtp_client.client_key == invalid_key_path |
| 263 |
assert smtp_client.cert_bundle == invalid_cert_path |
| 264 |
|
| 265 |
|
| 266 |
@pytest.mark.smtpd_options(tls=False) |
| 267 |
async def test_starttls_cert_error( |
| 268 |
hostname: str, |
| 269 |
smtpd_server_port: int, |
| 270 |
unknown_client_tls_context: ssl.SSLContext, |
| 271 |
) -> None: |
| 272 |
smtp_client = SMTP( |
| 273 |
hostname=hostname, |
| 274 |
port=smtpd_server_port, |
| 275 |
start_tls=False, |
| 276 |
tls_context=unknown_client_tls_context, |
| 277 |
timeout=1.0, |
| 278 |
) |
| 279 |
async with smtp_client: |
| 280 |
with pytest.raises(ssl.SSLError): |
| 281 |
await smtp_client.starttls() |
| 282 |
|
| 283 |
|
| 284 |
@pytest.mark.smtpd_options(tls=False) |
| 285 |
async def test_starttls_already_upgraded_error( |
| 286 |
hostname: str, |
| 287 |
smtpd_server_port: int, |
| 288 |
client_tls_context: ssl.SSLContext, |
| 289 |
) -> None: |
| 290 |
smtp_client = SMTP( |
| 291 |
hostname=hostname, |
| 292 |
port=smtpd_server_port, |
| 293 |
tls_context=client_tls_context, |
| 294 |
timeout=1.0, |
| 295 |
) |
| 296 |
async with smtp_client: |
| 297 |
with pytest.raises(SMTPException, match="Connection already using TLS"): |
| 298 |
await smtp_client.starttls() |
| 299 |
|
| 300 |
|
| 301 |
@pytest.mark.smtpd_options(tls=False) |
| 302 |
async def test_starttls_cert_no_validate(hostname: str, smtpd_server_port: int) -> None: |
| 303 |
smtp_client = SMTP( |
| 304 |
hostname=hostname, |
| 305 |
port=smtpd_server_port, |
| 306 |
start_tls=False, |
| 307 |
validate_certs=False, |
| 308 |
timeout=1.0, |
| 309 |
) |
| 310 |
async with smtp_client: |
| 311 |
response = await smtp_client.starttls() |
| 312 |
|
| 313 |
assert response.code == SMTPStatus.ready |
| 314 |
|
| 315 |
|
| 316 |
@pytest.mark.smtpd_options(tls=True) |
| 317 |
@pytest.mark.smtp_client_options(use_tls=True) |
| 318 |
async def test_tls_get_transport_info( |
| 319 |
smtp_client: SMTP, smtpd_server_port: int |
| 320 |
) -> None: |
| 321 |
async with smtp_client: |
| 322 |
compression = smtp_client.get_transport_info("compression") |
| 323 |
assert compression is None # Compression is not used here |
| 324 |
|
| 325 |
peername = smtp_client.get_transport_info("peername") |
| 326 |
assert peername[0] in ("127.0.0.1", "::1") # IP v4 and 6 |
| 327 |
assert peername[1] == smtpd_server_port |
| 328 |
|
| 329 |
sock = smtp_client.get_transport_info("socket") |
| 330 |
assert sock is not None |
| 331 |
|
| 332 |
sockname = smtp_client.get_transport_info("sockname") |
| 333 |
assert sockname is not None |
| 334 |
|
| 335 |
cipher = smtp_client.get_transport_info("cipher") |
| 336 |
assert cipher is not None |
| 337 |
|
| 338 |
peercert = smtp_client.get_transport_info("peercert") |
| 339 |
assert peercert is not None |
| 340 |
|
| 341 |
sslcontext = smtp_client.get_transport_info("sslcontext") |
| 342 |
assert sslcontext is not None |
| 343 |
|
| 344 |
sslobj = smtp_client.get_transport_info("ssl_object") |
| 345 |
assert sslobj is not None |
| 346 |
|
| 347 |
|
| 348 |
@pytest.mark.smtpd_options(tls=False) |
| 349 |
async def test_tls_smtp_connect_to_non_tls_server( |
| 350 |
smtp_client: SMTP, |
| 351 |
smtpd_server_port: int, |
| 352 |
) -> None: |
| 353 |
with pytest.raises(SMTPConnectError): |
| 354 |
await smtp_client.connect(use_tls=True, port=smtpd_server_port) |
| 355 |
assert not smtp_client.is_connected |
| 356 |
|
| 357 |
|
| 358 |
@pytest.mark.smtpd_options(tls=True) |
| 359 |
async def test_tls_connection_with_existing_sslcontext( |
| 360 |
smtp_client: SMTP, |
| 361 |
client_tls_context: ssl.SSLContext, |
| 362 |
) -> None: |
| 363 |
await smtp_client.connect(use_tls=True, tls_context=client_tls_context) |
| 364 |
assert smtp_client.is_connected |
| 365 |
|
| 366 |
assert smtp_client.tls_context is client_tls_context |
| 367 |
|
| 368 |
await smtp_client.quit() |
| 369 |
assert not smtp_client.is_connected |
| 370 |
|
| 371 |
|
| 372 |
@pytest.mark.smtpd_options(tls=True) |
| 373 |
async def test_tls_connection_with_client_cert( |
| 374 |
hostname: str, |
| 375 |
smtpd_server_port: int, |
| 376 |
ca_cert_path: str, |
| 377 |
valid_cert_path: str, |
| 378 |
valid_key_path: str, |
| 379 |
) -> None: |
| 380 |
smtp_client = SMTP( |
| 381 |
hostname=hostname, port=smtpd_server_port, use_tls=True, timeout=1.0 |
| 382 |
) |
| 383 |
await smtp_client.connect( |
| 384 |
hostname=hostname, |
| 385 |
client_cert=valid_cert_path, |
| 386 |
client_key=valid_key_path, |
| 387 |
cert_bundle=ca_cert_path, |
| 388 |
) |
| 389 |
assert smtp_client.is_connected |
| 390 |
|
| 391 |
await smtp_client.quit() |
| 392 |
assert not smtp_client.is_connected |
| 393 |
|
| 394 |
|
| 395 |
@pytest.mark.smtpd_options(tls=True) |
| 396 |
async def test_tls_connection_with_cert_error( |
| 397 |
hostname: str, |
| 398 |
smtpd_server_port: int, |
| 399 |
) -> None: |
| 400 |
smtp_client = SMTP( |
| 401 |
hostname=hostname, port=smtpd_server_port, use_tls=True, timeout=1.0 |
| 402 |
) |
| 403 |
|
| 404 |
with pytest.raises(SMTPConnectError) as exception_info: |
| 405 |
await smtp_client.connect() |
| 406 |
|
| 407 |
assert "CERTIFICATE" in str(exception_info.value).upper() |
| 408 |
|
| 409 |
|
| 410 |
async def test_starttls_when_disconnected() -> None: |
| 411 |
client = SMTP(timeout=1.0) |
| 412 |
|
| 413 |
with pytest.raises(SMTPServerDisconnected): |
| 414 |
await client.starttls() |
| 415 |
|
| 416 |
|
| 417 |
@pytest.mark.smtpd_options(tls=False) |
| 418 |
async def test_starttls_sets_protocol_over_ssl(smtp_client: SMTP) -> None: |
| 419 |
async with smtp_client: |
| 420 |
assert smtp_client.protocol is not None |
| 421 |
assert not smtp_client.protocol._over_ssl |
| 422 |
|
| 423 |
await smtp_client.starttls() |
| 424 |
|
| 425 |
assert smtp_client.protocol._over_ssl |
| 426 |
with pytest.raises(RuntimeError, match="Already using TLS"): |
| 427 |
await smtp_client.protocol.start_tls(smtp_client.tls_context) |