Example usage for org.bouncycastle.asn1.x500.style BCStyle L

List of usage examples for org.bouncycastle.asn1.x500.style BCStyle L

Introduction

In this page you can find the example usage for org.bouncycastle.asn1.x500.style BCStyle L.

Prototype

ASN1ObjectIdentifier L

To view the source code for org.bouncycastle.asn1.x500.style BCStyle L.

Click Source Link

Document

locality name - StringType(SIZE(1..64))

Usage

From source file:ca.trustpoint.m2m.M2mTrustAnchor.java

License:Apache License

/**
 * Creates a new instance.//from  w w  w.  j av  a  2 s .  c o m
 *
 * @param x509Certificate X.509 certificate to use as trust anchor.
 * @throws IllegalArgumentException if x509Certificate is null.
 */
public M2mTrustAnchor(X509Certificate x509Certificate) throws IllegalArgumentException {
    if (x509Certificate == null) {
        throw new IllegalArgumentException("x509Certificate cannot be null.");
    }

    X500Name x500Name = JcaX500NameUtil.getSubject(x509Certificate);
    EntityName caName = new EntityName();
    int attributeCount = 0;

    for (RDN rdn : x500Name.getRDNs()) {
        AttributeTypeAndValue attr = rdn.getFirst();
        EntityNameAttributeId attributeId;

        if (BCStyle.C.equals(attr.getType())) {
            attributeId = EntityNameAttributeId.Country;
        } else if (BCStyle.O.equals(attr.getType())) {
            attributeId = EntityNameAttributeId.Organization;
        } else if (BCStyle.OU.equals(attr.getType())) {
            attributeId = EntityNameAttributeId.OrganizationalUnit;
        } else if (BCStyle.DN_QUALIFIER.equals(attr.getType())) {
            attributeId = EntityNameAttributeId.DistinguishedNameQualifier;
        } else if (BCStyle.ST.equals(attr.getType())) {
            attributeId = EntityNameAttributeId.StateOrProvince;
        } else if (BCStyle.L.equals(attr.getType())) {
            attributeId = EntityNameAttributeId.Locality;
        } else if (BCStyle.CN.equals(attr.getType())) {
            attributeId = EntityNameAttributeId.CommonName;
        } else if (BCStyle.SN.equals(attr.getType())) {
            attributeId = EntityNameAttributeId.SerialNumber;
        } else if (BCStyle.DC.equals(attr.getType())) {
            attributeId = EntityNameAttributeId.DomainComponent;
        } else {
            // Unsupported attribute.
            continue;
        }

        caName.addAttribute(new EntityNameAttribute(attributeId, IETFUtils.valueToString(attr.getValue())));
        attributeCount++;

        if (attributeCount == EntityName.MAXIMUM_ATTRIBUTES) {
            // We have reached the maximum number of attributes for an EntityName, so stop here.
            break;
        }
    }

    this.caName = caName;
    this.publicKey = x509Certificate.getPublicKey();
    certificate = null;
}

From source file:com.aqnote.shared.cryptology.cert.util.X500NameUtil.java

License:Open Source License

public static X500Name createClass1EndPrincipal(String cn, String email) {
    X500NameBuilder x500NameBuilder = new X500NameBuilder(BCStyle.INSTANCE);
    x500NameBuilder.addRDN(BCStyle.E, email);
    x500NameBuilder.addRDN(BCStyle.CN, cn);
    x500NameBuilder.addRDN(BCStyle.OU, DN_OU);
    x500NameBuilder.addRDN(BCStyle.O, DN_O);
    x500NameBuilder.addRDN(BCStyle.L, DN_L);
    x500NameBuilder.addRDN(BCStyle.ST, DN_ST);
    x500NameBuilder.addRDN(BCStyle.C, DN_C);
    return x500NameBuilder.build();
}

From source file:com.aqnote.shared.cryptology.cert.util.X500NameUtil.java

