Example usage for org.bouncycastle.asn1.x509 X509Extension convertValueToObject

List of usage examples for org.bouncycastle.asn1.x509 X509Extension convertValueToObject

Introduction

In this page you can find the example usage for org.bouncycastle.asn1.x509 X509Extension convertValueToObject.

Prototype

public static ASN1Primitive convertValueToObject(X509Extension ext) throws IllegalArgumentException 

Source Link

Document

Convert the value of the passed in extension to an object

Usage

From source file:com.yacme.ext.oxsit.cust_it.comp.security.cert.CertificateComplianceCA_IT.java

License:Open Source License

/**
 * check if qcStatements are present as per ETSI 
 * @param _TbsC // w  w  w  .j a  va2s.co  m
 * @return
 */
private boolean hasQcStatements(TBSCertificateStructure _TbsC) {
    //first check for CNIPA requirement
    //then check for ETSI 102 280 requirements
    //then check for ETSI 101 862      
    //qcstatements are defined in ETSI 101 862
    X509Extensions xExt = _TbsC.getExtensions();
    X509Extension qcStats = xExt.getExtension(X509Extensions.QCStatements);

    if (qcStats == null) {
        //no qcStatement
        setCertificateStateHelper(CertificateState.MISSING_EXTENSION);
        m_aLogger.log("missing qcStatements");
        return false;
    }
    int numberOfChecksOk = 4; //if this drops to zero,

    //it's not marked critical
    if (!qcStats.isCritical())
        numberOfChecksOk--;

    ASN1Sequence dns = (ASN1Sequence) X509Extension.convertValueToObject(qcStats);
    for (int i = 0; i < dns.size(); i++) {
        QCStatement qcs = QCStatement.getInstance(dns.getObjectAt(i));
        if (QCStatement.id_etsi_qcs_QcCompliance.equals(qcs.getStatementId()))
            numberOfChecksOk--;
        if (QCStatement.id_etsi_qcs_QcSSCD.equals(qcs.getStatementId()))
            numberOfChecksOk--;
        if (QCStatement.id_etsi_qcs_RetentionPeriod.equals(qcs.getStatementId()))
            numberOfChecksOk--;
    }

    if (numberOfChecksOk != 0) {
        m_xQc.setCertificateElementErrorState(X509Extensions.QCStatements.getId(),
                CertificateElementState.INVALID_value);
        setCertificateStateHelper(CertificateState.ERROR_IN_EXTENSION);
        return false;
    }

    return true;
}

From source file:com.yacme.ext.oxsit.cust_it.comp.security.cert.CertificateCompliance_IT.java

License:Open Source License

/**
 * check if qcStatements are present as per ETSI 
 * @param _TbsC //from w ww .  jav  a2 s. co m
 * @return
 */
private boolean hasQcStatements(TBSCertificateStructure _TbsC) {
    //first check for CNIPA requirement
    //then check for ETSI 102 280 requirements
    //then check for ETSI 101 862      
    //qcstatements are defined in ETSI 101 862
    X509Extensions xExt = _TbsC.getExtensions();
    X509Extension qcStats = xExt.getExtension(X509Extensions.QCStatements);

    if (qcStats == null) {
        //no qcStatement
        setCertificateStateHelper(CertificateState.MISSING_EXTENSION);
        m_aLogger.log("missing qcStatements");
        String s = m_xQc.getCertificateDisplayObj()
                .getCertificateElementCommentString(CertificateElementID.NOT_CRITICAL_EXTENSION);
        s = s + "\r";

        m_xQc.getCertificateDisplayObj().setCertificateElementCommentString(
                CertificateElementID.NOT_CRITICAL_EXTENSION, s + "qcStatement missing");
        return false;
    }
    int numberOfChecksOk = 4; //if this drops to zero,

    //it's not marked critical
    if (!qcStats.isCritical())
        numberOfChecksOk--;

    ASN1Sequence dns = (ASN1Sequence) X509Extension.convertValueToObject(qcStats);
    for (int i = 0; i < dns.size(); i++) {
        QCStatement qcs = QCStatement.getInstance(dns.getObjectAt(i));
        if (QCStatement.id_etsi_qcs_QcCompliance.equals(qcs.getStatementId()))
            numberOfChecksOk--;
        if (QCStatement.id_etsi_qcs_QcSSCD.equals(qcs.getStatementId()))
            numberOfChecksOk--;
        if (QCStatement.id_etsi_qcs_RetentionPeriod.equals(qcs.getStatementId()))
            numberOfChecksOk--;
    }

    if (numberOfChecksOk != 0) {
        m_xQc.setCertificateElementErrorState(X509Extensions.QCStatements.getId(),
                CertificateElementState.INVALID_value);
        setCertificateStateHelper(CertificateState.ERROR_IN_EXTENSION);

        m_xQc.getCertificateDisplayObj().setCertificateExtensionCommentString(
                X509Extensions.QCStatements.getId(), "some statement is wrong.");
        return false;
    }

    return true;
}

From source file:edu.washington.iam.tools.IamCertificateHelper.java

License:Apache License

