B4J Question javax.net.ssl.SSLPeerUnverifiedException: peer not authenticated

bjfhs

Active Member
Licensed User
Longtime User
B4X:
   Private htp As HttpJob
    htp.Initialize ("hp",Me)
    htp.PostString("https://192.168.0.3:1234/iocm/app/sec/v1.1.0/login","appId=f8fc0c83 & secret=18498bde")
    htp.GetRequest.SetContentType("application/x-www-form-urlencoded")
I get error javax.net.ssl.SSLPeerUnverifiedException: peer not authenticated

And the provider give me two files(ca.jks,CertwithKey.pkcs12),but I don't know how to do it.
 

bjfhs

Active Member
Licensed User
Longtime User
B4X:
Sub AppStart (Form1 As Form, Args() As String)
    MainForm = Form1
    MainForm.RootPane.LoadLayout("main") 'Load the layout file.
    MainForm.Show

    nativeMe = Me
    nativeMe.RunMethod("initSSLConfig",Null)
 
     htp.Initialize ("hp",Me)
    htp.PostString("https://192.168.0.3:8743/iocm/app/sec/v1.1.0/login","appId=f8fc0c83 & secret=18498bde")
    htp.GetRequest.SetContentType("application/x-www-form-urlencoded")
 
End Sub
 

#If Java
import java.io.*;
import java.security.KeyStore;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import javax.net.ssl.KeyManagerFactory;
import javax.net.ssl.SSLContext;
import javax.net.ssl.TrustManagerFactory;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.conn.ClientConnectionManager;
import org.apache.http.conn.scheme.Scheme;
import org.apache.http.conn.scheme.SchemeRegistry;
import org.apache.http.conn.ssl.SSLSocketFactory;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
public class HttpsClientDemo extends DefaultHttpClient {

    public  String SELFCERTPATH = "D://work/software/B4j/20180314Test/cert/CertwithKey.pkcs12";

    public  String SELFCERTPWD = "IoM@1234";

    public String TRUSTCAPATH = "D://work/software/B4j/20180314Test/cert/ca.jks";

 
    public  String TRUSTCAPWD = "Huawei@123";

 
    public void initSSLConfigForTwoWay() throws Exception {

     KeyStore selfCert = KeyStore.getInstance("pkcs12");
        selfCert.load(new FileInputStream(SELFCERTPATH), SELFCERTPWD.toCharArray());
        KeyManagerFactory kmf = KeyManagerFactory.getInstance("sunx509");
        kmf.init(selfCert, SELFCERTPWD.toCharArray());

        KeyStore caCert = KeyStore.getInstance("jks");
        caCert.load(new FileInputStream(TRUSTCAPATH), TRUSTCAPWD.toCharArray());
        TrustManagerFactory tmf = TrustManagerFactory.getInstance("sunx509");
        tmf.init(caCert);

        SSLContext sc = SSLContext.getInstance("TLS");
        sc.init(kmf.getKeyManagers(), tmf.getTrustManagers(), null);
 
         SSLSocketFactory ssf = new SSLSocketFactory(sc);
        ssf.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);

        ClientConnectionManager ccm = this.getConnectionManager();
        SchemeRegistry sr = ccm.getSchemeRegistry();
        sr.register(new Scheme("https", ssf, 8743));
     
     
    }

}

 public void initSSLConfig(){
     HttpsClientDemo httpClient2 = new HttpsClientDemo();
        httpClient2.initSSLConfigForTwoWay();

 }
#End If

when compile:
Rc\b4j\example\main.java:275: error: unreported exception error Exception; must be captured or declared in order to throw out.

HttpClient2.initSSLConfigForTwoWay ();

^

1 error
 
Last edited:
Upvote 0

Daestrum

Expert
Licensed User
Longtime User
Two suggestions
B4X:
public class HttpsClientDemo extends DefaultHttpClient {
change to ( you will get non static member warning from 2nd change below)
B4X:
public static class HttpsClientDemo extends DefaultHttpClient {

B4X:
public void initSSLConfig(){
change to ( you will get not a member of calling class without the static )
B4X:
public static void initSSLConfig() throws Exception{
 
Upvote 0

bjfhs

Active Member
Licensed User
Longtime User
Thank you very much,you are great.
It's work Now.
 
Last edited:
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…