封装一个OCSPRequest结构体请求(signedReq)

按照OCSPRequest的请求语法,参照OCSP在线证书状态协议封装一个不含签名的请求signedReq串。

代码:

package com.xdja.asn1;

import org.bouncycastle.asn1.*;
import org.bouncycastle.asn1.ocsp.CertID;
import org.bouncycastle.asn1.ocsp.OCSPRequest;
import org.bouncycastle.asn1.ocsp.Request;
import org.bouncycastle.asn1.ocsp.TBSRequest;
import org.bouncycastle.asn1.oiw.OIWObjectIdentifiers;
import org.bouncycastle.asn1.x500.X500Name;
import org.bouncycastle.asn1.x509.AlgorithmIdentifier;
import org.bouncycastle.asn1.x509.Extensions;
import org.bouncycastle.asn1.x509.GeneralName;

import java.io.IOException;
import java.io.UnsupportedEncodingException;

public class OcspRequestTest {
    /*
    * 封装OCSPRequest请求,只含TBSRequest结构,OptionalSignature = null
    * 以base64输出
    * */
    public static void OcspRequestTest() {

        //serialNumber
        ASN1Integer serialNumber = new ASN1Integer(1);
        //algorithmIdentifier
        //OID
        ASN1ObjectIdentifier algorithm = OIWObjectIdentifiers.idSHA1;
        AlgorithmIdentifier algorithmIdentifier = new AlgorithmIdentifier(algorithm, null);
        //issuerKeyHash
        String issuerKey = "admin123456";
        DEROctetString issuerKeyHash = new DEROctetString(issuerKey.getBytes());
        //issuerNameHash
        String issuerName = "zhangsan";
        DEROctetString issuerNameHash = new DEROctetString(issuerName.getBytes());
        // CertID
        CertID reqCert = new CertID(algorithmIdentifier, issuerNameHash, issuerKeyHash, serialNumber);
        Extensions singleRequestExtensions = null;
        ASN1EncodableVector v = new ASN1EncodableVector();
        v.add(reqCert);
        if (singleRequestExtensions != null) {
            v.add(new DERTaggedObject(true, 0, singleRequestExtensions));
        }
        //Request
        DERSequence request = new DERSequence(v);
        Request re = Request.getInstance(request);
        //requestList
        ASN1EncodableVector v1 = new ASN1EncodableVector();
        v1.add(re);
        DERSequence requestList = new DERSequence(v1);
        //tbsRequest
        GeneralName requestorName = new GeneralName(new X500Name("CN=hello world"));
        Extensions requestExtensions = null;
        /*boolean versionSet = true;*/
        TBSRequest tbsRequest = new TBSRequest(requestorName,requestList,requestExtensions);
        // ocspRequest
        OCSPRequest ocspRequest = new OCSPRequest(tbsRequest, null);
        //byte[]
        byte[] encode = null;
        try {
            encode = ocspRequest.toASN1Primitive().getEncoded();
        } catch (IOException e) {
            e.printStackTrace();
        }
        //base64
        byte[] b64 = org.bouncycastle.util.encoders.Base64.encode(encode);
        //String
        String res = null;
        try {
            res = new String(b64, "UTF-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        System.out.println(res);
    }

    public static void main(String[] args) {
        OcspRequestTest();
    }
}

运行结果:

封装一个OCSPRequest结构体请求(signedReq)_第1张图片

 

 

你可能感兴趣的:(Java,ASN.1)