use of org.kse.crypto.CryptoException in project keystore-explorer by kaikramer.
the class KeyPairUtil method generateKeyPair.
/**
* Generate a key pair.
*
* @param keyPairType
* Key pair type to generate
* @param keySize
* Key size of key pair
* @return A keypair
* @param provider
* Crypto provider used for key generation
* @throws CryptoException
* If there was a problem generating the key pair
*/
public static KeyPair generateKeyPair(KeyPairType keyPairType, int keySize, Provider provider) throws CryptoException {
try {
// Get a key pair generator
KeyPairGenerator keyPairGen = null;
if (provider != null) {
keyPairGen = KeyPairGenerator.getInstance(keyPairType.jce(), provider);
} else {
// Always use BC provider for RSA
if (keyPairType == RSA) {
keyPairGen = KeyPairGenerator.getInstance(keyPairType.jce(), BOUNCY_CASTLE.jce());
} else {
// Use default provider for DSA
keyPairGen = KeyPairGenerator.getInstance(keyPairType.jce());
}
}
// Create a SecureRandom
SecureRandom rand = SecureRandom.getInstance("SHA1PRNG");
// Initialise key pair generator with key strength and randomness
keyPairGen.initialize(keySize, rand);
// Generate and return the key pair
KeyPair keyPair = keyPairGen.generateKeyPair();
return keyPair;
} catch (GeneralSecurityException ex) {
throw new CryptoException(MessageFormat.format(res.getString("NoGenerateKeypair.exception.message"), keyPairType), ex);
}
}
use of org.kse.crypto.CryptoException in project keystore-explorer by kaikramer.
the class KeyPairUtil method getKeyInfo.
/**
* Get the information about the supplied private key.
*
* @param privateKey
* The private key
* @return Key information
* @throws CryptoException
* If there is a problem getting the information
*/
public static KeyInfo getKeyInfo(PrivateKey privateKey) throws CryptoException {
try {
String algorithm = privateKey.getAlgorithm();
if (algorithm.equals(RSA.jce())) {
if (privateKey instanceof RSAPrivateKey) {
// Using default provider does not work for BKS and UBER resident private keys
KeyFactory keyFact = KeyFactory.getInstance(algorithm, BOUNCY_CASTLE.jce());
RSAPrivateKeySpec keySpec = keyFact.getKeySpec(privateKey, RSAPrivateKeySpec.class);
BigInteger modulus = keySpec.getModulus();
return new KeyInfo(ASYMMETRIC, algorithm, modulus.toString(2).length());
} else {
return new KeyInfo(ASYMMETRIC, algorithm, 0);
}
} else if (algorithm.equals(DSA.jce())) {
// Use SUN (DSA key spec not implemented for BC)
KeyFactory keyFact = KeyFactory.getInstance(algorithm);
DSAPrivateKeySpec keySpec = keyFact.getKeySpec(privateKey, DSAPrivateKeySpec.class);
BigInteger prime = keySpec.getP();
return new KeyInfo(ASYMMETRIC, algorithm, prime.toString(2).length());
} else if (algorithm.equals(EC.jce()) || algorithm.equals(ECDSA.jce())) {
ECPrivateKey pubk = (ECPrivateKey) privateKey;
int size = pubk.getParams().getOrder().bitLength();
return new KeyInfo(ASYMMETRIC, algorithm, size);
}
// size unknown
return new KeyInfo(ASYMMETRIC, algorithm);
} catch (GeneralSecurityException ex) {
throw new CryptoException(res.getString("NoPrivateKeysize.exception.message"), ex);
}
}
use of org.kse.crypto.CryptoException in project keystore-explorer by kaikramer.
the class KeyStoreUtil method copy.
/**
* Copy a KeyStore.
*
* @param keyStore
* KeyStore to copy
* @return Copy
* @throws CryptoException
* Problem encountered copying the KeyStore
*/
public static KeyStore copy(KeyStore keyStore) throws CryptoException {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
try {
char[] emptyPassword = {};
keyStore.store(baos, emptyPassword);
KeyStore theCopy = KeyStoreUtil.create(KeyStoreType.resolveJce(keyStore.getType()));
theCopy.load(new ByteArrayInputStream(baos.toByteArray()), emptyPassword);
return theCopy;
} catch (CryptoException ex) {
throw new CryptoException(res.getString("NoCopyKeyStore.exception.message"), ex);
} catch (GeneralSecurityException ex) {
throw new CryptoException(res.getString("NoCopyKeyStore.exception.message"), ex);
} catch (IllegalStateException ex) {
throw new CryptoException(res.getString("NoCopyKeyStore.exception.message"), ex);
} catch (IOException ex) {
throw new CryptoException(res.getString("NoCopyKeyStore.exception.message"), ex);
}
}
use of org.kse.crypto.CryptoException in project keystore-explorer by kaikramer.
the class DImportKeyPairPkcs8 method certificateDetailsPressed.
private void certificateDetailsPressed() {
try {
X509Certificate[] certs = loadCertificates();
if ((certs != null) && (certs.length != 0)) {
String path = new File(jtfCertificatePath.getText()).getName();
DViewCertificate dViewCertificate = new DViewCertificate(this, MessageFormat.format(res.getString("DImportKeyPairPkcs8.ViewCertificateDetails.Title"), path), certs, null, DViewCertificate.NONE);
dViewCertificate.setLocationRelativeTo(this);
dViewCertificate.setVisible(true);
}
} catch (CryptoException ex) {
DError.displayError(this, ex);
}
}
use of org.kse.crypto.CryptoException in project keystore-explorer by kaikramer.
the class DProperties method createTrustedCertificatesNodes.
private void createTrustedCertificatesNodes(DefaultMutableTreeNode parentNode) throws CryptoException {
try {
KeyStore keyStore = currentState.getKeyStore();
TreeSet<String> aliases = getAliasesInAlphaOrder();
DefaultMutableTreeNode trustCertsNode = new DefaultMutableTreeNode(res.getString("DProperties.properties.TrustedCertificates"));
parentNode.add(trustCertsNode);
boolean trustCertsPresent = false;
for (String alias : aliases) {
if (KeyStoreUtil.isTrustedCertificateEntry(alias, keyStore)) {
createTrustedCertificateNodes(trustCertsNode, alias);
trustCertsPresent = true;
}
}
if (!trustCertsPresent) {
DefaultMutableTreeNode emptyTrustCertsNode = new DefaultMutableTreeNode(res.getString("DProperties.properties.None"));
trustCertsNode.add(emptyTrustCertsNode);
}
} catch (KeyStoreException ex) {
throw new CryptoException(res.getString("DProperties.NoGetProperties.exception.message"), ex);
}
}
Aggregations