diff options
author | michaelbai@google.com <michaelbai@google.com@0039d316-1c4b-4281-b951-d872f2087c98> | 2011-08-11 16:05:56 +0000 |
---|---|---|
committer | michaelbai@google.com <michaelbai@google.com@0039d316-1c4b-4281-b951-d872f2087c98> | 2011-08-11 16:05:56 +0000 |
commit | 6dbdaa892d120c6fbb1355aeb0ba8810dad12840 (patch) | |
tree | 59b446fad4cccc144953888d0c2861d44782458b /net/android/network_library.cc | |
parent | b20c447c5d17bb563498c710445627ae4bfb6137 (diff) | |
download | chromium_src-6dbdaa892d120c6fbb1355aeb0ba8810dad12840.zip chromium_src-6dbdaa892d120c6fbb1355aeb0ba8810dad12840.tar.gz chromium_src-6dbdaa892d120c6fbb1355aeb0ba8810dad12840.tar.bz2 |
Upstream certificate and mime Android implementation.
BUG=
TEST=
Review URL: http://codereview.chromium.org/7538029
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@96401 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'net/android/network_library.cc')
-rw-r--r-- | net/android/network_library.cc | 109 |
1 files changed, 109 insertions, 0 deletions
diff --git a/net/android/network_library.cc b/net/android/network_library.cc new file mode 100644 index 0000000..6fbcd30 --- /dev/null +++ b/net/android/network_library.cc @@ -0,0 +1,109 @@ +// Copyright (c) 2010 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "net/android/network_library.h" + +#include "base/android/auto_jobject.h" +#include "base/android/jni_android.h" +#include "base/android/jni_array.h" +#include "base/android/jni_string.h" +#include "base/logging.h" +#include "jni/android_network_library_jni.h" + +using base::android::AttachCurrentThread; +using base::android::AutoJObject; +using base::android::CheckException; +using base::android::ConvertJavaStringToUTF8; +using base::android::ConvertUTF8ToJavaString; +using base::android::GetApplicationContext; +using base::android::ToJavaArrayOfByteArray; +using base::android::ToJavaByteArray; + +namespace net { +namespace android { + +VerifyResult VerifyX509CertChain(const std::vector<std::string>& cert_chain, + const std::string& hostname, + const std::string& auth_type) { + JNIEnv* env = AttachCurrentThread(); + if (!env) { + // TODO(husky): Maybe initialize the JVM in unit tests? + LOG(WARNING) << "JNI initialization failed"; + return VERIFY_INVOCATION_ERROR; + } + + AutoJObject chain_byte_array = AutoJObject::FromLocalRef( + env, ToJavaArrayOfByteArray(env, cert_chain)); + DCHECK(chain_byte_array.obj()); + + AutoJObject host_string = AutoJObject::FromLocalRef( + env, ConvertUTF8ToJavaString(env, hostname)); + DCHECK(host_string.obj()); + + AutoJObject auth_string = AutoJObject::FromLocalRef( + env, ConvertUTF8ToJavaString(env, auth_type)); + DCHECK(auth_string.obj()); + + jint error = Java_AndroidNetworkLibrary_verifyServerCertificates( + env, static_cast<jobjectArray>(chain_byte_array.obj()), + static_cast<jstring>(host_string.obj()), + static_cast<jstring>(auth_string.obj())); + + switch (error) { + case 0: + return VERIFY_OK; + case 1: + return VERIFY_BAD_HOSTNAME; + case 2: + return VERIFY_NO_TRUSTED_ROOT; + } + return VERIFY_INVOCATION_ERROR; +} + +bool StoreKeyPair(const uint8* public_key, + size_t public_len, + const uint8* private_key, + size_t private_len) { + JNIEnv* env = AttachCurrentThread(); + AutoJObject public_array = AutoJObject::FromLocalRef( + env, ToJavaByteArray(env, public_key, public_len)); + AutoJObject private_array = AutoJObject::FromLocalRef( + env, ToJavaByteArray(env, private_key, private_len)); + jboolean ret = Java_AndroidNetworkLibrary_storeKeyPair(env, + GetApplicationContext(), + static_cast<jbyteArray>(public_array.obj()), + static_cast<jbyteArray>(private_array.obj())); + if (CheckException(env) || !ret) { + LOG(WARNING) << "Call to Java_AndroidNetworkLibrary_storeKeyPair failed"; + return false; + } + return true; +} + +bool GetMimeTypeFromExtension(const std::string& extension, + std::string* result) { + JNIEnv* env = AttachCurrentThread(); + + AutoJObject extension_string = AutoJObject::FromLocalRef( + env, ConvertUTF8ToJavaString(env, extension)); + AutoJObject ret = AutoJObject::FromLocalRef( + env, Java_AndroidNetworkLibrary_getMimeTypeFromExtension( + env, static_cast<jstring>(extension_string.obj()))); + + if (CheckException(env) || !ret.obj()) { + LOG(WARNING) << "Call to getMimeTypeFromExtension failed"; + return false; + } + *result = ConvertJavaStringToUTF8(env, static_cast<jstring>(ret.obj())); + return true; +} + +bool RegisterNetworkLibrary(JNIEnv* env) { + return RegisterNativesImpl(env); +} + +} // namespace android +} // namespace net + + |