Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(148)

Side by Side Diff: net/android/keystore.cc

Issue 11571059: Add net/android/keystore.h (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: new version with simpler signing support Created 7 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
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/keystore.h"
6
7 #include <vector>
8
9 #include "base/android/jni_android.h"
10 #include "base/android/jni_array.h"
11 #include "base/android/jni_string.h"
12 #include "base/logging.h"
13
14 #include "jni/AndroidKeyStore_jni.h"
15
16 using base::android::AttachCurrentThread;
17 using base::android::ConvertUTF8ToJavaString;
18 using base::android::ConvertJavaStringToUTF8;
19 using base::android::GetApplicationContext;
20 using base::android::HasException;
21 using base::android::JavaByteArrayToByteVector;
22 using base::android::ScopedJavaLocalRef;
23 using base::android::ToJavaByteArray;
24 using base::android::ToJavaArrayOfByteArray;
25 using base::android::ToJavaArrayOfStrings;
26 using base::android::JavaArrayOfByteArrayToStringVector;
27
28 namespace net {
29 namespace android {
30
31 namespace {
32
33 typedef std::vector<ClientCertRequest*> ClientCertRequestList;
34
35 // Global list of active client certificate requests.
36 //
37 // Note: There is no locking because all functions related to
38 // ClientCertRequestList should run on the UI thread.
39 ClientCertRequestList g_client_cert_requests;
40
41 // Return a new unique request id. Can't be 0.
42 int GetNewRequestId() {
43 static int s_last_id;
44
45 int id = ++s_last_id;
46 if (id == INT_MAX)
47 s_last_id = 0;
48 DCHECK(id != 0);
49 return id;
50 }
51
52 } // namespace
53
54 void OnRequestCompletion(
55 JNIEnv* env,
56 jclass /* clazz */,
57 jint request_id,
58 jstring private_key_alias_ref,
59 jobjectArray encoded_chain_ref,
60 jobject private_key_ref) {
61 // Find the certificate request in the active list.
62 // If none found, it was cancelled so return immediately.
63 ClientCertRequest* request = NULL;
64 for (ClientCertRequestList::iterator it = g_client_cert_requests.begin();
65 it != g_client_cert_requests.end(); ++it) {
66 if ((*it)->request_id() == request_id) {
67 // Found it, remove it from the active list.
68 request = (*it);
69 g_client_cert_requests.erase(it);
70 break;
71 }
72 }
73
74 if (!request)
75 return;
76
77 // Convert Java object to their C++ counterparts
78 std::string private_key_alias;
79
80 if (private_key_alias_ref) {
81 private_key_alias = ConvertJavaStringToUTF8(
82 env, private_key_alias_ref);
83 }
84
85 // Convert the encoded chain to a vector of strings.
86 std::vector<std::string> encoded_chain;
87 JavaArrayOfByteArrayToStringVector(
88 env, encoded_chain_ref, &encoded_chain);
89
90 // Call the delegate.
91 request->OnCertificateSelection(private_key_alias,
92 encoded_chain,
93 private_key_ref);
94 }
95
96 ClientCertRequest::~ClientCertRequest() {
97 if (request_id_ != 0)
98 Cancel();
99 }
100
101 bool ClientCertRequest::Start(
102 const std::vector<std::string>& key_types,
103 const std::vector<std::string>& principals,
104 const std::string& host_name,
105 int port) {
106
107 // Convert parameters to equivalent Java objects.
108 JNIEnv* env = AttachCurrentThread();
109
110 ScopedJavaLocalRef<jobjectArray> key_types_ref =
111 ToJavaArrayOfStrings(env, key_types);
112 if (key_types_ref.is_null())
113 return false;
114
115 ScopedJavaLocalRef<jobjectArray> principals_ref =
116 ToJavaArrayOfByteArray(env, principals);
117 if (principals_ref.is_null())
118 return false;
119
120 ScopedJavaLocalRef<jstring> host_name_ref =
121 ConvertUTF8ToJavaString(env, host_name);
122 if (host_name_ref.is_null())
123 return false;
124
125 // Add to global list of active requests.
126 request_id_ = GetNewRequestId();
127 g_client_cert_requests.push_back(this);
128
129 // Invoke platform API.
130 return Java_AndroidKeyStore_selectClientCertificate(
131 env, request_id_, key_types_ref.obj(), principals_ref.obj(),
132 host_name_ref.obj(), port, NULL);
133 }
134
135 void ClientCertRequest::Cancel() {
136 if (request_id_ == 0)
137 return;
138
139 ClientCertRequestList::iterator it = std::find(
140 g_client_cert_requests.begin(),
141 g_client_cert_requests.end(),
142 this);
143
144 if (it != g_client_cert_requests.end()) {
145 (*it)->request_id_ = 0;
146 g_client_cert_requests.erase(it);
147 }
148 }
149
150 bool SignWithPrivateKey(
151 jobject private_key_ref,
152 const base::StringPiece& message,
153 std::vector<uint8>* signature) {
154 JNIEnv* env = AttachCurrentThread();
155
156 // Convert message to byte[] array.
157 ScopedJavaLocalRef<jbyteArray> message_ref =
158 ToJavaByteArray(env,
159 reinterpret_cast<const uint8*>(message.data()),
160 message.length());
161 DCHECK(!message_ref.is_null());
162
163 // Invoke platform API
164 ScopedJavaLocalRef<jbyteArray> signature_ref =
165 Java_AndroidKeyStore_signWithPrivateKey(
166 env, private_key_ref, message_ref.obj());
167 if (HasException(env) || signature_ref.is_null())
168 return false;
169
170 // Write signature to string.
171 JavaByteArrayToByteVector(env, signature_ref.obj(), signature);
172 return true;
173 }
174
175 bool RegisterKeyStore(JNIEnv* env) {
176 return RegisterNativesImpl(env);
177 }
178
179 } // namespace android
180 } // namespace net
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698