DotNetOpenAuth OAuth 2 库需要 RSAParameters 来访问公钥和私钥(DotNetOpenAuth OAuth 2 - UriStyleMessageFormatter 中的示例,它使用 RSAParameters 构造 RSACryptoServiceProvider)。
我遇到了一份Azure 安全白皮书,其中指出 Azure 将证书安装在“证书存储区,其中带有一个标志,表明私钥可以使用但不能导出”。我相信这可能是这个问题的核心。
虽然我已经能够在本地开发和调试时通过它的指纹(下面的示例)引用证书来从证书中提取公钥和私钥,但我没有运气在 Azure 中运行相同的代码。
以下代码给出了错误:Azure 中的“Key not valid for use in specified state”
public class Global : System.Web.HttpApplication, IContainerAccessor
{
private static string thumbPrint = "<<my certificate thumbprint>>";
public static readonly RSAParameters AuthorizationServerSigningPublicKey = OAuthUtil.GetPublicKey(thumbPrint);
internal static readonly RSAParameters ResourceServerEncryptionPrivateKey = OAuthUtil.GetPrivateKey(thumbPrint);
//....... unnecessary code omitted ..... //
public static class OAuthUtil
{
public static RSAParameters GetPublicKey(string thumbPrint)
{
X509Store store = new X509Store(StoreName.My, StoreLocation.LocalMachine);
store.Open(OpenFlags.OpenExistingOnly | OpenFlags.ReadOnly);
var cert = store.Certificates.Find(X509FindType.FindByThumbprint, thumbPrint, true)[0];
var rsaParams = ((RSACryptoServiceProvider) cert.PublicKey.Key).ExportParameters(false);
return rsaParams;
}
public static RSAParameters GetPrivateKey(string thumbPrint)
{
X509Store store = new X509Store(StoreName.My, StoreLocation.LocalMachine);
store.Open(OpenFlags.OpenExistingOnly | OpenFlags.ReadOnly);
var cert = store.Certificates.Find(X509FindType.FindByThumbprint, thumbPrint, true)[0];
var rsaParams = ((RSACryptoServiceProvider) cert.PrivateKey).ExportParameters(true);
return rsaParams;
}
}
Azure 中的加密/解密代码基于相同的证书(下面的示例),不需要导出密钥可以正常工作:
public class Certificate
{
public string FriendlyName { get; set; }
public string IssuedBy { get; set; }
public string IssuedTo { get; set; }
public string ExpirationDate { get; set; }
public string PublicKey { get; set; }
public string PrivateKey { get; set; }
}
public ActionResult Keys()
{
X509Certificate2Collection selectedCerts = new X509Certificate2Collection();
var certList = new List<Certificate>();
X509Store store = new X509Store(StoreName.My, StoreLocation.LocalMachine);
try
{
store.Open(OpenFlags.OpenExistingOnly | OpenFlags.ReadWrite);
foreach (X509Certificate2 cert in store.Certificates)
{
// Encrypt string "hello world"
CspParameters CSPParam = new CspParameters();
CSPParam.Flags = CspProviderFlags.UseMachineKeyStore;
string PlainString = "hello world";
byte[] cipherbytes = ASCIIEncoding.ASCII.GetBytes(PlainString);
RSACryptoServiceProvider rsa = (RSACryptoServiceProvider)cert.PublicKey.Key;
byte[] cipher = rsa.Encrypt(cipherbytes, false);
var encryptedString = Convert.ToBase64String(cipher);
var cert2 = cert;
string decryptedString = "verify = " + cert2.Verify() ;
if (cert2.HasPrivateKey && cert2.Verify())
{
// Decrypt encrypted string..
RSACryptoServiceProvider rsaDecrypt = (RSACryptoServiceProvider)cert2.PrivateKey;
byte[] cipherbytes2 = Convert.FromBase64String(encryptedString);
byte[] plainbytes = rsaDecrypt.Decrypt(cipherbytes2, false);
System.Text.ASCIIEncoding enc = new System.Text.ASCIIEncoding();
decryptedString = enc.GetString(plainbytes);
}
var certItem = new Certificate
{
FriendlyName = cert.FriendlyName,
IssuedBy = cert.Issuer,
IssuedTo = cert.SubjectName.Name,
ExpirationDate = cert.NotAfter.ToString("d"),
PublicKey =
"Public Key: " + cert.GetPublicKeyString() + "<br/>Encrypted String: " + encryptedString + "<br/>Decrypted String: " +
decryptedString,
PrivateKey =
"cert has private key?: " + cert.HasPrivateKey + "<br/> key algo:" +
cert.GetKeyAlgorithm()
};
certList.Add(certItem);
}
}
finally
{
store.Close();
}
return View(certList);
}
除了重写 OAuth 2 库以使用 RSACryptoServiceProvider 引用而不是 RSAParameters 之外,还有什么方法可以让它在 Azure 中工作?
从商店中读取证书时,是否还有其他人遇到与 DotNetOpenAuth OAuth 2 和 Azure 相同的问题?
我想避免黑客攻击,例如使用启动任务安装具有导出权限的证书(出于安全考虑)。