Skip to content

Commit 6437181

Browse files
committed
Add peer_cert_chain to return the whole certificate chain
1 parent 5d24384 commit 6437181

File tree

2 files changed

+14
-0
lines changed

2 files changed

+14
-0
lines changed

lib/net/http.rb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -660,6 +660,8 @@ class HTTPHeaderSyntaxError < StandardError; end
660660
# Sets the minimum SSL version.
661661
# - {#peer_cert}[rdoc-ref:Net::HTTP#peer_cert]:
662662
# Returns the X509 certificate for the session's socket peer.
663+
# - {#peer_cert_chain}[rdoc-ref:Net::HTTP#peer_cert_chain]:
664+
# Returns the X509 certificate chain for the session's socket peer.
663665
# - {:ssl_version}[rdoc-ref:Net::HTTP#ssl_version]:
664666
# Returns the SSL version.
665667
# - {:ssl_version=}[rdoc-ref:Net::HTTP#ssl_version=]:
@@ -1601,6 +1603,16 @@ def peer_cert
16011603
@socket.io.peer_cert
16021604
end
16031605

1606+
# Returns the X509 certificate chain (an array of OpenSSL::X509::Certificate)
1607+
# for the session's socket peer,
1608+
# or +nil+ if none.
1609+
def peer_cert_chain
1610+
if not use_ssl? or not @socket
1611+
return nil
1612+
end
1613+
@socket.io.peer_cert_chain
1614+
end
1615+
16041616
# Starts an \HTTP session.
16051617
#
16061618
# Without a block, returns +self+:

test/net/http/test_https.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ def test_get
3939
http.request_get("/") {|res|
4040
assert_equal($test_net_http_data, res.body)
4141
assert_equal(SERVER_CERT.to_der, http.peer_cert.to_der)
42+
assert_equal(SERVER_CERT.to_der, http.peer_cert_chain.first.to_der)
4243
}
4344
end
4445

@@ -50,6 +51,7 @@ def test_get_SNI
5051
http.request_get("/") {|res|
5152
assert_equal($test_net_http_data, res.body)
5253
assert_equal(SERVER_CERT.to_der, http.peer_cert.to_der)
54+
assert_equal(SERVER_CERT.to_der, http.peer_cert_chain.first.to_der)
5355
}
5456
end
5557

0 commit comments

Comments
 (0)