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
14using base::android::AttachCurrentThread;
15using base::android::ConvertJavaStringToUTF8;
16using base::android::ConvertUTF8ToJavaString;
17using base::android::GetApplicationContext;
18using base::android::ScopedJavaLocalRef;
19using base::android::ToJavaArrayOfByteArray;
20using base::android::ToJavaByteArray;
21
22namespace net {
23namespace android {
24
25void 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();
32
33  ScopedJavaLocalRef<jobjectArray> chain_byte_array =
34      ToJavaArrayOfByteArray(env, cert_chain);
35  DCHECK(!chain_byte_array.is_null());
36
37  ScopedJavaLocalRef<jstring> auth_string =
38      ConvertUTF8ToJavaString(env, auth_type);
39  DCHECK(!auth_string.is_null());
40
41  ScopedJavaLocalRef<jstring> host_string =
42      ConvertUTF8ToJavaString(env, host);
43  DCHECK(!host_string.is_null());
44
45  ScopedJavaLocalRef<jobject> result =
46      Java_AndroidNetworkLibrary_verifyServerCertificates(
47          env, chain_byte_array.obj(), auth_string.obj(), host_string.obj());
48
49  ExtractCertVerifyResult(result.obj(),
50                          status, is_issued_by_known_root, verified_chain);
51}
52
53void AddTestRootCertificate(const uint8* 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());
58}
59
60void ClearTestRootCertificates() {
61  JNIEnv* env = AttachCurrentThread();
62  Java_AndroidNetworkLibrary_clearTestRootCertificates(env);
63}
64
65bool StoreKeyPair(const uint8* public_key,
66                  size_t public_len,
67                  const uint8* private_key,
68                  size_t private_len) {
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";
78  return ret;
79}
80
81void StoreCertificate(net::CertificateMimeType cert_type,
82                      const void* data,
83                      size_t data_len) {
84  JNIEnv* env = AttachCurrentThread();
85  ScopedJavaLocalRef<jbyteArray> data_array =
86      ToJavaByteArray(env, reinterpret_cast<const uint8*>(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"
91      " failed";
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).
95}
96
97bool HaveOnlyLoopbackAddresses() {
98  JNIEnv* env = AttachCurrentThread();
99  return Java_AndroidNetworkLibrary_haveOnlyLoopbackAddresses(env);
100}
101
102std::string GetNetworkList() {
103  JNIEnv* env = AttachCurrentThread();
104  ScopedJavaLocalRef<jstring> ret =
105      Java_AndroidNetworkLibrary_getNetworkList(env);
106  return ConvertJavaStringToUTF8(ret);
107}
108
109bool GetMimeTypeFromExtension(const std::string& extension,
110                              std::string* result) {
111  JNIEnv* env = AttachCurrentThread();
112
113  ScopedJavaLocalRef<jstring> extension_string =
114      ConvertUTF8ToJavaString(env, extension);
115  ScopedJavaLocalRef<jstring> ret =
116      Java_AndroidNetworkLibrary_getMimeTypeFromExtension(
117          env, extension_string.obj());
118
119  if (!ret.obj())
120    return false;
121  *result = ConvertJavaStringToUTF8(ret);
122  return true;
123}
124
125std::string GetTelephonyNetworkCountryIso() {
126  return base::android::ConvertJavaStringToUTF8(
127      Java_AndroidNetworkLibrary_getNetworkCountryIso(
128          base::android::AttachCurrentThread(),
129          base::android::GetApplicationContext()));
130}
131
132std::string GetTelephonyNetworkOperator() {
133  return base::android::ConvertJavaStringToUTF8(
134      Java_AndroidNetworkLibrary_getNetworkOperator(
135          base::android::AttachCurrentThread(),
136          base::android::GetApplicationContext()));
137}
138
139bool RegisterNetworkLibrary(JNIEnv* env) {
140  return RegisterNativesImpl(env);
141}
142
143}  // namespace android
144}  // namespace net
145