B4J Code Snippet [B4X] Trust all SSL Socket

Discussion in 'B4J Code Snippets' started by Erel, Jan 27, 2019.

  1. Erel

    Erel Administrator Staff Member Licensed User

    Platforms: B4J (1st post) and B4A (2nd post)

    This code creates an SSL socket that doesn't verify the server certificate.

    Depends on jNet / Net and JavaObject libraries.

    Private Sub CreateTrustAllSSLSocket (EventName As StringAs Socket
    Dim socket As Socket
    Dim jo As JavaObject = socket
    "socket", CreateTrustAllSSLSocketFactory.RunMethod("createSocket"Null))
    Return socket
    End Sub

    Sub CreateTrustAllSSLSocketFactory As JavaObject
    Dim tm As CustomTrustManager
    Dim SSLContext As JavaObject
       SSLContext = SSLContext.InitializeStatic(
    "init"Array(Null, tm, Null))
    Dim Factory As JavaObject = SSLContext.RunMethod("getSocketFactory"Null)
    Return Factory
    End Sub
    Dim sock As Socket = CreateTrustAllSSLSocket("sock")
    Last edited: Jan 28, 2019
  2. MarcoRome

    MarcoRome Expert Licensed User

    In B4A work so:

  3. juvanum

    juvanum Active Member Licensed User

    help me to understand:
    does that mean I can not install any certificate on the server and make an https connection?
  4. Erel

    Erel Administrator Staff Member Licensed User

    If you install a trusted certificate then you don't need to do anything special. The code above is not required in that case.

    If the certificate is not trusted then you need to use the code above to create a socket that doesn't validate the certificate.
    juvanum likes this.
  5. juvanum

    juvanum Active Member Licensed User

    Thanks but if I have no certificate can I use https to connect to the server using the code above?
  6. Erel

    Erel Administrator Staff Member Licensed User

    You cannot create a SSL connection if the server doesn't have a certificate. Please start a new thread for further discussion.
    juvanum likes this.
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice