-
Notifications
You must be signed in to change notification settings - Fork 4.7k
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
Question: best practice of accepting password for X509Certificate2 constructors in .NET5 #51415
Comments
Tagging subscribers to this area: @bartonjs, @vcsjones, @krwq, @GrabYourPitchforks Issue DetailsDescriptionFrom the doc , there are several constructors have a parameter of a password used to access the certificate.
As we found the SecureString should not be used at https://github.com/dotnet/platform-compat/blob/master/docs/DE0001.md. If we are going to implement a command with .NET 5, accepting the password(if there is any) from customer, then sign with the certificate, is it a problem if just using String to store the password?
|
Related: #48697 (comment) If you really care about security, you could manage some memory yourself, and pass it through span. |
This is a very polarizing question, and can depend on app specifics, but the general answer is "no, it's not a problem". The risk of using System.String is that the password value can remain in-memory long after it is no longer needed. If memory inspection and memory dumps aren't a strong threat for your scenarios, then you're good to go.
If memory inspection and/or memory dumps of your process containing a password (beyond when it's needed) is a crisis, then you can do something like char[] arr = GC.AllocateArray<char>(512, pinned: true);
int offset = 0;
while (true)
{
int ch = Console.Read();
if (ch == -1 || ch == (int)'\n')
{
break;
}
if (ch == (int)'\r')
{
ch = Console.Peek();
if (ch == (int)'\n')
{
Console.Read();
}
break;
}
if (offset == arr.Length)
{
char[] arr2 = GC.AllocateArray<char>(arr.Length * 2, pinned: true);
arr.AsSpan().CopyTo(arr2);
arr.Clear();
arr = arr2;
}
arr[offset] = (char)ch;
offset++;
}
X509Certificate2 cert = new X509Certificate2(bytes, arr.AsSpan(0, offset), flags);
arr.Clear();
... But that's a lot of work. |
Thanks for your help @bartonjs ! |
Description
From the doc , there are several constructors have a parameter of a password used to access the certificate.
e.g.
As we found the SecureString should not be used at https://github.com/dotnet/platform-compat/blob/master/docs/DE0001.md.
And we also found new API proposals in .NET 6 at https://github.com/dotnet/designs/pull/147/files
If we are going to implement a command with .NET 5, accepting the password(if there is any) from customer, then sign with the certificate, is it a problem if just using String to store the password?
If it's not acceptable, is there any standard we should follow, since SecureString is also not recommended?
And is there any applicable way to get a password and sign with the certificate safely?
Especially in .NET 5 on Windows and non-Windows platforms.
Thanks!
The text was updated successfully, but these errors were encountered: