//---------------------------------------------------------------------------------- public static void SetPrivateKey(RSACryptoServiceProvider rsa, byte[] data) { using (MemoryStream ms = new MemoryStream(data)) { using (BinaryReader br = new BinaryReader(ms)) { int n = br.ReadUInt16(); if (n == 0x8130) { br.ReadByte(); } else if (n == 0x8230) { br.ReadInt16(); } else { throw new CryptographicException(OpenSSL.MSG_WRONG_OSSL_DATA); } if (br.ReadByte() != 0x02) { throw new CryptographicException(OpenSSL.MSG_WRONG_OSSL_DATA); } if (br.ReadUInt16() != 0x0001) { throw new CryptographicException(OpenSSL.MSG_WRONG_OSSL_DATA); } byte[] b = br.ReadBytes(15); if (!OpenSSL.CompareByteArray(b, OpenSSL._seqoid)) { throw new CryptographicException(OpenSSL.MSG_WRONG_OSSL_DATA); } if (br.ReadByte() != 0x04) { throw new CryptographicException(OpenSSL.MSG_WRONG_OSSL_DATA); } if ((n = br.ReadByte()) == 0x81) { br.ReadByte(); } else if (n == 0x82) { br.ReadUInt16(); } Array.Clear(b, 0, b.Length); b = br.ReadBytes((int)(ms.Length - ms.Position)); OpenSSL.SetRsaPrivateKey(rsa, b); Array.Clear(b, 0, b.Length); } } }
//---------------------------------------------------------------------------------- public static void SetEncryptedPrivateKey ( RSACryptoServiceProvider rsa , byte[] data , string password ) { byte[] OIDpkcs5PBES2 = { 0x06, 0x09, 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x05, 0x0D }; byte[] OIDpkcs5PBKDF2 = { 0x06, 0x09, 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x05, 0x0C }; byte[] OIDdesEDE3CBC = { 0x06, 0x08, 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x03, 0x07 }; using (MemoryStream ms = new MemoryStream(data)) { using (BinaryReader br = new BinaryReader(ms)) { int n = br.ReadUInt16(); if (n == 0x8130) { br.ReadByte(); } else if (n == 0x8230) { br.ReadInt16(); } else { throw new CryptographicException(OpenSSL.MSG_WRONG_OSSL_DATA); } if ((n = br.ReadUInt16()) == 0x8130) { br.ReadByte(); } else if (n == 0x8230) { br.ReadInt16(); } byte[] b = br.ReadBytes(11); if (!OpenSSL.CompareByteArray(b, OIDpkcs5PBES2)) { throw new CryptographicException(OpenSSL.MSG_WRONG_OSSL_DATA); } Array.Clear(b, 0, b.Length); if ((n = br.ReadUInt16()) == 0x8130) { br.ReadByte(); } else if (n == 0x8230) { br.ReadInt16(); } if ((n = br.ReadUInt16()) == 0x8130) { br.ReadByte(); } else if (n == 0x8230) { br.ReadInt16(); } b = br.ReadBytes(11); if (!OpenSSL.CompareByteArray(b, OIDpkcs5PBKDF2)) { throw new CryptographicException(OpenSSL.MSG_WRONG_OSSL_DATA); } Array.Clear(b, 0, b.Length); if ((n = br.ReadUInt16()) == 0x8130) { br.ReadByte(); } else if (n == 0x8230) { br.ReadInt16(); } if (br.ReadByte() != 0x04) { throw new CryptographicException(OpenSSL.MSG_WRONG_OSSL_DATA); } byte[] salt = br.ReadBytes(br.ReadByte()); if (br.ReadByte() != 0x02) { throw new CryptographicException(OpenSSL.MSG_WRONG_OSSL_DATA); } int iterations = 0; if ((n = br.ReadByte()) == 1) { iterations = br.ReadByte(); } else if (n == 2) { iterations = 256 * br.ReadByte() + br.ReadByte(); } else { throw new CryptographicException(OpenSSL.MSG_WRONG_OSSL_DATA); } if ((n = br.ReadUInt16()) == 0x8130) { br.ReadByte(); } else if (n == 0x8230) { br.ReadInt16(); } b = br.ReadBytes(10); if (!OpenSSL.CompareByteArray(b, OIDdesEDE3CBC)) { throw new CryptographicException(OpenSSL.MSG_WRONG_OSSL_DATA); } Array.Clear(b, 0, b.Length); if (br.ReadByte() != 0x04) { throw new CryptographicException(OpenSSL.MSG_WRONG_OSSL_DATA); } byte[] iv = br.ReadBytes(br.ReadByte()); if (br.ReadByte() != 0x04) { throw new CryptographicException(OpenSSL.MSG_WRONG_OSSL_DATA); } if ((n = br.ReadByte()) == 0x81) { n = br.ReadByte(); } else if (n == 0x82) { n = 256 * br.ReadByte() + br.ReadByte(); } data = br.ReadBytes(n); Rfc2898DeriveBytes db = new Rfc2898DeriveBytes(password, salt, iterations); byte[] bk = db.GetBytes(24); using (TripleDES td = TripleDES.Create()) b = OpenSSL.Decrypt(td, bk, iv, data); OpenSSL.SetPrivateKey(rsa, b); db.Reset(); Array.Clear(b, 0, b.Length); Array.Clear(data, 0, data.Length); Array.Clear(salt, 0, salt.Length); Array.Clear(iv, 0, iv.Length); Array.Clear(bk, 0, bk.Length); } } }
//---------------------------------------------------------------------------------- public static void SetPublicKey(RSACryptoServiceProvider rsa, byte[] data) { byte[] b; using (MemoryStream ms = new MemoryStream(data)) { using (BinaryReader br = new BinaryReader(ms)) { int n = br.ReadUInt16(); if (n == 0x8130) { br.ReadByte(); } else if (n == 0x8230) { br.ReadInt16(); } else { throw new CryptographicException(OpenSSL.MSG_WRONG_OSSL_DATA); } b = br.ReadBytes(15); if (!OpenSSL.CompareByteArray(b, OpenSSL._seqoid)) { throw new CryptographicException(OpenSSL.MSG_WRONG_OSSL_DATA); } if ((n = br.ReadUInt16()) == 0x8103) { br.ReadByte(); } else if (n == 0x8203) { br.ReadInt16(); } else { throw new CryptographicException(OpenSSL.MSG_WRONG_OSSL_DATA); } if (br.ReadByte() != 0x00) { throw new CryptographicException(OpenSSL.MSG_WRONG_OSSL_DATA); } if ((n = br.ReadUInt16()) == 0x8130) { br.ReadByte(); } else if (n == 0x8230) { br.ReadInt16(); } else { throw new CryptographicException(OpenSSL.MSG_WRONG_OSSL_DATA); } byte lb = 0; byte hb = 0; if ((n = br.ReadUInt16()) == 0x8102) { lb = br.ReadByte(); } else if (n == 0x8202) { hb = br.ReadByte(); lb = br.ReadByte(); } else { throw new CryptographicException(OpenSSL.MSG_WRONG_OSSL_DATA); } byte[] mod = { lb, hb, 0x00, 0x00 }; n = BitConverter.ToInt32(mod, 0); lb = br.ReadByte(); br.BaseStream.Seek(-1, SeekOrigin.Current); if (lb == 0x00) { br.ReadByte(); --n; } b = br.ReadBytes(n); if (br.ReadByte() != 0x02) { throw new CryptographicException(OpenSSL.MSG_WRONG_OSSL_DATA); } RSAParameters p = new RSAParameters(); p.Modulus = b; p.Exponent = br.ReadBytes((int)br.ReadByte()); rsa.ImportParameters(p); } } }