Skip to content

Remove Obsolete APIs #4968

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

Merged
merged 15 commits into from
Aug 18, 2021
Merged

Remove Obsolete APIs #4968

merged 15 commits into from
Aug 18, 2021

Conversation

singhashish-wpf
Copy link
Member

@singhashish-wpf singhashish-wpf commented Aug 2, 2021

Fixes Issue #3660

Description

WPF is using obsolete APIs. These need to be replaced for 6.0.

WebRequest.WebRequest()
WebRequest.Create(Uri)
Uri.EscapeUriString(string)

Customer Impact

Warnings related to various Obsolete APIs

Regression

No

Testing

Build
CI/CD

Risk

@singhashish-wpf singhashish-wpf requested a review from a team as a code owner August 2, 2021 08:45
@ghost ghost added the PR metadata: Label to tag PRs, to facilitate with triage label Aug 2, 2021
@ghost ghost requested review from fabiant3 and ryalanms August 2, 2021 08:45
@singhashish-wpf singhashish-wpf requested review from SamBent and removed request for a team August 2, 2021 08:46
#pragma warning disable SYSLIB0028
AsymmetricAlgorithm testKey = x509Certificate2.PrivateKey;
#pragma warning restore SYSLIB0028
AsymmetricAlgorithm testKey = x509Certificate2.GetRSAPrivateKey();
Copy link
Contributor

Choose a reason for hiding this comment

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

@singhashish-wpf As per comments above, the caller is expecting exception, if cryptographic operations fails. GetRSAPrivateKey won't do that.
Please cross check the consequence of not having the exceptions.

Copy link
Member Author

Choose a reason for hiding this comment

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

Throwing exception if the privatekey is null.
Commit : c43cd8d

@vishalmsft vishalmsft self-requested a review August 2, 2021 11:10
@azure-pipelines
Copy link

Azure Pipelines successfully started running 1 pipeline(s).

@ryalanms ryalanms added this to the 6.0.0 milestone Aug 3, 2021
@ThomasGoulet73
Copy link
Contributor

There are still some usage of ReliabilityContractAttribute in C++/CLI assemblies: https://github.com/dotnet/wpf/search?l=C%2B%2B&q=4950. I think those should be removed as well if they are removed in C# code. They were probably not found at first because they do not use the same warning code. C++/CLI raise the warning with the code 4950 for usage of APIs with ObsoleteAttribute.

@ryalanms
Copy link
Member

Please do not merge this until our testing is completed later today.

@ryalanms ryalanms added the * NO MERGE * metadata: The PR is not ready for merge yet (see discussion for detailed reasons) label Aug 17, 2021
#pragma warning disable SYSLIB0028
return cert.PrivateKey;
#pragma warning restore SYSLIB0028
throw new CryptographicException("PrivateKey is not retrievable.");
Copy link
Contributor

Choose a reason for hiding this comment

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

You are now throwing an exception if all three of the Get*Key methods return null.

  1. Does cert.PrivateKey also throw in that case? I couldn't find a doc that guaranteed that.
  2. Does it throw the same exception, with the same message? If not, this is a breaking change.
  3. Does the message need to be localized? If it's visible to users (i.e. not caught by WPF code), then yes.
  4. The comment at the head of the method says the keys returned by PrivateKey are different from Get*Key. If answer to (1) is 'no', any code that acts on that difference is now obsolete. Is there such code? (Not visible in this review.)

Copy link
Member

Choose a reason for hiding this comment

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

https://docs.microsoft.com/en-us/dotnet/api/system.security.cryptography.x509certificates.x509certificate2.privatekey?view=net-5.0.

For 1, there is no specific language about a null key throwing, but it does say an invalid RSA, DSA, or 'unreadable' key will throw CryptographicException. One indication that it throws if everything is null is that the callers like PackageDigitalSignatureManager.Sign catch but do not handle the null case.

For 2/3, we can leave the 509certificate2.privatekey replacement for RC2 and check in everything else. Or if 509certificate2.privatekey can only throw a single error code in CryptographicException when all three Get*Key methods return null, then it can be replaced now.

For 4, I read the comment as the PrivateKey property was used as a replacement for the Get*PrivateKey methods to intentionally cause an exception, rather than returning null.

Copy link
Contributor

Choose a reason for hiding this comment

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

That doc doesn't mention ECDsa keys at all, so I'm not sure whether PrivateKey can be an ECDsa key at all, and if so whether c.PrivateKey throws. The doc could be literally true, or simply out-of-date since ECDsa was added. Have you or Ashish asked the crypto team?

In any case, if we called c.PrivateKey to cause a user-visible exception, we are now throwing that exception ourselves and for compat we must throw the same exception (we do, if doc is correct) with the same message (unlikely), and that message needs localization. Ask the crypto team what message they use, and use SR to localize it. Plenty of examples in our code.

#pragma warning disable SYSLIB0028
AsymmetricAlgorithm testKey = x509Certificate2.PrivateKey;
#pragma warning restore SYSLIB0028
throw new CryptographicException("PrivateKey is not retrievable.");
Copy link
Contributor

Choose a reason for hiding this comment

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

Same concerns as my remark in XmlDigitalSignatureProcessor.cs. Except this exception is caught 8 lines down, so it's not visible to user.

Copy link
Contributor

Choose a reason for hiding this comment

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

The try/catch could probably be removed and just set requestAgain to true instead of throwing since none of the Get*Key methods are documented to throw CryptographicException.

@ryalanms
Copy link
Member

DRTs and Microsuite tests showed no regressions vs the most recent changes in main.

@ryalanms ryalanms removed the * NO MERGE * metadata: The PR is not ready for merge yet (see discussion for detailed reasons) label Aug 17, 2021
Copy link
Contributor

@SamBent SamBent left a comment

Choose a reason for hiding this comment

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

Approving the version without crypto changes.

@ryalanms ryalanms merged commit 07edfec into main Aug 18, 2021
@singhashish-wpf singhashish-wpf deleted the obsoleteapis branch December 17, 2021 06:05
@ghost ghost locked as resolved and limited conversation to collaborators Apr 7, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
PR metadata: Label to tag PRs, to facilitate with triage
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants