List of usage examples for java.security.cert CertificateFactory getInstance
public static final CertificateFactory getInstance(String type, Provider provider) throws CertificateException
From source file:org.gluu.oxtrust.ldap.service.SSLService.java
/** * Convert the supplied certificate object into an X509Certificate object. * * @param cert The Certificate object//from w ww . j a v a2 s .co m * @return The converted X509Certificate object * @throws Exception A problem occurred during the conversion */ public static X509Certificate convertCertificate(Certificate cert) throws Exception { CertificateFactory cf = CertificateFactory.getInstance(X509_CERT_TYPE, SECURITY_PROVIDER_BOUNCY_CASTLE); ByteArrayInputStream bais = new ByteArrayInputStream(cert.getEncoded()); return (X509Certificate) cf.generateCertificate(bais); }
From source file:de.brendamour.jpasskit.signing.PKSigningInformationUtil.java
/** * Load certificate file in DER format from the filesystem or the classpath * //from w ww . ja va 2 s . co m * @param filePath * @return * @throws IOException * @throws CertificateException */ public X509Certificate loadDERCertificate(final String filePath) throws IOException, CertificateException { FileInputStream certificateFileInputStream = null; try { File certFile = new File(filePath); if (!certFile.exists()) { // try loading it from the classpath URL localCertFile = PKFileBasedSigningUtil.class.getClassLoader().getResource(filePath); if (localCertFile == null) { throw new FileNotFoundException("File at " + filePath + " not found"); } certFile = new File(localCertFile.getFile()); } certificateFileInputStream = new FileInputStream(certFile); CertificateFactory certificateFactory = CertificateFactory.getInstance("X.509", BouncyCastleProvider.PROVIDER_NAME); Certificate certificate = certificateFactory.generateCertificate(certificateFileInputStream); if (certificate instanceof X509Certificate) { ((X509Certificate) certificate).checkValidity(); return (X509Certificate) certificate; } throw new IOException("The key from '" + filePath + "' could not be decrypted"); } catch (IOException ex) { throw new IOException("The key from '" + filePath + "' could not be decrypted", ex); } catch (NoSuchProviderException ex) { throw new IOException("The key from '" + filePath + "' could not be decrypted", ex); } finally { IOUtils.closeQuietly(certificateFileInputStream); } }
From source file:org.kse.crypto.x509.X509CertUtil.java
/** * Convert the supplied certificate object into an X509Certificate object. * * @param certIn//from ww w .j ava 2s. c om * The Certificate object * @return The converted X509Certificate object * @throws CryptoException * A problem occurred during the conversion */ public static X509Certificate convertCertificate(Certificate certIn) throws CryptoException { try { CertificateFactory cf = CertificateFactory.getInstance(X509_CERT_TYPE, BOUNCY_CASTLE.jce()); ByteArrayInputStream bais = new ByteArrayInputStream(certIn.getEncoded()); return (X509Certificate) cf.generateCertificate(bais); } catch (CertificateException | NoSuchProviderException e) { throw new CryptoException(res.getString("NoConvertCertificate.exception.message"), e); } }
From source file:de.brendamour.jpasskit.signing.PKSigningInformationUtil.java
/** * Load a DER Certificate from an <code>InputStream</code>. * /*from www . j a v a 2s . co m*/ * The caller is responsible for closing the stream after this method returns successfully or fails. * * @param certificateInputStream * <code>InputStream</code> containing the certificate. * @return Loaded certificate. * @throws IOException * @throws CertificateException */ public X509Certificate loadDERCertificate(final InputStream certificateInputStream) throws IOException, CertificateException { try { CertificateFactory certificateFactory = CertificateFactory.getInstance("X.509", BouncyCastleProvider.PROVIDER_NAME); Certificate certificate = certificateFactory.generateCertificate(certificateInputStream); if (certificate instanceof X509Certificate) { ((X509Certificate) certificate).checkValidity(); return (X509Certificate) certificate; } throw new IOException("The key from the input stream could not be decrypted"); } catch (IOException ex) { throw new IOException("The key from the input stream could not be decrypted", ex); } catch (NoSuchProviderException ex) { throw new IOException("The key from the input stream could not be decrypted", ex); } }
From source file:org.hyperledger.fabric.sdk.security.CryptoPrimitives.java
/** * Return X509Certificate from pem bytes. * So you may ask why this ? Well some providers (BC) seems to have problems with creating the * X509 cert from bytes so here we go through all available providers till one can convert. :) * * @param pemCertificate/* w w w. j av a 2 s . c om*/ * @return */ private X509Certificate getX509Certificate(byte[] pemCertificate) throws CryptoException { X509Certificate ret = null; CryptoException rete = null; List<Provider> providerList = new LinkedList<>(Arrays.asList(Security.getProviders())); if (SECURITY_PROVIDER != null) { //Add if overridden providerList.add(SECURITY_PROVIDER); } try { providerList.add(BouncyCastleProvider.class.newInstance()); // bouncy castle is there always. } catch (Exception e) { logger.warn(e); } for (Provider provider : providerList) { try { if (null == provider) { continue; } CertificateFactory certFactory = CertificateFactory.getInstance(CERTIFICATE_FORMAT, provider); if (null != certFactory) { try (ByteArrayInputStream bis = new ByteArrayInputStream(pemCertificate)) { Certificate certificate = certFactory.generateCertificate(bis); if (certificate instanceof X509Certificate) { ret = (X509Certificate) certificate; rete = null; break; } } } } catch (Exception e) { rete = new CryptoException(e.getMessage(), e); } } if (null != rete) { throw rete; } if (ret == null) { logger.error("Could not convert pem bytes"); } return ret; }
From source file:com.lastdaywaiting.example.kalkan.service.SecureManager.java
private Certificate createCerificateByFile(String fileName, String storeDescript) { CertPath cp = null;/*from w ww . j ava 2 s .c o m*/ try { InputStream inputStream = this.getClass().getResourceAsStream(fileName); CertificateFactory certFactory = CertificateFactory.getInstance("X.509", providerName); cp = certFactory.generateCertPath(inputStream, "PKCS7"); inputStream.close(); //IOUtils.closeQuietly(fis); } catch (Exception ex) { throw new RuntimeException( "ORE SIGN: ? ? ? ? '" + fileName + "' ? " + storeDescript + ".", ex); } List<? extends Certificate> certs = cp.getCertificates(); if (certs.size() == 1) { System.out.println(" ? " + fileName + " ? " + storeDescript); return certs.get(0); } else { throw new RuntimeException(" '" + fileName + "' ? " + storeDescript + " 1 ? " + certs.size()); } }
From source file:net.sf.keystore_explorer.crypto.x509.X509CertUtil.java
/** * Convert the supplied certificate object into an X509Certificate object. * * @param certIn// w w w. j a va 2s . co m * The Certificate object * @return The converted X509Certificate object * @throws CryptoException * A problem occurred during the conversion */ public static X509Certificate convertCertificate(Certificate certIn) throws CryptoException { try { CertificateFactory cf = CertificateFactory.getInstance(X509_CERT_TYPE, BOUNCY_CASTLE.jce()); ByteArrayInputStream bais = new ByteArrayInputStream(certIn.getEncoded()); return (X509Certificate) cf.generateCertificate(bais); } catch (CertificateException e) { throw new CryptoException(res.getString("NoConvertCertificate.exception.message"), e); } catch (NoSuchProviderException e) { throw new CryptoException(res.getString("NoConvertCertificate.exception.message"), e); } }
From source file:de.brendamour.jpasskit.signing.PKSigningUtil.java
public static X509Certificate loadDERCertificate(final String filePath) throws IOException, CertificateException { FileInputStream certificateFileInputStream = null; try {/*w ww. jav a 2s. c om*/ File certFile = new File(filePath); if (!certFile.exists()) { // try loading it from the classpath URL localCertFile = PKSigningUtil.class.getClassLoader().getResource(filePath); if (localCertFile == null) { throw new FileNotFoundException("File at " + filePath + " not found"); } certFile = new File(localCertFile.getFile()); } certificateFileInputStream = new FileInputStream(certFile); CertificateFactory certificateFactory = CertificateFactory.getInstance("X.509", BouncyCastleProvider.PROVIDER_NAME); Certificate certificate = certificateFactory.generateCertificate(certificateFileInputStream); if (certificate instanceof X509Certificate) { return (X509Certificate) certificate; } throw new IOException("The key from '" + filePath + "' could not be decrypted"); } catch (IOException ex) { throw new IOException("The key from '" + filePath + "' could not be decrypted", ex); } catch (NoSuchProviderException ex) { throw new IOException("The key from '" + filePath + "' could not be decrypted", ex); } finally { IOUtils.closeQuietly(certificateFileInputStream); } }
From source file:net.sf.keystore_explorer.crypto.x509.X509CertUtil.java
public static X509Certificate convertCertificate(Certificate certIn, String keyStoreType) throws CryptoException { try {//from w w w. j a v a 2 s . c o m CertificateFactory cf = null; if (keyStoreType.equals("HTKS")) { cf = CertificateFactory.getInstance(X509_CERT_TYPE, "GNU-PKI"); } else { cf = CertificateFactory.getInstance(X509_CERT_TYPE, BOUNCY_CASTLE.jce()); } ByteArrayInputStream bais = new ByteArrayInputStream(certIn.getEncoded()); return (X509Certificate) cf.generateCertificate(bais); } catch (CertificateException e) { throw new CryptoException(res.getString("NoConvertCertificate.exception.message"), e); } catch (NoSuchProviderException e) { throw new CryptoException(res.getString("NoConvertCertificate.exception.message"), e); } }
From source file:de.brendamour.jpasskit.signing.PKSigningUtil.java
/** * Load a DEAR Certificate from an <code>InputStream</code>. * //from w ww . j a va 2s.c o m * The caller is responsible for closing the stream after this method returns successfully or fails. * * @param certificateInputStream * <code>InputStream</code> containing the certificate. * @return Loaded certificate. * @throws IOException * @throws CertificateException */ public static X509Certificate loadDERCertificate(final InputStream certificateInputStream) throws IOException, CertificateException { try { CertificateFactory certificateFactory = CertificateFactory.getInstance("X.509", BouncyCastleProvider.PROVIDER_NAME); Certificate certificate = certificateFactory.generateCertificate(certificateInputStream); if (certificate instanceof X509Certificate) { return (X509Certificate) certificate; } throw new IOException("The key from the input stream could not be decrypted"); } catch (IOException ex) { throw new IOException("The key from the input stream could not be decrypted", ex); } catch (NoSuchProviderException ex) { throw new IOException("The key from the input stream could not be decrypted", ex); } }