1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 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 7 #include "base/android/jni_android.h" 8 #include "base/android/jni_array.h" 9 #include "base/android/jni_string.h" 10 #include "base/android/scoped_java_ref.h" 11 #include "base/logging.h" 12 #include "jni/AndroidNetworkLibrary_jni.h" 13 14 using base::android::AttachCurrentThread; 15 using base::android::ClearException; 16 using base::android::ConvertJavaStringToUTF8; 17 using base::android::ConvertUTF8ToJavaString; 18 using base::android::GetApplicationContext; 19 using base::android::ScopedJavaLocalRef; 20 using base::android::ToJavaArrayOfByteArray; 21 using base::android::ToJavaByteArray; 22 23 namespace net { 24 namespace android { 25 26 CertVerifyResultAndroid VerifyX509CertChain( 27 const std::vector<std::string>& cert_chain, 28 const std::string& auth_type) { 29 JNIEnv* env = AttachCurrentThread(); 30 31 ScopedJavaLocalRef<jobjectArray> chain_byte_array = 32 ToJavaArrayOfByteArray(env, cert_chain); 33 DCHECK(!chain_byte_array.is_null()); 34 35 ScopedJavaLocalRef<jstring> auth_string = 36 ConvertUTF8ToJavaString(env, auth_type); 37 DCHECK(!auth_string.is_null()); 38 39 jint result = Java_AndroidNetworkLibrary_verifyServerCertificates( 40 env, chain_byte_array.obj(), auth_string.obj()); 41 42 return static_cast<CertVerifyResultAndroid>(result); 43 } 44 45 void AddTestRootCertificate(const uint8* cert, size_t len) { 46 JNIEnv* env = AttachCurrentThread(); 47 ScopedJavaLocalRef<jbyteArray> cert_array = ToJavaByteArray(env, cert, len); 48 DCHECK(!cert_array.is_null()); 49 Java_AndroidNetworkLibrary_addTestRootCertificate(env, cert_array.obj()); 50 } 51 52 void ClearTestRootCertificates() { 53 JNIEnv* env = AttachCurrentThread(); 54 Java_AndroidNetworkLibrary_clearTestRootCertificates(env); 55 } 56 57 bool StoreKeyPair(const uint8* public_key, 58 size_t public_len, 59 const uint8* private_key, 60 size_t private_len) { 61 JNIEnv* env = AttachCurrentThread(); 62 ScopedJavaLocalRef<jbyteArray> public_array = 63 ToJavaByteArray(env, public_key, public_len); 64 ScopedJavaLocalRef<jbyteArray> private_array = 65 ToJavaByteArray(env, private_key, private_len); 66 jboolean ret = Java_AndroidNetworkLibrary_storeKeyPair(env, 67 GetApplicationContext(), public_array.obj(), private_array.obj()); 68 LOG_IF(WARNING, !ret) << 69 "Call to Java_AndroidNetworkLibrary_storeKeyPair failed"; 70 return ret; 71 } 72 73 void StoreCertificate(net::CertificateMimeType cert_type, 74 const void* data, 75 size_t data_len) { 76 JNIEnv* env = AttachCurrentThread(); 77 ScopedJavaLocalRef<jbyteArray> data_array = 78 ToJavaByteArray(env, reinterpret_cast<const uint8*>(data), data_len); 79 jboolean ret = Java_AndroidNetworkLibrary_storeCertificate(env, 80 GetApplicationContext(), cert_type, data_array.obj()); 81 LOG_IF(WARNING, !ret) << 82 "Call to Java_AndroidNetworkLibrary_storeCertificate" 83 " failed"; 84 // Intentionally do not return 'ret', there is little the caller can 85 // do in case of failure (the CertInstaller itself will deal with 86 // incorrect data and display the appropriate toast). 87 } 88 89 bool HaveOnlyLoopbackAddresses() { 90 JNIEnv* env = AttachCurrentThread(); 91 return Java_AndroidNetworkLibrary_haveOnlyLoopbackAddresses(env); 92 } 93 94 std::string GetNetworkList() { 95 JNIEnv* env = AttachCurrentThread(); 96 ScopedJavaLocalRef<jstring> ret = 97 Java_AndroidNetworkLibrary_getNetworkList(env); 98 return ConvertJavaStringToUTF8(ret); 99 } 100 101 bool GetMimeTypeFromExtension(const std::string& extension, 102 std::string* result) { 103 JNIEnv* env = AttachCurrentThread(); 104 105 ScopedJavaLocalRef<jstring> extension_string = 106 ConvertUTF8ToJavaString(env, extension); 107 ScopedJavaLocalRef<jstring> ret = 108 Java_AndroidNetworkLibrary_getMimeTypeFromExtension( 109 env, extension_string.obj()); 110 111 if (!ret.obj()) 112 return false; 113 *result = ConvertJavaStringToUTF8(ret); 114 return true; 115 } 116 117 bool RegisterNetworkLibrary(JNIEnv* env) { 118 return RegisterNativesImpl(env); 119 } 120 121 } // namespace android 122 } // namespace net 123