List of usage examples for org.bouncycastle.openssl PEMKeyPair getPrivateKeyInfo
public PrivateKeyInfo getPrivateKeyInfo()
From source file:cf.monteux.silvertunnel.netlib.layer.tor.util.Encryption.java
License:Open Source License
/** * makes RSA private key from PEM string. * * @param s PEM string that contains the key * @return/*from w ww .j av a 2 s . c om*/ * @see JCERSAPublicKey */ public static RSAKeyPair extractRSAKeyPair(final String s) { RSAKeyPair rsaKeyPair; try { // parse final PEMParser parser = new PEMParser(new StringReader(s)); final Object o = parser.readObject(); parser.close(); // check types if (!(o instanceof PEMKeyPair)) { throw new IOException("Encryption.extractRSAKeyPair: no private key found in string '" + s + "'"); } final PEMKeyPair keyPair = (PEMKeyPair) o; if (keyPair.getPrivateKeyInfo() == null) { throw new IOException( "Encryption.extractRSAKeyPair: no private key found in key pair of string '" + s + "'"); } if (keyPair.getPublicKeyInfo() == null) { throw new IOException( "Encryption.extractRSAKeyPair: no public key found in key pair of string '" + s + "'"); } // convert keys and pack them together into a key pair final RSAPrivateCrtKey privateKey = new TempJCERSAPrivateCrtKey(keyPair.getPrivateKeyInfo()); logger.debug("JCEPrivateKey={}", privateKey); final RSAPublicKey publicKey = new TempJCERSAPublicKey(keyPair.getPublicKeyInfo()); rsaKeyPair = new RSAKeyPair(publicKey, privateKey); } catch (final Exception e) { logger.warn("Encryption.extractPrivateRSAKey: Caught exception:" + e.getMessage()); rsaKeyPair = null; } return rsaKeyPair; }
From source file:cn.org.once.cstack.utils.KeyStoreUtils.java
License:Apache License
public static PrivateKey loadPrivateKey(String keyPath) throws IOException, GeneralSecurityException { PEMKeyPair keyPair = loadPEM(keyPath); PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(keyPair.getPrivateKeyInfo().getEncoded()); return KeyFactory.getInstance("RSA").generatePrivate(keySpec); }
From source file:co.lqnt.lockbox.key.KeyFactory.java
License:Open Source License
/** * Decrypts an encrypted PEM key pair.//ww w . j a v a 2 s .c o m * * @param encryptedKeyPair The key pair to decrypt. * @param password The password to use. * * @return The decrypted private key. * @throws PrivateKeyReadException If reading of the private key fails. */ protected PrivateKey decryptPemKeyPair(PEMEncryptedKeyPair encryptedKeyPair, String password) throws PrivateKeyReadException { PEMDecryptorProvider decryptorProvider = this.pemDecryptorProviderBuilder().build(password.toCharArray()); PEMKeyPair pemKeyPair; try { pemKeyPair = encryptedKeyPair.decryptKeyPair(decryptorProvider); } catch (IOException e) { throw new PrivateKeyReadException(e); } return this.convertPrivateKey(pemKeyPair.getPrivateKeyInfo()); }
From source file:com.brienwheeler.apps.tomcat.TomcatBean.java
License:Open Source License
private RSAPrivateKey readKeyFile() throws IOException, NoSuchAlgorithmException, InvalidKeySpecException { String parse[] = readPEMFile(sslKeyFile, KEY_PATTERN, 2); if (parse == null) throw new IllegalArgumentException("invalid key file contents"); if (parse[0].length() == 0) { // BEGIN PRIVATE KEY KeyFactory keyFactory = KeyFactory.getInstance("RSA"); return (RSAPrivateKey) keyFactory.generatePrivate(new PKCS8EncodedKeySpec(Base64.decode(parse[1]))); }/*from www .ja v a2 s .c om*/ if (parse[0].contains("RSA")) { // BEGIN RSA PRIVATE KEY Security.addProvider(new BouncyCastleProvider()); PEMParser pemParser = new PEMParser(new FileReader(sslKeyFile)); Object parsedObject = pemParser.readObject(); if (!(parsedObject instanceof PEMKeyPair)) throw new IllegalArgumentException("invalid key file contents"); PEMKeyPair keyPair = (PEMKeyPair) parsedObject; RSAPrivateKey privateKey = (RSAPrivateKey) BouncyCastleProvider .getPrivateKey(keyPair.getPrivateKeyInfo()); if (privateKey == null) throw new IllegalArgumentException("invalid key file contents"); return privateKey; } throw new IllegalArgumentException("invalid key file contents"); }
From source file:com.enioka.jqm.pki.JpaCa.java
License:Open Source License
public static CertificateRequest initCa(EntityManager em) { // result field CertificateRequest cr = new CertificateRequest(); // Get the alias of the private key to use String caAlias = null;/* ww w .j a va2 s.co m*/ try { caAlias = em .createQuery("SELECT p FROM GlobalParameter p WHERE p.key = 'keyAlias'", GlobalParameter.class) .getSingleResult().getValue(); } catch (NoResultException e) { caAlias = Constants.CA_DEFAULT_PRETTY_NAME; } // Create the CA if it does not already exist PKI pki = null; try { pki = em.createQuery("SELECT p FROM PKI p WHERE p.prettyName = :pn", PKI.class) .setParameter("pn", caAlias).getSingleResult(); } catch (NoResultException e) { // Create the CA certificate and PK cr = new CertificateRequest(); cr.generateCA(caAlias); // Store pki = new PKI(); pki.setPemPK(cr.writePemPrivateToString()); pki.setPemCert(cr.writePemPublicToString()); pki.setPrettyName(caAlias); em.getTransaction().begin(); em.persist(pki); em.getTransaction().commit(); } try { // Public (X509 certificate) String pemCert = pki.getPemCert(); StringReader sr = new StringReader(pemCert); PemReader pr = new PemReader(sr); cr.holder = new X509CertificateHolder(pr.readPemObject().getContent()); pr.close(); // Private key String pemPrivate = pki.getPemPK(); sr = new StringReader(pemPrivate); PEMParser pp = new PEMParser(sr); PEMKeyPair caKeyPair = (PEMKeyPair) pp.readObject(); pp.close(); byte[] encodedPrivateKey = caKeyPair.getPrivateKeyInfo().getEncoded(); KeyFactory keyFactory = KeyFactory.getInstance(Constants.KEY_ALGORITHM); PKCS8EncodedKeySpec privateKeySpec = new PKCS8EncodedKeySpec(encodedPrivateKey); cr.privateKey = keyFactory.generatePrivate(privateKeySpec); } catch (Exception e) { throw new PkiException(e); } // Done return cr; }
From source file:com.github.khazrak.jdocker.ssl.KeyStoreUtil.java
License:Apache License
static PrivateKey loadPrivateKey(String keyPath) throws IOException, GeneralSecurityException { PEMKeyPair keyPair = loadPEM(keyPath); PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(keyPair.getPrivateKeyInfo().getEncoded()); return KeyFactory.getInstance("RSA").generatePrivate(keySpec); }
From source file:com.hypersocket.certs.X509CertificateUtils.java
License:Open Source License
private static KeyPair loadKeyPair(PEMKeyPair privatekey) throws CertificateException { try {// ww w . j ava2 s . c om PEMKeyPair pair = (PEMKeyPair) privatekey; byte[] encodedPublicKey = pair.getPublicKeyInfo().getEncoded(); byte[] encodedPrivateKey = pair.getPrivateKeyInfo().getEncoded(); // Generate KeyPair. KeyFactory keyFactory = KeyFactory.getInstance( pair.getPublicKeyInfo().getAlgorithm().getParameters() instanceof DSAParameter ? "DSA" : "RSA"); X509EncodedKeySpec publicKeySpec = new X509EncodedKeySpec(encodedPublicKey); PublicKey publicKey = keyFactory.generatePublic(publicKeySpec); PKCS8EncodedKeySpec privateKeySpec = new PKCS8EncodedKeySpec(encodedPrivateKey); PrivateKey privateKey = keyFactory.generatePrivate(privateKeySpec); return new KeyPair(publicKey, privateKey); } catch (Exception e) { throw new CertificateException("Failed to convert PEMKeyPair into JCE KeyPair", e); } }
From source file:com.jcs_java_sdk.Utils.java
License:Open Source License
public static PrivateKey readPrivateKey(String privateKeyPath, String keyPassword) throws IOException { FileReader fileReader = new FileReader(privateKeyPath); PEMParser keyReader = new PEMParser(fileReader); JcaPEMKeyConverter converter = new JcaPEMKeyConverter(); PEMDecryptorProvider decryptionProv = new JcePEMDecryptorProviderBuilder().build(keyPassword.toCharArray()); Object keyPair = keyReader.readObject(); PrivateKeyInfo keyInfo;// w w w. ja va 2s.com if (keyPair instanceof PEMEncryptedKeyPair) { PEMKeyPair decryptedKeyPair = ((PEMEncryptedKeyPair) keyPair).decryptKeyPair(decryptionProv); keyInfo = decryptedKeyPair.getPrivateKeyInfo(); } else { keyInfo = ((PEMKeyPair) keyPair).getPrivateKeyInfo(); } keyReader.close(); return converter.getPrivateKey(keyInfo); }
From source file:com.rovemonteux.silvertunnel.netlib.layer.tor.util.Encryption.java
License:Open Source License
/** * makes RSA private key from PEM string. * * @param s PEM string that contains the key * @return//w w w. ja v a 2s . co m * @see JCERSAPublicKey */ public static RSAKeyPair extractRSAKeyPair(final String s) { RSAKeyPair rsaKeyPair; try { // parse final PEMParser parser = new PEMParser(new StringReader(s)); final Object o = parser.readObject(); parser.close(); // check types if (!(o instanceof PEMKeyPair)) { throw new IOException("Encryption.extractRSAKeyPair: no private key found in string '" + s + "'"); } final PEMKeyPair keyPair = (PEMKeyPair) o; if (keyPair.getPrivateKeyInfo() == null) { throw new IOException( "Encryption.extractRSAKeyPair: no private key found in key pair of string '" + s + "'"); } if (keyPair.getPublicKeyInfo() == null) { throw new IOException( "Encryption.extractRSAKeyPair: no public key found in key pair of string '" + s + "'"); } // convert keys and pack them together into a key pair final RSAPrivateCrtKey privateKey = new TempJCERSAPrivateCrtKey(keyPair.getPrivateKeyInfo()); LOG.debug("JCEPrivateKey={}", privateKey); final RSAPublicKey publicKey = new TempJCERSAPublicKey(keyPair.getPublicKeyInfo()); rsaKeyPair = new RSAKeyPair(publicKey, privateKey); } catch (final Exception e) { LOG.warn("Encryption.extractPrivateRSAKey: Caught exception:" + e.getMessage()); rsaKeyPair = null; } return rsaKeyPair; }
From source file:com.shekhargulati.reactivex.docker.client.ssl.DockerCertificates.java
License:Open Source License
private DockerCertificates(final Builder builder) throws DockerCertificateException { if ((builder.caCertPath == null) || (builder.clientCertPath == null) || (builder.clientKeyPath == null)) { throw new DockerCertificateException( "caCertPath, clientCertPath, and clientKeyPath must all be specified"); }/*from w w w . ja va 2 s . c om*/ try { final CertificateFactory cf = CertificateFactory.getInstance("X.509"); final Certificate caCert = cf.generateCertificate(Files.newInputStream(builder.caCertPath)); final Certificate clientCert = cf.generateCertificate(Files.newInputStream(builder.clientCertPath)); final PEMKeyPair clientKeyPair = (PEMKeyPair) new PEMParser( Files.newBufferedReader(builder.clientKeyPath, Charset.defaultCharset())).readObject(); final PKCS8EncodedKeySpec spec = new PKCS8EncodedKeySpec( clientKeyPair.getPrivateKeyInfo().getEncoded()); final KeyFactory kf = KeyFactory.getInstance("RSA"); final PrivateKey clientKey = kf.generatePrivate(spec); final KeyStore trustStore = KeyStore.getInstance(KeyStore.getDefaultType()); trustStore.load(null, null); trustStore.setEntry("ca", new KeyStore.TrustedCertificateEntry(caCert), null); final KeyStore keyStore = KeyStore.getInstance(KeyStore.getDefaultType()); keyStore.load(null, KEY_STORE_PASSWORD); keyStore.setCertificateEntry("client", clientCert); keyStore.setKeyEntry("key", clientKey, KEY_STORE_PASSWORD, new Certificate[] { clientCert }); this.sslContext = SSLContexts.custom().loadTrustMaterial(trustStore) .loadKeyMaterial(keyStore, KEY_STORE_PASSWORD).useTLS().build(); } catch (CertificateException | IOException | NoSuchAlgorithmException | InvalidKeySpecException | KeyStoreException | UnrecoverableKeyException | KeyManagementException e) { throw new DockerCertificateException(e); } }