@@ -0,0 +1,2741 @@
+package com.cloud.utils.security;%0A%0Aimport java.io.BufferedInputStream;%0Aimport java.io.ByteArrayInputStream;%0Aimport java.io.ByteArrayOutputStream;%0Aimport java.io.IOException;%0Aimport java.security.Key;%0Aimport java.security.KeyFactory;%0Aimport java.security.KeyStore;%0Aimport java.security.KeyStoreException;%0Aimport java.security.NoSuchAlgorithmException;%0Aimport java.security.cert.Certificate;%0Aimport java.security.cert.CertificateException;%0Aimport java.security.cert.CertificateFactory;%0Aimport java.security.spec.InvalidKeySpecException;%0Aimport java.security.spec.PKCS8EncodedKeySpec;%0A%0Aimport org.apache.commons.codec.binary.Base64;%0A%0Apublic class CertificateHelper %7B%0A%09public static byte%5B%5D buildAndSaveKeystore(String alias, String cert, String privateKey, String storePassword) throws KeyStoreException, CertificateException, %0A%09%09NoSuchAlgorithmException, InvalidKeySpecException, IOException %7B%0A%09%09KeyStore ks = buildKeystore(alias, cert, privateKey, storePassword);%0A%09%09%0A%09%09ByteArrayOutputStream os = new ByteArrayOutputStream();%0A%09%09ks.store(os, storePassword != null ? storePassword.toCharArray() : null);%0A%09%09os.close();%0A%09%09return os.toByteArray();%0A%09%7D%0A%09%0A%09public static KeyStore loadKeystore(byte%5B%5D ksData, String storePassword) throws KeyStoreException, CertificateException, NoSuchAlgorithmException, IOException %7B%0A%09%09assert(ksData != null);%0A%09%09KeyStore ks = KeyStore.getInstance(%22JKS%22);%0A%09%09ks.load(new ByteArrayInputStream(ksData), storePassword != null ? storePassword.toCharArray() : null);%0A%0A%09%09return ks;%0A%09%7D%0A%09%0A%09public static KeyStore buildKeystore(String alias, String cert, String privateKey, String storePassword) throws KeyStoreException, CertificateException, %0A%09%09NoSuchAlgorithmException, InvalidKeySpecException, IOException %7B%0A%09%0A%09%09KeyStore ks = KeyStore.getInstance(%22JKS%22);%0A%09%09ks.load(null, storePassword != null ? storePassword.toCharArray() : null);%0A%09%09Certificate%5B%5D certs = new Certificate%5B1%5D;%0A%09%09certs%5B0%5D = buildCertificate(cert);%0A%09    ks.setKeyEntry(alias, buildPrivateKey(privateKey), storePassword != null ? storePassword.toCharArray() : null, certs );%0A%09%09return ks;%0A%09%7D%0A%0A%09public static Certificate buildCertificate(String content) throws CertificateException %7B%0A%09%09assert(content != null);%0A%09%09%0A%09%09BufferedInputStream bis = new BufferedInputStream(new ByteArrayInputStream(content.getBytes()));%0A%09%09CertificateFactory cf = CertificateFactory.getInstance(%22X.509%22);%0A%09%09return cf.generateCertificate(bis);%0A%09%7D%0A%0A%09public static Key buildPrivateKey(String base64EncodedKeyContent) throws NoSuchAlgorithmException, InvalidKeySpecException, IOException %7B%0A%09    KeyFactory kf = KeyFactory.getInstance(%22RSA%22);%0A%09    PKCS8EncodedKeySpec  keysp = new PKCS8EncodedKeySpec (Base64.decodeBase64(base64EncodedKeyContent));%0A%09    return kf.generatePrivate (keysp);%0A%09%7D%0A%7D%0A

Add java keystore helper class, prepare for dynamically generating keystore for SSL use