4

我正在使用 bouncy castle 1.48 来验证 OCSP 的证书验证。它运作良好。但我使用 Ocsp Url 作为静态变量,我想从证书中读取它。网址在证书中写为Authority Info Access

[1]Authority Info Access
 Access Method=On-line Certificate Status Protocol (1.3.6.1.5.5.7.48.1)
 Alternative Name:
      URL=http://ocsp.mydomain

org.bouncycastle.asn1.x509.AuthorityInformationAccess从证书中得到了对象

byte[] octetBytes = certificate.getExtensionValue(X509Extension.authorityInfoAccess.getId());
ASN1InputStream octetStream = new ASN1InputStream(octetBytes);
byte[] encoded = X509ExtensionUtil.fromExtensionValue(octetBytes).getEncoded();
ASN1Sequence seq = ASN1Sequence.getInstance(ASN1Primitive.fromByteArray(encoded));
AuthorityInformationAccess access = AuthorityInformationAccess.getInstance(seq);

写入AuthorityInformationAccess: Oid(1.3.6.1.5.5.7.48.1)但无法从那里获取 URL

4

4 回答 4

6

我找到了方法。

private String getOcspUrl(X509Certificate certificate) throws Exception {
    byte[] octetBytes = certificate
            .getExtensionValue(X509Extension.authorityInfoAccess.getId());

    DLSequence dlSequence = null;
    ASN1Encodable asn1Encodable = null;

    try {
        ASN1Primitive fromExtensionValue = X509ExtensionUtil
                .fromExtensionValue(octetBytes);
        if (!(fromExtensionValue instanceof DLSequence))
            return null;
        dlSequence = (DLSequence) fromExtensionValue;
        for (int i = 0; i < dlSequence.size(); i++) {
            asn1Encodable = dlSequence.getObjectAt(i);
            if (asn1Encodable instanceof DLSequence)
                break;
        }
        if (!(asn1Encodable instanceof DLSequence))
            return null;
        dlSequence = (DLSequence) asn1Encodable;
        for (int i = 0; i < dlSequence.size(); i++) {
            asn1Encodable = dlSequence.getObjectAt(i);
            if (asn1Encodable instanceof DERTaggedObject)
                break;
        }
        if (!(asn1Encodable instanceof DERTaggedObject))
            return null;
        DERTaggedObject derTaggedObject = (DERTaggedObject) asn1Encodable;
        byte[] encoded = derTaggedObject.getEncoded();
        if (derTaggedObject.getTagNo() == 6) {
            int len = encoded[1];
            return new String(encoded, 2, len);
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
    return null;
}
于 2013-04-18T10:33:02.307 回答
1

我是这样做的:

 private String getOcspUrlFromCertificate(X509Certificate cert) {
    byte[] extensionValue = cert.getExtensionValue(X509Extensions.AuthorityInfoAccess.getId());

    try {
        ASN1Sequence asn1Seq = (ASN1Sequence) X509ExtensionUtil.fromExtensionValue(extensionValue); // AuthorityInfoAccessSyntax
        Enumeration<?> objects = asn1Seq.getObjects();

        while (objects.hasMoreElements()) {
            ASN1Sequence obj = (ASN1Sequence) objects.nextElement(); // AccessDescription
            DERObjectIdentifier oid = (DERObjectIdentifier) obj.getObjectAt(0); // accessMethod
            DERTaggedObject location = (DERTaggedObject) obj.getObjectAt(1); // accessLocation

            if (location.getTagNo() == GeneralName.uniformResourceIdentifier) {
                DEROctetString uri = (DEROctetString) location.getObject();
                String str = new String(uri.getOctets());
                if (oid.equals(X509ObjectIdentifiers.id_ad_ocsp)) {
                    return str;
                }
            }
        }
    } catch (Exception e) {
        logger.error("Error", e);
    }

    return null;
}
于 2016-10-26T17:51:29.120 回答
0

BouncyCastle 1.9 是:

依赖性

    <dependency>
        <groupId>org.bouncycastle</groupId>
        <artifactId>bcprov-jdk15on</artifactId>
        <version>1.59</version>
    </dependency>
    <dependency>
        <groupId>org.bouncycastle</groupId>
        <artifactId>bcpkix-jdk15on</artifactId>
        <version>1.59</version>
    </dependency>

获取 url OCSP 的方法

private ASN1Primitive getExtensionValue(X509Certificate certificate, String oid) throws IOException {
    byte[] bytes = certificate.getExtensionValue(oid);
    if (bytes == null) {
        return null;
    }
    ASN1InputStream aIn = new ASN1InputStream(new ByteArrayInputStream(bytes));
    ASN1OctetString octs = (ASN1OctetString) aIn.readObject();
    aIn = new ASN1InputStream(new ByteArrayInputStream(octs.getOctets()));
    return aIn.readObject();
}

public String getOcspUrl() throws Exception {
    ASN1Primitive obj;
    try {
        obj = getExtensionValue(getFirstCertificate(), Extension.authorityInfoAccess.getId());
    } catch (IOException ex) {
        ex.printStackTrace();
        return null;
    }

    if (obj == null) {
        return null;
    }

    AuthorityInformationAccess authorityInformationAccess = AuthorityInformationAccess.getInstance(obj);

    AccessDescription[] accessDescriptions = authorityInformationAccess.getAccessDescriptions();
    for (AccessDescription accessDescription : accessDescriptions) {
        boolean correctAccessMethod = accessDescription.getAccessMethod().equals(X509ObjectIdentifiers.ocspAccessMethod);
        if (!correctAccessMethod) {
            continue;
        }

        GeneralName name = accessDescription.getAccessLocation();
        if (name.getTagNo() != GeneralName.uniformResourceIdentifier) {
            continue;
        }

        DERIA5String derStr = DERIA5String.getInstance((ASN1TaggedObject) name.toASN1Primitive(), false);
        return derStr.getString();
    }

    return null;
}

验证撤销的功能

public CertificateStatus verifRevocationWithOCSP(X509Certificate certificate,X509Certificate issuerCertificate) throws Exception{
    OcspClientBouncyCastle ocspClient = new OcspClientBouncyCastle();
    String urlOCSP = getOcspUrl();
    BasicOCSPResp basicOCSPResp = ocspClient.getBasicOCSPResp(certificate, issuerCertificate, urlOCSP);

    if (basicOCSPResp == null)
        throw new IOException("Error en la consulta ar servidor OCSP ["+urlOCSP+"]");

    BasicOCSPResp basicResponse = basicOCSPResp;
    SingleResp[] responses = basicResponse.getResponses();
    if (responses.length == 1) {
        SingleResp resp = responses[0];
        Object status = resp.getCertStatus();
        System.out.println("OBJECT: "+status);
        if (status == CertificateStatus.GOOD) {
            return CertificateStatus.GOOD;
        } else if (status instanceof RevokedStatus) {
            RevokedStatus revokedStatus = (RevokedStatus)status;
            return revokedStatus;
        } else if(status instanceof UnknownStatus){
            UnknownStatus unknownStatus = (UnknownStatus)status;
            return unknownStatus;
        }
        throw new IOException("Tipo de respuesta de OCSP ["+status+"] desconocido");
    }
    else
        throw new IOException("No se recibio ni una respuesta al consultar el OCSP para la URL ["+urlOCSP+"]");
}
于 2020-02-19T18:54:36.067 回答
0

使用 BouncyCastle X509CertificateHolder。

使用 BouncyCastle 1.66、Java 8 和 Lombok val...

public static String getOcspUrl(final String certPEM) {                                                                                                                                                                                                                                                               
    val certHolder = toCertificateHolder(certPEM);                                                                                                                                                                                                                                                                    
                                                                                                                                                                                                                                                                                                                        
    val aiaExtension = AuthorityInformationAccess.fromExtensions(certHolder.getExtensions());                                                                                                                                                                                                                         
    val ocspUrl = Arrays.stream(aiaExtension.getAccessDescriptions())                                                                                                                                                                                                                                                 
          .filter(ad -> ad.getAccessMethod().equals(X509ObjectIdentifiers.id_ad_ocsp))                                                                                                                                                                                                                                  
          .map(ad -> ad.getAccessLocation().getName().toASN1Primitive().toString())                                                                                                                                                                                                                                     
          .findFirst();

    return ocspUrl.get();
}

public static X509CertificateHolder toCertificateHolder(final String certPEM) {                                                                                                                                                                                                                                       
    val parser = new PEMParser(new StringReader(certPEM));                                                                                                                                                                                                                                                   
    return (X509CertificateHolder) parser.readObject();                                                                                                                                                                                                                                                               
}
于 2020-12-04T22:16:36.217 回答