Search in sources :

Example 51 with X509Extension

use of org.bouncycastle.asn1.x509.X509Extension in project Falcon-File-Transfer-Optimizer by arif-zaman.

the class CertificateUtil method getKeyUsage.

/**
 * Gets a boolean array representing bits of the KeyUsage extension.
 *
 * @throws IOException if failed to extract the KeyUsage extension value.
 * @see java.security.cert.X509Certificate#getKeyUsage
 */
public static EnumSet<KeyUsage> getKeyUsage(X509Extension ext) throws IOException {
    DERBitString bits = (DERBitString) getExtensionObject(ext);
    EnumSet<KeyUsage> keyUsage = EnumSet.noneOf(KeyUsage.class);
    for (KeyUsage bit : KeyUsage.values()) {
        if (bit.isSet(bits)) {
            keyUsage.add(bit);
        }
    }
    return keyUsage;
}
Also used : DERBitString(org.bouncycastle.asn1.DERBitString)

Example 52 with X509Extension

use of org.bouncycastle.asn1.x509.X509Extension in project Falcon-File-Transfer-Optimizer by arif-zaman.

the class CertificateUtil method getCAPathConstraint.

/**
 * Return CA Path constraint
 *
 * @param crt
 * @return the CA path constraint
 * @throws IOException
 */
public static int getCAPathConstraint(TBSCertificateStructure crt) throws IOException {
    X509Extensions extensions = crt.getExtensions();
    if (extensions == null) {
        return -1;
    }
    X509Extension proxyExtension = extensions.getExtension(X509Extension.basicConstraints);
    if (proxyExtension != null) {
        BasicConstraints basicExt = getBasicConstraints(proxyExtension);
        if (basicExt.isCA()) {
            BigInteger pathLen = basicExt.getPathLenConstraint();
            return (pathLen == null) ? Integer.MAX_VALUE : pathLen.intValue();
        } else {
            return -1;
        }
    }
    return -1;
}
Also used : X509Extension(org.bouncycastle.asn1.x509.X509Extension) BigInteger(java.math.BigInteger) X509Extensions(org.bouncycastle.asn1.x509.X509Extensions) BasicConstraints(org.bouncycastle.asn1.x509.BasicConstraints)

Example 53 with X509Extension

use of org.bouncycastle.asn1.x509.X509Extension in project Falcon-File-Transfer-Optimizer by arif-zaman.

the class CertificateUtil method processCN.

private static GSIConstants.CertificateType processCN(X509Extensions extensions, GSIConstants.CertificateType type, ASN1Sequence ava) throws CertificateException {
    X509Extension ext;
    String value = ((ASN1String) ava.getObjectAt(1)).getString();
    GSIConstants.CertificateType certType = type;
    if (value.equalsIgnoreCase("proxy")) {
        certType = GSIConstants.CertificateType.GSI_2_PROXY;
    } else if (value.equalsIgnoreCase("limited proxy")) {
        certType = GSIConstants.CertificateType.GSI_2_LIMITED_PROXY;
    } else if (extensions != null) {
        boolean gsi4 = true;
        // GSI_4
        ext = extensions.getExtension(ProxyCertInfo.OID);
        if (ext == null) {
            // GSI_3
            ext = extensions.getExtension(ProxyCertInfo.OLD_OID);
            gsi4 = false;
        }
        if (ext != null) {
            if (ext.isCritical()) {
                certType = processCriticalExtension(ext, gsi4);
            } else {
                String err = "proxyCertCritical";
                throw new CertificateException(err);
            }
        }
    }
    return certType;
}
Also used : GSIConstants(org.globus.gsi.GSIConstants) X509Extension(org.bouncycastle.asn1.x509.X509Extension) CertificateException(java.security.cert.CertificateException) DERBitString(org.bouncycastle.asn1.DERBitString) ASN1String(org.bouncycastle.asn1.ASN1String) ASN1String(org.bouncycastle.asn1.ASN1String)

Example 54 with X509Extension

use of org.bouncycastle.asn1.x509.X509Extension in project Falcon-File-Transfer-Optimizer by arif-zaman.

the class CertificateUtil method getCertificateType.

