Skip to content

X509: add tracing for chain building - #18761

Closed
DDvO wants to merge 2 commits into
openssl:masterfrom
siemens:x509_trace_chain_building
Closed

DDvO wants to merge 2 commits into
openssl:masterfrom
siemens:x509_trace_chain_building

Conversation

@DDvO

@DDvO DDvO commented Jul 9, 2022 •

Copy link
Copy Markdown
Contributor

This provides a proper solution for the problem mentioned first in #14094:

When something goes wrong during the chain building phase of certificate verification,
the error code usually is just unable to get issuer certificate or unable to get local issuer certificate.
Often this does not tell much about the real problem leading to the situation that no suitable cert can be found.

Here is a simple example. Even when using -verbose,

apps/openssl verify -verbose -auth_level 1 -purpose sslserver -trusted test/certs/root-cert.pem   -untrusted test/certs/ca-pss-cert.pem test/certs/ee-pss-wrong1.5-cert.pem

just yields

CN=EE-PSS-wrong1.5
error 20 at 0 depth lookup: unable to get local issuer certificate
error test/certs/ee-pss-wrong1.5-cert.pem: verification failed

because check_issued() throws away all intermediate error codes during chain building.

This PR adds X509_STORE_CTX_set0_trace_output() and extends the semantics of the openssl verify app option -verbose such that the above call additionally provides the following output:

X509 chain building start
  handling target certificate at depth 0:
        Subject: CN=EE-PSS-wrong1.5
         Issuer: CN=CA-PSS
        Serial Number: 2 (0x2)
        Validity
            Not Before: Jan 27 06:52:32 2021 GMT
            Not After : Jan 28 06:52:32 2121 GMT
        X509v3 Subject Key Identifier: 
            E7:9B:E2:2A:AD:8A:6C:3A:CB:76:51:E5:8E:07:98:22:97:E1:73:A2
        X509v3 Authority Key Identifier: 
            19:F9:A1:03:F5:5C:C5:69:21:ED:55:01:1F:15:1D:1C:74:0B:82:B0
    looking in list of trusted certificates with 1 entries
    no more candidate trusted certs
    looking in list of untrusted certificates with 1 entries
      checking untrusted candidate issuer certificate:
        Subject: CN=CA-PSS
         Issuer: CN=Root CA
        Serial Number: 2 (0x2)
        Validity
            Not Before: Jan 26 10:05:08 2021 GMT
            Not After : Jan 27 10:05:08 2121 GMT
        X509v3 Subject Key Identifier: 
            19:F9:A1:03:F5:5C:C5:69:21:ED:55:01:1F:15:1D:1C:74:0B:82:B0
        X509v3 Authority Key Identifier: 
            8E:F5:25:AF:1E:9B:6B:84:3D:7C:80:9A:C9:53:9C:4D:72:F7:A3:52
      candidate issuer certificate rejected due to error 77: subject signature algorithm and issuer public key algorithm mismatch
    no more candidate untrusted certs, switching again to trusted certs
    looking in list of trusted certificates with 1 entries
    no more candidate trusted certs
    no more candidate certs
X509 chain building failure

which shows the main steps of cert chain building and points out the reason why no suitable cert could be found:
subject signature algorithm and issuer public key algorithm mismatch.