License:Open Source License

public static X500Name createClass3EndPrincipal(String cn, String email) {
    X500NameBuilder x500NameBuilder = new X500NameBuilder(BCStyle.INSTANCE);
    x500NameBuilder.addRDN(BCStyle.E, email);
    x500NameBuilder.addRDN(BCStyle.CN, cn);
    x500NameBuilder.addRDN(BCStyle.OU, DN_OU);
    x500NameBuilder.addRDN(BCStyle.O, DN_O);
    x500NameBuilder.addRDN(BCStyle.L, DN_L);
    x500NameBuilder.addRDN(BCStyle.ST, DN_ST);
    x500NameBuilder.addRDN(BCStyle.C, DN_C);
    return x500NameBuilder.build();
}

From source file:com.aqnote.shared.cryptology.cert.util.X500NameUtil.java

License:Open Source License

public static X500Name createClass3EndPrincipal(List<String> cnList, String email) {
    X500NameBuilder x500NameBuilder = new X500NameBuilder(BCStyle.INSTANCE);
    x500NameBuilder.addRDN(BCStyle.E, email);
    for (String cn : cnList) {
        x500NameBuilder.addRDN(BCStyle.CN, cn);
    }//w  w  w.j  av a2s  .com
    x500NameBuilder.addRDN(BCStyle.OU, DN_OU);
    x500NameBuilder.addRDN(BCStyle.O, DN_O);
    x500NameBuilder.addRDN(BCStyle.L, DN_L);
    x500NameBuilder.addRDN(BCStyle.ST, DN_ST);
    x500NameBuilder.addRDN(BCStyle.C, DN_C);
    return x500NameBuilder.build();
}

From source file:com.hypersocket.certs.X509CertificateUtils.java

License:Open Source License

public static X509Certificate generateSelfSignedCertificate(String cn, String ou, String o, String l, String s,
        String c, KeyPair pair, String signatureType) {
    try {// ww w . j a v a2 s  .c o m
        // Generate self-signed certificate
        X500NameBuilder builder = new X500NameBuilder(BCStyle.INSTANCE);
        builder.addRDN(BCStyle.OU, ou);
        builder.addRDN(BCStyle.O, o);
        builder.addRDN(BCStyle.L, l);
        builder.addRDN(BCStyle.ST, s);
        builder.addRDN(BCStyle.CN, cn);

        Date notBefore = new Date(System.currentTimeMillis() - 1000L * 60 * 60 * 24 * 30);
        Date notAfter = new Date(System.currentTimeMillis() + (1000L * 60 * 60 * 24 * 365 * 10));

        BigInteger serial = BigInteger.valueOf(System.currentTimeMillis());

        X509v3CertificateBuilder certGen = new JcaX509v3CertificateBuilder(builder.build(), serial, notBefore,
                notAfter, builder.build(), pair.getPublic());
        ContentSigner sigGen = new JcaContentSignerBuilder(signatureType).setProvider(BC)
                .build(pair.getPrivate());
        X509Certificate cert = new JcaX509CertificateConverter().setProvider(BC)
                .getCertificate(certGen.build(sigGen));
        cert.checkValidity(new Date());
        cert.verify(cert.getPublicKey());

        return cert;

    } catch (Throwable t) {
        throw new RuntimeException("Failed to generate self-signed certificate!", t);
    }
}

From source file:com.motorolamobility.studio.android.certmanager.core.KeyStoreUtils.java

License:Apache License

/**
 * Create a new X509 certificate for a given KeyPair
 * @param keyPair the {@link KeyPair} used to create the certificate,
 *     RSAPublicKey and RSAPrivateKey are mandatory on keyPair, IllegalArgumentExeption will be thrown otherwise.
 * @param issuerName The issuer name to be used on the certificate
 * @param ownerName  The owner name to be used on the certificate
 * @param expireDate The expire date//from  www .  ja  va2s.c o  m
 * @return The {@link X509Certificate}
 * @throws IOException
 * @throws OperatorCreationException
 * @throws CertificateException
 */
public static X509Certificate createX509Certificate(KeyPair keyPair, CertificateDetailsInfo certDetails)
        throws IOException, OperatorCreationException, CertificateException {

    PublicKey publicKey = keyPair.getPublic();
    PrivateKey privateKey = keyPair.getPrivate();
    if (!(publicKey instanceof RSAPublicKey) || !(privateKey instanceof RSAPrivateKey)) {
        throw new IllegalArgumentException(CertificateManagerNLS.KeyStoreUtils_RSA_Keys_Expected);
    }

    RSAPublicKey rsaPublicKey = (RSAPublicKey) publicKey;
    RSAPrivateKey rsaPrivateKey = (RSAPrivateKey) privateKey;

    //Transform the PublicKey into the BouncyCastle expected format
    ASN1InputStream asn1InputStream = null;
    X509Certificate x509Certificate = null;

    try {
        asn1InputStream = new ASN1InputStream(new ByteArrayInputStream(rsaPublicKey.getEncoded()));
        SubjectPublicKeyInfo pubKey = new SubjectPublicKeyInfo((ASN1Sequence) asn1InputStream.readObject());

        X500NameBuilder nameBuilder = new X500NameBuilder(new BCStrictStyle());
        addField(BCStyle.C, certDetails.getCountry(), nameBuilder);
        addField(BCStyle.ST, certDetails.getState(), nameBuilder);
        addField(BCStyle.L, certDetails.getLocality(), nameBuilder);
        addField(BCStyle.O, certDetails.getOrganization(), nameBuilder);
        addField(BCStyle.OU, certDetails.getOrganizationUnit(), nameBuilder);
        addField(BCStyle.CN, certDetails.getCommonName(), nameBuilder);

        X500Name subjectName = nameBuilder.build();
        X500Name issuerName = subjectName;
        X509v3CertificateBuilder certBuilder = new X509v3CertificateBuilder(issuerName,
                BigInteger.valueOf(new SecureRandom().nextInt()), GregorianCalendar.getInstance().getTime(),
                certDetails.getExpirationDate(), subjectName, pubKey);

        AlgorithmIdentifier sigAlgId = new DefaultSignatureAlgorithmIdentifierFinder().find("SHA1withRSA"); //$NON-NLS-1$
        AlgorithmIdentifier digAlgId = new DefaultDigestAlgorithmIdentifierFinder().find(sigAlgId);
        BcContentSignerBuilder sigGen = new BcRSAContentSignerBuilder(sigAlgId, digAlgId);

        //Create RSAKeyParameters, the private key format expected by Bouncy Castle
        RSAKeyParameters keyParams = new RSAKeyParameters(true, rsaPrivateKey.getPrivateExponent(),
                rsaPrivateKey.getModulus());

        ContentSigner contentSigner = sigGen.build(keyParams);
        X509CertificateHolder certificateHolder = certBuilder.build(contentSigner);

        //Convert the X509Certificate from BouncyCastle format to the java.security format
        JcaX509CertificateConverter certConverter = new JcaX509CertificateConverter();
        x509Certificate = certConverter.getCertificate(certificateHolder);
    } finally {
        if (asn1InputStream != null) {
            try {
                asn1InputStream.close();
            } catch (IOException e) {
                StudioLogger.error("Could not close stream while creating X509 certificate. " + e.getMessage());
            }
        }
    }

    return x509Certificate;
}

From source file:com.motorolamobility.studio.android.certmanager.ui.dialogs.CertificateInfoDialog.java

License:Apache License

@Override
protected Control createDialogArea(Composite parent) {
    Composite newComposite = (Composite) super.createDialogArea(parent);
    X509Certificate cert = null;//from   w w  w  .  j a  va 2  s .  co m

    try {
        cert = entry.getX509Certificate();

        if (cert != null) {
            X500Name x500name = new JcaX509CertificateHolder(cert).getSubject();
            RDN commonName = x500name.getRDNs(BCStyle.CN).length >= 1 ? x500name.getRDNs(BCStyle.CN)[0] : null;
            RDN organization = x500name.getRDNs(BCStyle.O).length >= 1 ? x500name.getRDNs(BCStyle.O)[0] : null;
            RDN organizationUnit = x500name.getRDNs(BCStyle.OU).length >= 1 ? x500name.getRDNs(BCStyle.OU)[0]
                    : null;
            RDN country = x500name.getRDNs(BCStyle.C).length >= 1 ? x500name.getRDNs(BCStyle.C)[0] : null;
            RDN state = x500name.getRDNs(BCStyle.ST).length >= 1 ? x500name.getRDNs(BCStyle.ST)[0] : null;
            RDN locality = x500name.getRDNs(BCStyle.L).length >= 1 ? x500name.getRDNs(BCStyle.L)[0] : null;

            block.createInfoBlock(newComposite, entry.getAlias(), printCertInfo(commonName),
                    printCertInfo(organization), printCertInfo(organizationUnit), printCertInfo(country),
                    printCertInfo(state), printCertInfo(locality), cert.getNotAfter(), cert.getNotBefore());
        } else {
            //not found Android certificate expected (X509Certificate)
            EclipseUtils.showErrorDialog(
                    CertificateManagerNLS.CertificateInfoDialog_UnknownCertificateKeypairType,
                    CertificateManagerNLS.CertificatePropertiesHandler_ErrorGettingCertificateOrKeypairProperties);
        }
    } catch (Exception e) {
        EclipseUtils.showErrorDialog(
                CertificateManagerNLS.CertificatePropertiesHandler_ErrorGettingCertificateOrKeypairProperties,
                e.getMessage());
        StudioLogger.error(CertificateInfoDialog.class,
                CertificateManagerNLS.CertificatePropertiesHandler_ErrorGettingCertificateOrKeypairProperties,
                e);
    }
    return newComposite;
}

From source file:eu.betaas.taas.securitymanager.core.service.impl.InitGWStarService.java

License:Apache License

public void initGwStar(String countryCode, String state, String location, String orgName, String gwId) {

    // subject root
    X500NameBuilder subjRootBld = new X500NameBuilder(BCStyle.INSTANCE);
    subjRootBld.addRDN(BCStyle.C, countryCode);
    subjRootBld.addRDN(BCStyle.ST, state);
    subjRootBld.addRDN(BCStyle.L, location);
    subjRootBld.addRDN(BCStyle.O, orgName);
    subjRootBld.addRDN(BCStyle.CN, "BETaaS Instance Root Certificate");

    X500Name subjRoot = subjRootBld.build();

    X500NameBuilder subjInterBld = new X500NameBuilder(BCStyle.INSTANCE);
    subjInterBld.addRDN(BCStyle.C, countryCode);
    subjInterBld.addRDN(BCStyle.ST, state);
    subjInterBld.addRDN(BCStyle.L, location);
    subjInterBld.addRDN(BCStyle.O, orgName);
    subjInterBld.addRDN(BCStyle.CN, "BETaaS Instance CA Certificate");

    X500Name subjInter = subjInterBld.build();

    X500NameBuilder subjEndBld = new X500NameBuilder(BCStyle.INSTANCE);
    subjEndBld.addRDN(BCStyle.C, countryCode);
    subjEndBld.addRDN(BCStyle.ST, state);
    subjEndBld.addRDN(BCStyle.L, location);
    subjEndBld.addRDN(BCStyle.O, orgName);
    subjEndBld.addRDN(BCStyle.CN, "BETaaS Gateway Certificate");

    X500Name subjEnd = subjEndBld.build();

    log.info("Start initiating GW* certificate now!!");
    gwStarCertIntService.createGwStarCredentials(subjRoot, subjInter, subjEnd, gwId);
}

