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

Fix loading more than one certificate in PEM format in the X509_load_cert_file_ex() function #22885

Closed
wants to merge 3 commits into from

Conversation

olszomal
Copy link
Contributor

@olszomal olszomal commented Nov 30, 2023

X509_load_cert_file_ex() was broken by commit ae29622 in PR #21545.

The following code shows this error.

#include <openssl/x509_vfy.h>

int main()
{
    const char *cafile = "./ca-certificates.crt";
    X509_STORE *store;
    X509_LOOKUP *lookup;
    STACK_OF(X509) *certs;
    int i;

    store = X509_STORE_new();
    lookup = X509_STORE_add_lookup(store, X509_LOOKUP_file());
    X509_load_cert_file(lookup, cafile, X509_FILETYPE_PEM);
    certs = X509_STORE_get1_all_certs(store);
    for(i=0; i<sk_X509_num(certs); i++) {
        char *name=X509_NAME_oneline(X509_get_subject_name(sk_X509_value(certs, i)), NULL, 0);
        printf("#%d: %s\n", i, name);
        OPENSSL_free(name);
    }
    sk_X509_pop_free(certs, X509_free);
    X509_STORE_free(store);
}

The ca-certificates.crt file contains 5 sample certificates in PEM format.
This PoC shows that there is only one certificate in the store, the last one in the file.

#0: /serialNumber=G63287510/C=ES/O=ANF Autoridad de Certificacion/OU=ANF CA Raiz/CN=ANF Secure Server Root CA

My PR fixes this by freeing an X509 object before any further use.
Additionally, it allocates and initializes an X509 structure with a library context and property query for all certificates.

#0: /C=ES/O=FNMT-RCM/OU=AC RAIZ FNMT-RCM
#1: /CN=ACCVRAIZ1/OU=PKIACCV/O=ACCV/C=ES
#2: /O=mkcert development CA/OU=mo@localhost-live.home/CN=mkcert mo@localhost-live.home
#3: /C=ES/O=FNMT-RCM/OU=Ceres/organizationIdentifier=VATES-Q2826004J/CN=AC RAIZ FNMT-RCM SERVIDORES SEGUROS
#4: /serialNumber=G63287510/C=ES/O=ANF Autoridad de Certificacion/OU=ANF CA Raiz/CN=ANF Secure Server Root CA

Fix #22895

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.

Would you be able to add a testcase? There are already files with multiple certs in them in test/certs - for example the leaf-chain.pem file.

@t8m t8m added branch: master Merge to master branch triaged: bug The issue/pr is/fixes a bug severity: regression The issue/pr is a regression from previous released version branch: 3.2 Merge to openssl-3.2 approval: review pending This pull request needs review by a committer approval: otc review pending This pull request needs review by an OTC member labels Nov 30, 2023
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.

Good, just nits

test/x509_load_cert_file_test.c Outdated Show resolved Hide resolved
test/recipes/60-test_x509_load_cert_file.t Outdated Show resolved Hide resolved
test/x509_load_cert_file_test.c Outdated Show resolved Hide resolved
X509_STORE *store = NULL;
X509_LOOKUP *lookup = NULL;
STACK_OF(X509) *certs = NULL;
const char *chain = test_get_argument(n);
Copy link
Member

Choose a reason for hiding this comment

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

This is a misuse of the n parameter. The argument to this function is the index of the test. The test will be repeated a given number of times and n tells you which iteration this is.

Copy link
Member

Choose a reason for hiding this comment

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

Actually this is only a partial misuse - there could be a loop going through all arguments here. On the other hand this requires all the test PEM files having 4 certificates because this is hardcoded below so I am not quite sure it is that useful to loop.

if (!TEST_int_gt(n, 0))
return 0;

ADD_ALL_TESTS(test_load_cert_file, n);
Copy link
Member

Choose a reason for hiding this comment

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

You should just use ADD_TEST here. The last argument to ADD_ALL_TESTS is the number of times that the test should be repeated. But we only want to run the test once.


#include "testutil.h"

const char *chain;
Copy link
Member

Choose a reason for hiding this comment

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

This must be static const char *chain;

return 0;
}

chain = test_get_argument(0);
Copy link
Member

