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