Search in sources :

Example 1 with CertificatesManager

use of com.bluenimble.platform.crypto.generator.CertificatesManager in project serverless by bluenimble.

the class SignVerifyDocument method main.

public static void main(String[] args) throws StoreLoaderException, UnrecoverableKeyException, KeyStoreException, NoSuchAlgorithmException, CertificateException, SignerException, IOException {
    String password = "beesphere";
    String alias = "beesphere";
    String p12 = "beesphere.p12";
    final String cer = "beesphere.cer";
    CertificatesManager cm = new DefaultCertificatesManager();
    Map<String, Object> properties = new HashMap<String, Object>();
    properties.put(CertificatesManager.KEY_PASSWORD, password);
    KeyStore ks = cm.load(new FileInputStream(p12), properties);
    PrivateKey key = (PrivateKey) ks.getKey(alias, password.toCharArray());
    Signer signer = new DefaultSigner();
    SecureDocument doc = new StringSecureDocument("a document to sign");
    signer.sign(doc, key, new X509Certificate[] { ReadX509.read(new FileInputStream(cer)) });
    System.out.println(new String(doc.getBytes()));
    signer.verify(doc, new CertificateAcceptor() {

        private static final long serialVersionUID = 8524753501741582177L;

        @Override
        public boolean accept(X509Certificate cert) throws SignerException {
            try {
                return cert.equals(ReadX509.read(new FileInputStream(cer)));
            } catch (Throwable th) {
                throw new SignerException(th, th.getMessage());
            }
        }
    });
    System.out.println(new String(doc.getBytes()));
}
Also used : StringSecureDocument(com.bluenimble.platform.crypto.signer.impl.StringSecureDocument) DefaultSigner(com.bluenimble.platform.crypto.signer.impl.DefaultSigner) PrivateKey(java.security.PrivateKey) DefaultCertificatesManager(com.bluenimble.platform.crypto.generator.impl.DefaultCertificatesManager) HashMap(java.util.HashMap) CertificateAcceptor(com.bluenimble.platform.crypto.signer.CertificateAcceptor) KeyStore(java.security.KeyStore) FileInputStream(java.io.FileInputStream) X509Certificate(java.security.cert.X509Certificate) SecureDocument(com.bluenimble.platform.crypto.SecureDocument) StringSecureDocument(com.bluenimble.platform.crypto.signer.impl.StringSecureDocument) DefaultSigner(com.bluenimble.platform.crypto.signer.impl.DefaultSigner) Signer(com.bluenimble.platform.crypto.signer.Signer) CertificatesManager(com.bluenimble.platform.crypto.generator.CertificatesManager) DefaultCertificatesManager(com.bluenimble.platform.crypto.generator.impl.DefaultCertificatesManager) SignerException(com.bluenimble.platform.crypto.signer.SignerException)

Example 2 with CertificatesManager

use of com.bluenimble.platform.crypto.generator.CertificatesManager in project serverless by bluenimble.

the class GenerateFullPKCS12KeyPair method main.

public static void main(String[] args) throws CertificatesManagerException, UnrecoverableKeyException, KeyStoreException, NoSuchAlgorithmException, CertificateException, IOException {
    String fileName = "files/testFull.p12";
    // Create generator instance
    CertificatesManager generator = new DefaultCertificatesManager();
    // Properties
    Map<String, Object> props = new HashMap<String, Object>();
    props.put(CertificatesManager.COMMON_NAME, "bs");
    props.put(CertificatesManager.ORGANIZATION_NAME, "bs");
    props.put(CertificatesManager.ORGANIZATION_UNIT, "bs");
    props.put(CertificatesManager.COUNTRY, "MA");
    props.put(CertificatesManager.STATE, "MA");
    props.put(CertificatesManager.LOCALITY_NAME, "Rabat");
    props.put(CertificatesManager.STORE, "PKCS12");
    props.put(CertificatesManager.KEY_SIZE, 1024);
    props.put(CertificatesManager.KEY_ALGORITHM, "RSA");
    props.put(CertificatesManager.SIGNATURE_ALGORITHM, "MD5withRSA");
    props.put(CertificatesManager.EMAIL, "ait@beesphere.com");
    props.put(CertificatesManager.KEY_PASSWORD, "beesphere");
    // Generate keypair
    OutputStream os = new FileOutputStream(fileName);
    generator.generate(props, os);
    os.close();
    // Read keypair
    System.out.println(ReadPKCS12.read(new FileInputStream(fileName), "beesphere", DefaultCertificatesManager.DEFAULT_ALIAS));
}
Also used : DefaultCertificatesManager(com.bluenimble.platform.crypto.generator.impl.DefaultCertificatesManager) HashMap(java.util.HashMap) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) FileOutputStream(java.io.FileOutputStream) CertificatesManager(com.bluenimble.platform.crypto.generator.CertificatesManager) DefaultCertificatesManager(com.bluenimble.platform.crypto.generator.impl.DefaultCertificatesManager) FileInputStream(java.io.FileInputStream)

Example 3 with CertificatesManager

use of com.bluenimble.platform.crypto.generator.CertificatesManager in project serverless by bluenimble.

the class GeneratePKCS12KeyPair method main.

public static void main(String[] args) throws CertificatesManagerException, UnrecoverableKeyException, KeyStoreException, NoSuchAlgorithmException, CertificateException, IOException {
    String fileName = "files/test.p12";
    // Create generator instance
    CertificatesManager generator = new DefaultCertificatesManager();
    // Generate keypair
    OutputStream os = new FileOutputStream(fileName);
    generator.generate(new HashMap<String, Object>(), os);
    os.close();
    // Read keypair
    System.out.println(ReadPKCS12.read(new FileInputStream(fileName), DefaultCertificatesManager.DUMMY_PASS, DefaultCertificatesManager.DEFAULT_ALIAS));
}
Also used : DefaultCertificatesManager(com.bluenimble.platform.crypto.generator.impl.DefaultCertificatesManager) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) FileOutputStream(java.io.FileOutputStream) CertificatesManager(com.bluenimble.platform.crypto.generator.CertificatesManager) DefaultCertificatesManager(com.bluenimble.platform.crypto.generator.impl.DefaultCertificatesManager) FileInputStream(java.io.FileInputStream)

Example 4 with CertificatesManager

use of com.bluenimble.platform.crypto.generator.CertificatesManager in project serverless by bluenimble.

the class ExportDERCertificate method main.

public static void main(String[] args) throws CertificateExportException, StoreLoaderException, IOException, CertificateException {
    String storeFileName = "files/test.p12";
    String certFileName = "files/test.cer";
    Map<String, Object> EMPTY_MAP = new HashMap<String, Object>();
    // Create generator instance
    CertificatesManager manager = new DefaultCertificatesManager();
    // Export certification DER format
    OutputStream os = new FileOutputStream(certFileName);
    manager.export(manager.load(new FileInputStream(storeFileName), EMPTY_MAP), EMPTY_MAP, CertificatesManager.ExportFormat.DER, os);
    os.close();
    // Read keypair
    System.out.println(ReadX509.read(new FileInputStream(certFileName)));
}
Also used : HashMap(java.util.HashMap) DefaultCertificatesManager(com.bluenimble.platform.crypto.generator.impl.DefaultCertificatesManager) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) FileOutputStream(java.io.FileOutputStream) CertificatesManager(com.bluenimble.platform.crypto.generator.CertificatesManager) DefaultCertificatesManager(com.bluenimble.platform.crypto.generator.impl.DefaultCertificatesManager) FileInputStream(java.io.FileInputStream)

Example 5 with CertificatesManager

use of com.bluenimble.platform.crypto.generator.CertificatesManager in project serverless by bluenimble.

the class ExportPEMCertificate method main.

public static void main(String[] args) throws CertificateExportException, StoreLoaderException, IOException, CertificateException {
    String storeFileName = "files/test.p12";
    String certFileName = "files/test.pem";
    Map<String, Object> EMPTY_MAP = new HashMap<String, Object>();
    // Create generator instance
    CertificatesManager manager = new DefaultCertificatesManager();
    // Export certification PEM format
    OutputStream os = new FileOutputStream(certFileName);
    manager.export(manager.load(new FileInputStream(storeFileName), EMPTY_MAP), EMPTY_MAP, CertificatesManager.ExportFormat.PEM, os);
    os.close();
}
Also used : HashMap(java.util.HashMap) DefaultCertificatesManager(com.bluenimble.platform.crypto.generator.impl.DefaultCertificatesManager) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) FileOutputStream(java.io.FileOutputStream) CertificatesManager(com.bluenimble.platform.crypto.generator.CertificatesManager) DefaultCertificatesManager(com.bluenimble.platform.crypto.generator.impl.DefaultCertificatesManager) FileInputStream(java.io.FileInputStream)

Aggregations

CertificatesManager (com.bluenimble.platform.crypto.generator.CertificatesManager)6 DefaultCertificatesManager (com.bluenimble.platform.crypto.generator.impl.DefaultCertificatesManager)6 FileInputStream (java.io.FileInputStream)6 FileOutputStream (java.io.FileOutputStream)5 OutputStream (java.io.OutputStream)5 HashMap (java.util.HashMap)5 SecureDocument (com.bluenimble.platform.crypto.SecureDocument)1 CertificateAcceptor (com.bluenimble.platform.crypto.signer.CertificateAcceptor)1 Signer (com.bluenimble.platform.crypto.signer.Signer)1 SignerException (com.bluenimble.platform.crypto.signer.SignerException)1 DefaultSigner (com.bluenimble.platform.crypto.signer.impl.DefaultSigner)1 StringSecureDocument (com.bluenimble.platform.crypto.signer.impl.StringSecureDocument)1 KeyStore (java.security.KeyStore)1 PrivateKey (java.security.PrivateKey)1 X509Certificate (java.security.cert.X509Certificate)1