Skip to content

WIP: Allocate param array entries dynamically.#9254

Closed
paulidale wants to merge 3 commits intoopenssl:masterfrom
paulidale:params-alloc-indiv
Closed

WIP: Allocate param array entries dynamically.#9254
paulidale wants to merge 3 commits intoopenssl:masterfrom
paulidale:params-alloc-indiv

Conversation

@paulidale
Copy link
Contributor

This provides an alternative for the OSSL_PARAM_construct_ series of functions which allocate storage for the values rather than relying on local on stack storage. The new OSSL_PARAM_allocate_ and the existing OSSL_PARAM_construct_ calls can be freely intermingled.

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

@paulidale paulidale added the branch: master Applies to master branch label Jun 26, 2019
@paulidale
Copy link
Contributor Author

paulidale commented Jun 26, 2019

This is intended to provide a way for OSSL_PARAM structures to escape a stack frame more easily.
It isn't a direct competitor to #9246, although the underlying aim is similar.

Tests, documentation, string types, BN and bug fixing are still required.

@levitte
Copy link
Member

levitte commented Jun 27, 2019

I prefer this over #9246.
For clarity, documentation should contain examples on how to keep track of allocated OSSL_PARAM elements, such as:

    params[foo_pos = n++] = OSSL_PARAM_allocate_int("foo", 42);

... or:

    foo_pos = n;
    params[n++] = OSSL_PARAM_allocate_int("foo", 42);

@paulidale paulidale mentioned this pull request Jun 27, 2019
@paulidale
Copy link
Contributor Author

My intention is to have a flag bit (or type bit) to indicate allocated. Because the function must return an OSSL_PARAM structure, error detection needs to occur later -- I've included a function for this here. Likewise, a free function would be desirable.

@levitte
Copy link
Member

levitte commented Jun 27, 2019

What you're implying here as well as in #9139 is that the creator of the OSSL_PARAM array looses control of that array, i.e stops being the owner. I think that's a bad direction to follow, and adds a ton of complexity.

@mattcaswell
Copy link
Member

I have a different approach to this in the works. At the moment it is EVP specific (because I wrote it for a very specific problem) but it could easily be made generic. It's not yet ready for prime time so I've not made a PR from it yet. See:

https://github.com/mattcaswell/openssl/blob/c3298b53dbe5c1543695549e28fe97a7fc7e2ea3/crypto/evp/evp_lib.c#L780-L819

The idea is to create a temporary "template" which then gets converted to an OSSL_PARAM at the end in a single OPENSSL_zalloc call (or OPENSSL_secure_zalloc as appropriate). The whole OSSL_PARAM structure can then be passed around as required, and the whole thing can be freed with a single OPENSSL_free call at the end.

@paulidale
Copy link
Contributor Author

The creator doesn't lose control in either.

Here, the creator has complete control -- populate the OSSL_PARAM array, pass it to the consumer, free it.

In #9246 the creator needs to keep control, the creator allocates the memory and must free it. Call the creator to get the param array, populate it, call the creator to use it and either call again to free it or have the use free as well.

@paulidale
Copy link
Contributor Author

@mattcaswell, that's kind of heading in a similar direction to #9246 with its single allocation/free. I used a varargs function rather than building the description up piecemeal but the intent seems similar.

@mattcaswell
Copy link
Member

Note that whatever solution is eventually chosen, it will need to be able to handle secure malloc so that we can pass private data as an OSSL_PARAM.

@richsalz
Copy link
Contributor

The template approach mentioned in #9254 (comment) is what I did for #8377; defining the parameters was a separate step from allocating storage.

This provides an alternative for the OSSL_PARAM_construct_ series
of functions which allocate storage for the values rather than relying on local
on stack storage.  The new OSSL_PARAM_allocate_ and the existing
OSSL_PARAM_construct_ calls can be freely intermingled.
@paulidale paulidale force-pushed the params-alloc-indiv branch from fb15f59 to d21b545 Compare July 14, 2019 22:29
Copy link
Contributor

@richsalz richsalz left a comment

Choose a reason for hiding this comment

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

I read through this, but only looked at the secure BN stuff (it's too hairy and complicated for me to follow, sorry). The secure BN part seems okay.


if (p != NULL) {
if (data == NULL)
memset(p, 0, data_size);
Copy link
Contributor

Choose a reason for hiding this comment

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

use ?: instead of the nested if.

crypto/params.c Outdated
if (bsize == 0)
bsize = (size_t)BN_num_bytes(bn);

if (BN_get_flags(bn, BN_FLG_SECURE)) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Add == BN_FLG_SECURE

Also, this function needs comments.

crypto/params.c Outdated
flag |= OWNER_FLAG_secure;
}
}
if (secure)
Copy link
Contributor

Choose a reason for hiding this comment

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

I prefer the ?: operator, IMO

@paulidale paulidale closed this Jul 17, 2019
@paulidale paulidale deleted the params-alloc-indiv branch September 19, 2019 02:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

branch: master Applies to master branch

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants