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

Clean up & improve PK write test functions #7449

Merged
merged 12 commits into from
Apr 24, 2023

Conversation

valeriosetti
Copy link
Contributor

@valeriosetti valeriosetti commented Apr 17, 2023

Reduce memory footprint for pkwrite's tests.

Resolves #7446

Gatekeeper checklist

@valeriosetti valeriosetti added enhancement needs-review Every commit must be reviewed by at least two team members, needs-ci Needs to pass CI tests needs-reviewer This PR needs someone to pick it up for review size-s Estimated task size: small (~2d) priority-medium Medium priority - this can be reviewed as time permits labels Apr 17, 2023
@valeriosetti valeriosetti requested a review from mpg April 17, 2023 15:37
@valeriosetti valeriosetti self-assigned this Apr 17, 2023
Copy link
Contributor

@mpg mpg left a comment

Choose a reason for hiding this comment

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

Going in the right direction, still found a few issues, mostly minor. Apparently the CI has complaints too, which I didn't check.

mpg
mpg previously approved these changes Apr 18, 2023
Copy link
Contributor

@mpg mpg left a comment

Choose a reason for hiding this comment

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

Thanks for addressing my feedback! LGTM!

@mpg
Copy link
Contributor

mpg commented Apr 18, 2023

Actually, I think we want to backport this to 2.28 - we're trying to keep testing aligned when possible, in order to facilitate backports for future bug fixes, which generally come with their regression test.

@mpg mpg added the needs-backports Backports are missing or are pending review and approval. label Apr 18, 2023
@valeriosetti valeriosetti removed the needs-ci Needs to pass CI tests label Apr 19, 2023
@AndrzejKurek AndrzejKurek self-requested a review April 20, 2023 11:49
AndrzejKurek
AndrzejKurek previously approved these changes Apr 20, 2023
Copy link
Contributor

@AndrzejKurek AndrzejKurek left a comment

Choose a reason for hiding this comment

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

Looks good, although I think a bit about the testing times:
We're testing the same things over again in DER and PEM... How about testing with DER only if there's no MBEDTLS_PEM_PARSE_C and MBEDTLS_PEM_WRITE_C?

@@ -999,6 +999,57 @@ ec_bp512_pub.comp.pem: ec_bp512_pub.pem
$(OPENSSL) ec -pubin -in $< -out $@ -conv_form compressed
all_final += ec_bp512_pub.comp.pem

################################################################
#### Convert PEM keys in DER format
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
#### Convert PEM keys in DER format
#### Convert PEM keys to DER format

@gilles-peskine-arm
Copy link
Contributor

How about testing with DER only if there's no MBEDTLS_PEM_PARSE_C and MBEDTLS_PEM_WRITE_C?

Let's not. There could be some mix-up in the parsing code that breaks DER parsing when PEM is enabled. The cost is negligible.

@AndrzejKurek AndrzejKurek removed the needs-backports Backports are missing or are pending review and approval. label Apr 20, 2023
@mpg
Copy link
Contributor

mpg commented Apr 21, 2023

Tests are failing on Windows. Interestingly enough, what's failing is the PEM tests, which were already present, not the DER tests which have just been added. I didn't investigate, but this smells CR+LF vs LF issues - we changed the way files are read I think, so this probably had an impact somehow?

@gilles-peskine-arm gilles-peskine-arm added needs-work needs-ci Needs to pass CI tests and removed approved Design and code approved - may be waiting for CI or backports labels Apr 21, 2023

TEST_ASSERT(ilen == pem_len);
TEST_ASSERT(memcmp((char *) buf, (char *) check_buf, ilen) == 0);
ASSERT_COMPARE(start_buf, buf_len, check_buf, check_buf_len);
Copy link
Contributor

Choose a reason for hiding this comment

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

As Manuel suspects, this fails with PEM files on Windows because of its handling of text files. mbedtls_pk_write_key_pem writes to the buffer, without going through the I/O layer, so newlines in start_buf are a \n character. mbedtls_pk_load_file loads files in binary mode (it has no choice since it must support DER), so newlines in check_buf are \n on non-Windows but \r\n on Windows (because Git treats PEM files as text, so it converts \n to \r\n upon checkout).

This wasn't a problem with the old code because it called fopen in text mode to read check_buf, so the I/O layer converted \r\n in the file to \n in memory. But of course that would have corrupted DER files.

I guess the solution is to remove CR characters from check_buf if the format is PEM. Or keep calling fopen directly, but choose text vs binary mode depending on whether the data is DER or PEM.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks a lot for your help! It really saved me a lot of time since I was not aware of this difference and I didn't have a Windows machine at hand.

Copy link
Contributor

Choose a reason for hiding this comment

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

We got hit by this on another PR not so long ago. While I understand why git it doing it, I still find it always surprising that what we check out (on Windows) is not byte-for-byte identical to what we commited (on Linux), at least with the default settings. Thanks Gilles for the reminder!

@valeriosetti valeriosetti dismissed stale reviews from AndrzejKurek and mpg via a12801c April 24, 2023 06:52
Copy link
Contributor

@mpg mpg left a comment

Choose a reason for hiding this comment

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

LGTM except for one truncated comment and one choice of libc function.

@valeriosetti valeriosetti requested a review from mpg April 24, 2023 08:41
Copy link
Contributor

@mpg mpg left a comment

Choose a reason for hiding this comment

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

LGTM

@mpg mpg added approved Design and code approved - may be waiting for CI or backports and removed needs-work labels Apr 24, 2023
@mpg mpg merged commit 0281d76 into Mbed-TLS:development Apr 24, 2023
@valeriosetti valeriosetti mentioned this pull request Apr 24, 2023
3 tasks
@valeriosetti valeriosetti deleted the issue7446 branch December 6, 2024 06:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
approved Design and code approved - may be waiting for CI or backports enhancement needs-ci Needs to pass CI tests priority-medium Medium priority - this can be reviewed as time permits size-s Estimated task size: small (~2d)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Clean up & improve PK write test functions
4 participants