List of usage examples for java.security KeyStore getDefaultType
public static final String getDefaultType()
From source file:org.glite.slcs.httpclient.ssl.ExtendedProtocolSocketFactory.java
/** * Creates and loads a truststore./*www .j av a 2s .c o m*/ * * @param path * The truststore filename in classpath or the absolute filename * @return A new initialized {@link KeyStore} containing the trust anchors. * @throws KeyStoreException * @throws NoSuchAlgorithmException * @throws CertificateException * @throws IOException * If an error occurs while loading the truststore. */ private KeyStore createTrustStore(String path) throws KeyStoreException, NoSuchAlgorithmException, CertificateException, IOException { if (path == null) { throw new IllegalArgumentException("Trust keystore path may not be null"); } // first search file in classpath, then as absolute filename LOG.debug("Load truststore from classpath: /" + path); InputStream is = getClass().getResourceAsStream("/" + path); if (is == null) { LOG.debug("Not in classpath, load truststore from file: " + path); is = new FileInputStream(path); } KeyStore keystore = KeyStore.getInstance(KeyStore.getDefaultType()); keystore.load(is, null); return keystore; }
From source file:org.seedstack.seed.crypto.internal.EncryptionServiceFactoryTest.java
/** * Test method for/*from w w w. jav a2 s . c o m*/ * {@link org.seedstack.seed.crypto.internal.EncryptionServiceFactory#createEncryptionService(org.seedstack.seed.crypto.internal.KeyStoreDefinition, org.seedstack.seed.crypto.internal.CertificateDefinition)} * . Test a {@link NoSuchAlgorithmException} if keystore can not be loaded. * * @throws Exception if an error occurred */ @Test(expected = RuntimeException.class) public void testCreateEncryptionServiceWithKeystoreAlgorithmException( @Mocked final KeyStoreDefinition keyStoreDefinition, @Mocked final CertificateDefinition certificateDefinition, @Mocked final KeyStore keyStore, @Mocked final FileInputStream file, @SuppressWarnings("unused") @Mocked final EncryptionServiceImpl asymetricCrypting) throws Exception { new Expectations() { final String pathToKeystore = "pathToKeystore"; final String password = "password"; { keyStoreDefinition.getPath(); returns(pathToKeystore); KeyStore.getInstance(KeyStore.getDefaultType()); returns(keyStore); new FileInputStream(pathToKeystore); result = file; keyStoreDefinition.getPassword(); returns(password); keyStore.load(file, password.toCharArray()); result = new NoSuchAlgorithmException("dummy exception"); } }; EncryptionServiceFactory factory = new EncryptionServiceFactory(); factory.createEncryptionService(keyStoreDefinition, certificateDefinition); }
From source file:org.roda.core.plugins.plugins.characterization.ODFSignatureUtils.java
public static Path runDigitalSignatureSign(Path input, String ks, String alias, String password, String fileFormat) throws IOException, GeneralSecurityException, DocumentException { Security.addProvider(new BouncyCastleProvider()); Path output = Files.createTempFile("odfsigned", "." + fileFormat); KeyStore keystore = KeyStore.getInstance(KeyStore.getDefaultType()); try (InputStream storeStream = new FileInputStream(ks)) { keystore.load(storeStream, password.toCharArray()); X509Certificate certificate = (X509Certificate) keystore .getCertificate(keystore.aliases().nextElement()); Key key = keystore.getKey(alias, password.toCharArray()); try (ByteArrayInputStream bais = createSignature(input.toString(), certificate, key)) { File file = output.toFile(); if (file != null && bais != null) { byte[] buffer = new byte[2048]; int length = 0; try (FileOutputStream fos = new FileOutputStream(file)) { while ((length = bais.read(buffer)) >= 0) { fos.write(buffer, 0, length); }//from w ww.ja v a 2 s . c o m } } } } return output; }
From source file:org.parosproxy.paros.core.proxy.WithBasicInfrastructureIntegrationTest.java
/** * Helper method to set the root certificate for this test. * /*w w w . j av a 2s . c om*/ * @throws KeyStoreException * @throws NoSuchAlgorithmException * @throws CertificateException * @throws IOException * @throws UnrecoverableKeyException */ protected static void initializeRootCertificate() throws KeyStoreException, NoSuchAlgorithmException, CertificateException, IOException, UnrecoverableKeyException { final SslCertificateService service = SslCertificateServiceImpl.getService(); final byte[] bytes = Base64.decodeBase64( "_u3-7QAAAAIAAAABAAAAAQARb3dhc3BfemFwX3Jvb3RfY2EAAAE2LA_eqAAABQIwggT-MA4GCisGAQQBKgIRAQEFAASCBOqboCBB1-ZSxYZqYDwdOLd22xkq1R3-ZJMVlt2ad-SMKGTG52RVHNhAfYYUS14QDwD6LF2TxKuHxKQt-CH2QXGZaRtWNzksTLl50J-irz9l7cDNkS-HI7l3ZLK3Hl8Fr_tx3pi91h1uw9qc5xLl0euLZYDo-bnqtqmrvqzBXS7Th0cDa7IDWWTN1foxoyV-26lc9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ArDY6i23kRFWUSKZcdVMU0hwoq9YAfI_E3FWJ9QH5l_P5EWYK9om349w1Ypl5Y4n1V74yEHeRZo8Fw0BFjk9uLLUTa_NUqEP24q3q7v1MD6-kUZFJJxlyyiGwbtBglxymkMwXKQL1GEPSb0ZOoxjSbDxOUsDjLHW9A8Xh6JQKYL0lMJ6clplrbaNjwtMIM26xJozpRQwQIDAQABo1MwUTAgBgNVHQ4BAf8EFgQUY7QvhPIV67X8kAxEoQTCoyp3N60wDwYDVR0TAQH_BAUwAwEB_zALBgNVHQ8EBAMCAbYwDwYDVR0lBAgwBgYEVR0lADANBgkqhkiG9w0BAQUFAAOCAQEAX5pQ0Pcgy8lEzK3xJaztS3OjUoI1UhJKIqgQeXujXDowMqCLPFHGeN6uVhi4ktjFuun4sfwLBE7CXACl5ZMUpjArD18qdQKQ6glHJ9HYrxKi0lJM2maYt5rkpAhHI8EHnX5IdsDEr2ihoCQUou1h8gGHIe1a6QkeCD_1VrRfKAdwt6UTE6RyaP6nTyr58IZ6Pdw5EQP3FmtA0yXRVapHodQPpudkS24Lj9cxSrpo_gJBSIb-LHpl81TVZg0SAROikcDk69qZDgnbQqLPz6WxsZtWcET1ACOqPHovQIPK6utkyOeOLm-uJL3Iqx84b4yQqnFkG-y-nlDmNWR33DOtE8rTqmBfifcjGibDbx0z26ogkB6I"); final ByteArrayInputStream bais = new ByteArrayInputStream(bytes); final KeyStore rootca = KeyStore.getInstance(KeyStore.getDefaultType()); rootca.load(bais, "0w45P.Z4p".toCharArray()); bais.close(); service.initializeRootCA(rootca); }
From source file:io.restassured.config.SSLConfig.java
/** * Creates a new SSL Config instance with the following settings: * <ul>// w w w .j ava 2 s . com * <li>No keystore</li> * <li>No truststore</li> * <li>No password</li> * <li>{@link java.security.KeyStore#getDefaultType()}</li> * <li>No explicit default port</li> * <li>No trust store</li> * <li>No SSLSocketFactory</li> * <li>{@link org.apache.http.conn.ssl.SSLSocketFactory#STRICT_HOSTNAME_VERIFIER} as {@link X509HostnameVerifier} implementation</li> * </ul> */ public SSLConfig() { this(null, null, null, null, KeyStore.getDefaultType(), KeyStore.getDefaultType(), UNDEFINED_PORT, null, null, STRICT_HOSTNAME_VERIFIER, null, false); }
From source file:com.esri.geoevent.test.performance.provision.GeoEventProvisioner.java
private SSLConnectionSocketFactory getSSLSocketFactory() { KeyStore trustStore;// www . j a v a2 s .com try { trustStore = KeyStore.getInstance(KeyStore.getDefaultType()); trustStore.load(null, null); TrustStrategy trustStrategy = new TrustStrategy() { @Override public boolean isTrusted(X509Certificate[] chain, String authType) throws CertificateException { return true; } }; SSLContextBuilder sslContextBuilder = new SSLContextBuilder(); sslContextBuilder.loadTrustMaterial(trustStore, trustStrategy); sslContextBuilder.useTLS(); SSLContext sslContext = sslContextBuilder.build(); SSLConnectionSocketFactory sslSocketFactory = new SSLConnectionSocketFactory(sslContext); return sslSocketFactory; } catch (GeneralSecurityException | IOException e) { System.err.println("SSL Error : " + e.getMessage()); } return null; }
From source file:org.wso2.carbon.identity.openidconnect.util.TestUtils.java
/** * Read Keystore from the file identified by given keystorename, password * * @param keystoreName/*w w w . j ava 2s . c o m*/ * @param password * @param home * @return * @throws Exception */ public static KeyStore getKeyStoreFromFile(String keystoreName, String password, String home) throws Exception { Path tenantKeystorePath = Paths.get(home, "repository", "resources", "security", keystoreName); FileInputStream file = new FileInputStream(tenantKeystorePath.toString()); KeyStore keystore = KeyStore.getInstance(KeyStore.getDefaultType()); keystore.load(file, password.toCharArray()); return keystore; }
From source file:org.openmrs.module.rheashradapter.util.GenerateORU_R01Alert.java
public void sendRequest(String msg, Encounter e) throws IOException, TransformerFactoryConfigurationError, TransformerException, KeyStoreException, NoSuchAlgorithmException, CertificateException, KeyManagementException { // Get the key store that includes self-signed cert as a "trusted" // entry.//w w w .j a va 2s.co m InputStream keyStoreStream = GenerateORU_R01Alert.class.getResourceAsStream("/truststore-prod.jks"); // Load the keyStore KeyStore keyStore = KeyStore.getInstance(KeyStore.getDefaultType()); keyStore.load(keyStoreStream, keystorePassword.toCharArray()); log.info("KeyStoreStream = " + IOUtils.toString(keyStoreStream)); keyStoreStream.close(); TrustManagerFactory tmf = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm()); tmf.init(keyStore); SSLContext ctx = SSLContext.getInstance("TLS"); ctx.init(null, tmf.getTrustManagers(), null); // set SSL Factory to be used for all HTTPS connections sslFactory = ctx.getSocketFactory(); callQueryFacility(msg, e); }
From source file:org.teiid.resource.adapter.ftp.FtpManagedConnectionFactory.java
public void setCertificate(String certificate) { this.certificate = certificate; if (this.certificate != null && Files.exists(Paths.get(this.certificate))) { try {/* w w w. j a v a2 s . c om*/ CertificateFactory certificateFactory = CertificateFactory.getInstance("X.509"); //$NON-NLS-1$ InputStream in = Files.newInputStream(Paths.get(this.certificate)); Certificate cert = certificateFactory.generateCertificate(in); KeyStore keyStore = KeyStore.getInstance(KeyStore.getDefaultType()); keyStore.load(null); keyStore.setCertificateEntry("alias", cert); //$NON-NLS-1$ trustManager = TrustManagerUtils.getDefaultTrustManager(keyStore); } catch (IOException | GeneralSecurityException e) { throw new TeiidRuntimeException(UTIL.getString("ftp_certificate_path", certificate, e)); //$NON-NLS-1$ } } }
From source file:sabina.integration.TestScenario.java
/** * Convenience method to use own truststore on SSL Sockets. Will default to * the self signed keystore provided in resources, but will respect * <p>//from w w w . j a va 2 s .c om * -Djavax.net.ssl.keyStore=serverKeys * -Djavax.net.ssl.keyStorePassword=password * -Djavax.net.ssl.trustStore=serverTrust * -Djavax.net.ssl.trustStorePassword=password SSLApplication * <p> * So these can be used to specify other key/trust stores if required. * * @return an SSL Socket Factory using either provided keystore OR the * keystore specified in JVM params */ private SSLSocketFactory getSslFactory() { KeyStore keyStore; try { keyStore = KeyStore.getInstance(KeyStore.getDefaultType()); FileInputStream fis = new FileInputStream(getTrustStoreLocation()); keyStore.load(fis, getTrustStorePassword().toCharArray()); fis.close(); TrustManagerFactory tmf = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm()); tmf.init(keyStore); SSLContext ctx = SSLContext.getInstance("TLS"); ctx.init(null, tmf.getTrustManagers(), null); return ctx.getSocketFactory(); } catch (Exception e) { e.printStackTrace(); return null; } }