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 void VerifyX509CertChain(const std::vector
<std::string
>& cert_chain
,
27 const std::string
& auth_type
,
28 const std::string
& host
,
29 CertVerifyStatusAndroid
* status
,
30 bool* is_issued_by_known_root
,
31 std::vector
<std::string
>* verified_chain
) {
32 JNIEnv
* env
= AttachCurrentThread();
34 ScopedJavaLocalRef
<jobjectArray
> chain_byte_array
=
35 ToJavaArrayOfByteArray(env
, cert_chain
);
36 DCHECK(!chain_byte_array
.is_null());
38 ScopedJavaLocalRef
<jstring
> auth_string
=
39 ConvertUTF8ToJavaString(env
, auth_type
);
40 DCHECK(!auth_string
.is_null());
42 ScopedJavaLocalRef
<jstring
> host_string
=
43 ConvertUTF8ToJavaString(env
, host
);
44 DCHECK(!host_string
.is_null());
46 ScopedJavaLocalRef
<jobject
> result
=
47 Java_AndroidNetworkLibrary_verifyServerCertificates(
48 env
, chain_byte_array
.obj(), auth_string
.obj(), host_string
.obj());
49 if (ClearException(env
)) {
50 *status
= android::VERIFY_FAILED
;
54 ExtractCertVerifyResult(result
.obj(),
55 status
, is_issued_by_known_root
, verified_chain
);
58 void AddTestRootCertificate(const uint8
* cert
, size_t len
) {
59 JNIEnv
* env
= AttachCurrentThread();
60 ScopedJavaLocalRef
<jbyteArray
> cert_array
= ToJavaByteArray(env
, cert
, len
);
61 DCHECK(!cert_array
.is_null());
62 Java_AndroidNetworkLibrary_addTestRootCertificate(env
, cert_array
.obj());
65 void ClearTestRootCertificates() {
66 JNIEnv
* env
= AttachCurrentThread();
67 Java_AndroidNetworkLibrary_clearTestRootCertificates(env
);
70 bool StoreKeyPair(const uint8
* public_key
,
72 const uint8
* private_key
,
74 JNIEnv
* env
= AttachCurrentThread();
75 ScopedJavaLocalRef
<jbyteArray
> public_array
=
76 ToJavaByteArray(env
, public_key
, public_len
);
77 ScopedJavaLocalRef
<jbyteArray
> private_array
=
78 ToJavaByteArray(env
, private_key
, private_len
);
79 jboolean ret
= Java_AndroidNetworkLibrary_storeKeyPair(env
,
80 GetApplicationContext(), public_array
.obj(), private_array
.obj());
81 LOG_IF(WARNING
, !ret
) <<
82 "Call to Java_AndroidNetworkLibrary_storeKeyPair failed";
86 void StoreCertificate(net::CertificateMimeType cert_type
,
89 JNIEnv
* env
= AttachCurrentThread();
90 ScopedJavaLocalRef
<jbyteArray
> data_array
=
91 ToJavaByteArray(env
, reinterpret_cast<const uint8
*>(data
), data_len
);
92 jboolean ret
= Java_AndroidNetworkLibrary_storeCertificate(env
,
93 GetApplicationContext(), cert_type
, data_array
.obj());
94 LOG_IF(WARNING
, !ret
) <<
95 "Call to Java_AndroidNetworkLibrary_storeCertificate"
97 // Intentionally do not return 'ret', there is little the caller can
98 // do in case of failure (the CertInstaller itself will deal with
99 // incorrect data and display the appropriate toast).
102 bool HaveOnlyLoopbackAddresses() {
103 JNIEnv
* env
= AttachCurrentThread();
104 return Java_AndroidNetworkLibrary_haveOnlyLoopbackAddresses(env
);
107 std::string
GetNetworkList() {
108 JNIEnv
* env
= AttachCurrentThread();
109 ScopedJavaLocalRef
<jstring
> ret
=
110 Java_AndroidNetworkLibrary_getNetworkList(env
);
111 return ConvertJavaStringToUTF8(ret
);
114 bool GetMimeTypeFromExtension(const std::string
& extension
,
115 std::string
* result
) {
116 JNIEnv
* env
= AttachCurrentThread();
118 ScopedJavaLocalRef
<jstring
> extension_string
=
119 ConvertUTF8ToJavaString(env
, extension
);
120 ScopedJavaLocalRef
<jstring
> ret
=
121 Java_AndroidNetworkLibrary_getMimeTypeFromExtension(
122 env
, extension_string
.obj());
126 *result
= ConvertJavaStringToUTF8(ret
);
130 bool RegisterNetworkLibrary(JNIEnv
* env
) {
131 return RegisterNativesImpl(env
);
134 } // namespace android