Add a new private key to the keystore.

@param s
The alias for the object.
@param key
The private key.
@param chars
The password.
@param certificates
The key's certificate chain.
@throws KeyStoreException
488861
@Override
	public void engineSetKeyEntry(String s, Key key, char[] chars,
			Certificate[] certificates) throws KeyStoreException {

		if (!(key instanceof PrivateKey)) {
			throw new KeyStoreException("PrivateKey expected");
		}

		if (!(certificates instanceof X509Certificate[])) {
			throw new KeyStoreException(
					"Certificate chain of X509Certificate expected");
		}
		CredentialWrapper wrapper;
		X509Credential credential = new X509Credential((PrivateKey) key,
				(X509Certificate[]) certificates);
		if (credential.isEncryptedKey()) {
			wrapper = createCertKeyCredential(s, credential);
		} else {
			wrapper = createProxyCredential(s, credential);
		}
		storeWrapper(wrapper);
		this.aliasObjectMap.put(wrapper.getAlias(), wrapper);
	}
780682
public static mps_ssl_certkey add(nitro_service client, mps_ssl_certkey resource) throws Exception
	{
		resource.validate("add");
		return ((mps_ssl_certkey[]) resource.perform_operation(client, "add"))[0];
	}
529188
public static PrivateKey loadPrivateKeyFromKeyStore(KeyStore keyStore,String alias,String keyPass){
        try {			
        	return (PrivateKey) keyStore.getKey(alias, keyPass.toCharArray());
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
    }
509906
public void setKeys(java.util.Collection<String> keys) {
        if (keys == null) {
            this.keys = null;
            return;
        }

        this.keys = new java.util.ArrayList<String>(keys);
    }
993712
public void validate(KeyStore keyStore, Certificate cert) throws CertificateException {
        Certificate[] certChain = null;
	if (cert != null && cert instanceof X509Certificate) {
            ((X509Certificate)cert).checkValidity();
            
            String certAlias = null;
            try {
                if (keyStore == null) {
                    throw new InvalidParameterException("Keystore cannot be null");
                }

                certAlias = keyStore.getCertificateAlias((X509Certificate)cert);
                if (certAlias == null) {
                    certAlias = "CHSMPP" + String.format("%016X", aliasCount.incrementAndGet());
                    keyStore.setCertificateEntry(certAlias, cert);
                }
                
                certChain = keyStore.getCertificateChain(certAlias);
                if (certChain == null || certChain.length == 0) {
                    throw new IllegalStateException("Unable to retrieve certificate chain");
                }
            }
            catch (KeyStoreException kse) {
                logger.debug("", kse);
                throw new CertificateException("Unable to validate certificate" +
					       (certAlias == null ? "":" for alias [" +certAlias + "]") + ": " + kse.getMessage(), kse);
            }
            validate(certChain);
        } 
    }
475358
private static void savePrivateKeyPKCS1(OutputStream os, PrivateKey key)
    throws IllegalArgumentException, IOException {

    CertificateUtils.savePrivateKey(os, key, Encoding.PEM, null, new char[0],
      true);

  }
1331093
public static KeySpec newPBEKeySpec(final String privateKey)
	{
		if (privateKey == null)
		{
			return new PBEKeySpec(CompoundAlgorithm.PRIVATE_KEY.toCharArray());
		}
		return new PBEKeySpec(privateKey.toCharArray());
	}
581469
public void setKey(String key) {

        m_key = key;
        if (LOG.isDebugEnabled()) {
            LOG.debug(Messages.get().getBundle().key(Messages.LOG_SET_KEY_1, key));
        }
    }
439964
public KeyStoreCredentialResolverBuilder addKeyPasswords(Map<String, String> keyPasswords) {
        requireNonNull(keyPasswords, "keyPasswords");
        keyPasswords.forEach(this::addKeyPassword);
        return this;
    }
1102754
public static PrivateKey generatePrivateKey(KeyStore keyStore, String alias, char[] password) {
		try {
			return (PrivateKey) keyStore.getKey(alias, password);
		} catch (Exception e) {
			throw new CryptoException(e);
		}
	}
173861
private void serverKeyExchange(DH_ServerKeyExchange mesg)
            throws IOException {
        if (debug != null && Debug.isOn("handshake")) {
            mesg.print(System.out);
        }
        dh = new DHCrypt(mesg.getModulus(), mesg.getBase(),
                                            sslContext.getSecureRandom());
        serverDH = mesg.getServerPublicKey();
    }
459858
public void setCertificates(java.util.Collection<CACertificate> certificates) {
        if (certificates == null) {
            this.certificates = null;
            return;
        }

        this.certificates = new java.util.ArrayList<CACertificate>(certificates);
    }
873745
private static void savePrivateKeyPKCS8(OutputStream os, PrivateKey key)
    throws IllegalArgumentException, IOException {

    CertificateUtils.savePrivateKey(os, key, Encoding.PEM, null, null);

  }
891837
public void installCertificates(final String key, final String cert, final String chain)
	{
		try
		{
			final File keyFile = File.createTempFile("key", ".pem");
			final File certFile = File.createTempFile("cert", ".pem");
			final File chainFile = File.createTempFile("chain", ".pem");

			try
			{
				FileHelper.write(keyFile, key);
				FileHelper.write(certFile, cert);
				FileHelper.write(chainFile, chain);

				final Execed process = Exec.rootUtility(new File(binPath, "cert-update").getAbsolutePath(),
				                                        keyFile.getAbsolutePath(),
				                                        certFile.getAbsolutePath(),
				                                        chainFile.getAbsolutePath());

				process.waitForExit(new Timeout(30, TimeUnit.SECONDS).start(), 0);
			}
			finally
			{
				FileUtils.deleteQuietly(keyFile);
				FileUtils.deleteQuietly(certFile);
				FileUtils.deleteQuietly(chainFile);
			}
		}
		catch (IOException e)
		{
			throw new RuntimeException("Error executing cert-update command", e);
		}
	}
220363
public void checkKeyStore() {
        if (sslContext != null)
            return; //nothing to check if using preconfigured context
        
        if (keyStoreInputStream == null &&
	    sslConfig.getKeyStorePath() == null) {
            throw new IllegalStateException("SSL doesn't have a valid keystore");
        }
        // if the keystore has been configured but there is no
        // truststore configured, use the keystore as the truststore
        if (trustStoreInputStream == null && sslConfig.getTrustStorePath() == null) {
            trustStoreInputStream = keyStoreInputStream;
            sslConfig.setTrustStorePath(sslConfig.getKeyStorePath());
            sslConfig.setTrustStoreType(sslConfig.getKeyStoreType());
            sslConfig.setTrustStoreProvider(sslConfig.getKeyStoreProvider());
            sslConfig.setTrustStorePassword(sslConfig.getKeyStorePassword());
            sslConfig.setTrustManagerFactoryAlgorithm(sslConfig.getKeyManagerFactoryAlgorithm());
        }

        // It's the same stream we cannot read it twice, so read it once in memory
        if (keyStoreInputStream != null && keyStoreInputStream == trustStoreInputStream) {
            try {
                ByteArrayOutputStream baos = new ByteArrayOutputStream();
		streamCopy(keyStoreInputStream, baos, null, false);
                keyStoreInputStream.close();
                keyStoreInputStream = new ByteArrayInputStream(baos.toByteArray());
                trustStoreInputStream = new ByteArrayInputStream(baos.toByteArray());
            } catch (Exception ex) {
                throw new IllegalStateException(ex);
            }
        }
    }
1036367
public boolean setPublicKey (String key)
    {
        return key == null ? false : setPublicKey(SecureUtil.stringToRSAPublicKey(key));
    }
992325
public static void write(final PrivateKey privateKey, final @NonNull OutputStream outputStream)
		throws IOException
	{
		final byte[] privateKeyBytes = privateKey.getEncoded();
		final PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(privateKeyBytes);
		outputStream.write(keySpec.getEncoded());
		outputStream.close();
	}