Example usage for java.security KeyStore load

List of usage examples for java.security KeyStore load

Introduction

In this page you can find the example usage for java.security KeyStore load.

Prototype

public final void load(InputStream stream, char[] password)
        throws IOException, NoSuchAlgorithmException, CertificateException 

Source Link

Document

Loads this KeyStore from the given input stream.

Usage

From source file:com.tremolosecurity.openunison.util.OpenUnisonUtils.java

private static KeyStore loadKeyStore(String ksPath, TremoloType tt) throws Exception {
    KeyStore ks = KeyStore.getInstance("PKCS12");
    try {/*from   w  w w . j av a2  s .  co m*/
        InputStream in = new FileInputStream(ksPath);
        ks.load(in, tt.getKeyStorePassword().toCharArray());
    } catch (Throwable t) {
        ks = KeyStore.getInstance("JCEKS");
        InputStream in = new FileInputStream(ksPath);
        ks.load(in, tt.getKeyStorePassword().toCharArray());
    }

    return ks;
}

From source file:org.everit.authentication.cas.ecm.tests.SecureHttpClient.java

/**
 * Constructor.//www .  ja v  a 2 s .c  o m
 */
public SecureHttpClient(final String principal, final BundleContext bundleContext) throws Exception {
    this.principal = principal;

    httpClientContext = HttpClientContext.create();
    httpClientContext.setCookieStore(new BasicCookieStore());

    KeyStore trustStore = KeyStore.getInstance("jks");
    trustStore.load(bundleContext.getBundle().getResource("/jetty-keystore").openStream(),
            "changeit".toCharArray());

    TrustManagerFactory trustManagerFactory = TrustManagerFactory
            .getInstance(TrustManagerFactory.getDefaultAlgorithm());
    trustManagerFactory.init(trustStore);
    TrustManager[] trustManagers = trustManagerFactory.getTrustManagers();

    SSLContext sslContext = SSLContext.getInstance("TLS");
    sslContext.init(null, trustManagers, new SecureRandom());

    httpClient = HttpClientBuilder.create().setSslcontext(sslContext)
            .setRedirectStrategy(new DefaultRedirectStrategy()).build();
}

From source file:edu.rit.csh.androidwebnews.WebnewsHttpClient.java

/**
 * Makes the SSL cert work correctly./*from w w w  .  j av  a  2  s  . c o  m*/
 *
 * @return SSLSocketFactory - provides the SSLFactory for communicating
 *         with the scheme
 */
private SSLSocketFactory newSslSocketFactory() {
    try {
        // Get an instance of the Bouncy Castle KeyStore format
        KeyStore trusted = KeyStore.getInstance(KeyStore.getDefaultType());
        trusted.load(null, null);
        // Pass the keystore to the SSLSocketFactory. The factory is responsible
        // for the verification of the server certificate.
        SSLSocketFactory sf = new WebnewsSocketFactory(trusted);
        // Hostname verification from certificate
        // http://hc.apache.org/httpcomponents-client-ga/tutorial/html/connmgmt.html#d4e506
        sf.setHostnameVerifier(SSLSocketFactory.STRICT_HOSTNAME_VERIFIER);
        return sf;
    } catch (Exception e) {
        throw new AssertionError(e);
    }
}

From source file:com.terradue.dsi.wire.KeyManagerProvider.java

@Override
public KeyManager[] get() {
    final char[] password = this.password.toCharArray();

    try {//from   w  ww.  ja  v a2 s  . c om
        final KeyStore store = new KeyMaterial(certificate, certificate, password).getKeyStore();
        store.load(null, password);

        // initialize key and trust managers -> default behavior
        final KeyManagerFactory keyManagerFactory = KeyManagerFactory.getInstance("SunX509");
        // password for key and store have to be the same IIRC
        keyManagerFactory.init(store, password);

        return keyManagerFactory.getKeyManagers();
    } catch (Exception e) {
        throw new ProvisionException("Impossible to initialize SSL certificate/key", e);
    }
}

From source file:sample.tomcat.X509ApplicationTests.java

private SSLConnectionSocketFactory socketFactory() throws Exception {
    char[] password = "password".toCharArray();
    KeyStore truststore = KeyStore.getInstance("PKCS12");
    truststore.load(getKeyStoreFile(), password);
    SSLContextBuilder builder = new SSLContextBuilder();
    builder.loadKeyMaterial(truststore, password);
    builder.loadTrustMaterial(truststore, new TrustSelfSignedStrategy());
    return new SSLConnectionSocketFactory(builder.build(), new AllowAllHostnameVerifier());
}

From source file:net.sf.taverna.t2.security.credentialmanager.impl.CredentialManagerImplTest.java

/**
 * @throws java.lang.Exception// ww  w .j a v  a2 s.co m
 */
