sfackler / rust-native-tls

Apache License 2.0
470 stars 195 forks source link

`openssl` vs `native-tls` on macOS differences using self signed `ca.crt` #210

Closed broccolihighkicks closed 2 years ago

broccolihighkicks commented 2 years ago

Hello,

I am connecting to a Postgres server with a self signed root CA. I have a ca.crt from a managed cloud Postgres service.

I am comparing the openssl and native-tls crates - I want to ensure my connections are secure.


openssl works:

  // OpenSSL - this works:
  let connector_openssl = {
      let mut builder = SslConnector::builder(SslMethod::tls()).unwrap();
      builder.set_ca_file("/x/ca.crt").unwrap();
      MakeTlsConnectorOpenSSL::new(builder.build())
  };

native-tls only seems to work when:


Questions:


Also this seems to work:

PGPASSWORD=pass psql "port=5432 host=db.com user=postgres sslrootcert=/x/ca.crt sslmode=verify-full"

Thanks

sfackler commented 2 years ago

native-tls is backed by Security.framework on macOS, which has some more pedantic requirements on certificates that it is willing to accept compared to OpenSSL: https://support.apple.com/en-us/HT210176. It could be that setting the certificate to Always Trust bypasses those extra checks.

danger_accept_invalid_certs will accept literally any certificate for any site, so it is definitely not the same thing as adding your particular certificate to the keychain.

If you know what certificate you are going to receive (e.g. you're talking to something else you control on the same host) then you don't need to rely on certificate validation. However, I strongly recommend not disabling certificate validation.

Some of the Security.framework behavior linked above is to ignore the common name entry in certificates, instead requiring SAN entries which could explain why you need to disable hostname verification.