|
| 1 | +:man_page: configuring_tls |
| 2 | + |
| 3 | +Configuring TLS |
| 4 | +=============== |
| 5 | + |
| 6 | +Building libmongoc with TLS support |
| 7 | +----------------------------------- |
| 8 | + |
| 9 | +By default, libmongoc will attempt to find a supported TLS library and enable TLS support. This is controlled by the cmake flag ``ENABLE_SSL``, which is set to ``AUTO`` by default. Valid values are: |
| 10 | + |
| 11 | +- ``AUTO`` the default behavior. Link to the system's native TLS library, or attempt to find OpenSSL. |
| 12 | +- ``DARWIN`` link to Secure Transport, the native TLS library on macOS. |
| 13 | +- ``WINDOWS`` link to Secure Channel, the native TLS on Windows. |
| 14 | +- ``OPENSSL`` link to OpenSSL (libssl). An optional install path may be specified with ``OPENSSL_ROOT``. |
| 15 | +- ``LIBRESSL`` link to LibreSSL's libtls. (LibreSSL's compatible libssl may be linked to by setting ``OPENSSL``). |
| 16 | +- ``OFF`` disable TLS support. |
| 17 | + |
| 18 | +Configuration with URI options |
| 19 | +------------------------------ |
| 20 | + |
| 21 | +Enable TLS by including ``tls=true`` the URI. |
| 22 | + |
| 23 | +.. code:: c |
| 24 | + |
| 25 | + mongoc_uri_t *uri = mongoc_uri_new ("mongodb://localhost:27017/"); |
| 26 | + mongoc_uri_set_option_as_bool (uri, MONGOC_URI_TLS, true); |
| 27 | +
|
| 28 | + mongoc_client_t *client = mongoc_client_new_from_uri (uri); |
| 29 | +
|
| 30 | +
|
| 31 | +The following URI options may be used to further configure TLS: |
| 32 | + |
| 33 | +.. include:: includes/tls-options.txt |
| 34 | + |
| 35 | +Configuration with mongoc_ssl_opt_t |
| 36 | +----------------------------------- |
| 37 | + |
| 38 | +Alternatively, the :symbol:`mongoc_ssl_opt_t` struct may be used to configure TLS with :symbol:`mongoc_client_set_ssl_opts()` or :symbol:`mongoc_client_pool_set_ssl_opts()`. Most of the configurable options can be using the Connection URI. |
| 39 | + |
| 40 | +=============================== =============================== |
| 41 | +**mongoc_ssl_opt_t key** **URI key** |
| 42 | +=============================== =============================== |
| 43 | +pem_file tlsClientCertificateKeyFile |
| 44 | +pem_pwd tlsClientCertificateKeyPassword |
| 45 | +ca_file tlsCAFile |
| 46 | +weak_cert_validation tlsAllowInvalidCertificates |
| 47 | +allow_invalid_hostname tlsAllowInvalidHostnames |
| 48 | +=============================== =============================== |
| 49 | + |
| 50 | +The only exclusions are ``crl_file`` and ``ca_dir``. Those may only be set with :symbol:`mongoc_ssl_opt_t`. |
| 51 | + |
| 52 | +Client Authentication |
| 53 | +--------------------- |
| 54 | + |
| 55 | +When MongoDB is started with TLS enabled, it will by default require the client to provide a client certificate issued by a certificate authority specified by ``--tlsCAFile``, or an authority trusted by the native certificate store in use on the server. |
| 56 | + |
| 57 | +To provide the client certificate, set the ``tlscertificatekeyfile`` in the URI to a PEM armored certificate file. |
| 58 | + |
| 59 | +.. code-block:: c |
| 60 | +
|
| 61 | + mongoc_uri_t *uri = mongoc_uri_new ("mongodb://localhost:27017/"); |
| 62 | + mongoc_uri_set_option_as_bool (uri, MONGOC_URI_TLS, true); |
| 63 | + mongoc_uri_set_option_as_utf8 (uri, MONGOC_URI_TLSCERTIFICATEKEYFILE, "/path/to/client-certificate.pem"); |
| 64 | +
|
| 65 | + mongoc_client_t *client = mongoc_client_new_from_uri (uri); |
| 66 | +
|
| 67 | +Server Certificate Verification |
| 68 | +------------------------------- |
| 69 | + |
| 70 | +The MongoDB C Driver will automatically verify the validity of the server certificate, such as issued by configured Certificate Authority, hostname validation, and expiration. |
| 71 | + |
| 72 | +To overwrite this behaviour, it is possible to disable hostname validation, and/or allow otherwise invalid certificates. This behaviour is controlled using the ``tlsallowinvalidhostnames`` and ``tlsallowinvalidcertificates`` options. By default, both are set to ``false``. It is not recommended to change these defaults as it exposes the client to *Man In The Middle* attacks (when ``tlsallowinvalidhostnames`` is set) and otherwise invalid certificates when ``tlsallowinvalidcertificates`` is set to ``true``. |
| 73 | + |
| 74 | +OpenSSL |
| 75 | +------- |
| 76 | + |
| 77 | +The MongoDB C Driver uses OpenSSL, if available, on Linux and Unix platforms (besides macOS). Industry best practices and some regulations require the use of TLS 1.1 or newer, which requires at least OpenSSL 1.0.1. Check your OpenSSL version like so:: |
| 78 | + |
| 79 | + $ openssl version |
| 80 | + |
| 81 | +Ensure your system's OpenSSL is a recent version (at least 1.0.1), or install a recent version in a non-system path and build against it with:: |
| 82 | + |
| 83 | + cmake -DOPENSSL_ROOT_DIR=/absolute/path/to/openssl |
| 84 | + |
| 85 | +When compiled against OpenSSL, the driver will attempt to load the system default certificate store, as configured by the distribution. That can be overridden by setting the ``tlscafile`` URI option or with the fields ``ca_file`` and ``ca_dir`` in the :symbol:`mongoc_ssl_opt_t`. |
| 86 | + |
| 87 | +LibreSSL / libtls |
| 88 | +----------------- |
| 89 | + |
| 90 | +The MongoDB C Driver supports LibreSSL through the use of OpenSSL compatibility checks when configured to compile against ``openssl``. It also supports the new ``libtls`` library when configured to build against ``libressl``. |
| 91 | + |
| 92 | +Native TLS Support on Windows (Secure Channel) |
| 93 | +---------------------------------------------- |
| 94 | + |
| 95 | +The MongoDB C Driver supports the Windows native TLS library (Secure Channel, or SChannel), and its native crypto library (Cryptography API: Next Generation, or CNG). |
| 96 | + |
| 97 | +When compiled against the Windows native libraries, the ``ca_dir`` option of a :symbol:`mongoc_ssl_opt_t` is not supported, and will issue an error if used. |
| 98 | + |
| 99 | +Encrypted PEM files (e.g., setting ``tlscertificatekeypassword``) are also not supported, and will result in error when attempting to load them. |
| 100 | + |
| 101 | +When ``tlscafile`` is set, the driver will only allow server certificates issued by the authority (or authorities) provided. When no ``tlscafile`` is set, the driver will look up the Certificate Authority using the ``System Local Machine Root`` certificate store to confirm the provided certificate or the ``Current user certificate store`` if the ``System Local Machine Root`` certificate store is unavailable. |
| 102 | + |
| 103 | +When ``crl_file`` is set with :symbol:`mongoc_ssl_opt_t`, the driver will import the revocation list to the ``System Local Machine Root`` certificate store. |
| 104 | + |
| 105 | +.. _Secure Transport: |
| 106 | + |
| 107 | +Native TLS Support on macOS / Darwin (Secure Transport) |
| 108 | +------------------------------------------------------- |
| 109 | + |
| 110 | +The MongoDB C Driver supports the Darwin (OS X, macOS, iOS, etc.) native TLS library (Secure Transport), and its native crypto library (Common Crypto, or CC). |
| 111 | + |
| 112 | +When compiled against Secure Transport, the ``ca_dir`` option of a :symbol:`mongoc_ssl_opt_t` is not supported, and will issue an error if used. |
| 113 | + |
| 114 | +When ``tlscafile`` is set, the driver will only allow server certificates issued by the authority (or authorities) provided. When no ``tlscafile`` is set, the driver will use the Certificate Authorities in the currently unlocked keychains. |
0 commit comments