Skip to content

Prerequisite for tracing chain building: refactor find_issuer() etc. in x509_lu.c and x509_vfy.c - #18762

Closed
DDvO wants to merge 2 commits into
openssl:masterfrom
siemens:x590_vfy-lu_refactor_find_issuer
Closed

DDvO wants to merge 2 commits into
openssl:masterfrom
siemens:x590_vfy-lu_refactor_find_issuer

Conversation

@DDvO

@DDvO DDvO commented Jul 9, 2022 •

Copy link
Copy Markdown
Contributor

This is a prerequisite for #18761. When preparing the new trace feature for cert chain building, I found again that there is an ugly overlap between

  • STACK_OF(X509) *X509_STORE_CTX_get1_certs(X509_STORE_CTX *ctx, const X509_NAME *nm)
  • X509_STORE_CTX_get1_issuer() in x509_lu.c used to search for suitable trusted certs
  • the local function find_issuer() in x509_vfy.c used to search for both untrusted and trusted certs.

Among others, this means that any changes (such as the addition of trace output) would need to be done consistently in their function bodies.

Moreover, most of X509_STORE_CTX_get1_issuer() does not really belong to x509_lu.c but to x509_vfy.c,
but not all of its implementation can be moved because it includes collecting certs via the internal X509/CRL object lookup mechanism.
When adding the tracing for chain building, I also noticed that the function checks the first cert found twice, which of course is needless and should be avoided.

This PR fixes all this, while not changing the documented/commented semantics of these functions,
by letting X509_STORE_CTX_get1_issuer() make use of X509_STORE_CTX_get1_certs()
by factoring out from X509_STORE_CTX_get1_certs() and X509_STORE_CTX_get1_issuer() a new function

and moving the rest to x509_vfy.c.

These functions are now implemented using a more general internal function

static X509 *get0_best_issuer_sk(X509_STORE_CTX *ctx, int trusted,
                                 int no_dup, STACK_OF(X509) *sk, X509 *x)

which will be extended by #18761 to provide tracing.

This PR also renames the local function get_issuer_sk() to get1_best_issuer_other_sk() in x509_vfy.c.

@DDvO DDvO added the triaged: refactor The issue/pr requests/implements refactoring label Jul 9, 2022
@DDvO DDvO changed the title x590_lu.c and x509_vfy.c: refactor find_issuer() and friends x590_lu.c and x509_vfy.c: refactor find_issuer() and friends Jul 9, 2022
@DDvO
DDvO force-pushed the x590_vfy-lu_refactor_find_issuer branch 2 times, most recently from 327ed1b to 208987b Compare July 10, 2022 06:57
@DDvO
DDvO requested a review from vdukhovni July 10, 2022 07:53
@DDvO
DDvO force-pushed the x590_vfy-lu_refactor_find_issuer branch 2 times, most recently from 6da4668 to 28ded08 Compare July 10, 2022 21:15
@DDvO
DDvO force-pushed the x590_vfy-lu_refactor_find_issuer branch from 28ded08 to 594542b Compare July 11, 2022 20:52
@DDvO
DDvO requested a review from t8m July 12, 2022 12:03
@t8m t8m added branch: master Applies to master branch approval: review pending This pull request needs review by a committer approval: otc review pending labels Jul 12, 2022
@DDvO

DDvO commented Jul 14, 2022

Copy link
Copy Markdown
Contributor Author

@vdukhovni can you please review this?
The new trace feature for chain building cannot move on without this.

@kaduk kaduk 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.

A couple thoughts noticed while skimming to assess if I can do a "real" review in the time allotted (answer: no).

Comment thread crypto/x509/x509_vfy.c Resolved
Comment thread crypto/x509/x509_vfy.c Resolved
@DDvO

DDvO commented Jul 20, 2022

Copy link
Copy Markdown
Contributor Author

Ping for reviews.

I wonder if anyone (except @kaduk, who cannot take the time) already plans or has started doing a review?

@DDvO

DDvO commented Jul 20, 2022 •

Copy link
Copy Markdown
Contributor Author

Would it help if I split this into two PRs:

  • one with all the just shallow coding style and doc/comment improvements
  • one with the actual 'meat', re-structuring the various 'find issuer' functions

?

@t8m

t8m commented Jul 20, 2022

Copy link
Copy Markdown
Member

Would it help if I split this into two PRs:

* one with all the just shallow coding style and doc/comment improvements

* one with the actual 'meat', re-structuring the various  'find issuer' functions

Not necessarily separate PRs but separate commits would probably help.

@DDvO DDvO changed the title x590_lu.c and x509_vfy.c: refactor find_issuer() and friends x509_lu.c and x509_vfy.c: refactor find_issuer() and friends Jul 20, 2022
@DDvO
DDvO force-pushed the x590_vfy-lu_refactor_find_issuer branch 2 times, most recently from 103d5c9 to 2041299 Compare July 20, 2022 13:50
@DDvO

