aiohttp HTTPS 연결 실패-TLS cipher 협상 문제
Context
특정 서버에 접속해서 이미지 url을 다운받는 로직에서 반복적으로 연결 실패. error msg: Connection reset by peer
What I Learned
A cipher suite is a set of algorithms that help secure a network connection. Suites typically use Transport Layer Security (TLS) or its deprecated predecessor Secure Socket Layer (SSL) as their protocol.
TLS에는 3가지 암호를 협상함: 키 교환 (Key Exchange), 대칭 암호 (Bulk Encryption), 무결성 (MAC / AEAD)
원인 및 해결
- Python의 기본 aiohttp의 디폴트 ssl context 에서 구형 cipher 차단.
- 문제 생긴 서버의 cipher suite 확인해보니 구형
- 이로 인해 TLS handshake 단계에서 cipher 협상이 실패하고 연결이 reset
- 해결: 연결 실패 시 fallback으로 SSLContext 에서 보안레벨을 낮춰 레거시 cipher를 허용
Code
ctx = ssl.create_default_context()
ctx.set_ciphers("DEFAULT@SECLEVEL=1")
Note
이전에 있었던 SSL 연결 문제랑은 원인이 달랐음. 이전 문제는 인증서 체인 문제로 우리가 해결할 수 없었지만, 이번 이슈는 fallback 로직을 통해 접근 가능했음. 문제 원인 파악을 확실히 할 것.