Tabnine Logo
KeyStore
Code IndexAdd Tabnine to your IDE (free)

How to use
KeyStore
in
java.security

Best Java code snippets using java.security.KeyStore (Showing top 20 results out of 15,111)

Refine searchRefine arrow

  • FileInputStream
  • TrustManagerFactory
  • SSLContext
  • KeyManagerFactory
  • Enumeration
  • InputStream
origin: square/okhttp

 private static SSLContext sslContext(String keystoreFile, String password)
   throws GeneralSecurityException, IOException {
  KeyStore keystore = KeyStore.getInstance(KeyStore.getDefaultType());
  try (InputStream in = new FileInputStream(keystoreFile)) {
   keystore.load(in, password.toCharArray());
  }
  KeyManagerFactory keyManagerFactory =
    KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
  keyManagerFactory.init(keystore, password.toCharArray());

  TrustManagerFactory trustManagerFactory =
    TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
  trustManagerFactory.init(keystore);

  SSLContext sslContext = SSLContext.getInstance("TLS");
  sslContext.init(
    keyManagerFactory.getKeyManagers(),
    trustManagerFactory.getTrustManagers(),
    new SecureRandom());

  return sslContext;
 }
}
origin: apache/kafka

static List<CertificateEntries> create(KeyStore keystore) throws GeneralSecurityException {
  Enumeration<String> aliases = keystore.aliases();
  List<CertificateEntries> entries = new ArrayList<>();
  while (aliases.hasMoreElements()) {
    String alias = aliases.nextElement();
    Certificate cert  = keystore.getCertificate(alias);
    if (cert instanceof X509Certificate)
      entries.add(new CertificateEntries((X509Certificate) cert));
  }
  return entries;
}
origin: stackoverflow.com

 InputStream is = new FileInputStream("cacert.crt");
// You could get a resource as a stream instead.

CertificateFactory cf = CertificateFactory.getInstance("X.509");
X509Certificate caCert = (X509Certificate)cf.generateCertificate(is);

TrustManagerFactory tmf = TrustManagerFactory
  .getInstance(TrustManagerFactory.getDefaultAlgorithm());
KeyStore ks = KeyStore.getInstance(KeyStore.getDefaultType());
ks.load(null); // You don't need the KeyStore instance to come from a file.
ks.setCertificateEntry("caCert", caCert);

tmf.init(ks);

SSLContext sslContext = SSLContext.getInstance("TLS");
sslContext.init(null, tmf.getTrustManagers(), null);
origin: square/okhttp

private KeyStore newEmptyKeyStore(char[] password) throws GeneralSecurityException {
 try {
  KeyStore keyStore = KeyStore.getInstance(KeyStore.getDefaultType());
  InputStream in = null; // By convention, 'null' creates an empty key store.
  keyStore.load(in, password);
  return keyStore;
 } catch (IOException e) {
  throw new AssertionError(e);
 }
}
origin: redisson/redisson

/**
 * Generates a new {@link KeyStore}.
 *
 * @param certChain a X.509 certificate chain
 * @param key a PKCS#8 private key
 * @param keyPasswordChars the password of the {@code keyFile}.
 *                    {@code null} if it's not password-protected.
 * @return generated {@link KeyStore}.
 */
static KeyStore buildKeyStore(X509Certificate[] certChain, PrivateKey key, char[] keyPasswordChars)
    throws KeyStoreException, NoSuchAlgorithmException,
        CertificateException, IOException {
  KeyStore ks = KeyStore.getInstance(KeyStore.getDefaultType());
  ks.load(null, null);
  ks.setKeyEntry(ALIAS, key, keyPasswordChars, certChain);
  return ks;
}
origin: stackoverflow.com

 protected org.apache.http.conn.ssl.SSLSocketFactory createAdditionalCertsSSLSocketFactory() {
  try {
    final KeyStore ks = KeyStore.getInstance("BKS");

    // the bks file we generated above
    final InputStream in = context.getResources().openRawResource( R.raw.mystore);  
    try {
      // don't forget to put the password used above in strings.xml/mystore_password
      ks.load(in, context.getString( R.string.mystore_password ).toCharArray());
    } finally {
      in.close();
    }

    return new AdditionalKeyStoresSSLSocketFactory(ks);

  } catch( Exception e ) {
    throw new RuntimeException(e);
  }
}
origin: apache/geode

