Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to trust self signed certificate on Android?

I have generated self signed certificate for my server. Then added it to Android with Settings -> Security -> Install.

When I'm trying to connect to my server from the application I'm getting error:

java.security.cert.CertPathValidatorException: Trust anchor for certification path not found.

As I understand after I've added certificate to list of trusted ones it should work fine. Am I missing something? The idea is to add certificate through Android system without modifying application code.

Btw I'm using OkHttpClient for network connection. Maybe I should enable something for https connection?

like image 759
Orest Avatar asked May 17 '16 16:05

Orest


1 Answers

Consider using src/debug/xml/network_security_config.xml.

It should look similar to:

<?xml version="1.0" encoding="utf-8"?>
<network-security-config>
    <debug-overrides>
        <trust-anchors>
            <certificates src="@raw/debug_cas"/>
        </trust-anchors>
    </debug-overrides>
</network-security-config>

Where debug_cas is the custom certification authority you used to generate the certificate for your server. Beware that if you are using a local server accessing it by IP you must have a subjectAltName with that IP inside your server certificate, otherwise it will give you a javax.net.ssl.SSLPeerUnverifiedException

like image 87
VariabileAleatoria Avatar answered Sep 18 '22 13:09

VariabileAleatoria