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

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

Introduction

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

Prototype

ASN1ObjectIdentifier NAME

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

Click Source Link

Document

id-at-name

Usage

From source file:org.cesecore.certificates.util.DnComponentsTest.java

License:Open Source License

@Test
public void test02() {
    String dn = CertTools.stringToBCDNString("uri=fff,CN=oid,SN=12345,NAME=name,C=se");
    final X500Name name = CertTools.stringToBcX500Name(dn);
    ASN1ObjectIdentifier[] oids = name.getAttributeTypes();
    assertEquals(BCStyle.CN, oids[0]);// w  w w.j  a  v  a2 s .  c o  m
    assertEquals(BCStyle.NAME, oids[1]);
    assertEquals(BCStyle.SERIALNUMBER, oids[2]);
    assertEquals(BCStyle.C, oids[3]);
    assertEquals("CN=oid,Name=name,SN=12345,C=se", dn);

    String dn1 = CertTools.stringToBCDNString("SURNAME=Json,=fff,CN=oid,SN=12345,NAME=name,C=se");
    final X500Name name1 = CertTools.stringToBcX500Name(dn1);
    ASN1ObjectIdentifier[] oids1 = name1.getAttributeTypes();
    assertEquals(BCStyle.CN, oids1[0]);
    assertEquals(BCStyle.NAME, oids1[1]);
    assertEquals(BCStyle.SERIALNUMBER, oids1[2]);
    assertEquals(BCStyle.SURNAME, oids1[3]);
    assertEquals(BCStyle.C, oids1[4]);
    assertEquals("CN=oid,Name=name,SN=12345,SURNAME=Json,C=se", dn1);

    String dn2 = CertTools.stringToBCDNString(
            "jurisdictionCountry=SE,jurisdictionState=Stockholm,SURNAME=Json,=fff,CN=oid,jurisdictionLocality=Solna,SN=12345,unstructuredname=foo.bar.com,unstructuredaddress=1.2.3.4,NAME=name,C=se");
    final X500Name name2 = CertTools.stringToBcX500Name(dn2);
    ASN1ObjectIdentifier[] oids2 = name2.getAttributeTypes();
    assertEquals(CeSecoreNameStyle.JURISDICTION_COUNTRY, oids2[0]);
    assertEquals(CeSecoreNameStyle.JURISDICTION_STATE, oids2[1]);
    assertEquals(CeSecoreNameStyle.JURISDICTION_LOCALITY, oids2[2]);
    assertEquals(CeSecoreNameStyle.UnstructuredAddress, oids2[3]);
    assertEquals(CeSecoreNameStyle.UnstructuredName, oids2[4]);
    assertEquals(BCStyle.CN, oids2[5]);
    assertEquals(BCStyle.NAME, oids2[6]);
    assertEquals(BCStyle.SERIALNUMBER, oids2[7]);
    assertEquals(BCStyle.SURNAME, oids2[8]);
    assertEquals(BCStyle.C, oids2[9]);
    assertEquals(
            "JurisdictionCountry=SE,JurisdictionState=Stockholm,JurisdictionLocality=Solna,unstructuredAddress=1.2.3.4,unstructuredName=foo.bar.com,CN=oid,Name=name,SN=12345,SURNAME=Json,C=se",
            dn2);

}

From source file:org.panbox.core.crypto.CryptCore.java

License:Open Source License

/**
 * Creates a self signed certificate valid for 10 years (necessary to store
 * public keys in keystore)/*from  w  ww.  java2s  .c om*/
 * 
 * @param privKey
 * @param pubKey
 * @param eMail
 * @param name
 * @return the certificate or NULL if there is an error
 */
private static X509Certificate createSelfSignedX509Certificate(PrivateKey privKey, PublicKey pubKey,
        String eMail, String name) {
    // Generate self-signed certificate
    X500NameBuilder builder = new X500NameBuilder(BCStyle.INSTANCE);
    builder.addRDN(BCStyle.OU, "Panbox");
    builder.addRDN(BCStyle.O, "Panbox");
    builder.addRDN(BCStyle.CN, "localhost");

    if (eMail != null) {
        builder.addRDN(BCStyle.EmailAddress, eMail);
    }

    if (name != null) {
        builder.addRDN(BCStyle.NAME, name);
    }

    Calendar cal = Calendar.getInstance();
    Date notBefore = cal.getTime();

    cal.add(Calendar.YEAR, PanboxConstants.CERTIFICATE_LIFETIME_YEARS);
    Date notAfter = cal.getTime();

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

    X509v3CertificateBuilder certGen = new JcaX509v3CertificateBuilder(builder.build(), serial, notBefore,
            notAfter, builder.build(), pubKey);

    X509Certificate cert = null;
    try {
        ContentSigner sigGen = new JcaContentSignerBuilder("SHA256WithRSAEncryption")
                .setProvider(KeyConstants.PROV_BC).build(privKey);

        cert = new JcaX509CertificateConverter().setProvider(KeyConstants.PROV_BC)
                .getCertificate(certGen.build(sigGen));

        cert.checkValidity(new Date());

        cert.verify(cert.getPublicKey());

    } catch (NoSuchAlgorithmException | InvalidKeyException | OperatorCreationException | CertificateException
            | NoSuchProviderException | SignatureException e) {
        logger.warn("Exception caught in CryptCore.createSelfSignedX509Certificate, returning null", e);
    }

    return cert;
}