Skip to content

Comments

EVP: Implement data-driven translation between known ctrl and OSSL_PARAMs#13913

Closed
levitte wants to merge 18 commits intoopenssl:masterfrom
levitte:ctrl-params-translate
Closed

EVP: Implement data-driven translation between known ctrl and OSSL_PARAMs#13913
levitte wants to merge 18 commits intoopenssl:masterfrom
levitte:ctrl-params-translate

Conversation

@levitte
Copy link
Member

@levitte levitte commented Jan 20, 2021

The idea is to make it as transparent as possible to call things like
EVP_PKEY_CTX_ctrl() with a provider backed EVP_PKEY_CTX, or things
like EVP_PKEY_get_bn_param() with a legacy EVP_PKEY.

All these sorts of calls demand that we translate between ctrl
commands and OSSL_PARAM keys, and treat the arguments appropriately.

This implementation has it being as data driven as possible, thereby
centralizing everything into one table of translation data, which
supports both directions.

Fixes #13528

@levitte levitte added the branch: master Applies to master branch label Jan 20, 2021
@levitte
Copy link
Member Author

levitte commented Jan 20, 2021

Note that this isn't finished yet, there are fixup functions missing. However, the functionality and structure have crystallised enough to make the idea reviewable. Please comment, even it doesn't build, early feedback welcome!

@richsalz
Copy link
Contributor

The fixup functions need to know the insize and outsize of all params, right? This means that third-party providers cant add new controls. Or am I just misreading the code?

@levitte
Copy link
Member Author

levitte commented Jan 21, 2021

@richsalz, I think you've misunderstood the purpose.

  1. This is entirely in libcrypto and about stuff that libcrypto knows. Period. Providers are not involved (more than being recipients of OSSL_PARAM data), at all.
  2. This is a tool that translates between ctrl functions and ctrl command numbers that libcrypto knows and OSSL_PARAM functions and names that libcrypto knows, in both directions.

Regarding insize and outsize, the OSSL_PARAM structure already delivers that. However, the limiting factor is the ctrl functions, which do not easily supply this (well, you can pass the insize as p1, and you can ask for an outsize with a separate ctrl command and passing the pointer to a size_t as p2...)

So in the end, this is meant to support calling EVP_PKEY_CTX_ctrl() with an EVP_PKEY_CTX that's set up for a provider backed operation, to support calling EVP_PKEY_CTX_set_params() with an EVP_PKEY_CTX that's set up for a legacy operation (limited to things libcrypto knows about), and to support calling EVP_PKEY_get_bn_param() with a legacy EVP_PKEY.

The life time of this source file is the same as the lifetime of legacy EVP_PKEYs, I totally expect them to be gone when ENGINE support is gone (ENGINE support being the last tangible reason why we retain legacy style EVP_PKEYs). That's also a reason to have all of this collected into one file.

(while I'm at it, I also mean to clean away the current hackery which is spread all over the source instead of being concentrated to one spot, and that includes huge amounts of code copying ... you've heard me battle that tendency before 😉)

@t8m
Copy link
Member

t8m commented Jan 21, 2021

This would be a great improvement! It would also allow to drop all the new convenience functions such as EVP_PKEY_get_group_name(). But on the other hand they could still be kept, just with very much simplified implementation.

@t8m t8m mentioned this pull request Jan 21, 2021
2 tasks
@levitte
Copy link
Member Author

levitte commented Jan 21, 2021

This would be a great improvement! It would also allow to drop all the new convenience functions such as EVP_PKEY_get_group_name(). But on the other hand they could still be kept, just with very much simplified implementation.

... and [ahem] we can revert all the older convenience functions to being macros calling EVP_PKEY_CTX_ctrl() 😆

@richsalz
Copy link
Contributor

Thank you for the detailed explanation. That should make it into the code/comments I think.

At one point we liked the wrapper functions because they provided type-safety.

@t8m
Copy link
Member

t8m commented Jan 21, 2021

Thank you for the detailed explanation. That should make it into the code/comments I think.

At one point we liked the wrapper functions because they provided type-safety.

IMO they definitely make sense for more common things. However for obscure parameters they make less sense as they contribute to API and code bloat.

@levitte
Copy link
Member Author

levitte commented Jan 21, 2021

At one point we liked the wrapper functions because they provided type-safety.

I agree that for ctrl wrapper, that's a nice benefit.

@levitte levitte force-pushed the ctrl-params-translate branch from e1afcbd to 0cc392f Compare January 25, 2021 14:53
@levitte levitte added this to the 3.0.0 beta1 milestone Jan 26, 2021
@jon-oracle
Copy link
Contributor

