2013-05-27 16 views
8

Devo connettermi a un webservice basato su REST.Bouncy Castle Keystore (BKS): java.io.IOException: versione errata del keystore

(https://someurl.com/api/lookup/jobfunction/lang/EN)

In IE o browser Chrome quando provo ad accedere a questo URL, ottengo un certificato che devo fidarmi e di accettare di continuare Dopo che devo inserire username e password e poi ho ottenere la risposta JSON.

La stessa cosa che devo fare programmaticamente per un'app per Android.

  1. Provato con EasySSLSocketFactory personalizzato e EasyX509TrustManager, non ha funzionato. Ho ricevuto il seguente errore: java.security.cert.CertPathValidatorException: Ancoraggio attendibile per percorso di certificazione non trovato.

  2. Usato keystore BKS, prega di notare che mykeystore.bks è un file vuoto prima ho eseguito il seguito comandi

    keytool -importcert -v -trustcacerts -file "test.crt" -alias IntermediateCA -keystore "mykeystore.bks" -provider org.bouncycastle.jce.provider.BouncyCastleProvider -providerpath "bcprov-jdk15on-148.jar" -storetype BKS -storepass abcd1234 
    
    
    keytool -list -keystore "mykeystore.bks" -provider org.bouncycastle.jce.provider.BouncyCastleProvider -providerpath "bcprov-jdk15on-148.jar" -storetype BKS -storepass abcd1234 
    

MyHTTPClient.java si presenta come di seguito:

public class MyHttpClient extends DefaultHttpClient { 

final Context context; 

public MyHttpClient(Context context) { 
    this.context = context; 
} 

@Override 
protected ClientConnectionManager createClientConnectionManager() { 
    SchemeRegistry registry = new SchemeRegistry(); 
    registry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80)); 
    // Register for port 443 our SSLSocketFactory with our keystore 
    // to the ConnectionManager 
    registry.register(new Scheme("https", newSslSocketFactory(), 443)); 
    return new SingleClientConnManager(getParams(), registry); 
} 

private SSLSocketFactory newSslSocketFactory() { 
    try { 
     // Get an instance of the Bouncy Castle KeyStore format 
     KeyStore trusted = KeyStore.getInstance("BKS"); 
     // Get the raw resource, which contains the keystore with 
     // your trusted certificates (root and any intermediate certs) 
     InputStream in = context.getResources().openRawResource(R.raw.mykeystore); 
     try { 
      // Initialize the keystore with the provided trusted certificates 
      // Also provide the password of the keystore 
      trusted.load(in, "abcd1234".toCharArray()); 
     } finally { 
      in.close(); 
     } 
     // Pass the keystore to the SSLSocketFactory. The factory is responsible 
     // for the verification of the server certificate. 
     SSLSocketFactory sf = new SSLSocketFactory(trusted); 
     // Hostname verification from certificate 
     // http://hc.apache.org/httpcomponents-client-ga/tutorial/html/connmgmt.html#d4e506 
     sf.setHostnameVerifier(SSLSocketFactory.STRICT_HOSTNAME_VERIFIER); 
     return sf; 
    } catch (Exception e) { 
     throw new AssertionError(e); 
    } 
} 

Quando chiamo il webservice, ricevo l'errore seguente: causato da: java.lang.AssertionError: java.io.IOException: versione errata di ke y store

Per favore dimmi cosa devo fare per connettermi al webservice di riposo basato su HTTPS che ha credenziali di username e passwd. ......

+1

Ciao ragazzi, utilizzare sempre bcprov-jdk15on-146.jar per creare il file di archivio chiavi. Ho risolto il problema utilizzando bcprov-jdk15on-146.jar anziché l'ultimo bcprov-jdk15on-148.jar – user2290834

+0

non c'è bcprov-jdk15on-148.jar nella mia directory di installazione Java –

+0

La domanda è stata già risolta qui: http://stackoverflow.com/a/33197845/5459467 – Cukic0d

risposta

1

La versione 148 del vaso BC non funziona con Android. Utilizzare la versione 146 o 147.

+0

Ho ancora ricevuto l'errore dalla versione 146, 147 e 148 !! –

1

Ho ricevuto aiuto da qualcun altro. la soluzione è a pochi passi seguono:

  • 1, Download strumento KeyStore Explorer
  • 2, dopo l'installazione, aprite il vostro certificato di BKS, poi trovare Strumenti-> Modifica tipo
  • 3, selezionare BKS-V1, quindi salvare e usarlo.