List of usage examples for java.security KeyStore store
public final void store(OutputStream stream, char[] password) throws KeyStoreException, IOException, NoSuchAlgorithmException, CertificateException
From source file:org.wso2.carbon.identity.test.common.testng.utils.ReadCertStoreSampleUtil.java
public static KeyStore createKeyStore(Class clazz) throws Exception { clazz.getResource(""); File file = new File(clazz.getResource("/repository/resources/security/wso2carbon.jks").getFile()); KeyStore keyStore = KeyStore.getInstance("JKS"); if (file.exists()) { // if exists, load keyStore.load(new FileInputStream(file), "wso2carbon".toCharArray()); } else {/*from w w w . ja v a2 s .c o m*/ // if not exists, create keyStore.load(null, null); keyStore.store(new FileOutputStream(file), "wso2carbon".toCharArray()); } return keyStore; }
From source file:com.wandrell.util.ksgen.KeyStoreGenerator.java
/** * Saves the received key store to a file. * * @param keyStore//from w ww . j av a2 s . c o m * key store to save * @param path * path where the key store will be saved * @param password * password to applyt to the saved key store * @throws KeyStoreException * if the keystore has not been initialized * @throws NoSuchAlgorithmException * if the appropriate data integrity algorithm could not be * found * @throws CertificateException * if any of the certificates included in the key store data * could not be stored * @throws IOException * if an I/O error occurs */ private static final void saveToFile(final KeyStore keyStore, final String path, final char[] password) throws KeyStoreException, NoSuchAlgorithmException, CertificateException, IOException { FileOutputStream output = null; // Output stream for the key store try { output = new FileOutputStream(path); keyStore.store(output, password); } finally { IOUtils.closeQuietly(output); } }
From source file:org.paxml.util.CryptoUtils.java
private static void saveKeyStore(final File file, final String password, final KeyStore ks) { file.delete();/*from ww w. j a v a2s . co m*/ FileOutputStream fos = null; try { fos = new FileOutputStream(file); ks.store(fos, password.toCharArray()); } catch (Exception e) { throw new PaxmlRuntimeException("Cannot write to key store file: " + file.getAbsolutePath(), e); } finally { IOUtils.closeQuietly(fos); } }
From source file:com.cerema.cloud2.lib.common.network.NetworkUtils.java
public static void addCertToKnownServersStore(Certificate cert, Context context) throws KeyStoreException, NoSuchAlgorithmException, CertificateException, IOException { KeyStore knownServers = getKnownServersStore(context); knownServers.setCertificateEntry(Integer.toString(cert.hashCode()), cert); FileOutputStream fos = null;//from ww w . j a v a 2 s. com try { fos = context.openFileOutput(LOCAL_TRUSTSTORE_FILENAME, Context.MODE_PRIVATE); knownServers.store(fos, LOCAL_TRUSTSTORE_PASSWORD.toCharArray()); } finally { fos.close(); } }
From source file:io.vertx.config.vault.utils.Certificates.java
/** * Constructs a Java truststore in JKS format, containing the Vault server certificate generated by * {@link #createVaultCertAndKey()}, so that Vault clients configured with this JKS will trust that * certificate./*from ww w. j a v a 2 s.com*/ */ public static void createClientCertAndKey() throws Exception { if (SSL_DIRECTORY.isDirectory() && CLIENT_CERT_PEMFILE.isFile()) { return; } // Store the Vault's server certificate as a trusted cert in the truststore final KeyStore trustStore = KeyStore.getInstance("jks"); trustStore.load(null); trustStore.setCertificateEntry("cert", vaultCertificate); try (final FileOutputStream keystoreOutputStream = new FileOutputStream(CLIENT_TRUSTSTORE)) { trustStore.store(keystoreOutputStream, "password".toCharArray()); } // Generate a client certificate, and store it in a Java keystore final KeyPair keyPair = generateKeyPair(); final X509Certificate clientCertificate = generateCert(keyPair, "C=AU, O=The Legion of the Bouncy Castle, OU=Client Certificate, CN=localhost"); final KeyStore keyStore = KeyStore.getInstance("jks"); keyStore.load(null); keyStore.setKeyEntry("privatekey", keyPair.getPrivate(), "password".toCharArray(), new java.security.cert.Certificate[] { clientCertificate }); keyStore.setCertificateEntry("cert", clientCertificate); try (final FileOutputStream keystoreOutputStream = new FileOutputStream(CLIENT_KEYSTORE)) { keyStore.store(keystoreOutputStream, "password".toCharArray()); } // Also write the client certificate to a PEM file, so it can be registered with Vault writeCertToPem(clientCertificate, CLIENT_CERT_PEMFILE); writePrivateKeyToPem(keyPair.getPrivate(), CLIENT_PRIVATE_KEY_PEMFILE); }
From source file:org.apache.nifi.toolkit.tls.util.TlsHelper.java
public static String writeKeyStore(KeyStore keyStore, OutputStreamFactory outputStreamFactory, File file, String password, boolean generatedPassword) throws IOException, GeneralSecurityException { try (OutputStream fileOutputStream = outputStreamFactory.create(file)) { keyStore.store(fileOutputStream, password.toCharArray()); } catch (IOException e) { if (e.getMessage().toLowerCase().contains(ILLEGAL_KEY_SIZE) && !isUnlimitedStrengthCryptographyEnabled()) { if (generatedPassword) { file.delete();/*from w w w .ja va2 s .c o m*/ String truncatedPassword = password.substring(0, 7); try (OutputStream fileOutputStream = outputStreamFactory.create(file)) { keyStore.store(fileOutputStream, truncatedPassword.toCharArray()); } logTruncationWarning(file); return truncatedPassword; } else { throw new GeneralSecurityException("Specified password for " + file + " too long to work without unlimited JCE policy installed." + System.lineSeparator() + "Please see " + JCE_URL); } } else { throw e; } } return password; }
From source file:org.pepstock.jem.node.security.keystore.KeyStoreUtil.java
/** * Saves the kwystore on the file/*from ww w. j a v a2s . c o m*/ * @param keystore keystore to be saved * @param info Key store info with all necessary info to save it * @throws KeyStoreException if any error occurs saving the key store */ static void save(KeyStore keystore, KeyStoreInfo info) throws KeyStoreException { OutputStream os = null; try { // creates the file stream os = new FileOutputStream(info.getFile()); // stores the file keystore.store(os, info.getPassword().toCharArray()); // checks if it must be backuped if (info.getBackupFile() != null) { // read keystore to check if is consistent getKeystore(info); FileUtils.copyFile(info.getFile(), info.getBackupFile()); } } catch (FileNotFoundException e) { throw new KeyStoreException(e.getMessage(), e); } catch (IOException e) { throw new KeyStoreException(e.getMessage(), e); } catch (NoSuchAlgorithmException e) { throw new KeyStoreException(e.getMessage(), e); } catch (CertificateException e) { throw new KeyStoreException(e.getMessage(), e); } finally { // always it closes the outut stream if (os != null) { try { os.close(); } catch (Exception e) { // ignore LogAppl.getInstance().ignore(e.getMessage(), e); } } } }
From source file:com.owncloud.android.lib.common.network.NetworkUtils.java
public static void removeCertFromKnownServersStore(String alias, Context context) throws KeyStoreException, NoSuchAlgorithmException, CertificateException, IOException { KeyStore knownServers = getKnownServersStore(context); if (knownServers.containsAlias(alias)) { knownServers.deleteEntry(alias); }//from w w w .jav a 2 s. c om FileOutputStream fos = null; try { fos = context.openFileOutput(LOCAL_TRUSTSTORE_FILENAME, Context.MODE_PRIVATE); knownServers.store(fos, LOCAL_TRUSTSTORE_PASSWORD.toCharArray()); } finally { fos.close(); } }
From source file:com.owncloud.android.lib.common.network.NetworkUtils.java
public static String addCertToKnownServersStore(Certificate cert, Context context) throws KeyStoreException, NoSuchAlgorithmException, CertificateException, IOException { KeyStore knownServers = getKnownServersStore(context); String alias = Integer.toString(cert.hashCode()); knownServers.setCertificateEntry(alias, cert); FileOutputStream fos = null;/* w ww.ja v a 2s. c om*/ try { fos = context.openFileOutput(LOCAL_TRUSTSTORE_FILENAME, Context.MODE_PRIVATE); knownServers.store(fos, LOCAL_TRUSTSTORE_PASSWORD.toCharArray()); } finally { fos.close(); } return alias; }
From source file:org.tolven.security.cert.CertificateHelper.java
public static byte[] toByteArray(KeyStore keyStore, char[] password) { ByteArrayOutputStream baos = null; try {//w w w .j a v a 2s .co m baos = new ByteArrayOutputStream(); try { keyStore.store(baos, password); } catch (Exception ex) { throw new RuntimeException("Could not store keystore", ex); } byte[] byteArr = baos.toByteArray(); return byteArr; } finally { if (baos != null) try { baos.close(); } catch (Exception ex) { throw new RuntimeException("Could not close bytearrayoutputstream for keystore", ex); } } }