如何创建一个PKCS12密钥库?

huangapple go评论92阅读模式
英文:

How to create a create a PKCS12 keystore?

问题

我想用Java生成一个PKCS12密钥库。我知道可以使用keytool,类似于以下内容:

  1. keytool -genkeypair -alias senderKeyPair -keyalg RSA -keysize 2048 \
  2. -dname "CN=Baeldung" -validity 365 -storetype PKCS12 \
  3. -keystore sender_keystore.p12 -storepass changeit

但我需要在Java内部执行此操作。我已经搜索过是否可能,但没有找到相关信息。有任何想法吗?

我非常感谢您能提供的任何帮助。

英文:

I want to generate a PKCS12 keystore with Java. I know that I can use the keytool with something like that:

  1. keytool -genkeypair -alias senderKeyPair -keyalg RSA -keysize 2048 \
  2. -dname "CN=Baeldung" -validity 365 -storetype PKCS12 \
  3. -keystore sender_keystore.p12 -storepass changeit

But I need to do this stuff internally in Java. I've searched if it is possible but I didn't find anything about it. Any idea?

I really appreciate any help you can provide.

答案1

得分: 5

  1. import java.io.FileInputStream;
  2. import java.io.FileOutputStream;
  3. import java.security.Key;
  4. import java.security.KeyStore;
  5. import java.security.cert.X509Certificate;
  6. public class Main {
  7. public static void main(String[] args) {
  8. System.out.println("generate keystore type PKCS12 with RSA keys");
  9. System.out.println("source: https://www.pixelstech.net/article/1420427307-Different-types-of-keystore-in-Java----PKCS12");
  10. String keystoreType = "PKCS12";
  11. String keystoreFilename = "sender_keystore.p12";
  12. String keystorePassword = "changeit";
  13. String alias = "senderKeyPair";
  14. String cnString = "CN=Baeldung";
  15. int rsaKeylength = 2048;
  16. String certificateSignatureAlgorithm = "SHA1WithRSA";
  17. int certificateValidityDays = 365;
  18. try {
  19. KeyStore keyStore = KeyStore.getInstance(keystoreType);
  20. keyStore.load(null, null);
  21. keyStore.store(new FileOutputStream(keystoreFilename), keystorePassword.toCharArray());
  22. } catch (Exception ex) {
  23. ex.printStackTrace();
  24. }
  25. try {
  26. KeyStore keyStore = KeyStore.getInstance(keystoreType);
  27. keyStore.load(new FileInputStream(keystoreFilename), keystorePassword.toCharArray());
  28. CertAndKeyGen gen = new CertAndKeyGen("RSA", certificateSignatureAlgorithm);
  29. gen.generate(rsaKeylength);
  30. Key key = gen.getPrivateKey();
  31. X509Certificate cert = gen.getSelfCertificate(new X500Name(cnString), (long) certificateValidityDays * 24 * 3600);
  32. X509Certificate[] chain = new X509Certificate[1];
  33. chain[0] = cert;
  34. keyStore.setKeyEntry(alias, key, keystorePassword.toCharArray(), chain);
  35. keyStore.store(new FileOutputStream(keystoreFilename), keystorePassword.toCharArray());
  36. } catch (Exception ex) {
  37. ex.printStackTrace();
  38. }
  39. System.out.println("keystore generated in file: " + keystoreFilename +
  40. " keystore password: " + keystorePassword +
  41. " alias: " + alias);
  42. }
  43. }
英文:

The below code will generate a RSA keypair, generates a self signed certificate and store the private key and the cartificate in a PKCS#12 keystore with the given credentials (alias, password etc).

Please keep in mind that my program is running on Java 11 and that it is using dependencies that you should avoid to use as they can go away without any further warning in future versions (e.g. sun.security.x509.X500Name).

The "CertAndKeyGen.java" class is taken from the Alvin Alexander project and the main parts of my program are taken from an article here: https://www.pixelstech.net/article/1420427307-Different-types-of-keystore-in-Java----PKCS12, so all credits go to the original authors!

A more smart version is available with usage of a 3rd party library (Bouncy Castle) to avoid the sun-dependencies.

