Example usage for org.bouncycastle.asn1.x509 NameConstraints getInstance

List of usage examples for org.bouncycastle.asn1.x509 NameConstraints getInstance

Introduction

In this page you can find the example usage for org.bouncycastle.asn1.x509 NameConstraints getInstance.

Prototype

public static NameConstraints getInstance(Object obj) 

Source Link

Usage

From source file:eu.emi.security.authn.x509.helpers.pkipath.bc.FixedBCPKIXCertPathReviewer.java

License:Open Source License

private void checkNameConstraints() {
    X509Certificate cert = null;/*  ww w .  j  a  va2 s  .c  o  m*/

    //
    // Setup
    //

    // (b)  and (c)
    PKIXNameConstraintValidator nameConstraintValidator = new PKIXNameConstraintValidator();

    //
    // process each certificate except the self issued which are not last in the path
    //
    int index;

    try {
        for (index = certs.size() - 1; index >= 0; index--) {
            //
            // certificate processing
            //    

            cert = (X509Certificate) certs.get(index);

            // b),c)

            if (!(isSelfIssued(cert) && index != 0)) {
                X500Principal principal = getSubjectPrincipal(cert);
                ASN1InputStream aIn = new ASN1InputStream(new ByteArrayInputStream(principal.getEncoded()));
                ASN1Sequence dns;

                try {
                    dns = (ASN1Sequence) aIn.readObject();
                } catch (IOException e) {
                    ErrorBundle msg = new ErrorBundle(RESOURCE_NAME, "CertPathReviewer.ncSubjectNameError",
                            new Object[] { new UntrustedInput(principal) });
                    throw new CertPathReviewerException(msg, e, certPath, index);
                }

                try {
                    nameConstraintValidator.checkPermittedDN(dns);
                } catch (PKIXNameConstraintValidatorException cpve) {
                    ErrorBundle msg = new ErrorBundle(RESOURCE_NAME, "CertPathReviewer.notPermittedDN",
                            new Object[] { new UntrustedInput(principal.getName()) });
                    throw new CertPathReviewerException(msg, cpve, certPath, index);
                }

                try {
                    nameConstraintValidator.checkExcludedDN(dns);
                } catch (PKIXNameConstraintValidatorException cpve) {
                    ErrorBundle msg = new ErrorBundle(RESOURCE_NAME, "CertPathReviewer.excludedDN",
                            new Object[] { new UntrustedInput(principal.getName()) });
                    throw new CertPathReviewerException(msg, cpve, certPath, index);
                }

                //FIX (missing in orig cert path reviewer)
                Vector emails = new X509Name(dns).getValues(X509Name.EmailAddress);
                for (Enumeration e = emails.elements(); e.hasMoreElements();) {
                    String email = (String) e.nextElement();
                    GeneralName emailAsGeneralName = new GeneralName(GeneralName.rfc822Name, email);
                    try {
                        nameConstraintValidator.checkPermitted(emailAsGeneralName);
                    } catch (PKIXNameConstraintValidatorException cpve) {
                        ErrorBundle msg = new ErrorBundle(RESOURCE_NAME, "CertPathReviewer.notPermittedDN",
                                new Object[] { new UntrustedInput(principal.getName()) });
                        throw new CertPathReviewerException(msg, cpve, certPath, index);
                    }

                    try {
                        nameConstraintValidator.checkExcluded(emailAsGeneralName);
                    } catch (PKIXNameConstraintValidatorException cpve) {
                        ErrorBundle msg = new ErrorBundle(RESOURCE_NAME, "CertPathReviewer.excludedDN",
                                new Object[] { new UntrustedInput(principal.getName()) });
                        throw new CertPathReviewerException(msg, cpve, certPath, index);
                    }
                }

                ASN1Sequence altName;
                try {
                    altName = (ASN1Sequence) getExtensionValue(cert, SUBJECT_ALTERNATIVE_NAME);
                } catch (AnnotatedException ae) {
                    ErrorBundle msg = new ErrorBundle(RESOURCE_NAME, "CertPathReviewer.subjAltNameExtError");
                    throw new CertPathReviewerException(msg, ae, certPath, index);
                }

                if (altName != null) {
                    for (int j = 0; j < altName.size(); j++) {
                        GeneralName name = GeneralName.getInstance(altName.getObjectAt(j));

                        try {
                            nameConstraintValidator.checkPermitted(name);
                            nameConstraintValidator.checkExcluded(name);
                        } catch (PKIXNameConstraintValidatorException cpve) {
                            ErrorBundle msg = new ErrorBundle(RESOURCE_NAME,
                                    "CertPathReviewer.notPermittedEmail",
                                    new Object[] { new UntrustedInput(name) });
                            throw new CertPathReviewerException(msg, cpve, certPath, index);
                        }
                    }
                }

            }

            //
            // prepare for next certificate
            //

            //
            // (g) handle the name constraints extension
            //
            ASN1Sequence ncSeq;
            try {
                ncSeq = (ASN1Sequence) getExtensionValue(cert, NAME_CONSTRAINTS);
            } catch (AnnotatedException ae) {
                ErrorBundle msg = new ErrorBundle(RESOURCE_NAME, "CertPathReviewer.ncExtError");
                throw new CertPathReviewerException(msg, ae, certPath, index);
            }

            if (ncSeq != null) {
                NameConstraints nc = NameConstraints.getInstance(ncSeq);

                //
                // (g) (1) permitted subtrees
                //
                GeneralSubtree[] permitted = nc.getPermittedSubtrees();
                if (permitted != null) {
                    nameConstraintValidator.intersectPermittedSubtree(permitted);
                }

                //
                // (g) (2) excluded subtrees
                //
                GeneralSubtree[] excluded = nc.getExcludedSubtrees();
                if (excluded != null) {
                    for (int c = 0; c != excluded.length; c++) {
                        nameConstraintValidator.addExcludedSubtree(excluded[c]);
                    }
                }
            }

        } // for
    } catch (CertPathReviewerException cpre) {
        addError(cpre.getErrorMessage(), cpre.getIndex());
    }
}

