Example usage for java.security KeyStore setEntry

List of usage examples for java.security KeyStore setEntry

Introduction

In this page you can find the example usage for java.security KeyStore setEntry.

Prototype

public final void setEntry(String alias, Entry entry, ProtectionParameter protParam) throws KeyStoreException 

Source Link

Document

Saves a keystore Entry under the specified alias.

Usage

From source file:org.teknux.jettybootstrap.keystore.JettyKeystore.java

private static KeyStore createKeyStore(PrivateKey privateKey, Certificate certificate, String alias,
        String password) throws JettyKeystoreException {
    try {/*from   ww  w  .j  a v  a 2s .com*/
        KeyStore keyStore = KeyStore.getInstance(KeyStore.getDefaultType());
        keyStore.load(null, null);

        PrivateKeyEntry privateKeyEntry = new PrivateKeyEntry(privateKey, new Certificate[] { certificate });
        keyStore.setEntry(alias, privateKeyEntry, new KeyStore.PasswordProtection(password.toCharArray()));

        return keyStore;
    } catch (KeyStoreException | NoSuchAlgorithmException | CertificateException | IOException e) {
        throw new JettyKeystoreException(JettyKeystoreException.ERROR_CREATE_KEYSTORE,
                "Can not create keystore file", e);
    }
}

From source file:org.texai.x509.X509Utils.java

/** Copies the given keystore from the .uber format to the .jceks format.
 *
 * @param uberKeyStorePath the .uber keystore path
 * @param uberKeyStorePassword the .uber keystore password
 * @param jceksKeyStorePath the .jceks keystore path
 * @param jceksKeyStorePassword the .jceks keystore password
 * @throws KeyStoreException if no Provider supports a KeyStoreSpi implementation for the specified type
 * @throws IOException if there is an I/O or format problem with the keystore data,
 * if a password is required but not given, or if the given password was incorrect
 * @throws NoSuchAlgorithmException if the algorithm used to check the integrity of the keystore cannot be found
 * @throws CertificateException if any of the certificates in the keystore could not be loaded
 * @throws NoSuchProviderException if the cryptography provider cannot be found
 * @throws UnrecoverableEntryException if the keystore entry cannot be recovered with the provided password and alias
 *///w w  w. jav a2s . c o  m
public static synchronized void copyKeyStoreUberToJceks(final String uberKeyStorePath,
        final char[] uberKeyStorePassword, final String jceksKeyStorePath, final char[] jceksKeyStorePassword)
        throws KeyStoreException, IOException, NoSuchAlgorithmException, CertificateException,
        NoSuchProviderException, UnrecoverableEntryException {
    //Preconditions
    assert uberKeyStorePath != null : "uberKeyStorePath must not be null";
    assert !uberKeyStorePath.isEmpty() : "uberKeyStorePath must not be empty";
    assert uberKeyStorePath.endsWith(".uber") : "uber keystore file extension must be .uber";
    assert uberKeyStorePassword != null : "uberKeyStorePassword must not be null";
    assert jceksKeyStorePath != null : "jceksKeyStorePath must not be null";
    assert !jceksKeyStorePath.isEmpty() : "jceksKeyStorePath must not be empty";
    assert jceksKeyStorePath.endsWith(".jceks") : "jceks keystore file extension must be .jceks";
    assert uberKeyStorePassword != null : "uberKeyStorePassword must not be null";

    LOGGER.info("copying keystore contents of " + uberKeyStorePath + " to " + jceksKeyStorePath);
    final KeyStore uberKeyStore = findOrCreateUberKeyStore(uberKeyStorePath, uberKeyStorePassword);
    final KeyStore jceksKeyStore = findOrCreateJceksKeyStore(jceksKeyStorePath, jceksKeyStorePassword);
    final Enumeration<String> aliases_enumeration = uberKeyStore.aliases();
    final PasswordProtection uberPasswordProtection = new PasswordProtection(uberKeyStorePassword);
    final PasswordProtection jceksPasswordProtection = new PasswordProtection(jceksKeyStorePassword);
    while (aliases_enumeration.hasMoreElements()) {
        final String alias = aliases_enumeration.nextElement();
        final KeyStore.Entry entry = uberKeyStore.getEntry(alias, uberPasswordProtection);
        assert entry != null;
        jceksKeyStore.setEntry(alias, entry, jceksPasswordProtection);
        LOGGER.info("  copied entry: " + alias);
    }
    jceksKeyStore.store(new FileOutputStream(jceksKeyStorePath), jceksKeyStorePassword);
}

From source file:org.wildfly.security.keystore.ModifyTrackingKeyStoreTest.java

private void addSecretKey(final String alias, final KeyStore keyStore) throws GeneralSecurityException {
    SecretKey key = getSecretKey();

    keyStore.setEntry(alias, new SecretKeyEntry(key), protectionParameter);
}

From source file:org.xwiki.contrib.encryption.internal.DefaultEncryptionTool.java

/**
 * Store the encryption key./*from   w  w w  .  j  a  v a  2s  .  c o m*/
 * 
 * @param ks Keystore where the key should be stored
 */
private void storeEncryptionKey(KeyStore ks) {
    try {
        logger.debug("Start storing password");
        String storePassword = KEYSTORE_PASSWORD;
        String protection = ENCRYPTION_KEY_PROTECTION;
        SecretKeySpec key = generateRandomKey();
        logger.debug("Encryption key generated : " + key);
        KeyStore.SecretKeyEntry skEntry = new KeyStore.SecretKeyEntry(key);
        ks.setEntry("encryptionKey", skEntry, new KeyStore.PasswordProtection(protection.toCharArray()));
        File file = this.getEncryptionFile();
        if (!file.exists()) {
            file.createNewFile();
        }
        FileOutputStream fos = new FileOutputStream(file);
        ks.store(fos, storePassword.toCharArray());
        logger.debug("Finish storing encryption key");
    } catch (Exception e) {
        logger.warn("Exception encountered while trying to store the key : " + e.getMessage());
    }
}