private void populateMap() {
 try {
  final KeyStore keyStore = KeyStore.getInstance("JKS");
  final char[] passPhrase = this.pubKeyPass != null ? this.pubKeyPass.toCharArray() : null;
  final FileInputStream keyStoreFile = new FileInputStream(this.pubKeyFilePath);
  try {
   keyStore.load(keyStoreFile, passPhrase);
  } finally {
   keyStoreFile.close();
  }
  for (Enumeration e = keyStore.aliases(); e.hasMoreElements();) {
   final Object alias = e.nextElement();
   final Certificate cert = keyStore.getCertificate((String) alias);
   if (cert instanceof X509Certificate) {
    this.aliasCertificateMap.put(alias, cert);
   }
  }
 } catch (Exception e) {
  throw new AuthenticationFailedException(
    "Exception while getting public keys: " + e.getMessage(), e);
 }
}
origin: google/data-transfer-project

private SSLSocketFactory getSocketFactory() throws GeneralSecurityException, IOException {
 KeyManagerFactory keyManagerFactory = KeyManagerFactory.getInstance("SunX509");
 KeyStore keyStore = KeyStore.getInstance("PKCS12");
 InputStream keyInput = new FileInputStream(pathToPkcs12File);
 keyStore.load(keyInput, password.toCharArray());
 keyInput.close();
 keyManagerFactory.init(keyStore, password.toCharArray());
 SSLContext context = SSLContext.getInstance("TLS");
 context.init(keyManagerFactory.getKeyManagers(), null, new SecureRandom());
 return context.getSocketFactory();
}
origin: rapidoid/rapidoid

private static KeyManager[] initKeyManagers(String keystore, char[] keystorePassword, char[] keyManagerPassword) throws Exception {
  KeyStore keyStore = KeyStore.getInstance("JKS");
  keyStore.load(new FileInputStream(keystore), keystorePassword);
  KeyManagerFactory keyManagerFactory = KeyManagerFactory.getInstance("SunX509");
  keyManagerFactory.init(keyStore, keyManagerPassword);
  return keyManagerFactory.getKeyManagers();
}
origin: apache/usergrid

InputStream in = new FileInputStream( file );
KeyStore ks = KeyStore.getInstance( KeyStore.getDefaultType() );
ks.load( in, passphrase );
in.close();
SSLContext context = SSLContext.getInstance( "TLS" );
TrustManagerFactory tmf = TrustManagerFactory.getInstance( TrustManagerFactory.getDefaultAlgorithm() );
tmf.init( ks );
X509TrustManager defaultTrustManager = ( X509TrustManager ) tmf.getTrustManagers()[0];
SavingTrustManager tm = new SavingTrustManager( defaultTrustManager );
context.init( null, new TrustManager[] { tm }, null );
SSLSocketFactory factory = context.getSocketFactory();
ks.setCertificateEntry( host, cert );
ks.store( out, passphrase );
out.close();
origin: Tencent/tinker

private String getSignatureAlgorithm() throws Exception {
  InputStream is = null;
  try {
    is = new BufferedInputStream(new FileInputStream(config.mSignatureFile));
    KeyStore keyStore = KeyStore.getInstance("JKS");
    keyStore.load(is, config.mStorePass.toCharArray());
    Key key = keyStore.getKey(config.mStoreAlias, config.mKeyPass.toCharArray());
    String keyAlgorithm = key.getAlgorithm();
    String signatureAlgorithm;
    if (keyAlgorithm.equalsIgnoreCase("DSA")) {
      signatureAlgorithm = "SHA1withDSA";
    } else if (keyAlgorithm.equalsIgnoreCase("RSA")) {
      signatureAlgorithm = "SHA1withRSA";
    } else if (keyAlgorithm.equalsIgnoreCase("EC")) {
      signatureAlgorithm = "SHA1withECDSA";
    } else {
      throw new RuntimeException("private key is not a DSA or "
          + "RSA key");
    }
    return signatureAlgorithm;
  } finally {
    StreamUtil.closeQuietly(is);
  }
}
origin: stackoverflow.com

