2017-04-13 45 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

相關問題