LoginSignup
20
20

More than 5 years have passed since last update.

Volleyで自己署名証明書を使ったHTTPS通信

Last updated at Posted at 2013-11-03
  1. 証明書をres/rawに置く (この例ではself.cerとします)
  2. その証明書を利用してTrustManagerを生成し,SSLSocketFactoryのインスタンスを得る.
    参考: http://developer.android.com/training/articles/security-ssl.html#UnknownCa (英語)
  3. SSLSocketFactoryのインスタンスから,VolleyのBasicNetworkインスタンスを生成し,RequestQueueに渡す.

最終的に以下のようなソースコードになる.

public class MySSL {
    public static SSLSocketFactory getSocketFactory(Context context) {
        try {
            CertificateFactory certificateFactory = CertificateFactory.getInstance("X.509");
            Certificate certificate = certificateFactory.generateCertificate(
                    context.getResources().openRawResource(R.raw.self));
            Log.d("ca=" + ((X509Certificate) certificate).getSubjectDN());

            String keyStoreType = KeyStore.getDefaultType();
            KeyStore keyStore = KeyStore.getInstance(keyStoreType);
            keyStore.load(null, null);
            keyStore.setCertificateEntry("ca", certificate);

            TrustManagerFactory managerFactory = TrustManagerFactory.getInstance(
                    TrustManagerFactory.getDefaultAlgorithm());
            managerFactory.init(keyStore);

            SSLContext sslContext = SSLContext.getInstance("TLS");
            sslContext.init(null, managerFactory.getTrustManagers(), null);
            return sslContext.getSocketFactory();
        } catch (Exception e) {
            // TODO: handle error
        }
        return null;
    }
}
/*
 * 自己署名証明書のために,カスタム{@link SSLSocketFactory}を利用する{@link RequestQueue}.
 * Volley本家の実装を流用
**/
public class MyRequestQueue extends RequestQueue {
    private static final String DEFAULT_CACHE_DIR = "volley";
    private final Context context;

    public MyRequestQueue(final Context context) {
        super(
                new DiskBasedCache(new File(context.getCacheDir(), DEFAULT_CACHE_DIR)),
                createNetwork(context));
        this.context = context;
    }

    private static Network createNetwork(Context context) {
        String userAgent = "volley/0";
        try {
            String packageName = context.getPackageName();
            PackageInfo info = context.getPackageManager().getPackageInfo(packageName, 0);
            userAgent = packageName + "/" + info.versionCode;
        } catch (PackageManager.NameNotFoundException e) {
        }
        // Gingerbread以前のHttpUrlConnectionにはバグがあるので注意とのこと,今回は気にしない
        // See: http://android-developers.blogspot.com/2011/09/androids-http-clients.html
        return new BasicNetwork(new HurlStack(null, MySSL.getSocketFactory(context)));
    }
}
20
20
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
20
20