KeyStore ks = KeyStore.getInstance("JKS");
 InputStream ksIs = new FileInputStream("...");
 try {
   ks.load(ksIs, "password".toCharArray());
 } finally {
   if (ksIs != null) {
     ksIs.close();
   }
 }
 KeyManagerFactory kmf = KeyManagerFactory.getInstance(KeyManagerFactory
     .getDefaultAlgorithm());
 kmf.init(ks, "keypassword".toCharArray());
origin: apache/usergrid

InputStream in = new FileInputStream( file );
KeyStore ks = KeyStore.getInstance( KeyStore.getDefaultType() );
ks.load( in, passphrase );
in.close();
  ks.setCertificateEntry( hostname, cert );
  LOG.debug( "Added certificate to keystore 'jssecacerts' using alias '" + hostname + "'" );
ks.store( out, passphrase );
out.close();
origin: apache/hbase

protected void load(URI uri) throws IOException {
 String path = uri.getPath();
 if (path == null || path.isEmpty()) {
  throw new RuntimeException("KeyProvider parameters should specify a path");
 }
 InputStream is = new FileInputStream(new File(path));
 try {
  store.load(is, password);
 } catch (NoSuchAlgorithmException e) {
  throw new RuntimeException(e);
 } catch (CertificateException e) {
  throw new RuntimeException(e);
 } finally {
  is.close();
 }
}
origin: stackoverflow.com

KeyStore ks = KeyStore.getInstance("JKS");
 // get user password and file input stream
 char[] password = ("mykspassword")).toCharArray();
 ClassLoader cl = this.getClass().getClassLoader();
 InputStream stream = cl.getResourceAsStream("myjks.jks");
 ks.load(stream, password);
 stream.close();
 SSLContext sc = SSLContext.getInstance("TLS");
 KeyManagerFactory kmf = KeyManagerFactory.getInstance("SunX509");
 TrustManagerFactory tmf = TrustManagerFactory.getInstance("SunX509");
 kmf.init(ks, password);
 tmf.init(ks);
 sc.init(kmf.getKeyManagers(), tmf.getTrustManagers(),null);
 return sc.getSocketFactory();
origin: gocd/gocd

KeyStore agentTruststore() throws IOException, CertificateException, KeyStoreException, NoSuchAlgorithmException {
  KeyStore trustStore = null;
  List<X509Certificate> certificates = new CertificateFileParser().certificates(rootCertFile);
  for (X509Certificate certificate : certificates) {
    if (trustStore == null) {
      trustStore = KeyStore.getInstance(KeyStore.getDefaultType());
      trustStore.load(null, null);
    }
    trustStore.setCertificateEntry(certificate.getSubjectX500Principal().getName(), certificate);
  }
  return trustStore;
}
origin: apache/geode

private TrustManager[] getTrustManagers()
  throws KeyStoreException, NoSuchAlgorithmException, CertificateException, IOException {
 if (Objects.isNull(trustStorePath)) {
  return new TrustManager[0];
 }
 String trustStoreType = "jks";
 KeyStore keyStore = KeyStore.getInstance(trustStoreType);
 FileInputStream fileInputStream = new FileInputStream(trustStorePath);
 char[] password = "password".toCharArray();
 keyStore.load(fileInputStream, password);
 TrustManagerFactory tmf =
   TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
 tmf.init(keyStore);
 return tmf.getTrustManagers();
}
origin: apache/geode

private KeyManager[] getKeyManagers(SSLConfig sslConfig) throws Exception {
 FileInputStream keyStoreStream = null;
 KeyManagerFactory keyManagerFactory = null;
 try {
  if (StringUtils.isNotBlank(sslConfig.getKeystore())) {
   KeyStore clientKeys = KeyStore.getInstance(sslConfig.getKeystoreType());
   keyStoreStream = new FileInputStream(sslConfig.getKeystore());
   clientKeys.load(keyStoreStream, sslConfig.getKeystorePassword().toCharArray());
   keyManagerFactory =
     KeyManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
   keyManagerFactory.init(clientKeys, sslConfig.getKeystorePassword().toCharArray());
  }
 } finally {
  if (keyStoreStream != null) {
   keyStoreStream.close();
  }
 }
 return keyManagerFactory != null ? keyManagerFactory.getKeyManagers() : null;
}
origin: rhuss/jolokia