Security warning: the code does not have any exception handling and if for educational purpose only.

output:

  1. generate keystore type PKCS12 with RSA keys
  2. source: https://www.pixelstech.net/article/1420427307-Different-types-of-keystore-in-Java----PKCS12
  3. keystore generated in file: sender_keystore.p12 keystore password: changeit alias: senderKeyPair

code Main.java:

  1. import sun.security.x509.X500Name;
  2. import java.io.FileInputStream;
  3. import java.io.FileOutputStream;
  4. import java.security.Key;
  5. import java.security.KeyStore;
  6. import java.security.cert.X509Certificate;
  7. public class Main {
  8. public static void main(String[] args) {
  9. System.out.println("generate keystore type PKCS12 with RSA keys");
  10. System.out.println("source: https://www.pixelstech.net/article/1420427307-Different-types-of-keystore-in-Java----PKCS12");
  11. String keystoreType = "PKCS12";
  12. String keystoreFilename = "sender_keystore.p12";
  13. String keystorePassword = "changeit";
  14. String alias = "senderKeyPair";
  15. String cnString = "CN=Baeldung";
  16. int rsaKeylength = 2048;
  17. String certificateSignatureAlgorithm = "SHA1WithRSA";
  18. int certificateValidityDays = 365;
  19. /*
  20. Create PKCS12 keystore
  21. Before storing an entry into a PKCS12 keystore, the keystore has to be loaded first. This means we have to have a keystore created first.
  22. The simplest way of creating a PKCS12 keystore is :
  23. */
  24. try {
  25. KeyStore keyStore = KeyStore.getInstance(keystoreType);
  26. keyStore.load(null, null);
  27. keyStore.store(new FileOutputStream(keystoreFilename), keystorePassword.toCharArray());
  28. } catch (Exception ex) {
  29. ex.printStackTrace();
  30. }
  31. /*
  32. Store private key
  33. The private key and its associated certificate chain can be stored in PKCS12 keystore.
  34. The keystore contains private keys and certificates can be used in SSL communications across the web.
  35. A RSA private key is generated with the CertAndKeyGen and the associated certificate is also generated.
  36. Then the key entry is stored in the keyStore by calling keyStore.setEntry().
  37. Don't forget to save the keyStore by calling keyStore.store(), otherwise the entry will be lost when the program exits.
  38. */
  39. try {
  40. KeyStore keyStore = KeyStore.getInstance(keystoreType);
  41. keyStore.load(new FileInputStream(keystoreFilename), keystorePassword.toCharArray());
  42. //keyStore.load(null, null);
  43. CertAndKeyGen gen = new CertAndKeyGen("RSA", certificateSignatureAlgorithm);
  44. gen.generate(rsaKeylength);
  45. Key key = gen.getPrivateKey();
  46. X509Certificate cert = gen.getSelfCertificate(new X500Name(cnString), (long) certificateValidityDays * 24 * 3600);
  47. X509Certificate[] chain = new X509Certificate[1];
  48. chain[0] = cert;
  49. keyStore.setKeyEntry(alias, key, keystorePassword.toCharArray(), chain);
  50. keyStore.store(new FileOutputStream(keystoreFilename), keystorePassword.toCharArray());
  51. } catch (Exception ex) {
  52. ex.printStackTrace();
  53. }
  54. System.out.println("keystore generated in file: " + keystoreFilename +
  55. " keystore password: " + keystorePassword +
  56. " alias: " + alias);
  57. }
  58. }

code CertAndKeyGen.java:

  1. /*
  2. * Copyright (c) 1996, 2012, Oracle and/or its affiliates. All rights reserved.
  3. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  4. *
  5. * This code is free software; you can redistribute it and/or modify it
  6. * under the terms of the GNU General Public License version 2 only, as
  7. * published by the Free Software Foundation. Oracle designates this
  8. * particular file as subject to the "Classpath" exception as provided
  9. * by Oracle in the LICENSE file that accompanied this code.
  10. *
  11. * This code is distributed in the hope that it will be useful, but WITHOUT
  12. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  14. * version 2 for more details (a copy is included in the LICENSE file that
  15. * accompanied this code).
  16. *
  17. * You should have received a copy of the GNU General Public License version
  18. * 2 along with this work; if not, write to the Free Software Foundation,
  19. * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20. *
  21. * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22. * or visit www.oracle.com if you need additional information or have any
  23. * questions.
  24. */
  25. // package sun.security.tools.keytool;
  26. import sun.security.pkcs10.PKCS10;
  27. import sun.security.x509.*;
  28. import java.io.IOException;
  29. import java.security.*;
  30. import java.security.cert.CertificateEncodingException;
  31. import java.security.cert.CertificateException;
  32. import java.security.cert.X509Certificate;
  33. import java.util.Date;
  34. // source: https://alvinalexander.com/java/jwarehouse/openjdk-8/jdk/src/share/classes/sun/security/tools/keytool/CertAndKeyGen.java.shtml
  35. /**
  36. * Generate a pair of keys, and provide access to them. This class is
  37. * provided primarily for ease of use.
  38. *
  39. * <P>This provides some simple certificate management functionality.
  40. * Specifically, it allows you to create self-signed X.509 certificates
  41. * as well as PKCS 10 based certificate signing requests.
  42. *
  43. * <P>Keys for some public key signature algorithms have algorithm
  44. * parameters, such as DSS/DSA. Some sites' Certificate Authorities
  45. * adopt fixed algorithm parameters, which speeds up some operations
  46. * including key generation and signing. <em>At this time, this interface
  47. * does not provide a way to provide such algorithm parameters, e.g.
  48. * by providing the CA certificate which includes those parameters.</em>
  49. *
  50. * <P>Also, note that at this time only signature-capable keys may be
  51. * acquired through this interface. Diffie-Hellman keys, used for secure
  52. * key exchange, may be supported later.
  53. *
  54. * @author David Brownell
  55. * @author Hemma Prafullchandra
  56. * @see PKCS10
  57. * @see X509CertImpl
  58. */
  59. public final class CertAndKeyGen {
  60. /**
  61. * Creates a CertAndKeyGen object for a particular key type
  62. * and signature algorithm.
  63. *
  64. * @param keyType type of key, e.g. "RSA", "DSA"
  65. * @param sigAlg name of the signature algorithm, e.g. "MD5WithRSA",
  66. * "MD2WithRSA", "SHAwithDSA".
  67. * @exception NoSuchAlgorithmException on unrecognized algorithms.
  68. */
  69. public CertAndKeyGen(String keyType, String sigAlg)
  70. throws NoSuchAlgorithmException
  71. {
  72. keyGen = KeyPairGenerator.getInstance(keyType);
  73. this.sigAlg = sigAlg;
  74. }
  75. /**
  76. * Creates a CertAndKeyGen object for a particular key type,
  77. * signature algorithm, and provider.
  78. *
  79. * @param keyType type of key, e.g. "RSA", "DSA"
  80. * @param sigAlg name of the signature algorithm, e.g. "MD5WithRSA",
  81. * "MD2WithRSA", "SHAwithDSA".
  82. * @param providerName name of the provider
  83. * @exception NoSuchAlgorithmException on unrecognized algorithms.
  84. * @exception NoSuchProviderException on unrecognized providers.
  85. */
  86. public CertAndKeyGen(String keyType, String sigAlg, String providerName)
  87. throws NoSuchAlgorithmException, NoSuchProviderException
  88. {
  89. if (providerName == null) {
  90. keyGen = KeyPairGenerator.getInstance(keyType);
  91. } else {
  92. try {
  93. keyGen = KeyPairGenerator.getInstance(keyType, providerName);
  94. } catch (Exception e) {
  95. // try first available provider instead
  96. keyGen = KeyPairGenerator.getInstance(keyType);
  97. }
  98. }
  99. this.sigAlg = sigAlg;
  100. }
  101. /**
  102. * Sets the source of random numbers used when generating keys.
  103. * If you do not provide one, a system default facility is used.
  104. * You may wish to provide your own source of random numbers
  105. * to get a reproducible sequence of keys and signatures, or
  106. * because you may be able to take advantage of strong sources
  107. * of randomness/entropy in your environment.
  108. */
  109. public void setRandom (SecureRandom generator)
  110. {
  111. prng = generator;
  112. }
  113. // want "public void generate (X509Certificate)" ... inherit DSA/D-H param
  114. /**
  115. * Generates a random public/private key pair, with a given key
  116. * size. Different algorithms provide different degrees of security
  117. * for the same key size, because of the "work factor" involved in
  118. * brute force attacks. As computers become faster, it becomes
  119. * easier to perform such attacks. Small keys are to be avoided.
  120. *
  121. * <P>Note that not all values of "keyBits" are valid for all
  122. * algorithms, and not all public key algorithms are currently
  123. * supported for use in X.509 certificates. If the algorithm
  124. * you specified does not produce X.509 compatible keys, an
  125. * invalid key exception is thrown.
  126. *
  127. * @param keyBits the number of bits in the keys.
  128. * @exception InvalidKeyException if the environment does not
  129. * provide X.509 public keys for this signature algorithm.
  130. */
  131. public void generate (int keyBits)
  132. throws InvalidKeyException
  133. {
  134. KeyPair pair;
  135. try {
  136. if (prng == null) {
  137. prng = new SecureRandom();
  138. }
  139. keyGen.initialize(keyBits, prng);
  140. pair = keyGen.generateKeyPair();
  141. } catch (Exception e) {
  142. throw new IllegalArgumentException(e.getMessage());
  143. }
  144. publicKey = pair.getPublic();
  145. privateKey = pair.getPrivate();
  146. // publicKey's format must be X.509 otherwise
  147. // the whole CertGen part of this class is broken.
  148. if (!"X.509".equalsIgnoreCase(publicKey.getFormat())) {
  149. throw new IllegalArgumentException("publicKey's is not X.509, but "
  150. + publicKey.getFormat());
  151. }
  152. }
  153. /**
  154. * Returns the public key of the generated key pair if it is of type
  155. * <code>X509Key, or null if the public key is of a different type.
  156. *
  157. * XXX Note: This behaviour is needed for backwards compatibility.
  158. * What this method really should return is the public key of the
  159. * generated key pair, regardless of whether or not it is an instance of
  160. * <code>X509Key. Accordingly, the return type of this method
  161. * should be <code>PublicKey.
  162. */
  163. public X509Key getPublicKey()
  164. {
  165. if (!(publicKey instanceof X509Key)) {
  166. return null;
  167. }
  168. return (X509Key)publicKey;
  169. }
  170. /**
  171. * Always returns the public key of the generated key pair. Used
  172. * by KeyTool only.
  173. *
  174. * The publicKey is not necessarily to be an instance of
  175. * X509Key in some JCA/JCE providers, for example SunPKCS11.
  176. */
  177. public PublicKey getPublicKeyAnyway() {
  178. return publicKey;
  179. }
  180. /**
  181. * Returns the private key of the generated key pair.
  182. *
  183. * <P>Be extremely careful when handling private keys.
  184. * When private keys are not kept secret, they lose their ability
  185. * to securely authenticate specific entities ... that is a huge
  186. * security risk!</em>
  187. */
  188. public PrivateKey getPrivateKey ()
  189. {
  190. return privateKey;
  191. }
  192. /**
  193. * Returns a self-signed X.509v3 certificate for the public key.
  194. * The certificate is immediately valid. No extensions.
  195. *
  196. * <P>Such certificates normally are used to identify a "Certificate
  197. * Authority" (CA). Accordingly, they will not always be accepted by
  198. * other parties. However, such certificates are also useful when
  199. * you are bootstrapping your security infrastructure, or deploying
  200. * system prototypes.
  201. *
  202. * @param myname X.500 name of the subject (who is also the issuer)
  203. * @param firstDate the issue time of the certificate
  204. * @param validity how long the certificate should be valid, in seconds
  205. * @exception CertificateException on certificate handling errors.
  206. * @exception InvalidKeyException on key handling errors.
  207. * @exception SignatureException on signature handling errors.
  208. * @exception NoSuchAlgorithmException on unrecognized algorithms.
  209. * @exception NoSuchProviderException on unrecognized providers.
  210. */
  211. public X509Certificate getSelfCertificate (
  212. X500Name myname, Date firstDate, long validity)
  213. throws CertificateException, InvalidKeyException, SignatureException,
  214. NoSuchAlgorithmException, NoSuchProviderException
  215. {
  216. return getSelfCertificate(myname, firstDate, validity, null);
  217. }
  218. // Like above, plus a CertificateExtensions argument, which can be null.
  219. public X509Certificate getSelfCertificate (X500Name myname, Date firstDate,
  220. long validity, CertificateExtensions ext)
  221. throws CertificateException, InvalidKeyException, SignatureException,
  222. NoSuchAlgorithmException, NoSuchProviderException
  223. {
  224. X509CertImpl cert;
  225. Date lastDate;
  226. try {
  227. lastDate = new Date ();
  228. lastDate.setTime (firstDate.getTime () + validity * 1000);
  229. CertificateValidity interval =
  230. new CertificateValidity(firstDate,lastDate);
  231. X509CertInfo info = new X509CertInfo();
  232. // Add all mandatory attributes
  233. info.set(X509CertInfo.VERSION,
  234. new CertificateVersion(CertificateVersion.V3));
  235. info.set(X509CertInfo.SERIAL_NUMBER, new CertificateSerialNumber(
  236. new java.util.Random().nextInt() & 0x7fffffff));
  237. AlgorithmId algID = AlgorithmId.get(sigAlg);
  238. info.set(X509CertInfo.ALGORITHM_ID,
  239. new CertificateAlgorithmId(algID));
  240. info.set(X509CertInfo.SUBJECT, myname);
  241. info.set(X509CertInfo.KEY, new CertificateX509Key(publicKey));
  242. info.set(X509CertInfo.VALIDITY, interval);
  243. info.set(X509CertInfo.ISSUER, myname);
  244. if (ext != null) info.set(X509CertInfo.EXTENSIONS, ext);
  245. cert = new X509CertImpl(info);
  246. cert.sign(privateKey, this.sigAlg);
  247. return (X509Certificate)cert;
  248. } catch (IOException e) {
  249. throw new CertificateEncodingException("getSelfCert: " +
  250. e.getMessage());
  251. }
  252. }
  253. // Keep the old method
  254. public X509Certificate getSelfCertificate (X500Name myname, long validity)
  255. throws CertificateException, InvalidKeyException, SignatureException,
  256. NoSuchAlgorithmException, NoSuchProviderException
  257. {
  258. return getSelfCertificate(myname, new Date(), validity);
  259. }
  260. /**
  261. * Returns a PKCS #10 certificate request. The caller uses either
  262. * <code>PKCS10.print or PKCS10.toByteArray
  263. * operations on the result, to get the request in an appropriate
  264. * transmission format.
  265. *
  266. * <P>PKCS #10 certificate requests are sent, along with some proof
  267. * of identity, to Certificate Authorities (CAs) which then issue
  268. * X.509 public key certificates.
  269. *
  270. * @param myname X.500 name of the subject
  271. * @exception InvalidKeyException on key handling errors.
  272. * @exception SignatureException on signature handling errors.
  273. */
  274. public PKCS10 getCertRequest (X500Name myname)
  275. throws InvalidKeyException, SignatureException
  276. {
  277. PKCS10 req = new PKCS10 (publicKey);
  278. try {
  279. Signature signature = Signature.getInstance(sigAlg);
  280. signature.initSign (privateKey);
  281. req.encodeAndSign(myname, signature);
  282. } catch (CertificateException e) {
  283. throw new SignatureException (sigAlg + " CertificateException");
  284. } catch (IOException e) {
  285. throw new SignatureException (sigAlg + " IOException");
  286. } catch (NoSuchAlgorithmException e) {
  287. // "can't happen"
  288. throw new SignatureException (sigAlg + " unavailable?");
  289. }
  290. return req;
  291. }
  292. private SecureRandom prng;
  293. private String sigAlg;
  294. private KeyPairGenerator keyGen;
  295. private PublicKey publicKey;
  296. private PrivateKey privateKey;
  297. }

huangapple
  • 本文由 发表于 2020年10月20日 17:18:23
  • 转载请务必保留本文链接:https://go.coder-hub.com/64442156.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定