org.spongycastle.asn1.x500.style.BCStyle Java Examples

The following examples show how to use org.spongycastle.asn1.x500.style.BCStyle. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: DistinguishedNameValues.java    From java-n-IDE-for-Android with Apache License 2.0 5 votes vote down vote up
public DistinguishedNameValues() {
    put(BCStyle.C,null);
    put(BCStyle.ST,null);
    put(BCStyle.L,null);
    put(BCStyle.STREET,null);
    put(BCStyle.O,null);
    put(BCStyle.OU,null);
    put(BCStyle.CN,null);
}
 
Example #2
Source File: TlsHelper.java    From an2linuxclient with GNU General Public License v3.0 5 votes vote down vote up
static void initialiseCertificate(Context c, KeyPair keyPair){
    Calendar calendar = Calendar.getInstance();
    calendar.add(Calendar.DAY_OF_MONTH, -1);
    Date notBefore = calendar.getTime();
    calendar.add(Calendar.YEAR, 10);
    Date notAfter = calendar.getTime();

    X500NameBuilder nameBuilder = new X500NameBuilder(BCStyle.INSTANCE);
    nameBuilder.addRDN(BCStyle.CN, "an2linuxclient");
    nameBuilder.addRDN(BCStyle.SERIALNUMBER, new BigInteger(128, new Random()).toString(16));

    X509v3CertificateBuilder certificateBuilder = new JcaX509v3CertificateBuilder(
            nameBuilder.build(),
            BigInteger.ONE,
            notBefore, notAfter,
            nameBuilder.build(),
            keyPair.getPublic()
    );
    try {
        ContentSigner contentSigner = new JcaContentSignerBuilder("SHA256WithRSAEncryption").build(keyPair.getPrivate());
        X509Certificate certificate = new JcaX509CertificateConverter().getCertificate(certificateBuilder.build(contentSigner));

        SharedPreferences deviceKeyPref = c.getSharedPreferences(
                c.getString(R.string.device_key_and_cert), MODE_PRIVATE);
        deviceKeyPref.edit().putString(c.getString(R.string.certificate),
                Base64.encodeToString(certificate.getEncoded(), Base64.NO_WRAP)).apply();
        Log.d("TlsHelper", "Generated new certificate successfully");
    } catch (Exception e){
        Log.e("TlsHelper", "initialiseCertificate");
        Log.e("StackTrace", Log.getStackTraceString(e));
    }
}
 
Example #3
Source File: TlsHelper.java    From an2linuxclient with GNU General Public License v3.0 5 votes vote down vote up
static void initialiseCertificate(Context c, KeyPair keyPair){
    Calendar calendar = Calendar.getInstance();
    calendar.add(Calendar.DAY_OF_MONTH, -1);
    Date notBefore = calendar.getTime();
    calendar.add(Calendar.YEAR, 10);
    Date notAfter = calendar.getTime();

    X500NameBuilder nameBuilder = new X500NameBuilder(BCStyle.INSTANCE);
    nameBuilder.addRDN(BCStyle.CN, "an2linuxclient");
    nameBuilder.addRDN(BCStyle.SERIALNUMBER, new BigInteger(128, new Random()).toString(16));

    X509v3CertificateBuilder certificateBuilder = new JcaX509v3CertificateBuilder(
            nameBuilder.build(),
            BigInteger.ONE,
            notBefore, notAfter,
            nameBuilder.build(),
            keyPair.getPublic()
    );
    try {
        ContentSigner contentSigner = new JcaContentSignerBuilder("SHA256WithRSAEncryption").build(keyPair.getPrivate());
        X509Certificate certificate = new JcaX509CertificateConverter().getCertificate(certificateBuilder.build(contentSigner));

        SharedPreferences deviceKeyPref = c.getSharedPreferences(
                c.getString(R.string.device_key_and_cert), MODE_PRIVATE);
        deviceKeyPref.edit().putString(c.getString(R.string.certificate),
                Base64.encodeToString(certificate.getEncoded(), Base64.NO_WRAP)).apply();
        Log.d("TlsHelper", "Generated new certificate successfully");
    } catch (Exception e){
        Log.e("TlsHelper", "initialiseCertificate");
        Log.e("StackTrace", Log.getStackTraceString(e));
    }
}
 
Example #4
Source File: ModSSL.java    From spydroid-ipcamera with GNU General Public License v3.0 5 votes vote down vote up
public static X509Certificate generateSignedCertificate(X509Certificate caCertificate, PrivateKey caPrivateKey, PublicKey publicKey, String CN) 
		throws NoSuchAlgorithmException, OperatorCreationException, CertificateException, 
		KeyStoreException, UnrecoverableKeyException, IOException, 
		InvalidKeyException, NoSuchPaddingException, InvalidParameterSpecException, 
		InvalidKeySpecException, InvalidAlgorithmParameterException, IllegalBlockSizeException, 
		BadPaddingException {

	X500NameBuilder builder = new X500NameBuilder(BCStyle.INSTANCE);

	builder.addRDN(BCStyle.CN, CN);

	// We want this root certificate to be valid for one year
	Calendar calendar = Calendar.getInstance();
	calendar.add(Calendar.YEAR, 1);

	ContentSigner sigGen = new JcaContentSignerBuilder("SHA1WithRSAEncryption").setProvider(BC).build(caPrivateKey);
	X509v3CertificateBuilder certGen = new JcaX509v3CertificateBuilder(
			caCertificate, 
			new BigInteger(80, new Random()), 
			new Date(System.currentTimeMillis() - 50000),
			calendar.getTime(),
			new X500Principal(builder.build().getEncoded()),
			publicKey);

	// Those are the extensions needed for the certificate to be a leaf certificate that authenticates a SSL server
	certGen.addExtension(new ASN1ObjectIdentifier("2.5.29.15"), true, new X509KeyUsage(X509KeyUsage.keyEncipherment));
	certGen.addExtension(new ASN1ObjectIdentifier("2.5.29.37"), true, new DERSequence(KeyPurposeId.id_kp_serverAuth));

	X509CertificateHolder certificateHolder = certGen.build(sigGen);
	X509Certificate certificate = new JcaX509CertificateConverter().setProvider(BC).getCertificate(certificateHolder);

	return certificate;

}
 
