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.
5 #include "net/android/network_library.h"
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"
14 using base::android::AttachCurrentThread
;
15 using base::android::ConvertJavaStringToUTF8
;
16 using base::android::ConvertUTF8ToJavaString
;
17 using base::android::GetApplicationContext
;
18 using base::android::ScopedJavaLocalRef
;
19 using base::android::ToJavaArrayOfByteArray
;
20 using base::android::ToJavaByteArray
;
25 void VerifyX509CertChain(const std::vector
<std::string
>& cert_chain
,
26 const std::string
& auth_type
,
27 const std::string
& host
,
28 CertVerifyStatusAndroid
* status
,
29 bool* is_issued_by_known_root
,
30 std::vector
<std::string
>* verified_chain
) {
31 JNIEnv
* env
= AttachCurrentThread();
33 ScopedJavaLocalRef
<jobjectArray
> chain_byte_array
=
34 ToJavaArrayOfByteArray(env
, cert_chain
);
35 DCHECK(!chain_byte_array
.is_null());
37 ScopedJavaLocalRef
<jstring
> auth_string
=
38 ConvertUTF8ToJavaString(env
, auth_type
);
39 DCHECK(!auth_string
.is_null());
41 ScopedJavaLocalRef
<jstring
> host_string
=
42 ConvertUTF8ToJavaString(env
, host
);
43 DCHECK(!host_string
.is_null());
45 ScopedJavaLocalRef
<jobject
> result
=
46 Java_AndroidNetworkLibrary_verifyServerCertificates(
47 env
, chain_byte_array
.obj(), auth_string
.obj(), host_string
.obj());
49 ExtractCertVerifyResult(result
.obj(),
50 status
, is_issued_by_known_root
, verified_chain
);
53 void AddTestRootCertificate(const uint8_t* cert
, size_t len
) {
54 JNIEnv
* env
= AttachCurrentThread();
55 ScopedJavaLocalRef
<jbyteArray
> cert_array
= ToJavaByteArray(env
, cert
, len
);
56 DCHECK(!cert_array
.is_null());
57 Java_AndroidNetworkLibrary_addTestRootCertificate(env
, cert_array
.obj());
60 void ClearTestRootCertificates() {
61 JNIEnv
* env
= AttachCurrentThread();
62 Java_AndroidNetworkLibrary_clearTestRootCertificates(env
);
65 bool StoreKeyPair(const uint8_t* public_key
,
67 const uint8_t* private_key
,
69 JNIEnv
* env
= AttachCurrentThread();
70 ScopedJavaLocalRef
<jbyteArray
> public_array
=
71 ToJavaByteArray(env
, public_key
, public_len
);
72 ScopedJavaLocalRef
<jbyteArray
> private_array
=
73 ToJavaByteArray(env
, private_key
, private_len
);
74 jboolean ret
= Java_AndroidNetworkLibrary_storeKeyPair(env
,
75 GetApplicationContext(), public_array
.obj(), private_array
.obj());
76 LOG_IF(WARNING
, !ret
) <<
77 "Call to Java_AndroidNetworkLibrary_storeKeyPair failed";
81 void StoreCertificate(net::CertificateMimeType cert_type
,
84 JNIEnv
* env
= AttachCurrentThread();
85 ScopedJavaLocalRef
<jbyteArray
> data_array
=
86 ToJavaByteArray(env
, reinterpret_cast<const uint8_t*>(data
), data_len
);
87 jboolean ret
= Java_AndroidNetworkLibrary_storeCertificate(env
,
88 GetApplicationContext(), cert_type
, data_array
.obj());
89 LOG_IF(WARNING
, !ret
) <<
90 "Call to Java_AndroidNetworkLibrary_storeCertificate"
92 // Intentionally do not return 'ret', there is little the caller can
93 // do in case of failure (the CertInstaller itself will deal with
94 // incorrect data and display the appropriate toast).
97 bool HaveOnlyLoopbackAddresses() {
98 JNIEnv
* env
= AttachCurrentThread();
99 return Java_AndroidNetworkLibrary_haveOnlyLoopbackAddresses(env
);
102 bool GetMimeTypeFromExtension(const std::string
& extension
,
103 std::string
* result
) {
104 JNIEnv
* env
= AttachCurrentThread();
106 ScopedJavaLocalRef
<jstring
> extension_string
=
107 ConvertUTF8ToJavaString(env
, extension
);
108 ScopedJavaLocalRef
<jstring
> ret
=
109 Java_AndroidNetworkLibrary_getMimeTypeFromExtension(
110 env
, extension_string
.obj());
114 *result
= ConvertJavaStringToUTF8(ret
);
118 std::string
GetTelephonyNetworkCountryIso() {
119 return base::android::ConvertJavaStringToUTF8(
120 Java_AndroidNetworkLibrary_getNetworkCountryIso(
121 base::android::AttachCurrentThread(),
122 base::android::GetApplicationContext()));
125 std::string
GetTelephonyNetworkOperator() {
126 return base::android::ConvertJavaStringToUTF8(
127 Java_AndroidNetworkLibrary_getNetworkOperator(
128 base::android::AttachCurrentThread(),
129 base::android::GetApplicationContext()));
132 bool RegisterNetworkLibrary(JNIEnv
* env
) {
133 return RegisterNativesImpl(env
);
136 } // namespace android