Estoy construyendo una aplicación que hace el consumo de un webservice a través de un SSLContext con protocolo TLSv1.2 y TLS Mutual. Como debo de realizar un intercambio de certificados, en ambiente de desarrollo, he creado un certificado autofirmado (SO Windows 8) como se menciona en https://www.youtube.com/watch?v=GTVVYBcB09Q, que practicamente es crear un certificado, exportarlo e importarlo en los almacenes de confianza para que el certificado quede autofirmado. El envio del mensaje lo realizo como lo indican en https://blog.behrang.org/2019/01/30/ssl-mutual-authentication-java.html, sin embargo estoy recibiendo un mensaje de error desde el server host
Received fatal alert: handshake_failure
El error que se presenta en el server host es
error:140890C7:SSL routines:ssl3_get_client_certificate:peer did not return a certificate
Request processing failed: Connection terminated before request headers read because of the connection error occurs, from URL:
SSL peer did not send a certificate during the handshake
Mi certificado fue exportado como un X509 de 64 bit, ese es el mismo que se instalo en el server host y es el mismo que yo importe en un keystore tipo JKS el cual leo y cargo en el SSLContext.
El siguiente es el código fuente del mi aplicación demo
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.security.KeyStore;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.security.cert.CertificateException;
import javax.net.ssl.SSLContext;
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
import org.apache.http.conn.ssl.SSLContexts;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
public class SSLDemo {
public static final String URL = "https://host-destino/context/recurso";
public static final String PATH_KEYSTORE = "F:\\certificados\\keyStore.jks";
public static final String KEY_PASS = "passwordkeystore";
public static final String PATH_TRUST_STORE = "F:\\certificados\\trustStore.jks";
public static final String TRUST_PASS = "passwordtruststore";
public static void main(String[] args) {
try {
KeyStore keyStore = getKeyStore(PATH_KEYSTORE, KEY_PASS);
KeyStore trustStore = getKeyStore(PATH_TRUST_STORE, TRUST_PASS);
SSLContext sslcontext = SSLContexts.custom()
.loadTrustMaterial(trustStore)
.loadKeyMaterial(keyStore, KEY_PASS.toCharArray())
.build();
String[] protocolos = { "TLSv1.2" };
SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sslcontext, protocolos, null,
SSLConnectionSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
CloseableHttpClient httpclient = HttpClients.custom().setSSLSocketFactory(sslsf).build();
// Construye la peticin
HttpGet httpGet = new HttpGet(URL);
System.out.println("Enviando petición: " + httpGet.getRequestLine());
CloseableHttpResponse response = httpclient.execute(httpGet);
try {
HttpEntity responseEntity = response.getEntity();
System.out.println(response.getStatusLine());
String result = null;
if (responseEntity != null) {
result = EntityUtils.toString(responseEntity);
}
EntityUtils.consume(responseEntity);
System.out.println( result);
httpclient.close();
} finally {
response.close();
httpclient.close();
}
} catch (Exception e) {
System.out.println(e.getMessage());
e.printStackTrace();
}
}
public static KeyStore getKeyStore(String path, String pass) throws KeyStoreException, FileNotFoundException,
IOException, NoSuchAlgorithmException, CertificateException {
char[] claveKeyStoreArr = pass.toCharArray();
KeyStore keyStore = KeyStore.getInstance("JKS");
FileInputStream instreamKey = new FileInputStream(new File(path));
try {
keyStore.load(instreamKey, claveKeyStoreArr);
return keyStore;
} finally {
instreamKey.close();
}
}
}
getkeyStore()
, para el almacenkeyStore
(que contiene mi certificado) no esta cargando el certificado que se encuentran dentro del almacen – isaac el 7 jun. 19 a las 18:54