Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Document EVP_PKEY_ASN1_METHOD and associated functions #4596

Closed
wants to merge 1 commit into from

Conversation

levitte
Copy link
Member

@levitte levitte commented Oct 26, 2017

No description provided.

@levitte levitte added the branch: master Merge to master branch label Oct 26, 2017
@levitte
Copy link
Member Author

levitte commented Oct 26, 2017

@mattcaswell, you might be interested of this...

@levitte levitte changed the title WIP: Document EVP_PKEY_ASN1_METHOD and associated functions Document EVP_PKEY_ASN1_METHOD and associated functions Oct 27, 2017
@levitte
Copy link
Member Author

levitte commented Oct 27, 2017

Ok, this is now done as far as I can see. Time for a review.

@bernd-edlinger
Copy link
Member

So supposed I want to implement a better RSAPSS algorithm,
then I can override the standard RSAPSS method, apparently.
But what if openssl is used by more than one software package in the same
address space how can I make sure they wont overwrite each other's RSAPSS
algorihm?

@levitte
Copy link
Member Author

levitte commented Oct 27, 2017

Well, an override is made by the application, or an engine if you load one (possibly through a config file), dynamically. Ultimately, that's controlled by the order in which the application does things, doesn't it?

EVP_PKEY_asn1_find_str() looks up the B<EVP_PKEY_ASN1_METHOD> with PEM
type string B<str>.
Just like EVP_PKEY_asn1_find(), it searches through the methods added
by the application first, and then through the standard methods.
Copy link
Member

Choose a reason for hiding this comment

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

really?

Copy link
Member Author

Choose a reason for hiding this comment

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

Copy link
Member

Choose a reason for hiding this comment

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

to me this looks like the standard methods win:

