How to Trust Android SSL PKCS12 Certificate -


here sample code..

        system.setproperty("http.keepalive", "false");         httpsurlconnection                 .setdefaulthostnameverifier(new hostnameverifier() {                     public boolean verify(string hostname,                             sslsession session) {                         // todo auto-generated method stub                         return false;           char[] passwkey = "pass".tochararray();         keystore ts = keystore.getinstance("pkcs12");          inputstream in = getresources().openrawresource(                 r.raw.certificatefile);         ts.load(in, passwkey);         keymanagerfactory tmf = keymanagerfactory                 .getinstance("x.509");         tmf.init(ts, passwkey);          sslcontext context = sslcontext.getinstance("tls");         context.init(tmf.getkeymanagers(),                 new x509trustmanager[] { new myx509trustmanager(in,                         "mobile".tochararray()) }, new securerandom());          httpsurlconnection.setdefaultsslsocketfactory(context                 .getsocketfactory());          url url = new url("https://url");         httpsurlconnection connection = (httpsurlconnection) url                 .openconnection();         connection.setrequestmethod("get");         connection.setrequestproperty("content-type", "params");         connection.setrequestproperty("appname", "params");         connection.setrequestproperty("appid",                 "params");          bufferedreader bf = new bufferedreader(new inputstreamreader(                 connection.getinputstream()));         string inputline;          while ((inputline = bf.readline()) != null) {             txtmain.append("response " + inputline + "\n");             log.d("@: ", inputline);         }         in.close();      } catch (exception e) { // should never happen         e.printstacktrace();     } 

i getting error not trusted server certificate

whereas if try same in core java with: keymanagerfactory tmf = keymanagerfactory .getinstance("x.509"); working there..

ok guys founded android supports bks keystore here complete solution

try{          system.setproperty("http.keepalive", "false");         httpsurlconnection                 .setdefaulthostnameverifier(new hostnameverifier() {                      public boolean verify(string hostname,                             sslsession session) {                         return true;                     }                 });          char[] passwkey = "password".tochararray();         keystore ts = keystore.getinstance("bks");                 inputstream in = getresources().openrawresource(             r.raw.your_certificate_file);                 inputstream = getresources().openrawresource(             r.raw.your_certificate_file);         ts.load(in, passwkey);         keymanagerfactory tmf = keymanagerfactory.getinstance("x509");         tmf.init(ts, passwkey);          sslcontext context = sslcontext.getinstance("tls");         context.init(tmf.getkeymanagers(),                 new x509trustmanager[] { new myx509trustmanager(is,                         "password".tochararray()) }, new securerandom());         httpsurlconnection.setdefaultsslsocketfactory(context                 .getsocketfactory());                  url url = new url(commons.apicall);          httpsurlconnection connection = (httpsurlconnection) url                 .openconnection();         connection.setrequestmethod("get");         connection.setrequestproperty("username", username);         connection.setrequestproperty("password", password);           bufferedreader bin = new bufferedreader(new inputstreamreader(                 connection.getinputstream()));           stringbuffer sb = new stringbuffer();          while ((line = bin.readline()) != null) {             sb.append(line);         }           in.close();                   is.close();       } catch (exception e) { // should never happen         e.printstacktrace();         log.d("err", e.tostring());     } 

and here myx509trustmanager class

public class myx509trustmanager implements x509trustmanager {     x509trustmanager pkixtrustmanager;      public myx509trustmanager(inputstream truststore, char[] password)             throws exception {         // create "default" jsse x509trustmanager.          keystore ks = keystore.getinstance("bks");          ks.load(truststore, password);          trustmanagerfactory tmf = trustmanagerfactory.getinstance("x509");         tmf.init(ks);          trustmanager tms[] = tmf.gettrustmanagers();          /*          * iterate on returned trustmanagers, instance of          * x509trustmanager. if found, use our "default" trust manager.          */         (int = 0; < tms.length; i++) {             if (tms[i] instanceof x509trustmanager) {                 pkixtrustmanager = (x509trustmanager) tms[i];                 return;             }         }          /*          * find other way initialize, or else have fail          * constructor.          */         throw new exception("couldn't initialize");     }      public void checkclienttrusted(x509certificate[] arg0, string arg1)             throws certificateexception {         // todo auto-generated method stub         try {             pkixtrustmanager.checkclienttrusted(arg0, arg1);         } catch (certificateexception excep) {             // special handling here, or rethrow exception.         }      }      public void checkservertrusted(x509certificate[] arg0, string arg1)             throws certificateexception {         // todo auto-generated method stub         try {             pkixtrustmanager.checkservertrusted(arg0, arg1);         } catch (certificateexception excep) {             /*              * possibly pop dialog box asking whether trust cert              * chain.              */         }     }      public x509certificate[] getacceptedissuers() {         // todo auto-generated method stub         return pkixtrustmanager.getacceptedissuers();     } } 

Comments

Popular posts from this blog

c# - How to set Z index when using WPF DrawingContext? -

razor - Is this a bug in WebMatrix PageData? -

visual c++ - Using relative values in array sorting ( asm ) -