📄 keytools.java.14
字号:
/************************************************************************* * * * 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 org.ejbca.util;import java.io.ByteArrayInputStream;import java.io.IOException;import java.security.InvalidAlgorithmParameterException;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.Certificate;import java.security.cert.CertificateException;import java.security.cert.CertificateFactory;import java.security.cert.X509Certificate;import java.security.interfaces.RSAPublicKey;import java.security.spec.InvalidKeySpecException;import java.security.spec.PKCS8EncodedKeySpec;import java.util.ArrayList;import java.util.Collection;import org.apache.commons.lang.StringUtils;import org.apache.log4j.Logger;import org.bouncycastle.asn1.ASN1InputStream;import org.bouncycastle.asn1.ASN1Sequence;import org.bouncycastle.asn1.DERBMPString;import org.bouncycastle.asn1.pkcs.PKCSObjectIdentifiers;import org.bouncycastle.asn1.x509.SubjectKeyIdentifier;import org.bouncycastle.asn1.x509.SubjectPublicKeyInfo;import org.bouncycastle.jce.interfaces.PKCS12BagAttributeCarrier;import org.bouncycastle.jce.provider.JCEECPublicKey;import org.ejbca.core.model.ca.catoken.CATokenConstants;/** * Tools to handle common key and keystore operations. * * @version $Id: KeyTools.java.14,v 1.1 2007/01/16 11:46:44 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 keyspec specification of keys to generate, typical value is 1024 for RSA keys or prime192v1 for ECDSA keys * @param keyalg algorithm of keys to generate, typical value is RSA or ECDSA, see org.ejbca.core.model.ca.catoken.CATokenConstants.KEYALGORITHM_XX * * @see org.ejbca.core.model.ca.catoken.CATokenConstants * @see org.bouncycastle.asn1.x9.X962NamedCurves * @see org.bouncycastle.asn1.nist.NISTNamedCurves * @see org.bouncycastle.asn1.sec.SECNamedCurves * * @return KeyPair the generated keypair * @throws InvalidAlgorithmParameterException */ public static KeyPair genKeys(String keySpec, String keyAlg) throws NoSuchAlgorithmException, NoSuchProviderException, InvalidAlgorithmParameterException { if (log.isDebugEnabled()) { log.debug(">genKeys("+keySpec+", "+keyAlg+")"); } KeyPairGenerator keygen = KeyPairGenerator.getInstance(keyAlg, "BC"); if (StringUtils.equals(keyAlg, CATokenConstants.KEYALGORITHM_ECDSA)) { throw new NoSuchAlgorithmException("ECDSA requires that you run at least java 5."); } else { // RSA keys int keysize = Integer.parseInt(keySpec); keygen.initialize(keysize); } KeyPair keys = keygen.generateKeyPair(); if (log.isDebugEnabled()) { PublicKey pk = keys.getPublic(); int len = getKeyLength(pk); log.debug("Generated " + keys.getPublic().getAlgorithm() + " keys with length " + len); log.debug("<genKeys()"); } return keys; } // genKeys /** * Gets the key length of supported keys * @param priv PrivateKey to check * @return -1 if key is unsupported, otherwise a number >= 0. 0 usually means the length can not be calculated, * for example if the key is en EC key and the "implicitlyCA" encoding is used. */ public static int getKeyLength(PublicKey pk) { int len = -1; if (pk instanceof RSAPublicKey) { RSAPublicKey rsapub = (RSAPublicKey) pk; len = rsapub.getModulus().bitLength(); } else if (pk instanceof JCEECPublicKey) { JCEECPublicKey ecpriv = (JCEECPublicKey) pk; org.bouncycastle.jce.spec.ECParameterSpec spec = ecpriv.getParameters(); if (spec != null) { len = spec.getN().bitLength(); } else { // We support the key, but we don't know the key length len = 0; } } return len; } /** * 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 */ public static 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] = 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(
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -