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: do not persist the keys loaded from PKCS#12 on Windows #14645

Merged
merged 3 commits into from
Aug 19, 2024

Conversation

teo-tsirpanis
Copy link
Contributor

@teo-tsirpanis teo-tsirpanis commented Aug 15, 2024

As part of investigating #14349, I noticed that the PFXImportCertStore function persists the keys on disk by default. This PR fixes that by passing the PKCS12_NO_PERSIST_KEY flag, and geting a preallocated HCRYPTPROV to get the certificate's key.

Validated locally by running _build/Default/google/cloud/storage/oauth2_service_account_credentials_test.exe --gtest_filter=ServiceAccountCredentialsTest.ParseSimpleP12 --gtest_repeat=-1 and letting the test run hundreds of times. I'm not sure if this will fix the occasional CI failures, but this looks like an improvement to me.


This change is Reviewable

Instead of getting the handle with `CryptAcquireCertificatePrivateKey`, we get it from a property of the certificate context.
@teo-tsirpanis teo-tsirpanis requested a review from a team as a code owner August 15, 2024 00:42
@@ -121,9 +112,7 @@ StatusOr<UniqueCertStore> OpenP12File(std::string const& source) {
StatusOr<UniqueCertContext> GetCertificate(HCERTSTORE certstore,
std::string const& source) {
// Get the certificate from the store.
PCCERT_CONTEXT cert_raw = CertFindCertificateInStore(
Copy link
Contributor Author

Choose a reason for hiding this comment

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

CertFindCertificateInStore supports searching with more complex criteria. We just want the first/only certificate in the store, and therefore can use the simpler CertEnumCertificatesInStore.

@dbolduc
Copy link
Member

dbolduc commented Aug 15, 2024

/gcbrun

Copy link

codecov bot commented Aug 15, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 90.91%. Comparing base (bdd3e73) to head (5083504).
Report is 5 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main   #14645      +/-   ##
==========================================
- Coverage   93.59%   90.91%   -2.68%     
==========================================
  Files        2316     1755     -561     
  Lines      207122   133091   -74031     
==========================================
- Hits       193849   121004   -72845     
+ Misses      13273    12087    -1186     

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

@dbolduc dbolduc merged commit cd1c047 into googleapis:main Aug 19, 2024
69 of 70 checks passed
@dbolduc
Copy link
Member

dbolduc commented Aug 19, 2024

Thanks @teo-tsirpanis for the help on Windows!

@teo-tsirpanis teo-tsirpanis deleted the pkcs12-no-persist-cng branch August 19, 2024 15:44
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.

2 participants