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

Add NULL check before accessing PKCS7 encrypted algorithm #23632

Closed
wants to merge 1 commit into from

Conversation

lejcik
Copy link
Contributor

@lejcik lejcik commented Feb 19, 2024

Printing content of an invalid test certificate causes application crash, because of NULL dereference:

user@user:~/openssl$ openssl pkcs12 -in test/recipes/80-test_pkcs12_data/bad2.p12 -passin pass: -info
MAC: sha256, Iteration 2048
MAC length: 32, salt length: 8
PKCS7 Encrypted data: Segmentation fault (core dumped)

@@ -901,7 +901,11 @@ int dump_certs_keys_p12(BIO *out, const PKCS12 *p12, const char *pass,
} else if (bagnid == NID_pkcs7_encrypted) {
if (options & INFO) {
BIO_printf(bio_err, "PKCS7 Encrypted data: ");
alg_print(p7->d.encrypted->enc_data->algorithm);
if (p7->d.encrypted == NULL) {
BIO_printf(bio_err, "<no data>\n");
Copy link
Contributor Author

Choose a reason for hiding this comment

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

not sure how to fix this, I've added a check for NULL dereference and the error is then handled in PKCS12_unpack_p7encdata(), output then looks like:

user@user:~/openssl$ openssl pkcs12 -in test/recipes/80-test_pkcs12_data/bad2.p12 -passin pass: -info
MAC: sha256, Iteration 2048
MAC length: 32, salt length: 8
PKCS7 Encrypted data: <no data>
Error outputting keys and certificates
80CB79DB737F0000:error:11800065:PKCS12 routines:PKCS12_unpack_p7encdata:decode error:crypto/pkcs12/p12_add.c:163:

Copy link
Member

Choose a reason for hiding this comment

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

Since PKCS12_unpack_p7encdata is going to be called anyway, how about call it first and then print the algorithm, like:

...
bags = PKCS12_unpack_p7encdata(p7, pass, passlen);
if (bags == NULL)
    goto err;
alg_print(p7->d.encrypted->enc_data->algorithm);
....

Copy link
Contributor Author

Choose a reason for hiding this comment

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

fixed as suggested, now the output looks like:

user@user:~/openssl$ openssl pkcs12 -in test/recipes/80-test_pkcs12_data/bad2.p12 -passin pass: -info
MAC: sha256, Iteration 2048
MAC length: 32, salt length: 8
Error outputting keys and certificates
800B080A977F0000:error:11800065:PKCS12 routines:PKCS12_unpack_p7encdata:decode error:crypto/pkcs12/p12_add.c:163:

does this look better now?

Copy link
Member

Choose a reason for hiding this comment

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

Well, actually I'd agree with @t8m that the previous version was already okay.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

ok, reverted the commit to the previous version

@t8m t8m added branch: master Merge to master branch approval: review pending This pull request needs review by a committer approval: otc review pending This pull request needs review by an OTC member triaged: bug The issue/pr is/fixes a bug branch: 3.0 Merge to openssl-3.0 branch branch: 3.1 Merge to openssl-3.1 tests: exempted The PR is exempt from requirements for testing branch: 3.2 Merge to openssl-3.2 labels Feb 20, 2024
@t8m
Copy link
Member

t8m commented Feb 22, 2024

I actually like the original patch more than the new one.

@bernd-edlinger
Copy link
Member

Could you please add a test case?

@bernd-edlinger
Copy link
Member

Could you please add a test case?

Ping...
Note: that is not difficult at all:

diff --git a/test/recipes/80-test_pkcs12.t b/test/recipes/80-test_pkcs12.t
index 817b0bf064..c524e57fef 100644
--- a/test/recipes/80-test_pkcs12.t
+++ b/test/recipes/80-test_pkcs12.t
@@ -54,7 +54,7 @@ if (eval { require Win32::API; 1; }) {
 }
 $ENV{OPENSSL_WIN32_UTF8}=1;
 
-plan tests => 28;
+plan tests => 29;
 
 # Test different PKCS#12 formats
 ok(run(test(["pkcs12_format_test"])), "test pkcs12 formats");
@@ -187,6 +187,10 @@ with({ exit_checker => sub { return shift == 1; } },
         ok(run(app(["openssl", "pkcs12", "-in", $bad2, "-password", "pass:"])),
            "test bad pkcs12 file 2");
 
+        ok(run(app(["openssl", "pkcs12", "-in", $bad2, "-password", "pass:",
+                    "-info"])),
+           "test bad pkcs12 file 2 (info)");
+
         ok(run(app(["openssl", "pkcs12", "-in", $bad3, "-password", "pass:"])),
            "test bad pkcs12 file 3");
      });

@openssl-machine openssl-machine added the hold: cla required The contributor needs to submit a license agreement label Mar 7, 2024
@lejcik
Copy link
Contributor Author

lejcik commented Mar 7, 2024

@bernd-edlinger thanks for hint how to add a test case,

I added test cases for all 3 bad certificates, as I tested them on my ubuntu (OpenSSL 3.0.3 3), all the bad certificates caused openssl app crash, so rather let have these cases covered:

# openssl pkcs12 -in bad1.p12 -passin pass: -info
MAC: sha256, Iteration 2048
MAC length: 32, salt length: 8
Segmentation fault
#
# openssl pkcs12 -in bad2.p12 -passin pass: -info
MAC: sha256, Iteration 2048
MAC length: 32, salt length: 8
PKCS7 Encrypted data: Segmentation fault
#
# openssl pkcs12 -in bad3.p12 -passin pass: -info
MAC: sha256, Iteration 2048
MAC length: 32, salt length: 8
Segmentation fault

does the test look ok now?

@bernd-edlinger
Copy link
Member

does the test look ok now?

Excellent. Thanks.

Two things would be still to do.

  1. It would be good to remove the reverted commit
    completely.

  2. More importantly the CLA-check is failing with the last commit,
    that is funny because the first two commits had a CLA on file.
    The difference is how the Author's e-mail address is spelled.

So could you please squash all the commits (git rebase -i HEAD^^^^)
remove the second and third commits from the list list
and change the last commit from pick to squash.
Then do a force-push (git push -f)

@lejcik
Copy link
Contributor Author

lejcik commented Mar 10, 2024

ok, I've updated the git history, the commits are now squashed, is this what you wanted?

I never did such "black magic" with git commit history, so at least I've learned something new 🙂

@bernd-edlinger bernd-edlinger removed the approval: review pending This pull request needs review by a committer label Mar 10, 2024
@t8m t8m added approval: done This pull request has the required number of approvals tests: present The PR has suitable tests present and removed tests: exempted The PR is exempt from requirements for testing approval: otc review pending This pull request needs review by an OTC member labels Mar 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 Mar 13, 2024
@openssl-machine
Copy link
Collaborator

This pull request is ready to merge

@t8m t8m closed this Mar 13, 2024
@t8m t8m reopened this Mar 13, 2024
@t8m t8m added the hold: cla required The contributor needs to submit a license agreement label Mar 13, 2024
@t8m
Copy link
Member

t8m commented Mar 13, 2024

@lejcik Could you please submit a CLA? https://www.openssl.org/policies/cla.html According to our CLA policy we cannot accept your contribution without that.

@lejcik
Copy link
Contributor Author

lejcik commented Mar 13, 2024

ok, how to do that? is it enough to add CLA: trivial line into the commit description, and do push -f ?

I think this is a trivial change category...

@t8m
Copy link
Member

t8m commented Mar 13, 2024

Unfortunately this is not eligible for CLA: trivial.

May I ask you to fill in and sign a regular CLA document and e-mail it to us as described on the above linked CLA page?

Printing content of an invalid test certificate causes application crash, because of NULL dereference:

user@user:~/openssl$ openssl pkcs12 -in test/recipes/80-test_pkcs12_data/bad2.p12 -passin pass: -info
MAC: sha256, Iteration 2048
MAC length: 32, salt length: 8
PKCS7 Encrypted data: Segmentation fault (core dumped)

Added test cases for pkcs12 bad certificates
@lejcik lejcik closed this Mar 22, 2024
@lejcik lejcik reopened this Mar 22, 2024
@openssl-machine openssl-machine removed the hold: cla required The contributor needs to submit a license agreement label Mar 22, 2024
@t8m t8m added the branch: 3.3 Merge to openssl-3.3 label Mar 25, 2024
openssl-machine pushed a commit that referenced this pull request Mar 25, 2024
Printing content of an invalid test certificate causes application crash, because of NULL dereference:

user@user:~/openssl$ openssl pkcs12 -in test/recipes/80-test_pkcs12_data/bad2.p12 -passin pass: -info
MAC: sha256, Iteration 2048
MAC length: 32, salt length: 8
PKCS7 Encrypted data: Segmentation fault (core dumped)

Added test cases for pkcs12 bad certificates

Reviewed-by: Bernd Edlinger <bernd.edlinger@hotmail.de>
Reviewed-by: Tomas Mraz <tomas@openssl.org>
(Merged from #23632)
openssl-machine pushed a commit that referenced this pull request Mar 25, 2024
Printing content of an invalid test certificate causes application crash, because of NULL dereference:

user@user:~/openssl$ openssl pkcs12 -in test/recipes/80-test_pkcs12_data/bad2.p12 -passin pass: -info
MAC: sha256, Iteration 2048
MAC length: 32, salt length: 8
PKCS7 Encrypted data: Segmentation fault (core dumped)

Added test cases for pkcs12 bad certificates

Reviewed-by: Bernd Edlinger <bernd.edlinger@hotmail.de>
Reviewed-by: Tomas Mraz <tomas@openssl.org>
(Merged from #23632)

(cherry picked from commit a4cbffc)
openssl-machine pushed a commit that referenced this pull request Mar 25, 2024
Printing content of an invalid test certificate causes application crash, because of NULL dereference:

user@user:~/openssl$ openssl pkcs12 -in test/recipes/80-test_pkcs12_data/bad2.p12 -passin pass: -info
MAC: sha256, Iteration 2048
MAC length: 32, salt length: 8
PKCS7 Encrypted data: Segmentation fault (core dumped)

Added test cases for pkcs12 bad certificates

Reviewed-by: Bernd Edlinger <bernd.edlinger@hotmail.de>
Reviewed-by: Tomas Mraz <tomas@openssl.org>
(Merged from #23632)

(cherry picked from commit a4cbffc)
@t8m
Copy link
Member

t8m commented Mar 25, 2024

Merged to all the active branches. Thank you for your contribution.

@t8m t8m closed this Mar 25, 2024
openssl-machine pushed a commit that referenced this pull request Mar 25, 2024
Printing content of an invalid test certificate causes application crash, because of NULL dereference:

user@user:~/openssl$ openssl pkcs12 -in test/recipes/80-test_pkcs12_data/bad2.p12 -passin pass: -info
MAC: sha256, Iteration 2048
MAC length: 32, salt length: 8
PKCS7 Encrypted data: Segmentation fault (core dumped)

Added test cases for pkcs12 bad certificates

Reviewed-by: Bernd Edlinger <bernd.edlinger@hotmail.de>
Reviewed-by: Tomas Mraz <tomas@openssl.org>
(Merged from #23632)

(cherry picked from commit a4cbffc)
openssl-machine pushed a commit that referenced this pull request Mar 25, 2024
Printing content of an invalid test certificate causes application crash, because of NULL dereference:

user@user:~/openssl$ openssl pkcs12 -in test/recipes/80-test_pkcs12_data/bad2.p12 -passin pass: -info
MAC: sha256, Iteration 2048
MAC length: 32, salt length: 8
PKCS7 Encrypted data: Segmentation fault (core dumped)

Added test cases for pkcs12 bad certificates

Reviewed-by: Bernd Edlinger <bernd.edlinger@hotmail.de>
Reviewed-by: Tomas Mraz <tomas@openssl.org>
(Merged from #23632)

(cherry picked from commit a4cbffc)
bernd-edlinger pushed a commit to bernd-edlinger/openssl that referenced this pull request Mar 27, 2024
Printing content of an invalid test certificate causes application crash, because of NULL dereference:

user@user:~/openssl$ openssl pkcs12 -in test/recipes/80-test_pkcs12_data/bad2.p12 -passin pass: -info
MAC: sha256, Iteration 2048
MAC length: 32, salt length: 8
PKCS7 Encrypted data: Segmentation fault (core dumped)

Added test cases for pkcs12 bad certificates

Reviewed-by: Bernd Edlinger <bernd.edlinger@hotmail.de>
Reviewed-by: Tomas Mraz <tomas@openssl.org>
(Merged from openssl#23632)

(cherry picked from commit a4cbffc)
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.0 Merge to openssl-3.0 branch branch: 3.1 Merge to openssl-3.1 branch: 3.2 Merge to openssl-3.2 branch: 3.3 Merge to openssl-3.3 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.

None yet

5 participants