From source file:net.sf.keystore_explorer.crypto.x509.X509Ext.java

License:Open Source License

private String getNameConstraintsStringValue(byte[] value) throws IOException {
    // @formatter:off

    /*/*from ww  w . j av  a  2  s  . co  m*/
     * NameConstraints ::= ASN1Sequence { permittedSubtrees [0]
     * GeneralSubtrees OPTIONAL, excludedSubtrees [1] GeneralSubtrees
     * OPTIONAL }
     *
     * GeneralSubtrees ::= ASN1Sequence SIZE (1..MAX) OF GeneralSubtree
     *
     * GeneralSubtree ::= ASN1Sequence { base GeneralName, minimum [0]
     * BaseDistance DEFAULT nodistance, maximum [1] BaseDistance OPTIONAL }
     *
     * BaseDistance ::= ASN1Integer {nodistance(0) } (0..MAX)
     */

    // @formatter:on

    StringBuilder sb = new StringBuilder();

    NameConstraints nameConstraints = NameConstraints.getInstance(value);

    GeneralSubtrees permittedSubtrees = null;
    if (nameConstraints.getPermittedSubtrees() != null && nameConstraints.getPermittedSubtrees().length != 0) {
        permittedSubtrees = new GeneralSubtrees(nameConstraints.getPermittedSubtrees());
    }

    sb.append(res.getString("PermittedSubtrees"));

    if (permittedSubtrees == null) {
        sb.append(" ").append(res.getString("NoValue"));
        sb.append(NEWLINE);
    } else {
        sb.append(NEWLINE);

        int permitted = 0;

        for (GeneralSubtree permittedSubtree : permittedSubtrees.getGeneralSubtrees()) {
            permitted++;

            sb.append(INDENT);
            sb.append(MessageFormat.format(res.getString("PermittedSubtree"), permitted));
            sb.append(NEWLINE);

            sb.append(INDENT);
            sb.append(INDENT);
            sb.append(res.getString("Base"));
            sb.append(NEWLINE);

            GeneralName base = permittedSubtree.getBase();

            sb.append(INDENT);
            sb.append(INDENT);
            sb.append(INDENT);
            sb.append(GeneralNameUtil.toString(base));
            sb.append(NEWLINE);

            BigInteger minimum = permittedSubtree.getMinimum();
            int minimumInt = 0; // Default 'nodistance' value

            if (minimum != null) {
                minimumInt = minimum.intValue();
            }

            sb.append(INDENT);
            sb.append(INDENT);
            sb.append(MessageFormat.format(res.getString("Minimum"), minimumInt));
            sb.append(NEWLINE);

            BigInteger maximum = permittedSubtree.getMaximum();

            if (maximum != null) {
                int maximumInt = maximum.intValue();

                sb.append(INDENT);
                sb.append(INDENT);
                sb.append(MessageFormat.format(res.getString("Maximum"), maximumInt));
                sb.append(NEWLINE);
            }
        }
    }

    GeneralSubtree[] excludedSubtreeArray = nameConstraints.getExcludedSubtrees();

    sb.append(res.getString("ExcludedSubtrees"));

    if (excludedSubtreeArray == null) { // Optional
        sb.append(" ").append(res.getString("NoValue"));
        sb.append(NEWLINE);
    } else {

        GeneralSubtrees excludedSubtrees = new GeneralSubtrees(excludedSubtreeArray);

        sb.append(NEWLINE);

        int excluded = 0;

        for (GeneralSubtree excludedSubtree : excludedSubtrees.getGeneralSubtrees()) {
            excluded++;

            sb.append(INDENT);
            sb.append(MessageFormat.format(res.getString("ExcludedSubtree"), excluded));
            sb.append(NEWLINE);

            sb.append(INDENT);
            sb.append(INDENT);
            sb.append(res.getString("Base"));
            sb.append(NEWLINE);

            GeneralName base = excludedSubtree.getBase();

            sb.append(INDENT);
            sb.append(INDENT);
            sb.append(INDENT);
            sb.append(GeneralNameUtil.toString(base));
            sb.append(NEWLINE);

            BigInteger minimum = excludedSubtree.getMinimum();
            int minimumInt = minimum.intValue();

            sb.append(INDENT);
            sb.append(INDENT);
            sb.append(MessageFormat.format(res.getString("Minimum"), minimumInt));
            sb.append(NEWLINE);

            BigInteger maximum = excludedSubtree.getMaximum();

            if (maximum != null) {
                int maximumInt = maximum.intValue();

                sb.append(INDENT);
                sb.append(INDENT);
                sb.append(MessageFormat.format(res.getString("Maximum"), maximumInt));
                sb.append(NEWLINE);
            }
        }
    }

    return sb.toString();
}

