List of usage examples for org.bouncycastle.openssl PEMKeyPair getPublicKeyInfo
public SubjectPublicKeyInfo getPublicKeyInfo()
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// www .ja v 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:com.hypersocket.certs.X509CertificateUtils.java
License:Open Source License
private static KeyPair loadKeyPair(PEMKeyPair privatekey) throws CertificateException { try {/*from w w w .j av a 2 s. c o m*/ 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.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 ww . j av a2 s . 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.stormpath.spring.cloud.zuul.config.PemResourceKeyResolver.java
License:Apache License
private Key doApply(Resource resource) throws IOException, CertificateException { try (Reader reader = new BufferedReader(new InputStreamReader(resource.getInputStream(), "UTF-8"))) { PEMParser pemParser = new PEMParser(reader); Object o;//from w ww .j ava 2 s.c o m boolean encryptedPrivateFound = false; while ((o = pemParser.readObject()) != null) { if (o instanceof PKCS8EncryptedPrivateKeyInfo) { encryptedPrivateFound = true; } if (o instanceof PEMKeyPair) { PEMKeyPair pemKeyPair = (PEMKeyPair) o; return findPrivate ? pemKeyConverter.getPrivateKey(pemKeyPair.getPrivateKeyInfo()) : pemKeyConverter.getPublicKey(pemKeyPair.getPublicKeyInfo()); } if (o instanceof PrivateKeyInfo && findPrivate) { PrivateKeyInfo privateKeyInfo = (PrivateKeyInfo) o; return pemKeyConverter.getPrivateKey(privateKeyInfo); } if (o instanceof SubjectPublicKeyInfo && !findPrivate) { SubjectPublicKeyInfo info = (SubjectPublicKeyInfo) o; return pemKeyConverter.getPublicKey(info); } if (o instanceof X509CertificateHolder && !findPrivate) { X509CertificateHolder holder = (X509CertificateHolder) o; X509Certificate cert = x509Converter.getCertificate(holder); return cert.getPublicKey(); } } //if we haven't returned yet, we couldn't find a key based on our preferences. String msg; if (encryptedPrivateFound && findPrivate) { msg = "Key resource [" + resource + "] contains a PKCS8 Encrypted PrivateKey. Only unencrypted " + "private keys are supported."; } else { msg = "Key resource [" + resource + "] did not contain a " + (findPrivate ? "private " : "public ") + "key."; } throw new IllegalArgumentException(msg); } }
From source file:edu.wisc.doit.tcrypt.AbstractPublicKeyDecrypter.java
License:Apache License
/** * Create an encrypter and decrypter using the specified key pair * //from w w w . ja va 2 s. com * @param keyPair The key pair to use */ public AbstractPublicKeyDecrypter(PEMKeyPair keyPair) throws IOException { super(keyPair.getPublicKeyInfo()); this.privateKeyParam = PrivateKeyFactory.createKey(keyPair.getPrivateKeyInfo()); }
From source file:org.albertschmitt.crypto.RSAService.java
License:Open Source License
/** * Extract the Public Key from the RSA Private Key from the input stream and return it to the client. * * @param instream//from w w w. ja v a 2 s . com * The input stream that contains the RSA Private Key. * @return The RSAPublicKey. * @throws java.io.IOException */ public RSAPublicKey readPublicKeyFromPrivate(InputStream instream) throws IOException { org.bouncycastle.openssl.PEMKeyPair pkp; try (InputStreamReader reader = new InputStreamReader(instream)) { try (PEMParser pem = new PEMParser(reader)) { pkp = (PEMKeyPair) pem.readObject(); } } SubjectPublicKeyInfo pki = pkp.getPublicKeyInfo(); byte[] data = pki.getEncoded(); RSAPublicKey key = new RSAPublicKey(); key.setKey(PublicKeyFactory.createKey(data)); return key; }
From source file:org.crsh.auth.FilePublicKeyProvider.java
License:Open Source License
public Iterable<KeyPair> loadKeys() { if (!SecurityUtils.isBouncyCastleRegistered()) { throw new IllegalStateException("BouncyCastle must be registered as a JCE provider"); }/*from ww w .ja v a 2 s. com*/ List<KeyPair> keys = new ArrayList<KeyPair>(); for (String file : files) { try { Object o = KeyPairUtils.readKey(new InputStreamReader(new FileInputStream(file))); if (o instanceof KeyPair) { keys.add(new KeyPair(((KeyPair) o).getPublic(), null)); } else if (o instanceof PublicKey) { keys.add(new KeyPair((PublicKey) o, null)); } else if (o instanceof PEMKeyPair) { PEMKeyPair keyPair = (PEMKeyPair) o; JcaPEMKeyConverter converter = new JcaPEMKeyConverter(); keys.add(new KeyPair(converter.getPublicKey(keyPair.getPublicKeyInfo()), null)); } } catch (Exception e) { LOG.info("Unable to read key {}: {}", file, e); } } return keys; }
From source file:org.diqube.ticket.TicketRsaKeyManager.java
License:Open Source License
@PostConstruct public void initialize() { List<RSAKeyParameters> allPublicKeys = new ArrayList<>(); publicValidationKeys = Collections.unmodifiableList(allPublicKeys); provider.getPemFiles().whenComplete((pemFiles, error) -> { if (error != null) throw new RuntimeException("Exception while identifying .pem files!", error); if (pemFiles.isEmpty()) throw new RuntimeException("No .pem files configured that can be used to sign/validate tickets!"); for (int i = 0; i < pemFiles.size(); i++) { String pemFileName = pemFiles.get(i).getLeft(); String pemPassword = pemFiles.get(i).getRight(); try (InputStream pemStream = pemFiles.get(i).getMiddle().get()) { Reader pemReader = new InputStreamReader(pemStream); try (PEMParser parser = new PEMParser(pemReader)) { Object o = parser.readObject(); SubjectPublicKeyInfo publicKeyInfo = null; PrivateKeyInfo privateKeyInfo = null; if (o instanceof PEMEncryptedKeyPair) { if (pemPassword == null) throw new RuntimeException("PEM file '" + pemFileName + "' is password protected, but the password is not configured."); PEMDecryptorProvider decryptionProvider = new JcePEMDecryptorProviderBuilder() .build(pemPassword.toCharArray()); PEMEncryptedKeyPair encryptedKeyPair = (PEMEncryptedKeyPair) o; PEMKeyPair keyPair = encryptedKeyPair.decryptKeyPair(decryptionProvider); publicKeyInfo = keyPair.getPublicKeyInfo(); privateKeyInfo = keyPair.getPrivateKeyInfo(); } else if (o instanceof PEMKeyPair) { PEMKeyPair keyPair = (PEMKeyPair) o; publicKeyInfo = keyPair.getPublicKeyInfo(); privateKeyInfo = keyPair.getPrivateKeyInfo(); } else if (o instanceof SubjectPublicKeyInfo) publicKeyInfo = (SubjectPublicKeyInfo) o; else throw new RuntimeException("Could not identify content of pem file '" + pemFileName + "': " + o.toString()); if (publicKeyInfo == null) throw new RuntimeException("Could not load '" + pemFileName + "' because it did not contain a public key."); if (privateKeyInfo == null) logger.info("Loading public key from '{}'.", pemFileName); else if (!provider.filesWithPrivateKeyAreRequired()) throw new RuntimeException("File '" + pemFileName + "' contains a private key, but only public keys are " + "accepted. Please extract the public key from the current file and configure diqube to use " + "that new file."); allPublicKeys.add((RSAKeyParameters) PublicKeyFactory.createKey(publicKeyInfo)); if (i == 0 && privateKeyInfo != null) { logger.info("Loading private key from '{}' and will use that for signing tickets.", pemFileName); privateSigningKey = (RSAPrivateCrtKeyParameters) PrivateKeyFactory .createKey(privateKeyInfo); }/* w w w .j a v a 2 s . c o m*/ } } catch (IOException e) { throw new RuntimeException("Could not interact with '" + pemFileName + "'. Correct password?", e); } } if (privateSigningKey == null && provider.filesWithPrivateKeyAreRequired()) throw new RuntimeException("A .pem file containing a private key for signing tickets is required. " + "Make sure that the first configured .pem file contains a private key."); initialized = true; }); }
From source file:org.metaeffekt.dcc.commons.pki.KeyUtils.java
License:Apache License
public static PublicKey loadPublicKeyFromKeyPair(String file) throws IOException { PEMParser parser = new PEMParser(new FileReader(file)); try {//from www . j a v a 2 s .c o m PEMKeyPair pemObject = (PEMKeyPair) parser.readObject(); SubjectPublicKeyInfo info = pemObject.getPublicKeyInfo(); JcaPEMKeyConverter converter = new JcaPEMKeyConverter(); return converter.getPublicKey(info); } finally { IOUtils.closeQuietly(parser); } }
From source file:org.tdmx.client.crypto.certificate.PrivateKeyIOUtils.java
License:Open Source License
public static KeyPair pemToRSAPrivateKeyPair(String input) throws CryptoCertificateException { StringReader sr = new StringReader(input); PEMParser pp = new PEMParser(sr); Object o = null;//w w w . j a v a 2 s .co m try { while ((o = pp.readObject()) != null) { if (o instanceof PEMKeyPair) { PEMKeyPair ch = (PEMKeyPair) o; byte[] pkbytes = ch.getPublicKeyInfo().getEncoded(ASN1Encoding.DER); KeyFactory kf = KeyFactory.getInstance(ALGORITHM); EncodedKeySpec eks = new X509EncodedKeySpec(pkbytes); PublicKey publicKey = kf.generatePublic(eks); byte[] privbytes = ch.getPrivateKeyInfo().getEncoded(ASN1Encoding.DER); EncodedKeySpec epks = new PKCS8EncodedKeySpec(privbytes); PrivateKey privateKey = kf.generatePrivate(epks); KeyPair kp = new KeyPair(publicKey, privateKey); return kp; } } } catch (IOException e) { throw new CryptoCertificateException(CertificateResultCode.ERROR_IO, e); } catch (NoSuchAlgorithmException e) { throw new CryptoCertificateException(CertificateResultCode.ERROR_MISSING_ALGORITHM, e); } catch (InvalidKeySpecException e) { throw new CryptoCertificateException(CertificateResultCode.ERROR_INVALID_KEY_SPEC, e); } finally { try { pp.close(); } catch (IOException e) { } } return null; }