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

Fix OpenSSL::SSL::Context::Client#alpn_protocol= #12724

Merged
merged 1 commit into from
Nov 16, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions spec/std/openssl/ssl/socket_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,26 @@ describe OpenSSL::SSL::Socket do
)
end

it "returns selected alpn protocol" do
tcp_server = TCPServer.new("127.0.0.1", 0)
server_context, client_context = ssl_context_pair

server_context.alpn_protocol = "h2"
client_context.alpn_protocol = "h2"

OpenSSL::SSL::Server.open(tcp_server, server_context) do |server|
spawn do
Client.open(TCPSocket.new(tcp_server.local_address.address, tcp_server.local_address.port), client_context, hostname: "example.com") do |socket|
socket.alpn_protocol.should eq("h2")
end
end

client = server.accept
client.alpn_protocol.should eq("h2")
client.close
end
end

it "accepts clients that only write then close the connection" do
tcp_server = TCPServer.new("127.0.0.1", 0)
server_context, client_context = ssl_context_pair
Expand Down
1 change: 1 addition & 0 deletions src/openssl/lib_ssl.cr
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,7 @@ lib LibSSL

fun ssl_get0_alpn_selected = SSL_get0_alpn_selected(handle : SSL, data : Char**, len : LibC::UInt*) : Void
fun ssl_ctx_set_alpn_select_cb = SSL_CTX_set_alpn_select_cb(ctx : SSLContext, cb : ALPNCallback, arg : Void*) : Void
fun ssl_ctx_set_alpn_protos = SSL_CTX_set_alpn_protos(ctx : SSLContext, protos : Char*, protos_len : Int) : Int
{% end %}

{% if compare_versions(OPENSSL_VERSION, "1.0.2") >= 0 %}
Expand Down
44 changes: 26 additions & 18 deletions src/openssl/ssl/context.cr
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,14 @@ abstract class OpenSSL::SSL::Context
end
}, hostname.as(Void*))
end

private def alpn_protocol=(protocol : Bytes)
{% if LibSSL.has_method?(:ssl_ctx_set_alpn_protos) %}
LibSSL.ssl_ctx_set_alpn_protos(@handle, protocol, protocol.size)
{% else %}
raise NotImplementedError.new("LibSSL.ssl_ctx_set_alpn_protos")
{% end %}
end
end

class Server < Context
Expand Down Expand Up @@ -173,6 +181,24 @@ abstract class OpenSSL::SSL::Context
raise OpenSSL::Error.new("SSL_CTX_set_num_tickets") if ret != 1
{% end %}
end

private def alpn_protocol=(protocol : Bytes)
{% if LibSSL.has_method?(:ssl_ctx_set_alpn_select_cb) %}
alpn_cb = ->(ssl : LibSSL::SSL, o : LibC::Char**, olen : LibC::Char*, i : LibC::Char*, ilen : LibC::Int, data : Void*) {
proto = Box(Bytes).unbox(data)
ret = LibSSL.ssl_select_next_proto(o, olen, proto, 2, i, ilen)
if ret != LibSSL::OPENSSL_NPN_NEGOTIATED
LibSSL::SSL_TLSEXT_ERR_NOACK
else
LibSSL::SSL_TLSEXT_ERR_OK
end
}
@alpn_protocol = alpn_protocol = Box.box(protocol)
LibSSL.ssl_ctx_set_alpn_select_cb(@handle, alpn_cb, alpn_protocol)
{% else %}
raise NotImplementedError.new("LibSSL.ssl_ctx_set_alpn_select_cb")
{% end %}
end
end

protected def initialize(method : LibSSL::SSLMethod)
Expand Down Expand Up @@ -429,24 +455,6 @@ abstract class OpenSSL::SSL::Context
self.alpn_protocol = proto
end

private def alpn_protocol=(protocol : Bytes)
{% if LibSSL.has_method?(:ssl_ctx_set_alpn_select_cb) %}
alpn_cb = ->(ssl : LibSSL::SSL, o : LibC::Char**, olen : LibC::Char*, i : LibC::Char*, ilen : LibC::Int, data : Void*) {
proto = Box(Bytes).unbox(data)
ret = LibSSL.ssl_select_next_proto(o, olen, proto, 2, i, ilen)
if ret != LibSSL::OPENSSL_NPN_NEGOTIATED
LibSSL::SSL_TLSEXT_ERR_NOACK
else
LibSSL::SSL_TLSEXT_ERR_OK
end
}
@alpn_protocol = alpn_protocol = Box.box(protocol)
LibSSL.ssl_ctx_set_alpn_select_cb(@handle, alpn_cb, alpn_protocol)
{% else %}
raise NotImplementedError.new("LibSSL.ssl_ctx_set_alpn_select_cb")
{% end %}
end

# Sets this context verify param to the default one of the given name.
#
# Depending on the OpenSSL version, the available defaults are
Expand Down