Skip to content
Merged
Show file tree
Hide file tree
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
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@
<asn:Sequence
xmlns:asn="http://schemas.dot.net/asnxml/201808/"
name="RSAPrivateKeyAsn"
namespace="System.Security.Cryptography.Asn1"
rebind="false">
namespace="System.Security.Cryptography.Asn1">

<!--
https://tools.ietf.org/html/rfc3447#appendix-C
Expand All @@ -29,12 +28,12 @@
Since we don't support otherPrimeInfos (Version=1) just don't map it in.
-->
<asn:Integer name="Version" backingType="int" />
<asn:Integer name="Modulus" />
<asn:Integer name="PublicExponent" />
<asn:Integer name="PrivateExponent" />
<asn:Integer name="Prime1" />
<asn:Integer name="Prime2" />
<asn:Integer name="Exponent1" />
<asn:Integer name="Exponent2" />
<asn:Integer name="Coefficient" />
<asn:Integer name="Modulus" backingType="ReadOnlyMemory" />
<asn:Integer name="PublicExponent" backingType="ReadOnlyMemory" />
<asn:Integer name="PrivateExponent" backingType="ReadOnlyMemory" />
<asn:Integer name="Prime1" backingType="ReadOnlyMemory" />
<asn:Integer name="Prime2" backingType="ReadOnlyMemory" />
<asn:Integer name="Exponent1" backingType="ReadOnlyMemory" />
<asn:Integer name="Exponent2" backingType="ReadOnlyMemory" />
<asn:Integer name="Coefficient" backingType="ReadOnlyMemory" />
</asn:Sequence>
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,14 @@ namespace System.Security.Cryptography.Asn1
internal partial struct RSAPrivateKeyAsn
{
internal int Version;
internal System.Numerics.BigInteger Modulus;
internal System.Numerics.BigInteger PublicExponent;
internal System.Numerics.BigInteger PrivateExponent;
internal System.Numerics.BigInteger Prime1;
internal System.Numerics.BigInteger Prime2;
internal System.Numerics.BigInteger Exponent1;
internal System.Numerics.BigInteger Exponent2;
internal System.Numerics.BigInteger Coefficient;
internal ReadOnlyMemory<byte> Modulus;
internal ReadOnlyMemory<byte> PublicExponent;
internal ReadOnlyMemory<byte> PrivateExponent;
internal ReadOnlyMemory<byte> Prime1;
internal ReadOnlyMemory<byte> Prime2;
internal ReadOnlyMemory<byte> Exponent1;
internal ReadOnlyMemory<byte> Exponent2;
internal ReadOnlyMemory<byte> Coefficient;

internal readonly void Encode(AsnWriter writer)
{
Expand All @@ -31,14 +31,14 @@ internal readonly void Encode(AsnWriter writer, Asn1Tag tag)
writer.PushSequence(tag);

writer.WriteInteger(Version);
writer.WriteInteger(Modulus);
writer.WriteInteger(PublicExponent);
writer.WriteInteger(PrivateExponent);
writer.WriteInteger(Prime1);
writer.WriteInteger(Prime2);
writer.WriteInteger(Exponent1);
writer.WriteInteger(Exponent2);
writer.WriteInteger(Coefficient);
writer.WriteInteger(Modulus.Span);
writer.WriteInteger(PublicExponent.Span);
writer.WriteInteger(PrivateExponent.Span);
writer.WriteInteger(Prime1.Span);
writer.WriteInteger(Prime2.Span);
writer.WriteInteger(Exponent1.Span);
writer.WriteInteger(Exponent2.Span);
writer.WriteInteger(Coefficient.Span);
writer.PopSequence(tag);
}

Expand All @@ -53,7 +53,7 @@ internal static RSAPrivateKeyAsn Decode(Asn1Tag expectedTag, ReadOnlyMemory<byte
{
AsnValueReader reader = new AsnValueReader(encoded.Span, ruleSet);

DecodeCore(ref reader, expectedTag, out RSAPrivateKeyAsn decoded);
DecodeCore(ref reader, expectedTag, encoded, out RSAPrivateKeyAsn decoded);
reader.ThrowIfNotEmpty();
return decoded;
}
Expand All @@ -63,42 +63,53 @@ internal static RSAPrivateKeyAsn Decode(Asn1Tag expectedTag, ReadOnlyMemory<byte
}
}

internal static void Decode(ref AsnValueReader reader, out RSAPrivateKeyAsn decoded)
internal static void Decode(ref AsnValueReader reader, ReadOnlyMemory<byte> rebind, out RSAPrivateKeyAsn decoded)
{
Decode(ref reader, Asn1Tag.Sequence, out decoded);
Decode(ref reader, Asn1Tag.Sequence, rebind, out decoded);
}

internal static void Decode(ref AsnValueReader reader, Asn1Tag expectedTag, out RSAPrivateKeyAsn decoded)
internal static void Decode(ref AsnValueReader reader, Asn1Tag expectedTag, ReadOnlyMemory<byte> rebind, out RSAPrivateKeyAsn decoded)
{
try
{
DecodeCore(ref reader, expectedTag, out decoded);
DecodeCore(ref reader, expectedTag, rebind, out decoded);
}
catch (AsnContentException e)
{
throw new CryptographicException(SR.Cryptography_Der_Invalid_Encoding, e);
}
}

private static void DecodeCore(ref AsnValueReader reader, Asn1Tag expectedTag, out RSAPrivateKeyAsn decoded)
private static void DecodeCore(ref AsnValueReader reader, Asn1Tag expectedTag, ReadOnlyMemory<byte> rebind, out RSAPrivateKeyAsn decoded)
{
decoded = default;
AsnValueReader sequenceReader = reader.ReadSequence(expectedTag);
ReadOnlySpan<byte> rebindSpan = rebind.Span;
int offset;
ReadOnlySpan<byte> tmpSpan;


if (!sequenceReader.TryReadInt32(out decoded.Version))
{
sequenceReader.ThrowIfNotEmpty();
}

decoded.Modulus = sequenceReader.ReadInteger();
decoded.PublicExponent = sequenceReader.ReadInteger();
decoded.PrivateExponent = sequenceReader.ReadInteger();
decoded.Prime1 = sequenceReader.ReadInteger();
decoded.Prime2 = sequenceReader.ReadInteger();
decoded.Exponent1 = sequenceReader.ReadInteger();
decoded.Exponent2 = sequenceReader.ReadInteger();
decoded.Coefficient = sequenceReader.ReadInteger();
tmpSpan = sequenceReader.ReadIntegerBytes();
decoded.Modulus = rebindSpan.Overlaps(tmpSpan, out offset) ? rebind.Slice(offset, tmpSpan.Length) : tmpSpan.ToArray();
tmpSpan = sequenceReader.ReadIntegerBytes();
decoded.PublicExponent = rebindSpan.Overlaps(tmpSpan, out offset) ? rebind.Slice(offset, tmpSpan.Length) : tmpSpan.ToArray();
tmpSpan = sequenceReader.ReadIntegerBytes();
decoded.PrivateExponent = rebindSpan.Overlaps(tmpSpan, out offset) ? rebind.Slice(offset, tmpSpan.Length) : tmpSpan.ToArray();
tmpSpan = sequenceReader.ReadIntegerBytes();
decoded.Prime1 = rebindSpan.Overlaps(tmpSpan, out offset) ? rebind.Slice(offset, tmpSpan.Length) : tmpSpan.ToArray();
tmpSpan = sequenceReader.ReadIntegerBytes();
decoded.Prime2 = rebindSpan.Overlaps(tmpSpan, out offset) ? rebind.Slice(offset, tmpSpan.Length) : tmpSpan.ToArray();
tmpSpan = sequenceReader.ReadIntegerBytes();
decoded.Exponent1 = rebindSpan.Overlaps(tmpSpan, out offset) ? rebind.Slice(offset, tmpSpan.Length) : tmpSpan.ToArray();
tmpSpan = sequenceReader.ReadIntegerBytes();
decoded.Exponent2 = rebindSpan.Overlaps(tmpSpan, out offset) ? rebind.Slice(offset, tmpSpan.Length) : tmpSpan.ToArray();
tmpSpan = sequenceReader.ReadIntegerBytes();
decoded.Coefficient = rebindSpan.Overlaps(tmpSpan, out offset) ? rebind.Slice(offset, tmpSpan.Length) : tmpSpan.ToArray();

sequenceReader.ThrowIfNotEmpty();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@
<asn:Sequence
xmlns:asn="http://schemas.dot.net/asnxml/201808/"
name="RSAPublicKeyAsn"
namespace="System.Security.Cryptography.Asn1"
rebind="false">
namespace="System.Security.Cryptography.Asn1">

<!--
https://tools.ietf.org/html/rfc3447#appendix-C
Expand All @@ -13,6 +12,6 @@
publicExponent INTEGER - e
}
-->
<asn:Integer name="Modulus" />
<asn:Integer name="PublicExponent" />
<asn:Integer name="Modulus" backingType="ReadOnlyMemory" />
<asn:Integer name="PublicExponent" backingType="ReadOnlyMemory" />
</asn:Sequence>
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ namespace System.Security.Cryptography.Asn1
[StructLayout(LayoutKind.Sequential)]
internal partial struct RSAPublicKeyAsn
{
internal System.Numerics.BigInteger Modulus;
internal System.Numerics.BigInteger PublicExponent;
internal ReadOnlyMemory<byte> Modulus;
internal ReadOnlyMemory<byte> PublicExponent;

internal readonly void Encode(AsnWriter writer)
{
Expand All @@ -23,8 +23,8 @@ internal readonly void Encode(AsnWriter writer, Asn1Tag tag)
{
writer.PushSequence(tag);

writer.WriteInteger(Modulus);
writer.WriteInteger(PublicExponent);
writer.WriteInteger(Modulus.Span);
writer.WriteInteger(PublicExponent.Span);
writer.PopSequence(tag);
}

Expand All @@ -39,7 +39,7 @@ internal static RSAPublicKeyAsn Decode(Asn1Tag expectedTag, ReadOnlyMemory<byte>
{
AsnValueReader reader = new AsnValueReader(encoded.Span, ruleSet);

DecodeCore(ref reader, expectedTag, out RSAPublicKeyAsn decoded);
DecodeCore(ref reader, expectedTag, encoded, out RSAPublicKeyAsn decoded);
reader.ThrowIfNotEmpty();
return decoded;
}
Expand All @@ -49,30 +49,35 @@ internal static RSAPublicKeyAsn Decode(Asn1Tag expectedTag, ReadOnlyMemory<byte>
}
}

internal static void Decode(ref AsnValueReader reader, out RSAPublicKeyAsn decoded)
internal static void Decode(ref AsnValueReader reader, ReadOnlyMemory<byte> rebind, out RSAPublicKeyAsn decoded)
{
Decode(ref reader, Asn1Tag.Sequence, out decoded);
Decode(ref reader, Asn1Tag.Sequence, rebind, out decoded);
}

internal static void Decode(ref AsnValueReader reader, Asn1Tag expectedTag, out RSAPublicKeyAsn decoded)
internal static void Decode(ref AsnValueReader reader, Asn1Tag expectedTag, ReadOnlyMemory<byte> rebind, out RSAPublicKeyAsn decoded)
{
try
{
DecodeCore(ref reader, expectedTag, out decoded);
DecodeCore(ref reader, expectedTag, rebind, out decoded);
}
catch (AsnContentException e)
{
throw new CryptographicException(SR.Cryptography_Der_Invalid_Encoding, e);
}
}

private static void DecodeCore(ref AsnValueReader reader, Asn1Tag expectedTag, out RSAPublicKeyAsn decoded)
private static void DecodeCore(ref AsnValueReader reader, Asn1Tag expectedTag, ReadOnlyMemory<byte> rebind, out RSAPublicKeyAsn decoded)
{
decoded = default;
AsnValueReader sequenceReader = reader.ReadSequence(expectedTag);
ReadOnlySpan<byte> rebindSpan = rebind.Span;
int offset;
ReadOnlySpan<byte> tmpSpan;

decoded.Modulus = sequenceReader.ReadInteger();
decoded.PublicExponent = sequenceReader.ReadInteger();
tmpSpan = sequenceReader.ReadIntegerBytes();
decoded.Modulus = rebindSpan.Overlaps(tmpSpan, out offset) ? rebind.Slice(offset, tmpSpan.Length) : tmpSpan.ToArray();
tmpSpan = sequenceReader.ReadIntegerBytes();
decoded.PublicExponent = rebindSpan.Overlaps(tmpSpan, out offset) ? rebind.Slice(offset, tmpSpan.Length) : tmpSpan.ToArray();

sequenceReader.ThrowIfNotEmpty();
}
Expand Down
Loading
Loading