From source file:eu.betaas.taas.securitymanager.core.service.impl.JoinInstanceService.java

License:Apache License

public boolean requestGwCertificate(String countryCode, String state, String location, String orgName,
        String gwId) throws Exception {
    Security.addProvider(new BouncyCastleProvider());

    boolean ok = false;
    log.info("Start the request certificate instance...");

    ArrayOfCertificate certsArray = null;

    // initiate a CertificationRequest message
    X500NameBuilder x500NameBld = new X500NameBuilder(BCStyle.INSTANCE);
    x500NameBld.addRDN(BCStyle.C, countryCode);
    x500NameBld.addRDN(BCStyle.ST, state);
    x500NameBld.addRDN(BCStyle.L, location);
    x500NameBld.addRDN(BCStyle.O, orgName);
    x500NameBld.addRDN(BCStyle.CN, "BETaaS Gateway Certificate");

    X500Name subject = x500NameBld.build();

    AsymmetricCipherKeyPair kp = ECKeyPairGen.generateECKeyPair192();
    //      log.info("intServ: "+intServ.toString());
    // get the certification request message
    PKCS10CertificationRequest gwCertReq = gwCertificateService.buildCertificationRequest(subject, kp, gwId);
    log.info("Successfully generate PKCS10CertificationRequest!!");

    // get the GW* external cert. service via ServiceTracker
    IGatewayStarCertificateExtService extServ = null;

    extCertTracker = new ServiceTracker(context, IGatewayStarCertificateExtService.class.getName(), null);
    extCertTracker.open();/*from w  ww.  j  a v  a2  s  .  c  o m*/

    // give time to the tracker to find CertificateExtService
    Thread.sleep(2500);

    ServiceReference[] refs = extCertTracker.getServiceReferences();

    // iterating through the service references
    for (ServiceReference ref : refs) {
        log.debug("GW ID: " + ref.getProperty("gwId"));
        log.debug("Is it GW*: " + ((IGatewayStarCertificateExtService) context.getService(ref)).isGWStar());
        // check if the gatewayId of remote GW equals gwStar
        if (((IGatewayStarCertificateExtService) context.getService(ref)).isGWStar()) {
            log.debug("Found the ExtCert service of GW*");
            extServ = (IGatewayStarCertificateExtService) context.getService(ref);
        }
    }

    // send a request to issue a certificate for me (this GW) to GW* 
    if (gwCertReq != null && extServ != null) {
        certsArray = extServ.issueGwCertificate(gwCertReq.getEncoded());

        X509CertificateHolder[] certs = new X509CertificateHolder[certsArray.getCertificate().size()];

        // decode the received array of certificates (consists of intermediate 
        // and my own certificates) from array byte[] to X509Certificate
        for (int i = 0; i < certsArray.getCertificate().size(); i++) {
            byte[] cert = certsArray.getCertificate().get(i);
            certs[i] = new X509CertificateHolder(cert);
        }

        log.debug("Start storing the newly created certificate from GW*...");
        // now store the certificates in a .p12 file
        gwCertificateService.storeMyCertificate(kp.getPrivate(), certs);
        ok = true;

        log.info("Successfully requesting certificate from GW* and store it");

        // closing the service tracker
        extCertTracker.close();
    }

    return ok;
}

From source file:net.sf.keystore_explorer.crypto.csr.spkac.SpkacSubject.java

License:Open Source License

/**
 * Construct SpkacSubject.//from ww  w.  j av a 2s .co m
 *
 * @param name
 *            Name
 */
public SpkacSubject(X500Name name) {
    cn = getRdn(name, BCStyle.CN);
    ou = getRdn(name, BCStyle.OU);
    o = getRdn(name, BCStyle.O);
    l = getRdn(name, BCStyle.L);
    st = getRdn(name, BCStyle.ST);
    c = getRdn(name, BCStyle.C);
}