/**
 * Update a keystore with a CA certificate
 *
 * @param pTrustStore the keystore to update
 * @param pCaCert     CA cert as PEM used for the trust store
 */
public static void updateWithCaPem(KeyStore pTrustStore, File pCaCert)
    throws IOException, CertificateException, KeyStoreException, NoSuchAlgorithmException {
  InputStream is = new FileInputStream(pCaCert);
  try {
    CertificateFactory certFactory = CertificateFactory.getInstance("X509");
    X509Certificate cert = (X509Certificate) certFactory.generateCertificate(is);
    String alias = cert.getSubjectX500Principal().getName();
    pTrustStore.setCertificateEntry(alias, cert);
  } finally {
    is.close();
  }
}
origin: apache/zookeeper

@Override
public KeyStore loadKeyStore() throws IOException, GeneralSecurityException {
  KeyStore ks = KeyStore.getInstance(JKS_KEY_STORE_TYPE);
  InputStream inputStream = null;
  try {
    inputStream = new FileInputStream(new File(keyStorePath));
    ks.load(inputStream, passwordStringToCharArray(keyStorePassword));
    return ks;
  } finally {
    forceClose(inputStream);
  }
}
java.securityKeyStore

Javadoc

KeyStore is responsible for maintaining cryptographic keys and their owners.

The type of the system key store can be changed by setting the 'keystore.type' property in the file named JAVA_HOME/lib/security/java.security.

Most used methods

  • getInstance
    Generates a keystore object for the specified keystore type from the specified provider. Note: the p
  • load
    Loads this KeyStore using the specified LoadStoreParameter.
  • getDefaultType
    Returns the default keystore type as specified in the Java security properties file, or the string "
  • getCertificate
    Returns the certificate associated with the given alias.If the given alias name identifies atrusted
  • setCertificateEntry
    Assigns the given certificate to the given alias.If the given alias already exists in this keystore
  • getKey
    Returns the key associated with the given alias, using the given password to recover it.
  • aliases
    Lists all the alias names of this keystore.
  • setKeyEntry
    Assigns the given key (that has already been protected) to the given alias.If the protected key is o
  • store
    Stores this KeyStore using the specified LoadStoreParameter.
  • getCertificateChain
    Returns the certificate chain associated with the given alias.
  • getEntry
    Returns the Entry with the given alias, using the specified ProtectionParameter.
  • isKeyEntry
    Returns true if the entry identified by the given alias is akey entry, and false otherwise.
  • getEntry,
  • isKeyEntry,
  • containsAlias,
  • deleteEntry,
  • isCertificateEntry,
  • setEntry,
  • getCertificateAlias,
  • size,
  • getType,
  • getProvider

Popular in Java

  • Running tasks concurrently on multiple threads
  • orElseThrow (Optional)
    Return the contained value, if present, otherwise throw an exception to be created by the provided s
  • addToBackStack (FragmentTransaction)
  • getExternalFilesDir (Context)
  • FileInputStream (java.io)
    An input stream that reads bytes from a file. File file = ...finally if (in != null) in.clos
  • ConnectException (java.net)
    A ConnectException is thrown if a connection cannot be established to a remote host on a specific po
  • Arrays (java.util)
    This class contains various methods for manipulating arrays (such as sorting and searching). This cl
  • CountDownLatch (java.util.concurrent)
    A synchronization aid that allows one or more threads to wait until a set of operations being perfor
  • JFileChooser (javax.swing)
  • Options (org.apache.commons.cli)
    Main entry-point into the library. Options represents a collection of Option objects, which describ
  • Top PhpStorm plugins
Tabnine Logo
  • Products

    Search for Java codeSearch for JavaScript code
  • IDE Plugins

    IntelliJ IDEAWebStormVisual StudioAndroid StudioEclipseVisual Studio CodePyCharmSublime TextPhpStormVimGoLandRubyMineEmacsJupyter NotebookJupyter LabRiderDataGripAppCode
  • Company

    About UsContact UsCareers
  • Resources

    FAQBlogTabnine AcademyTerms of usePrivacy policyJava Code IndexJavascript Code Index
Get Tabnine for your IDE now