Skip to content

Commit 9556457

Browse files
committed
Merge branch 'master' of github.com:jruby/jruby-openssl
* 'master' of github.com:jruby/jruby-openssl: Add renegotiation cb (#121)
2 parents 5136388 + 279bd0a commit 9556457

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
@@ -242,6 +242,7 @@ private IRubyObject connectImpl(final ThreadContext context, final boolean block
242242
handshakeStatus = engine.getHandshakeStatus();
243243
initialHandshake = true;
244244
}
245+
callRenegotiationCallback(context);
245246
final IRubyObject ex = doHandshake(blocking, exception);
246247
if ( ex != null ) return ex; // :wait_readable | :wait_writable
247248
}
@@ -325,6 +326,7 @@ private IRubyObject acceptImpl(final ThreadContext context, final boolean blocki
325326
handshakeStatus = engine.getHandshakeStatus();
326327
initialHandshake = true;
327328
}
329+
callRenegotiationCallback(context);
328330
final IRubyObject ex = doHandshake(blocking, exception);
329331
if ( ex != null ) return ex; // :wait_readable | :wait_writable
330332
}
@@ -592,6 +594,18 @@ private int writeToChannel(ByteBuffer buffer, boolean blocking) throws IOExcepti
592594
private void finishInitialHandshake() {
593595
initialHandshake = false;
594596
}
597+
598+
private void callRenegotiationCallback(final ThreadContext context) throws RaiseException {
599+
IRubyObject renegotiationCallback = sslContext.getInstanceVariable("@renegotiation_cb");
600+
if(renegotiationCallback == null || renegotiationCallback.isNil()) {
601+
return;
602+
}
603+
else {
604+
// the return of the Proc is not important
605+
// Can throw ruby exception to "disallow" renegotiations
606+
renegotiationCallback.callMethod(context, "call", this);
607+
}
608+
}
595609

596610
public int write(ByteBuffer src, boolean blocking) throws SSLException, IOException {
597611
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)