Skip to content

Add the ability to set raw private/public keys in EVP - #5520

Closed
mattcaswell wants to merge 14 commits into
openssl:masterfrom
mattcaswell:curve448-set-priv
Closed

mattcaswell wants to merge 14 commits into
openssl:masterfrom
mattcaswell:curve448-set-priv

Conversation

@mattcaswell

Copy link
Copy Markdown
Member

The EVP interface that we have for X25519/X448/Ed25519/X25519 is very bare bones and provides just enough to use it for TLS. It is difficult to use directly because it is hard to setup private/public keys. You either have to load them from a file, or generate completely new ones. If you have the raw key data from some external source you cannot get OpenSSL to use it.

This PR adds functions to enable that. It provides a new function which is almost identical in behaviour to the existing EVP_PKEY_new_mac_key(). This PR introduces the function EVP_PKEY_new_private_key() which can be used by all the 25519/448 functions, as well as all of the functions supported by EVP_PKEY_new_mac_key(). Although the external behaviour between the two is similar the internal implementation is completely different. The initial attempt to implement these new functions was in #5419 and was heavily based on the EVP_PKEY_new_mac_key() implementation. However that implementation turned out to be controversial and was therefore removed from #5419.

This new PR implements the same functions as were removed from #5419 but does so in a completely different (and I hope less controversial) way.

I think this should be included in the beta1 release.

Checklist
  • documentation is added or updated
  • tests are added or updated

@mattcaswell
mattcaswell requested a review from kroeckx March 5, 2018 19:36
@mattcaswell

Copy link
Copy Markdown
Member Author

@kroeckx - any thoughts on this?

@levitte

levitte commented Mar 8, 2018

Copy link
Copy Markdown
Member

I've some thoughts on this...

  1. EVP_PKEY_new_private_key is a very generic name, and intuitively, I'm sure someone would want to create a RSA EVP_PKEY using this, is there any plans to expand this functionality to cover all public key algos that we implement?
  2. What, exactly, is "raw key data"? Is it formatted in any way? I'm thinking that for an RSA key, there are a few numbers, so the "raw key data" would have to be formatted precisely. Me, I'd expect DER formatting, and quite frankly, since we often talk about raw DER (as opposed to PEM formatted DER), I can imagine others will think along the same lines. This may be important to know for anyone wanting to implement the method functions to support this new functionality, and should be documented.

Comment thread crypto/include/internal/asn1_int.h Outdated

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.

You should add functions to set these, in crypto/asn1/ameth_lib.c, and document them in doc/man3/EVP_PKEY_ASN1_METHOD.pod

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.

Good point. Done.

@mattcaswell

Copy link
Copy Markdown
Member Author
  1. EVP_PKEY_new_private_key is a very generic name, and intuitively, I'm sure someone would want to create a RSA EVP_PKEY using this, is there any plans to expand this functionality to cover all public key algos that we implement?
  2. What, exactly, is "raw key data"? Is it formatted in any way? I'm thinking that for an RSA key, there are a few numbers, so the "raw key data" would have to be formatted precisely. Me, I'd expect DER formatting, and quite frankly, since we often talk about raw DER (as opposed to PEM formatted DER), I can imagine others will think along the same lines. This may be important to know for anyone wanting to implement the method functions to support this new functionality, and should be documented.

Well I was thinking slightly differently. I see the private/public keys set via these functions as just a block of binary data without any real structure to it. Instead, where there are algos which require a structured key we should have an individual function for each of them, e.g. EVP_PKEY_new_RSA_key(), EVP_PKEY_new_EC_key(), EVP_PKEY_new_DSA_key().

Each of those would then take the relevant component parts required to construct the key as arguments e.g.

EVP_PKEY *EVP_PKEY_new_RSA_key(ENGINE *e, BIGNUM *n, BIGNUM *e, BIGNUM *d);

You can of course already do this today by creating an RSA structure first and then assigning it to the EVP_PKEY - but that requires use of the low level algo functions which we are trying to discourage.

But all of that is for a different PR. I wanted to see how this one was received first.

@mattcaswell

Copy link
Copy Markdown
Member Author

New commit pushed addressing comments.

@mattcaswell

Copy link
Copy Markdown
Member Author

Ping @levitte.

Not all algorithms will support this, since their keys are not a simple
block of data. But many can.
Also to use the new EVP_PKEY_new_CMAC_key()
Also adds some documentation for related existing functions/macros
Previously private and public keys had to be pem encoded to be read by
evp_test. This enables us to embed the raw private/public key values
in the test file. The algorithm has to support EVP_PKEY_new_private_key()
and EVP_PKEY_new_public_key() for this to work.
Include more information about how to create keys for these algorithms.
@mattcaswell

Copy link
Copy Markdown
Member Author

Rebased to resolve a conflict with master. @levitte?

@levitte

levitte commented Mar 15, 2018

Copy link
Copy Markdown
Member

I read your explanation, and while I understand what you say and can get along with that, I stil think that a very generic name like EVP_PKEY_new_private_key intuitively leads people to think that it should be usable for all sorts of EVP_PKEY. I can certainly go along with the thought that you simply feed it random bits and have the underlying method functions deal with it, and I can see this extend to having those functions decide the format, which could be whatever.

However, unless the intent is to have the function being used as generically as the name suggests (regardless of the itty gritty details in the docs), I cannot see that function name as acceptable.

@t-j-h

t-j-h commented Mar 15, 2018

Copy link
Copy Markdown
Member

So would EVP_PKEY_new_binary_key or EVP_PKEY_new_raw_key be better names?

@levitte

levitte commented Mar 15, 2018

Copy link
Copy Markdown
Member

That's not really the point... but you know, whatever @mattcaswell says, I'm sure that sooner or later, someone is going to want to create RSA keys that way, and will implement the necessary method. With that line of thought, I can play :-)

@levitte

levitte commented Mar 15, 2018

Copy link
Copy Markdown
Member

... although, even though add raw into the name doesn't make it much less generic, it does add a bit more clarity to what we're dealing with, so yes, I think I'd like that name change (and preferably raw... if nothing else then because it's shorter ;-))

Renamed to EVP_PKEY_new_raw_private_key()/EVP_new_raw_public_key() as per
feedback.
@mattcaswell

Copy link
Copy Markdown
Member Author

Ok I did the rename (pushed). Is this better @levitte?

@levitte levitte left a comment

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.

Yes

@mattcaswell

Copy link
Copy Markdown
Member Author

Pushed (I fixed a trivial whitespace issue in libcrypto.num first).

levitte pushed a commit that referenced this pull request Mar 15, 2018
Not all algorithms will support this, since their keys are not a simple
block of data. But many can.

Reviewed-by: Richard Levitte <[email protected]>
(Merged from #5520)
levitte pushed a commit that referenced this pull request Mar 15, 2018
levitte pushed a commit that referenced this pull request Mar 15, 2018
Reviewed-by: Richard Levitte <[email protected]>
(Merged from #5520)
levitte pushed a commit that referenced this pull request Mar 15, 2018
Reviewed-by: Richard Levitte <[email protected]>
(Merged from #5520)
levitte pushed a commit that referenced this pull request Mar 15, 2018
levitte pushed a commit that referenced this pull request Mar 15, 2018
Reviewed-by: Richard Levitte <[email protected]>
(Merged from #5520)
levitte pushed a commit that referenced this pull request Mar 15, 2018
Also to use the new EVP_PKEY_new_CMAC_key()

Reviewed-by: Richard Levitte <[email protected]>
(Merged from #5520)
levitte pushed a commit that referenced this pull request Mar 15, 2018
Also adds some documentation for related existing functions/macros

Reviewed-by: Richard Levitte <[email protected]>
(Merged from #5520)
levitte pushed a commit that referenced this pull request Mar 15, 2018
Previously private and public keys had to be pem encoded to be read by
evp_test. This enables us to embed the raw private/public key values
in the test file. The algorithm has to support EVP_PKEY_new_private_key()
and EVP_PKEY_new_public_key() for this to work.

Reviewed-by: Richard Levitte <[email protected]>
(Merged from #5520)
levitte pushed a commit that referenced this pull request Mar 15, 2018
levitte pushed a commit that referenced this pull request Mar 15, 2018
Include more information about how to create keys for these algorithms.

Reviewed-by: Richard Levitte <[email protected]>
(Merged from #5520)
levitte pushed a commit that referenced this pull request Mar 15, 2018
levitte pushed a commit that referenced this pull request Mar 15, 2018
levitte pushed a commit that referenced this pull request Mar 15, 2018
Renamed to EVP_PKEY_new_raw_private_key()/EVP_new_raw_public_key() as per
feedback.

Reviewed-by: Richard Levitte <[email protected]>
(Merged from #5520)
@kareldonk

Copy link
Copy Markdown

Any chance to get a set of functions to export the raw key data to a buffer to use elsewhere outside of openssl?

@richsalz

richsalz commented Apr 3, 2018

Copy link
Copy Markdown
Contributor

It's too late to add this new feature for this release, sorry.

@mattcaswell

Copy link
Copy Markdown
Member Author

Any chance to get a set of functions to export the raw key data to a buffer to use elsewhere outside of openssl?

Darn...I should have added that. Please could you create an issue for this?

@kareldonk

Copy link
Copy Markdown

Yes I will thanks, and even if it doesn't make it in the current release, it would be very useful to have in a future release.

@ms-s

ms-s commented May 22, 2018

Copy link
Copy Markdown

@mattcaswell @kareldonk I can confirm that a feature to export raw keys would be very useful. I use Curve25519 and send the public keys on the wire in JSON Web Key format (RFC7517). I can import the peer key received on the wire with EVP_PKEY_new_public_key() and derive the shared secret. For exporting I just read the last 32 bytes (BITSTRING) from the EVP_PKEY. Would be nice to have a cleaner solution in the next release.

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.

6 participants