for (i = 0; i < EVP_PKEY_asn1_get_count(); i++) {
    ameth = EVP_PKEY_asn1_get0(i);
    if (ameth->pkey_flags & ASN1_PKEY_ALIAS)
        continue;
    if (((int)strlen(ameth->pem_str) == len)
        && (strncasecmp(ameth->pem_str, str, len) == 0))

Copy link
Member

Choose a reason for hiding this comment

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

FYI: The comment refers to EVP_PKEY_asn1_find_str

Copy link
Member Author

Choose a reason for hiding this comment

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

Ah, you are quite correct! That's... not very consistent. I guess the idea was that everyone would play nice with each other and not try to compete over the same algorithms, but rather that application would handle their own different algo.

When I look at EVP_PKEY_asn1_add0 and the way it sorts the stack every time a new method is added, it seems clear to me that the original line of thought is one of no competition, and to make sure that is always true, EVP_PKEY_asn1_add0 should refuse to add a method for an algo that already has one. In that case, the exact order in which these methods are found don't matter at all.

Copy link
Member

Choose a reason for hiding this comment

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

Yes, that would be a significant improvement.

Copy link
Member

Choose a reason for hiding this comment

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

Should the check in the add method also look for duplicate strings?

EVP_PKEY_asn1_find() looks up the B<EVP_PKEY_ASN1_METHOD> with NID
B<type>.
It searches through the methods added by the application first, and
then through the standard methods.
Copy link
Member

Choose a reason for hiding this comment

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

and what happens if pe != NULL ?

Copy link
Member

Choose a reason for hiding this comment

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

that looks like the engine always wins ?

Copy link
Member

Choose a reason for hiding this comment

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

in EVP_PKEY_asn1_find the first loop changes "type" if ASN1_PKEY_ALIAS
is in app_methods, and then if pe != NULL
ENGINE_get_pkey_asn1_meth_engine can look up something,
otherwise "t" from the frst loop is returned.
Weird...

Copy link
Member

Choose a reason for hiding this comment

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

If I understand you correct, then it should not matter where we search first.
In that case this sentence can be removed.
However I wonder why the engine search uses the aliases at all, maybe the
whole "if (pe)" block should move up?

Copy link
Member

Choose a reason for hiding this comment

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

previous comment refers to EVP_PKEY_asn1_find

Copy link
Member Author

Choose a reason for hiding this comment

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

The loop at the start is there to follow aliases down to the root identity, which means that the methods provided by engines can't be aliases. An engine can of course still provide an alias by adding them directly with EVP_PKEY_asn1_add_alias, but that will end up in the app_methods stack anyway... So the loop to resolve aliases should come first, as it does.

Copy link
Member

Choose a reason for hiding this comment

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

Maybe the documentation should reflect that ?

Copy link
Member Author

Choose a reason for hiding this comment

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

Dunno how it would here. EVP_PKEY_asn1_add_alias is the only way to add an alias, and it's not ENGINE specific, so...

The ENGINE API documentation needs to be refreshed for sure, though. That's for a different PR...

Copy link
Member

Choose a reason for hiding this comment

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

More or less make one sentence out of your previous two comments.
I believe the rationale is not obvious.

@bernd-edlinger
Copy link
Member

I would have liked to have a way to store the app_methods in a context structure,
not in a static.

@levitte
Copy link
Member Author

levitte commented Oct 27, 2017

In what context?

@bernd-edlinger
Copy link
Member

Maybe it is a silly idea or just infeasible...

But I mean as an extra parameter maybe similar to an EVP object, which
contains a lookup for algorithms I want to use in the requested operation.

@levitte
Copy link
Member Author

levitte commented Oct 27, 2017

I think it's an infeasible idea in this case, sorry...

@levitte
Copy link
Member Author

levitte commented Oct 27, 2017

@bernd-edlinger, I hope the new commits I just pushed answer your concerns.

EVP_PKEY_asn1_get0() return a public key method or B<NULL> if B<idx> is
out of range.

EVP_PKEY_asn1_get0_info() returns 0 on failure, 1 on success.
Copy link
Member

Choose a reason for hiding this comment

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

maybe it should be noted that these APIs are not thread-safe?

Copy link
Member Author

Choose a reason for hiding this comment

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

See #4606

B<EVP_PKEY_ASN1_METHOD> object otherwise.

EVP_PKEY_asn1_add0() and EVP_PKEY_asn1_add_alias() return 0 on error,
or 1 on success.
Copy link
Member

Choose a reason for hiding this comment

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

Also these APIs are not thread-safe, either make the thread-safe or document that?

Copy link
Member Author

Choose a reason for hiding this comment

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

See #4606

The pub_cmp() method is called when two public keys are to be
compared.
It MUST return 1 when the keys are equal, 0 otherwise.
it's called by L<EVP_PKEY_cmp(3)>.
Copy link
Member

Choose a reason for hiding this comment

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

new sentence, start with upper case.

Copy link
Member Author

Choose a reason for hiding this comment

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

Yup

The pub_print() method is called to print a public key in humanly
readable text to B<out>, indented B<indent> spaces.
It MUST return 0 on error, 1 on success.
it's called by L<EVP_PKEY_print_public(3)>.
Copy link
Member

Choose a reason for hiding this comment

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

Likewise.

Copy link
Member Author

Choose a reason for hiding this comment

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

Yup

The priv_decode() and priv_encode() methods are called to decode /
encode B<PKCS8_PRIV_KEY_INFO> form private key to / from B<pk>.
They MUST return 0 on error, 1 on success.
they're called by L<EVP_PKCS82PKEY(3)> and L<EVP_PKEY2PKCS8(3)>.
Copy link
Member

Choose a reason for hiding this comment

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

and here.

Copy link
Member Author

Choose a reason for hiding this comment

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

Done

@levitte
Copy link
Member Author

levitte commented Oct 30, 2017

Because the thread safety question isn't quite resolved yet and I don't want this PR to wait endlessly, I just added a commit with thread safety information. Looks good?

Copy link
Member

@bernd-edlinger bernd-edlinger left a comment

Choose a reason for hiding this comment

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

Yes, looks good, the other PR needs to be merged first.

@levitte
Copy link
Member Author

levitte commented Oct 30, 2017

Hmmm, most of this should also be backported to 1.1.0. I'll prepare a separate PR for it, considering master has added functionality...

@bernd-edlinger
Copy link
Member

Is the red cross from travis a doc-nits ?

levitte added a commit that referenced this pull request Oct 30, 2017
[skip ci]

Reviewed-by: Bernd Edlinger <bernd.edlinger@hotmail.de>
(Merged from #4596)
levitte added a commit that referenced this pull request Oct 30, 2017
No two public key ASN.1 methods with the same pkey_id can be
registered at the same time.

Reviewed-by: Bernd Edlinger <bernd.edlinger@hotmail.de>
(Merged from #4596)
@levitte levitte force-pushed the EVP_PKEY_ASN1_METHOD_docs branch 2 times, most recently from 29ea155 to d8586bb Compare October 30, 2017 17:26
@levitte
Copy link
Member Author

levitte commented Oct 30, 2017

Good catch! Added commit should resolved the matter

@levitte
Copy link
Member Author

levitte commented Oct 30, 2017

Unfortunately, I already merged the commits before the make update one... 751148e and d85722d

@bernd-edlinger
Copy link
Member

Sorry, for not checking earlier.

@levitte
Copy link
Member Author

levitte commented Oct 30, 2017

It's alright, but I need you to approve the added commit

levitte added a commit to levitte/openssl that referenced this pull request Oct 30, 2017
[skip ci]

Reviewed-by: Bernd Edlinger <bernd.edlinger@hotmail.de>
(Merged from openssl#4596)
levitte added a commit to levitte/openssl that referenced this pull request Oct 30, 2017
No two public key ASN.1 methods with the same pkey_id can be
registered at the same time.

Reviewed-by: Bernd Edlinger <bernd.edlinger@hotmail.de>
(Merged from openssl#4596)
@levitte
Copy link
Member Author

levitte commented Oct 30, 2017

Merged the last commit as well. 79204b9

@levitte levitte closed this Oct 30, 2017
levitte added a commit that referenced this pull request Oct 30, 2017
Reviewed-by: Bernd Edlinger <bernd.edlinger@hotmail.de>
(Merged from #4596)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
branch: master Merge to master branch
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants