镜像站点 · 本页由第三方 GitHub 只读镜像提供,非 GitHub 官方站点,不接受任何登录或凭据输入。前往 github.com
Skip to content

Resize PFX export buffer after export as appropriate - #136365

Merged
vcsjones merged 3 commits into
dotnet:mainfrom
vcsjones:win-pkcs12-export-padding-fix
Oct 24, 2026
Merged

vcsjones merged 3 commits into
dotnet:mainfrom
vcsjones:win-pkcs12-export-padding-fix

Conversation

@vcsjones

Copy link
Copy Markdown
Member

Fixes #136316

@vcsjones vcsjones added this to the 12.0.0 milestone Oct 23, 2026
@vcsjones vcsjones self-assigned this Oct 23, 2026
@vcsjones
vcsjones requested review from bartonjs and a balanced review from Copilot October 23, 2026 14:47
@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
Successfully started running 3 pipeline(s).
13 pipeline(s) were filtered out due to trigger conditions.
There may be pipelines that require an authorized user to comment /azp run to run.

@dotnet-policy-service

Copy link
Copy Markdown
Contributor

Tagging subscribers to this area: @bartonjs, @vcsjones, @dotnet/area-system-security
See info in area-owners.md if you want to be subscribed.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🟢 Approval recommended

The targeted length correction addresses the reported defect, with regression coverage and only optional spelling cleanup remaining.

1 open issue
What changed in this PR

Fixes Windows PKCS#12 exports returning trailing bytes by using the final native export length, addressing interoperability failures described in #136316.

Changes:

  • Resize returned PFX buffers to the actual exported length.
  • Pass only the exported bytes to the re-encryption helper.
  • Add single-certificate and collection regression tests with a shared legacy RSA fixture.

Review was source-only; no builds or tests were run.

File Description
src/​libraries/​System.Security.Cryptography/​tests/​X509Certificates/​ExportTests.cs Adds a legacy Windows single-certificate export regression test.
src/​libraries/​System.Security.Cryptography/​tests/​X509Certificates/​CollectionTests.cs Adds corresponding collection export coverage.
src/​libraries/​System.Security.Cryptography/​src/​System/​Security/​Cryptography/​X509Certificates/​StorePal.Windows.Export.cs Uses the final native length for returned and re-encrypted PFX data.
src/​libraries/​Common/​tests/​System/​Security/​Cryptography/​X509Certificates/​TestData.cs Adds the certificate and CSP key fixture for the regression tests.

Review effort: Balanced

@vcsjones
vcsjones enabled auto-merge (squash) October 23, 2026 19:17
@vcsjones

Copy link
Copy Markdown
Member Author

/azp run runtime

@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
Successfully started running 1 pipeline(s).

@vcsjones

Copy link
Copy Markdown
Member Author

/azp run runtime-coreclr libraries-jitstress

@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
Successfully started running 1 pipeline(s).

@vcsjones

Copy link
Copy Markdown
Member Author

Running the "Windows 10SP0" pipelines because I am suspicious these tests may fail on Windows 10 if the certs got RC2 encrypted.

@vcsjones

Copy link
Copy Markdown
Member Author

/ba-g failures are #136367

@vcsjones
vcsjones merged commit 814ed7c into dotnet:main Oct 24, 2026
94 of 96 checks passed
@bartonjs

Copy link
Copy Markdown
Member

/backport to release/10.0

@github-actions

Copy link
Copy Markdown
Contributor

Started backporting to release/10.0 (link to workflow run)

@bartonjs

Copy link
Copy Markdown
Member

/backport to release/11.0

@github-actions

Copy link
Copy Markdown
Contributor

Started backporting to release/11.0 (link to workflow run)

@vcsjones
vcsjones deleted the win-pkcs12-export-padding-fix branch October 26, 2026 21:05
@bartonjs bartonjs added the cryptographic-docs-impact Issues impacting cryptographic docs. Cleared and reused after documentation is updated each release. label Nov 6, 2026
ViveliDuCh pushed a commit that referenced this pull request Nov 10, 2026
…136415)

Backport of #136365 to release/11.0

/cc @bartonjs @vcsjones

## Customer Impact

- [x] Customer reported
- [ ] Found internally

Reported in #136316. Customers
that export a certificate with a private key using PKCS#12 ("PFX")
format may include trailing zeros in the export. This impacts certain
legacy RSA keys used with `RSACryptoServiceProvider`.

Some ecosystems do not tolerate the trailing zeros in the PKCS#12 and
reject them as input. This prevents interoperability between .NET and
those ecosystems.

## Regression

- [x] Yes
- [ ] No

Regressed from .NET Framework.

## Testing

Custom provided a reproduction which was adapted in to a unit test.
Existing unit tests ensure current export scenarios continue to work.

## Risk

Low. This change is in a well-tested area and the needed change was well
understood. A buffer was not being correctly resized based on an API's
reported "amount written".

Co-authored-by: Kevin Jones <kevin@vcsjones.com>
Co-authored-by: Jeremy Barton <jbarton@microsoft.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area-System.Security cryptographic-docs-impact Issues impacting cryptographic docs. Cleared and reused after documentation is updated each release.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Windows: legacy PKCS#12 export can return trailing bytes beyond the encoded PFX

3 participants