DDvO commented Jul 20, 2022

Copy link
Copy Markdown
Contributor Author

Would it help if I split this into two PRs:

* one with all the just shallow coding style and doc/comment improvements

* one with the actual 'meat', re-structuring the various  'find issuer' functions

Not necessarily separate PRs but separate commits would probably help.

Done.
And rebased the two commits, just in case.

Comment thread crypto/x509/x509_lu.c Resolved
Comment thread crypto/x509/x509_lu.c Resolved
Comment thread crypto/x509/x509_vfy.c Resolved
@DDvO
DDvO requested a review from t8m July 21, 2022 06:25
@DDvO
DDvO force-pushed the x590_vfy-lu_refactor_find_issuer branch 2 times, most recently from ea955cf to 4e76701 Compare July 21, 2022 06:34
@DDvO

DDvO commented Jul 21, 2022 •

Copy link
Copy Markdown
Contributor Author

I've just re-ordered the new fixup commit among the existing ones.
to avoid future merge conflicts on autosquash.

@t8m

t8m commented Jul 21, 2022

Copy link
Copy Markdown
Member

The codingstyle changes LGTM now. However the refactoring will need more time to review which I do not have right now.

@DDvO

DDvO commented Jul 21, 2022

Copy link
Copy Markdown
Contributor Author

The codingstyle changes LGTM now. However the refactoring will need more time to review which I do not have right now.

Thank you for letting me know.

Then I suppose it is best now if I copy the codingstyle commit(s) to a different PR, which you could approve right away, and after this is merged (likely soon), I'll rebase this PR and this PR will become simpler to review.
Ok?

@t8m

t8m commented Jul 21, 2022

Copy link
Copy Markdown
Member

OK

@DDvO
DDvO force-pushed the x590_vfy-lu_refactor_find_issuer branch from d24ef3b to 1ebcc6c Compare November 9, 2024 21:18
@DDvO DDvO changed the title x509_lu.c and x509_vfy.c: refactor find_issuer() and friends Prerequisite for tracing chain building: refactor find_issuer() etc. in x509_lu.c and x509_vfy.c Nov 9, 2024
@DDvO

DDvO commented Nov 9, 2024

Copy link
Copy Markdown
Contributor Author

Rebased again to fix merge conflict.

Ping @openssl/committers for 2nd review

t8m
t8m previously approved these changes Nov 11, 2024
hlandau
hlandau previously approved these changes Nov 12, 2024
@hlandau hlandau 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 Nov 12, 2024
@openssl-machine openssl-machine 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 Nov 13, 2024
@openssl-machine

Copy link
Copy Markdown
Collaborator

This pull request is ready to merge

Comment thread crypto/x509/x509_lu.c
i = ossl_x509_store_ctx_get_by_subject(ctx, X509_LU_X509, nm, xobj);
if (i <= 0) {
X509_OBJECT_free(xobj);
i = ossl_x509_store_ctx_get_by_subject(ctx, X509_LU_X509, nm, NULL);

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Some day, it might be good to not assume that cached data is always complete and correct. Trust anchors can become untrusted, and new ones can come along. Certainly with CApath, it makes some sense to periodically ignore the cached data, and do a fresh lookup, perhaps a new control can set the refresh interval. Nothing needs to be done in this PR, but something to keep in mind...

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Good point.
I suggest opening an issue to help keeping this in mind.

Comment thread crypto/x509/x509_lu.c Resolved
Comment thread crypto/x509/x509_vfy.c Resolved
@DDvO
DDvO dismissed stale reviews from hlandau and t8m via 205564f November 15, 2024 16:52
@DDvO DDvO added approval: review pending This pull request needs review by a committer and removed approval: ready to merge The 24 hour grace period has passed, ready to merge labels Nov 15, 2024

@t8m t8m 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.

Approved if CI passes

@t8m
t8m requested a review from hlandau November 15, 2024 17:16
@DDvO
DDvO requested a review from vdukhovni November 18, 2024 07:23
@hlandau hlandau 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 Nov 19, 2024
@openssl-machine openssl-machine removed the approval: done This pull request has the required number of approvals label Nov 20, 2024
@openssl-machine

Copy link
Copy Markdown
Collaborator

This pull request is ready to merge

@openssl-machine openssl-machine added the approval: ready to merge The 24 hour grace period has passed, ready to merge label Nov 20, 2024
@t8m

t8m commented Nov 20, 2024

Copy link
Copy Markdown
Member

Merged to the master branch. Thank you for your contribution.

@t8m t8m closed this Nov 20, 2024
openssl-machine pushed a commit that referenced this pull request Nov 20, 2024
…_issuer(), etc.

Reviewed-by: Hugo Landau <[email protected]>
Reviewed-by: Tomas Mraz <[email protected]>
(Merged from #18762)
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 tests: exempted The PR is exempt from requirements for testing triaged: refactor The issue/pr requests/implements refactoring

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants