Skip to content

Use the private RNG for data that is not public - #4665

Closed
kroeckx wants to merge 2 commits into
openssl:masterfrom
kroeckx:drbg_private
Closed

kroeckx wants to merge 2 commits into
openssl:masterfrom
kroeckx:drbg_private

Conversation

@kroeckx

@kroeckx kroeckx commented Nov 3, 2017

Copy link
Copy Markdown
Member

No description provided.

@mspncp mspncp left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I am not sure whether these changes lead in the right direction. For details, see my post in #4641.

Comment thread ssl/statem/statem_srvr.c Outdated

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This change is a step backwards, contrary to the original intention of the per SSL DRBGs. Since the per SSL DRBGs are short-lived and private already, it does not make sense to fall back to using RAND_priv_bytes(). In particular since one looses the advantage of lock-free access. And the latter is an important aspect if one has a lot of concurrent SSL connections.

Also, I'm not sure whether the premaster secret qualifies as a long term secret.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

The client normally sends us the premaster secret encrypted to the server's RSA key. If decrypting that fails we'll end up using this random premaster secret and continue as if decryption was successful. So it's clearly not long term, in normal cases it's not even used at all.

Comment thread ssl/ssl_lib.c Outdated

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I prefer the original name ssl_randbytes (or even better ssl_rand_bytes, which would be more consistent with the naming scheme) and don't see any sense in adding the _public suffix. See my comment on "public" and "private" random output in #4641 (comment).

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

ssl_randbytes() has been obsoleted by 16cfc2c. This needs to be rebased.

Comment thread ssl/ssl_lib.c Outdated

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

IMHO, RAND_priv_bytes() should be only used for long term secrets like RSA and ECDSA private keys. These two keys look more like session keys to me. But I'm not so familiar with libssl details, since I'm mainly using libcrypto.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

That keys is used to send an encrypted ssl ticket to the client which the client can send back in the next connection. Note that this key is in an SSL_CTX, not an SSL. As far as I know we only generate this key once per SSL_CTX, so this key might be used for a lot of connections for a long time.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Yes, I noticed that it's an SSL_CTX, so ssl_randbytes() can't be used. But I was not sure about the typical lifetime of an SSL_CTX. Does a webserver typically have only one or multiple SSL_CTX contexts? Does an SSL_CTX live througout the entire lifetime of the process? Is it passed on to forked children? etc., etc... As I said, I know too little about TLS implementation details.

Still, even if the process lives for some months, that's still much less than the lifetime of a certificate. So I thougth RAND_bytes() would be fine. But that's probably a matter of taste.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

It all depends on the application, but I think it is safe to assume that SSL_CTX is long lived and passed to children.

However, this case is interesting. An attacker would be able to grab an arbitrary amount of information from the DRBG which pushes toward using the public DRBG yet the values are long lived (process lifetime) which leans towards the private. It almost makes for a third kind.

We could instantiate a new DRBG for this and throw it away afterwards. Is this overkill?

@kroeckx

kroeckx commented Nov 4, 2017

Copy link
Copy Markdown
Member Author

The reason to start this pull request is so that people can comment on it to see in which direction it could be going.

@kroeckx

kroeckx commented Nov 5, 2017

Copy link
Copy Markdown
Member Author

I've pushed a new commit with what I think the private/public split is. There are actually cases I'm not really sure about.

@richsalz

richsalz commented Nov 5, 2017

Copy link
Copy Markdown
Contributor

I think there are many ways to slice this. Keep in mind that I don't think protecting against root, or other process that can our memory is worth trying to address. So the question becomes, can an adversary see something about one CSPRNG that gives it information about others? To me, that means having a CSPRNG per each SSL object, or if that is too much (memory or whatever) then for each SSL_CTX object. And then, maybe you have two at whatever level is appropriate -- one for private key material, and one for public. But also, maybe that is too much.

I think it makes sense to have two CSPRNG's at the global level, maybe. What "public" key information is okay to leak? Maybe it's all private? Maybe for CMS, the bulk encryption key is kind of public, since it's protected via asymmetric crypto.

