Skip to content

TLS for all connections - #5381

Closed
jlhawn wants to merge 10 commits into
rethinkdb:v2.2.xfrom
jlhawn:tls
Closed

jlhawn wants to merge 10 commits into
rethinkdb:v2.2.xfrom
jlhawn:tls

Conversation

@jlhawn

@jlhawn jlhawn commented Feb 10, 2016

Copy link
Copy Markdown

This set of commits adds the ability to wrap any of the web, driver, or cluster connections with TLS. The --help output has been accompanied with the following section:

TLS options:
  --web-tls                                   secure the web administration console
                                              with TLS
  --web-tls-key key_filename                  private key to use for web
                                              administration console TLS
  --web-tls-cert cert_filename                certificate to use for web
                                              administration console TLS
  --driver-tls                                secure client driver connections with
                                              TLS
  --driver-tls-key key_filename               private key to use for client driver
                                              connection TLS
  --driver-tls-cert cert_filename             certificate to use for client driver
                                              connection TLS
  --driver-tls-ca ca_filename                 CA certificate bundle used to verify
                                              client certificates; TLS client
                                              authentication disabled if omitted
  --cluster-tls                               secure intra-cluster connections with
                                              TLS
  --cluster-tls-key key_filename              private key to use for intra-cluster
                                              connection TLS
  --cluster-tls-cert cert_filename            certificate to use for intra-cluster
                                              connection TLS
  --cluster-tls-ca ca_filename                CA certificate bundle used to verify
                                              cluster peer certificates
  --tls-ciphers cipher_list                   specify a list of TLS ciphers to use;
                                              default is 'ECDHE+AESGCM'
  --tls-ec-curves curve_list                  specify a list of elliptic curves to
                                              use for ECDHE; default is
                                              'secp521r1:secp384r1:prime256v1'
  --tls-dhparams dhparams_filename            provide parameters for DHE key
                                              agreement; REQUIRED if using DHE
                                              cipher suites; at least 2048-bit
                                              recommended

TODO: rebase onto the next branch?

fixes #3151
fixes #3158

return true;
}

