[英]How to create an pkcs7 block for key exchange only (bouncy castle)
我正在嘗試創建一個包含 pkcs 7 塊的文件。 在這個容器中,我需要我的公鑰和我的簽名者信息(沒有簽名數據..:)。 我已經嘗試了幾種替代方法,但沒有任何運氣。 這是我的代碼:
首先是簽名信息:
List<X509Certificate> certs = new List<X509Certificate> { cert };
IX509Store x509Certs = X509StoreFactory.Create(
"CERTIFICATE/COLLECTION",
new X509CollectionStoreParameters(certs));
var ias = new IssuerAndSerialNumber(cert.IssuerDN, cert.SerialNumber);
SignerIdentifier sid = new SignerIdentifier(ias);
AlgorithmIdentifier algoDigId = new AlgorithmIdentifierCmsSignedGenerator.DigestSha1);
AlgorithmIdentifier algoCryptId = new AlgorithmIdentifier(CmsSignedGenerator.EncryptionRsa);
SignerInfo si = new SignerInfo(sid, algoDigId, null, algoCryptId,
new DerOctetString(contentSignature), null);
contentSignature byte[] 包含一些信息的簽名摘要。
現在,當我嘗試創建 SignedData 時,一切都失敗了
var signedContent = new ContentInfo(CmsObjectIdentifiers.Data, DerNull.Instance);
CmsSignedData csd = new CmsSignedData(signedContent);
我不是要發送信息,這僅用於密鑰交換和驗證目的。 我相信這是一個有效的場景,但不知何故這不起作用。
謝謝你的幫助。
更新:
更多上下文。
我正在嘗試從 a.Net 可執行文件中簽署 JAR。 我已經完成了該過程的 rest,但 jarsigner 創建了一個 pkcs7 文件:
ContentInfo 設置為數據類型且沒有內容。 到目前為止,制作 new ContentInfo( CmsObjectIdentifiers.Data, null) 只是在將內容信息添加到 CmsData 時引發異常
添加了一個 SignerInfo,此 SignerInfo 包括先前從 JAR 內容派生的簽名。
由於這個問題與簽署 APK / JAR 文件特別相關,因此我將在這種情況下回答。
假如說:
您已執行以下所有設置步驟:
以下代碼將生成一個有效的分離 pkcs7 簽名,它實際上是您的 CERT.RSA 內容:
string OID_DATA = "1.2.840.113549.1.7.1";
// setup the data to sign
ContentInfo content = new ContentInfo( new Oid( OID_DATA ), manifestSFBytes );
SignedCms signedCms = new SignedCms( content, true );
CmsSigner signer = new CmsSigner( SubjectIdentifierType.IssuerAndSerialNumber, cert );
// create the signature
signedCms.ComputeSignature( signer );
byte[] data = signedCms.Encode();
此代碼依賴於 System.Security.Cryptography.Pkcs 命名空間,不需要 BouncyCastle。
這里發生的是原始內容(簽名文件二進制數據)通過 ComputeSignature() 調用在 go 中進行散列和簽名。
因此,不需要“null ContentInfo”技巧,即 ContentInfo 包含要簽名和散列的原始數據,這與 Java 實現不同,后者在 PKCS7 生成之前對內容進行簽名和散列。
HTH
-(e)
這是我認為您想要做的一個簡單示例。 注意:下面的代碼適用於 Java bouncycastle,但我認為這些類在庫的 C# 版本中非常相似。
import java.io.*;
import java.security.cert.*;
import java.util.ArrayList;
import java.util.List;
import org.bouncycastle.cert.jcajce.JcaCertStore;
import org.bouncycastle.cms.*;
public class PKCS7CertList1
{
public static byte[] buildCMSCertThingy() throws Exception
{
final List<X509Certificate> certs = new ArrayList<X509Certificate>();
final InputStream certIs = new FileInputStream("google_com.p7b");
final CertificateFactory cf = CertificateFactory.getInstance("X.509");
for (Certificate cert : cf.generateCertificates(certIs))
{
certs.add((X509Certificate) cert);
}
certIs.close();
System.err.printf("Number of certs parsed = %d%n", certs.size());
final CMSSignedDataGenerator cmsGen = new CMSSignedDataGenerator();
cmsGen.addCertificates(new JcaCertStore(certs));
final CMSSignedData sigData = cmsGen.generate(new CMSAbsentContent(), false);
return sigData.getEncoded();
}
public static void main(String[] args) throws Exception
{
FileOutputStream fos = new FileOutputStream("signed_data.der");
fos.write(buildCMSCertThingy());
fos.close();
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.