-
Notifications
You must be signed in to change notification settings - Fork 5.2k
Fix Equals for cryptographic wrappers #118715
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
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull Request Overview
This PR fixes a fundamental issue with the Equals
method implementation in cryptographic wrapper classes (RSAWrapper, ECDsaWrapper, and ECDiffieHellmanWrapper), which violated the Object.Equals contract by never returning true for self-comparison.
Key changes:
- Updated
Equals
method in all three wrapper classes to properly compare the wrapped instances when both objects are wrappers - Added comprehensive unit tests to verify correct equality behavior for all cryptographic factory methods
Reviewed Changes
Copilot reviewed 10 out of 10 changed files in this pull request and generated no comments.
Show a summary per file
File | Description |
---|---|
RSAWrapper.cs | Fixed Equals implementation to compare wrapped instances when both objects are RSAWrapper |
ECDsaWrapper.cs | Fixed Equals implementation to compare wrapped instances when both objects are ECDsaWrapper |
ECDiffieHellmanWrapper.cs | Fixed Equals implementation to compare wrapped instances when both objects are ECDiffieHellmanWrapper |
RSAFactoryTests.cs | Added tests verifying RSA instances satisfy reflexive equality and different instances are not equal |
ECDsaFactoryTests.cs | Added tests verifying ECDsa instances satisfy reflexive equality and different instances are not equal |
ECDiffieHellmanFactoryTests.cs | Added tests verifying ECDiffieHellman instances satisfy reflexive equality and different instances are not equal |
*.Tests.csproj | Updated test project files to include the new factory test files |
You can also share your feedback on Copilot code review for a chance to win a $100 gift card. Take the survey.
...ries/System.Security.Cryptography/src/System/Security/Cryptography/ECDiffieHellmanWrapper.cs
Outdated
Show resolved
Hide resolved
Tagging subscribers to this area: @dotnet/area-system-security, @bartonjs, @vcsjones |
A final, final possible solution is to not do anything and say "Cryptographic primitives are like NaN!" No? Bad idea? Okay. |
src/libraries/System.Security.Cryptography/src/System/Security/Cryptography/RSAWrapper.cs
Show resolved
Hide resolved
One more: runtime/src/libraries/System.Security.Cryptography/src/System/Security/Cryptography/DSAWrapper.cs Line 153 in f53ffe6
|
Thanks! |
...es/Common/tests/System/Security/Cryptography/AlgorithmImplementations/DSA/DSAFactoryTests.cs
Outdated
Show resolved
Hide resolved
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good aside from one copy/paste/bad-edit
…thmImplementations/DSA/DSAFactoryTests.cs Co-authored-by: Jeremy Barton <jbarton@microsoft.com>
The internal cryptographic wrapper, ECDsaWrapper, RSAWrapper, ECDiffieHellmanWrapper, and DSAWrapper implemented
Equals
in such a way that it was never going to be true. It delegated equals to "what was wrapped", but since you can't get what is wrapped out, you would end up comparing a wrapper to a wrapped.This violated FDG 8.9.1
Instead, let's let wrappers compare what is being wrapped.
Fixes #118701