bool initialize_tls_ctx(SSL_CTX **tls_ctx) {

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This section needs to be audited.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@VeXocide can you have a look at this section?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll have a closer look tomorrow, this'll require some coffee.

@danielmewes danielmewes added this to the 2.3 milestone Feb 10, 2016
@danielmewes danielmewes self-assigned this Feb 10, 2016
Comment thread src/arch/io/network.cc Outdated
OpenSSL context. The given handshake function is used to perform the handshake
for the client or server using SSL_connect or SSL_accept respectively. */
void linux_secure_tcp_conn_t::establish_conn(SSL_CTX *tls_ctx, int (*handshake)(SSL *) ) THROWS_ONLY(connect_failed_exc_t) {
conn = SSL_new(tls_ctx);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not exception safe in that an exception will cause us to leak the allocated memory. We'll probably need to create an RAII wrapper for this.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've moved this allocation into the constructors. Since only one raises an exception, I've added a try/catch block which frees the allocated memory before re-throwing the exception.

@jlhawn

jlhawn commented Feb 11, 2016

Copy link
Copy Markdown
Author

@VeXocide thanks' I'll try to work in your raii wrapper for SSL *.

@VeXocide

Copy link
Copy Markdown
Member

Awesome, don't hesitate to ask if you have any other questions regarding C++ or our codebase!

@jlhawn

jlhawn commented Feb 15, 2016

Copy link
Copy Markdown
Author

The last commit adds 3 additional options that we discussed: --tls-ciphers, --tls-ec-curves, and --tls-dhparams to give administrators more control for configuring TLS, though the defaults should be recommended if possible. This should enable backwards (and possible forwards) compatibility with clients and other versions of OpenSSL.

The next few things I was wondering about are:

  1. Testing

    How would one add unit/integration tests for these TLS capabilities?

  2. Documentation

    Should I add this or is there another team that works on documenting features and 'how-to's?

  3. Configuration file

    I haven't added this yet, opting only for command-line options. It should be fairly straightforward to add those options to the config file.

@VeXocide

Copy link
Copy Markdown
Member

Awesome, I'm looking forward to this.

  1. Testing
    I'm not quite sure how to best fit this into our testing infrastructure to be honest, hopefully @larkost who maintains this can chime in.
  2. Documentation
    We have a separate repository for documentation, it's at https://github.com/rethinkdb/docs, and it'd be great if you could open an issue there with the list of parameters you've added. A technical writer can take it from there to add the necessary documentation.
  3. Configuration file
    I've just been told that the configuration options are converted to command-line options internally, so this should already work.

Thanks again for all the work you've done here!

@jlhawn

jlhawn commented Feb 16, 2016

Copy link
Copy Markdown
Author

I've got some potentially sad news: I use the following OpenSSL function for ECDHE:

int SSL_CTX_set1_curves_list(SSL_CTX *ctx, char *list);
int SSL_set_ecdh_auto(SSL *s, int onoff);

But it looks like these are only available in OpenSSL 1.0.2: https://www.openssl.org/docs/man1.0.2/ssl/SSL_CTX_set1_curves.html

I don't see any way of configuring ECDHE in earlier versions of libssl.

I've been building this so far on my Mac with OpenSSL 1.0.2 installed and I ran into an error when trying to build it on an Ubuntu 14.04 system. The latest version available on that distro is OpenSSL 1.0.1f 6 Jan 2014. It looks like it works on Ubuntu 15.10 though: OpenSSL 1.0.2d 9 Jul 2015.

What do you guys think I should do?

@danielmewes

Copy link
Copy Markdown
Member

If it's too hard to make this work with versions older than OpenSSL 1.0.2, we can always bump the required OpenSSL version. Though it wouldn't be ideal, since users won't get security updates from their distributors for OpenSSL if we need to link in our own version.

Maybe @VeXocide has an idea?

@VeXocide

Copy link
Copy Markdown
Member

OpenSSL 1.0.1. does support ECDHE if I understand correctly, and will, by default, send a list of all curves it supports which is clearly undesirable. It boggles my mind that OpenSSL became a defacto standard.

Fortunately we're not the first to run into this and it turns out there's an undocumented SSL_CTX_set_tmp_ecdh calling SSL_CTX_ctrl with SSL_CTRL_SET_TMP_ECDH under water which can be used to set a single curve. This is based on http://openssl.6102.n7.nabble.com/question-about-ecdh-functions-td38239.html which seems to explain how it is to be used. If possible it'd be cool to select the first curve from --tls-ec-curves though that might prove tricky.

If this isn't an option I'd be comfortable adding EDH, especially now that we have --tls-dhparams. I'd like to prevent statically linking OpenSSL for the the reason that @danielmewes mentions, it doesn't have the greatest track record.

@VeXocide

Copy link
Copy Markdown
Member

Just after posting I found https://github.com/nodejs/node/blob/master/src/node_crypto.cc#L790 which shows how to select a single curve based on a string. If OpenSSL is at a version below 1.0.2 this is probably the way to go.

@larkost

larkost commented Feb 17, 2016

Copy link
Copy Markdown
Collaborator

On the testing front, this is going to be a bit tricky to do in a proper manner since Python's certificate checking is uneven at best. For building (already at ./configure, which is currently required to run the tests anyways) we already require either curl or wget, and at least the former has good support for certificate checking. So at least some of the testing could be done though that binary called with Python's subprocess module (I would prefer to avoid the PyCurl module since it requires compiling). That would make the web part easy, and it is possible that you could use curl's telnet support to jerry-rig enough to test client and server connections.

Alternately, it would probably not be too difficult to create c/c++ tests around libcurl or OpenSSL that could handle all of the cases in a less hacky manner. That would require a little make work, but I would be happy to help on that.

@larkost

larkost commented Feb 17, 2016

Copy link
Copy Markdown
Collaborator

I don't think that this will be relevant for this work, but on MacOS if the non-system version of OpenSSL is used, then you will not have access to the root certificates. So only self-signed certificates would be viable in that case.

@jlhawn

jlhawn commented Feb 19, 2016

Copy link
Copy Markdown
Author

@danielmewes @VeXocide Yesterday I added a commit which makes this compatible with older versions of OpenSSL. The tradeoff is that we only get to specify one elliptic curve to use for ECDHE. The default is the P-256 NIST/FIPS curve but we still have an option for specifying a different curve if the system admin knows what they are doing. Let me know what you think.

Also, I noticed that there's a developer preview of RethinkDB for Windows which is slated for 2.3. Since this PR is also part of the 2.3 milestone, does that mean I have to make sure this will work on Windows or is there a team of Windows Devs that can port this? Honestly, I haven't investigated how much effort that would be.

Oh, one more thing!

I published a Docker image which has a build of this branch: https://hub.docker.com/r/jlhawn/rethinkdb-tls/

I tried to make the image as minimal as possible so it's mostly just the rethinkdb binary and some dependent libs. I just recently built it so there's not much documentation for it at the moment, but I'll put together a guide on how to set up a 3 node cluster with mutual TLS authentication and a proxy node with the web UI secured with TLS and driver connections secured with TLS using the existing authkey method for authenticating clients. Expect that some time soon this afternoon.

@jlhawn

jlhawn commented Feb 19, 2016

Copy link
Copy Markdown
Author

@danielmewes

Copy link
Copy Markdown
Member

You shouldn't worry about the Windows compatibility.
We've had some discussion about this internally. For Windows the code will need some changes to get OpenSSL to work with the asynchronous networking code. On Windows you can't just do a non-blocking read or write and wait for a notification once you're supposed to retry, but instead need to register something they call completion ports (this post seems to describe all the details: https://famellee.wordpress.com/2013/02/20/use-openssl-with-io-completion-port-and-certificate-signing/ ).
This is something we can take care of post merge (though if you want to look into it, that would be awesome too of course! The Windows preview is build from the branch atnnn/windows2 I think).

Comment thread src/arch/io/network.cc Outdated
}

// Transfer ownership of the connection to the caller.
void reclaim() {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For consistency with other similar structures in our code and in the standard libraries, I suggest changing this function into the following:

// Transfer ownership of the connection to the caller.
SSL *release() {
    rassert(conn != nullptr);
    SSL *res = conn;
    conn = nullptr;
    return res;
}

This also fixes two other minor things:

  1. Use nullptr instead of NULL (this is mostly a stylistic change)
  2. Adds an assertion that the connection hasn't been released before. rasserts are enabled when compiling with make DEBUG=1.

Josh Hawn added 6 commits March 12, 2016 16:51
I should probably figure out how best to break this into multiple commits.

Docker-DCO-1.1-Signed-off-by: Josh Hawn <[email protected]> (github: jlhawn)
linux_secure_tcp_conn_t has too many messages spamming the logs.

Docker-DCO-1.1-Signed-off-by: Josh Hawn <[email protected]> (github: jlhawn)
Docker-DCO-1.1-Signed-off-by: Josh Hawn <[email protected]> (github: jlhawn)
--tls-ciphers cipher_list

  Allows a system admin to specify a list of TLS ciphers to use. The default
  is 'ECDHE+AESGCM'. If, for example, the system (or clients) do not support
  newer ECDHE key agreement or GCM an admin could specify:

    --tls-ciphers 'DHE+AES'

--tls-ec-curves curve_list

  Allows a system admin to specify a list of curves supported by the sytem (and
  clients) to use for ECDHE key agreement. To use the curve popularized by
  Bitcoin:

    --tls-ec-curves secp256k1

--tls-dhparams dhparams_filename

  Allows a system admin to specify a file containing Diffie-Hellman parameters
  to use with DHE cipher suites. These cipher suites will not be used unless
  parameters are specified. As such, this option is required to use DHE cipher
  suites. It is highly recommended that a system admin create parameters with
  a strong prime of at least 2048 bits in length:

    openssl dhparam -out dhparams.pem 2048

  and specify using this option:

    --tls-dhparams dhparams.pem

Docker-DCO-1.1-Signed-off-by: Josh Hawn <[email protected]> (github: jlhawn)
While OpenSSL 1.0.2x lets us specify multiple curves to use for ECDHE, those
functions are missing from OpenSSL 1.0.1x and earlier! While you can still
use ECDHE with these versions of OpenSSL, you may only specify a single curve
to use! In order to support these older versions of OpenSSL, the option:

  --tls-ec-curves curves_list

has been replaced with:

  --tls-ecdh-curve curve_name

With the single default curve being 'prime256v1', a.k.a, 'P-256'. System
admins may use this option to change the one curve they want to use.

Docker-DCO-1.1-Signed-off-by: Josh Hawn <[email protected]> (github: jlhawn)
I forgot that struct fields are not automatically initialized with zero values.

This patch also fixes a minor issue between versions of OpenSSL. Specifically
older versions not knowing that ECDHE and EECDH are just different names for
the same thing.

Docker-DCO-1.1-Signed-off-by: Josh Hawn <[email protected]> (github: jlhawn)
@jlhawn
jlhawn force-pushed the tls branch 7 times, most recently from f726ebc to 533d0ef Compare March 14, 2016 07:42
@jlhawn
jlhawn force-pushed the tls branch 2 times, most recently from a7f2fab to fd455ff Compare March 14, 2016 19:50
@danielmewes

Copy link
Copy Markdown
Member

Thanks again for updating the PR.
I added a few last comments. Once you address those, we can merge this. :-)

Docker-DCO-1.1-Signed-off-by: Josh Hawn <[email protected]> (github: jlhawn)
Docker-DCO-1.1-Signed-off-by: Josh Hawn <[email protected]> (github: jlhawn)
@danielmewes

Copy link
Copy Markdown
Member

Awesome, this looks good. 🎉 👏

I have to port this to next, but it should be straight forward.

danielmewes pushed a commit that referenced this pull request Mar 22, 2016
Also fixed compilation in DEBUG mode (including unit tests).
@danielmewes

Copy link
Copy Markdown
Member

This is in next as of d14a456 and will ship with RethinkDB 2.3.0.

Many thanks again for your work on this @jlhawn !

jjqq2013 pushed a commit to sjitech/rethinkdb that referenced this pull request Mar 26, 2016
Also fixed compilation in DEBUG mode (including unit tests).
@AtnNn AtnNn mentioned this pull request Apr 2, 2016
totalwindupflightsystems pushed a commit to totalwindupflightsystems/rethinkdb that referenced this pull request Aug 7, 2026
Also fixed compilation in DEBUG mode (including unit tests).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants