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::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
;
26 CertVerifyResultAndroid
VerifyX509CertChain(
27 const std::vector
<std::string
>& cert_chain
,
28 const std::string
& auth_type
) {
29 JNIEnv
* env
= AttachCurrentThread();
31 ScopedJavaLocalRef
<jobjectArray
> chain_byte_array
=
32 ToJavaArrayOfByteArray(env
, cert_chain
);
33 DCHECK(!chain_byte_array
.is_null());
35 ScopedJavaLocalRef
<jstring
> auth_string
=
36 ConvertUTF8ToJavaString(env
, auth_type
);
37 DCHECK(!auth_string
.is_null());
39 jint result
= Java_AndroidNetworkLibrary_verifyServerCertificates(
40 env
, chain_byte_array
.obj(), auth_string
.obj());
42 return static_cast<CertVerifyResultAndroid
>(result
);
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());
52 void ClearTestRootCertificates() {
53 JNIEnv
* env
= AttachCurrentThread();
54 Java_AndroidNetworkLibrary_clearTestRootCertificates(env
);
57 bool StoreKeyPair(const uint8
* public_key
,
59 const uint8
* private_key
,
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";
73 void StoreCertificate(net::CertificateMimeType cert_type
,
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"
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).
89 bool HaveOnlyLoopbackAddresses() {
90 JNIEnv
* env
= AttachCurrentThread();
91 return Java_AndroidNetworkLibrary_haveOnlyLoopbackAddresses(env
);
94 std::string
GetNetworkList() {
95 JNIEnv
* env
= AttachCurrentThread();
96 ScopedJavaLocalRef
<jstring
> ret
=
97 Java_AndroidNetworkLibrary_getNetworkList(env
);
98 return ConvertJavaStringToUTF8(ret
);
101 bool GetMimeTypeFromExtension(const std::string
& extension
,
102 std::string
* result
) {
103 JNIEnv
* env
= AttachCurrentThread();
105 ScopedJavaLocalRef
<jstring
> extension_string
=
106 ConvertUTF8ToJavaString(env
, extension
);
107 ScopedJavaLocalRef
<jstring
> ret
=
108 Java_AndroidNetworkLibrary_getMimeTypeFromExtension(
109 env
, extension_string
.obj());
113 *result
= ConvertJavaStringToUTF8(ret
);
117 bool RegisterNetworkLibrary(JNIEnv
* env
) {
118 return RegisterNativesImpl(env
);
121 } // namespace android