A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://www.mongodb.com/docs/languages/cpp/cpp-driver/current/connect/tls/ below:

Configure Transport Layer Security (TLS) - C++ Driver v4.1

In this guide, you can learn how to use the TLS protocol to secure your connection to a MongoDB deployment.

When you enable TLS for a connection, the C++ driver performs the following actions:

To learn how to configure your MongoDB deployment for TLS, see the TLS configuration guide in the MongoDB Server manual.

Note

This page assumes prior knowledge of TLS/SSL and access to valid certificates. A full description of TLS/SSL, PKI (Public Key Infrastructure) certificates, and Certificate Authorities (CAs) is beyond the scope of this documentation.

Tip

The C++ driver delegates most TLS behavior to the MongoDB C Driver. For information about how the C driver handles TLS, including configuration steps and expected behavior, see Configuring TLS in the C driver Documentation.

To enable TLS for the connection to your MongoDB instance, set the tls connection option to true in your connection string, as shown in the following example:

mongocxx::uri uri("mongodb://<hostname>:<port>/?tls=true");
Tip

If your connection string includes the +srv modification, which specifies the SRV connection format, TLS is enabled on your connection by default.

To learn more about the SRV connection format, see SRV Connection Format in the MongoDB Server documentation.

During the TLS handshake, the MongoDB deployment presents a certificate key file to your application to establish its identity. Usually, a deployment's certificate has been signed by a well-known CA, and your application relies on this CA to validate the certificate.

During testing, however, you might want to act as your own CA. In this case, you must instruct the C++ driver to use your CA certificates instead of ones signed by another CA.

To do so, specify the path to a .pem file containing the root certificate chain. You can do this in two ways: by setting a property on a mongocxx::options::tls object, or by using the tlsCAFile parameter in your connection string.

mongocxx::options::client client_options;mongocxx::options::tls tls_options;tls_options.pem_file("/path/to/file.pem");client_options.tls_opts(tls_options);mongocxx::uri uri("mongodb://<hostname>:<port>/?tls=true");mongocxx::client client(uri, client_options);
mongocxx::uri uri("mongodb://<hostname>:<port>/?tls=true&tlsCAFile=/path/to/file.pem");

If you are using OpenSSL or LibreSSL (libtls) for TLS support, you can also instruct the C++ driver to search for a CA file within a directory. The driver searches this directory if it doesn't find a CA file at the path specified in the tlsCAFile or pem_file option.

The following code example shows how to use the ca_dir option to specify the directory that the driver should search:

mongocxx::options::client client_options;mongocxx::options::tls tls_options;tls_options.ca_dir("/path/to/search/");client_options.tls_opts(tls_options);mongocxx::uri uri("mongodb://<hostname>:<port>/?tls=true");mongocxx::client client(uri, client_options);

When an X.509 certificate is no longer trustworthy—for example, if its private key has been compromised—the CA revokes the certificate. The C++ driver includes two ways to check whether a server's certificate has been revoked.

The Online Certificate Status Protocol (OCSP) process varies depending on the version of MongoDB Server you're connecting to:

To stop the C++ driver from contacting the OCSP endpoint, set the tlsDisableOCSPEndpointCheck connection option to true in your connection string, as shown in the following code example:

mongocxx::uri uri("mongodb://<hostname>:<port>/?tls=true&tlsDisableOCSPEndpointCheck=true");

Instead of using OCSP, you can instruct the C++ driver to check the server's certificate against a Certificate Revocation List (CRL) published by the CA.

The following code example shows how to use the crl_file option to specify the path to specify the path to a .pem file from the CA:

mongocxx::options::client client_options;mongocxx::options::tls tls_options;tls_options.crl_file("/path/to/file.pem");client_options.tls_opts(tls_options);mongocxx::uri uri("mongodb://<hostname>:<port>/?tls=true");mongocxx::client client(uri, client_options);
Tip

You can specify a CRL file in either the .pem or .der format.

Some MongoDB deployments require every connecting application to present a client certificate that proves its identity. To specify the client certificate for the C++ driver to present, specify the file path of the .pem file that contains your certificate and private key.

You can do this in two ways: by setting a property on a mongocxx::options::tls object, or by using the tlsCertificateKeyFile parameter in your connection string.

mongocxx::options::client client_options;mongocxx::options::tls tls_options;tls_options.pem_file("/path/to/file.pem");client_options.tls_opts(tls_options);mongocxx::uri uri("mongodb://<hostname>:<port>/?tls=true");mongocxx::client client(uri, client_options);
mongocxx::uri uri("mongodb://<hostname>:<port>/?tls=true&tlsCertificateKeyFile=/path/to/file.pem");
Important

Your client certificate and private key must be in the same .pem file. If they are stored in different files, you must concatenate them. The following example shows how to concatenate a key file and a certificate file into a third file called combined.pem on a Unix system:

$ cat key.pem cert.pem > combined.pem

If the private key in your certificate file is encrypted, you must provide the password. You can do this in two ways: by setting a property on a mongocxx::options::tls object, or by using the tlsCertificateKeyFilePassword parameter in your connection string.

mongocxx::options::client client_options;mongocxx::options::tls tls_options;tls_options.pem_file("/path/to/file.pem");tls_options.pem_password("<password>");client_options.tls_opts(tls_options);mongocxx::uri uri("mongodb://<hostname>:<port>/?tls=true");mongocxx::client client(uri, client_options);
mongocxx::uri uri("mongodb://<hostname>:<port>/?tls=true&tlsCertificateKeyFile=/path/to/file.pem&tlsCertificateKeyFilePassword=<password>");

When TLS is enabled, the C++ driver automatically verifies the certificate that the server presents. When testing your code, you can disable this verification. This is known as insecure TLS.

When insecure TLS is enabled, the driver requires only that the server present an X.509 certificate. The driver accepts a certificate even if any of the following are true:

Note

Even when insecure TLS is enabled, communication between the client and server is encrypted with TLS.

To enable insecure TLS, set the tlsInsecure connection option to true in your connection string, as shown in the following code example:

mongocxx::uri uri("mongodb://<hostname>:<port>/?tls=true&tlsInsecure=true");

To disable only certificate validation, set the tlsAllowInvalidCertificates option to true, and set the tlsInsecure option to false (or omit it):

mongocxx::options::client client_options;mongocxx::options::tls tls_options;tls_options.allow_invalid_certificates(true);client_options.tls_opts(tls_options);mongocxx::uri uri("mongodb://<hostname>:<port>/?tls=true");mongocxx::client client(uri, client_options);
mongocxx::uri uri("mongodb://<hostname>:<port>/?tls=true&tlsAllowInvalidCertificates=true");

To disable only hostname verification, set the tlsAllowInvalidHostnames option to true, and set the tlsInsecure option to false (or omit it):

mongocxx::uri uri("mongodb://<hostname>:<port>/?tls=true&tlsAllowInvalidHostnames=true");
Warning Don't Use in Production

Always set the tlsInsecure, tlsAllowInvalidCertificates, and tlsAllowInvalidHostnames options to false in production.

Setting any of these options to true in a production environment makes your application insecure and potentially vulnerable to expired certificates and to foreign processes posing as valid client instances.

To learn more about configuring TLS for the C++ driver, see the following API documentation:


RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4