2017-04-13 46 views
3

我已经为此苦苦挣扎了好几天,RFC 2315有点难以理解。EnvelopedCms解密不适用于Azure密钥保管库

我想实现我自己的EnvelopedCms.Decrypt()版本,让我能以正确的方式使用Azure的主要跳马的证书操作UnwrapKey和/或Decrypt一个PKCS#7消息(CMS对象)。我在.Net中使用EnevelopedCms给Decode留言,然后我尝试DecryptEnvelopedCms.ContentInfo.Content

这是我试图做的;

public static async Task<byte[]> DecryptCustom(string certificateId, string encryptedBase64Content) 
{ 
    var bytes = Convert.FromBase64String(encryptedBase64Content); 
    var contentInfo = new ContentInfo(bytes); 
    var envelopedCms = new EnvelopedCms(contentInfo); 
    envelopedCms.Decode(bytes); 
    // envelopedCms.Decrypt() <-- no go. Can't extract certificate from Key Vault 

    // My (naive) attempt to decrypt CMS content using Azure Key Vault certificates 
    byte[] decryptedContent; 
    using (var client = new KeyVaultClient(GetKeyVaultToken)) 
    { 
     var decryptionresult = await client.DecryptAsync(GetKeyUrl(certificateId), "RSA1_5", envelopedCms.ContentInfo.Content); 
     decryptedContent = decryptionresult.Result; 
    } 
    return decryptedContent; 
} 

我当时希望可以那么容易,但是它给了我下面的错误;

无法用此密钥解密指定的值。

我在RFC 2315中读了一些关于八位字节的内容,所以在我解密之前,流(字节数组)可能需要重新排序。我是否需要打开一些对称密钥才能解密真正的有效负载?我在这里薄冰。

我不是密码专业人员,所以我可能错过了一些明显的东西。我希望有人知道在这种情况下该怎么做,因为我真的想让我的证书保存在密钥保管库(HSM)中

回答

3

CMS信封内容使用会话密钥进行加密,并且每个收件人(此处可以很多)公钥之前传输。

您需要的是提取收件人的加密会话密钥,并使用存储在密钥保管库中的私钥解开它。我不是附近的Visual Studio的权利,但这里是伪代码:

// Extract the first (and often only) receiver's encrypted session key 
var key = envelopedCms.Receivers[0].EncryptionKey; 
// Unwrap the sessionKey using the receiver's private key stored in key vault: 
var sessionKey = (await keyVaultClient.Unwrap(uri, "certificatename", key)).Result; 

最后,使用sessionKey,您可以解密信封内容(ContentInfo.Content)。加密类型在信封的加密算法属性中指定。

+0

谢谢!这帮助我解决了这个问题。我现在可以使用Azure密钥保管库中的专用证书来解密CMS对象! – Frode

相关问题