public static int parseCsr(IamCertificate cert) throws IamCertificateException {

    try {//from   w  ww .j a v  a 2 s .  com
        PEMReader pRd = new PEMReader(new StringReader(cert.pemRequest));
        PKCS10CertificationRequest request = (PKCS10CertificationRequest) pRd.readObject();
        if (request == null)
            throw new IamCertificateException("invalid CSR (request)");
        CertificationRequestInfo info = request.getCertificationRequestInfo();
        if (info == null)
            throw new IamCertificateException("invalid CSR (info)");

        X509Name dn = info.getSubject();
        if (dn == null)
            throw new IamCertificateException("invalid CSR (dn)");
        log.debug("dn=" + dn.toString());
        cert.dn = dn.toString();
        try {
            List cns = dn.getValues(X509Name.CN);
            cert.cn = (String) (cns.get(0));
            log.debug("cn=" + cert.cn);
            cert.names.add(cert.cn); // first entry for names is always cn
            cns = dn.getValues(X509Name.C);
            cert.dnC = (String) (cns.get(0));
            cns = dn.getValues(X509Name.ST);
            cert.dnST = (String) (cns.get(0));
        } catch (Exception e) {
            log.debug("get cn error: " + e);
            throw new IamCertificateException("invalid CSR");
        }

        // see if we've got alt names (in extensions)

        ASN1Set attrs = info.getAttributes();
        if (attrs != null) {
            for (int a = 0; a < attrs.size(); a++) {
                Attribute attr = Attribute.getInstance(attrs.getObjectAt(a));
                if (attr.getAttrType().equals(PKCSObjectIdentifiers.pkcs_9_at_extensionRequest)) {

                    // is the extension
                    X509Extensions extensions = X509Extensions.getInstance(attr.getAttrValues().getObjectAt(0));

                    // get the subAltName extension
                    DERObjectIdentifier sanoid = new DERObjectIdentifier(
                            X509Extensions.SubjectAlternativeName.getId());
                    X509Extension xext = extensions.getExtension(sanoid);
                    if (xext != null) {
                        log.debug("processing altname extensions");
                        ASN1Object asn1 = X509Extension.convertValueToObject(xext);
                        Enumeration dit = DERSequence.getInstance(asn1).getObjects();
                        while (dit.hasMoreElements()) {
                            GeneralName gn = GeneralName.getInstance(dit.nextElement());
                            log.debug("altname tag=" + gn.getTagNo());
                            log.debug("altname name=" + gn.getName().toString());
                            if (gn.getTagNo() == GeneralName.dNSName)
                                cert.names.add(gn.getName().toString());
                        }
                    }

                }
            }
        }

        // check key size
        PublicKey pk = request.getPublicKey();
        log.debug("key alg = " + pk.getAlgorithm());
        log.debug("key fmt = " + pk.getFormat());
        if (pk.getAlgorithm().equals("RSA")) {
            RSAPublicKey rpk = (RSAPublicKey) pk;
            cert.keySize = rpk.getModulus().bitLength();
            log.debug("key size = " + cert.keySize);
        }

    } catch (IOException e) {
        log.debug("ioerror: " + e);
        throw new IamCertificateException("invalid CSR " + e.getMessage());
    } catch (Exception e) {
        log.debug("excp: " + e);
        throw new IamCertificateException("invalid CSR");
    }
    return 1;
}

From source file:org.globus.gsi.util.CertificateUtil.java

License:Apache License

/**
 * Creates a <code>BasicConstraints</code> object from given extension.
 *
 * @param ext the extension.//from  w ww  .  j  av a  2 s  .c  om
 * @return the <code>BasicConstraints</code> object.
 * @throws IOException if something fails.
 */
public static BasicConstraints getBasicConstraints(X509Extension ext) throws IOException {

    ASN1Object object = X509Extension.convertValueToObject(ext);
    return BasicConstraints.getInstance(object);
}

From source file:org.qipki.crypto.x509.X509ExtensionsReaderImpl.java

License:Open Source License

@Override
public List<X509ExtensionHolder> extractRequestedExtensions(PKCS10CertificationRequest pkcs10) {
    final List<X509ExtensionHolder> extractedExtensions = new ArrayList<X509ExtensionHolder>();
    final CertificationRequestInfo certificationRequestInfo = pkcs10.getCertificationRequestInfo();
    final ASN1Set attributesAsn1Set = certificationRequestInfo.getAttributes();
    if (attributesAsn1Set == null) {
        return extractedExtensions;
    }//from  www  .  j a va2  s  .co  m
    // The `Extension Request` attribute is contained within an ASN.1 Set,
    // usually as the first element.
    X509Extensions requestedExtensions = null;
    for (int i = 0; i < attributesAsn1Set.size(); ++i) {
        // There should be only only one attribute in the set. (that is, only
        // the `Extension Request`, but loop through to find it properly)
        final DEREncodable derEncodable = attributesAsn1Set.getObjectAt(i);
        if (derEncodable instanceof DERSequence) {
            final Attribute attribute = new Attribute((DERSequence) attributesAsn1Set.getObjectAt(i));

            if (attribute.getAttrType().equals(PKCSObjectIdentifiers.pkcs_9_at_extensionRequest)) {
                // The `Extension Request` attribute is present.
                final ASN1Set attributeValues = attribute.getAttrValues();

                // The X509Extensions are contained as a value of the ASN.1 Set.
                // WARN Assuming that it is the first value of the set.
                if (attributeValues.size() >= 1) {
                    DEREncodable extensionsDEREncodable = attributeValues.getObjectAt(0);
                    ASN1Sequence extensionsASN1Sequence = (ASN1Sequence) extensionsDEREncodable;
                    requestedExtensions = new X509Extensions(extensionsASN1Sequence);
                    // No need to search any more.
                    break;
                }
            }
        }
    }
    if (requestedExtensions != null) {
        Enumeration<?> e = requestedExtensions.oids();
        while (e.hasMoreElements()) {
            DERObjectIdentifier oid = (DERObjectIdentifier) e.nextElement();
            X509Extension extension = requestedExtensions.getExtension(oid);
            extractedExtensions.add(new X509ExtensionHolder(oid, extension.isCritical(),
                    X509Extension.convertValueToObject(extension)));
        }
    }
    return extractedExtensions;
}