From source file:net.sf.keystore_explorer.gui.dialogs.extensions.DNameConstraints.java

License:Open Source License

private void prepopulateWithValue(byte[] value) throws IOException {
    NameConstraints nameConstraints = NameConstraints.getInstance(value);

    if (nameConstraints.getPermittedSubtrees() != null) {
        jgsPermittedSubtrees.setGeneralSubtrees(new GeneralSubtrees(nameConstraints.getPermittedSubtrees()));
    }/*from  ww w  .j  ava2  s  .c  om*/

    if (nameConstraints.getExcludedSubtrees() != null) {
        jgsExcludedSubtrees.setGeneralSubtrees(new GeneralSubtrees(nameConstraints.getExcludedSubtrees()));
    }
}

From source file:org.cesecore.util.CertTools.java

License:Open Source License

/**
 * Checks that the given SubjectDN / SAN satisfies the Name Constraints of the given issuer (if there are any).
 * This method checks the Name Constraints in the given issuer only. A complete implementation of
 * name constraints should check the whole certificate chain.
 * //from   w  w w .j  a  v  a  2s .c  o m
 * @param issuer Issuing CA.
 * @param subjectDNName Subject DN to check. Optional.
 * @param subjectAltName Subject Alternative Name to check. Optional.
 * @throws CertificateExtensionException
 */
