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.

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

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

    MarcoRome Expert Licensed User

    In B4A work so:

    Code:
    sock.InitializeSSLAcceptAll("sock")
     
  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?
    thanks
     
  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.
  7. Magma

    Magma Well-Known Member Licensed User

    Hi there... i am searching all over the forum... finding jnet library for b4j but can't for b4a.... can i have a download link please with latest version for b4a...?

    by the way need any permission to work... ?
     
  8. OliverA

    OliverA Well-Known Member Licensed User

    It's part of B4A's Network library. As per post#1, you don't need the code in post#1 for B4A, but use B4A's InitializeSSLAcceptAll method of the Socket class (as per post#2).
     
    Magma likes this.
  9. Magma

    Magma Well-Known Member Licensed User

    Thanks!!!
    need any permission to work... to higher versions of Android ?
     
  10. DonManfred

    DonManfred Expert Licensed User

    No. Did you get any error? WHICH one?
     
    Magma likes this.
  11. Magma

    Magma Well-Known Member Licensed User

    @DonManfred ...Didn't get any error - but needed to know that - because i want it to use at Android 9 Pie versions and haven't for now.... Thanks for the answer :)
     
Loading...
  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