Is there some test coverage in this area? I have some ad-hoc tests for legacy vs new EVP_PKEYs which I can clean up and supply a commit for.

@levitte
Copy link
Member Author

levitte commented Jan 27, 2021

Please do, thank you 😊

@jon-oracle
Copy link
Contributor

Some tests. Will add checks for other get/set APIs in due course:

5198095

@levitte
Copy link
Member Author

levitte commented Jan 27, 2021

Thanks

Side note: github seemed to assume that commit belongs in openssl/openssl... it took me a moment to find out where it really belonged 😉

@t8m
Copy link
Member

t8m commented Feb 4, 2021

Linked to otc evaluated issue.

@paulnelsontx paulnelsontx self-assigned this Feb 4, 2021
@levitte levitte force-pushed the ctrl-params-translate branch 2 times, most recently from 9bb0069 to 634fc52 Compare February 5, 2021 09:17
@paulnelsontx paulnelsontx modified the milestones: 3.0.0 beta1, Sprint-Hydrogen Feb 8, 2021
@levitte levitte force-pushed the ctrl-params-translate branch from 23b0d4c to bce4661 Compare February 8, 2021 16:27
@paulnelsontx paulnelsontx modified the milestones: Sprint-Hydrogen, Sprint Hydrogen Feb 8, 2021
@levitte levitte force-pushed the ctrl-params-translate branch from 00f8f72 to df75f5a Compare February 8, 2021 22:25
@t8m
Copy link
Member

t8m commented Feb 10, 2021

There is one thing that comes to me a little bit awkward is that for the helper functions that used to call either set/get params or ctrl based on whether the key is legacy (like for example EVP_PKEY_CTX_set_dh_kdf_type()) we now call just the ctrl. Which is an obsolete API call so perhaps we should avoid using it internally. On the other hand it is indicative that it is extremely simple to convert the helpers to the ctrl call (as they were previously just macros, it makes it kind of obvious) but it is fairly harder to use the set/get_params instead. Could this mean we are missing some helper functions or macros for working with params?

@levitte levitte force-pushed the ctrl-params-translate branch from 99bc21d to 2354269 Compare February 22, 2021 10:36
@levitte
Copy link
Member Author

levitte commented Feb 22, 2021

Rebasing to resolve conflicts

Copy link
Member

@t8m t8m left a comment

Choose a reason for hiding this comment

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

Approved!

@t8m t8m added approval: done This pull request has the required number of approvals and removed approval: review pending This pull request needs review by a committer labels Feb 22, 2021
Copy link
Contributor

@paulidale paulidale left a comment

Choose a reason for hiding this comment

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

Some little nits. Nothing significant.

Good comments, they really help with comprehension.

It's a bit of a shame to have such beautiful code for legacy support.

@levitte
Copy link
Member Author

levitte commented Feb 23, 2021

It's a bit of a shame to have such beautiful code for legacy support.

Legacy will go, but at least it will get to leave with some panache 😉

@t8m
Copy link
Member

t8m commented Feb 23, 2021

This triggers a question - should we deprecate EVP_PKEY_CTX_ctrl and all the other EVP*ctrl functions?

@mattcaswell
Copy link
Member

This triggers a question - should we deprecate EVP_PKEY_CTX_ctrl and all the other EVP*ctrl functions?

Yes, we probably should.

@t8m
Copy link
Member

t8m commented Feb 23, 2021

Issue filed #14287 for the deprecations.

@levitte levitte added approval: ready to merge The 24 hour grace period has passed, ready to merge and removed approval: done This pull request has the required number of approvals labels Feb 23, 2021
openssl-machine pushed a commit that referenced this pull request Feb 23, 2021
This does what was previously done by looking at pctx->pmeth->pkey_id,
but handles both legacy and provider side contexts, and is supposed to
become a replacement for the old way.

