Certificate expiration issue on Android 5.X devices
Updated at:
Symptoms
The following log is displayed on the client.
com.android.org.bouncycastle.jce.exception.ExtCertPathValidatorException:
Could not validate certificate: Certificate expired at Sat No
v 06 20:00:00 GMT+08:00 2021 (compared to Wed Jan 12 10:12:30 GMT+08:00 2022)\"",Cause
This issue is caused by the Android 5.x operating system. To resolve this issue, you must trust all certificates.
Solution
Set TinyAppRequestPluginProvider on the client.
// H5Utils.setProvider(TinyAppRequestPluginProvider.class.getName() ,
new TinyAppRequestPluginProviderImp() );
public class TinyAppRequestPluginProviderImpl implements TinyAppRequestPluginProvider {
private static final String TAG = "TinyAppRequestPluginProviderImpl";
SSLSocketFactoryImp sf;
public void onAndroidHttpClientCreate(AndroidHttpClient androidHttpClient) {
setCA(androidHttpClient);
}
private void setCA(AndroidHttpClient client) {
// Set trusted certificates for devices running Android 5.x or earlier.
if (Build.VERSION.SDK_INT < 23) {
KeyStore trustStore;
try {
trustStore = KeyStore.getInstance(KeyStore.getDefaultType());
trustStore.load(null ,null);
// Create an SSLSocketFactory and the related sockets.
sf = new SSLSocketFactoryImp(trustStore);
sf.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
client.getConnectionManager().getSchemeRegistry()
.register(new Scheme("https" ,sf ,443));
}catch (Exception e){
}
}
}
}
/* Baseline version 21 --*/
public class SSLSocketFactoryImp extends SSLSocketFactory {
final SSLContext sslContext = SSLContext.getInstance("TLS");
public SSLSocketFactoryImp(KeyStore truststore)
throws NoSuchAlgorithmException, KeyManagementException,
KeyStoreException, UnrecoverableKeyException {
super(truststore);
TrustManager tm = new X509TrustManager() {
public java.security.cert.X509Certificate[] getAcceptedIssuers() {
return null;
}
@Override
public void checkClientTrusted(
java.security.cert.X509Certificate[] chain,
String authType)
throws java.security.cert.CertificateException {
}
@Override
public void checkServerTrusted(
java.security.cert.X509Certificate[] chain,
String authType)
throws java.security.cert.CertificateException {
}
};
sslContext.init(null, new TrustManager[] { tm }, null);
}
@Override
public Socket createSocket(Socket socket, String host, int port,
boolean autoClose) throws IOException, UnknownHostException {
return sslContext.getSocketFactory().createSocket(socket, host,
port, autoClose);
}
@Override
public Socket createSocket() throws IOException {
return sslContext.getSocketFactory().createSocket();
}
}Is this page helpful?