Skip to content

Add renegotiation cb #121

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

Merged
merged 2 commits into from
Feb 16, 2017
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
1 change: 1 addition & 0 deletions src/main/java/org/jruby/ext/openssl/SSLContext.java
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,7 @@ public static void createSSLContext(final Ruby runtime, final RubyModule SSL) {
SSLContext.addReadWriteAttribute(context, "session_id_context");
SSLContext.addReadWriteAttribute(context, "tmp_dh_callback");
SSLContext.addReadWriteAttribute(context, "servername_cb");
SSLContext.addReadWriteAttribute(context, "renegotiation_cb");

SSLContext.defineAlias("ssl_timeout", "timeout");
SSLContext.defineAlias("ssl_timeout=", "timeout=");
Expand Down
14 changes: 14 additions & 0 deletions src/main/java/org/jruby/ext/openssl/SSLSocket.java
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,7 @@ private IRubyObject connectImpl(final ThreadContext context, final boolean block
handshakeStatus = engine.getHandshakeStatus();
initialHandshake = true;
}
callRenegotiationCallback(context);
final IRubyObject ex = doHandshake(blocking, exception);
if ( ex != null ) return ex; // :wait_readable | :wait_writable
}
Expand Down Expand Up @@ -317,6 +318,7 @@ private IRubyObject acceptImpl(final ThreadContext context, final boolean blocki
handshakeStatus = engine.getHandshakeStatus();
initialHandshake = true;
}
callRenegotiationCallback(context);
final IRubyObject ex = doHandshake(blocking, exception);
if ( ex != null ) return ex; // :wait_readable | :wait_writable
}
Expand Down Expand Up @@ -584,6 +586,18 @@ private int writeToChannel(ByteBuffer buffer, boolean blocking) throws IOExcepti
private void finishInitialHandshake() {
initialHandshake = false;
}

private void callRenegotiationCallback(final ThreadContext context) throws RaiseException {
IRubyObject renegotiationCallback = sslContext.getInstanceVariable("@renegotiation_cb");
if(renegotiationCallback == null || renegotiationCallback.isNil()) {
return;
}
else {
// the return of the Proc is not important
// Can throw ruby exception to "disallow" renegotiations
renegotiationCallback.callMethod(context, "call", this);
}
}

public int write(ByteBuffer src, boolean blocking) throws SSLException, IOException {
if ( initialHandshake ) {
Expand Down
13 changes: 13 additions & 0 deletions src/test/ruby/ssl/test_ssl.rb
Original file line number Diff line number Diff line change
Expand Up @@ -185,4 +185,17 @@ def test_connect_nonblock_would_block
end
end if RUBY_VERSION > '1.9'

def test_renegotiation_cb
num_handshakes = 0
renegotiation_cb = Proc.new { |ssl| num_handshakes += 1 }
ctx_proc = Proc.new { |ctx| ctx.renegotiation_cb = renegotiation_cb }
start_server(PORT, OpenSSL::SSL::VERIFY_NONE, true, {:ctx_proc => ctx_proc}) do |server, port|
sock = TCPSocket.new("127.0.0.1", port)
ssl = OpenSSL::SSL::SSLSocket.new(sock)
ssl.connect
assert_equal(1, num_handshakes)
ssl.close
end
end

end