In addition, this PR adds a new trace category: X509_VERIFY, such that the above output can also be generated when the trace API is statically enabled (which is not the default, see also #18734) and X509_VERIFY is dynamically enabled, for instance because the env. variable OPENSSL_TRACE contains it.

@DDvO

DDvO commented Jul 10, 2022 •

Copy link
Copy Markdown
Contributor Author

@vdukhovni, here is an even more interesting trace for

apps/openssl verify -verbose -attime 0 -trusted <(cat test/certs/root-cert.pem) \
  -untrusted <(cat test/certs/ca-cert.pem test/certs/ca-root2.pem) test/certs/ee-cert.pem

where the chain build failure is just because of -attime 0:

X509 chain building start
  handling target certificate at depth 0:
        Subject: CN=server.example
         Issuer: CN=CA
        Serial Number: 2 (0x2)
        Validity
            Not Before: Jan 15 08:19:49 2016 GMT
            Not After : Jan 16 08:19:49 2116 GMT
        X509v3 Subject Key Identifier: 
            E7:9B:E2:2A:AD:8A:6C:3A:CB:76:51:E5:8E:07:98:22:97:E1:73:A2
        X509v3 Authority Key Identifier: 
            B4:11:33:F1:D7:E2:5E:F7:53:9E:20:22:10:4F:86:06:BF:1F:C9:5E
    looking in list of trusted certificates with 1 entries
    no more candidate trusted certs
    looking in list of untrusted certificates with 2 entries
      checking untrusted candidate issuer certificate:
        Subject: CN=CA
         Issuer: CN=Root CA
        Serial Number: 2 (0x2)
        Validity
            Not Before: Dec 12 20:16:50 2020 GMT
            Not After : Dec 13 20:16:50 2120 GMT
        X509v3 Subject Key Identifier: 
            B4:11:33:F1:D7:E2:5E:F7:53:9E:20:22:10:4F:86:06:BF:1F:C9:5E
        X509v3 Authority Key Identifier: 
            8E:F5:25:AF:1E:9B:6B:84:3D:7C:80:9A:C9:53:9C:4D:72:F7:A3:52
      candidate issuer certificate likely rejected due to error 9: certificate is not yet valid
      checking untrusted candidate issuer certificate:
        Subject: CN=CA
         Issuer: CN=Root CA
        Serial Number: 2 (0x2)
        Validity
            Not Before: Dec 12 20:17:04 2020 GMT
            Not After : Dec 13 20:17:04 2120 GMT
        X509v3 Subject Key Identifier: 
            B4:11:33:F1:D7:E2:5E:F7:53:9E:20:22:10:4F:86:06:BF:1F:C9:5E
        X509v3 Authority Key Identifier: 
            9C:CE:66:42:30:AB:1C:08:26:5F:73:19:6D:DF:0F:A7:AE:58:52:1E
      candidate issuer certificate likely rejected due to error 9: certificate is not yet valid
      provisionally accepted the most recently expired candidate issuer certificate:
        Subject: CN=CA
         Issuer: CN=Root CA
        Serial Number: 2 (0x2)
        Validity
            Not Before: Dec 12 20:17:04 2020 GMT
            Not After : Dec 13 20:17:04 2120 GMT
        X509v3 Subject Key Identifier: 
            B4:11:33:F1:D7:E2:5E:F7:53:9E:20:22:10:4F:86:06:BF:1F:C9:5E
        X509v3 Authority Key Identifier: 
            9C:CE:66:42:30:AB:1C:08:26:5F:73:19:6D:DF:0F:A7:AE:58:52:1E
  handling chain certificate at depth 1:
        Subject: CN=CA
         Issuer: CN=Root CA
        Serial Number: 2 (0x2)
        Validity
            Not Before: Dec 12 20:17:04 2020 GMT
            Not After : Dec 13 20:17:04 2120 GMT
        X509v3 Subject Key Identifier: 
            B4:11:33:F1:D7:E2:5E:F7:53:9E:20:22:10:4F:86:06:BF:1F:C9:5E
        X509v3 Authority Key Identifier: 
            9C:CE:66:42:30:AB:1C:08:26:5F:73:19:6D:DF:0F:A7:AE:58:52:1E
    looking in list of trusted certificates with 1 entries
      checking trusted candidate issuer certificate:
        Subject: CN=Root CA
        self-issued
        Serial Number: 1 (0x1)
        Validity
            Not Before: Dec 12 20:09:49 2020 GMT
            Not After : Dec 13 20:09:49 2120 GMT
        X509v3 Subject Key Identifier: 
            8E:F5:25:AF:1E:9B:6B:84:3D:7C:80:9A:C9:53:9C:4D:72:F7:A3:52
        X509v3 Authority Key Identifier: 
            8E:F5:25:AF:1E:9B:6B:84:3D:7C:80:9A:C9:53:9C:4D:72:F7:A3:52
      candidate issuer certificate rejected due to error 30: authority and subject key identifier mismatch
    no more candidate trusted certs
    looking in list of untrusted certificates with 1 entries
    no more candidate untrusted certs, switching again to trusted certs
    looking in list of trusted certificates with 1 entries
      checking trusted candidate issuer certificate:
        Subject: CN=Root CA
        self-issued
        Serial Number: 1 (0x1)
        Validity
            Not Before: Dec 12 20:09:49 2020 GMT
            Not After : Dec 13 20:09:49 2120 GMT
        X509v3 Subject Key Identifier: 
            8E:F5:25:AF:1E:9B:6B:84:3D:7C:80:9A:C9:53:9C:4D:72:F7:A3:52
        X509v3 Authority Key Identifier: 
            8E:F5:25:AF:1E:9B:6B:84:3D:7C:80:9A:C9:53:9C:4D:72:F7:A3:52
      candidate issuer certificate rejected due to error 30: authority and subject key identifier mismatch
    no more candidate trusted certs
    no more candidate certs
X509 chain building failure
CN=CA
error 20 at 1 depth lookup: unable to get local issuer certificate
error test/certs/ee-cert.pem: verification failed

This and many other traces reveal that on chain build failure the trust store is searched twice: before and after untrusted certs.
Is this on purpose, and if so, why?

Moreover, also in successful chain building the trust store is searched at each depth (with different subject name).
Wouldn't it be more efficient to collect the list of all trusted certs once and then use it for all depths?

@DDvO

DDvO commented Jul 11, 2022 •

Copy link
Copy Markdown
Contributor Author

I meanwhile realized that for successful chain building the trust store is not consulted more than once per depth (and thus, I updated my above statement accordingly),
simply because then a suitable candidate cert is found in the trust store or in the untrusted list, so for this reason the trust store is not actually considered again at the same depth.
Still for unsuccessful chain builds at the final (failing) depth the trust store is considered twice, which pretty surely is needless and appears to be due to a mistake.

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

So if someone enables tracing through the OPENSSL_TRACE environment variable (for example with the value ALL), and uses -verbose (i.e. cause X509_STORE_CTX_set0_trace_output() to be called), won't that mean that the output comes in duplicates?

Comment thread crypto/x509/x509_vfy.c
static void trace_msg(const X509_STORE_CTX *ctx, const char *msg)
{
BIO_printf(ctx->out, "%s\n", msg);
OSSL_TRACE1(X509_VERIFY, "%s\n", msg);

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.

!

Comment thread crypto/x509/x509_vfy.c
const char *elems)
{
BIO_printf(ctx->out, "%s %d %s\n", msg, num, elems);
OSSL_TRACE3(X509_VERIFY, "%s %d %s\n", msg, num, elems);

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.

!

Comment thread crypto/x509/x509_vfy.c
const char *str = X509_verify_cert_error_string(err);

BIO_printf(ctx->out, fmt, msg, err, str);
OSSL_TRACE3(X509_VERIFY, fmt, msg, err, str);

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.

!

@levitte

levitte commented Jul 11, 2022 •

Copy link
Copy Markdown
Member

Might I suggest making this PR only about adding the tracing stuff?
The -verbose (X509_STORE_CTX_set0_trace_output()) idea is certainly an idea worth considering, but should be considered separately, and needs more care.

@DDvO

DDvO commented Jul 11, 2022

Copy link
Copy Markdown
Contributor Author

So if someone enables tracing through the OPENSSL_TRACE environment variable (for example with the value ALL), and uses -verbose (i.e. cause X509_STORE_CTX_set0_trace_output() to be called), won't that mean that the output comes in duplicates?

Yes, and this might even be intended: suppose the trace API output goes to a log file and the one set via X509_STORE_CTX_set0_trace_output() goes to the console, or the other way round.
At least I'd say it does not hurt to even have both simultaneously, and if the user already gets chain building trace output via the trace API (which is anyway rare, as the facility is statically disabled by default), (s)he will likely not use -verbose in addition.

@DDvO

DDvO commented Jul 11, 2022

Copy link
Copy Markdown
Contributor Author

Might I suggest making this PR only about adding the tracing stuff? The -verbose (X509_STORE_CTX_set0_trace_output()) idea is certainly an idea worth considering, but should be considered separately, and needs more care.

I can certainly provide the additional/alternative output via X509_STORE_CTX_set0_trace_output() in a follow-up PR,
though this form will be much more important in practice because the trace API output is rather inaccessible, see also #18734.
What do others think?

@DDvO

DDvO commented Jul 11, 2022

Copy link
Copy Markdown
Contributor Author

BTW, this PR is still in draft state solely because it builds upon

@t8m

t8m commented Jul 11, 2022

Copy link
Copy Markdown
Member

I agree with Richard that this should be split in two PRs. One adding the tracing and one that builds upon the first one to add the diagnostic output (that one should be discussed with OTC IMO).

@t8m t8m added the triaged: feature The issue/pr requests/adds a feature label Jul 11, 2022
@DDvO

DDvO commented Jul 11, 2022 •

Copy link
Copy Markdown
Contributor Author

I agree with Richard that this should be split in two PRs. One adding the tracing and one that builds upon the first one to add the diagnostic output (that one should be discussed with OTC IMO).

All right, I will do the part adding X509_STORE_CTX_set0_trace_output() and its connection with openssl verify -verbose in a follow-up PR.
Yet I plan to defer this until the two prerequisite PRs #18758 and #18762 have been merged, such that I can rebase this PR on master including these changes and this PR does not need to be a draft anymore.

@DDvO
DDvO force-pushed the x509_trace_chain_building branch 2 times, most recently from 377ee41 to 5ff328d Compare July 27, 2022 09:15
@DDvO
DDvO force-pushed the x509_trace_chain_building branch from 5ff328d to bb01955 Compare July 27, 2022 09:15
@DDvO DDvO added the help wanted Issue to be resolved or being resolved by community. label Feb 6, 2026
@DDvO

DDvO commented Feb 6, 2026

Copy link
Copy Markdown
Contributor Author

Unfortunately, I likely won't have the time to continue working on this.
Can someone take over, or shall I close this draft PR (which I'd do end of next week), or how else to handle this?

@DDvO

DDvO commented Feb 16, 2026

Copy link
Copy Markdown
Contributor Author

Closing this PR for the time being as nobody seems to be able to spend energy on this in the near future.

@DDvO DDvO closed this Feb 16, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

help wanted Issue to be resolved or being resolved by community. severity: fips change The pull request changes FIPS provider sources triaged: feature The issue/pr requests/adds a feature

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants