本文へスキップ
ecdsa.com
OpenSSL 3OpenSSL CLI

エラーメッセージ

error:0A000126:SSL routines::unexpected eof while reading

別の表記

  • SSL routines::unexpected eof while reading

openssl CLI は「<thread id>:error:0A000126:SSL routines::unexpected eof while reading:<source>」と出力します。curl はこれを「curl: (56) OpenSSL SSL_read: … unexpected eof while reading, errno 0」として、Python は「[SSL: UNEXPECTED_EOF_WHILE_READING] EOF occurred in violation of protocol」として表示します。

OpenSSL 3 — SSL_R_UNEXPECTED_EOF_WHILE_READING (peer closed the TCP connection without close_notify)

このエラーの意味

OpenSSL がまだデータを待っている間に(ハンドシェイク中またはストリームの途中で)、ピアが TLS の close_notify アラートを先に送らずに TCP 接続を閉じました。通知なしの切断は、攻撃者がストリームを切り詰めた場合と区別できないため、OpenSSL 3 はこれをエラーとして報告します。OpenSSL 1.1.1 では、システムコールのエラー経路を通じてしか報告されませんでした。

発生する理由

修正方法

  1. 1.

    接続がいつ終わるかを確認する

    証明書が表示される前にエラーが出る場合は、サーバーまたはネットワークがハンドシェイクを拒否しています。データの後に出る場合は、ピアまたはミドルボックスがストリームを切断しています。

    bash
    # During the handshake (no certificate printed) -> the server or network refused the connection
    # After data was exchanged -> the peer stopped mid-stream
    echo | openssl s_client -connect api.example.com:443 -servername api.example.com -state 2>&1 | grep -E 'SSL_connect|error'
  2. 2.

    管理下にある古いピア:SSL_OP_IGNORE_UNEXPECTED_EOF

    OpenSSL 3 では、通知なしの切断を通常のストリームの終わりとして扱えます。このオプションは切り詰めの検出を無効にするため、プロトコル自体が切り詰めを検出できる(たとえば Content-Length)特定の古いピアに限って使ってください。

    python
    # Only for peers you control that close without close_notify (legacy embedded servers):
    # OpenSSL 3 option SSL_OP_IGNORE_UNEXPECTED_EOF, exposed by Python 3.10+ as ssl.OP_IGNORE_UNEXPECTED_EOF
    import ssl
    ctx = ssl.create_default_context()
    ctx.options |= ssl.OP_IGNORE_UNEXPECTED_EOF   # truncation is then no longer detected
  3. 3.

    ピアを修正する:TLS を正しくシャットダウンする

    接続を閉じる前に close_notify を送信すれば、すべてのクライアントでこのエラーが解消されます。

    text
    Server code: call SSL_shutdown() (or the library's close/unwrap) before closing the socket,
    and delimit HTTP responses with Content-Length or chunked encoding.

関連するエラー

署名エラーデータベースをすべて見る