[email protected] | b47feba | 2012-04-24 01:34:41 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
[email protected] | 6dbdaa8 | 2011-08-11 16:05:56 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include "net/android/network_library.h" |
| 6 | |
[email protected] | 6dbdaa8 | 2011-08-11 16:05:56 | [diff] [blame] | 7 | #include "base/android/jni_android.h" |
| 8 | #include "base/android/jni_array.h" |
| 9 | #include "base/android/jni_string.h" |
[email protected] | b47feba | 2012-04-24 01:34:41 | [diff] [blame] | 10 | #include "base/android/scoped_java_ref.h" |
[email protected] | 6dbdaa8 | 2011-08-11 16:05:56 | [diff] [blame] | 11 | #include "base/logging.h" |
[email protected] | e46f6615 | 2012-07-19 20:02:55 | [diff] [blame^] | 12 | #include "jni/AndroidNetworkLibrary_jni.h" |
[email protected] | 6dbdaa8 | 2011-08-11 16:05:56 | [diff] [blame] | 13 | |
| 14 | using base::android::AttachCurrentThread; |
[email protected] | b47feba | 2012-04-24 01:34:41 | [diff] [blame] | 15 | using base::android::ClearException; |
[email protected] | 6dbdaa8 | 2011-08-11 16:05:56 | [diff] [blame] | 16 | using base::android::ConvertJavaStringToUTF8; |
| 17 | using base::android::ConvertUTF8ToJavaString; |
| 18 | using base::android::GetApplicationContext; |
[email protected] | b47feba | 2012-04-24 01:34:41 | [diff] [blame] | 19 | using base::android::ScopedJavaLocalRef; |
[email protected] | 6dbdaa8 | 2011-08-11 16:05:56 | [diff] [blame] | 20 | using base::android::ToJavaArrayOfByteArray; |
| 21 | using base::android::ToJavaByteArray; |
| 22 | |
| 23 | namespace net { |
| 24 | namespace android { |
| 25 | |
| 26 | VerifyResult VerifyX509CertChain(const std::vector<std::string>& cert_chain, |
[email protected] | 6dbdaa8 | 2011-08-11 16:05:56 | [diff] [blame] | 27 | const std::string& auth_type) { |
| 28 | JNIEnv* env = AttachCurrentThread(); |
[email protected] | b47feba | 2012-04-24 01:34:41 | [diff] [blame] | 29 | if (!env || !g_AndroidNetworkLibrary_verifyServerCertificates) { |
| 30 | // TODO(bulach): Remove when we initialize the JVM in unit tests. |
[email protected] | 6dbdaa8 | 2011-08-11 16:05:56 | [diff] [blame] | 31 | LOG(WARNING) << "JNI initialization failed"; |
| 32 | return VERIFY_INVOCATION_ERROR; |
| 33 | } |
| 34 | |
[email protected] | b47feba | 2012-04-24 01:34:41 | [diff] [blame] | 35 | ScopedJavaLocalRef<jobjectArray> chain_byte_array = |
| 36 | ToJavaArrayOfByteArray(env, cert_chain); |
| 37 | DCHECK(!chain_byte_array.is_null()); |
[email protected] | 6dbdaa8 | 2011-08-11 16:05:56 | [diff] [blame] | 38 | |
[email protected] | b47feba | 2012-04-24 01:34:41 | [diff] [blame] | 39 | ScopedJavaLocalRef<jstring> auth_string = |
| 40 | ConvertUTF8ToJavaString(env, auth_type); |
| 41 | DCHECK(!auth_string.is_null()); |
[email protected] | 6dbdaa8 | 2011-08-11 16:05:56 | [diff] [blame] | 42 | |
[email protected] | b47feba | 2012-04-24 01:34:41 | [diff] [blame] | 43 | jboolean trusted = Java_AndroidNetworkLibrary_verifyServerCertificates( |
| 44 | env, chain_byte_array.obj(), auth_string.obj()); |
| 45 | if (ClearException(env)) |
| 46 | return VERIFY_INVOCATION_ERROR; |
[email protected] | 6dbdaa8 | 2011-08-11 16:05:56 | [diff] [blame] | 47 | |
[email protected] | b47feba | 2012-04-24 01:34:41 | [diff] [blame] | 48 | return trusted ? VERIFY_OK : VERIFY_NO_TRUSTED_ROOT; |
[email protected] | 6dbdaa8 | 2011-08-11 16:05:56 | [diff] [blame] | 49 | } |
| 50 | |
| 51 | bool StoreKeyPair(const uint8* public_key, |
| 52 | size_t public_len, |
| 53 | const uint8* private_key, |
| 54 | size_t private_len) { |
| 55 | JNIEnv* env = AttachCurrentThread(); |
[email protected] | b47feba | 2012-04-24 01:34:41 | [diff] [blame] | 56 | ScopedJavaLocalRef<jbyteArray> public_array = |
| 57 | ToJavaByteArray(env, public_key, public_len); |
| 58 | ScopedJavaLocalRef<jbyteArray> private_array = |
| 59 | ToJavaByteArray(env, private_key, private_len); |
[email protected] | 6dbdaa8 | 2011-08-11 16:05:56 | [diff] [blame] | 60 | jboolean ret = Java_AndroidNetworkLibrary_storeKeyPair(env, |
[email protected] | b47feba | 2012-04-24 01:34:41 | [diff] [blame] | 61 | GetApplicationContext(), public_array.obj(), private_array.obj()); |
| 62 | LOG_IF(WARNING, !ret) << |
| 63 | "Call to Java_AndroidNetworkLibrary_storeKeyPair failed"; |
| 64 | return ret; |
[email protected] | 6dbdaa8 | 2011-08-11 16:05:56 | [diff] [blame] | 65 | } |
| 66 | |
[email protected] | c470e1b | 2012-06-15 07:46:39 | [diff] [blame] | 67 | bool HaveOnlyLoopbackAddresses() { |
| 68 | JNIEnv* env = AttachCurrentThread(); |
| 69 | return Java_AndroidNetworkLibrary_haveOnlyLoopbackAddresses(env); |
| 70 | } |
| 71 | |
[email protected] | 6dbdaa8 | 2011-08-11 16:05:56 | [diff] [blame] | 72 | bool GetMimeTypeFromExtension(const std::string& extension, |
| 73 | std::string* result) { |
| 74 | JNIEnv* env = AttachCurrentThread(); |
| 75 | |
[email protected] | b47feba | 2012-04-24 01:34:41 | [diff] [blame] | 76 | ScopedJavaLocalRef<jstring> extension_string = |
| 77 | ConvertUTF8ToJavaString(env, extension); |
| 78 | ScopedJavaLocalRef<jstring> ret = |
| 79 | Java_AndroidNetworkLibrary_getMimeTypeFromExtension( |
| 80 | env, extension_string.obj()); |
[email protected] | 6dbdaa8 | 2011-08-11 16:05:56 | [diff] [blame] | 81 | |
[email protected] | b47feba | 2012-04-24 01:34:41 | [diff] [blame] | 82 | if (!ret.obj()) { |
[email protected] | 6dbdaa8 | 2011-08-11 16:05:56 | [diff] [blame] | 83 | LOG(WARNING) << "Call to getMimeTypeFromExtension failed"; |
| 84 | return false; |
| 85 | } |
[email protected] | b47feba | 2012-04-24 01:34:41 | [diff] [blame] | 86 | *result = ConvertJavaStringToUTF8(ret); |
[email protected] | 6dbdaa8 | 2011-08-11 16:05:56 | [diff] [blame] | 87 | return true; |
| 88 | } |
| 89 | |
| 90 | bool RegisterNetworkLibrary(JNIEnv* env) { |
| 91 | return RegisterNativesImpl(env); |
| 92 | } |
| 93 | |
| 94 | } // namespace android |
| 95 | } // namespace net |