Как загрузить открытый ключ RSA, сгенерированный openSSL, в RSACryptoServiceProvider?
Я пишу.Net Class, который читает куки с нашего центрального сервера аутентификации. Он содержит идентификатор пользователя, некоторые временные метки и подпись, созданную openssl_sign() с использованием 2048-битного ключа RSA и хэша SHA1.
Текущий открытый ключ предоставляется в формате openssl PEM на сервере и иногда изменяется. Я не могу прочитать Ключ, используя только управляемый код.Net, и разработал следующую процедуру, чтобы заставить его работать:
- Извлечь экспоненту и модуль из открытого ключа
- Проверьте ключ все еще 2048 бит
- Сохранение длины ключа, экспонента модуля в Source, компиляция и развертывание (удаление начальных нулей из модуля, чтобы заставить его работать)
Затем класс создает new RSACryptoServiceProvider(2048)
и кормит общедоступные компоненты, используя RSAParameters
структура в CSP. Проверка подписи тогда успешна.
Мне бы хотелось, чтобы это работало без создания, компиляции и развертывания новой сборки при каждом изменении ключа. Чтобы сделать вещи интересными, я хотел бы придерживаться только управляемого кода (исключая большинство примеров, которые я нашел). Что-то, что звучит идеально, это встроенный ASN.1 Reader при создании экземпляра AsnEncodedData(oid, data)
но единственный найденный мной oid, который мог бы соответствовать, RSA aka 1.2.840.113549.1.1.1, не работал и производил только необработанные байты.
Добавлено: бывший открытый ключ
----- НАЧАТЬ ПУБЛИЧНЫЙ КЛЮЧ -----
MFwwDQYJKoZIhvcNAQEBBQADSwAwSAJBAMW90O6C17fapbS35auKolsy7kI0FOE1
C08y5HqgZ0rMXoocV4nHSHYBm2HVx2QSR5OLQtERgWDmxOu + vwU1GXUCAwEAAQ ==
----- КОНЕЦ ОБЩЕСТВЕННОГО КЛЮЧА -----
Я нашел pempublic.cs, который, кажется, решает эту проблему, используя (как кажется) исходный код из openSSL. Я оставлю вопрос открытым, чтобы посмотреть, есть ли другие решения.
2 ответа
Чтобы загрузить закрытый ключ, давайте используем этот код:
private RSACryptoServiceProvider GetPrivateKey(string privateKey)
{
byte[] privkey = Convert.FromBase64String(privateKey);
byte[] MODULUS, E, D, P, Q, DP, DQ, IQ;
// --------- Set up stream to decode the asn.1 encoded RSA private key ------
MemoryStream mem = new MemoryStream(privkey);
BinaryReader binr = new BinaryReader(mem); //wrap Memory Stream with BinaryReader for easy reading
byte bt = 0;
ushort twobytes = 0;
int elems = 0;
try
{
twobytes = binr.ReadUInt16();
if (twobytes == 0x8130) //data read as little endian order (actual data order for Sequence is 30 81)
binr.ReadByte(); //advance 1 byte
else if (twobytes == 0x8230)
binr.ReadInt16(); //advance 2 bytes
else
return null;
twobytes = binr.ReadUInt16();
if (twobytes != 0x0102) //version number
return null;
bt = binr.ReadByte();
if (bt != 0x00)
return null;
//------ all private key components are Integer sequences ----
elems = GetIntegerSize(binr);
MODULUS = binr.ReadBytes(elems);
elems = GetIntegerSize(binr);
E = binr.ReadBytes(elems);
elems = GetIntegerSize(binr);
D = binr.ReadBytes(elems);
elems = GetIntegerSize(binr);
P = binr.ReadBytes(elems);
elems = GetIntegerSize(binr);
Q = binr.ReadBytes(elems);
elems = GetIntegerSize(binr);
DP = binr.ReadBytes(elems);
elems = GetIntegerSize(binr);
DQ = binr.ReadBytes(elems);
elems = GetIntegerSize(binr);
IQ = binr.ReadBytes(elems);
// ------- create RSACryptoServiceProvider instance and initialize with public key -----
CspParameters CspParameters = new CspParameters();
CspParameters.Flags = CspProviderFlags.UseMachineKeyStore;
RSACryptoServiceProvider RSA = new RSACryptoServiceProvider(1024, CspParameters);
RSAParameters RSAparams = new RSAParameters();
RSAparams.Modulus = MODULUS;
RSAparams.Exponent = E;
RSAparams.D = D;
RSAparams.P = P;
RSAparams.Q = Q;
RSAparams.DP = DP;
RSAparams.DQ = DQ;
RSAparams.InverseQ = IQ;
RSA.ImportParameters(RSAparams);
return RSA;
}
catch (Exception ex)
{
return null;
}
finally
{
binr.Close();
}
}
Чтобы загрузить открытый ключ, давайте используем этот код:
private RSACryptoServiceProvider GetPublicKey(string publicKeyString)
{
// encoded OID sequence for PKCS #1 rsaEncryption szOID_RSA_RSA = "1.2.840.113549.1.1.1"
byte[] SeqOID = {0x30, 0x0D, 0x06, 0x09, 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x01, 0x01, 0x05, 0x00} ;
byte[] x509key;
byte[] seq = new byte[15];
int x509size;
x509key = Convert.FromBase64String(publicKeyString);
x509size = x509key.Length;
// --------- Set up stream to read the asn.1 encoded SubjectPublicKeyInfo blob ------
MemoryStream mem = new MemoryStream(x509key);
BinaryReader binr = new BinaryReader(mem); //wrap Memory Stream with BinaryReader for easy reading
byte bt = 0;
ushort twobytes = 0;
try
{
twobytes = binr.ReadUInt16();
if (twobytes == 0x8130) //data read as little endian order (actual data order for Sequence is 30 81)
binr.ReadByte(); //advance 1 byte
else if (twobytes == 0x8230)
binr.ReadInt16(); //advance 2 bytes
else
return null;
seq = binr.ReadBytes(15); //read the Sequence OID
if (!CompareBytearrays(seq, SeqOID)) //make sure Sequence for OID is correct
return null;
twobytes = binr.ReadUInt16();
if (twobytes == 0x8103) //data read as little endian order (actual data order for Bit String is 03 81)
binr.ReadByte(); //advance 1 byte
else if (twobytes == 0x8203)
binr.ReadInt16(); //advance 2 bytes
else
return null;
bt = binr.ReadByte();
if (bt != 0x00) //expect null byte next
return null;
twobytes = binr.ReadUInt16();
if (twobytes == 0x8130) //data read as little endian order (actual data order for Sequence is 30 81)
binr.ReadByte(); //advance 1 byte
else if (twobytes == 0x8230)
binr.ReadInt16(); //advance 2 bytes
else
return null;
twobytes = binr.ReadUInt16();
byte lowbyte = 0x00;
byte highbyte = 0x00;
if (twobytes == 0x8102) //data read as little endian order (actual data order for Integer is 02 81)
lowbyte = binr.ReadByte(); // read next bytes which is bytes in modulus
else if (twobytes == 0x8202)
{
highbyte = binr.ReadByte(); //advance 2 bytes
lowbyte = binr.ReadByte();
}
else
return null;
byte[] modint = { lowbyte, highbyte, 0x00, 0x00 }; //reverse byte order since asn.1 key uses big endian order
int modsize = BitConverter.ToInt32(modint, 0);
int firstbyte = binr.PeekChar();
if (firstbyte == 0x00)
{ //if first byte (highest order) of modulus is zero, don't include it
binr.ReadByte(); //skip this null byte
modsize -= 1; //reduce modulus buffer size by 1
}
byte[] modulus = binr.ReadBytes(modsize); //read the modulus bytes
if (binr.ReadByte() != 0x02) //expect an Integer for the exponent data
return null;
int expbytes = (int)binr.ReadByte(); // should only need one byte for actual exponent data (for all useful values)
byte[] exponent = binr.ReadBytes(expbytes);
// ------- create RSACryptoServiceProvider instance and initialize with public key -----
RSACryptoServiceProvider RSA = new RSACryptoServiceProvider();
RSAParameters RSAKeyInfo = new RSAParameters();
RSAKeyInfo.Modulus = modulus;
RSAKeyInfo.Exponent = exponent;
RSA.ImportParameters(RSAKeyInfo);
return RSA;
}
finally
{
binr.Close();
}
}
Вы можете прочитать оригинальный пост здесь, чтобы получить более подробную информацию
У вас есть закрытый ключ, поэтому, вероятно, проще создать самозаверяющий сертификат из ключа. Ты можешь использовать System.Security.Cryptography.X509Certificates.X509Certificate
(Я думаю, у вас должен быть сертификат в формате DER / ASN.1, а не PEM), чтобы загрузить сертификат и X509Certificate.GetPublicKey()
получить открытый ключ.