zoukankan      html  css  js  c++  java
  • 申请X509证书以及PKCS#10 详解

    一、证书颁发

    1.单证书的签发

    1. 用户填写信息注册(或者由RA的业务操作员注册用户)。
    2. 用户信息传递到RA。
    3. RA审核通过。
    4. 用户请求发证。
    5. RA审核通过。
    6. 用户签发证书请求。
    7. RA把用户信息传递到CA。
    8. CA到KMC中取密钥对,(密钥对由加密机生成,生成的密钥对)。
    9. CA把用户信息和从KMC中取到的公钥制作成证书。
    10. CA用自己的私钥给用户证书签名。
    11. CA把自己的用户证书和用户的私钥通过SSL通路传递给RA。
    12. 用户从RA下载证书。
    13. 用户安装证书。

    2.双证书的签发

    签名证书的签发

    • a) 用户填写信息注册(或者由RA的业务操作员注册用户)。
    • b) 用户本地ACTIVE控件调用IE中的加密机生成签名证书的密钥对。
    • c) 用户填写的信息和签名证书的公钥传递给RA。
    • d) RA把用户信息和公钥传递给CA。
    • e) CA根据用户信息和公钥制作成证书
    • f) CA用自己的私钥给用户证书签名。
    • g) CA把生成的用户证书传递给RA。
    • h) 用户从RA下载证书。
    • i)用户安装签名证书。

    加密证书的签发

    • a) 用户把用户的签名证书传递到RA。
    • b) RA用户的签名证书传递到CA。
    • c) CA到KMC中取密钥对,(密钥对由加密机生成,生成的密钥对)。
    • d) CA把从签名证书中得到的用户信息和从KMC中取到的公钥制作成证书。
    • e) CA用自己的私钥给用户证书签名。
    • f) CA调用签名证书的公钥给加密证书和用户加密证书的私钥加密
    • g) CA把加密之后的加密证书和加密证书的私钥传递给RA。
    • h) 用户从RA加密之后的加密证书和加密证书的私钥。
    • i) 用户在本地调用签名证书的私钥解密加密证书和加密证书的私钥。
    • j) 用户安装加密证书。

    二、具体实现

    2.1 用户首先产生自己的密钥对,并将公共密钥及部分个人信息传送给CA(通过P10请求

     CertAndKeyGen gen = new CertAndKeyGen("RSA", "SHA1WithRSA");
      gen.generate(1024);//生成1024位密钥
     PKCS10CertificationRequestBuilder p10Builder = new JcaPKCS10CertificationRequestBuilder(
                         new X500Principal("CN = " + name), gen.getPublicKey());// CN和公钥 
      JcaContentSignerBuilder csBuilder = new JcaContentSignerBuilder("SHA256withRSA");// 签名算法
      ContentSigner signer = null;
      signer = csBuilder.build(gen.getPrivateKey());
      PKCS10CertificationRequest csr = p10Builder.build(signer);// PKCS10的请求
      return csr;//返回PKCS10的请求
    

    2.2 CA接受请求并生成证书

    CertificateFactory certFactory = CertificateFactory.getInstance("X.509");
    X509Certificate cacert = (X509Certificate) certFactory.generateCertificate(new  FileInputStream(certPath));
    //一大堆参数 ,填充到生成器里
    AlgorithmIdentifier sigAlgId = new 	   DefaultSignatureAlgorithmIdentifierFinder().find("SHA1withRSA");
    AlgorithmIdentifier digAlgId = new DefaultDigestAlgorithmIdentifierFinder().find(sigAlgId);
    org.bouncycastle.asn1.x500.X500Name issuer = new org.bouncycastle.asn1.x500.X500Name(
                                              cacert.getSubjectX500Principal().getName());        BigInteger serial = new BigInteger(32, new SecureRandom());
      Date from = new Date();
      Date to = new Date(System.currentTimeMillis() + (365 * 80 * 86400000L));
    
      X509v3CertificateBuilder certgen = new X509v3CertificateBuilder(issuer, serial, from, to,                                                                    csr.getSubject(),
    csr.getSubjectPublicKeyInfo())
    Key privateKey = productPrivateKey();
    // CA端进行签名, 才有具有法律效力
    ContentSigner signer = new BcRSAContentSignerBuilder(sigAlgId, digAlgId)
                   .build(PrivateKeyFactory.createKey(privateKey.getEncoded()));
    // 生成BC结构的证书
     Security.addProvider(new BouncyCastleProvider());
    X509Certificate resultBc = new JcaX509CertificateConverter().setProvider("BC").getCertificate(certgen.build(signer));
      return resultBc;//返回证书
    

    三、P10请求详解

    • 定义:证书请求文件,类似于CSR文件。
    • p10证书一般是一个base64文件,实际上他不是一张真正的证书应该是一段可以向CA申请证书的P10请求,该请求一般是通过硬件生成密钥对后,将私钥单独存放,但是将公钥放入p10中,CA受到该p10请求后,可以校验,并根据p10中的信息制作一张没有私钥的公钥证书。

    CSR:证书签发请求(Certificate Signing Request),

    • CSR也叫做认证申请,是一个发送到CA的请求认证信息。 有两种格式,应用最广泛的是由PKCS#10定义的,另一个用的少的是由SPKAC定义的,主要应用于网景浏览器。

    • 在PKCS#10定义中,CSR有两种编码格式:二进制(ASN.1或DER (Distinguished Encoding Rules))和文本格式(the text or PEM (Privacy Enhanced Mail)formatted CSR is the binary CSR after it has been Base-64 encoded to create a text version of the CSR.)

    • CSR文件生成步骤如下:

      • 根据Version、Distinguished Name、Public Key、Attributes生成请求证书;
      • 用Private Key加密证书请求信息;
      • 根据请求信息、签名算法和签名生成CSR文件;
    • CSR文件包含的信息描述如下

           CertificationRequest ::= SEQUENCE {
           certificationRequestInfo CertificationRequestInfo,//证书信息
           signatureAlgorithm AlgorithmIdentifier{{ SignatureAlgorithms }},//签名算法
           signature          BIT STRING //签名
         }//另外还可能有可选的字段,如postal address和Email address,这两个字段可以应用于证书的撤销。  
      

    注意:私钥不包含在CSR文件中,但是应用于数字签名

    签名分两步

    • 将certificateRequestInfo 进行DER编码,产生八位字节字符串;

    • 将步骤一的结果用请求者的私钥在指定的签名算法下,产生签名;

    • 请求信息定义如下

      CertificationRequestInfo ::= SEQUENCE {       
         version       INTEGER { v1(0) } (v1,...),
          subject       Name, //证书主体的专有名称
          subjectPKInfo SubjectPublicKeyInfo{{ PKInfoAlgorithms }},
          attributes    [0] Attributes{{ CRIAttributes }}
      }
      
    • subjectPublicKeyInfo 包含被认证的公钥

    • attributes 关于认证主提其他信息属性集合

       SubjectPublicKeyInfo { ALGORITHM : IOSet} ::= SEQUENCE {
          algorithm        AlgorithmIdentifier {{IOSet}},
          subjectPublicKey BIT STRING
      }
      
      PKInfoAlgorithms ALGORITHM ::= {
          ...  -- add any locally defined algorithms here -- }
      
      Attributes { ATTRIBUTE:IOSet } ::= SET OF Attribute{{ IOSet }}
      
      CRIAttributes  ATTRIBUTE  ::= {
          ... -- add any locally defined attributes here -- }
      
      Attribute { ATTRIBUTE:IOSet } ::= SEQUENCE {
          type   ATTRIBUTE.&id({IOSet}),
          values SET SIZE(1..MAX) OF ATTRIBUTE.&Type({IOSet}{@type})
      }
      
    • 等价写法

      CertificationRequest ::= SIGNED { EncodedCertificationRequestInfo }
          (CONSTRAINED BY { -- Verify or sign encoded
           -- CertificationRequestInfo -- })
      
        EncodedCertificationRequestInfo ::=
            TYPE-IDENTIFIER.&Type(CertificationRequestInfo)
      
       SIGNED { ToBeSigned } ::= SEQUENCE {
          toBeSigned ToBeSigned,
          algorithm  AlgorithmIdentifier { {SignatureAlgorithms} },
           signature  BIT STRING
        }
      
  • 相关阅读:
    HDOJ2036 改革春风吹满地
    NYOJ3 多边形重心问题
    HDOJ1085Holding BinLaden Captive!(母函数做法)
    SQL Server2005杂谈(5):将聚合记录集逆时针和顺时针旋转90度(行列互换)
    使用wxWidgets进行跨平台的C++开发
    SQL Server2005杂谈(2):公用表表达式(CTE)的递归调用
    HeadFirst设计模式学习笔记(C#版):鸭子与策略(Strategy)模式
    选择创业项目的基础——适合自己的才是最好的
    博客也是网络赚钱的有利工具
    SQL Server2005杂谈(1):使用公用表表达式(CTE)简化嵌套SQL
  • 原文地址:https://www.cnblogs.com/NathanYang/p/10156731.html
Copyright © 2011-2022 走看看