Linux サーバーと Android アプリの間で相互認証 SSL をセットアップしようとしています。これまでのところ、SSL 経由でサーバー証明書と通信するアプリを動作させることができましたが、クライアント証明書のみを受け入れるようにサーバーを設定すると、動作しなくなります。サーバーの構成は問題ないようですが、私は行き詰まっています。私の最善の推測は、クライアント証明書がサーバーに正しく提示されていないが、次にテストする方法がわからないということです。OS X キーチェーンのクライアントに .pem を使用しようとしましたが、ブラウザがその証明書で動作しないようです。次に、https接続を実現でき、APPが署名されていないサーバー証明書を受け入れるため、サーバー証明書は完全に機能します。
私が使用しているコードは、さまざまなチュートリアルの組み合わせです。回答は、ブックマークした主なものです。
- http://www.androidhive.info/2012/05/how-to-connect-android-with-php-mysql/
- Android での自己署名 SSL の受け入れ
- モバイルアプリでコミュニケーション [信頼性、プライバシー、完全性] を確保しますか?
- (サーバー構成の場合) https://security.stackexchange.com/questions/34897/configure-ssl-mutual-two-way-authentication
これは、私が接続に使用している 2 つの主要なクラスです。1) このクラスは、JSON の解析を処理し、REQUESTS を実行します。
package edu.hci.additional;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.util.List;
import android.content.Context;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.utils.URLEncodedUtils;
import org.json.JSONException;
import org.json.JSONObject;
import android.util.Log;
public class JSONParser {
static InputStream is = null;
static JSONObject jObj = null;
static String json = "";
// constructor
public JSONParser() {
}
// function get json from url
// by making HTTP POST or GET mehtod
public JSONObject makeHttpRequest(String url, String method,
List<NameValuePair> params, Context context) {
// Making HTTP request
try {
// check for request method
if(method == "POST"){
// request method is POST
// defaultHttpClient
SecureHttpClient httpClient = new SecureHttpClient(context);
HttpPost httpPost = new HttpPost(url);
httpPost.setEntity(new UrlEncodedFormEntity(params));
HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
is = httpEntity.getContent();
}else if(method == "GET"){
// request method is GET
SecureHttpClient httpClient = new SecureHttpClient(context);
String paramString = URLEncodedUtils.format(params, "utf-8");
url += "?" + paramString;
HttpGet httpGet = new HttpGet(url);
HttpResponse httpResponse = httpClient.execute(httpGet);
HttpEntity httpEntity = httpResponse.getEntity();
is = httpEntity.getContent();
}
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(
is, "iso-8859-1"), 8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
is.close();
json = sb.toString();
} catch (Exception e) {
Log.e("Buffer Error", "Error converting result " + e.toString());
}
// try parse the string to a JSON object
try {
jObj = new JSONObject(json);
} catch (JSONException e) {
Log.e("JSON Parser", "Error parsing data " + e.toString());
}
// return JSON String
return jObj;
}
}
この 2 番目のクラスは、SSL 認証を処理します。
package edu.hci.additional;
import android.content.Context;
import android.util.Log;
import edu.hci.R;
import org.apache.http.conn.scheme.PlainSocketFactory;
import org.apache.http.conn.scheme.Scheme;
import org.apache.http.conn.scheme.SchemeRegistry;
import org.apache.http.conn.ssl.SSLSocketFactory;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.HttpParams;
import java.io.IOException;
import java.io.InputStream;
import java.security.*;
public class SecureHttpClient extends DefaultHttpClient {
private static Context appContext = null;
private static HttpParams params = null;
private static SchemeRegistry schmReg = null;
private static Scheme httpsScheme = null;
private static Scheme httpScheme = null;
private static String TAG = "MyHttpClient";
public SecureHttpClient(Context myContext) {
appContext = myContext;
if (httpScheme == null || httpsScheme == null) {
httpScheme = new Scheme("http", PlainSocketFactory.getSocketFactory(), 80);
httpsScheme = new Scheme("https", mySSLSocketFactory(), 443);
}
getConnectionManager().getSchemeRegistry().register(httpScheme);
getConnectionManager().getSchemeRegistry().register(httpsScheme);
}
private SSLSocketFactory mySSLSocketFactory() {
SSLSocketFactory ret = null;
try {
final KeyStore clientCert = KeyStore.getInstance("BKS");
final KeyStore serverCert = KeyStore.getInstance("BKS");
final InputStream client_inputStream = appContext.getResources().openRawResource(R.raw.authclientcerts);
final InputStream server_inputStream = appContext.getResources().openRawResource(R.raw.certs);
clientCert.load(client_inputStream, appContext.getString(R.string.client_store_pass).toCharArray());
serverCert.load(server_inputStream, appContext.getString(R.string.server_store_pass).toCharArray());
String client_password = appContext.getString(R.string.client_store_pass);
server_inputStream.close();
client_inputStream.close();
ret = new SSLSocketFactory(clientCert,client_password,serverCert);
} catch (UnrecoverableKeyException ex) {
Log.d(TAG, ex.getMessage());
} catch (KeyStoreException ex) {
Log.d(TAG, ex.getMessage());
} catch (KeyManagementException ex) {
Log.d(TAG, ex.getMessage());
} catch (NoSuchAlgorithmException ex) {
Log.d(TAG, ex.getMessage());
} catch (IOException ex) {
Log.d(TAG, ex.getMessage());
} catch (Exception ex) {
Log.d(TAG, ex.getMessage());
} finally {
return ret;
}
}
}
このコマンドでopensslを使用したキーを作成するには:
openssl req -nodes -x509 -newkey rsa:2048 -keyout key.pem -out cert.pem -days 500
Android 用 BKS のキーを取得するために、 http ://www.bouncycastle.org/latest_releases.html にある弾む城 bcprov-jdk15on-150.jar を使用しました。
そして、次のコマンドを使用しました:
keytool -import -v -trustcacerts -alias 0 -file ~/cert.pem -keystore ~/Downloads/authclientcerts.bks -storetype BKS -provider org.bouncycastle.jce.provider.BouncyCastleProvider -providerpath ~/Downloads/bcprov-jdk15on-150.jar -storepass passWORD
最後に、Fedora 19 でクライアント証明書を要求し、(作成したクライアント証明書と一致する) 証明書の有効性を確認するために /etc/httpd/conf.d/ssl.conf に追加した行は次のとおりです。
...
SSLVerifyClient require
SSLVerifyDepth 5
...
<Location />
SSLRequire ( %{SSL_CLIENT_S_DN_O} eq "Develop" \
and %{SSL_CLIENT_S_DN_OU} in {"Staff", "Operations", "Dev"} )
</Location>
...
SSLOptions +FakeBasicAuth +StrictRequire
この構成ファイルで多くの組み合わせを試しましたが、すべて同じ結果になり、「SSLPeerUnverifiedException: No peer certificate」例外が発生しました。サーバーの SSL 構成ファイルにこの行をコメントすると、すべて正常に動作しますが、サーバーはすべてのクライアントを受け入れますが、これは私が必要としているものではありません。
前もって感謝します :)
アップデート
@EJPの回答はうまくいきました
最初に、証明書を正しい ( /etc/pki/tls/certs/ ) パスに追加し、次を使用してロードする必要がありました。
ln -s ca-andr.crt $( openssl x509 -hash -noout -in ca-andr.crt )".0"
これにより、「f3f24175.0」のような名前の openSSL 読み取り可能なシンボリック リンクが作成されます。</p>
次に、新しい証明書ファイルを /etc/httpd/conf.d/ssl.conf 構成ファイルに設定します。
…
SSLCACertificateFile /etc/pki/tls/certs/f2f62175.0
…
ここで http サービスを再起動し、証明書がロードされているかどうかをテストします。
openssl verify -CApath /etc/pki/tls/certs/ f2f62175.0
すべて問題なければ、次のように表示されます。
f3f24175.0: わかりました
そして、次のコマンドでテストを終了できます:
openssl s_client -connect example.com:443 -CApath /etc/pki/tls/certs
これにより、信頼できるクライアント証明書のリストが返されます (追加した証明書が機能している場合)。
問題の 2 番目の部分は、authclientcerts.BKS に秘密鍵が含まれていないため、指定したパスワードが使用されず、サーバーが証明書を認証しないことでした。そのため、鍵と証明書を pkcs12 にエクスポートし、それに応じて JAVA コードを更新しました。
エクスポート コマンド:
openssl pkcs12 -export -in ~/cert.pem -inkey ~/key.pem > android_client_p12.p12
次に、SecureHttpClient.java クラスの一部を変更して、BKS の代わりに PKCS12 でクライアント証明書を作成しました。
キー ストア タイプを BKS から PKCS12 に変更するには、以下を置き換えました。
final KeyStore clientCert = KeyStore.getInstance("BKS”);
このため:
final KeyStore clientCert = KeyStore.getInstance("PKCS12");
次に、 res/raw/ にある実際のキー ストア ファイルへの参照を更新しました。
final InputStream client_inputStream = appContext.getResources().openRawResource(R.raw.authclientcerts);
このため:
final InputStream client_inputStream = appContext.getResources().openRawResource(R.raw.android_client_p12);
そして、それはトリックをしました:D