Search in sources :

Example 21 with ASN1Primitive

use of com.github.zhenwei.core.asn1.ASN1Primitive in project staplr by pridiltal.

the class PdfPKCS7 method getSubject.

/**
 * Get the "subject" from the TBSCertificate bytes that are passed in
 * @param enc A TBSCertificate in a byte array
 * @return a ASN1Primitive
 */
private static ASN1Primitive getSubject(byte[] enc) {
    try {
        ASN1InputStream in = new ASN1InputStream(new ByteArrayInputStream(enc));
        ASN1Sequence seq = (ASN1Sequence) in.readObject();
        return (ASN1Primitive) seq.getObjectAt(seq.getObjectAt(0) instanceof DERTaggedObject ? 5 : 4);
    } catch (IOException e) {
        throw new ExceptionConverter(e);
    }
}
Also used : ExceptionConverter(pdftk.com.lowagie.text.ExceptionConverter) ASN1InputStream(org.bouncycastle.asn1.ASN1InputStream) ASN1Sequence(org.bouncycastle.asn1.ASN1Sequence) ByteArrayInputStream(java.io.ByteArrayInputStream) DERTaggedObject(org.bouncycastle.asn1.DERTaggedObject) IOException(java.io.IOException) ASN1Primitive(org.bouncycastle.asn1.ASN1Primitive)

Example 22 with ASN1Primitive

use of com.github.zhenwei.core.asn1.ASN1Primitive in project pdf-sign-check by spapas.

the class CRLVerifier method getCrlDistributionPoints.

/**
 * Extracts all CRL distribution point URLs from the "CRL Distribution
 * Point" extension in a X.509 certificate. If CRL distribution point
 * extension is unavailable, returns an empty list.
 * @param cert
 * @return List of CRL distribution point URLs.
 * @throws java.io.IOException
 */
public static List<String> getCrlDistributionPoints(X509Certificate cert) throws IOException {
    byte[] crldpExt = cert.getExtensionValue(Extension.cRLDistributionPoints.getId());
    if (crldpExt == null) {
        return new ArrayList<>();
    }
    ASN1Primitive derObjCrlDP;
    try (ASN1InputStream oAsnInStream = new ASN1InputStream(crldpExt)) {
        derObjCrlDP = oAsnInStream.readObject();
    }
    if (!(derObjCrlDP instanceof ASN1OctetString)) {
        LOG.warn("CRL distribution points for certificate subject " + cert.getSubjectX500Principal().getName() + " should be an octet string, but is " + derObjCrlDP);
        return new ArrayList<>();
    }
    ASN1OctetString dosCrlDP = (ASN1OctetString) derObjCrlDP;
    byte[] crldpExtOctets = dosCrlDP.getOctets();
    ASN1Primitive derObj2;
    try (ASN1InputStream oAsnInStream2 = new ASN1InputStream(crldpExtOctets)) {
        derObj2 = oAsnInStream2.readObject();
    }
    CRLDistPoint distPoint = CRLDistPoint.getInstance(derObj2);
    List<String> crlUrls = new ArrayList<>();
    for (DistributionPoint dp : distPoint.getDistributionPoints()) {
        DistributionPointName dpn = dp.getDistributionPoint();
        // Look for URIs in fullName
        if (dpn != null && dpn.getType() == DistributionPointName.FULL_NAME) {
            // Look for an URI
            for (GeneralName genName : GeneralNames.getInstance(dpn.getName()).getNames()) {
                if (genName.getTagNo() == GeneralName.uniformResourceIdentifier) {
                    String url = ASN1IA5String.getInstance(genName.getName()).getString();
                    crlUrls.add(url);
                }
            }
        }
    }
    return crlUrls;
}
Also used : ASN1OctetString(org.bouncycastle.asn1.ASN1OctetString) ASN1InputStream(org.bouncycastle.asn1.ASN1InputStream) ArrayList(java.util.ArrayList) DistributionPointName(org.bouncycastle.asn1.x509.DistributionPointName) ASN1OctetString(org.bouncycastle.asn1.ASN1OctetString) ASN1IA5String(org.bouncycastle.asn1.ASN1IA5String) DistributionPoint(org.bouncycastle.asn1.x509.DistributionPoint) GeneralName(org.bouncycastle.asn1.x509.GeneralName) ASN1Primitive(org.bouncycastle.asn1.ASN1Primitive) CRLDistPoint(org.bouncycastle.asn1.x509.CRLDistPoint)

Example 23 with ASN1Primitive

use of com.github.zhenwei.core.asn1.ASN1Primitive in project Falcon-File-Transfer-Optimizer by arif-zaman.

the class ProxyCertInfoTest method testParseProxyCertInfo.

public void testParseProxyCertInfo() throws Exception {
    ProxyPolicy policy = new ProxyPolicy(testOid, testPolicy);
    ProxyCertInfo info = new ProxyCertInfo(3, policy);
    ByteArrayOutputStream bOut = new ByteArrayOutputStream();
    DEROutputStream dOut = new DEROutputStream(bOut);
    dOut.writeObject(info);
    ByteArrayInputStream bIn = new ByteArrayInputStream(bOut.toByteArray());
    ASN1InputStream dIn = new ASN1InputStream(bIn);
    ASN1Primitive obj = dIn.readObject();
    assertTrue(obj instanceof ASN1Sequence);
    ProxyCertInfo testInfo = new ProxyCertInfo((ASN1Sequence) obj);
    assertEquals(3, testInfo.getPathLenConstraint());
    assertEquals(testPolicy, testInfo.getProxyPolicy().getPolicyAsString());
    assertEquals(testOid, testInfo.getProxyPolicy().getPolicyLanguage());
}
Also used : ASN1InputStream(org.bouncycastle.asn1.ASN1InputStream) ASN1Sequence(org.bouncycastle.asn1.ASN1Sequence) ByteArrayInputStream(java.io.ByteArrayInputStream) ProxyPolicy(org.globus.gsi.proxy.ext.ProxyPolicy) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ASN1Primitive(org.bouncycastle.asn1.ASN1Primitive) ProxyCertInfo(org.globus.gsi.proxy.ext.ProxyCertInfo) DEROutputStream(org.bouncycastle.asn1.DEROutputStream)

Example 24 with ASN1Primitive

use of com.github.zhenwei.core.asn1.ASN1Primitive in project Falcon-File-Transfer-Optimizer by arif-zaman.

the class ProxyCertInfoTest method testCreateProxyCertInfo2.

public void testCreateProxyCertInfo2() throws Exception {
    ProxyPolicy policy = new ProxyPolicy(testOid, testPolicy);
    ProxyCertInfo info = new ProxyCertInfo(policy);
    assertEquals(Integer.MAX_VALUE, info.getPathLenConstraint());
    assertEquals(testPolicy, info.getProxyPolicy().getPolicyAsString());
    assertEquals(testOid, info.getProxyPolicy().getPolicyLanguage());
    ByteArrayOutputStream bOut = new ByteArrayOutputStream();
    DEROutputStream dOut = new DEROutputStream(bOut);
    dOut.writeObject(info);
    ByteArrayInputStream bIn = new ByteArrayInputStream(bOut.toByteArray());
    ASN1InputStream dIn = new ASN1InputStream(bIn);
    ASN1Primitive obj = dIn.readObject();
    ProxyCertInfo testInfo = new ProxyCertInfo((ASN1Sequence) obj);
    assertEquals(Integer.MAX_VALUE, testInfo.getPathLenConstraint());
    assertEquals(testPolicy, testInfo.getProxyPolicy().getPolicyAsString());
    assertEquals(testOid, testInfo.getProxyPolicy().getPolicyLanguage());
}
Also used : ASN1InputStream(org.bouncycastle.asn1.ASN1InputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) ProxyPolicy(org.globus.gsi.proxy.ext.ProxyPolicy) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ASN1Primitive(org.bouncycastle.asn1.ASN1Primitive) ProxyCertInfo(org.globus.gsi.proxy.ext.ProxyCertInfo) DEROutputStream(org.bouncycastle.asn1.DEROutputStream)

Example 25 with ASN1Primitive

use of com.github.zhenwei.core.asn1.ASN1Primitive in project Falcon-File-Transfer-Optimizer by arif-zaman.

the class BouncyCastleUtil method getExtensionValue.

/**
 * Retrieves the actual value of the X.509 extension.
 *
 * @param certExtValue the DER-encoded OCTET string value of the extension.
 * @return the decoded/actual value of the extension (the octets).
 */
public static byte[] getExtensionValue(byte[] certExtValue) throws IOException {
    ByteArrayInputStream inStream = new ByteArrayInputStream(certExtValue);
    ASN1InputStream derInputStream = new ASN1InputStream(inStream);
    ASN1Primitive object = derInputStream.readObject();
    if (object instanceof ASN1OctetString) {
        return ((ASN1OctetString) object).getOctets();
    } else {
        throw new IOException(i18n.getMessage("octectExp"));
    }
}
Also used : ASN1OctetString(org.bouncycastle.asn1.ASN1OctetString) ASN1InputStream(org.bouncycastle.asn1.ASN1InputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) IOException(java.io.IOException) ASN1Primitive(org.bouncycastle.asn1.ASN1Primitive)

Aggregations

ASN1EncodableVector (com.github.zhenwei.core.asn1.ASN1EncodableVector)253 DERSequence (com.github.zhenwei.core.asn1.DERSequence)231 ASN1Primitive (org.bouncycastle.asn1.ASN1Primitive)177 IOException (java.io.IOException)107 ASN1InputStream (org.bouncycastle.asn1.ASN1InputStream)62 ASN1Sequence (org.bouncycastle.asn1.ASN1Sequence)57 DERTaggedObject (com.github.zhenwei.core.asn1.DERTaggedObject)55 ASN1Integer (com.github.zhenwei.core.asn1.ASN1Integer)42 DEROctetString (com.github.zhenwei.core.asn1.DEROctetString)38 ByteArrayInputStream (java.io.ByteArrayInputStream)38 ASN1OctetString (org.bouncycastle.asn1.ASN1OctetString)32 ASN1Primitive (com.github.zhenwei.core.asn1.ASN1Primitive)31 ASN1ObjectIdentifier (org.bouncycastle.asn1.ASN1ObjectIdentifier)31 ASN1Encodable (org.bouncycastle.asn1.ASN1Encodable)30 DEROctetString (org.bouncycastle.asn1.DEROctetString)28 BigInteger (java.math.BigInteger)24 GeneralSecurityException (java.security.GeneralSecurityException)24 X509Certificate (java.security.cert.X509Certificate)24 ASN1Integer (org.bouncycastle.asn1.ASN1Integer)23 DERIA5String (org.bouncycastle.asn1.DERIA5String)22