Choose a reason for hiding this comment

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

You should check that chain != NULL.

@t8m t8m added approval: done This pull request has the required number of approvals tests: present The PR has suitable tests present and removed approval: review pending This pull request needs review by a committer labels Dec 1, 2023
@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 Dec 2, 2023
@openssl-machine
Copy link
Collaborator

This pull request is ready to merge

openssl-machine pushed a commit that referenced this pull request Dec 4, 2023
…_file_ex()

Fixes #22895

Reviewed-by: Matt Caswell <matt@openssl.org>
Reviewed-by: Tomas Mraz <tomas@openssl.org>
(Merged from #22885)
openssl-machine pushed a commit that referenced this pull request Dec 4, 2023
Reviewed-by: Matt Caswell <matt@openssl.org>
Reviewed-by: Tomas Mraz <tomas@openssl.org>
(Merged from #22885)
@t8m
Copy link
Member

t8m commented Dec 4, 2023

Merged to the master and 3.2 branches. Thank you for your contribution.

@t8m t8m closed this Dec 4, 2023
openssl-machine pushed a commit that referenced this pull request Dec 4, 2023
…_file_ex()

Fixes #22895

Reviewed-by: Matt Caswell <matt@openssl.org>
Reviewed-by: Tomas Mraz <tomas@openssl.org>
(Merged from #22885)

(cherry picked from commit 20c680d)
openssl-machine pushed a commit that referenced this pull request Dec 4, 2023
Reviewed-by: Matt Caswell <matt@openssl.org>
Reviewed-by: Tomas Mraz <tomas@openssl.org>
(Merged from #22885)

(cherry picked from commit d6961af)
wanghao75 pushed a commit to openeuler-mirror/openssl that referenced this pull request Dec 7, 2023
…_file_ex()

Fixes #22895

Reviewed-by: Matt Caswell <matt@openssl.org>
Reviewed-by: Tomas Mraz <tomas@openssl.org>
(Merged from openssl/openssl#22885)

Signed-off-by: fly2x <fly2x@hitls.org>
wanghao75 pushed a commit to openeuler-mirror/openssl that referenced this pull request Dec 7, 2023
Reviewed-by: Matt Caswell <matt@openssl.org>
Reviewed-by: Tomas Mraz <tomas@openssl.org>
(Merged from openssl/openssl#22885)

Signed-off-by: fly2x <fly2x@hitls.org>
wanghao75 pushed a commit to openeuler-mirror/openssl that referenced this pull request Dec 15, 2023
…_file_ex()

Fixes #22895

Reviewed-by: Matt Caswell <matt@openssl.org>
Reviewed-by: Tomas Mraz <tomas@openssl.org>
(Merged from openssl/openssl#22885)

(cherry picked from commit 20c680de9c435534be48fa85b2a975067a4e7c9d)
Signed-off-by: fly2x <fly2x@hitls.org>
wanghao75 pushed a commit to openeuler-mirror/openssl that referenced this pull request Dec 15, 2023
Reviewed-by: Matt Caswell <matt@openssl.org>
Reviewed-by: Tomas Mraz <tomas@openssl.org>
(Merged from openssl/openssl#22885)

(cherry picked from commit d6961af1acbdf29b684f3307578bd03890a26a9c)
Signed-off-by: fly2x <fly2x@hitls.org>
wbeck10 pushed a commit to wbeck10/openssl that referenced this pull request Jan 8, 2024
…_file_ex()

Fixes openssl#22895

Reviewed-by: Matt Caswell <matt@openssl.org>
Reviewed-by: Tomas Mraz <tomas@openssl.org>
(Merged from openssl#22885)
wbeck10 pushed a commit to wbeck10/openssl that referenced this pull request Jan 8, 2024
Reviewed-by: Matt Caswell <matt@openssl.org>
Reviewed-by: Tomas Mraz <tomas@openssl.org>
(Merged from openssl#22885)
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 Merge to master branch branch: 3.2 Merge to openssl-3.2 severity: regression The issue/pr is a regression from previous released version tests: present The PR has suitable tests present triaged: bug The issue/pr is/fixes a bug
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Regression from refactor of X509_load_cert_file_ex in 3.2.0
4 participants