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

Commit 9b16041

Browse files
committed
Merge pull request #2226 from justinvp/capihelper
RSA: Read the exponent without LowLevelList<T>
2 parents ded0488 + 6d3f402 commit 9b16041

File tree

2 files changed

+39
-21
lines changed

2 files changed

+39
-21
lines changed

src/System.Security.Cryptography.RSA/src/System.Security.Cryptography.RSA.csproj

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,6 @@
3838
<Compile Include="System\Security\Cryptography\RSAParameters.cs" />
3939
</ItemGroup>
4040
<ItemGroup Condition=" '$(TargetsWindows)' == 'true' ">
41-
<Compile Include="$(CommonPath)\System\Collections\Generic\LowLevelList.cs">
42-
<Link>Common\System\Collections\Generic\LowLevelList.cs</Link>
43-
</Compile>
4441
<Compile Include="$(CommonPath)\Interop\Windows\Crypt32\OidInfo.cs">
4542
<Link>Common\Interop\Windows\Crypt32\OidInfo.cs</Link>
4643
</Compile>

src/System.Security.Cryptography.RSA/src/System/Security/Cryptography/CapiHelper.cs

Lines changed: 39 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1039,26 +1039,9 @@ internal static RSAParameters ToRSAParameters(this byte[] cspBlob, bool includeP
10391039
int halfModulusLength = (modulusLength + 1) / 2;
10401040

10411041
uint expAsDword = br.ReadUInt32();
1042-
byte[] exponent;
1043-
if (expAsDword == 0)
1044-
{
1045-
exponent = new byte[] { 0 };
1046-
}
1047-
else
1048-
{
1049-
LowLevelList<byte> exponentBytes = new LowLevelList<byte>();
1050-
while (expAsDword != 0)
1051-
{
1052-
byte b = (byte)expAsDword;
1053-
exponentBytes.Add(b);
1054-
expAsDword >>= 8;
1055-
}
1056-
exponent = exponentBytes.ToArray();
1057-
Array.Reverse(exponent);
1058-
}
10591042

10601043
RSAParameters rsaParameters = new RSAParameters();
1061-
rsaParameters.Exponent = exponent;
1044+
rsaParameters.Exponent = ExponentAsBytes(expAsDword);
10621045
rsaParameters.Modulus = br.ReadReversed(modulusLength);
10631046
if (includePrivateParameters)
10641047
{
@@ -1080,6 +1063,44 @@ internal static RSAParameters ToRSAParameters(this byte[] cspBlob, bool includeP
10801063
}
10811064
}
10821065

1066+
/// <summary>
1067+
/// Helper for converting a UInt32 exponent to bytes.
1068+
/// </summary>
1069+
private static byte[] ExponentAsBytes(uint exponent)
1070+
{
1071+
if (exponent <= 0xFF)
1072+
{
1073+
return new[] { (byte)exponent };
1074+
}
1075+
else if (exponent <= 0xFFFF)
1076+
{
1077+
return new[]
1078+
{
1079+
(byte)(exponent >> 8),
1080+
(byte)(exponent)
1081+
};
1082+
}
1083+
else if (exponent <= 0xFFFFFF)
1084+
{
1085+
return new[]
1086+
{
1087+
(byte)(exponent >> 16),
1088+
(byte)(exponent >> 8),
1089+
(byte)(exponent)
1090+
};
1091+
}
1092+
else
1093+
{
1094+
return new[]
1095+
{
1096+
(byte)(exponent >> 24),
1097+
(byte)(exponent >> 16),
1098+
(byte)(exponent >> 8),
1099+
(byte)(exponent)
1100+
};
1101+
}
1102+
}
1103+
10831104
/// <summary>
10841105
/// Read in a byte array in reverse order.
10851106
/// </summary>

0 commit comments

Comments
 (0)