Skip to content

Commit 279bd0a

Browse files
lampadkares
authored andcommitted
Add renegotiation cb (#121)
* Add support for renegotiation_cb on SSLContext
1 parent b1bac76 commit 279bd0a

File tree

3 files changed

+28
-0
lines changed

3 files changed

+28
-0
lines changed

src/main/java/org/jruby/ext/openssl/SSLContext.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,7 @@ public static void createSSLContext(final Ruby runtime, final RubyModule SSL) {
175175
SSLContext.addReadWriteAttribute(context, "session_id_context");
176176
SSLContext.addReadWriteAttribute(context, "tmp_dh_callback");
177177
SSLContext.addReadWriteAttribute(context, "servername_cb");
178+
SSLContext.addReadWriteAttribute(context, "renegotiation_cb");
178179

179180
SSLContext.defineAlias("ssl_timeout", "timeout");
180181
SSLContext.defineAlias("ssl_timeout=", "timeout=");

src/main/java/org/jruby/ext/openssl/SSLSocket.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,7 @@ private IRubyObject connectImpl(final ThreadContext context, final boolean block
241241
handshakeStatus = engine.getHandshakeStatus();
242242
initialHandshake = true;
243243
}
244+
callRenegotiationCallback(context);
244245
final IRubyObject ex = doHandshake(blocking, exception);
245246
if ( ex != null ) return ex; // :wait_readable | :wait_writable
246247
}
@@ -324,6 +325,7 @@ private IRubyObject acceptImpl(final ThreadContext context, final boolean blocki
324325
handshakeStatus = engine.getHandshakeStatus();
325326
initialHandshake = true;
326327
}
328+
callRenegotiationCallback(context);
327329
final IRubyObject ex = doHandshake(blocking, exception);
328330
if ( ex != null ) return ex; // :wait_readable | :wait_writable
329331
}
@@ -591,6 +593,18 @@ private int writeToChannel(ByteBuffer buffer, boolean blocking) throws IOExcepti
591593
private void finishInitialHandshake() {
592594
initialHandshake = false;
593595
}
596+
597+
private void callRenegotiationCallback(final ThreadContext context) throws RaiseException {
598+
IRubyObject renegotiationCallback = sslContext.getInstanceVariable("@renegotiation_cb");
599+
if(renegotiationCallback == null || renegotiationCallback.isNil()) {
600+
return;
601+
}
602+
else {
603+
// the return of the Proc is not important
604+
// Can throw ruby exception to "disallow" renegotiations
605+
renegotiationCallback.callMethod(context, "call", this);
606+
}
607+
}
594608

595609
public int write(ByteBuffer src, boolean blocking) throws SSLException, IOException {
596610
if ( initialHandshake ) {

src/test/ruby/ssl/test_ssl.rb

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,4 +185,17 @@ def test_connect_nonblock_would_block
185185
end
186186
end if RUBY_VERSION > '1.9'
187187

188+
def test_renegotiation_cb
189+
num_handshakes = 0
190+
renegotiation_cb = Proc.new { |ssl| num_handshakes += 1 }
191+
ctx_proc = Proc.new { |ctx| ctx.renegotiation_cb = renegotiation_cb }
192+
start_server(PORT, OpenSSL::SSL::VERIFY_NONE, true, {:ctx_proc => ctx_proc}) do |server, port|
193+
sock = TCPSocket.new("127.0.0.1", port)
194+
ssl = OpenSSL::SSL::SSLSocket.new(sock)
195+
ssl.connect
196+
assert_equal(1, num_handshakes)
197+
ssl.close
198+
end
199+
end
200+
188201
end

0 commit comments

Comments
 (0)