- 証明書をres/rawに置く (この例ではself.cerとします)
- その証明書を利用してTrustManagerを生成し,SSLSocketFactoryのインスタンスを得る.
参考: http://developer.android.com/training/articles/security-ssl.html#UnknownCa (英語) - 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)));
}
}