Skip to content

Commit b7b2009

Browse files
committed
[fix] avoid circular chains in SSL KeyManager
1 parent 69bd973 commit b7b2009

2 files changed

Lines changed: 59 additions & 21 deletions

File tree

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

Lines changed: 22 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1373,29 +1373,30 @@ public java.security.cert.X509Certificate[] getCertificateChain(String alias) {
13731373
}
13741374

13751375
final ArrayList<java.security.cert.X509Certificate> chain = new ArrayList<>(8);
1376-
StoreContext storeCtx = internalContext.createStoreContext(null);
1377-
X509AuxCertificate x = credential.leaf;
1378-
while (true) {
1379-
1380-
chain.add(x);
1381-
1382-
if ( x.getIssuerDN().equals(x.getSubjectDN()) ) break;
1383-
1384-
try {
1385-
final Name name = new Name(x.getIssuerX500Principal());
1386-
X509Object[] s_obj = new X509Object[1];
1387-
if (storeCtx.getBySubject(X509Utils.X509_LU_X509, name, s_obj) <= 0) {
1376+
chain.add(credential.leaf);
1377+
final StoreContext storeCtx = internalContext.createStoreContext(null);
1378+
if (storeCtx != null) {
1379+
X509AuxCertificate x = credential.leaf;
1380+
final int maxChain = Math.max(storeCtx.getParam().getDepth(), 0) + 2;
1381+
while (chain.size() < maxChain && !x.getIssuerDN().equals(x.getSubjectDN())) {
1382+
try {
1383+
final Name name = new Name(x.getIssuerX500Principal());
1384+
X509Object[] s_obj = new X509Object[1];
1385+
if (storeCtx.getBySubject(X509Utils.X509_LU_X509, name, s_obj) <= 0) {
1386+
break;
1387+
}
1388+
x = ((Certificate) s_obj[0]).cert;
1389+
}
1390+
catch (RuntimeException e) {
1391+
LOG.debugStack(e);
13881392
break;
13891393
}
1390-
x = ((Certificate) s_obj[0]).cert;
1391-
}
1392-
catch (RuntimeException e) {
1393-
LOG.debugStack(e);
1394-
break;
1395-
}
1396-
catch (Exception e) {
1397-
LOG.debug("KeyManagerImpl bySubject failed", e);
1398-
break;
1394+
catch (Exception e) {
1395+
LOG.debug("KeyManagerImpl bySubject failed", e);
1396+
break;
1397+
}
1398+
if (chain.contains(x)) break; // avoid circular chains
1399+
chain.add(x);
13991400
}
14001401
}
14011402
return chain.toArray( new java.security.cert.X509Certificate[chain.size()] );

‎test/ssl/test_ssl.rb‎

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1585,6 +1585,43 @@ def test_extra_chain_cert_sends_leaf
15851585
end
15861586
end
15871587

1588+
def test_auto_chain_with_circular_cert_store
1589+
now = Time.now
1590+
ca_exts = [["basicConstraints", "CA:TRUE", true], ["keyUsage", "cRLSign,keyCertSign", true]]
1591+
key_a = OpenSSL::PKey::RSA.new(2048)
1592+
key_b = OpenSSL::PKey::RSA.new(2048)
1593+
dn_a = OpenSSL::X509::Name.parse("/CN=Cross-CA-A")
1594+
dn_b = OpenSSL::X509::Name.parse("/CN=Cross-CA-B")
1595+
1596+
# Cross-signed cycle: A issued by B, B issued by A
1597+
stub_b = OpenSSL::X509::Certificate.new
1598+
stub_b.subject = dn_b
1599+
cert_a = issue_cert(dn_a, key_a, 20, ca_exts, stub_b, key_b, not_before: now - 60, not_after: now + 3600)
1600+
cert_b = issue_cert(dn_b, key_b, 21, ca_exts, cert_a, key_a, not_before: now - 60, not_after: now + 3600)
1601+
1602+
store = OpenSSL::X509::Store.new
1603+
store.add_cert(cert_a)
1604+
store.add_cert(cert_b)
1605+
1606+
ctx_proc = Proc.new do |ctx|
1607+
ctx.cert_store = store
1608+
ctx.cert = cert_a
1609+
ctx.key = key_a
1610+
end
1611+
1612+
start_server(OpenSSL::SSL::VERIFY_NONE, true, ctx_proc: ctx_proc) do |server, port|
1613+
cctx = OpenSSL::SSL::SSLContext.new
1614+
cctx.verify_mode = OpenSSL::SSL::VERIFY_NONE
1615+
server_connect(port, cctx) do |ssl|
1616+
chain = ssl.peer_cert_chain
1617+
assert_equal ["/CN=Cross-CA-A", "/CN=Cross-CA-B"], chain.first(2).map { |c| c.subject.to_s }
1618+
assert_equal 2, chain.size if jruby?
1619+
ssl.puts "hello"
1620+
assert_equal "hello\n", ssl.gets
1621+
end
1622+
end
1623+
end
1624+
15881625
def test_sysread_syswrite_raise_before_handshake
15891626
require 'socket'
15901627
server = TCPServer.new("127.0.0.1", 0)

0 commit comments

Comments
 (0)