public static void checkNameConstraints(X509Certificate issuer, X500Name subjectDNName,
        GeneralNames subjectAltName) throws IllegalNameException {
    final byte[] ncbytes = issuer.getExtensionValue(Extension.nameConstraints.getId());
    final ASN1OctetString ncstr = (ncbytes != null ? DEROctetString.getInstance(ncbytes) : null);
    final ASN1Sequence ncseq = (ncbytes != null ? DERSequence.getInstance(ncstr.getOctets()) : null);
    final NameConstraints nc = (ncseq != null ? NameConstraints.getInstance(ncseq) : null);

    if (nc != null) {
        if (subjectDNName != null) {
            // Skip check for root CAs
            final X500Name issuerDNName = X500Name.getInstance(issuer.getSubjectX500Principal().getEncoded());
            if (issuerDNName.equals(subjectDNName)) {
                return;
            }
        }

        final PKIXNameConstraintValidator validator = new PKIXNameConstraintValidator();

        GeneralSubtree[] permitted = nc.getPermittedSubtrees();
        GeneralSubtree[] excluded = nc.getExcludedSubtrees();

        if (permitted != null) {
            validator.intersectPermittedSubtree(permitted);
        }
        if (excluded != null) {
            for (GeneralSubtree subtree : excluded) {
                validator.addExcludedSubtree(subtree);
            }
        }

        if (subjectDNName != null) {
            GeneralName dngn = new GeneralName(subjectDNName);
            try {
                validator.checkPermitted(dngn);
                validator.checkExcluded(dngn);
            } catch (PKIXNameConstraintValidatorException e) {
                final String dnStr = subjectDNName.toString();
                final boolean isLdapOrder = dnHasMultipleComponents(dnStr) && !isDNReversed(dnStr);
                if (isLdapOrder) {
                    final String msg = intres.getLocalizedMessage("nameconstraints.x500dnorderrequired");
                    throw new IllegalNameException(msg);
                } else {
                    final String msg = intres.getLocalizedMessage("nameconstraints.forbiddensubjectdn",
                            subjectDNName);
                    throw new IllegalNameException(msg, e);
                }
            }
        }

        if (subjectAltName != null) {
            for (GeneralName sangn : subjectAltName.getNames()) {
                try {
                    validator.checkPermitted(sangn);
                    validator.checkExcluded(sangn);
                } catch (PKIXNameConstraintValidatorException e) {
                    final String msg = intres.getLocalizedMessage("nameconstraints.forbiddensubjectaltname",
                            sangn);
                    throw new IllegalNameException(msg, e);
                }
            }
        }
    }
}

From source file:org.tdmx.client.crypto.certificate.PKIXCertificate.java

License:Open Source License

private X500Name getSubjectNameConstraint() {
    Extension e = holder.getExtension(Extension.nameConstraints);
    if (e != null && e.isCritical()) {
        NameConstraints nc = NameConstraints.getInstance(e.getParsedValue());
        GeneralSubtree[] permitted = nc.getPermittedSubtrees();
        if (permitted != null && permitted.length > 0) {
            GeneralName base = permitted[0].getBase();
            if (base != null) {
                if (GeneralName.directoryName == base.getTagNo()) {
                    X500Name baseName = X500Name.getInstance(base.getName());
                    return baseName;
                }/*from  w w w  .j  a  v a 2s  . co  m*/
            }
        }
    }
    return null;
}