use of org.kse.crypto.KeyInfo in project keystore-explorer by kaikramer.
the class KeyStoreTableModel method load.
/**
* Load the KeyStoreTableModel with the entries from a KeyStore.
*
* @param history
* KeyStore history
* @throws GeneralSecurityException
* If a KeyStore problem occurs while accessing the KeyStore's
* entries
* @throws CryptoException
* If a crypto problem occurs while accessing the KeyStore's
* entries
*/
public void load(KeyStoreHistory history) throws GeneralSecurityException, CryptoException {
KeyStoreState currentState = history.getCurrentState();
KeyStore keyStore = currentState.getKeyStore();
KeyStoreType type = KeyStoreType.resolveJce(keyStore.getType());
Enumeration<String> aliases = keyStore.aliases();
TreeMap<String, String> sortedAliases = new TreeMap<String, String>(new AliasComparator());
while (aliases.hasMoreElements()) {
String alias = aliases.nextElement();
if (!KeyStoreUtil.isSupportedEntryType(alias, keyStore)) {
continue;
}
sortedAliases.put(alias, alias);
}
data = new Object[sortedAliases.size()][8];
int i = 0;
for (Iterator<Entry<String, String>> itr = sortedAliases.entrySet().iterator(); itr.hasNext(); i++) {
String alias = itr.next().getKey();
String entryType = null;
// Type column
if (KeyStoreUtil.isTrustedCertificateEntry(alias, keyStore)) {
entryType = TRUST_CERT_ENTRY;
} else if (KeyStoreUtil.isKeyPairEntry(alias, keyStore)) {
entryType = KEY_PAIR_ENTRY;
} else {
entryType = KEY_ENTRY;
}
data[i][0] = entryType;
// Lock column - only applies to KeyStores types that actually support passwords for entries
if ((entryType.equals(KEY_PAIR_ENTRY) || entryType.equals(KEY_ENTRY)) && type.hasEntryPasswords()) {
if (currentState.getEntryPassword(alias) != null) {
// Unlocked
data[i][1] = Boolean.FALSE;
} else {
// Locked
data[i][1] = Boolean.TRUE;
}
} else {
// Lock status does not apply
data[i][1] = null;
}
// Expiry status column
Date expiry = getCertificateExpiry(alias, keyStore);
if (expiry == null) {
// No expiry - must be a key entry
data[i][2] = null;
} else if (new Date().after(expiry)) {
// Expired
data[i][2] = Boolean.TRUE;
} else {
// Not expired
data[i][2] = Boolean.FALSE;
}
// Alias column
data[i][3] = alias;
KeyInfo keyInfo = getKeyInfo(alias, keyStore, currentState);
if (keyInfo != null) {
// Algorithm column
data[i][4] = getAlgorithmName(keyInfo);
// Key Size column
data[i][5] = keyInfo.getSize();
}
// Expiry date column
if (expiry != null) {
data[i][6] = expiry;
} else {
// No expiry date - must be a key entry
data[i][6] = null;
}
// Modified date column - only applies to non-PKCS #11/#12 KeyStores
if (!keyStore.getType().equals(KeyStoreType.PKCS12.jce()) && !keyStore.getType().equals(KeyStoreType.PKCS11.jce())) {
data[i][7] = keyStore.getCreationDate(alias);
} else {
data[i][7] = null;
}
}
fireTableDataChanged();
}
use of org.kse.crypto.KeyInfo 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.KeyInfo in project keystore-explorer by kaikramer.
the class KeyPairUtilTest method generateRsaDsaKeys.
@ParameterizedTest
@CsvSource({ "DSA, 512", "DSA, 1024", "RSA, 512", "RSA, 1024", "RSA, 2048" // "RSA, 3072", takes too long
// "RSA, 4096", takes too long
})
public void generateRsaDsaKeys(KeyPairType keyPairType, Integer keySize) throws Exception {
KeyPair keyPair = KeyPairUtil.generateKeyPair(keyPairType, keySize, BC);
PrivateKey privateKey = keyPair.getPrivate();
KeyInfo privateKeyInfo = KeyPairUtil.getKeyInfo(privateKey);
assertEquals(keyPairType.toString(), privateKeyInfo.getAlgorithm());
assertEquals(keySize, privateKeyInfo.getSize());
PublicKey publicKey = keyPair.getPublic();
KeyInfo publicKeyInfo = KeyPairUtil.getKeyInfo(publicKey);
assertEquals(keyPairType.toString(), publicKeyInfo.getAlgorithm());
assertEquals(keySize, publicKeyInfo.getSize());
assertTrue(KeyPairUtil.validKeyPair(privateKey, publicKey));
}
use of org.kse.crypto.KeyInfo in project keystore-explorer by kaikramer.
the class SecretKeyUtilTest method testSecretKeyType.
@Test
private void testSecretKeyType(SecretKeyType secretKeyType) throws CryptoException {
for (int keySize = secretKeyType.minSize(); keySize <= secretKeyType.maxSize(); keySize += secretKeyType.stepSize()) {
SecretKey secretKey = SecretKeyUtil.generateSecretKey(secretKeyType, keySize);
KeyInfo keyInfo = SecretKeyUtil.getKeyInfo(secretKey);
assertThat(secretKeyType.jce()).isEqualToIgnoringCase(keyInfo.getAlgorithm());
assertThat(secretKeyType).isEqualTo(SecretKeyType.resolveJce(keyInfo.getAlgorithm()));
assertThat(keySize).isEqualTo(keyInfo.getSize().intValue());
}
}
use of org.kse.crypto.KeyInfo in project keystore-explorer by kaikramer.
the class DSignCsr method populatePublicKey.
private void populatePublicKey() throws CryptoException {
KeyInfo keyInfo = KeyPairUtil.getKeyInfo(csrPublicKey);
jtfCsrPublicKey.setText(keyInfo.getAlgorithm());
Integer keySize = keyInfo.getSize();
if (keySize != null) {
jtfCsrPublicKey.setText(MessageFormat.format(res.getString("DSignCsr.jtfCsrPublicKey.text"), jtfCsrPublicKey.getText(), "" + keySize));
} else {
jtfCsrPublicKey.setText(MessageFormat.format(res.getString("DSignCsr.jtfCsrPublicKey.text"), jtfCsrPublicKey.getText(), "?"));
}
jtfCsrPublicKey.setCaretPosition(0);
}
Aggregations