internal RSAParameters ParseRSAPublicKey()
{
var parameters = new RSAParameters ();
// Current value
byte[] value = null;
// Sanity Check
int length = 0;
// Checkpoint
int position = parser.CurrentPosition ();
// Ignore Sequence - PublicKeyInfo
length = parser.NextSequence ();
if (length != parser.RemainingBytes ())
{
var sb = new StringBuilder("Incorrect Sequence Size. ");
sb.AppendFormat("Specified: {0}, Remaining: {1}",
length.ToString(CultureInfo.InvariantCulture),
parser.RemainingBytes ().ToString(CultureInfo.InvariantCulture));
throw new BerDecodeException(sb.ToString (), position);
}
// Checkpoint
position = parser.CurrentPosition ();
// Ignore Sequence - AlgorithmIdentifier
length = parser.NextSequence ();
if (length > parser.RemainingBytes ())
{
var sb = new StringBuilder("Incorrect AlgorithmIdentifier Size. ");
sb.AppendFormat("Specified: {0}, Remaining: {1}",
length.ToString(CultureInfo.InvariantCulture),
parser.RemainingBytes ().ToString(CultureInfo.InvariantCulture));
throw new BerDecodeException(sb.ToString (), position);
}
// Checkpoint
position = parser.CurrentPosition ();
// Grab the OID
value = parser.NextOID ();
byte[] oid = {0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x01};
if (!EqualOid(value, oid))
{
throw new BerDecodeException("Expected OID 1.2.840.113549.1.1.1", position);
}
// Optional Parameters
if (parser.IsNextNull ())
{
parser.NextNull ();
// Also OK: value = parser.Next();
}
else
{
// Gracefully skip the optional data
value = parser.Next ();
}
// Checkpoint
position = parser.CurrentPosition ();
// Ignore BitString - PublicKey
length = parser.NextBitString ();
if (length > parser.RemainingBytes ())
{
var sb = new StringBuilder("Incorrect PublicKey Size. ");
sb.AppendFormat("Specified: {0}, Remaining: {1}",
length.ToString(CultureInfo.InvariantCulture),
(parser.RemainingBytes ()).ToString(CultureInfo.InvariantCulture));
throw new BerDecodeException(sb.ToString (), position);
}
// Checkpoint
position = parser.CurrentPosition ();
// Ignore Sequence - RSAPublicKey
length = parser.NextSequence ();
if (length < parser.RemainingBytes ())
{
var sb = new StringBuilder("Incorrect RSAPublicKey Size. ");
sb.AppendFormat("Specified: {0}, Remaining: {1}",
length.ToString(CultureInfo.InvariantCulture),
parser.RemainingBytes ().ToString(CultureInfo.InvariantCulture));
throw new BerDecodeException(sb.ToString (), position);
}
parameters.Modulus = TrimLeadingZero(parser.NextInteger ());
parameters.Exponent = TrimLeadingZero(parser.NextInteger ());
Debug.Assert(0 == parser.RemainingBytes ());
return parameters;
}