List of usage examples for java.security.cert CertificateFactory generateCertificate
public final Certificate generateCertificate(InputStream inStream) throws CertificateException
From source file:com.github.mrstampy.gameboot.otp.OtpTestConfiguration.java
/** * Client context./*from ww w . j a v a 2 s.c o m*/ * * @return the SSL context * @throws Exception * the exception */ @Bean(name = CLIENT_SSL_CONTEXT) public SSLContext clientContext() throws Exception { CertificateFactory cf = CertificateFactory.getInstance(X_509); Certificate cert = cf.generateCertificate(getResource(CERT_LOCATION)); KeyStore keystore = getKeyStore(); keystore.load(null); keystore.setCertificateEntry(ALIAS, cert); return createContext(keystore, null); }
From source file:com.cedarsoft.crypt.CertTest.java
@Test public void testCert() throws Exception { DataInputStream inStream = new DataInputStream(getClass().getResource("/test.crt").openStream()); CertificateFactory cf = CertificateFactory.getInstance("X.509"); X509Certificate cert = (X509Certificate) cf.generateCertificate(inStream); inStream.close();//from w w w . j ava2 s . c o m assertNotNull(cert); cert.checkValidity(); Cipher cipher = Cipher.getInstance("RSA"); cipher.init(Cipher.DECRYPT_MODE, cert); byte[] clear = cipher.doFinal(Base64.decodeBase64(SCRAMBLED.getBytes())); assertEquals(PLAINTEXT, new String(clear)); }
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 av a 2s.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:com.googlecode.dex2jar.tools.ApkSign.java
@Override protected void doCommandLine() throws Exception { if (remainingArgs.length != 1) { usage();//from w w w .j ava 2s . c o m return; } File apkIn = new File(remainingArgs[0]); if (!apkIn.exists()) { System.err.println(apkIn + " is not exists"); usage(); return; } if (output == null) { if (apkIn.isDirectory()) { output = new File(apkIn.getName() + "-signed.apk"); } else { output = new File(FilenameUtils.getBaseName(apkIn.getName()) + "-signed.apk"); } } if (output.exists() && !forceOverwrite) { System.err.println(output + " exists, use --force to overwrite"); usage(); return; } File realJar; if (apkIn.isDirectory()) { realJar = File.createTempFile("d2j", ".jar"); realJar.deleteOnExit(); System.out.println("zipping " + apkIn + " -> " + realJar); OutHandler out = FileOut.create(realJar, true); try { new FileWalker().withStreamHandler(new OutAdapter(out)).walk(apkIn); } finally { IOUtils.closeQuietly(out); } } else { realJar = apkIn; } CertificateFactory certificateFactory = CertificateFactory.getInstance("X.509"); X509Certificate cert = (X509Certificate) certificateFactory .generateCertificate(ApkSign.class.getResourceAsStream("ApkSign.cer")); KeyFactory rSAKeyFactory = KeyFactory.getInstance("RSA"); PrivateKey privateKey = rSAKeyFactory.generatePrivate( new PKCS8EncodedKeySpec(IOUtils.toByteArray(ApkSign.class.getResourceAsStream("ApkSign.private")))); Class<?> clz; try { clz = Class.forName("com.android.signapk.SignApk"); } catch (ClassNotFoundException cnfe) { System.err.println("please run d2j-apk-sign in a sun compatible JRE (contains sun.security.*)"); return; } Method m = clz.getMethod("sign", X509Certificate.class, PrivateKey.class, boolean.class, File.class, File.class); m.setAccessible(true); System.out.println("sign " + realJar + " -> " + output); m.invoke(null, cert, privateKey, this.signWhole, realJar, output); }
From source file:eu.musesproject.client.connectionmanager.TLSManager.java
/** * Convert local certificate to BKS//from ww w .j a v a2s. c om * @param cerStream * @param alias * * @param password * @return keyStore */ private KeyStore convertCerToBKS(InputStream cerStream, String alias, char[] password) { KeyStore keyStore = null; try { keyStore = KeyStore.getInstance("BKS", "BC"); CertificateFactory factory = CertificateFactory.getInstance("X.509", "BC"); Certificate certificate = factory.generateCertificate(cerStream); keyStore.load(null, password); keyStore.setCertificateEntry(alias, certificate); } catch (Exception e) { Log.d(TAG, e.getLocalizedMessage()); } return keyStore; }
From source file:test.unit.be.e_contract.mycarenet.etee.EncryptionTokenTest.java
@Test public void testReadEncryptionToken() throws Exception { InputStream etkInputStream = EncryptionTokenTest.class.getResourceAsStream("/etk-fcorneli.der"); assertNotNull(etkInputStream);/*w w w . j a v a 2 s . com*/ CMSSignedData cmsSignedData = new CMSSignedData(etkInputStream); LOG.debug("SignedData version: " + cmsSignedData.getVersion()); SignerInformationStore signers = cmsSignedData.getSignerInfos(); LOG.debug("signers: " + signers.size()); SignerInformation signer = (SignerInformation) signers.getSigners().iterator().next(); SignerId signerId = signer.getSID(); LOG.debug("signer Id: " + signerId.getIssuer()); Store certificateStore = cmsSignedData.getCertificates(); @SuppressWarnings("unchecked") Collection<X509CertificateHolder> certificateCollection = certificateStore.getMatches(signerId); X509CertificateHolder certificateHolder = certificateCollection.iterator().next(); LOG.debug("certificate collection size: " + certificateCollection.size()); CertificateFactory certificateFactory = CertificateFactory.getInstance("X.509"); X509Certificate certificate = (X509Certificate) certificateFactory .generateCertificate(new ByteArrayInputStream(certificateHolder.getEncoded())); LOG.debug("signer certificate: " + certificate); CMSTypedData signedContent = cmsSignedData.getSignedContent(); byte[] data = (byte[]) signedContent.getContent(); X509Certificate encryptionCertificate = (X509Certificate) certificateFactory .generateCertificate(new ByteArrayInputStream(data)); LOG.debug("encryption certificate: " + encryptionCertificate); }
From source file:org.codice.ddf.security.sts.crl.CrlInterceptorTest.java
/** * Creates a mock message with a cert attached * * @param certificateString The string of the certificate to attach * @return A message object to be passed to the CrlInterceptor for testing * @throws CertificateException/*from ww w.jav a 2s . co m*/ */ private Message createMockMessageWithCert(String certificateString) throws CertificateException { // create mock objects Message message = mock(Message.class); HttpServletRequest request = mock(HttpServletRequest.class); when(message.get(AbstractHTTPDestination.HTTP_REQUEST)).thenReturn(request); // add in certificate InputStream stream = new ByteArrayInputStream(Base64.decodeBase64(certificateString.getBytes())); CertificateFactory factory = CertificateFactory.getInstance("X.509"); X509Certificate cert = (X509Certificate) factory.generateCertificate(stream); X509Certificate[] certs = new X509Certificate[] { cert }; when(request.getAttribute(("javax.servlet.request.X509Certificate"))).thenReturn(certs); return message; }
From source file:org.globus.gsi.stores.ResourceSigningPolicyStoreTest.java
private X509Certificate readCertificate(String certPath) { try {// w w w .j a va 2 s. c o m FileInputStream fr = new FileInputStream(certPath); CertificateFactory cf = CertificateFactory.getInstance("X509"); X509Certificate crt = (X509Certificate) cf.generateCertificate(fr); logger.info("Read certificate:"); logger.info("\tCertificate for: " + crt.getSubjectDN()); logger.info("\tCertificate issued by: " + crt.getIssuerDN()); logger.info("\tCertificate is valid from " + crt.getNotBefore() + " to " + crt.getNotAfter()); logger.info("\tCertificate SN# " + crt.getSerialNumber()); logger.info("\tGenerated with " + crt.getSigAlgName()); return crt; } catch (Exception e) { e.printStackTrace(); } return null; }
From source file:org.thingsboard.rule.engine.mqtt.credentials.CertPemClientCredentials.java
private X509Certificate readCertFile(String fileContent) throws Exception { X509Certificate certificate = null; if (fileContent != null && !fileContent.trim().isEmpty()) { fileContent = fileContent.replace("-----BEGIN CERTIFICATE-----", "") .replace("-----END CERTIFICATE-----", "").replaceAll("\\s", ""); byte[] decoded = Base64.decodeBase64(fileContent); CertificateFactory certFactory = CertificateFactory.getInstance("X.509"); certificate = (X509Certificate) certFactory.generateCertificate(new ByteArrayInputStream(decoded)); }/* w w w.j a va2 s. c om*/ return certificate; }
From source file:com.spotify.docker.client.DockerCertificates.java
private DockerCertificates(final Builder builder) throws DockerCertificateException { try {// w w w . ja va 2 s . co m 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, null); 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); } }