Repository navigation
Description
Background and motivation
The Rfc2898DeriveBytes type has constructors with default values for iterations and hashAlgorithm set to 1000 and HashAlgorithmName.SHA1, respectively.
These defaults are not suitable. 1000 iterations of PBKDF2 is too low, and using SHA1 is discouraged now. We can’t change the defaults as that would be a breaking change, and any new defaults decided on today would not be suitable as defaults in the future. Suggested values can be covered in documentation or external resources.
We should obsolete the constructors that provide unsafe defaults. Developers that are impacted by the obsoletion can either suppress it, or use the constructors that explicitly accept the iterations and hashAlgorithm.
API Proposal
namespace System.Security.Cryptography {
public partial class Rfc2898DeriveBytes : DeriveBytes {
// Uses SHA1 by default
[Obsolete("The default hash algorithm and iteration counts in Rfc2898DeriveBytes constructors are outdated and insecure. Use a constructor that accepts the hash algorithm and the number of iterations.")]
public Rfc2898DeriveBytes(byte[] password, byte[] salt, int iterations);
// Uses SHA1 by default and 1000 iteration default
[Obsolete("The default hash algorithm and iteration counts in Rfc2898DeriveBytes constructors are outdated and insecure. Use a constructor that accepts the hash algorithm and the number of iterations.")]
public Rfc2898DeriveBytes(string password, byte[] salt);
// Uses SHA1 by default
[Obsolete("The default hash algorithm and iteration counts in Rfc2898DeriveBytes constructors are outdated and insecure. Use a constructor that accepts the hash algorithm and the number of iterations.")]
public Rfc2898DeriveBytes(string password, byte[] salt, int iterations);
// Uses SHA1 by default and uses 1000 iterations by default
[Obsolete("The default hash algorithm and iteration counts in Rfc2898DeriveBytes constructors are outdated and insecure. Use a constructor that accepts the hash algorithm and the number of iterations.")]
public Rfc2898DeriveBytes(string password, int saltSize);
// Uses SHA1 by default
[Obsolete("The default hash algorithm and iteration counts in Rfc2898DeriveBytes constructors are outdated and insecure. Use a constructor that accepts the hash algorithm and the number of iterations.")]
public Rfc2898DeriveBytes(string password, int saltSize, int iterations);
}
}API Usage
No usage.
Risks
These APIs are likely to have high use.
Alternatives
Implement an analyzer if obsoletion is determined to be too disruptive.
There appears to have been some attempt to move folks toward safer defaults in the past, namely in #21760. I’m unsure exactly what the outcome of that was. The link no longer works and I see no notes in the documentation.
This however was done before obsoleting was something that had a paved path, so perhaps it is worth revisiting as a full obsoletion or analyzer.
Activity
GrabYourPitchforks commentedon Aug 8, 2021
This is likely to be a bit tricky. There are two different sets of customers: people who have existing applications which they want to upgrade while maintaining compat, and people who are writing greenfield code who may be misled into using this bad API. Each audience must take a completely different set of resolution steps.
Reminds me of an older FxCop rule which flagged usage of MD5, SHA1, etc. The goal was to steer people toward using better algorithms, but the end result was that significant numbers of developers ended up suppressing the rule entirely. ("Yes, I know the algorithm is busted. Take it up with the people who wrote the RFC I'm implementing.")
A fixer which automatically rewrites call sites from the old ctors to the new ctors may be useful from the perspective of telling devs more clearly what the underlying behavior is. And that can be done independently of any "insecure algorithm!" analyzer.
Edit: An "insecure algorithm!" analyzer would also run into the sticky situation of us declaring policy on whether HMACSHA1 really is insecure, etc. Last I checked, the underlying algorithm being vulnerable does not automatically imply that an HMAC construct built around that algorithm is also vulnerable. At which point we say "well, maybe not insecure, but perhaps inadvisable," and then the desire to do it drops off considerably.
vcsjones commentedon Aug 8, 2021
Yeah, and to be honest I think that is a fine outcome. I assume this would have its own diagnostic code. We can’t actually move them to something better without breaking their application. If they suppress the obsoletion, they will still be using bad defaults. If an analyzer “fixes” it for them, then maybe they won’t stop to consider why they were obsoleted in the first place and are still using bad defaults. Going right for the light bulb and “no more squiggles” is something I actually wanted to dissuade. Though at least that approach makes it obvious that SHA1 / 1000 iterations is being used.
Yeah, I was not proposing an “insecure algorithm!” analyzer - really just removing our opinions on defaults, which may be interpreted as “use these and you’re good to go”. These defaults are from 2005. I would not, say, propose throwing a runtime exception if SHA1 or a low number of iterations is used.
Indeed, there are no known attacks on HMACMD5, either.
vcsjones commentedon Aug 8, 2021
Perhaps a better issue title would have been "[API Proposal]: Obsolete Rfc2898DeriveBytes constructors with opinionated defaults" but I will refrain from changing the title. 😄
GrabYourPitchforks commentedon Aug 11, 2021
I mean, I'm content to obsolete every instance method on the type and to ask people to use the static one-shots. But that's perhaps overkill. :)
bartonjs commentedon Aug 11, 2021
I think I like the idea of using a fixer to be like "hey, you're using some defaults from 2005. I'm going to put them in your face so you can see what decisions you've made". What would the fixer look for? This obsoletion code, perhaps?
vcsjones commentedon Aug 13, 2021
Am I correctly understanding that an obsolete probably won't fly, so we should re-work this idea in to an analyzer?
bartonjs commentedon Aug 13, 2021
I started off thinking that. Then realized that Obsolete is its own analyzer, so we should just use that. But I think the fixer is an important aspect of it. (Fixers just fire off of diagnostic codes, pretty sure that we could write one that looks for SYSLIB00NM and rewrites the call to a longer signature.)
The only drawback that I see is someone cross-compiling NS2.0 and NET7, but they can just turn off the diagnostic code if they don't want to suppress or #if.
19 remaining items
terrajobst commentedon Jun 3, 2022
Ah, gotcha
AlexLaroche commentedon Dec 27, 2024
Is this still relevant? I believe changes have been made to the API since then if I looked at the documentation [https://learn.microsoft.com/fr-fr/dotnet/api/system.security.cryptography.rfc2898derivebytes.-ctor?view=net-8.0].
See : #67158
I think this issue could be close.
vcsjones commentedon Dec 27, 2024
It was originally left open because at some point there was a thought we might want write an analyzer / code fixer.
Given it hasn't been done in three years, and there has been little to no feedback about this particular obsoletion, I would agree we might as well just close it.
What do you think @bartonjs?
AlexLaroche commentedon Dec 27, 2024
Sorry, I missed the code fixer part when I read the issue this morning.
There is a certain complexity in implementing code fixer due to the fact that we cannot assume that the application is "brand new". The derived values could have been persisted and if we rewrite the constructor functions, we will change the existing behavior. By example, a password validation function returning that the password was identical may now return that the password are different.
Any plans to mark as obsolete these algorithms: SHA1, DES, DSA, RC2, ... ?
vcsjones commentedon Dec 27, 2024
Yep. The intention of the code fixer would not be to "upgrade" the hash that they were using, but to make it apparent. For example, the fixer could change:
To
These two things are functionally identical.
The intention would be to make the use of SHA-1 and the number of iterations visible. It's not better, but it does stop hiding the use of SHA-1 and the low number of iterations.
Not that I am aware of. Generally outdated primitives are not obsoleted. (However APIs that might use them implicitly can be, such as in this case). Instead, analyzers can be enabled.
If you add
<EnableNETAnalyzers>true</EnableNETAnalyzers>to your csproj, then enable theCA5351rule in the .editorconfig, you will get a warning or error, depending on how you configure it, about the use of outdated cryptography.AlexLaroche commentedon Jan 4, 2025
Thanks for the response. This allows me to further my understanding of the entire .NET ecosystem.
According to my understanding, the analyzer and "code fixer" should be added here:
https://github.com/dotnet/roslyn-analyzers/tree/main/src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/Security
There would be two parts to develop. An analyzer and then the "code fixer".
The analyzer would look like this:
How should the id of the added rule be determined?
Am I correct?
vcsjones commentedon Jan 6, 2025
According to my understanding, the analyzer and "code fixer" should be added here:
https://github.com/dotnet/roslyn-analyzers/tree/main/src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/Security
That's the right repository, but I don't know a whole lot about writing an analyzer / fixer. I don't think you need the write an analyzer, just the code fixer. The obsolete diagnostic is already the analyzer. The fixer might be able to fire off the obsoletion diagnostic.
Before you go too far in to this if you are interesting in writing the analyzer or fixer, we might just end up obsoletion all of the constructors. #97221
If we end up doing that, I don't think the code fixer makes much sense to re-write the obsoletion from one obsolete method to another obsolete method.
Having the code-fixer re-write from the instance methods to the static methods (which are now preferred) is a lot more involved, and in some cases not possible depending on how the instance method is being used.
AlexLaroche commentedon Jan 9, 2025
Okay, I'm going to stop so as not to waste my time. I was just curious to understand how to write a parser or fixer in Roselyn as more an intellectual challenge.
I agree that this proposal would make a lot more sense #97221 to implement.
bartonjs commentedon Jan 9, 2025
I believe
[Obsolete]also produces Diagnostics.So no analyzer would be required for these.
A fixer would be possible, as discussed before, but given that a) there's a proposal to remove the members the fixer would rewrite the code to, and b) I don't remember hearing any complaints from people from .NET 7 or 8 about this; I'm just going to go ahead and close out the issue.
The fixer wouldn't be useless with all the ctors marked obsolete, since it would help someone figure out how to get from
to
by the intermediate state of
So, if anyone felt like writing it, I expect we'd take it (over in the analyzers repository)... I'm just turning off the advertising beacon 😄.