Skip to content

fix(Tornado): Fix SSL client certificate authentication with M2Crypto - #8790

Closed
fstagni wants to merge 1 commit into
DIRACGrid:integrationfrom
fstagni:m2crypto50_tornado
Closed

fstagni wants to merge 1 commit into
DIRACGrid:integrationfrom
fstagni:m2crypto50_tornado

Conversation

@fstagni

@fstagni fstagni commented Sep 15, 2026

Copy link
Copy Markdown
Contributor

The Tornado server was failing to extract client certificates during TLS handshake, resulting in 401 Unauthorized errors. The root causes were:

  1. M2IOStream.initialize imported m2_wrap_socket at module load time, so late patching didn't affect the bound reference
  2. The peer certificate was lost when close_fd() cleared the socket after handshake completion
  3. read_from_fd returning 0 was treated as EOF, causing premature stream closure

Fixes applied:

  • Replace M2IOStream.initialize with version that imports m2_wrap_socket at runtime
  • Patch _do_ssl_handshake to store peer cert/chain before socket cleanup
  • Patch read_from_fd to return None instead of 0 to prevent premature closure
  • Patch get_ssl_certificate/get_ssl_certificate_chain to use stored cert
  • Patch HTTPServerRequest to delegate to stream's certificate methods

BEGINRELEASENOTES
Thank you for writing the text to appear in the release notes. It will show up
exactly as it appears between the two bold lines

Please follow the template:
*Subsystem
NEW/CHANGE/FIX: explanation

For examples look into release.notes

ENDRELEASENOTES

The Tornado server was failing to extract client certificates during TLS
handshake, resulting in 401 Unauthorized errors. The root causes were:

1. M2IOStream.initialize imported m2_wrap_socket at module load time,
   so late patching didn't affect the bound reference
2. The peer certificate was lost when close_fd() cleared the socket
   after handshake completion
3. read_from_fd returning 0 was treated as EOF, causing premature
   stream closure

Fixes applied:
- Replace M2IOStream.initialize with version that imports m2_wrap_socket
  at runtime
- Patch _do_ssl_handshake to store peer cert/chain before socket cleanup
- Patch read_from_fd to return None instead of 0 to prevent premature closure
- Patch get_ssl_certificate/get_ssl_certificate_chain to use stored cert
- Patch HTTPServerRequest to delegate to stream's certificate methods
@chrisburr

Copy link
Copy Markdown
Member

This definitely isn't the solution. We'll need another tornado_m2crypto release for the real fix.

@chrisburr chrisburr closed this Sep 15, 2026
@chrisburr

Copy link
Copy Markdown
Member

I think the real fix is DIRACGrid/tornado_m2crypto#10

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants