This source file includes following definitions.
- RecordClientCertificateKey
- StartClientCertificateRequest
- OnSystemRequestCompletion
- NotifyClientCertificatesChanged
- NotifyClientCertificatesChangedOnIOThread
- RegisterSSLClientCertificateRequestAndroid
- ShowSSLClientCertificateSelector
#include "chrome/browser/ui/android/ssl_client_certificate_request.h"
#include "base/android/jni_array.h"
#include "base/android/jni_string.h"
#include "base/android/scoped_java_ref.h"
#include "base/basictypes.h"
#include "base/bind.h"
#include "base/callback_helpers.h"
#include "base/compiler_specific.h"
#include "base/logging.h"
#include "chrome/browser/ssl/ssl_client_certificate_selector.h"
#include "chrome/browser/ui/android/window_android_helper.h"
#include "content/public/browser/browser_thread.h"
#include "jni/SSLClientCertificateRequest_jni.h"
#include "net/android/keystore_openssl.h"
#include "net/base/host_port_pair.h"
#include "net/cert/cert_database.h"
#include "net/cert/x509_certificate.h"
#include "net/ssl/openssl_client_key_store.h"
#include "net/ssl/ssl_cert_request_info.h"
#include "net/ssl/ssl_client_cert_type.h"
#include "ui/base/android/window_android.h"
namespace chrome {
namespace {
typedef net::OpenSSLClientKeyStore::ScopedEVP_PKEY ScopedEVP_PKEY;
void RecordClientCertificateKey(
const scoped_refptr<net::X509Certificate>& client_cert,
ScopedEVP_PKEY private_key) {
DCHECK_CURRENTLY_ON(content::BrowserThread::IO);
net::OpenSSLClientKeyStore::GetInstance()->RecordClientCertPrivateKey(
client_cert.get(), private_key.get());
}
void StartClientCertificateRequest(
const net::SSLCertRequestInfo* cert_request_info,
ui::WindowAndroid* window,
const chrome::SelectCertificateCallback& callback) {
DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
base::Closure post_task_closure = base::Bind(
base::IgnoreResult(&content::BrowserThread::PostTask),
content::BrowserThread::UI,
FROM_HERE,
base::Bind(callback, scoped_refptr<net::X509Certificate>()));
base::ScopedClosureRunner guard(post_task_closure);
std::vector<std::string> key_types;
for (size_t n = 0; n < cert_request_info->cert_key_types.size(); ++n) {
switch (cert_request_info->cert_key_types[n]) {
case net::CLIENT_CERT_RSA_SIGN:
key_types.push_back("RSA");
break;
case net::CLIENT_CERT_DSS_SIGN:
key_types.push_back("DSA");
break;
case net::CLIENT_CERT_ECDSA_SIGN:
key_types.push_back("ECDSA");
break;
default:
break;
}
}
JNIEnv* env = base::android::AttachCurrentThread();
ScopedJavaLocalRef<jobjectArray> key_types_ref =
base::android::ToJavaArrayOfStrings(env, key_types);
if (key_types_ref.is_null()) {
LOG(ERROR) << "Could not create key types array (String[])";
return;
}
ScopedJavaLocalRef<jobjectArray> principals_ref =
base::android::ToJavaArrayOfByteArray(
env, cert_request_info->cert_authorities);
if (principals_ref.is_null()) {
LOG(ERROR) << "Could not create principals array (byte[][])";
return;
}
ScopedJavaLocalRef<jstring> host_name_ref =
base::android::ConvertUTF8ToJavaString(
env, cert_request_info->host_and_port.host());
scoped_ptr<chrome::SelectCertificateCallback> request(
new chrome::SelectCertificateCallback(callback));
jlong request_id = reinterpret_cast<intptr_t>(request.get());
if (!chrome::android::
Java_SSLClientCertificateRequest_selectClientCertificate(
env,
request_id,
window->GetJavaObject().obj(),
key_types_ref.obj(),
principals_ref.obj(),
host_name_ref.obj(),
cert_request_info->host_and_port.port())) {
return;
}
ignore_result(guard.Release());
chrome::SelectCertificateCallback* ALLOW_UNUSED dummy =
request.release();
}
}
namespace android {
static void OnSystemRequestCompletion(
JNIEnv* env,
jclass clazz,
jlong request_id,
jobjectArray encoded_chain_ref,
jobject private_key_ref) {
DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
scoped_ptr<chrome::SelectCertificateCallback> callback(
reinterpret_cast<chrome::SelectCertificateCallback*>(request_id));
base::Closure null_closure =
base::Bind(*callback, scoped_refptr<net::X509Certificate>());
base::ScopedClosureRunner guard(null_closure);
if (encoded_chain_ref == NULL || private_key_ref == NULL) {
LOG(ERROR) << "Client certificate request cancelled";
return;
}
std::vector<std::string> encoded_chain_strings;
if (encoded_chain_ref) {
base::android::JavaArrayOfByteArrayToStringVector(
env, encoded_chain_ref, &encoded_chain_strings);
}
std::vector<base::StringPiece> encoded_chain;
for (size_t n = 0; n < encoded_chain_strings.size(); ++n)
encoded_chain.push_back(encoded_chain_strings[n]);
scoped_refptr<net::X509Certificate> client_cert(
net::X509Certificate::CreateFromDERCertChain(encoded_chain));
if (!client_cert.get()) {
LOG(ERROR) << "Could not decode client certificate chain";
return;
}
ScopedEVP_PKEY private_key(
net::android::GetOpenSSLPrivateKeyWrapper(private_key_ref));
if (!private_key.get()) {
LOG(ERROR) << "Could not create OpenSSL wrapper for private key";
return;
}
ignore_result(guard.Release());
content::BrowserThread::PostTaskAndReply(
content::BrowserThread::IO,
FROM_HERE,
base::Bind(&RecordClientCertificateKey,
client_cert,
base::Passed(&private_key)),
base::Bind(*callback, client_cert));
}
static void NotifyClientCertificatesChanged() {
net::CertDatabase::GetInstance()->OnAndroidKeyStoreChanged();
}
static void NotifyClientCertificatesChangedOnIOThread(JNIEnv* env, jclass) {
if (content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)) {
NotifyClientCertificatesChanged();
} else {
content::BrowserThread::PostTask(
content::BrowserThread::IO,
FROM_HERE,
base::Bind(&NotifyClientCertificatesChanged));
}
}
bool RegisterSSLClientCertificateRequestAndroid(JNIEnv* env) {
return RegisterNativesImpl(env);
}
}
void ShowSSLClientCertificateSelector(
content::WebContents* contents,
const net::HttpNetworkSession* network_session,
net::SSLCertRequestInfo* cert_request_info,
const chrome::SelectCertificateCallback& callback) {
ui::WindowAndroid* window =
WindowAndroidHelper::FromWebContents(contents)->GetWindowAndroid();
DCHECK(window);
DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
StartClientCertificateRequest(cert_request_info, window, callback);
}
}