Advertisement
Guest User

Untitled

a guest
Feb 12th, 2016
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.70 KB | None | 0 0
  1. import org.slf4j.Logger;
  2. import org.slf4j.LoggerFactory;
  3. import sun.security.tools.keytool.CertAndKeyGen;
  4. import sun.security.x509.X500Name;
  5.  
  6. import java.io.IOException;
  7. import java.security.InvalidKeyException;
  8. import java.security.KeyStore;
  9. import java.security.KeyStoreException;
  10. import java.security.NoSuchAlgorithmException;
  11. import java.security.NoSuchProviderException;
  12. import java.security.SignatureException;
  13. import java.security.cert.Certificate;
  14. import java.security.cert.CertificateException;
  15. import java.security.cert.X509Certificate;
  16.  
  17. public class CertificateUtils {
  18.  
  19. public static final String KEY_TYPE_RSA = "RSA";
  20. public static final String SIG_ALG_SHA_RSA = "SHA1WithRSA";
  21. public static final int KEY_SIZE = 1024;
  22. public static final long CERT_VALIDITY = 365 * 24 * 3600L;
  23. public static final String ALIAS_PRIVATE = "private";
  24. public static final String ALIAS_CERT = "cert";
  25. public static final String IN_MEMORY_PWD = "notReallyImportant"; // this would only ever be relevant if/when persisted.
  26. private static final Logger LOGGER = LoggerFactory.getLogger(CertificateUtils.class);
  27.  
  28. /**
  29. * @param certValues e.g. CN=Dave, OU=JavaSoft, O=Sun Microsystems, C=US
  30. * @return
  31. */
  32. public static KeyStore createSelfSigned(String certValues) {
  33. try {
  34. CertAndKeyGen keyGen = new CertAndKeyGen(KEY_TYPE_RSA, SIG_ALG_SHA_RSA);
  35. keyGen.generate(KEY_SIZE);
  36.  
  37. KeyStore ks = emptyStore();
  38. if (ks == null) {
  39. return null;
  40. }
  41. X509Certificate certificate = keyGen.getSelfCertificate(new X500Name(certValues), CERT_VALIDITY);
  42. ks.setCertificateEntry(ALIAS_CERT, certificate);
  43. ks.setKeyEntry(ALIAS_PRIVATE, keyGen.getPrivateKey(), IN_MEMORY_PWD.toCharArray(), new Certificate[]{certificate});
  44. return ks;
  45.  
  46. } catch (KeyStoreException | NoSuchAlgorithmException | NoSuchProviderException | CertificateException | SignatureException | InvalidKeyException | IOException e) {
  47. LOGGER.error("Cannot create self signed certificate.", e);
  48. }
  49. return null;
  50. }
  51.  
  52. public static KeyStore createSelfSignedForHost(String host) {
  53. return createSelfSigned("CN=" + host);
  54. }
  55.  
  56. public static KeyStore emptyStore() {
  57. try {
  58. KeyStore ks = KeyStore.getInstance("JKS");
  59.  
  60. // Loading creates the store, can't do anything with it until it's loaded
  61. ks.load(null, IN_MEMORY_PWD.toCharArray());
  62. return ks;
  63. } catch (KeyStoreException | CertificateException | NoSuchAlgorithmException | IOException e) {
  64. LOGGER.error("Cannot create empty keystore.", e);
  65. }
  66. return null;
  67. }
  68.  
  69. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement