Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introduce basic support for close_read and close_write. #743

Merged
merged 3 commits into from
Apr 30, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
26 changes: 26 additions & 0 deletions lib/openssl/ssl.rb
Original file line number Diff line number Diff line change
Expand Up @@ -459,6 +459,32 @@ def session
nil
end

# Close the stream for reading.
# This method is ignored by OpenSSL as there is no reasonable way to
# implement it, but exists for compatibility with IO.
def close_read
# Unsupported and ignored.
# Just don't read any more.
end

# Closes the stream for writing. The behavior of this method depends on
# the version of OpenSSL and the TLS protocol in use.
#
# In TLS 1.3 and later:
# - Sends a 'close_notify' alert to the peer.
# - Does not wait for the peer's 'close_notify' alert in response.
#
# In TLS 1.2 and earlier:
# - Sends a 'close_notify' alert to the peer.
# - Waits for the peer's 'close_notify' alert in response.
ioquatix marked this conversation as resolved.
Show resolved Hide resolved
#
# Therefore, on TLS 1.2, this method will cause the connection to be
# completely shut down. On TLS 1.3, the connection will remain open for
# reading only.
ioquatix marked this conversation as resolved.
Show resolved Hide resolved
def close_write
stop
end

private

def using_anon_cipher?
Expand Down
24 changes: 24 additions & 0 deletions test/openssl/test_ssl.rb
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,30 @@ def test_socket_open_with_local_address_port_context
}
end

def test_socket_close_write
server_proc = proc do |ctx, ssl|
message = ssl.read
ssl.write(message)
ssl.close_write
ensure
ssl.close
end

start_server(server_proc: server_proc) do |port|
ctx = OpenSSL::SSL::SSLContext.new
ssl = OpenSSL::SSL::SSLSocket.open("127.0.0.1", port, context: ctx)
ssl.sync_close = true
ssl.connect

message = "abc"*1024
ssl.write message
ssl.close_write
assert_equal message, ssl.read
ensure
ssl&.close
end
end

def test_add_certificate
ctx_proc = -> ctx {
# Unset values set by start_server
Expand Down