List of usage examples for org.bouncycastle.openssl.jcajce JcaPEMWriter JcaPEMWriter
public JcaPEMWriter(Writer out)
From source file:mitm.BouncyCastleSslEngineSource.java
License:Apache License
private static void exportPem(File exportFile, Object... certs) throws IOException, CertificateEncodingException { Writer sw = null;/* w w w .j a v a 2 s.co m*/ JcaPEMWriter pw = null; try { sw = new FileWriter(exportFile); pw = new JcaPEMWriter(sw); for (Object cert : certs) { pw.writeObject(cert); pw.flush(); } } finally { IOUtils.closeQuietly(pw); IOUtils.closeQuietly(sw); } }
From source file:net.etfbl.cryptodigitalcertificate.tool.util.CryptoPEMExtractor.java
public void writeObject(Object object, String filePath) throws FileNotFoundException, IOException { JcaPEMWriter writer = new JcaPEMWriter(new OutputStreamWriter(new FileOutputStream(filePath))); try {//from w ww . ja va2 s . c om writer.writeObject(object); } finally { writer.close(); } }
From source file:net.sf.portecle.FPortecle.java
License:Open Source License
/** * Export the head certificate of the keystore entry in a PEM encoding. * // ww w .ja va2s. c om * @param sEntryAlias Entry alias * @return True if the export is successful, false otherwise */ private boolean exportHeadCertOnlyPem(String sEntryAlias) { X509Certificate cert = null; try { cert = getHeadCert(sEntryAlias); } catch (CryptoException ex) { DThrowable.showAndWait(this, null, ex); return false; } String basename = X509CertUtil.getCertificateAlias(cert); if (basename.isEmpty()) { basename = sEntryAlias; } // Let the user choose the export certificate file File fExportFile = chooseExportCertFile(basename); if (fExportFile == null) { return false; } if (!confirmOverwrite(fExportFile, getTitle())) { return false; } try (JcaPEMWriter pw = new JcaPEMWriter(new FileWriter(fExportFile))) { pw.writeObject(cert); m_lastDir.updateLastDir(fExportFile); return true; } catch (FileNotFoundException ex) { String sMessage = MessageFormat.format(RB.getString("FPortecle.NoWriteFile.message"), fExportFile.getName()); JOptionPane.showMessageDialog(this, sMessage, getTitle(), JOptionPane.WARNING_MESSAGE); return false; } catch (IOException ex) { DThrowable.showAndWait(this, null, ex); return false; } }
From source file:net.sf.portecle.FPortecle.java
License:Open Source License
/** * Export the private key and certificates of the keystore entry to a PEM encoded "OpenSSL" format bundle. * /*from www. j a v a 2s . c o m*/ * @param sEntryAlias Entry alias * @return True if the export is successful, false otherwise */ private boolean exportPrivKeyCertChainPEM(String sEntryAlias) { KeyStore keyStore = m_keyStoreWrap.getKeyStore(); // Get the entry's password (we may already know it from the wrapper) char[] cPassword = m_keyStoreWrap.getEntryPassword(sEntryAlias); if (cPassword == null) { cPassword = KeyStoreUtil.DUMMY_PASSWORD; if (m_keyStoreWrap.getKeyStoreType().isEntryPasswordSupported()) { DGetPassword dGetPassword = new DGetPassword(this, RB.getString("FPortecle.KeyEntryPassword.Title")); dGetPassword.setLocationRelativeTo(this); SwingHelper.showAndWait(dGetPassword); cPassword = dGetPassword.getPassword(); if (cPassword == null) { return false; } } } File fExportFile = null; try { // Get the private key and certificate chain from the entry Key privKey = keyStore.getKey(sEntryAlias, cPassword); Certificate[] certs = keyStore.getCertificateChain(sEntryAlias); // Get a new password to encrypt the private key with DGetNewPassword dGetNewPassword = new DGetNewPassword(this, RB.getString("FPortecle.PrivateKeyExportPassword.Title")); dGetNewPassword.setLocationRelativeTo(this); SwingHelper.showAndWait(dGetNewPassword); char[] password = dGetNewPassword.getPassword(); if (password == null) { return false; } String basename = null; if (certs.length > 0 && certs[0] instanceof X509Certificate) { basename = X509CertUtil.getCertificateAlias((X509Certificate) certs[0]); } if (basename == null || basename.isEmpty()) { basename = sEntryAlias; } // Let the user choose the PEM export file fExportFile = chooseExportPEMFile(basename); if (fExportFile == null) { return false; } if (!confirmOverwrite(fExportFile, getTitle())) { return false; } // Do the export try (JcaPEMWriter pw = new JcaPEMWriter(new FileWriter(fExportFile))) { if (password.length == 0) { pw.writeObject(privKey); } else { // TODO: make algorithm configurable/ask user? String algorithm = "DES-EDE3-CBC"; SecureRandom rand = SecureRandom.getInstance("SHA1PRNG"); PEMEncryptor encryptor = new JcePEMEncryptorBuilder(algorithm).setSecureRandom(rand) .build(password); pw.writeObject(privKey, encryptor); } for (Certificate cert : certs) { pw.writeObject(cert); } } m_lastDir.updateLastDir(fExportFile); return true; } catch (FileNotFoundException ex) { String sMessage = MessageFormat.format(RB.getString("FPortecle.NoWriteFile.message"), fExportFile.getName()); JOptionPane.showMessageDialog(this, sMessage, getTitle(), JOptionPane.WARNING_MESSAGE); return false; } catch (GeneralSecurityException | IOException ex) { DThrowable.showAndWait(this, null, ex); return false; } }
From source file:net.sf.portecle.FPortecle.java
License:Open Source License
/** * Let the user generate a CSR for the selected key pair entry. * //from ww w . j a va2 s .c o m * @return True if the generation is successful, false otherwise */ private boolean generateCsrSelectedEntry() { assert m_keyStoreWrap != null; assert m_keyStoreWrap.getKeyStore() != null; // Not valid for a key-only or a trusted certificate entry if (!KeyStoreTableModel.KEY_PAIR_ENTRY.equals(m_jtKeyStore.getSelectedType())) { return false; } String sAlias = m_jtKeyStore.getSelectedAlias(); KeyStore keyStore = m_keyStoreWrap.getKeyStore(); File fCsrFile = null; try { // Get the entry's password (we may already know it from the wrapper) char[] cPassword = m_keyStoreWrap.getEntryPassword(sAlias); if (cPassword == null) { cPassword = KeyStoreUtil.DUMMY_PASSWORD; if (m_keyStoreWrap.getKeyStoreType().isEntryPasswordSupported()) { DGetPassword dGetPassword = new DGetPassword(this, RB.getString("FPortecle.KeyEntryPassword.Title")); dGetPassword.setLocationRelativeTo(this); SwingHelper.showAndWait(dGetPassword); cPassword = dGetPassword.getPassword(); if (cPassword == null) { return false; } } } // Get the key pair entry's private key using the password PrivateKey privKey = (PrivateKey) keyStore.getKey(sAlias, cPassword); // Update the keystore wrapper m_keyStoreWrap.setEntryPassword(sAlias, cPassword); // Get the first certificate in the entry's certificate chain X509Certificate cert = X509CertUtil .orderX509CertChain(X509CertUtil.convertCertificates(keyStore.getCertificateChain(sAlias)))[0]; // Let the user choose the file to write the CSR to fCsrFile = chooseGenerateCsrFile(X509CertUtil.getCertificateAlias(cert)); if (fCsrFile == null) { return false; } if (!confirmOverwrite(fCsrFile, RB.getString("FPortecle.GenerateCsr.Title"))) { return false; } // Generate CSR and write it out to file try (JcaPEMWriter pw = new JcaPEMWriter(new FileWriter(fCsrFile))) { pw.writeObject(X509CertUtil.generatePKCS10CSR(cert, privKey)); } // Display success message JOptionPane.showMessageDialog(this, RB.getString("FPortecle.CsrGenerationSuccessful.message"), RB.getString("FPortecle.GenerateCsr.Title"), JOptionPane.INFORMATION_MESSAGE); m_lastDir.updateLastDir(fCsrFile); return true; } catch (FileNotFoundException ex) { JOptionPane.showMessageDialog(this, MessageFormat.format(RB.getString("FPortecle.NoWriteFile.message"), fCsrFile), RB.getString("FPortecle.GenerateCsr.Title"), JOptionPane.WARNING_MESSAGE); return false; } catch (Exception ex) { DThrowable.showAndWait(this, null, ex); return false; } }
From source file:net.sf.portecle.gui.crypto.DViewPEM.java
License:Open Source License
/** * Initialize the dialog's GUI components. * /*from w ww. j a v a 2s . com*/ * @throws CryptoException A problem was encountered getting the object's PEM encoding */ private void initComponents() throws CryptoException { if (m_pem == null) { StringWriter encoded = new StringWriter(); try (JcaPEMWriter pw = new JcaPEMWriter(encoded)) { pw.writeObject(m_object); } catch (IOException e) { throw new CryptoException(RB.getString("DViewPEM.exception.message"), e); } m_pem = encoded.toString(); } JPanel jpButtons = new JPanel(new FlowLayout(FlowLayout.CENTER)); JButton jbOK = getOkButton(true); final JButton jbSave = new JButton(RB.getString("DViewPEM.jbSave.text")); jbSave.setMnemonic(RB.getString("DViewPEM.jbSave.mnemonic").charAt(0)); if (m_chooser == null || m_pem == null) { jbSave.setEnabled(false); } else { jbSave.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent evt) { savePressed(); } }); } jpButtons.add(jbOK); jpButtons.add(jbSave); JPanel jpPEM = new JPanel(new BorderLayout()); jpPEM.setBorder(new EmptyBorder(5, 5, 5, 5)); // Load text area with the PEM encoding JTextArea jtaPEM = new JTextArea(m_pem); jtaPEM.setCaretPosition(0); jtaPEM.setEditable(false); jtaPEM.setFont(new Font(Font.MONOSPACED, Font.PLAIN, jtaPEM.getFont().getSize())); JScrollPane jspPEM = new JScrollPane(jtaPEM, ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS, ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS); jspPEM.setPreferredSize(new Dimension(500, 300)); jpPEM.add(jspPEM, BorderLayout.CENTER); getContentPane().add(jpPEM, BorderLayout.CENTER); getContentPane().add(jpButtons, BorderLayout.SOUTH); getRootPane().setDefaultButton(jbOK); initDialog(); setResizable(true); jbOK.requestFocusInWindow(); }
From source file:net.weta.components.communication.security.JavaKeystoreTest.java
License:EUPL
public static void exportCertficate(File keystore, String alias, File cert) throws Exception { KeyStore ks = KeyStore.getInstance(KeyStore.getDefaultType()); char[] password = "password".toCharArray(); ks.load(new FileInputStream(keystore), password); java.security.cert.Certificate c = ks.getCertificate(alias); JcaPEMWriter writer = new JcaPEMWriter(new FileWriter(cert)); writer.writeObject(c);/*from w w w. j ava 2s . c om*/ writer.close(); }
From source file:org.albertschmitt.crypto.RSAService.java
License:Open Source License
/** * Utility function that writes an RSA Public or Private key to an output stream in PEM format. * * @param outstream//w w w . ja va2s. c o m * The stream to write the RSA key to. * @param pki * The Key to be written to the stream. */ private <T> void writePEMKey(OutputStream outstream, T pki) throws IOException { OutputStreamWriter writer = new OutputStreamWriter(outstream, "UTF-8"); try (JcaPEMWriter pem = new JcaPEMWriter(writer)) { pem.writeObject(pki); } }
From source file:org.albertschmitt.crypto.RSAService.java
License:Open Source License
/** * Generate a Public / Private RSA key pair and write them to the designated Output Streams. * * @param os_private/* ww w . java2s.c o m*/ * The stream to which the RSA Private Key will be written. * @param os_public * The stream to which the RSA Public Key will be written. * @param password * The RSA Private Key will be encrypted with this password. * @throws java.security.NoSuchAlgorithmException * @throws org.bouncycastle.operator.OperatorCreationException * @throws org.bouncycastle.util.io.pem.PemGenerationException * @throws java.io.UnsupportedEncodingException * @throws java.io.IOException */ public void generateKey(OutputStream os_private, OutputStream os_public, char[] password) throws NoSuchAlgorithmException, OperatorCreationException, PemGenerationException, UnsupportedEncodingException, IOException { final KeyPairGenerator kpg = KeyPairGenerator.getInstance("RSA"); final SecureRandom secure = new SecureRandom(); kpg.initialize(keysize.getKeySize(), secure); KeyPair keyPair = kpg.generateKeyPair(); final PemObject pem = encryptKey(keyPair, password); try (JcaPEMWriter writer = new JcaPEMWriter(new OutputStreamWriter(os_private, "UTF-8"))) { writer.writeObject(pem); } try (JcaPEMWriter writer = new JcaPEMWriter(new OutputStreamWriter(os_public, "UTF-8"))) { writer.writeObject(keyPair.getPublic()); } }
From source file:org.apache.cloudstack.utils.security.CertUtils.java
License:Apache License
public static String x509CertificateToPem(final X509Certificate cert) throws IOException { final StringWriter sw = new StringWriter(); try (final JcaPEMWriter pw = new JcaPEMWriter(sw)) { pw.writeObject(cert);//from w w w . j av a 2 s .co m pw.flush(); } return sw.toString(); }