Skip to content
This repository was archived by the owner on Jan 23, 2023. It is now read-only.

Make HashAlgorithm.Create(string) and friends work on .NET Core - #28385

Merged
stephentoub merged 4 commits into
dotnet:masterfrom
bartonjs:cryptoprims_create_withname
Mar 24, 2018
Merged

stephentoub merged 4 commits into
dotnet:masterfrom
bartonjs:cryptoprims_create_withname

Conversation

@bartonjs

Copy link
Copy Markdown
Member

The Create(void) methods will continue to throw PNSE, because we do not want
people depending on the "I'll create a thing for you" behavior and assuming that
it has positive value (historically it has been kept as-is in .NET Framework for
compatibility sake). Now they have a message in their PNSE.

The Create(string) methods will forward to CryptoConfig, just like in .NET Framework.
The difference in .NET Core is that CryptoConfig lives in a future assembly, and
rather than build System.Private.Cryptography for this, just build a reflection bridge.

Fixes #22626.

The Create(void) methods will continue to throw PNSE, because we do not want
people depending on the "I'll create a thing for you" behavior and assuming that
it has positive value (historically it has been kept as-is in .NET Framework for
compatibility sake).  Now they have a message in their PNSE.

The Create(string) methods will forward to CryptoConfig, just like in .NET Framework.
The difference in .NET Core is that CryptoConfig lives in a future assembly, and
rather than build System.Private.Cryptography for this, just build a reflection bridge.
@bartonjs

Copy link
Copy Markdown
Member Author

Asking for "ECDsa" doesn't work on non-Windows because it's CNG-backed by default. I'll think on whether I want to remove the test case, or make CryptoConfig do something differently on those systems.

}

s_createFromName = (Func<string, object>)createFromName.CreateDelegate(typeof(Func<string, object>));
}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Could you make this into a static method that returns the func and then use that in the declaration of the field? Then the type can be marked beforefieldinit by the compiler.


const string CreateFromNameMethodName = "CreateFromName";

Type t = Type.GetType(CryptoConfigTypeName, true);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Nit: could you name the bool argument so it's obvious what it is? IIRC, it's throwOnError?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I would if it'd be better to make it throwOnError:false, and then if you get back null, make s_createFromName be a delegate that throws a PlatformNotSupportedException.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

What's the motivation for the PNSE? The type would only be missing if 1) we moved it (which we'd notice via the tests) or 2) a trimmer removed it. If a trimmer removed it then the TypeLoadException seems more informative.

Of course, it could be a PNSE which carries the TypeLoadException or MissingMethodException as an innerException.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I'm fine with either.

{
// These are not supported because CryptoConfig exists in Algorithms assembly.
// CryptoConfig exists in Algorithms partly because it requires the Oid class in Encoding assembly.
// .NET Core does not allow the base classes to pick an algorithm.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

And we still want to stick with that behavior?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Yeah. HashAlgorithm.Create() was built as being SHA-1, because SHA-1 was the best algorithm available at the time. Now SHA-1 is considered broken, but our normal compat rules say that we can't change it for Framework, because a number of people could be

a) blind casting it to SHA1CryptoServiceProvider
b) Depending on the output value without checking to see what algorithm was even used.

The only way to use it correctly would be if you call it before producing a new hash, then you inspect the return value and write down an identifier which will get you to tell the hash recipient (you or a remote party) which algorithm was used so they can get the right one later. So the too-much-of-a-base-class Create() methods are pits of failure. With slick walls. And spikes at the bottom. If bubonic plague can survive in stagnant water it is probably in this pit.

@GrabYourPitchforks GrabYourPitchforks left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Only minor feedback, not critical.

<value>FlushFinalBlock() method was called twice on a CryptoStream. It can only be called once.</value>
</data>
<data name="Cryptography_DefaultAlgorithm_NotSupported" xml:space="preserve">
<value>This platform does not allow the automatic selection of an algorithm.</value>

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Nit: I almost feel like we'll want an fwlink in here to direct developers to documentation showing the correct usage pattern.

{
return Create("System.Security.Cryptography.AsymmetricAlgorithm");
}
public static AsymmetricAlgorithm Create() =>

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Is it possible to add [Obsolete] (as error) to the reference assemblies so that devs targeting netstandard or netcoreapp will see the failure at compilation time?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Last I knew: no (against our rules). But we were/did/will build(ing) an analyzer pack of methods that are cross-platform risks and/or just generally disrecommended. Rfc2898DeriveBytes overloads which don't take the HashAlgorithmName are on it, as are all of these.

@bartonjs

Copy link
Copy Markdown
Member Author

@dotnet-bot Test Windows x86 Release Build please (System.Data.Tests.DataTableTest4/XmlTest13 failed with System.UnauthorizedAccessException : Access to the path 'C:\Users\DotNetTestRunner\AppData\Local' is denied.)

@stephentoub
stephentoub merged commit 57608f6 into dotnet:master Mar 24, 2018
@bartonjs
bartonjs deleted the cryptoprims_create_withname branch March 26, 2018 17:34
@karelz karelz added this to the 2.1.0 milestone Mar 27, 2018
picenka21 pushed a commit to picenka21/runtime that referenced this pull request Feb 18, 2022
…et/corefx#28385)

* Make HashAlgorithm.Create(string) and friends work on .NET Core

The Create(void) methods will continue to throw PNSE, because we do not want
people depending on the "I'll create a thing for you" behavior and assuming that
it has positive value (historically it has been kept as-is in .NET Framework for
compatibility sake).  Now they have a message in their PNSE.

The Create(string) methods will forward to CryptoConfig, just like in .NET Framework.
The difference in .NET Core is that CryptoConfig lives in a future assembly, and
rather than build System.Private.Cryptography for this, just build a reflection bridge.

* Review feedback

* Make CryptoConfig.CreateFromName("ECDsa") work on non-Windows

* Make the CryptoConfig tests aware that "ECDsa" works on non-Windows now


Commit migrated from dotnet/corefx@57608f6
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants