SORU
6 Ocak 2010, ÇARŞAMBA


Android HTTPs için sertifika kabul

Android telefonlarda Https bağlantıları, HttpClient kullanarak yapmaya çalışıyorum. Sorun sertifika imzalı olmadığı için "javax.alıyorum olmasıdır net.ssl.SSLException: server sertifika güvenilir Değil".

Şimdi sadece tüm sertifikaları kabul, ama eğer kullanıcı sormak istiyorum, eğer bir çözüm gördüm? Kullanıcı izin devam etmek için karar ya da değil, bir iletişim tarayıcı benzer olsun istiyorum.

Tercihen tarayıcı olarak aynı certificatestore kullanmak istiyorum. Herhangi bir fikir?

CEVAP
22 EKİM 2010, Cuma


Aşağıdaki ana adımları android platformu tarafından güvenilir olarak kabul edilmemektedir hangi Sertifika Yetkilileri tarafından verilen güvenli bir bağlantı elde etmek için gereklidir.

Birçok kullanıcı tarafından talep edildiği gibi, blog article buradan bizim en önemli parçalarından aynalı ettim

  1. Tüm gerekli sertifikaları (kök ve herhangi bir ara CA) al
  2. Keytool ve BouncyCastle sağlayıcı ve ithalat ile bir anahtar deposu sertifikaları oluşturun
  3. Android uygulaması içinde anahtar deposu yük ve güvenli bağlantı kullanın (java.net.ssl.HttpsURLConnection standart (anlamak daha kolay, daha fazla ölçülebilir) yerine. Apache HttpClient kullanmanızı tavsiye ederim

Sertifikaları al

Kök CA için son nokta sertifika zinciri tüm yol inşa tüm sertifikalar almak zorunda. Bu herhangi bir (varsa) Ara CA sertifikaları ve ayrıca Root cert CA anlamına gelir. Bitiş noktası sertifikası almaya gerek yok.

Anahtar deposu oluşturmak

BouncyCastle Provider indirme ve bilinen bir yere saklayın. Ayrıca keytool komutu (genellikle TOTEM kurulumunuzun bin klasörü altında bulunan) çağırabilirsiniz emin olun.

Şimdi ithal edilen sertifikaları (bitiş noktası sertifikası almak yok) BouncyCastle biçimlendirilmiş bir anahtar deposu içine.

Henüz test etmedim ama sertifika alma sırası önemlidir bence. Bunun anlamı, kanseri Ara CA sertifikası ilk almak ve daha sonra Kök CA sertifikası için tüm yol.

Aşağıdaki komutu yeni bir anahtar deposunda zaten mevcut değilse) şifre ile birliktemysecretoluşturulur ve Ara CA sertifikası alınır. Ben de dosya sistemi ve deposu biçimi benim üzerinde bulunduğu BouncyCastle sağlayıcı tanımlanmış. Zincirdeki her sertifika için bu komutu kullanın.

keytool -importcert -v -trustcacerts -file "path_to_cert/interm_ca.cer" -alias IntermediateCA -keystore "res/raw/myKeystore.bks" -provider org.bouncycastle.jce.provider.BouncyCastleProvider -providerpath "path_to_bouncycastle/bcprov-jdk16-145.jar" -storetype BKS -storepass mysecret

Sertifikalar doğru anahtar deposu ithal olsaydı doğrulayın:

keytool -list -keystore "res/raw/myKeystore.bks" -provider org.bouncycastle.jce.provider.BouncyCastleProvider -providerpath "path_to_bouncycastle/bcprov-jdk16-145.jar" -storetype BKS -storepass mysecret

Çıkış tüm zinciri:

RootCA, 22.10.2010, trustedCertEntry, Thumbprint (MD5): 24:77:D9:A8:91:D1:3B:FA:88:2D:C2:FF:F8:CD:33:93
IntermediateCA, 22.10.2010, trustedCertEntry, Thumbprint (MD5): 98:0F:C3:F8:39:F7:D8:05:07:02:0D:E3:14:5B:29:43

Şimdi res/raw/ altında android uygulaması ham bir kaynak deposu olarak kopyalayabilirsiniz

Uygulamanızda anahtar deposu kullanın

Öncelikle HTTPS bağlantıları için anahtar deposu kullanan özel Apache HttpClient oluşturma

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, "mysecret".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);
      }
  }
}

Özel bizim HttpClient yarattık, şimdi sadece güvenli bağlantı için kullanabiliriz. Bir ELDE yaptığımızda örneğin DİNLENME bir kaynak arayın.

// Instantiate the custom HttpClient
DefaultHttpClient client = new MyHttpClient(getApplicationContext());
HttpGet get = new HttpGet("https://www.mydomain.ch/rest/contacts/23");
// Execute the GET call and obtain the response
HttpResponse getResponse = client.execute(get);
HttpEntity responseEntity = getResponse.getEntity();

İşte bu ;)

Bunu Paylaş:
  • Google+
  • E-Posta
Etiketler:

YORUMLAR

SPONSOR VİDEO

Rastgele Yazarlar

  • adrianisen

    adrianisen

    25 Kasım 2009
  • CMTelly

    CMTelly

    2 Mayıs 2007
  • The White House

    The White Ho

    21 Ocak 2006