Reviewed-by: Tomas Mraz <[email protected]>
Reviewed-by: Paul Dale <[email protected]>
(Merged from #13913)
openssl-machine pushed a commit that referenced this pull request Feb 23, 2021
This will help with transitioning diverse functions to be able to use the
ctrl<->OSSL_PARAM translators.

Reviewed-by: Tomas Mraz <[email protected]>
Reviewed-by: Paul Dale <[email protected]>
(Merged from #13913)
openssl-machine pushed a commit that referenced this pull request Feb 23, 2021
Reviewed-by: Tomas Mraz <[email protected]>
Reviewed-by: Paul Dale <[email protected]>
(Merged from #13913)
openssl-machine pushed a commit that referenced this pull request Feb 23, 2021
…RAMs

The idea is to make it as transparent as possible to call things like
EVP_PKEY_CTX_ctrl() with a provider backed EVP_PKEY_CTX, or things
like EVP_PKEY_get_bn_param() with a legacy EVP_PKEY.

All these sorts of calls demand that we translate between ctrl
commands and OSSL_PARAM keys, and treat the arguments appropriately.

This implementation has it being as data driven as possible, thereby
centralizing everything into one table of translation data, which
supports both directions.

Fixes #13528

Reviewed-by: Tomas Mraz <[email protected]>
Reviewed-by: Paul Dale <[email protected]>
(Merged from #13913)
openssl-machine pushed a commit that referenced this pull request Feb 23, 2021
In the interest of calling these functions on legacy EVP_PKEY
contexts, only check the settable / gettable params for provider side
keys, leaving to the translated EVP_PKEY_CTX_ctrl() call check the
ctrl commands on its own.

Reviewed-by: Tomas Mraz <[email protected]>
Reviewed-by: Paul Dale <[email protected]>
(Merged from #13913)
openssl-machine pushed a commit that referenced this pull request Feb 23, 2021
EVP_PKEY_get_group_name() now simply calls EVP_PKEY_get_utf8_string_param().
EVP_PKEY_CTX_set_group_name() now simply calls EVP_PKEY_CTX_set_params().

EVP_PKEY_get_bn_param(), EVP_PKEY_get_octet_string_param(),
EVP_PKEY_get_utf8_string_param() and EVP_PKEY_get_int_param() can now
handle legacy EVP_PKEYs by calling evp_pkey_get_params_to_ctrl().

EVP_PKEY_CTX_get_params() can now handle a legacy backed EVP_PKEY_CTX
by calling evp_pkey_ctx_get_params_to_ctrl().

Note: EVP_PKEY_CTX_set_params() doesn't call the translator yet.
      Should it ever?

Reviewed-by: Tomas Mraz <[email protected]>
Reviewed-by: Paul Dale <[email protected]>
(Merged from #13913)
openssl-machine pushed a commit that referenced this pull request Feb 23, 2021
legacy_ctrl_to_param() and legacy_ctrl_str_to_param() are now
replaced with calls to evp_pkey_ctx_ctrl_to_param() and
evp_pkey_ctx_ctrl_str_to_param().

Reviewed-by: Tomas Mraz <[email protected]>
Reviewed-by: Paul Dale <[email protected]>
(Merged from #13913)
openssl-machine pushed a commit that referenced this pull request Feb 23, 2021
openssl-machine pushed a commit that referenced this pull request Feb 23, 2021
openssl-machine pushed a commit that referenced this pull request Feb 23, 2021
It would check the keytype and optype before determining if it even
supported the ctrl command number.  This turned out to be disruptive,
so we make it check that it supports the request ctrl command number
first.

Reviewed-by: Tomas Mraz <[email protected]>
Reviewed-by: Paul Dale <[email protected]>
(Merged from #13913)
openssl-machine pushed a commit that referenced this pull request Feb 23, 2021
@levitte
Copy link
Member Author

levitte commented Feb 23, 2021

Merged

6179dfc EVP: Implement EVP_PKEY_CTX_is_a()
e19246d EVP: Make evp_pkey_ctx_state() available to all of EVP
4d4928e EVP: make evp_pkey_is_assigned() usable in the FIPS module
9a1c4e4 EVP: Implement data-driven translation between known ctrl and OSSL_PARAMs
5137312 EVP: Make evp_pkey_ctx_{set,get}_params_strict() legacy aware
6fcd92d EVP: Adapt diverse OSSL_PARAM setters and getters
5524580 EVP: Adapt the EVP_PKEY_CTX ctrl functions
df4592c EVP: Adapt the DH specific EVP_PKEY_CTX setter / getter functions
13f91a7 EVP: Adapt the RSA specific EVP_PKEY_CTX setter / getter functions
bbf4dc9 EVP: Make checks in evp_pkey_ctx_store_cached_data() more restricted
f5b0083 EVP: Adapt the EC_KEY specific EVP_PKEY_CTX setter / getter functions

@levitte levitte closed this Feb 23, 2021
@levitte levitte deleted the ctrl-params-translate branch February 23, 2021 12:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

approval: ready to merge The 24 hour grace period has passed, ready to merge branch: master Applies to master branch

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Calling EVP_PKEY_get_bn_param() on a legacy key does not work

7 participants