FAQ - Help and questions

Why does my TLS client reject a self-signed certificate, and what should I do instead of turning verification off?

#security#tls#rustlspublished 0 replies0 views
ReplyWatch
Share
Hacker NewsRedditLinkedInE-mail

Short answer

The error UnknownIssuer means the certificate was not signed by a root the client trusts, which is exactly what verification is for. Fix it by adding that one certificate or your own certificate authority to the client's trusted roots in test setups. Never ship code that accepts any certificate.

Checked with rustc 1.93.0, rustls 0.23.46 (ring), rcgen 0.13.2, webpki-roots 1.0.12, Windows 11

Frequently asked question, written and answered by the Ruststudio team. Replies are open: corrections are welcome.

#1

Answer#

A client with the public root list cannot know that a self-signed or privately issued certificate is genuine, so rustls refuses it. The program below repeats the in-memory handshake of the previous thread, but this time with a normal client that trusts only the public web roots.

src/main.rsrust
use rustls::{crypto::ring, ClientConfig, ClientConnection, RootCertStore, ServerConfig, ServerConnection};
use rustls_pki_types::{CertificateDer, PrivateKeyDer, PrivatePkcs8KeyDer, ServerName};
use std::sync::Arc;

/// Run a TLS handshake between a client and a server that talk through memory, not a network.
fn handshake(client: Arc<ClientConfig>, server: Arc<ServerConfig>, name: &'static str) -> Result<String, String> {
    let mut c = ClientConnection::new(client, ServerName::try_from(name).unwrap()).map_err(|e| e.to_string())?;
    let mut s = ServerConnection::new(server).map_err(|e| e.to_string())?;
    for _ in 0..10 {
        let mut buf = Vec::new();
        c.write_tls(&mut buf).unwrap();
        s.read_tls(&mut buf.as_slice()).unwrap();
        s.process_new_packets().map_err(|e| e.to_string())?;
        buf.clear();
        s.write_tls(&mut buf).unwrap();
        c.read_tls(&mut buf.as_slice()).unwrap();
        c.process_new_packets().map_err(|e| e.to_string())?;
        if !c.is_handshaking() && !s.is_handshaking() {
            return Ok(format!("{:?}", c.protocol_version().unwrap()));
        }
    }
    Err("no result".into())
}

fn main() {
    let provider = Arc::new(ring::default_provider());
    // A throw-away certificate for "localhost", made in memory for this demonstration.
    let cert = rcgen::generate_simple_self_signed(vec!["localhost".to_string()]).unwrap();
    let cert_der: CertificateDer<'static> = cert.cert.der().clone();
    let key = PrivateKeyDer::Pkcs8(PrivatePkcs8KeyDer::from(cert.key_pair.serialize_der()));

    let server = ServerConfig::builder_with_provider(provider.clone())
        .with_safe_default_protocol_versions().unwrap()
        .with_no_client_auth()
        .with_single_cert(vec![cert_der], key).unwrap();

    // A normal client: it trusts the public roots only, so a self-signed certificate must fail.
    let roots = RootCertStore { roots: webpki_roots::TLS_SERVER_ROOTS.to_vec() };
    let client = ClientConfig::builder_with_provider(provider)
        .with_safe_default_protocol_versions().unwrap()
        .with_root_certificates(roots)
        .with_no_client_auth();

    println!("{:?}", handshake(Arc::new(client), Arc::new(server), "localhost"));
}
text
Err("invalid peer certificate: UnknownIssuer")

UnknownIssuer is rustls saying: "I cannot link this certificate to any root I trust". It is the correct result.

How to do it (for tests and internal services)#

  1. Create your own test authority or use a self-signed certificate, and keep its certificate (never its key) in the test setup.
  2. Build the client's RootCertStore with that certificate: roots.add(cert_der), as in the previous thread. Only that certificate is trusted; the name is still checked.
  3. For internal services, run a private certificate authority and distribute its certificate to the clients that need it.
  4. For public services use certificates from a public authority, so clients need no extra setup.

Why not "just turn it off"#

A client that accepts every certificate still encrypts the traffic, but it no longer knows who is on the other end. Anyone on the network path can present their own certificate and read or change everything, and the connection looks normal. A verification bypass that was added "temporarily" and reaches production is a common finding in security reviews. If a test needs a special case, make it a test-only code path that cannot compile into the release build.

Common mistakes#

  • Adding the self-signed certificate to the machine-wide trust store of a development computer and forgetting it there.
  • Fixing a name-mismatch error by disabling checks; instead issue the certificate for the right name.
  • Reusing one test certificate across many projects.

What does a name mismatch look like?#

The previous thread shows it: certificate not valid for name "other.test"; certificate is only valid for DnsName("localhost").

Is a certificate error ever the user's fault?#

Sometimes: a wrong system clock makes valid certificates look expired. Report the error as it is, and never offer the user a "continue anyway" button for a service you control.

Sources#

    QuoteReply
    Share
    Hacker NewsRedditLinkedInE-mail

    No replies yet