Example #5
Source File: ModSSL.java    From spydroid-ipcamera with GNU General Public License v3.0 5 votes vote down vote up
public static X509Certificate generateRootCertificate(KeyPair keys, String CN) 
		throws NoSuchAlgorithmException, OperatorCreationException, CertificateException, 
		KeyStoreException, UnrecoverableKeyException, IOException, 
		InvalidKeyException, NoSuchPaddingException, InvalidParameterSpecException, 
		InvalidKeySpecException, InvalidAlgorithmParameterException, IllegalBlockSizeException, 
		BadPaddingException {

	X500NameBuilder builder = new X500NameBuilder(BCStyle.INSTANCE);

	builder.addRDN(BCStyle.CN, CN);

	// We want this root certificate to be valid for one year 
	Calendar calendar = Calendar.getInstance();
	calendar.add( Calendar.YEAR, 1 );

	ContentSigner sigGen = new JcaContentSignerBuilder("SHA1WithRSAEncryption").setProvider(BC).build(keys.getPrivate());
	X509v3CertificateBuilder certGen = new JcaX509v3CertificateBuilder(
			builder.build(), 
			new BigInteger(80, new Random()), 
			new Date(System.currentTimeMillis() - 50000),
			calendar.getTime(),
			builder.build(),
			keys.getPublic());

	// Those are the extensions needed for a CA certificate
	certGen.addExtension(new ASN1ObjectIdentifier("2.5.29.19"), true, new BasicConstraints(true));
	certGen.addExtension(new ASN1ObjectIdentifier("2.5.29.15"), true, new X509KeyUsage(X509KeyUsage.digitalSignature));
	certGen.addExtension(new ASN1ObjectIdentifier("2.5.29.37"), true, new ExtendedKeyUsage(KeyPurposeId.id_kp_serverAuth));

	X509CertificateHolder certificateHolder = certGen.build(sigGen);

	X509Certificate certificate = new JcaX509CertificateConverter().setProvider(BC).getCertificate(certificateHolder);

	return certificate;

}
 
Example #6
Source File: CertHelper.java    From moVirt with Apache License 2.0 5 votes vote down vote up
/**
 * @param certificate certificate
 * @return common name
 * @throws IllegalArgumentException if certificate is incorrect type
 */
@NonNull
public static String getCommonName(Certificate certificate) {
    assertX509Certificate(certificate);
    String result = null;
    try {
        X500Name x500name = new JcaX509CertificateHolder((X509Certificate) certificate).getSubject();
        RDN cn = x500name.getRDNs(BCStyle.CN)[0];
        result = IETFUtils.valueToString(cn.getFirst().getValue());
    } catch (CertificateEncodingException ignored) {
    }

    return (result == null) ? "" : result;
}
 
Example #7
Source File: DistinguishedNameValues.java    From java-n-IDE-for-Android with Apache License 2.0 4 votes vote down vote up
public void setCountry( String country) {
    put(BCStyle.C,country);
}
 
Example #8
Source File: DistinguishedNameValues.java    From java-n-IDE-for-Android with Apache License 2.0 4 votes vote down vote up
public void setState( String state) {
    put(BCStyle.ST,state);
}
 
Example #9
Source File: DistinguishedNameValues.java    From java-n-IDE-for-Android with Apache License 2.0 4 votes vote down vote up
public void setLocality( String locality) {
    put(BCStyle.L,locality);
}
 
Example #10
Source File: DistinguishedNameValues.java    From java-n-IDE-for-Android with Apache License 2.0 4 votes vote down vote up
public void setStreet( String street) {
    put( BCStyle.STREET, street);
}
 
Example #11
Source File: DistinguishedNameValues.java    From java-n-IDE-for-Android with Apache License 2.0 4 votes vote down vote up
public void setOrganization( String organization) {
    put(BCStyle.O,organization);
}
 
Example #12
Source File: DistinguishedNameValues.java    From java-n-IDE-for-Android with Apache License 2.0 4 votes vote down vote up
public void setOrganizationalUnit( String organizationalUnit) {
    put(BCStyle.OU,organizationalUnit);
}
 
Example #13
Source File: DistinguishedNameValues.java    From java-n-IDE-for-Android with Apache License 2.0 4 votes vote down vote up
public void setCommonName( String commonName) {
    put(BCStyle.CN,commonName);
}