@BeforeClass
public static void setUpBeforeClass() throws Exception {

    // Just in case, add the BouncyCastle provider
    // It gets added from the CredentialManagerImpl constructor as well
    // but we may need some crypto operations before we invoke the Cred. Manager 
    Security.addProvider(new BouncyCastleProvider());

    // Create a test username and password for a service
    serviceURI = new URI("http://someservice");
    usernamePassword = new UsernamePassword("testuser", "testpasswd");

    // Load the test private key and its certificate
    File privateKeyCertFile = new File(privateKeyFileURL.getPath());
    KeyStore pkcs12Keystore = java.security.KeyStore.getInstance("PKCS12", "BC"); // We have to use the BC provider here as the certificate chain is not loaded if we use whichever provider is first in Java!!!
    FileInputStream inStream = new FileInputStream(privateKeyCertFile);
    pkcs12Keystore.load(inStream, privateKeyAndPKCS12KeystorePassword.toCharArray());
    // KeyStore pkcs12Keystore = credentialManager.loadPKCS12Keystore(privateKeyCertFile, privateKeyPassword);
    Enumeration<String> aliases = pkcs12Keystore.aliases();
    while (aliases.hasMoreElements()) {
        // The test-private-key-cert.p12 file contains only one private key
        // and corresponding certificate entry
        String alias = aliases.nextElement();
        if (pkcs12Keystore.isKeyEntry(alias)) { // is it a (private) key entry?
            privateKey = pkcs12Keystore.getKey(alias, privateKeyAndPKCS12KeystorePassword.toCharArray());
            privateKeyCertChain = pkcs12Keystore.getCertificateChain(alias);
            break;
        }
    }
    inStream.close();

    // Load the test trusted certificate (belonging to *.Google.com)
    File trustedCertFile = new File(trustedCertficateFileURL.getPath());
    inStream = new FileInputStream(trustedCertFile);
    CertificateFactory certFactory = CertificateFactory.getInstance("X.509");
    trustedCertficate = (X509Certificate) certFactory.generateCertificate(inStream);
    try {
        inStream.close();
    } catch (Exception e) {
        // Ignore
    }

    keystoreChangedObserver = new Observer<KeystoreChangedEvent>() {
        @Override
        public void notify(Observable<KeystoreChangedEvent> sender, KeystoreChangedEvent message)
                throws Exception {
            // TODO Auto-generated method stub
        }
    };
}

From source file:com.github.mrstampy.gameboot.otp.OtpTestConfiguration.java

/**
 * Ssl context.//from w w  w.j a  v a2  s .  c o m
 *
 * @return the SSL context
 * @throws Exception
 *           the exception
 */
@Bean(name = SERVER_SSL_CONTEXT)
public SSLContext sslContext() throws Exception {
    char[] password = HARDCODED_NSA_APPROVED_PASSWORD.toCharArray();

    KeyStore keystore = getKeyStore();
    keystore.load(getResource(JKS_LOCATION), password);

    KeyManagerFactory kmf = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());

    kmf.init(keystore, password);

    return createContext(keystore, kmf);
}

From source file:no.difi.sdp.client.SikkerDigitalPostKlientIntegrationTest.java

private Noekkelpar avsenderNoekkelpar() {
    try {//from   w w  w  . j a va 2 s  .  c  o m
        String alias = "meldingsformidler";
        String passphrase = "abcd1234";
        String keyStoreFile = "/keystore.jce";

        KeyStore keyStore = KeyStore.getInstance("JCEKS");
        keyStore.load(new ClassPathResource(keyStoreFile).getInputStream(), passphrase.toCharArray());
        return Noekkelpar.fraKeyStore(keyStore, alias, passphrase);
    } catch (Exception e) {
        throw new RuntimeException("Kunne ikke laste nkkelpar for kjring av tester. "
                + "For  kjre integrasjonstester m det ligge inne et gyldig virksomhetssertifikat for test (med tilhrende certificate chain). "
                + "Keystore med tilhrende alias og passphrase settes i " + this.getClass().getSimpleName()
                + ".", e);
    }
}

From source file:net.sf.hajdbc.codec.crypto.CipherCodecFactoryTest.java

@Before
public void before() throws Exception {
    File file = File.createTempFile("ha-jdbc", "keystore");

    SecretKeyFactory factory = SecretKeyFactory.getInstance(ALGORITHM);
    this.key = factory.generateSecret(new DESKeySpec(Base64.decodeBase64(KEY.getBytes())));
    KeyStore store = KeyStore.getInstance(CipherCodecFactory.Property.KEYSTORE_TYPE.defaultValue);
    store.load(null, null);
    store.setKeyEntry(CipherCodecFactory.Property.KEY_ALIAS.defaultValue, this.key, KEY_PASSWORD.toCharArray(),
            null);//  ww w .  jav  a 2s . c o  m

    FileOutputStream out = new FileOutputStream(file);
    try {
        store.store(out, STORE_PASSWORD.toCharArray());
    } finally {
        Resources.close(out);
    }

    System.setProperty(CipherCodecFactory.Property.KEYSTORE_FILE.name, file.getPath());
    System.setProperty(CipherCodecFactory.Property.KEYSTORE_PASSWORD.name, STORE_PASSWORD);
    System.setProperty(CipherCodecFactory.Property.KEY_PASSWORD.name, KEY_PASSWORD);
}