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 support for PKCS12_set_mac #2128

Open
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

samuel40791765
Copy link
Contributor

Issues:

Addresses CryptoAlg-2821

Description of changes:

Ruby has added a binding for an additional function called PKCS12_set_mac in it's master branch: ruby/ruby@c79b435 This was discovered prior to the PR for Ruby's CI integration with the master branch being merged: #2071

OpenSSL's implementation of the function directly sets a designated mac field within the PKCS12 structure. Our PKCS12 structure is folded into a string of bytes along with its length and there aren't any available fields for us to directly set. This means that AWS-LC's implementation of PKCS12_set_mac has to parse the proper contents from PKCS12 and rerun the key and mac generation with the new parameters provided.

Call-outs:

  1. The parsing logic is similar to PKCS12_get_key_and_certs, but the logic can't be shared. This is because we need to maintain pointers to certain parts of the CBS parsers in PKCS12_set_mac, so that we can properly rewrite the contents later.
  2. I've abstracted the key and mac generation in PKCS12_create and PKCS12_set_mac to a single function called pkcs12_gen_and_write_mac.
  3. OpenSSL 3.0 uses SHA-256 as the default hash, yet OpenSSL 1.1.1 uses SHA1. TODO: Make a decision

Testing:

New Tests

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license and the ISC license.

@codecov-commenter
Copy link

codecov-commenter commented Jan 18, 2025

Codecov Report

Attention: Patch coverage is 85.08772% with 17 lines in your changes missing coverage. Please review.

Project coverage is 78.97%. Comparing base (cc9c9f0) to head (3deaf76).

Files with missing lines Patch % Lines
crypto/pkcs8/pkcs8_x509.c 78.20% 17 Missing ⚠️
Additional details and impacted files
@@           Coverage Diff           @@
##             main    #2128   +/-   ##
=======================================
  Coverage   78.97%   78.97%           
=======================================
  Files         611      611           
  Lines      105552   105632   +80     
  Branches    14950    14951    +1     
=======================================
+ Hits        83361    83425   +64     
- Misses      21538    21558   +20     
+ Partials      653      649    -4     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@samuel40791765 samuel40791765 marked this pull request as ready for review January 22, 2025 22:20
@samuel40791765 samuel40791765 requested a review from a team as a code owner January 22, 2025 22:20
@samuel40791765 samuel40791765 force-pushed the pkcs12-set-mac branch 3 times, most recently from 7f55519 to 655d214 Compare January 23, 2025 18:04
skmcgrail
skmcgrail previously approved these changes Jan 24, 2025
Copy link
Member

@skmcgrail skmcgrail left a comment

Choose a reason for hiding this comment

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

Minor style feedback on tests, non-blocking.

crypto/pkcs8/pkcs12_test.cc Outdated Show resolved Hide resolved
crypto/pkcs8/pkcs12_test.cc Outdated Show resolved Hide resolved
skmcgrail
skmcgrail previously approved these changes Jan 27, 2025
crypto/pkcs8/pkcs8_x509.c Show resolved Hide resolved
crypto/pkcs8/pkcs8_x509.c Show resolved Hide resolved
const uint8_t *orig_authsafe = CBS_data(&authsafe);
size_t orig_authsafe_len = CBS_len(&authsafe);

// Parse for |authsafes| which is the data that we should be running HMAC on.
Copy link
Contributor

Choose a reason for hiding this comment

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

what is "authsafe"? i don't see it mentioned anywhere in the overview

Copy link
Contributor Author

Choose a reason for hiding this comment

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

authsafe is the ASN1 content that we need to run HMAC against. It's a field defined within the PFX syntax for PKCS12: https://datatracker.ietf.org/doc/html/rfc7292#section-4
It has the ContentInfo type, which is defined in another RFC: https://datatracker.ietf.org/doc/html/rfc2315#section-7.

I didn't go into further explanation here, since we have prior art/documentation in our parsers. I also tried sharing the code here, but both parsers need slightly different information extracted.

// Parse for |authsafes| which is the data that we should be running HMAC on.
if (!CBS_get_asn1(&authsafe, &content_type, CBS_ASN1_OBJECT) ||
!CBS_get_asn1(&authsafe, &wrapped_authsafes,
CBS_ASN1_CONTEXT_SPECIFIC | CBS_ASN1_CONSTRUCTED | 0) ||
Copy link
Contributor

Choose a reason for hiding this comment

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

why | 0?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Great question, 0 is the explicit tag that corresponds to this field. We use CBS_ASN1_CONSTRUCTED to OR into a tag to look up the constructed bit.

Other usages of CBS_ASN1_CONSTRUCTED follow a similar pattern: https://github.com/search?q=repo%3Aaws/aws-lc%20CBS_ASN1_CONSTRUCTED&type=code

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants