本文整理汇总了Java中javax.net.ssl.KeyManagerFactory类的典型用法代码示例。如果您正苦于以下问题:Java KeyManagerFactory类的具体用法?Java KeyManagerFactory怎么用?Java KeyManagerFactory使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
KeyManagerFactory类属于javax.net.ssl包,在下文中一共展示了KeyManagerFactory类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: getSocketFactory
import javax.net.ssl.KeyManagerFactory; //导入依赖的package包/类
private SSLSocketFactory getSocketFactory() {
try {
Security.addProvider(new BouncyCastleProvider());
TrustManagerFactory trustManagerFactory = createAndInitTrustManagerFactory();
KeyManagerFactory keyManagerFactory = createAndInitKeyManagerFactory();
SSLContext context = SSLContext.getInstance(TLS_VERSION);
context.init(keyManagerFactory.getKeyManagers(), trustManagerFactory.getTrustManagers(), null);
return context.getSocketFactory();
} catch (Exception e) {
log.error("[{}:{}:{}:{}] Creating TLS factory failed!", caCert, cert, privateKey, password, e);
throw new RuntimeException("Creating TLS factory failed!", e);
}
}
示例2: newSSLContext
import javax.net.ssl.KeyManagerFactory; //导入依赖的package包/类
public static SSLContext newSSLContext(final KeyStore ks, final String password,
final String ksAlgorithm) throws InvalidSSLConfig {
try {
// Get a KeyManager and initialize it
final KeyManagerFactory kmf = KeyManagerFactory.getInstance(ksAlgorithm);
kmf.init(ks, password.toCharArray());
// Get a TrustManagerFactory with the DEFAULT KEYSTORE, so we have all the certificates in cacerts trusted
final TrustManagerFactory tmf = TrustManagerFactory.getInstance(ksAlgorithm);
tmf.init((KeyStore) null);
// Get the SSLContext to help create SSLSocketFactory
final SSLContext sslContext = SSLContext.getInstance("TLS");
sslContext.init(kmf.getKeyManagers(), tmf.getTrustManagers(), null);
return sslContext;
} catch (final GeneralSecurityException e) {
throw new InvalidSSLConfig(e);
}
}
示例3: upgradeToTls
import javax.net.ssl.KeyManagerFactory; //导入依赖的package包/类
private void upgradeToTls(Socket socket) throws KeyStoreException, IOException, NoSuchAlgorithmException,
CertificateException, UnrecoverableKeyException, KeyManagementException {
KeyStore keyStore = keyStoreProvider.getKeyStore();
String defaultAlgorithm = KeyManagerFactory.getDefaultAlgorithm();
KeyManagerFactory keyManagerFactory = KeyManagerFactory.getInstance(defaultAlgorithm);
keyManagerFactory.init(keyStore, keyStoreProvider.getPassword());
SSLContext sslContext = SSLContext.getInstance("TLS");
sslContext.init(keyManagerFactory.getKeyManagers(), null, null);
SSLSocketFactory sslSocketFactory = sslContext.getSocketFactory();
SSLSocket sslSocket = (SSLSocket) sslSocketFactory.createSocket(
socket, socket.getInetAddress().getHostAddress(), socket.getPort(), true);
sslSocket.setUseClientMode(false);
sslSocket.startHandshake();
input = Okio.buffer(Okio.source(sslSocket.getInputStream()));
output = Okio.buffer(Okio.sink(sslSocket.getOutputStream()));
}
示例4: SslHandlerFactory
import javax.net.ssl.KeyManagerFactory; //导入依赖的package包/类
public SslHandlerFactory(AmqpServerConfiguration configuration) throws KeyStoreException, IOException,
CertificateException, NoSuchAlgorithmException, UnrecoverableKeyException, KeyManagementException {
KeyStore keyStore = getKeyStore(configuration.getSsl().getKeyStore().getType(),
configuration.getSsl().getKeyStore().getLocation(),
configuration.getSsl().getKeyStore().getPassword());
KeyManagerFactory keyManagerFactory = KeyManagerFactory.getInstance(configuration.getSsl()
.getKeyStore()
.getCertType());
keyManagerFactory.init(keyStore, configuration.getSsl().getKeyStore().getPassword().toCharArray());
KeyStore trustStore = getKeyStore(configuration.getSsl().getTrustStore().getType(),
configuration.getSsl().getTrustStore().getLocation(),
configuration.getSsl().getTrustStore().getPassword());
TrustManagerFactory trustManagerFactory = TrustManagerFactory.getInstance(configuration.getSsl()
.getTrustStore()
.getCertType());
trustManagerFactory.init(trustStore);
sslContext = SSLContext.getInstance(configuration.getSsl().getProtocol());
sslContext.init(keyManagerFactory.getKeyManagers(), trustManagerFactory.getTrustManagers(), null);
}
示例5: createKeyManagerFactory
import javax.net.ssl.KeyManagerFactory; //导入依赖的package包/类
private static KeyManagerFactory createKeyManagerFactory(
final String clientCertificateFileName, final String clientKeyFileName, final String clientKeyPassword)
throws InvalidKeySpecException, NoSuchAlgorithmException, KeyStoreException, IOException, CertificateException, UnrecoverableKeyException
{
// Creates a key manager factory
// Load and create the client certificate
final X509Certificate clientCertificate = createX509CertificateFromFile(clientCertificateFileName);
// Load the private client key
final PrivateKey privateKey = createPrivateKeyFromPemFile(clientKeyFileName);
// Client key and certificate are sent to server
final KeyStore keyStore = KeyStore.getInstance(KeyStore.getDefaultType());
keyStore.load(null, null);
keyStore.setCertificateEntry("certificate", clientCertificate);
keyStore.setKeyEntry("private-key", privateKey,
clientKeyPassword.toCharArray(),
new Certificate[] { clientCertificate });
final KeyManagerFactory keyManagerFactory = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
keyManagerFactory.init(keyStore, clientKeyPassword.toCharArray());
return keyManagerFactory;
}
开发者ID:PacktPublishing,项目名称:MQTT-Essentials-A-Lightweight-IoT-Protocol,代码行数:22,代码来源:SecurityHelper.java
示例6: getSSLSocketFactory
import javax.net.ssl.KeyManagerFactory; //导入依赖的package包/类
/**
* 获得SSLSocektFactory
*
* @param password 密码
* @param keyStorePath 密钥库路径
* @param trustStorePath 信任库路径
* @return SSLSocketFactory
* @throws Exception
*/
private static SSLSocketFactory getSSLSocketFactory(String password, String keyStorePath, String trustStorePath)
throws Exception {
// 实例化密钥库
KeyManagerFactory keyManagerFactory = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
// 获得密钥库
KeyStore keyStore = getKeyStore(keyStorePath, password);
// 初始化密钥工厂
keyManagerFactory.init(keyStore, password.toCharArray());
// 实例化信任库
TrustManagerFactory trustManagerFactory = TrustManagerFactory
.getInstance(TrustManagerFactory.getDefaultAlgorithm());
// 获得信任库
KeyStore trustStore = getKeyStore(trustStorePath, password);
// 初始化信任库
trustManagerFactory.init(trustStore);
// 实例化SSL上下文
SSLContext ctx = SSLContext.getInstance(PROTOCOL);
// 初始化SSL上下文
ctx.init(keyManagerFactory.getKeyManagers(), trustManagerFactory.getTrustManagers(), new SecureRandom());
// 获得SSLSocketFactory
return ctx.getSocketFactory();
}
示例7: getSocketFactory
import javax.net.ssl.KeyManagerFactory; //导入依赖的package包/类
private SSLSocketFactory getSocketFactory() {
try {
Security.addProvider(new BouncyCastleProvider());
TrustManagerFactory trustManagerFactory = createAndInitTrustManagerFactory();
KeyManagerFactory keyManagerFactory = createAndInitKeyManagerFactory();
SSLContext context = SSLContext.getInstance(TLS_VERSION);
context.init(keyManagerFactory.getKeyManagers(), trustManagerFactory.getTrustManagers(), null);
return context.getSocketFactory();
} catch (Exception e) {
log.error("[{}:{}:{}:{}] Creating TLS factory failed!", caCert, cert, privateKey, password, e);
throw new RuntimeException("Creating TLS factory failed!", e);
}
}
示例8: createKeyManagers
import javax.net.ssl.KeyManagerFactory; //导入依赖的package包/类
public static KeyManager[] createKeyManagers(final KeyStore keystore,
char[] keyvalue) {
try {
KeyManagerFactory kmfactory =
KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
kmfactory.init(keystore, keyvalue);
return kmfactory.getKeyManagers();
} catch (Exception e) {
throw new IllegalArgumentException("Bad key store."
+ e.getMessage());
}
}
示例9: makeSSLSocketFactory
import javax.net.ssl.KeyManagerFactory; //导入依赖的package包/类
/**
* Creates an SSLSocketFactory for HTTPS. Pass a KeyStore resource with your
* certificate and passphrase
*/
public static SSLServerSocketFactory makeSSLSocketFactory(String keyAndTrustStoreClasspathPath, char[] passphrase) throws IOException {
try {
KeyStore keystore = KeyStore.getInstance(KeyStore.getDefaultType());
InputStream keystoreStream = NanoHTTPD.class.getResourceAsStream(keyAndTrustStoreClasspathPath);
if (keystoreStream == null) {
throw new IOException("Unable to load keystore from classpath: " + keyAndTrustStoreClasspathPath);
}
keystore.load(keystoreStream, passphrase);
KeyManagerFactory keyManagerFactory = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
keyManagerFactory.init(keystore, passphrase);
return makeSSLSocketFactory(keystore, keyManagerFactory);
} catch (Exception e) {
throw new IOException(e.getMessage());
}
}
示例10: addToStore
import javax.net.ssl.KeyManagerFactory; //导入依赖的package包/类
private void addToStore(String alias, KeyPair kp, Certificate cert) throws KeyStoreException,
NoSuchAlgorithmException, CertificateException, IOException, UnrecoverableKeyException {
Certificate[] chain = {
cert,
};
keyStore.setKeyEntry(alias, kp.getPrivate(),
"".toCharArray(), chain);
keyStore.store(new FileOutputStream(keyStoreFile), "".toCharArray());
/*
* After adding an entry to the keystore we need to create a fresh
* KeyManager by reinitializing the KeyManagerFactory with the new key
* store content and then rewrapping the default KeyManager with our own
*/
KeyManagerFactory keyManagerFactory = KeyManagerFactory
.getInstance(KeyManagerFactory.getDefaultAlgorithm());
keyManagerFactory.init(keyStore, "".toCharArray());
KeyManager defaultKeyManager = keyManagerFactory.getKeyManagers()[0];
KeyManager wrappedKeyManager = new KerplappKeyManager((X509KeyManager) defaultKeyManager);
keyManagers = new KeyManager[] {
wrappedKeyManager,
};
}
示例11: createSslContext
import javax.net.ssl.KeyManagerFactory; //导入依赖的package包/类
private static SSLContext createSslContext(ApacheThriftClientConfig config)
{
try {
KeyStore trustStore = loadTrustStore(config.getTrustCertificate());
TrustManagerFactory trustManagerFactory = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
trustManagerFactory.init(trustStore);
KeyManager[] keyManagers = null;
if (config.getKey() != null) {
Optional<String> keyPassword = Optional.ofNullable(config.getKeyPassword());
KeyStore keyStore = loadKeyStore(config.getTrustCertificate(), config.getKey(), keyPassword);
KeyManagerFactory keyManagerFactory = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
keyManagerFactory.init(keyStore, new char[0]);
keyManagers = keyManagerFactory.getKeyManagers();
}
SSLContext sslContext = SSLContext.getInstance("TLS");
sslContext.init(keyManagers, trustManagerFactory.getTrustManagers(), null);
return sslContext;
}
catch (IOException | GeneralSecurityException e) {
throw new IllegalArgumentException("Unable to load SSL keys", e);
}
}
示例12: prepareKeyManager
import javax.net.ssl.KeyManagerFactory; //导入依赖的package包/类
private static KeyManager[] prepareKeyManager(InputStream bksFile, String password) {
if (bksFile == null || password == null) {
return null;
}
KeyStore clientKeyStore;
try {
clientKeyStore = KeyStore.getInstance("BKS");
clientKeyStore.load(bksFile, password.toCharArray());
KeyManagerFactory keyManagerFactory = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
keyManagerFactory.init(clientKeyStore, password.toCharArray());
return keyManagerFactory.getKeyManagers();
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
示例13: makeSSLSocketFactory
import javax.net.ssl.KeyManagerFactory; //导入依赖的package包/类
/**
* Creates an SSLSocketFactory for HTTPS. Pass a KeyStore resource with your
* certificate and passphrase
*/
public static SSLServerSocketFactory makeSSLSocketFactory(String keyAndTrustStoreClasspathPath, char[] passphrase)
{
try {
KeyStore keystore = KeyStore.getInstance(KeyStore.getDefaultType());
File keystrorefile = new File(keyAndTrustStoreClasspathPath);
System.out.println(keystrorefile.getAbsolutePath());
InputStream keystoreStream = new FileInputStream(keystrorefile);//NanoHTTPD.class.getResourceAsStream(keyAndTrustStoreClasspathPath);
// if (keystoreStream == null)
// {
// System.out.println("Unable to load keystore from classpath: " + keyAndTrustStoreClasspathPath);
// //throw new IOException("Unable to load keystore from classpath: " + keyAndTrustStoreClasspathPath);
// return null;
// }
keystore.load(keystoreStream, passphrase);
KeyManagerFactory keyManagerFactory = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
keyManagerFactory.init(keystore, passphrase);
return makeSSLSocketFactory(keystore, keyManagerFactory);
} catch (Exception e) {
System.out.println(e.toString());
//throw new IOException(e.getMessage());
}
return null;
}
示例14: withSSL
import javax.net.ssl.KeyManagerFactory; //导入依赖的package包/类
private DseCluster.Builder withSSL(DseCluster.Builder builder) throws KeyStoreException, NoSuchAlgorithmException, IOException, CertificateException, KeyManagementException, UnrecoverableKeyException {
// JKS Truststore
KeyStore truststore = KeyStore.getInstance("JKS");
truststore.load(new FileInputStream(sslTruststoreFile), sslTruststorePassword.toCharArray());
TrustManagerFactory tmf = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
tmf.init(truststore);
SSLContext sslContext = SSLContext.getInstance("TLSv1.2");
// Keystore details means supporting client authentication
if (null != sslKeystoreFile && sslKeystoreFile.length() > 0 && null != sslKeystorePassword && sslKeystorePassword.length() > 0) {
KeyStore keystore = KeyStore.getInstance("JKS");
keystore.load(new FileInputStream(sslKeystoreFile), sslKeystorePassword.toCharArray());
KeyManagerFactory kmf = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
kmf.init(keystore, sslKeystorePassword.toCharArray());
sslContext.init(kmf.getKeyManagers(), tmf.getTrustManagers(), new java.security.SecureRandom());
} else {
sslContext.init(null, tmf.getTrustManagers(), new java.security.SecureRandom());
}
return builder.withSSL(RemoteEndpointAwareJdkSSLOptions.builder().withSSLContext(sslContext).build());
}
示例15: keyManager
import javax.net.ssl.KeyManagerFactory; //导入依赖的package包/类
private static KeyManagerFactory keyManager(NetworkSslConfig cfg, ResourceService res) throws GeneralSecurityException, IOException,
ResourceLoadingException {
KeyManagerFactory factory;
if (cfg.getKeyStoreAlgorithm() == null || cfg.getKeyStoreAlgorithm().isEmpty()) {
factory = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
} else {
factory = KeyManagerFactory.getInstance(cfg.getKeyStoreAlgorithm());
}
KeyStore store = keyStore(cfg.getKeyStorePath(), cfg.getKeyStorePassword(), cfg.getKeyStoreType(), res);
factory.init(store, cfg.getKeyStorePassword().toCharArray());
return factory;
}