Skip to content
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
8 changes: 6 additions & 2 deletions ext/openssl/ossl_ssl.c
Original file line number Diff line number Diff line change
Expand Up @@ -1410,6 +1410,10 @@ get_cert_and_key(VALUE cert, VALUE key, X509 **x509, EVP_PKEY **pkey)
* repeated calls of this method, and OpenSSL will choose the most appropriate
* certificate during the handshake.
*
* If a certificate with the same public key type was already added, the
* earlier certificate and its extra certificates are replaced. If
* _extra_certs_ is omitted, the earlier extra certificates are discarded.
*
* #cert=, #key=, and #extra_chain_cert= are old accessor methods for setting
* certificate and internally call this method.
*
Expand Down Expand Up @@ -1459,7 +1463,7 @@ ossl_sslctx_add_certificate(int argc, VALUE *argv, VALUE self)
sk_X509_pop_free(extra_chain, X509_free);
ossl_raise(eSSLError, "SSL_CTX_use_PrivateKey");
}
if (extra_chain && !SSL_CTX_set0_chain(ctx, extra_chain)) {
if (!SSL_CTX_set0_chain(ctx, extra_chain)) {
sk_X509_pop_free(extra_chain, X509_free);
ossl_raise(eSSLError, "SSL_CTX_set0_chain");
}
Expand Down Expand Up @@ -1497,7 +1501,7 @@ ossl_ssl_add_certificate(int argc, VALUE *argv, VALUE self)
sk_X509_pop_free(extra_chain, X509_free);
ossl_raise(eSSLError, "SSL_use_PrivateKey");
}
if (extra_chain && !SSL_set0_chain(ssl, extra_chain)) {
if (!SSL_set0_chain(ssl, extra_chain)) {
sk_X509_pop_free(extra_chain, X509_free);
ossl_raise(eSSLError, "SSL_set0_chain");
}
Expand Down
35 changes: 33 additions & 2 deletions test/openssl/test_ssl.rb
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,19 @@ def test_add_certificate
ssl.puts "abc"; assert_equal "abc\n", ssl.gets
}
end

# Extra certificates from an earlier call are not sent
sctx = OpenSSL::SSL::SSLContext.new
sctx.add_certificate(@svr_cert, @svr_key, [@ca_cert])
sctx.add_certificate(@cli_cert, @cli_key)

start_server(sctx) do |port|
server_connect(port) { |ssl|
assert_equal [@cli_cert], ssl.peer_cert_chain

ssl.puts "abc"; assert_equal "abc\n", ssl.gets
}
end
end

def test_socket_add_certificate
Expand All @@ -321,6 +334,23 @@ def test_socket_add_certificate
}
}

# Extra certificates copied from the SSLContext are not sent
server_proc = -> sock {
ctx = OpenSSL::SSL::SSLContext.new
ctx.add_certificate(@svr_cert, @svr_key, [@ca_cert])
ssl = OpenSSL::SSL::SSLSocket.new(sock, ctx)
ssl.add_certificate(@cli_cert, @cli_key)
ssl.accept
readwrite_loop(ssl)
}
start_server_proc(server_proc) { |port|
server_connect(port) { |ssl|
assert_equal [@cli_cert], ssl.peer_cert_chain

ssl.puts "abc"; assert_equal "abc\n", ssl.gets
}
}

# Invalid
IO.pipe { |r, _w|
ssl = OpenSSL::SSL::SSLSocket.new(r)
Expand Down Expand Up @@ -1187,7 +1217,8 @@ def test_tlsext_hostname

def test_servername_cb_add_certificate
called = []
sctx = make_server_context
sctx = OpenSSL::SSL::SSLContext.new
sctx.add_certificate(@svr_cert, @svr_key, [@ca_cert])
sctx.servername_cb = proc do |ssl, servername|
called << servername
ssl.add_certificate(@cli_cert, @cli_key)
Expand All @@ -1198,7 +1229,7 @@ def test_servername_cb_add_certificate
ssl = OpenSSL::SSL::SSLSocket.new(sock)
ssl.hostname = "example.org"
ssl.connect
assert_equal(@cli_cert, ssl.peer_cert)
assert_equal([@cli_cert], ssl.peer_cert_chain)
assert_equal(["example.org"], called)
ssl.puts("abc"); ssl.gets
ensure
Expand Down
Loading