Skip to content
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

Update recovery code generation #40723

Merged
merged 3 commits into from
Mar 16, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 54 additions & 1 deletion src/Identity/Extensions.Core/src/UserManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Runtime.InteropServices;
using System.Security.Claims;
using System.Security.Cryptography;
using System.Text;
Expand Down Expand Up @@ -2276,7 +2277,59 @@ public virtual string GenerateNewAuthenticatorKey()
/// </summary>
/// <returns></returns>
protected virtual string CreateTwoFactorRecoveryCode()
=> Guid.NewGuid().ToString().Substring(0, 8);
{
var recoveryCode = new StringBuilder(11);
Copy link
Contributor

Choose a reason for hiding this comment

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

Since it's a fixed-length string, maybe we could use char[] instead of StringBuilder?

Copy link
Member

Choose a reason for hiding this comment

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

Since the goal is to generate a string, in that case it wouldn't matter much, SB is allocating an array internally and then generates a single string out of it.

recoveryCode.Append(GetRandomRecoveryCodeChar());
recoveryCode.Append(GetRandomRecoveryCodeChar());
recoveryCode.Append(GetRandomRecoveryCodeChar());
recoveryCode.Append(GetRandomRecoveryCodeChar());
recoveryCode.Append(GetRandomRecoveryCodeChar());
recoveryCode.Append('-');
recoveryCode.Append(GetRandomRecoveryCodeChar());
recoveryCode.Append(GetRandomRecoveryCodeChar());
recoveryCode.Append(GetRandomRecoveryCodeChar());
recoveryCode.Append(GetRandomRecoveryCodeChar());
recoveryCode.Append(GetRandomRecoveryCodeChar());
return recoveryCode.ToString();
}

// We don't want to use any confusing characters like 0/O 1/I/L/l
// Taken from windows valid product key source
private static readonly char[] AllowedChars = "23456789BCDFGHJKMNPQRTVWXY".ToCharArray();
private static char GetRandomRecoveryCodeChar()
{
// Based on RandomNumberGenerator implementation of GetInt32
uint range = (uint)AllowedChars.Length - 1;

// Create a mask for the bits that we care about for the range. The other bits will be
// masked away.
uint mask = range;
mask |= mask >> 1;
mask |= mask >> 2;
mask |= mask >> 4;
mask |= mask >> 8;
mask |= mask >> 16;

#if NETCOREAPP
Span<uint> resultBuffer = stackalloc uint[1];
#else
var resultBuffer = new byte[1];
#endif
uint result;

do
{
#if NETCOREAPP
RandomNumberGenerator.Fill(MemoryMarshal.AsBytes(resultBuffer));
#else
_rng.GetBytes(resultBuffer);
#endif
result = mask & resultBuffer[0];
}
while (result > range);

return AllowedChars[(int)result];
}

/// <summary>
/// Returns whether a recovery code is valid for a user. Note: recovery codes are only valid
Expand Down