📄 keytools.java
字号:
/************************************************************************* * * * EJBCA: The OpenSource Certificate Authority * * * * This software is free software; you can redistribute it and/or * * modify it under the terms of the GNU Lesser General Public * * License as published by the Free Software Foundation; either * * version 2.1 of the License, or any later version. * * * * See terms of license at gnu.org. * * * *************************************************************************/ package se.anatom.ejbca.util;import java.io.*;import java.security.KeyFactory;import java.security.KeyPair;import java.security.KeyPairGenerator;import java.security.KeyStore;import java.security.KeyStoreException;import java.security.NoSuchAlgorithmException;import java.security.NoSuchProviderException;import java.security.PrivateKey;import java.security.PublicKey;import java.security.cert.*;import java.security.interfaces.*;import java.security.spec.*;import java.util.*;import org.apache.log4j.Logger;import org.bouncycastle.asn1.*;import org.bouncycastle.asn1.pkcs.*;import org.bouncycastle.asn1.x509.*;import org.bouncycastle.jce.interfaces.*;/** * Tools to handle common key and keystore operations. * * @version $Id: KeyTools.java,v 1.27 2004/05/31 14:28:51 anatom Exp $ */public class KeyTools { private static Logger log = Logger.getLogger(KeyTools.class); /** * Prevent from creating new KeyTools object */ private KeyTools() { } /** * Generates a keypair * * @param keysize size of keys to generate, typical value is 1024 for RSA keys * * @return KeyPair the generated keypair */ public static KeyPair genKeys(int keysize) throws NoSuchAlgorithmException, NoSuchProviderException { log.debug(">genKeys()"); KeyPairGenerator keygen = KeyPairGenerator.getInstance("RSA", "BC"); keygen.initialize(keysize); KeyPair rsaKeys = keygen.generateKeyPair(); log.debug("Generated " + rsaKeys.getPublic().getAlgorithm() + " keys with length " + ((RSAPrivateKey) rsaKeys.getPrivate()).getPrivateExponent().bitLength()); log.debug("<genKeys()"); return rsaKeys; } // genKeys /** * Creates PKCS12-file that can be imported in IE or Netscape. The alias for the private key is * set to 'privateKey' and the private key password is null. * * @param alias the alias used for the key entry * @param privKey RSA private key * @param cert user certificate * @param cacert CA-certificate or null if only one cert in chain, in that case use 'cert'. * * @return KeyStore containing PKCS12-keystore * * @exception Exception if input parameters are not OK or certificate generation fails */ public static KeyStore createP12(String alias, PrivateKey privKey, X509Certificate cert, X509Certificate cacert) throws IOException, KeyStoreException, CertificateException, NoSuchProviderException, NoSuchAlgorithmException, InvalidKeySpecException { Certificate[] chain; if (cacert == null) { chain = null; } else { chain = new Certificate[1]; chain[0] = cacert; } return createP12(alias, privKey, cert, chain); } // createP12 /** * Creates PKCS12-file that can be imported in IE or Netscape. * The alias for the private key is set to 'privateKey' and the private key password is null. * @param alias the alias used for the key entry * @param privKey RSA private key * @param cert user certificate * @param cacert Collection of X509Certificate, or null if only one cert in chain, in that case use 'cert'. * @param username user's username * @param password user's password * @return KeyStore containing PKCS12-keystore * @exception Exception if input parameters are not OK or certificate generation fails */ static public KeyStore createP12(String alias, PrivateKey privKey, X509Certificate cert, Collection cacerts) throws IOException, KeyStoreException, CertificateException, NoSuchProviderException, NoSuchAlgorithmException, InvalidKeySpecException { Certificate[] chain; if (cacerts == null) chain = null; else { chain = new Certificate[cacerts.size()]; chain = (Certificate[])cacerts.toArray(chain); } return createP12(alias, privKey, cert, chain); } // createP12 /** * Creates PKCS12-file that can be imported in IE or Netscape. The alias for the private key is * set to 'privateKey' and the private key password is null. * * @param alias the alias used for the key entry * @param privKey RSA private key * @param cert user certificate * @param cachain CA-certificate chain or null if only one cert in chain, in that case use 'cert'. * @return KeyStore containing PKCS12-keystore * @exception Exception if input parameters are not OK or certificate generation fails */ public static KeyStore createP12(String alias, PrivateKey privKey, X509Certificate cert, Certificate[] cachain) throws IOException, KeyStoreException, CertificateException, NoSuchProviderException, NoSuchAlgorithmException, InvalidKeySpecException { log.debug(">createP12: alias=" + alias + ", privKey, cert=" + CertTools.getSubjectDN(cert) +", cachain.length=" + ((cachain == null) ? 0 : cachain.length)); // Certificate chain if (cert == null) { throw new IllegalArgumentException("Parameter cert cannot be null."); } int len = 1; if (cachain != null) { len += cachain.length; } Certificate[] chain = new Certificate[len]; // To not get a ClassCastException we need to genereate a real new certificate with BC CertificateFactory cf = CertTools.getCertificateFactory(); chain[0] = (X509Certificate) cf.generateCertificate(new ByteArrayInputStream( cert.getEncoded())); if (cachain != null) { for (int i = 0; i < cachain.length; i++) { X509Certificate tmpcert = (X509Certificate) cf.generateCertificate(new ByteArrayInputStream( cachain[i].getEncoded())); chain[i + 1] = tmpcert; } } if (chain.length > 1) { for (int i = 1; i < chain.length; i++) { X509Certificate cacert = (X509Certificate) cf.generateCertificate(new ByteArrayInputStream( chain[i].getEncoded())); // Set attributes on CA-cert PKCS12BagAttributeCarrier caBagAttr = (PKCS12BagAttributeCarrier) chain[i]; // We constuct a friendly name for the CA, and try with some parts from the DN if they exist. String cafriendly = CertTools.getPartFromDN(CertTools.getSubjectDN(cacert), "CN"); // On the ones below we +i to make it unique, O might not be otherwise if (cafriendly == null) { cafriendly = CertTools.getPartFromDN(CertTools.getSubjectDN(cacert), "O")+i; } if (cafriendly == null) { cafriendly = CertTools.getPartFromDN(CertTools.getSubjectDN(cacert), "OU"+i); } if (cafriendly == null) { cafriendly = "CA_unknown"+i; } caBagAttr.setBagAttribute(PKCSObjectIdentifiers.pkcs_9_at_friendlyName, new DERBMPString(cafriendly)); } } // Set attributes on user-cert PKCS12BagAttributeCarrier certBagAttr = (PKCS12BagAttributeCarrier) chain[0]; certBagAttr.setBagAttribute(PKCSObjectIdentifiers.pkcs_9_at_friendlyName, new DERBMPString(alias)); // in this case we just set the local key id to that of the public key
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -