All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.earcam.utilitarian.net.ssl.KeyManagers Maven / Gradle / Ivy

The newest version!
/*-
 * #%L
 * io.earcam.utilitarian.net
 * %%
 * Copyright (C) 2017 earcam
 * %%
 * SPDX-License-Identifier: (BSD-3-Clause OR EPL-1.0 OR Apache-2.0 OR MIT)
 *
 * You must choose to accept, in full - any individual or combination of
 * the following licenses:
 * 
 * #L%
 */
package io.earcam.utilitarian.net.ssl;

import java.net.Socket;
import java.security.KeyStore;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.security.Principal;
import java.security.PrivateKey;
import java.security.UnrecoverableKeyException;
import java.security.cert.X509Certificate;

import javax.net.ssl.KeyManager;
import javax.net.ssl.KeyManagerFactory;
import javax.net.ssl.X509ExtendedKeyManager;

/**
 * Key manager
 *
 */
public final class KeyManagers {

	static final class DummyX509KeyManager extends X509ExtendedKeyManager {

		private static final String[] NADDA = new String[0];


		private DummyX509KeyManager()
		{}


		@Override
		public String[] getClientAliases(String arg0, Principal[] arg1)
		{
			return NADDA;
		}


		@Override
		public String chooseClientAlias(String[] arg0, Principal[] arg1, Socket arg2)
		{
			return null;
		}


		@Override
		public String[] getServerAliases(String arg0, Principal[] arg1)
		{
			return NADDA;
		}


		@Override
		public String chooseServerAlias(String arg0, Principal[] arg1, Socket arg2)
		{
			return null;
		}


		@Override
		public X509Certificate[] getCertificateChain(String arg0)
		{
			return new X509Certificate[0];
		}


		@Override
		public PrivateKey getPrivateKey(String arg0)
		{
			return null;
		}
	}

	static final X509ExtendedKeyManager NOOP_KEY_MANAGER = new DummyX509KeyManager();


	private KeyManagers()
	{}


	public static KeyManager[] keyManagerSunX509(KeyStore keyStore, char[] storePassword)
			throws UnrecoverableKeyException, KeyStoreException, NoSuchAlgorithmException
	{
		return keyManager(keyStore, storePassword, "SunX509");
	}


	public static KeyManager[] keyManager(KeyStore keyStore, char[] storePassword, String algorithm)
			throws NoSuchAlgorithmException, KeyStoreException, UnrecoverableKeyException
	{
		KeyManagerFactory kmf = KeyManagerFactory.getInstance(algorithm);
		kmf.init(keyStore, storePassword);
		return kmf.getKeyManagers();
	}


	public static KeyManager[] keyManagerDummy()
	{
		return new KeyManager[] { NOOP_KEY_MANAGER };
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy