java.security.KeyStore.setKeyEntry()方法的使用及代码示例

x33g5p2x  于2022-01-23 转载在 其他  
字(11.3k)|赞(0)|评价(0)|浏览(335)

本文整理了Java中java.security.KeyStore.setKeyEntry()方法的一些代码示例,展示了KeyStore.setKeyEntry()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。KeyStore.setKeyEntry()方法的具体详情如下:
包路径:java.security.KeyStore
类名称:KeyStore
方法名:setKeyEntry

KeyStore.setKeyEntry介绍

[英]Associates the given alias with the key, password and certificate chain.

If the specified alias already exists, it will be reassigned.
[中]将给定别名与密钥、密码和证书链相关联。
如果指定的别名已存在,将重新分配它。

代码示例

代码示例来源: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: apache/nifi

X509Certificate clientCert = CertificateUtils.generateIssuedCertificate(reorderedDn, keyPair.getPublic(), null, certificate, caKeyPair, signingAlgorithm, days);
KeyStore keyStore = KeyStoreUtils.getKeyStore(KeystoreType.PKCS12.toString());
keyStore.load(null, null);
keyStore.setKeyEntry(NIFI_KEY, keyPair.getPrivate(), null, new Certificate[]{clientCert, certificate});
String password = TlsHelper.writeKeyStore(keyStore, outputStreamFactory, clientCertFile, clientPasswords.get(i), standaloneConfig.isClientPasswordsGenerated());

代码示例来源:origin: stackoverflow.com

KeyStore ks = KeyStore.getInstance("JKS");
ks.setKeyEntry("keyAlias", key, passwordForKeyCharArray, certChain);
OutputStream writeStream = new FileOutputStream(filePathToStore);
ks.store(writeStream, keystorePasswordCharArray);
writeStream.close();

代码示例来源:origin: floragunncom/search-guard

public static KeyStore toKeystore(final String authenticationCertificateAlias, final char[] password, final X509Certificate authenticationCertificate[], final PrivateKey authenticationKey) throws Exception {
  if(authenticationCertificateAlias != null && authenticationCertificate != null && authenticationKey != null) {          
    KeyStore ks = KeyStore.getInstance(JKS);
    ks.load(null, null);
    ks.setKeyEntry(authenticationCertificateAlias, authenticationKey, password, authenticationCertificate);
    return ks;
  } else {
    return null;
  }
}

代码示例来源:origin: eu.eu-emi.security/canl

protected void createSingleKeyView(KeyStore original, String alias, char[] password)
{
  try
  {
    ks = KeyStoreHelper.getInstanceForCredential("JKS");
    ks.load(null);
    Key key = original.getKey(alias, password);
    Certificate []chain = original.getCertificateChain(alias);
    ks.setKeyEntry(ALIAS, key, KEY_PASSWD, chain);
  } catch (Exception e)
  {
    throw new RuntimeException("Got error when loading data from the " +
        "correct original keystore - this is most probably a bug", e);
  }
}

代码示例来源:origin: wildfly/wildfly

/**
 * 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: neo4j/neo4j

public KeyStore getKeyStore( char[] keyStorePass, char[] privateKeyPass )
{
  KeyStore keyStore;
  try
  {
    keyStore = KeyStore.getInstance( KeyStore.getDefaultType() );
    log.debug( "Keystore loaded is of type " + keyStore.getClass().getName() );
    keyStore.load( null, keyStorePass );
    keyStore.setKeyEntry( "key", privateKey, privateKeyPass, keyCertChain );
  }
  catch ( Exception e )
  {
    throw new RuntimeException( e );
  }
  return keyStore;
}

代码示例来源:origin: apache/zookeeper

public static KeyStore loadKeyStore(File certificateChainFile, File privateKeyFile, Optional<String> keyPassword)
    throws IOException, GeneralSecurityException
{
  PrivateKey key = loadPrivateKey(privateKeyFile, keyPassword);
  List<X509Certificate> certificateChain = readCertificateChain(certificateChainFile);
  if (certificateChain.isEmpty()) {
    throw new CertificateException("Certificate file does not contain any certificates: " + certificateChainFile);
  }
  KeyStore keyStore = KeyStore.getInstance("JKS");
  keyStore.load(null, null);
  keyStore.setKeyEntry("key", key, keyPassword.orElse("").toCharArray(), certificateChain.toArray(new Certificate[0]));
  return keyStore;
}

代码示例来源:origin: Graylog2/graylog2-server

public static KeyManager[] initKeyStore(File tlsKeyFile, File tlsCertFile, String tlsKeyPassword)
    throws IOException, GeneralSecurityException {
  final KeyStore ks = KeyStore.getInstance("JKS");
  ks.load(null, null);
  final Collection<? extends Certificate> certChain = loadCertificates(tlsCertFile.toPath());
  final PrivateKey privateKey = loadPrivateKey(tlsKeyFile, tlsKeyPassword);
  final char[] password = Strings.nullToEmpty(tlsKeyPassword).toCharArray();
  ks.setKeyEntry("key", privateKey, password, certChain.toArray(new Certificate[certChain.size()]));
  if (LOG.isDebugEnabled()) {
    LOG.debug("Private key file: {}", tlsKeyFile);
    LOG.debug("Certificate file: {}", tlsCertFile);
    LOG.debug("Aliases: {}", join(ks.aliases()));
  }
  final KeyManagerFactory kmf = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
  kmf.init(ks, password);
  return kmf.getKeyManagers();
}

代码示例来源:origin: wildfly/wildfly

@Override
public void engineSetKeyEntry(String alias, Key key, char[] password, Certificate[] chain) throws KeyStoreException {
  try {
    // pack key into keystore and protect it using password
    ByteArrayOutputStream os = new ByteArrayOutputStream();
    KeyStore keystore = KeyStore.getInstance(keyType);
    keystore.load(null, password);
    keystore.setKeyEntry(alias, key, password, chain);
    keystore.store(os, password);
    byte[] keystoreBytes = os.toByteArray();
    engineSetKeyEntry(alias, keystoreBytes, chain);
  } catch (CertificateException | NoSuchAlgorithmException | IOException e) {
    throw log.ldapKeyStoreFailedToSerializeKey(alias, e);
  }
}

代码示例来源:origin: docker-java/docker-java

@SuppressFBWarnings(value = "NP_NULL_ON_SOME_PATH_FROM_RETURN_VALUE")
public static KeyStore createKeyStore(final String keypem, final String certpem) throws NoSuchAlgorithmException,
    InvalidKeySpecException, IOException, CertificateException, KeyStoreException {
  PrivateKey privateKey = loadPrivateKey(keypem);
  requireNonNull(privateKey);
  List<Certificate> privateCertificates = loadCertificates(certpem);
  KeyStore keyStore = KeyStore.getInstance("JKS");
  keyStore.load(null);
  keyStore.setKeyEntry("docker",
      privateKey,
      "docker".toCharArray(),
      privateCertificates.toArray(new Certificate[privateCertificates.size()])
  );
  return keyStore;
}

代码示例来源:origin: apache/zookeeper

private void writeKeystore(X509Certificate certificate, KeyPair entityKeyPair, String path) throws Exception {
  KeyStore keyStore = KeyStore.getInstance(KeyStore.getDefaultType());
  keyStore.load(null, PASSWORD);
  keyStore.setKeyEntry("alias", entityKeyPair.getPrivate(), PASSWORD, new Certificate[] { certificate });
  FileOutputStream outputStream = new FileOutputStream(path);
  keyStore.store(outputStream, PASSWORD);
  outputStream.flush();
  outputStream.close();
}

代码示例来源:origin: oracle/helidon

private static KeyManagerFactory buildKmf(KeyConfig privateKeyConfig) throws IOException, GeneralSecurityException {
  String algorithm = Security.getProperty("ssl.KeyManagerFactory.algorithm");
  if (algorithm == null) {
    algorithm = "SunX509";
  }
  byte[] passwordBytes = new byte[64];
  RANDOM.nextBytes(passwordBytes);
  char[] password = Base64.getEncoder().encodeToString(passwordBytes).toCharArray();
  KeyStore ks = KeyStore.getInstance("JKS");
  ks.load(null, null);
  ks.setKeyEntry("key",
          privateKeyConfig.privateKey().orElseThrow(() -> new RuntimeException("Private key not available")),
          password,
          privateKeyConfig.certChain().toArray(new Certificate[0]));
  KeyManagerFactory kmf = KeyManagerFactory.getInstance(algorithm);
  kmf.init(ks, password);
  return kmf;
}

代码示例来源:origin: jenkinsci/jenkins

keyStore = KeyStore.getInstance("JKS");
char[] password = "password".toCharArray();
try {
  keyStore.load(null, password);
} catch (IOException e) {
  throw new IllegalStateException("Specification says this should not happen as we are not doing I/O", e);
  throw new IllegalStateException("Specification says this should not happen as we are not loading keys", e);
keyStore.setKeyEntry("jenkins", privateKey, password,
    new X509Certificate[]{identityCertificate});

代码示例来源:origin: prestodb/presto

public static KeyStore loadKeyStore(File certificateChainFile, File privateKeyFile, Optional<String> keyPassword)
    throws IOException, GeneralSecurityException
{
  PrivateKey key = loadPrivateKey(privateKeyFile, keyPassword);
  List<X509Certificate> certificateChain = readCertificateChain(certificateChainFile);
  if (certificateChain.isEmpty()) {
    throw new CertificateException("Certificate file does not contain any certificates: " + certificateChainFile);
  }
  KeyStore keyStore = KeyStore.getInstance("JKS");
  keyStore.load(null, null);
  // ensure there is a certificate that matches the private key
  Certificate[] certificates = certificateChain.toArray(new Certificate[0]);
  boolean foundMatchingCertificate = false;
  for (int i = 0; i < certificates.length; i++) {
    Certificate certificate = certificates[i];
    if (matches(key, certificate)) {
      foundMatchingCertificate = true;
      // certificate for private key must be in index zero
      certificates[i] = certificates[0];
      certificates[0] = certificate;
      break;
    }
  }
  if (!foundMatchingCertificate) {
    throw new KeyStoreException("Private key does not match the public key of any certificate");
  }
  keyStore.setKeyEntry("key", key, new char[0], certificates);
  return keyStore;
}

代码示例来源:origin: Graylog2/graylog2-server

final KeyStore ks = KeyStore.getInstance(KeyStore.getDefaultType());
ks.load(null, password);
ks.setKeyEntry("key", key, password, certChain.toArray(new Certificate[certChain.size()]));

代码示例来源:origin: wildfly/wildfly

KeyStore store=KeyStore.getInstance(storeType);
store.load(null, null);
store.setKeyEntry(alias, key, storePass.toCharArray(), null);
store.store(stream, storePass.toCharArray());

代码示例来源:origin: fabric8io/docker-maven-plugin

/**
 * Create a key stored holding certificates and secret keys from the given Docker key cert
 *
 * @param certPath directory holding the keys (key.pem) and certs (ca.pem, cert.pem)
 * @return a keystore where the private key is secured with "docker"
 *
 * @throws IOException is reading of the the PEMs failed
 * @throws GeneralSecurityException when the files in a wrong format
 */
public static KeyStore createDockerKeyStore(String certPath) throws IOException, GeneralSecurityException {
  PrivateKey privKey = loadPrivateKey(certPath + "/key.pem");
  Certificate[] certs = loadCertificates(certPath + "/cert.pem");
  KeyStore keyStore = KeyStore.getInstance(KeyStore.getDefaultType());
  keyStore.load(null);
  keyStore.setKeyEntry("docker", privKey, "docker".toCharArray(), certs);
  addCA(keyStore, certPath + "/ca.pem");
  return keyStore;
}

代码示例来源:origin: jamesdbloom/mockserver

if (keyStore == null) {
  keyStore = KeyStore.getInstance(KeyStore.getDefaultType());
  keyStore.load(null, keyStorePassword);
keyStore.setKeyEntry(certificationAlias, privateKey, keyStorePassword, chain);

代码示例来源:origin: stackoverflow.com

public static void main(String[] args) throws Exception {
  KeyStore keyStore = KeyStore.getInstance("JKS");
  keyStore.load(null, null);
  keyStore.setKeyEntry(alias, privKey, keyPass, chain);

相关文章