I think more than two levels of CSPRNG are more than we need, but don't feel strongly about that. I think the second-level ones should seed off a global one, where the personalization string includes the memory address of the second-level object.

Another way to slice this is to have one (maybe two) per thread.

I have no information about which is better. I hope whoever works on this, or the related PR's, can justify their decisions with some facgts.

@mattcaswell mattcaswell modified the milestones: Post 1.1.1, 1.1.1 Jan 24, 2018
@kroeckx

kroeckx commented Mar 4, 2018

Copy link
Copy Markdown
Member Author

So does anybody want to comment on how to go forward with this? I'm thinking about having 2 DRBGs in the SSL object, one that has as parent the public and the other the private DRBG.

Did we have a discussion about this in some other PR/issue?

@richsalz

richsalz commented Mar 4, 2018

Copy link
Copy Markdown
Contributor

There was discussion in another PR; @mspncp probably remembers where. FWIW I think two global generators is enough. Maybe one in the SSL object to avoid any "leakage." Right now it feels like we're headed toward a separate generator for every single time we need random bytes :)

@mspncp

mspncp commented Mar 5, 2018

Copy link
Copy Markdown
Contributor

I don't remember the location, but I remember having said that the SSL drbg is in a sense already a private instance, so two SSL instances are IMHO a little overkill. I mean, we are talking about things like session keys, not long term secrets, right?

@kroeckx

kroeckx commented Mar 5, 2018

Copy link
Copy Markdown
Member Author

I think a session is actually also long term. I expect that my TLS session still can't be decrypted in X years.

Anyway, I think there are 2 ways that people seem to look at this:

  • public vs private/secret
  • long term vs short term

If you look at something like ECDHE, you create a temporary key and sign the public key. It's at least my understanding that that temporary key is important to keep secret. In #5481 @richsalz insisted that we change from RAND_bytes() to RAND_priv_bytes() for it. But I guess some people could argue that the X functions should be done with the public RNG, and the ED with the private.

For me it's usually at least clear what is considered private/secret vs public than what is long term or short term.

Anyway, I would like to move as much as possible DRBG calls use the SSL specific DRBG, which is what #5510 does. That is currently at the point that I can set a break point at RAND_bytes() and never hit it (except in SSL_CTX_new()). The only place where I currently hit RAND_priv_bytes() is in the generation of a temporary key.

But if people think this patch makes sense we would start taking the lock on the private DRBG more, and so it might make sense to have a private DRBG we don't need to lock. That could for instance be per SSL, or per thread.

@kroeckx

kroeckx commented Mar 7, 2018

Copy link
Copy Markdown
Member Author

How hard would it be to automatically create two thread local DRBGs?

@mspncp

mspncp commented Mar 7, 2018 •

Copy link
Copy Markdown
Contributor

How hard would it be to automatically create two thread local DRBGs?

Just to be sure that I understand your question right: Are you asking for the effort to replace the <public> and the <private> DRBG by per-thread DRBG instances <public_i> and <private_i> (i=1,2,...) which are automatically created and destroyed and accessed via thread local storage?

In theory, this would be a good idea, because it would make RAND_bytes() and RAND_priv_bytes() lock-free. Modifying my ascii art picture from the documentation, this would give the following overall picture:

           +--------------------+
           | os entropy sources |
           +--------------------+
                    |            
                    v           +-----------------------------+
  RAND_add() ==> <master>     <-| shared DRBG (with locking)  |
                  /   \         +-----------------------------+
                 |     |              +---------------------------+
          <public_i>  <private_i>  <- | per-thread DRBG instances |
                |        |            +---------------------------+
                |        + ==> RAND_priv_bytes()
                |        |      ^
                |        |      |
                |        |     +------------------------------------+
                |        |     | used only for long-term secrets    |
                |        |     | like private keys for certificates |
                |        |     +------------------------------------+
                |        |
                |        v
                |        <ssl->priv_drbg> ==> RAND_DRBG_bytes(ssl->priv_drbg, ...)
                |                ^                         
                |                |                         
                |               +--------------------------+
                |               | private per-SSL instance |
                |               +--------------------------+
                |
                + ==> RAND_bytes()
                |      ^
                |      |
                |     +----------------------------------+
                |     | general purpose random generator |
                |     +----------------------------------+
                v
              <ssl->drbg> ==> RAND_DRBG_bytes(ssl->drbg, ...)
                     ^
                     |
                    +-------------------------+
                    | public per-SSL instance |
                    +-------------------------+

The drawback is that thread local storage is platform and compiler specific and might not be available everywhere. It would have to be an optional feature which could be selectively enabled on suitable platforms. So it's hard to say how big the effort is. Does OpenSSL currently use thread local storage at all?

At first sight, the above picture looks a bit over-engineered, so I'm undeceded whether it's worth the effort. If at all, this would be a task for the next release. Let's discuss this after the code freeze in more detail.

@kroeckx

kroeckx commented Mar 7, 2018

Copy link
Copy Markdown
Member Author

We have thread support, including thread local storage, for pthread and windows. If it's not enabled, we don't even take a lock.

@mspncp

mspncp commented Mar 7, 2018

Copy link
Copy Markdown
Contributor

So is it that what I sketched in my last post what you were thinking of?

@kroeckx

kroeckx commented Mar 7, 2018

Copy link
Copy Markdown
Member Author

It's at least one way of doing it.

@paulidale

Copy link
Copy Markdown
Contributor

I'm okay moving the two global drbgs to be per thread. I think I mentioned per thread drbg's at one point but I was think of them in addition to the two global ones rather than replacing them.

I don't see a necessity for two per ssl drbg's but wouldn't object to having them. The greater the separation the better I feel -- so long as the overhead isn't huge -- which it isn't. @richsalz I wouldn't want to see a per random request drbg either.

Comment thread crypto/bn/bn_prime.c Outdated

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

The comment Not all primes are private right in front of a call to BN_priv_rand() is a little confusing. And by the way: What does XXX: mean?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Maybe I should replace it with TODO

Comment thread crypto/bn/bn_sqrt.c Outdated

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I'm not sure whether I always understand the rationale behind your choice of public vs. private rng.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Anything that gets send to a peer is public, the rest is private.

Comment thread crypto/bn/bn_prime.c Outdated

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Why do you use the public drbg in bn_probable_prime_dh()and the private drbg in probable_prime()?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

That doesn't make sense at all ...

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

(And clearly why the other one is marked as XXX/TODO)

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Oh, but this is about primes for use in DH, where the prime is actually send to the peer.

Comment thread ssl/ssl_lib.c Outdated

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

ssl_randbytes() has been obsoleted by 16cfc2c. This needs to be rebased.

@mspncp

mspncp commented Mar 27, 2018

Copy link
Copy Markdown
Contributor

I will continue the review after the pull request is rebased.

@kroeckx
kroeckx force-pushed the drbg_private branch 3 times, most recently from c517fb7 to 3a049d4 Compare March 31, 2018 18:00
@mspncp

mspncp commented Mar 31, 2018

Copy link
Copy Markdown
Contributor

Is this still WIP or do you consider it ready-for-review?

@kroeckx

kroeckx commented Mar 31, 2018

Copy link
Copy Markdown
Member Author

It's WIP because I didn't go over all the calls yet.

@kroeckx kroeckx changed the title WIP: Use the private RNG for data that is not public Use the private RNG for data that is not public Apr 1, 2018

@mspncp mspncp left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Looks good to me. But maybe somebody who is more familiar with the ssl code should also have a quick look over it (@richsalz? @mattcaswell?).

@mspncp mspncp added the approval: done This pull request has the required number of approvals label Apr 2, 2018
@kroeckx kroeckx closed this Apr 2, 2018
levitte pushed a commit that referenced this pull request Apr 2, 2018
Reviewed-by: Dr. Matthias St. Pierre <[email protected]>
Reviewed-by: Rich Salz <[email protected]>

Fixes: #4641
GH: #4665
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

approval: done This pull request has the required number of approvals

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants