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