【问题标题】:How to create an pkcs7 block for key exchange only (bouncy castle)如何创建仅用于密钥交换的 pkcs7 块(充气城堡)
【发布时间】:2011-07-03 05:11:56
【问题描述】:

我正在尝试创建一个包含 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);

我不想发送信息,这仅用于密钥交换和验证目的。我相信这是一个有效的场景,但不知何故这不起作用。

感谢您的帮助。

更新:

更多上下文。

我正在尝试从 .Net 可执行文件中签署 JAR。我已经完成了剩下的过程,但 jarsigner 创建了一个 pkcs7 文件:

  • ContentInfo 设置为数据类型且无内容。到目前为止,制作 new ContentInfo(CmsObjectIdentifiers.Data, null) 只是在将内容信息添加到 CmsData 时引发异常

  • 添加了一个 SignerInfo,此 SignerInfo 包含先前从 JAR 内容派生的签名。

【问题讨论】:

  • 请在语句“...没有任何运气”中添加详细信息。
  • 请看我下面的评论(我不小心把它作为答案,对不起)
  • @GregS 我已经为这个问题添加了更多信息。顺便说一句,我还没有在 .Net 端口中看到 CMSAbsentContent 类。我去看看java版本看看是什么。

标签: c# rsa bouncycastle pkcs#7


【解决方案1】:

由于这个问题与签署 APK / JAR 文件特别相关,我将在这种情况下回答。

假设:

您已执行以下所有设置步骤:

  1. 生成了一个有效的 MANIFEST.MF
  2. 生成了有效的 CERT.SF
  3. 将有效的 PFX 文件加载到名为“cert”的 X509Certificate2 变量中
  4. 将 CERT.SF 文件的二进制内容保存在名为“manifestSFBytes”的字节数组中

以下代码将生成一个有效的分离 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() 调用一次性进行散列和签名。

因此不需要“null ContentInfo”技巧,即 ContentInfo 包含要签名和散列的原始数据,这与在 PKCS7 生成之前对内容进行签名和散列的 Java 实现不同。

HTH

-(e)

【讨论】:

    【解决方案2】:

    这是我认为您想要做的一个简单示例。注意:下面的代码适用于 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();
        }
    
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-01-15
      • 2015-06-29
      • 2013-04-20
      • 1970-01-01
      • 2013-05-25
      • 2015-03-30
      • 2014-09-19
      • 1970-01-01
      相关资源
      最近更新 更多