cass_ssl_set_cert drops intermediate certificates from a PEM chain (inverted condition in SSL_CTX_use_certificate_chain_bio)

Open Beginner friendly
#542 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
2/5
Estimated time
1-3 hours
Newbie friendliness
76/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Active
Tech stack
rust

Research direction

Start in scylla-rust-wrapper/src/ssl.rs at the chain-loading helper used by cass_ssl_set_cert_n (around lines 353-378), then compare it with src/ssl/ssl_openssl_impl.cpp lines 188-240. Reproduce with the leaf+intermediate PEM test from the issue before editing. Done means the loop runs only after the leaf installs, the previous chain is cleared, PEM_R_NO_START_LINE is treated as end-of-input, CASS_OK is returned, and a unit test reports one extra chain cert.

Written by the indexing model from the issue text.

Description

area/Driver_-_cpp-rs-driver bug

What goes wrong

cass_ssl_set_cert() / cass_ssl_set_cert_n() never install intermediate certificates from a PEM bundle. SSL_CTX_use_certificate_chain_bio (a port of the C++ driver's helper) reads the leaf, calls SSL_CTX_use_certificate, and then only enters the "add following PEM certs as chain certs" loop when ret != 1, i.e. when installing the leaf failed:

https://github.com/scylladb/cpp-rs-driver/blob/7a86e01c2f6e07ceb45633f697c7507349a6108b/scylla-rust-wrapper/src/ssl.rs#L353-L378

So on the normal path (leaf installed, ret == 1) every intermediate in the bundle is silently dropped and CASS_OK is returned. On the failure path the loop needlessly mutates the context and always ends with ret = 0.

The original C++ driver runs the loop on success (if (ret)), and treats the end-of-input PEM_R_NO_START_LINE error as success:

https://github.com/scylladb/cpp-rs-driver/blob/7a86e01c2f6e07ceb45633f697c7507349a6108b/src/ssl/ssl_openssl_impl.cpp#L188-L240

Impact

A client configured for mutual TLS with a leaf + intermediate(s) bundle presents only the leaf during the handshake. Servers that trust only the root CA reject the client certificate, even though the application passed a correct chain and got CASS_OK. This is a regression compared to the C/C++ driver.

Reproduction

Temporary unit test in scylla-rust-wrapper/src/ssl.rs (root -> intermediate -> leaf generated with openssl, chain.pem = leaf.pem + int.pem):

let ssl = cass_ssl_new();
let rc = cass_ssl_set_cert_n(ssl.borrow(), CassStrLenDelimited::from_raw(CHAIN.as_ptr() as _), CassStrLen::from_raw(CHAIN.len() as _));
let ctx = ArcFFI::as_ref(ssl.borrow()).unwrap().ssl_context;
let mut sk: *mut openssl_sys::stack_st_X509 = std::ptr::null_mut();
openssl_sys::SSL_CTX_ctrl(ctx, 82 /* SSL_CTRL_GET_EXTRA_CHAIN_CERTS */, 0, &mut sk as *mut _ as *mut c_void);
let n = if sk.is_null() { 0 } else { openssl_sys::OPENSSL_sk_num(sk as *const _) };

Output on 7a86e01:

cass_ssl_set_cert_n rc=CassError_(0), extra chain certs = 0

Just flipping the condition to ret == 1 gives extra chain certs = 1 but then returns CASS_ERROR_SSL_INVALID_CERT, because reaching the end of the bundle (PEM_read_bio_X509 returning NULL) unconditionally sets ret = 0.

Fix direction

Run the chain loop only when SSL_CTX_use_certificate succeeded, clear any previous chain first (SSL_CTX_clear_chain_certs/extra certs), and when PEM_read_bio_X509 returns NULL check the last error: PEM_R_NO_START_LINE means "no more certificates" and should clear the error queue and keep ret = 1; any other error should fail. That mirrors OpenSSL's use_certificate_chain_file and the C++ driver. A unit test with a leaf + intermediate bundle asserting one extra chain cert and CASS_OK would cover it.

Dominant language
C++
Stars
27
Forks
18
Avg merge
1d 7h
Merged PRs (30d)
5

Getting set up

  • No Dockerfile or Docker Compose file
  • Has a pull request template
  • No contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from scylladb/cpp-rs-driver

All issues in scylladb/cpp-rs-driver

Similar issues

More C++ issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.