我正在尝试理解Android与服务器之间的TLS连接。有人能帮我纠正吗?
TLS连接有两种启动方式。第一种,只有服务器有证书,客户端需要决定是否信任它。第二种,客户端和服务器都有证书。我说得对吗?
如何在Android设备上生成自定义唯一的TLS连接证书,并将其用于连接服务器?我只找到了第一种连接方式的实现。
有人可以帮帮我吗?
private Connector createSslConnector() {
//print the client keystore
printClientKeystore();
Connector connector = new Connector("org.apache.coyote.http11.Http11NioProtocol");
Http11NioProtocol protocol = (Http11NioProtocol) connector.getProtocolHandler();
try {
//read the keystore from the jar
//and write it to a tmp file
Resource keystoreResource = context.getResource("classpath:config/server.keystore");
byte[] keystoreData = readKeystore(keystoreResource.getInputStream());
File tmpKeystoreFile = File.createTempFile("keystore", "");
writeKeystore(tmpKeystoreFile, keystoreData);
//keystore information
final String keystoreFile = tmpKeystoreFile.getAbsolutePath();
final String keystorePass = "yourKeystorePass";
final String keystoreType = "pkcs12";
final String keystoreProvider = "SunJSSE";
final String keystoreAlias = "comics_tomcat";
connector.setScheme("https");
connector.setAttribute("clientAuth", "true");
connector.setPort(HTTPS_PORT);
connector.setSecure(true);
protocol.setSSLEnabled(true);
//keystore
protocol.setKeystoreFile(keystoreFile);
protocol.setKeystorePass(keystorePass);
protocol.setKeystoreType(keystoreType);
protocol.setProperty("keystoreProvider", keystoreProvider);
protocol.setKeyAlias(keystoreAlias);
//truststore
protocol.setTruststoreFile(keystoreFile);
protocol.setTruststorePass(keystorePass);
protocol.setPort(HTTPS_PORT);
return connector;
}
catch (IOException e) {
LOGGER.error(e.getMessage(), e);
throw new IllegalStateException("cant access keystore: [" + "keystore"
+ "] or truststore: [" + "keystore" + "]", e);
}
}
对于服务器端而言,你可以使用“setSecure(true)”方法,为ssl连接器设置安全标志,从而实现需要用户进行私钥/公钥身份验证。
以下代码是客户端的内容,它加载密钥库和信任库(使用相同的密钥库),配置主机名验证以允许连接到特定域,并打开连接。
KeyStore keyStore = KeyStore.getInstance(KeyStore.getDefaultType());
KeyStore trustStore = KeyStore.getInstance(KeyStore.getDefaultType());
//load keystore stream
byte[] keystoreData = readInputStream(getAssets().open("client.keystore"));
//load keystore
ByteArrayInputStream bais = new ByteArrayInputStream(keystoreData);
keyStore.load(bais, KEYSTORE_PASSWORD.toCharArray());
//load truststore
bais = new ByteArrayInputStream(keystoreData);
trustStore.load(bais, KEYSTORE_PASSWORD.toCharArray());
//load trustmanager
TrustManagerFactory tmf = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
tmf.init(trustStore);
//init keymanager
KeyManagerFactory kmf = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
kmf.init(keyStore, KEYSTORE_PASSWORD.toCharArray());
//create ssl context
sslContext = SSLContext.getInstance("TLS");
sslContext.init(kmf.getKeyManagers(), tmf.getTrustManagers(), null);
HostnameVerifier HOSTNAME_VERIFIER = new HostnameVerifier() {
@Override
public boolean verify(String hostname, SSLSession session) {
List<String> allowedHostnames = new ArrayList<String>();
allowedHostnames.add("pinterest.com");
allowedHostnames.add("192.168.1.43");
allowedHostnames.add("10.0.2.2");
return allowedHostnames.indexOf(hostname) != -1;
}
};
//open https connection
URL url = new URL("https://" + SERVER_URL + ":" + SERVER_PORT + "/api/v1/publication/getDescriptor/" + publicationId);
HttpsURLConnection urlConnection = (HttpsURLConnection) url.openConnection();
urlConnection.setSSLSocketFactory(sslContext.getSocketFactory());
urlConnection.setHostnameVerifier(HOSTNAME_VERIFIER);
//read server response
byte[] serverResult = readInputStream(urlConnection.getInputStream());
如果您有任何问题,请告诉我!