Android Custom Socket Deriving from OpenSSLSocketImpl (not SSLSocket) -


sorry such long question, it's involved. reading.

i have custom socket factory , socket class (android 5.0) have developed perform specific tasks need @ level. here's socket factory , socket (for brevity, i'm leaving out many of methods):

public class customsocketfactory extends sslsocketfactory { private final sslsocketfactory delegate;  public customsocketfactory(sslsocketfactory delegate) {     this.delegate = delegate; }  private socket createcustomsocket(socket socket) {     if (socket instanceof sslsocket) {         socket = new customsocket((sslsocket) socket);     }     return socket; }  @override public socket createsocket(socket s, string host, int port, boolean autoclose) throws ioexception {     return createcustomsocket(delegate.createsocket(s, host, port, autoclose)); }  @override public socket createsocket(string host, int port) throws ioexception {     return createcustomsocket(delegate.createsocket(host, port)); }   private class customsocket extends sslsocket {      protected final sslsocket delegate;      private customsocket(sslsocket delegate) {         this.delegate = delegate;     } 

i'm using factory this:

  private void docustomsocketfactorywithhttpurlconnection() {      try {         string uri = "https://alice.sni.velox.ch";          sslcontext context = sslcontext.getinstance("tls");         context.init(null, null, null);         customsocketfactory customsocketfactory = new customsocketfactory(context.getsocketfactory());         httpsurlconnection.setdefaultsslsocketfactory(customsocketfactory);          url url = new url(uri);         httpsurlconnection conn = (httpsurlconnection) url.openconnection();          log.d(tag, "http response code: " + conn.getresponsecode());      } catch (exception e) {         log.d(tag, e.getlocalizedmessage());     }  } 

this works expected except when i'm hitting site uses server name indication alice.sni.velox.ch. in case, site complains (and confirm wireshark) sni tls headers not being sent app.

take out custom socket factory , headers sent.

digging further, found code in okhttp platform.java class (okhttp classes used inside httpsurlconnection).

@override public void enabletlsextensions(sslsocket socket, string urihost) {      super.enabletlsextensions(socket, urihost);      if (!opensslsocketclass.isinstance(socket)) return;      try {        setusesessiontickets.invoke(socket, true);        sethostname.invoke(socket, urihost);      } catch (invocationtargetexception e) { // snip } 

the opensslsocketclass being setup way:

class<?> opensslsocketclass =    class.forname("com.android.org.conscrypt.opensslsocketimpl"); 

so code enables sni , session ticketing if socket extends opensslsocketimpl.

back custom socket, in debugger see class of socket getting passed constructor is: com.android.org.conscrypt.opensslsocketimplwrapper (which extends opensslsocketimpl).

so miss sni , session ticketing functionality because socket extends java.net.ssl.sslsocket (not opensslsocketimpl).

the best solution comes mind have customsocket extend opensslsocketimpl , add needed delegate methods can't see how import opensslsocketimpl. not appear in standard android libraries. android documentation discusses sslsocket , says nothing opensslsocketimpl.

is there way can have customsocket class extend opensslsocketimpl i'm missing?

i realize can use reflection call methods on "delegate" in customsocket class worry reliability of in cases , where/when make calls. also, if continue add new features opensslsocketimpl class in new android releases using similar method miss features well.

thanks reading way through!

this depends on functionality need implement in customsocket, might following:

  • make customsocket extend socket (no ssl)
  • in socket factory, create plain custom socket, wrap ssl socket around delegate.createsocket(s, host, port, autoclose)
  • return result of delegate, of type com.android.org.conscrypt.opensslsocketimplwrapper

Comments

Popular posts from this blog

android - MPAndroidChart - How to add Annotations or images to the chart -

javascript - Add class to another page attribute using URL id - Jquery -

firefox - Where is 'webgl.osmesalib' parameter? -