Example usage for org.bouncycastle.asn1.x509 GeneralNames toASN1Primitive

List of usage examples for org.bouncycastle.asn1.x509 GeneralNames toASN1Primitive

Introduction

In this page you can find the example usage for org.bouncycastle.asn1.x509 GeneralNames toASN1Primitive.

Prototype

public ASN1Primitive toASN1Primitive() 

Source Link

Document

Produce an object suitable for an ASN1OutputStream.

Usage

From source file:it.zero11.acme.utils.X509Utils.java

License:Apache License

public static PKCS10CertificationRequest generateCSR(String[] commonNames, KeyPair pair)
        throws OperatorCreationException, IOException {
    X500NameBuilder namebuilder = new X500NameBuilder(X500Name.getDefaultStyle());
    namebuilder.addRDN(BCStyle.CN, commonNames[0]);

    List<GeneralName> subjectAltNames = new ArrayList<>(commonNames.length);
    for (String cn : commonNames)
        subjectAltNames.add(new GeneralName(GeneralName.dNSName, cn));
    GeneralNames subjectAltName = new GeneralNames(subjectAltNames.toArray(new GeneralName[0]));

    ExtensionsGenerator extGen = new ExtensionsGenerator();
    extGen.addExtension(Extension.subjectAlternativeName, false, subjectAltName.toASN1Primitive());

    PKCS10CertificationRequestBuilder p10Builder = new JcaPKCS10CertificationRequestBuilder(namebuilder.build(),
            pair.getPublic());/*from   w ww .j  av a 2s  .com*/
    p10Builder.addAttribute(PKCSObjectIdentifiers.pkcs_9_at_extensionRequest, extGen.generate());
    JcaContentSignerBuilder csBuilder = new JcaContentSignerBuilder("SHA256withRSA");
    ContentSigner signer = csBuilder.build(pair.getPrivate());
    PKCS10CertificationRequest request = p10Builder.build(signer);
    return request;
}

From source file:net.wstech2.me.httpsclient.CertificateValidatorUtils.java

License:Apache License

/**
 * Retrieves the list of alternative DNS names for this certificate, if any.
 * //w  w w .ja  va2 s.c om
 * @param cert
 *            The certificate from which the issuer name is to the
 *            extracted.
 * @return A list with all alternative DNS names included in the
 *         certificate.
 * @throws IOException
 */
public static List extractSubjectAlternativeNameList(org.bouncycastle.asn1.x509.Certificate cert)
        throws IOException {
    List dnsNames = new ArrayList();
    dnsNames.add(CertificateValidatorUtils.extractCommonName(cert, true));
    Extension subjectAlternativeName = cert.getTBSCertificate().getExtensions()
            .getExtension(Extension.subjectAlternativeName);
    if (subjectAlternativeName == null) {
        return dnsNames;
    }
    ASN1OctetString oct = subjectAlternativeName.getExtnValue();
    ASN1InputStream extIn = new ASN1InputStream(new ByteArrayInputStream(oct.getOctets()));
    GeneralNames gn = GeneralNames.getInstance(extIn.readObject());
    extIn.close();
    ASN1Sequence sq = (ASN1Sequence) gn.toASN1Primitive();
    for (int i = 0; i != sq.size(); i++) {
        GeneralName n = GeneralName.getInstance(sq.getObjectAt(i));
        dnsNames.add(n.getName().toString());

    }
    return dnsNames;
}

From source file:org.glite.voms.ac.AttributeCertificate.java

License:eu-egee.org license

public String getHolderX509() {
    if (acInfo == null) {
        return null;
    }//from w  w w .  jav a2 s.  co  m

    if (acInfo.getHolder() == null) {
        return null;
    }

    GeneralNames gns = acInfo.getHolder().getIssuer();

    for (Enumeration e = ((ASN1Sequence) gns.toASN1Primitive()).getObjects(); e.hasMoreElements();) {
        GeneralName gn = GeneralName.getInstance(e.nextElement());

        if (gn.getTagNo() == 4) {
            X509Principal principal = Util.generalNameToX509Name(gn);
            return PKIUtils.getOpenSSLFormatPrincipal(principal);
        }
    }

    return null;
}

From source file:org.glite.voms.ac.Holder.java

License:eu-egee.org license

protected static boolean matchesDN(X500Principal subject, GeneralNames targets) {
    Enumeration e = ((ASN1Sequence) targets.toASN1Primitive()).getObjects();

    while (e.hasMoreElements()) {
        GeneralName gn = GeneralName.getInstance(e.nextElement());

        if (gn.getTagNo() == 4) {
            try {
                ByteArrayOutputStream b = new ByteArrayOutputStream();
                new DEROutputStream(b).writeObject(gn.getName());

                X500Principal principal = new X500Principal(b.toByteArray());

                if (principal.equals(subject)) {
                    return true;
                }/*w w  w  .  ja v  a 2  s. c  om*/
            } catch (IOException i) {
            }
        }
    }

    return false;
}

From source file:org.glite.voms.PKIUtils.java

License:Open Source License

static private GeneralName[] getNames(GeneralNames gns) {
    ASN1Primitive obj = gns.toASN1Primitive();
    Vector v = new Vector();

    ASN1Sequence seq = (ASN1Sequence) obj;

    int size = seq.size();
    //        System.out.println("Size = " + size);
    for (int i = 0; i < size; i++) {
        //            System.out.println("Adding element:");
        //            System.out.println("Class is: " + ((DERTaggedObject)seq.getObjectAt(i)).getObject().getClass());
        //            ASN1Sequence dseq = (ASN1Sequence)((DERTaggedObject)seq.getObjectAt(i)).getObject();
        //             int size2 = dseq.size();
        //             for (int j = 0; j < size; j++) {
        //                 System.out.println("2Adding element:");
        //                 System.out.println("2Class is: " + dseq.getObjectAt(j));
        //                 System.out.println("Class is: " + ((DERTaggedObject)dseq.getObjectAt(j)).getObject().getClass());
        //                 //                ASN1Sequence dseq = (ASN1Sequence)((DERTaggedObject)seq.getObjectAt(i)).getObject();
        //                 //                int size2 = dseq.size();

        v.add(GeneralName.getInstance(seq.getObjectAt(i)));
        //             }
    }//from w  w  w  .j a  v  a2 s .  c om
    return (GeneralName[]) v.toArray(new GeneralName[0]);
}

From source file:org.glite.voms.VOMSAttribute.java

License:eu-egee.org license

/**
 * Returns an String representation of the AC holder.
 * @return the AC holder./* w ww  . j  a  v a 2 s.  co m*/
 *
 * @throws IllegalArgumentException if no Attribute Certificate has been
 * loaded.
 */
public String getHolder() {
    if (myAC == null)
        throw new IllegalArgumentException("No Attribute Certificate loaded.");

    GeneralNames names = myAC.getHolder().getIssuer();

    Enumeration e = ((ASN1Sequence) names.toASN1Primitive()).getObjects();
    if (e.hasMoreElements()) {
        GeneralName gn = GeneralName.getInstance(e.nextElement());

        if (gn.getTagNo() == 4) {
            try {
                ByteArrayOutputStream b = new ByteArrayOutputStream();
                new DEROutputStream(b).writeObject(gn.getName());

                X500Principal principal = new X500Principal(b.toByteArray());
                return principal.getName();
            } catch (IOException ex) {
                return null;
            }
        }
    }
    return null;
}