Search in sources :

Example 1 with KeyInfo

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();
}
Also used : KeyStoreState(org.kse.utilities.history.KeyStoreState) TreeMap(java.util.TreeMap) KeyStore(java.security.KeyStore) Date(java.util.Date) Entry(java.util.Map.Entry) KeyStoreType(org.kse.crypto.keystore.KeyStoreType) KeyInfo(org.kse.crypto.KeyInfo)

Example 2 with KeyInfo

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);
    }
}
Also used : DSAPrivateKeySpec(java.security.spec.DSAPrivateKeySpec) ECPrivateKey(java.security.interfaces.ECPrivateKey) RSAPrivateKeySpec(java.security.spec.RSAPrivateKeySpec) KeyInfo(org.kse.crypto.KeyInfo) GeneralSecurityException(java.security.GeneralSecurityException) BigInteger(java.math.BigInteger) CryptoException(org.kse.crypto.CryptoException) RSAPrivateKey(java.security.interfaces.RSAPrivateKey) KeyFactory(java.security.KeyFactory)

Example 3 with KeyInfo

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));
}
Also used : KeyPair(java.security.KeyPair) PrivateKey(java.security.PrivateKey) KeyInfo(org.kse.crypto.KeyInfo) PublicKey(java.security.PublicKey) CsvSource(org.junit.jupiter.params.provider.CsvSource) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 4 with KeyInfo

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());
    }
}
Also used : SecretKey(javax.crypto.SecretKey) KeyInfo(org.kse.crypto.KeyInfo) Test(org.junit.jupiter.api.Test)

Example 5 with KeyInfo

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);
}
Also used : BigInteger(java.math.BigInteger) KeyInfo(org.kse.crypto.KeyInfo)

Aggregations

KeyInfo (org.kse.crypto.KeyInfo)15 BigInteger (java.math.BigInteger)10 DefaultMutableTreeNode (javax.swing.tree.DefaultMutableTreeNode)3 CryptoException (org.kse.crypto.CryptoException)3 GeneralSecurityException (java.security.GeneralSecurityException)2 KeyFactory (java.security.KeyFactory)2 DSAParams (java.security.interfaces.DSAParams)2 DSAPrivateKey (java.security.interfaces.DSAPrivateKey)2 DSAPublicKey (java.security.interfaces.DSAPublicKey)2 RSAPrivateKey (java.security.interfaces.RSAPrivateKey)2 RSAPublicKey (java.security.interfaces.RSAPublicKey)2 Date (java.util.Date)2 SecretKeyType (org.kse.crypto.secretkey.SecretKeyType)2 KeyPair (java.security.KeyPair)1 KeyStore (java.security.KeyStore)1 PrivateKey (java.security.PrivateKey)1 PublicKey (java.security.PublicKey)1 CertificateEncodingException (java.security.cert.CertificateEncodingException)1 X509Certificate (java.security.cert.X509Certificate)1 ECPrivateKey (java.security.interfaces.ECPrivateKey)1