|
| 1 | +package com.cloud.utils.security; |
| 2 | + |
| 3 | +import java.io.BufferedInputStream; |
| 4 | +import java.io.ByteArrayInputStream; |
| 5 | +import java.io.ByteArrayOutputStream; |
| 6 | +import java.io.IOException; |
| 7 | +import java.security.Key; |
| 8 | +import java.security.KeyFactory; |
| 9 | +import java.security.KeyStore; |
| 10 | +import java.security.KeyStoreException; |
| 11 | +import java.security.NoSuchAlgorithmException; |
| 12 | +import java.security.cert.Certificate; |
| 13 | +import java.security.cert.CertificateException; |
| 14 | +import java.security.cert.CertificateFactory; |
| 15 | +import java.security.spec.InvalidKeySpecException; |
| 16 | +import java.security.spec.PKCS8EncodedKeySpec; |
| 17 | + |
| 18 | +import org.apache.commons.codec.binary.Base64; |
| 19 | + |
| 20 | +public class CertificateHelper { |
| 21 | + public static byte[] buildAndSaveKeystore(String alias, String cert, String privateKey, String storePassword) throws KeyStoreException, CertificateException, |
| 22 | + NoSuchAlgorithmException, InvalidKeySpecException, IOException { |
| 23 | + KeyStore ks = buildKeystore(alias, cert, privateKey, storePassword); |
| 24 | + |
| 25 | + ByteArrayOutputStream os = new ByteArrayOutputStream(); |
| 26 | + ks.store(os, storePassword != null ? storePassword.toCharArray() : null); |
| 27 | + os.close(); |
| 28 | + return os.toByteArray(); |
| 29 | + } |
| 30 | + |
| 31 | + public static KeyStore loadKeystore(byte[] ksData, String storePassword) throws KeyStoreException, CertificateException, NoSuchAlgorithmException, IOException { |
| 32 | + assert(ksData != null); |
| 33 | + KeyStore ks = KeyStore.getInstance("JKS"); |
| 34 | + ks.load(new ByteArrayInputStream(ksData), storePassword != null ? storePassword.toCharArray() : null); |
| 35 | + |
| 36 | + return ks; |
| 37 | + } |
| 38 | + |
| 39 | + public static KeyStore buildKeystore(String alias, String cert, String privateKey, String storePassword) throws KeyStoreException, CertificateException, |
| 40 | + NoSuchAlgorithmException, InvalidKeySpecException, IOException { |
| 41 | + |
| 42 | + KeyStore ks = KeyStore.getInstance("JKS"); |
| 43 | + ks.load(null, storePassword != null ? storePassword.toCharArray() : null); |
| 44 | + Certificate[] certs = new Certificate[1]; |
| 45 | + certs[0] = buildCertificate(cert); |
| 46 | + ks.setKeyEntry(alias, buildPrivateKey(privateKey), storePassword != null ? storePassword.toCharArray() : null, certs ); |
| 47 | + return ks; |
| 48 | + } |
| 49 | + |
| 50 | + public static Certificate buildCertificate(String content) throws CertificateException { |
| 51 | + assert(content != null); |
| 52 | + |
| 53 | + BufferedInputStream bis = new BufferedInputStream(new ByteArrayInputStream(content.getBytes())); |
| 54 | + CertificateFactory cf = CertificateFactory.getInstance("X.509"); |
| 55 | + return cf.generateCertificate(bis); |
| 56 | + } |
| 57 | + |
| 58 | + public static Key buildPrivateKey(String base64EncodedKeyContent) throws NoSuchAlgorithmException, InvalidKeySpecException, IOException { |
| 59 | + KeyFactory kf = KeyFactory.getInstance("RSA"); |
| 60 | + PKCS8EncodedKeySpec keysp = new PKCS8EncodedKeySpec (Base64.decodeBase64(base64EncodedKeyContent)); |
| 61 | + return kf.generatePrivate (keysp); |
| 62 | + } |
| 63 | +} |
0 commit comments