2014-09-10 16:13:04 +08:00
|
|
|
SSL Certificate Verification
|
|
|
|
============================
|
2003-06-27 05:30:48 +08:00
|
|
|
|
2014-09-10 16:13:04 +08:00
|
|
|
SSL is TLS
|
|
|
|
----------
|
|
|
|
|
|
|
|
SSL is the old name. It is called TLS these days.
|
|
|
|
|
|
|
|
|
|
|
|
Native SSL
|
|
|
|
----------
|
|
|
|
|
|
|
|
If libcurl was built with Schannel or Secure Transport support (the native SSL
|
|
|
|
libraries included in Windows and Mac OS X), then this does not apply to
|
|
|
|
you. Scroll down for details on how the OS-native engines handle SSL
|
|
|
|
certificates. If you're not sure, then run "curl -V" and read the results. If
|
2020-08-07 23:11:52 +08:00
|
|
|
the version string says `Schannel` in it, then it was built with Schannel
|
2014-09-10 16:13:04 +08:00
|
|
|
support.
|
|
|
|
|
|
|
|
It is about trust
|
|
|
|
-----------------
|
|
|
|
|
2015-03-27 15:22:39 +08:00
|
|
|
This system is about trust. In your local CA certificate store you have certs
|
|
|
|
from *trusted* Certificate Authorities that you then can use to verify that the
|
2014-09-10 16:13:04 +08:00
|
|
|
server certificates you see are valid. They're signed by one of the CAs you
|
|
|
|
trust.
|
|
|
|
|
|
|
|
Which CAs do you trust? You can decide to trust the same set of companies your
|
|
|
|
operating system trusts, or the set one of the known browsers trust. That's
|
|
|
|
basically trust via someone else you trust. You should just be aware that
|
|
|
|
modern operating systems and browsers are setup to trust *hundreds* of
|
|
|
|
companies and recent years several such CAs have been found untrustworthy.
|
|
|
|
|
|
|
|
Certificate Verification
|
|
|
|
------------------------
|
2013-02-19 07:27:30 +08:00
|
|
|
|
2008-02-18 19:35:12 +08:00
|
|
|
libcurl performs peer SSL certificate verification by default. This is done
|
2015-03-27 15:22:39 +08:00
|
|
|
by using a CA certificate store that the SSL library can use to make sure the
|
|
|
|
peer's server certificate is valid.
|
2002-11-12 06:37:59 +08:00
|
|
|
|
2014-09-10 16:13:04 +08:00
|
|
|
If you communicate with HTTPS, FTPS or other TLS-using servers using
|
2015-03-27 15:22:39 +08:00
|
|
|
certificates that are signed by CAs present in the store, you can be sure
|
2014-09-10 16:13:04 +08:00
|
|
|
that the remote server really is the one it claims to be.
|
2008-02-18 19:35:12 +08:00
|
|
|
|
|
|
|
If the remote server uses a self-signed certificate, if you don't install a CA
|
2015-03-27 15:22:39 +08:00
|
|
|
cert store, if the server uses a certificate signed by a CA that isn't
|
|
|
|
included in the store you use or if the remote host is an impostor
|
2004-06-29 21:16:30 +08:00
|
|
|
impersonating your favorite site, and you want to transfer files from this
|
2004-03-16 15:25:52 +08:00
|
|
|
server, do one of the following:
|
2002-11-12 06:37:59 +08:00
|
|
|
|
2008-02-09 05:04:24 +08:00
|
|
|
1. Tell libcurl to *not* verify the peer. With libcurl you disable this with
|
2014-09-10 05:46:58 +08:00
|
|
|
`curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, FALSE);`
|
2002-11-12 06:37:59 +08:00
|
|
|
|
2004-06-29 15:58:52 +08:00
|
|
|
With the curl command line tool, you disable this with -k/--insecure.
|
2002-11-12 06:37:59 +08:00
|
|
|
|
|
|
|
2. Get a CA certificate that can verify the remote server and use the proper
|
|
|
|
option to point out this CA cert for verification when connecting. For
|
2020-03-27 06:11:28 +08:00
|
|
|
libcurl hackers: `curl_easy_setopt(curl, CURLOPT_CAINFO, cacert);`
|
2002-11-12 06:37:59 +08:00
|
|
|
|
2004-06-29 15:58:52 +08:00
|
|
|
With the curl command line tool: --cacert [file]
|
2002-11-12 06:37:59 +08:00
|
|
|
|
2015-03-27 15:22:39 +08:00
|
|
|
3. Add the CA cert for your server to the existing default CA certificate
|
2020-08-28 19:26:30 +08:00
|
|
|
store. The default CA certificate store can be changed at compile time with
|
|
|
|
the following configure options:
|
2004-03-30 14:46:36 +08:00
|
|
|
|
2015-03-27 15:22:39 +08:00
|
|
|
--with-ca-bundle=FILE: use the specified file as CA certificate store. CA
|
|
|
|
certificates need to be concatenated in PEM format into this file.
|
|
|
|
|
|
|
|
--with-ca-path=PATH: use the specified path as CA certificate store. CA
|
|
|
|
certificates need to be stored as individual PEM files in this directory.
|
|
|
|
You may need to run c_rehash after adding files there.
|
|
|
|
|
|
|
|
If neither of the two options is specified, configure will try to auto-detect
|
|
|
|
a setting. It's also possible to explicitly not hardcode any default store
|
|
|
|
but rely on the built in default the crypto library may provide instead.
|
|
|
|
You can achieve that by passing both --without-ca-bundle and
|
|
|
|
--without-ca-path to the configure script.
|
2004-08-25 16:09:48 +08:00
|
|
|
|
|
|
|
If you use Internet Explorer, this is one way to get extract the CA cert
|
|
|
|
for a particular server:
|
|
|
|
|
2014-09-10 05:46:58 +08:00
|
|
|
- View the certificate by double-clicking the padlock
|
|
|
|
- Find out where the CA certificate is kept (Certificate>
|
2004-08-25 16:09:48 +08:00
|
|
|
Authority Information Access>URL)
|
2014-09-10 05:46:58 +08:00
|
|
|
- Get a copy of the crt file using curl
|
|
|
|
- Convert it from crt to PEM using the openssl tool:
|
2004-08-25 16:09:48 +08:00
|
|
|
openssl x509 -inform DES -in yourdownloaded.crt \
|
|
|
|
-out outcert.pem -text
|
2015-03-27 15:22:39 +08:00
|
|
|
- Add the 'outcert.pem' to the CA certificate store or use it stand-alone
|
2004-08-25 16:09:48 +08:00
|
|
|
as described below.
|
|
|
|
|
2004-09-13 02:27:12 +08:00
|
|
|
If you use the 'openssl' tool, this is one way to get extract the CA cert
|
|
|
|
for a particular server:
|
|
|
|
|
2018-08-20 20:05:28 +08:00
|
|
|
- `openssl s_client -showcerts -servername server -connect server:443 > cacert.pem`
|
|
|
|
- type "quit", followed by the "ENTER" key
|
2014-09-10 05:46:58 +08:00
|
|
|
- The certificate will have "BEGIN CERTIFICATE" and "END CERTIFICATE"
|
2004-09-13 02:27:12 +08:00
|
|
|
markers.
|
2014-09-10 05:46:58 +08:00
|
|
|
- If you want to see the data in the certificate, you can do: "openssl
|
2004-09-13 02:27:12 +08:00
|
|
|
x509 -inform PEM -in certfile -text -out certdata" where certfile is
|
|
|
|
the cert you extracted from logfile. Look in certdata.
|
2015-03-27 15:22:39 +08:00
|
|
|
- If you want to trust the certificate, you can add it to your CA
|
|
|
|
certificate store or use it stand-alone as described. Just remember that
|
|
|
|
the security is no better than the way you obtained the certificate.
|
2004-09-13 02:27:12 +08:00
|
|
|
|
2004-08-25 16:09:48 +08:00
|
|
|
4. If you're using the curl command line tool, you can specify your own CA
|
2020-03-27 06:11:28 +08:00
|
|
|
cert file by setting the environment variable `CURL_CA_BUNDLE` to the path
|
2004-06-29 15:58:52 +08:00
|
|
|
of your choice.
|
|
|
|
|
2004-06-29 21:16:30 +08:00
|
|
|
If you're using the curl command line tool on Windows, curl will search
|
2004-06-29 15:58:52 +08:00
|
|
|
for a CA cert file named "curl-ca-bundle.crt" in these directories and in
|
|
|
|
this order:
|
|
|
|
1. application's directory
|
|
|
|
2. current working directory
|
|
|
|
3. Windows System directory (e.g. C:\windows\system32)
|
|
|
|
4. Windows Directory (e.g. C:\windows)
|
|
|
|
5. all directories along %PATH%
|
|
|
|
|
2004-08-25 16:09:48 +08:00
|
|
|
5. Get a better/different/newer CA cert bundle! One option is to extract the
|
2009-08-19 03:51:18 +08:00
|
|
|
one a recent Firefox browser uses by running 'make ca-bundle' in the curl
|
2008-02-08 19:18:23 +08:00
|
|
|
build tree root, or possibly download a version that was generated this
|
2020-11-04 21:02:01 +08:00
|
|
|
way for you: [CA Extract](https://curl.se/docs/caextract.html)
|
2004-08-09 15:02:51 +08:00
|
|
|
|
2004-06-29 21:16:30 +08:00
|
|
|
Neglecting to use one of the above methods when dealing with a server using a
|
|
|
|
certificate that isn't signed by one of the certificates in the installed CA
|
2015-03-27 15:22:39 +08:00
|
|
|
certificate store, will cause SSL to report an error ("certificate verify
|
|
|
|
failed") during the handshake and SSL will then refuse further communication
|
|
|
|
with that server.
|
2009-09-22 07:00:12 +08:00
|
|
|
|
2014-09-10 16:13:04 +08:00
|
|
|
Certificate Verification with NSS
|
|
|
|
---------------------------------
|
2009-09-22 07:00:12 +08:00
|
|
|
|
2013-02-19 07:27:30 +08:00
|
|
|
If libcurl was built with NSS support, then depending on the OS distribution,
|
2014-09-10 16:13:04 +08:00
|
|
|
it is probably required to take some additional steps to use the system-wide
|
|
|
|
CA cert db. RedHat ships with an additional module, libnsspem.so, which
|
2015-03-27 15:22:39 +08:00
|
|
|
enables NSS to read the OpenSSL PEM CA bundle. On openSUSE you can install
|
|
|
|
p11-kit-nss-trust which makes NSS use the system wide CA certificate store. NSS
|
2014-09-10 16:13:04 +08:00
|
|
|
also has a new [database format](https://wiki.mozilla.org/NSS_Shared_DB).
|
2013-02-19 07:27:30 +08:00
|
|
|
|
2014-08-01 21:27:46 +08:00
|
|
|
Starting with version 7.19.7, libcurl automatically adds the 'sql:' prefix to
|
|
|
|
the certdb directory (either the hardcoded default /etc/pki/nssdb or the
|
|
|
|
directory configured with SSL_DIR environment variable). To check which certdb
|
|
|
|
format your distribution provides, examine the default certdb location:
|
|
|
|
/etc/pki/nssdb; the new certdb format can be identified by the filenames
|
|
|
|
cert9.db, key4.db, pkcs11.txt; filenames of older versions are cert8.db,
|
|
|
|
key3.db, secmod.db.
|
2009-09-22 07:00:12 +08:00
|
|
|
|
2014-09-10 16:13:04 +08:00
|
|
|
Certificate Verification with Schannel and Secure Transport
|
|
|
|
-----------------------------------------------------------
|
2013-02-19 07:27:30 +08:00
|
|
|
|
2014-09-10 16:13:04 +08:00
|
|
|
If libcurl was built with Schannel (Microsoft's native TLS engine) or Secure
|
|
|
|
Transport (Apple's native TLS engine) support, then libcurl will still perform
|
2013-02-19 07:27:30 +08:00
|
|
|
peer certificate verification, but instead of using a CA cert bundle, it will
|
|
|
|
use the certificates that are built into the OS. These are the same
|
|
|
|
certificates that appear in the Internet Options control panel (under Windows)
|
|
|
|
or Keychain Access application (under OS X). Any custom security rules for
|
|
|
|
certificates will be honored.
|
|
|
|
|
|
|
|
Schannel will run CRL checks on certificates unless peer verification is
|
|
|
|
disabled. Secure Transport on iOS will run OCSP checks on certificates unless
|
|
|
|
peer verification is disabled. Secure Transport on OS X will run either OCSP
|
|
|
|
or CRL checks on certificates if those features are enabled, and this behavior
|
|
|
|
can be adjusted in the preferences of Keychain Access.
|
2017-03-17 06:00:24 +08:00
|
|
|
|
|
|
|
HTTPS proxy
|
|
|
|
-----------
|
|
|
|
|
|
|
|
Since version 7.52.0, curl can do HTTPS to the proxy separately from the
|
|
|
|
connection to the server. This TLS connection is handled separately from the
|
|
|
|
server connection so instead of `--insecure` and `--cacert` to control the
|
|
|
|
certificate verification, you use `--proxy-insecure` and `--proxy-cacert`.
|
|
|
|
With these options, you make sure that the TLS connection and the trust of the
|
|
|
|
proxy can be kept totally separate from the TLS connection to the server.
|