/**
 * Returns certificate type of the given TBS certificate. <BR> The
 * certificate type is {@link org.globus.gsi.GSIConstants.CertificateType#CA
 * GSIConstants.CertificateType.CA} <B>only</B> if the certificate contains a
 * BasicConstraints extension and it is marked as CA.<BR> A certificate is a
 * GSI-2 proxy when the subject DN of the certificate ends with
 * <I>"CN=proxy"</I> (certificate type {@link org.globus.gsi.GSIConstants.CertificateType#GSI_2_PROXY
 * GSIConstants.CertificateType.GSI_2_PROXY}) or <I>"CN=limited proxy"</I> (certificate
 * type {@link org.globus.gsi.GSIConstants.CertificateType#GSI_2_LIMITED_PROXY
 * GSIConstants.CertificateType.LIMITED_PROXY}) component and the issuer DN of the
 * certificate matches the subject DN without the last proxy <I>CN</I>
 * component.<BR> A certificate is a GSI-3 proxy when the subject DN of the
 * certificate ends with a <I>CN</I> component, the issuer DN of the
 * certificate matches the subject DN without the last <I>CN</I> component
 * and the certificate contains {@link ProxyCertInfo
 * ProxyCertInfo} critical extension. The certificate type is {@link
 * org.globus.gsi.GSIConstants.CertificateType#GSI_3_IMPERSONATION_PROXY
 * GSIConstants.CertificateType.GSI_3_IMPERSONATION_PROXY} if the policy language of the
 * {@link ProxyCertInfo ProxyCertInfo}
 * extension is set to {@link ProxyPolicy#IMPERSONATION
 * ProxyPolicy.IMPERSONATION} OID. The certificate type is {@link
 * org.globus.gsi.GSIConstants.CertificateType#GSI_3_LIMITED_PROXY
 * GSIConstants.CertificateType.GSI_3_LIMITED_PROXY} if the policy language of the {@link
 * ProxyCertInfo ProxyCertInfo} extension
 * is set to {@link ProxyPolicy#LIMITED
 * ProxyPolicy.LIMITED} OID. The certificate type is {@link
 * org.globus.gsi.GSIConstants.CertificateType#GSI_3_INDEPENDENT_PROXY
 * GSIConstants.CertificateType.GSI_3_INDEPENDENT_PROXY} if the policy language of the
 * {@link ProxyCertInfo ProxyCertInfo}
 * extension is set to {@link ProxyPolicy#INDEPENDENT
 * ProxyPolicy.INDEPENDENT} OID. The certificate type is {@link
 * org.globus.gsi.GSIConstants.CertificateType#GSI_3_RESTRICTED_PROXY
 * GSIConstants.CertificateType.GSI_3_RESTRICTED_PROXY} if the policy language of the
 * {@link ProxyCertInfo ProxyCertInfo}
 * extension is set to any other OID then the above.<BR> The certificate
 * type is {@link org.globus.gsi.GSIConstants.CertificateType#EEC
 * GSIConstants.CertificateType.EEC} if the certificate is not a CA certificate or a
 * GSI-2 or GSI-3 proxy.
 *
 * @param crt the TBS certificate to get the type of.
 * @return the certificate type. The certificate type is determined by rules
 *         described above.
 * @throws java.io.IOException if something goes wrong.
 * @throws java.security.cert.CertificateException
 *                             for proxy certificates, if the issuer DN of
 *                             the certificate does not match the subject DN
 *                             of the certificate without the last <I>CN</I>
 *                             component. Also, for GSI-3 proxies when the
 *                             <code>ProxyCertInfo</code> extension is not
 *                             marked as critical.
 */
public static GSIConstants.CertificateType getCertificateType(TBSCertificateStructure crt) throws CertificateException, IOException {
    X509Extensions extensions = crt.getExtensions();
    X509Extension ext = null;
    if (extensions != null) {
        ext = extensions.getExtension(X509Extension.basicConstraints);
        if (ext != null) {
            BasicConstraints basicExt = getBasicConstraints(ext);
            if (basicExt.isCA()) {
                return GSIConstants.CertificateType.CA;
            }
        }
    }
    GSIConstants.CertificateType type = GSIConstants.CertificateType.EEC;
    // does not handle multiple AVAs
    X500Name subject = crt.getSubject();
    ASN1Set entry = X509NameHelper.getLastNameEntry(subject);
    ASN1Sequence ava = (ASN1Sequence) entry.getObjectAt(0);
    if (BCStyle.CN.equals(ava.getObjectAt(0))) {
        type = processCN(extensions, type, ava);
    }
    return type;
}
Also used : GSIConstants(org.globus.gsi.GSIConstants) ASN1Sequence(org.bouncycastle.asn1.ASN1Sequence) ASN1Set(org.bouncycastle.asn1.ASN1Set) X509Extension(org.bouncycastle.asn1.x509.X509Extension) X509Extensions(org.bouncycastle.asn1.x509.X509Extensions) X500Name(org.bouncycastle.asn1.x500.X500Name) BasicConstraints(org.bouncycastle.asn1.x509.BasicConstraints)

Example 55 with X509Extension

use of org.bouncycastle.asn1.x509.X509Extension in project Falcon-File-Transfer-Optimizer by arif-zaman.

the class BouncyCastleCertProcessingFactoryTest method testResctrictedWithOtherExt.

public void testResctrictedWithOtherExt() throws Exception {
    ClassLoader loader = BouncyCastleCertProcessingFactoryTest.class.getClassLoader();
    GlobusCredential cred = new GlobusCredential(loader.getResource(proxyFile).getPath());
    X509Extension ext = null;
    String oid = "1.2.3.4";
    String expectedValue = "foo";
    boolean critical = false;
    String policyOid = "1.2.3.4.5.6.7.8.9";
    String policyValue = "bar";
    X509ExtensionSet extSet = new X509ExtensionSet();
    ext = new X509Extension(oid, critical, expectedValue.getBytes());
    extSet.add(ext);
    DERSequence seq = new DERSequence(new ASN1Encodable[] { DERBoolean.FALSE, new ASN1Integer(15) });
    BasicConstraints constraints = BasicConstraints.getInstance(seq);
    ext = new BouncyCastleX509Extension(org.bouncycastle.asn1.x509.X509Extension.basicConstraints.getId(), false, constraints);
    extSet.add(ext);
    ProxyPolicy policy = new ProxyPolicy(policyOid, policyValue.getBytes());
    ext = new ProxyCertInfoExtension(new ProxyCertInfo(policy));
    extSet.add(ext);
    GlobusCredential newCred = factory.createCredential(cred.getCertificateChain(), cred.getPrivateKey(), 512, 60 * 60, GSIConstants.GSI_3_RESTRICTED_PROXY, extSet, null);
    X509Certificate newCert = newCred.getCertificateChain()[0];
    verifyExtension(newCert, oid, expectedValue, critical);
    byte[] realValue = BouncyCastleUtil.getExtensionValue(newCert, ProxyCertInfo.OID.getId());
    assertTrue(realValue != null && realValue.length > 0);
    ProxyCertInfo proxyCertInfo = ProxyCertInfo.getInstance(realValue);
    assertTrue(proxyCertInfo != null);
    assertTrue(proxyCertInfo.getProxyPolicy() != null);
    assertEquals(policyOid, proxyCertInfo.getProxyPolicy().getPolicyLanguage().getId());
    assertEquals(policyValue, proxyCertInfo.getProxyPolicy().getPolicyAsString());
}
Also used : X509Extension(org.globus.gsi.X509Extension) BouncyCastleX509Extension(org.globus.gsi.bc.BouncyCastleX509Extension) ProxyPolicy(org.globus.gsi.proxy.ext.ProxyPolicy) ASN1Integer(org.bouncycastle.asn1.ASN1Integer) ProxyCertInfoExtension(org.globus.gsi.proxy.ext.ProxyCertInfoExtension) ProxyCertInfo(org.globus.gsi.proxy.ext.ProxyCertInfo) X509Certificate(java.security.cert.X509Certificate) X509ExtensionSet(org.globus.gsi.X509ExtensionSet) DERSequence(org.bouncycastle.asn1.DERSequence) GlobusCredential(org.globus.gsi.GlobusCredential) BasicConstraints(org.bouncycastle.asn1.x509.BasicConstraints) BouncyCastleX509Extension(org.globus.gsi.bc.BouncyCastleX509Extension)

Aggregations

X509Extension (org.bouncycastle.asn1.x509.X509Extension)21 Enumeration (java.util.Enumeration)20 X509Extensions (org.bouncycastle.asn1.x509.X509Extensions)20 IOException (java.io.IOException)18 ASN1ObjectIdentifier (org.bouncycastle.asn1.ASN1ObjectIdentifier)12 HashSet (java.util.HashSet)11 X509Extension (org.gudy.bouncycastle.asn1.x509.X509Extension)11 X509Certificate (java.security.cert.X509Certificate)10 ASN1Sequence (org.bouncycastle.asn1.ASN1Sequence)10 DERObjectIdentifier (org.bouncycastle.asn1.DERObjectIdentifier)10 ASN1Encodable (org.bouncycastle.asn1.ASN1Encodable)8 DERBitString (org.bouncycastle.asn1.DERBitString)8 DERSequence (org.bouncycastle.asn1.DERSequence)8 BasicConstraints (org.bouncycastle.asn1.x509.BasicConstraints)8 CertificateException (java.security.cert.CertificateException)7 ASN1EncodableVector (org.bouncycastle.asn1.ASN1EncodableVector)7 GeneralSecurityException (java.security.GeneralSecurityException)6 ArrayList (java.util.ArrayList)6 Set (java.util.Set)6 ASN1OctetString (org.bouncycastle.asn1.ASN1OctetString)6