List of usage examples for java.security.cert CertificateFactory generateCertificates
public final Collection<? extends Certificate> generateCertificates(InputStream inStream) throws CertificateException
From source file:ImportKey.java
/** * <p>//from w w w . j a v a 2 s. com * Takes two file names for a key and the certificate for the key, and * imports those into a keystore. Optionally it takes an alias for the key. * <p> * The first argument is the filename for the key. The key should be in * PKCS8-format. * <p> * The second argument is the filename for the certificate for the key. * <p> * If a third argument is given it is used as the alias. If missing, the key * is imported with the alias importkey * <p> * The name of the keystore file can be controlled by setting the keystore * property (java -Dkeystore=mykeystore). If no name is given, the file is * named <code>keystore.ImportKey</code> and placed in your home directory. * * @param args * [0] Name of the key file, [1] Name of the certificate file [2] * Alias for the key. **/ public static void main(String args[]) { // change this if you want another password by default String keypass = "password"; // change this if you want another alias by default String defaultalias = "tomcat"; // change this if you want another keystorefile by default String keystorename = null; // parsing command line input String keyfile = ""; String certfile = ""; if (args.length < 3 || args.length > 4) { System.out.println("Usage: java comu.ImportKey keystore keyfile certfile [alias]"); System.exit(0); } else { keystorename = args[0]; keyfile = args[1]; certfile = args[2]; if (args.length > 3) defaultalias = args[3]; } try { // initializing and clearing keystore KeyStore ks = KeyStore.getInstance("JKS", "SUN"); ks.load(null, keypass.toCharArray()); System.out.println("Using keystore-file : " + keystorename); ks.store(new FileOutputStream(keystorename), keypass.toCharArray()); ks.load(new FileInputStream(keystorename), keypass.toCharArray()); // loading Key InputStream fl = fullStream(keyfile); byte[] key = new byte[fl.available()]; KeyFactory kf = KeyFactory.getInstance("RSA"); fl.read(key, 0, fl.available()); fl.close(); PKCS8EncodedKeySpec keysp = new PKCS8EncodedKeySpec(key); PrivateKey ff = kf.generatePrivate(keysp); // loading CertificateChain CertificateFactory cf = CertificateFactory.getInstance("X.509"); InputStream certstream = fullStream(certfile); Collection c = cf.generateCertificates(certstream); Certificate[] certs = new Certificate[c.toArray().length]; if (c.size() == 1) { certstream = fullStream(certfile); System.out.println("One certificate, no chain."); Certificate cert = cf.generateCertificate(certstream); certs[0] = cert; } else { System.out.println("Certificate chain length: " + c.size()); certs = (Certificate[]) c.toArray(new Certificate[c.size()]); } // storing keystore ks.setKeyEntry(defaultalias, ff, keypass.toCharArray(), certs); System.out.println("Key and certificate stored."); System.out.println("Alias:" + defaultalias + " Password:" + keypass); ks.store(new FileOutputStream(keystorename), keypass.toCharArray()); } catch (Exception ex) { ex.printStackTrace(); } }
From source file:Main.java
/** * load the certificates as X.509 certificates */// w w w. ja v a2s .co m private static Collection<? extends Certificate> loadCertificates(final java.io.InputStream is) throws IOException, CertificateException { final CertificateFactory factory = CertificateFactory.getInstance("X.509"); return factory.generateCertificates(is); }
From source file:Main.java
private static SSLContext sslContextForTrustedCertificates(InputStream in) { try {/* w w w.jav a 2 s.co m*/ CertificateFactory e = CertificateFactory.getInstance("X.509"); Collection certificates = e.generateCertificates(in); if (certificates.isEmpty()) { throw new IllegalArgumentException("expected non-empty set of trusted certificates"); } else { char[] password = "password".toCharArray(); KeyStore keyStore = newEmptyKeyStore(password); int index = 0; Iterator keyManagerFactory = certificates.iterator(); while (keyManagerFactory.hasNext()) { Certificate trustManagerFactory = (Certificate) keyManagerFactory.next(); String sslContext = Integer.toString(index++); keyStore.setCertificateEntry(sslContext, trustManagerFactory); } KeyManagerFactory var10 = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm()); var10.init(keyStore, password); TrustManagerFactory var11 = TrustManagerFactory .getInstance(TrustManagerFactory.getDefaultAlgorithm()); var11.init(keyStore); SSLContext var12 = SSLContext.getInstance("TLS"); var12.init(var10.getKeyManagers(), var11.getTrustManagers(), new SecureRandom()); return var12; } } catch (Exception var9) { var9.printStackTrace(); } return null; }
From source file:org.xdi.oxauth.util.CertUtil.java
@SuppressWarnings("unchecked") public static List<X509Certificate> loadX509CertificateFromFile(String filePath) { if (StringHelper.isEmpty(filePath)) { log.error("X509Certificate file path is empty"); return null; }/*from www . ja va 2s .c o m*/ InputStream is; try { is = FileUtils.openInputStream(new File(filePath)); } catch (IOException ex) { log.error("Failed to read X.509 certificates from file: '" + filePath + "'", ex); return null; } List<X509Certificate> certificates = null; try { CertificateFactory cf = CertificateFactory.getInstance("X.509"); certificates = (List<X509Certificate>) cf.generateCertificates(is); } catch (CertificateException ex) { log.error("Failed to parse X.509 certificates from file: '" + filePath + "'", ex); } finally { IOUtils.closeQuietly(is); } return certificates; }
From source file:org.haox.pki.Pkix.java
public static List<Certificate> getCerts(InputStream inputStream) throws IOException, CertificateException { CertificateFactory certFactory = CertificateFactory.getInstance("X.509"); Collection<? extends Certificate> certs = (Collection<? extends Certificate>) certFactory .generateCertificates(inputStream); return new ArrayList<Certificate>(certs); }
From source file:Main.java
/** * Extracts certificate from APK/*from w w w .j ava2s . c o m*/ * * @param jar * @param entry * rsa or dsa jar item * @return * @throws IOException * I/O problem to read jar * @throws CertificateException * certificate has problems */ private static List<Certificate> extractCertificate(JarFile jar, JarEntry entry) throws IOException, CertificateException { List<Certificate> certList = new ArrayList<Certificate>(); InputStream inStream = null; try { CertificateFactory cf = CertificateFactory.getInstance("X.509"); inStream = jar.getInputStream(entry); Collection<? extends Certificate> c = cf.generateCertificates(inStream); Iterator<? extends Certificate> i = c.iterator(); while (i.hasNext()) { Certificate cert = i.next(); certList.add(cert); } } finally { if (inStream != null) { inStream.close(); } } return certList; }
From source file:org.gw2InfoViewer.factories.HttpsConnectionFactory.java
public static Certificate[] convertByteArrayToCertificate(byte[] sslCertificate) throws CertificateException { CertificateFactory cf = CertificateFactory.getInstance("X.509"); Collection c = cf.generateCertificates(new ByteArrayInputStream(sslCertificate)); Certificate[] certs;// w w w . ja v a 2 s .com certs = new Certificate[c.toArray().length]; if (c.size() == 1) { InputStream certstream = new ByteArrayInputStream(sslCertificate); Certificate cert = cf.generateCertificate(certstream); certs[0] = cert; } else { certs = (Certificate[]) c.toArray(); } return certs; }
From source file:org.samlsnort.util.KeyStoreTool.java
public static void importIntoKeystore(File certificateChainFile, File privateKeyFile, String alias) throws Exception { FileInputStream certificateStream = new FileInputStream(certificateChainFile); CertificateFactory certificateFactory = CertificateFactory.getInstance("X.509"); Certificate[] chain = {};/* w ww .j ava 2s . com*/ chain = certificateFactory.generateCertificates(certificateStream).toArray(chain); certificateStream.close(); byte[] encodedKey = new byte[(int) privateKeyFile.length()]; FileInputStream keyInputStream = new FileInputStream(privateKeyFile); keyInputStream.read(encodedKey); keyInputStream.close(); KeyFactory rSAKeyFactory = KeyFactory.getInstance("RSA"); PrivateKey privateKey = rSAKeyFactory.generatePrivate(new PKCS8EncodedKeySpec(encodedKey)); keyStore.setEntry(alias, new KeyStore.PrivateKeyEntry(privateKey, chain), new KeyStore.PasswordProtection(Configuration.getInstance().getKeystorePassword().toCharArray())); saveKeystore(); }
From source file:org.apache.ofbiz.base.util.KeyStoreUtil.java
public static void importPKCS8CertChain(KeyStore ks, String alias, byte[] keyBytes, String keyPass, byte[] certChain) throws InvalidKeySpecException, NoSuchAlgorithmException, CertificateException, KeyStoreException { // load the private key KeyFactory kf = KeyFactory.getInstance("RSA"); PKCS8EncodedKeySpec keysp = new PKCS8EncodedKeySpec(keyBytes); PrivateKey pk = kf.generatePrivate(keysp); // load the cert chain CertificateFactory cf = CertificateFactory.getInstance("X.509"); ByteArrayInputStream bais = new ByteArrayInputStream(certChain); Collection<? extends Certificate> certCol = cf.generateCertificates(bais); Certificate[] certs = new Certificate[certCol.toArray().length]; if (certCol.size() == 1) { Debug.logInfo("Single certificate; no chain", module); bais = new ByteArrayInputStream(certChain); Certificate cert = cf.generateCertificate(bais); certs[0] = cert;/*from www . j a va 2 s . c om*/ } else { Debug.logInfo("Certificate chain length : " + certCol.size(), module); certs = certCol.toArray(new Certificate[certCol.size()]); } ks.setKeyEntry(alias, pk, keyPass.toCharArray(), certs); }
From source file:org.gluu.oxtrust.ldap.service.SSLService.java
/** * Load one or more certificates from the specified byte array. * * @param certsBytes Byte array to load certificates from * @return The array of certificates/*from w w w . j a v a 2s.c o m*/ */ public static X509Certificate[] loadCertificates(byte[] certsBytes) throws Exception { try { // fix common input certificate problems by converting PEM/B64 to DER certsBytes = fixCommonInputCertProblems(certsBytes); CertificateFactory cf = CertificateFactory.getInstance(X509_CERT_TYPE, SECURITY_PROVIDER_BOUNCY_CASTLE); Collection<? extends Certificate> certs = cf.generateCertificates(new ByteArrayInputStream(certsBytes)); ArrayList<X509Certificate> loadedCerts = new ArrayList<X509Certificate>(); for (Iterator<? extends Certificate> itr = certs.iterator(); itr.hasNext();) { X509Certificate cert = (X509Certificate) itr.next(); if (cert != null) { loadedCerts.add(cert); } } return loadedCerts.toArray(new X509Certificate[loadedCerts.size()]); } catch (CertificateException ex) { try { // Failed to load certificates, may be pki path encoded - try loading as that return loadCertificatesAsPkiPathEncoded(new ByteArrayInputStream(certsBytes)); } catch (CertificateException e) { // Failed to load certificates, may be PEM certificate X509Certificate certs[] = new X509Certificate[1]; certs[0] = getPEMCertificateStatic(new ByteArrayInputStream(certsBytes)); return certs; } } }