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:com.github.technosf.posterer.models.impl.KeyStoreBeanTest.java
/** * Create clean temp key store files and ensure we can access the main test * key store file/*from ww w. j a v a 2 s. c om*/ */ @BeforeClass private void init() throws KeyStoreException, NoSuchAlgorithmException, CertificateException, IOException { // Delete preexisting testing keystores FileUtils.deleteQuietly(FileUtils.getFile(missingKeyStore)); FileUtils.deleteQuietly(FileUtils.getFile(unknownKeyStore)); FileUtils.deleteQuietly(FileUtils.getFile(emptyKeyStore)); // Get the keystore algo and create the ks in memory KeyStore ks = KeyStore.getInstance("JKS"); ks.load(null, passwordchr); // Write out unknown pw keystore FileOutputStream fos = new FileOutputStream(unknownKeyStore); ks.store(fos, "unknownpw".toCharArray()); fos.close(); assertFalse(FileUtils.getFile(missingKeyStore).exists()); // Write out empty key store fos = new FileOutputStream(emptyKeyStore); ks.store(fos, passwordchr); fos.close(); assertFalse(FileUtils.getFile(missingKeyStore).exists()); // Check the main test key store URL testKeystoreURL = this.getClass().getResource("/testkeystore.jks"); testKeyStoreFile = FileUtils.toFile(testKeystoreURL); assertNotNull(testKeyStoreFile); }
From source file:org.nuxeo.ecm.core.blob.binary.TestAESBinaryManager.java
protected void createKeyStore(File file) throws GeneralSecurityException, IOException { AESBinaryManager.setUnlimitedJCEPolicy(); KeyGenerator kgen = KeyGenerator.getInstance("AES"); kgen.init(256);//from ww w.ja v a 2s.c om Key skey = kgen.generateKey(); KeyStore keyStore = KeyStore.getInstance(KEY_STORE_TYPE); // keyStore.load(null, KEY_STORE_PASSWORD.toCharArray()); keyStore.load(null, null); keyStore.setKeyEntry(KEY_ALIAS, skey, KEY_PASSWORD.toCharArray(), null); OutputStream out = new FileOutputStream(file); keyStore.store(out, KEY_STORE_PASSWORD.toCharArray()); out.close(); }
From source file:org.forgerock.openidm.security.impl.KeystoreResourceProviderTest.java
@BeforeClass public void runInitalSetup() throws Exception { Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider()); KeyStore keyStore = KeyStore.getInstance(KEYSTORE_TYPE); keyStore.load(null, KEYSTORE_PASSWORD.toCharArray()); keystoreFile = File.createTempFile(KEYSTORE, KEYSTORE_TYPE); FileOutputStream fileOutputStream = new FileOutputStream(keystoreFile); keyStore.store(fileOutputStream, KEYSTORE_PASSWORD.toCharArray()); fileOutputStream.close();//from w ww. ja v a 2 s.c o m keyStoreHandler = new JcaKeyStoreHandler(KEYSTORE_TYPE, keystoreFile.getAbsolutePath(), KEYSTORE_PASSWORD); }
From source file:org.kaazing.maven.plugins.TrustStoreMojo.java
public void execute() throws MojoExecutionException { if (skipTests) { return;/*from ww w . ja v a2 s . c om*/ } String trustStoreTmpFile = String.format("%s%struststore.db", project.getBuild().getDirectory(), File.separator); getLog().info(String.format("TRUSTSTORE: truststore.file = '%s'", trustStoreFile)); getLog().info(String.format("TRUSTSTORE: truststore.tmp-file = '%s'", trustStoreTmpFile)); getLog().info(String.format("TRUSTSTORE: truststore.type = '%s'", trustStoreType)); getLog().info(String.format("TRUSTSTORE: truststore.pass = '%s'", trustStorePass)); getLog().info(String.format("TRUSTSTORE: truststore.source-url = '%s'", trustStoreSourceURL)); File tmpFile = null; FileOutputStream fos = null; try { getLog().info(String.format("TRUSTSTORE: Generating new truststore.db from %s", trustStoreSourceURL)); Map<String, String> certs = getCertificates(trustStoreSourceURL); KeyStore ks = getTrustStore(certs, trustStoreType); tmpFile = new File(trustStoreTmpFile); fos = new FileOutputStream(tmpFile); ks.store(fos, trustStorePass.toCharArray()); } catch (Exception e) { getLog().error("TRUSTSTORE: Error while generating truststore: " + e.getMessage()); throw new MojoExecutionException("Error while generating truststore: " + e.getMessage(), e); } finally { if (fos != null) { try { fos.close(); File f = new File(trustStoreFile); // Delete the destination file first; Windows will barf if // you don't do that before trying to rename the original // file to it. This was the cause of KG-6456. if (f.exists()) { if (!f.isFile()) { throw new MojoExecutionException(String.format( "Error while generating truststore: truststore file '%s' already exists but is not a file", trustStoreFile)); } if (!f.delete()) { throw new MojoExecutionException(String.format( "Error while generating truststore: truststore file '%s' already exists but could not be deleted", trustStoreFile)); } } if (!tmpFile.getCanonicalPath().equals(f.getCanonicalPath())) { File parent = f.getParentFile(); if (!parent.exists()) { if (!parent.mkdirs()) { throw new IOException(String.format("Error creating directory '%s'", parent)); } } if (tmpFile.renameTo(f) == false) { throw new IOException(String.format("Error renaming '%s' to '%s'", trustStoreTmpFile, trustStoreFile)); } } getLog().info(String.format("TRUSTSTORE: Renamed %s to %s", trustStoreTmpFile, trustStoreFile)); } catch (IOException ioe) { getLog().error("TRUSTSTORE: Error while generating truststore: " + ioe.getMessage()); throw new MojoExecutionException("Error while generating truststore: " + ioe.getMessage(), ioe); } } } }
From source file:com.streamsets.datacollector.credential.javakeystore.JavaKeyStoreCredentialStore.java
protected void persistStore(KeyStore keyStore) throws IOException { DataStore dataStore = new DataStore(getKeyStoreFile()); try (OutputStream os = dataStore.getOutputStream()) { OutputStream csos = new CloseShieldOutputStream(os); keyStore.store(csos, getKeystorePassword().toCharArray()); dataStore.commit(os);/*from w w w. j a v a 2 s . c om*/ } catch (IOException ex) { throw ex; } catch (Exception ex) { throw new IOException(ex); } finally { dataStore.release(); } }
From source file:com.tremolosecurity.openunison.util.OpenUnisonUtils.java
private static void importSecreyKey(Options options, CommandLine cmd, TremoloType tt, KeyStore ks, String ksPath) throws KeyStoreException, Base64DecodingException, NoSuchAlgorithmException, CertificateException, FileNotFoundException, IOException { String alias = loadOption(cmd, "alias", options); logger.info("importing to " + alias); String base64Key = loadOption(cmd, "secretkey", options); SecretKey sc = new SecretKeySpec(Base64.decode(base64Key), "AES"); ks.setKeyEntry(alias, sc, tt.getKeyStorePassword().toCharArray(), null); ks.store(new FileOutputStream(ksPath), tt.getKeyStorePassword().toCharArray()); logger.info("import complete"); }
From source file:com.thoughtworks.go.server.util.HttpTestUtil.java
private void prepareCertStore(File serverKeyStore) { KeyPair keyPair = generateKeyPair(); X509Certificate cert = generateCert(keyPair); FileOutputStream os = null;//w w w .j a v a 2 s .com try { KeyStore store = KeyStore.getInstance("JKS"); store.load(null, null); store.setKeyEntry("test", keyPair.getPrivate(), STORE_PASSWORD.toCharArray(), new Certificate[] { cert }); os = new FileOutputStream(serverKeyStore); store.store(os, STORE_PASSWORD.toCharArray()); } catch (Exception e) { throw new RuntimeException(e); } finally { if (os != null) { IOUtils.closeQuietly(os); } } }
From source file:be.fgov.kszbcss.rhq.websphere.connector.security.TrustStoreManager.java
public void execute(TrustStoreAction action, boolean readOnly) throws Exception { Lock lock = readOnly ? truststoreLock.readLock() : truststoreLock.writeLock(); lock.lock();/* w w w . ja v a 2s. c o m*/ try { KeyStore truststore = loadTrustStore(); action.execute(truststore); if (!readOnly) { if (log.isDebugEnabled()) { log.debug("Writing trust store with " + truststore.size() + " entries to " + truststoreFile); } OutputStream out = new FileOutputStream(truststoreFile); try { truststore.store(out, new char[0]); } finally { out.close(); } reloadTrustManager(); } } finally { lock.unlock(); } }
From source file:com.adito.keystore.actions.ShowKeyStoreDispatchAction.java
/** * @param mapping//from www . j a v a2 s . com * @param form * @param request * @param response * @return ActionForward * @throws Exception */ public ActionForward exportPrivate(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { String sel = ((ShowKeyStoreForm) form).getSelectedItem(); KeyStore systemClientStore = ((ShowKeyStoreForm) form).getSelectedKeyStore().getKeyStore(); FileDownloadPageInterceptListener l = (FileDownloadPageInterceptListener) CoreUtil .getPageInterceptListenerById(request.getSession(), "fileDownload"); if (l == null) { l = new FileDownloadPageInterceptListener(); CoreUtil.addPageInterceptListener(request.getSession(), l); } File clientCertFile = new File(CoreUtil.getTempDownloadDirectory(getSessionInfo(request)), sel + ".p12"); FileOutputStream out = new FileOutputStream(clientCertFile); char[] password = ((ShowKeyStoreForm) form).getSelectedKeyStore().getKeyStorePassword().toCharArray(); if (systemClientStore.isKeyEntry(sel)) { PrivateKey keypair = ((ShowKeyStoreForm) form).getSelectedKeyStore().getPrivateKey(sel, password); KeyStore userStore = KeyStore.getInstance("PKCS12", "BC"); userStore.load(null, null); userStore.setKeyEntry(sel, keypair, ((ShowKeyStoreForm) form).getPassword().toCharArray(), ((ShowKeyStoreForm) form).getSelectedKeyStore().getCertificateChain(sel)); userStore.store(out, ((ShowKeyStoreForm) form).getPassword().toCharArray()); out.close(); } l.addDownload(new CSRDownload(clientCertFile, clientCertFile.getName(), "application/octet-stream", mapping.findForward("success"), "exportPrivateKey.message", "keystore", sel)); return mapping.findForward("success"); }
From source file:org.apache.accumulo.test.util.CertUtils.java
public void createPublicCert(File targetKeystoreFile, String keyName, String rootKeystorePath, String rootKeystorePassword, String truststorePassword) throws NoSuchAlgorithmException, CertificateException, FileNotFoundException, IOException, KeyStoreException, UnrecoverableKeyException { KeyStore signerKeystore = KeyStore.getInstance(keystoreType); char[] signerPasswordArray = rootKeystorePassword.toCharArray(); try (FileInputStream fis = new FileInputStream(rootKeystorePath)) { signerKeystore.load(fis, signerPasswordArray); }//w w w. j av a2 s .co m Certificate rootCert = findCert(signerKeystore); KeyStore keystore = KeyStore.getInstance(keystoreType); keystore.load(null, null); keystore.setCertificateEntry(keyName + "Cert", rootCert); try (FileOutputStream fos = new FileOutputStream(targetKeystoreFile)) { keystore.store(fos, truststorePassword.toCharArray()); } }