Load client certificate by host name

This commit is contained in:
M66B 2020-02-10 09:16:19 +01:00
parent 867add25e5
commit 531aad4504
1 changed files with 14 additions and 1 deletions

View File

@ -35,8 +35,10 @@ import java.net.Socket;
import java.net.UnknownHostException;
import java.security.GeneralSecurityException;
import java.security.KeyStore;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.security.Principal;
import java.security.cert.Certificate;
import java.security.cert.CertificateEncodingException;
import java.security.cert.CertificateException;
import java.security.cert.CertificateParsingException;
@ -646,12 +648,23 @@ public class EmailService implements AutoCloseable {
KeyManager[] km = null;
if (use_certificate)
try {
KeyStore ks = KeyStore.getInstance("AndroidCAStore");
Log.i("Client certificate init");
KeyStore ca = KeyStore.getInstance("AndroidCAStore");
ca.load(null, null);
Certificate cert = ca.getCertificate(server);
if (cert == null)
throw new KeyStoreException("Certificate not found host=" + server);
KeyStore ks = KeyStore.getInstance(KeyStore.getDefaultType());
ks.load(null, null);
ks.setCertificateEntry(server, cert);
KeyManagerFactory kmf = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
kmf.init(ks, null);
km = kmf.getKeyManagers();
Log.i("Client certificate initialized");
} catch (Throwable ex) {
Log.e(ex);
}