blob: 080307138bde49ba1000d24d2944795691d857c4 [file] [log] [blame]
Kenny Root07438c82012-11-02 15:41:02 -07001/*
2**
3** Copyright 2008, The Android Open Source Project
4**
5** Licensed under the Apache License, Version 2.0 (the "License");
6** you may not use this file except in compliance with the License.
7** You may obtain a copy of the License at
8**
9** http://www.apache.org/licenses/LICENSE-2.0
10**
11** Unless required by applicable law or agreed to in writing, software
12** distributed under the License is distributed on an "AS IS" BASIS,
13** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14** See the License for the specific language governing permissions and
15** limitations under the License.
16*/
17
18#include <stdint.h>
19#include <sys/types.h>
20
21#define LOG_TAG "KeystoreService"
22#include <utils/Log.h>
23
24#include <binder/Parcel.h>
25#include <binder/IPCThreadState.h>
26#include <binder/IServiceManager.h>
27
28#include <keystore/IKeystoreService.h>
29
30namespace android {
31
32class BpKeystoreService: public BpInterface<IKeystoreService>
33{
34public:
35 BpKeystoreService(const sp<IBinder>& impl)
36 : BpInterface<IKeystoreService>(impl)
37 {
38 }
39
40 // test ping
41 virtual int32_t test()
42 {
43 Parcel data, reply;
44 data.writeInterfaceToken(IKeystoreService::getInterfaceDescriptor());
45 status_t status = remote()->transact(BnKeystoreService::TEST, data, &reply);
46 if (status != NO_ERROR) {
47 ALOGD("test() could not contact remote: %d\n", status);
48 return -1;
49 }
50 int32_t err = reply.readExceptionCode();
51 int32_t ret = reply.readInt32();
52 if (err < 0) {
53 ALOGD("test() caught exception %d\n", err);
54 return -1;
55 }
56 return ret;
57 }
58
59 virtual int32_t get(const String16& name, uint8_t** item, size_t* itemLength)
60 {
61 Parcel data, reply;
62 data.writeInterfaceToken(IKeystoreService::getInterfaceDescriptor());
63 data.writeString16(name);
64 status_t status = remote()->transact(BnKeystoreService::GET, data, &reply);
65 if (status != NO_ERROR) {
66 ALOGD("get() could not contact remote: %d\n", status);
67 return -1;
68 }
69 int32_t err = reply.readExceptionCode();
70 ssize_t len = reply.readInt32();
71 if (len >= 0 && (size_t) len <= reply.dataAvail()) {
72 size_t ulen = (size_t) len;
73 const void* buf = reply.readInplace(ulen);
74 *item = (uint8_t*) malloc(ulen);
75 if (*item != NULL) {
76 memcpy(*item, buf, ulen);
77 *itemLength = ulen;
78 } else {
79 ALOGE("out of memory allocating output array in get");
80 *itemLength = 0;
81 }
82 } else {
83 *itemLength = 0;
84 }
85 if (err < 0) {
86 ALOGD("get() caught exception %d\n", err);
87 return -1;
88 }
89 return 0;
90 }
91
Kenny Rootb88c3eb2013-02-13 14:43:43 -080092 virtual int32_t insert(const String16& name, const uint8_t* item, size_t itemLength, int uid)
Kenny Root07438c82012-11-02 15:41:02 -070093 {
94 Parcel data, reply;
95 data.writeInterfaceToken(IKeystoreService::getInterfaceDescriptor());
96 data.writeString16(name);
97 data.writeInt32(itemLength);
98 void* buf = data.writeInplace(itemLength);
99 memcpy(buf, item, itemLength);
Kenny Rootb88c3eb2013-02-13 14:43:43 -0800100 data.writeInt32(uid);
Kenny Root07438c82012-11-02 15:41:02 -0700101 status_t status = remote()->transact(BnKeystoreService::INSERT, data, &reply);
102 if (status != NO_ERROR) {
103 ALOGD("import() could not contact remote: %d\n", status);
104 return -1;
105 }
106 int32_t err = reply.readExceptionCode();
107 int32_t ret = reply.readInt32();
108 if (err < 0) {
109 ALOGD("import() caught exception %d\n", err);
110 return -1;
111 }
112 return ret;
113 }
114
Kenny Rootb88c3eb2013-02-13 14:43:43 -0800115 virtual int32_t del(const String16& name, int uid)
Kenny Root07438c82012-11-02 15:41:02 -0700116 {
117 Parcel data, reply;
118 data.writeInterfaceToken(IKeystoreService::getInterfaceDescriptor());
119 data.writeString16(name);
Kenny Rootb88c3eb2013-02-13 14:43:43 -0800120 data.writeInt32(uid);
Kenny Root07438c82012-11-02 15:41:02 -0700121 status_t status = remote()->transact(BnKeystoreService::DEL, data, &reply);
122 if (status != NO_ERROR) {
123 ALOGD("del() could not contact remote: %d\n", status);
124 return -1;
125 }
126 int32_t err = reply.readExceptionCode();
127 int32_t ret = reply.readInt32();
128 if (err < 0) {
129 ALOGD("del() caught exception %d\n", err);
130 return -1;
131 }
132 return ret;
133 }
134
Kenny Rootb88c3eb2013-02-13 14:43:43 -0800135 virtual int32_t exist(const String16& name, int uid)
Kenny Root07438c82012-11-02 15:41:02 -0700136 {
137 Parcel data, reply;
138 data.writeInterfaceToken(IKeystoreService::getInterfaceDescriptor());
139 data.writeString16(name);
Kenny Rootb88c3eb2013-02-13 14:43:43 -0800140 data.writeInt32(uid);
Kenny Root07438c82012-11-02 15:41:02 -0700141 status_t status = remote()->transact(BnKeystoreService::EXIST, data, &reply);
142 if (status != NO_ERROR) {
143 ALOGD("exist() could not contact remote: %d\n", status);
144 return -1;
145 }
146 int32_t err = reply.readExceptionCode();
147 int32_t ret = reply.readInt32();
148 if (err < 0) {
149 ALOGD("exist() caught exception %d\n", err);
150 return -1;
151 }
152 return ret;
153 }
154
Kenny Rootb88c3eb2013-02-13 14:43:43 -0800155 virtual int32_t saw(const String16& name, int uid, Vector<String16>* matches)
Kenny Root07438c82012-11-02 15:41:02 -0700156 {
157 Parcel data, reply;
158 data.writeInterfaceToken(IKeystoreService::getInterfaceDescriptor());
159 data.writeString16(name);
Kenny Rootb88c3eb2013-02-13 14:43:43 -0800160 data.writeInt32(uid);
Kenny Root07438c82012-11-02 15:41:02 -0700161 status_t status = remote()->transact(BnKeystoreService::SAW, data, &reply);
162 if (status != NO_ERROR) {
163 ALOGD("saw() could not contact remote: %d\n", status);
164 return -1;
165 }
166 int32_t err = reply.readExceptionCode();
167 int32_t numMatches = reply.readInt32();
168 for (int32_t i = 0; i < numMatches; i++) {
169 matches->push(reply.readString16());
170 }
171 int32_t ret = reply.readInt32();
172 if (err < 0) {
173 ALOGD("saw() caught exception %d\n", err);
174 return -1;
175 }
176 return ret;
177 }
178
179 virtual int32_t reset()
180 {
181 Parcel data, reply;
182 data.writeInterfaceToken(IKeystoreService::getInterfaceDescriptor());
183 status_t status = remote()->transact(BnKeystoreService::RESET, data, &reply);
184 if (status != NO_ERROR) {
185 ALOGD("reset() could not contact remote: %d\n", status);
186 return -1;
187 }
188 int32_t err = reply.readExceptionCode();
189 int32_t ret = reply.readInt32();
190 if (err < 0) {
191 ALOGD("reset() caught exception %d\n", err);
192 return -1;
193 }
194 return ret;
195 }
196
197 virtual int32_t password(const String16& password)
198 {
199 Parcel data, reply;
200 data.writeInterfaceToken(IKeystoreService::getInterfaceDescriptor());
201 data.writeString16(password);
202 status_t status = remote()->transact(BnKeystoreService::PASSWORD, data, &reply);
203 if (status != NO_ERROR) {
204 ALOGD("password() could not contact remote: %d\n", status);
205 return -1;
206 }
207 int32_t err = reply.readExceptionCode();
208 int32_t ret = reply.readInt32();
209 if (err < 0) {
210 ALOGD("password() caught exception %d\n", err);
211 return -1;
212 }
213 return ret;
214 }
215
216 virtual int32_t lock()
217 {
218 Parcel data, reply;
219 data.writeInterfaceToken(IKeystoreService::getInterfaceDescriptor());
220 status_t status = remote()->transact(BnKeystoreService::LOCK, data, &reply);
221 if (status != NO_ERROR) {
222 ALOGD("lock() could not contact remote: %d\n", status);
223 return -1;
224 }
225 int32_t err = reply.readExceptionCode();
226 int32_t ret = reply.readInt32();
227 if (err < 0) {
228 ALOGD("lock() caught exception %d\n", err);
229 return -1;
230 }
231 return ret;
232 }
233
234 virtual int32_t unlock(const String16& password)
235 {
236 Parcel data, reply;
237 data.writeInterfaceToken(IKeystoreService::getInterfaceDescriptor());
238 data.writeString16(password);
239 status_t status = remote()->transact(BnKeystoreService::UNLOCK, data, &reply);
240 if (status != NO_ERROR) {
241 ALOGD("unlock() could not contact remote: %d\n", status);
242 return -1;
243 }
244 int32_t err = reply.readExceptionCode();
245 int32_t ret = reply.readInt32();
246 if (err < 0) {
247 ALOGD("unlock() caught exception %d\n", err);
248 return -1;
249 }
250 return ret;
251 }
252
253 virtual int32_t zero()
254 {
255 Parcel data, reply;
256 data.writeInterfaceToken(IKeystoreService::getInterfaceDescriptor());
257 status_t status = remote()->transact(BnKeystoreService::ZERO, data, &reply);
258 if (status != NO_ERROR) {
259 ALOGD("zero() could not contact remote: %d\n", status);
260 return -1;
261 }
262 int32_t err = reply.readExceptionCode();
263 int32_t ret = reply.readInt32();
264 if (err < 0) {
265 ALOGD("zero() caught exception %d\n", err);
266 return -1;
267 }
268 return ret;
269 }
270
Kenny Rootb88c3eb2013-02-13 14:43:43 -0800271 virtual int32_t generate(const String16& name, int uid)
Kenny Root07438c82012-11-02 15:41:02 -0700272 {
273 Parcel data, reply;
274 data.writeInterfaceToken(IKeystoreService::getInterfaceDescriptor());
275 data.writeString16(name);
Kenny Rootb88c3eb2013-02-13 14:43:43 -0800276 data.writeInt32(uid);
Kenny Root07438c82012-11-02 15:41:02 -0700277 status_t status = remote()->transact(BnKeystoreService::GENERATE, data, &reply);
278 if (status != NO_ERROR) {
279 ALOGD("generate() could not contact remote: %d\n", status);
280 return -1;
281 }
282 int32_t err = reply.readExceptionCode();
283 int32_t ret = reply.readInt32();
284 if (err < 0) {
285 ALOGD("generate() caught exception %d\n", err);
286 return -1;
287 }
288 return ret;
289 }
290
Kenny Rootb88c3eb2013-02-13 14:43:43 -0800291 virtual int32_t import(const String16& name, const uint8_t* key, size_t keyLength, int uid)
Kenny Root07438c82012-11-02 15:41:02 -0700292 {
293 Parcel data, reply;
294 data.writeInterfaceToken(IKeystoreService::getInterfaceDescriptor());
295 data.writeString16(name);
296 data.writeInt32(keyLength);
297 void* buf = data.writeInplace(keyLength);
298 memcpy(buf, key, keyLength);
Kenny Rootb88c3eb2013-02-13 14:43:43 -0800299 data.writeInt32(uid);
Kenny Root07438c82012-11-02 15:41:02 -0700300 status_t status = remote()->transact(BnKeystoreService::IMPORT, data, &reply);
301 if (status != NO_ERROR) {
302 ALOGD("import() could not contact remote: %d\n", status);
303 return -1;
304 }
305 int32_t err = reply.readExceptionCode();
306 int32_t ret = reply.readInt32();
307 if (err < 0) {
308 ALOGD("import() caught exception %d\n", err);
309 return -1;
310 }
311 return ret;
312 }
313
314 virtual int32_t sign(const String16& name, const uint8_t* in, size_t inLength, uint8_t** out,
315 size_t* outLength)
316 {
317 Parcel data, reply;
318 data.writeInterfaceToken(IKeystoreService::getInterfaceDescriptor());
319 data.writeString16(name);
320 data.writeInt32(inLength);
321 void* buf = data.writeInplace(inLength);
322 memcpy(buf, in, inLength);
323 status_t status = remote()->transact(BnKeystoreService::SIGN, data, &reply);
324 if (status != NO_ERROR) {
325 ALOGD("import() could not contact remote: %d\n", status);
326 return -1;
327 }
328 int32_t err = reply.readExceptionCode();
329 ssize_t len = reply.readInt32();
330 if (len >= 0 && (size_t) len <= reply.dataAvail()) {
331 size_t ulen = (size_t) len;
332 const void* outBuf = reply.readInplace(ulen);
333 *out = (uint8_t*) malloc(ulen);
334 if (*out != NULL) {
335 memcpy((void*) *out, outBuf, ulen);
336 *outLength = ulen;
337 } else {
338 ALOGE("out of memory allocating output array in sign");
339 *outLength = 0;
340 }
341 } else {
342 *outLength = 0;
343 }
344 if (err < 0) {
345 ALOGD("import() caught exception %d\n", err);
346 return -1;
347 }
348 return 0;
349 }
350
351 virtual int32_t verify(const String16& name, const uint8_t* in, size_t inLength,
352 const uint8_t* signature, size_t signatureLength)
353 {
354 Parcel data, reply;
355 void* buf;
356
357 data.writeInterfaceToken(IKeystoreService::getInterfaceDescriptor());
358 data.writeString16(name);
359 data.writeInt32(inLength);
360 buf = data.writeInplace(inLength);
361 memcpy(buf, in, inLength);
362 data.writeInt32(signatureLength);
363 buf = data.writeInplace(signatureLength);
364 memcpy(buf, signature, signatureLength);
365 status_t status = remote()->transact(BnKeystoreService::VERIFY, data, &reply);
366 if (status != NO_ERROR) {
367 ALOGD("verify() could not contact remote: %d\n", status);
368 return -1;
369 }
370 int32_t err = reply.readExceptionCode();
371 int32_t ret = reply.readInt32();
372 if (err < 0) {
373 ALOGD("verify() caught exception %d\n", err);
374 return -1;
375 }
376 return ret;
377 }
378
379 virtual int32_t get_pubkey(const String16& name, uint8_t** pubkey, size_t* pubkeyLength)
380 {
381 Parcel data, reply;
382 data.writeInterfaceToken(IKeystoreService::getInterfaceDescriptor());
383 data.writeString16(name);
384 status_t status = remote()->transact(BnKeystoreService::GET_PUBKEY, data, &reply);
385 if (status != NO_ERROR) {
386 ALOGD("get_pubkey() could not contact remote: %d\n", status);
387 return -1;
388 }
389 int32_t err = reply.readExceptionCode();
390 ssize_t len = reply.readInt32();
391 if (len >= 0 && (size_t) len <= reply.dataAvail()) {
392 size_t ulen = (size_t) len;
393 const void* buf = reply.readInplace(ulen);
394 *pubkey = (uint8_t*) malloc(ulen);
395 if (*pubkey != NULL) {
396 memcpy(*pubkey, buf, ulen);
397 *pubkeyLength = ulen;
398 } else {
399 ALOGE("out of memory allocating output array in get_pubkey");
400 *pubkeyLength = 0;
401 }
402 } else {
403 *pubkeyLength = 0;
404 }
405 if (err < 0) {
406 ALOGD("get_pubkey() caught exception %d\n", err);
407 return -1;
408 }
409 return 0;
410 }
411
Kenny Rootb88c3eb2013-02-13 14:43:43 -0800412 virtual int32_t del_key(const String16& name, int uid)
Kenny Root07438c82012-11-02 15:41:02 -0700413 {
414 Parcel data, reply;
415 data.writeInterfaceToken(IKeystoreService::getInterfaceDescriptor());
416 data.writeString16(name);
Kenny Rootb88c3eb2013-02-13 14:43:43 -0800417 data.writeInt32(uid);
Kenny Root07438c82012-11-02 15:41:02 -0700418 status_t status = remote()->transact(BnKeystoreService::DEL_KEY, data, &reply);
419 if (status != NO_ERROR) {
420 ALOGD("del_key() could not contact remote: %d\n", status);
421 return -1;
422 }
423 int32_t err = reply.readExceptionCode();
424 int32_t ret = reply.readInt32();
425 if (err < 0) {
426 ALOGD("del_key() caught exception %d\n", err);
427 return -1;
428 }
429 return ret;
430 }
431
432 virtual int32_t grant(const String16& name, int32_t granteeUid)
433 {
434 Parcel data, reply;
435 data.writeInterfaceToken(IKeystoreService::getInterfaceDescriptor());
436 data.writeString16(name);
437 data.writeInt32(granteeUid);
438 status_t status = remote()->transact(BnKeystoreService::GRANT, data, &reply);
439 if (status != NO_ERROR) {
440 ALOGD("grant() could not contact remote: %d\n", status);
441 return -1;
442 }
443 int32_t err = reply.readExceptionCode();
444 int32_t ret = reply.readInt32();
445 if (err < 0) {
446 ALOGD("grant() caught exception %d\n", err);
447 return -1;
448 }
449 return ret;
450 }
451
452 virtual int32_t ungrant(const String16& name, int32_t granteeUid)
453 {
454 Parcel data, reply;
455 data.writeInterfaceToken(IKeystoreService::getInterfaceDescriptor());
456 data.writeString16(name);
457 data.writeInt32(granteeUid);
458 status_t status = remote()->transact(BnKeystoreService::UNGRANT, data, &reply);
459 if (status != NO_ERROR) {
460 ALOGD("ungrant() could not contact remote: %d\n", status);
461 return -1;
462 }
463 int32_t err = reply.readExceptionCode();
464 int32_t ret = reply.readInt32();
465 if (err < 0) {
466 ALOGD("ungrant() caught exception %d\n", err);
467 return -1;
468 }
469 return ret;
470 }
471
472 int64_t getmtime(const String16& name)
473 {
474 Parcel data, reply;
475 data.writeInterfaceToken(IKeystoreService::getInterfaceDescriptor());
476 data.writeString16(name);
477 status_t status = remote()->transact(BnKeystoreService::GETMTIME, data, &reply);
478 if (status != NO_ERROR) {
479 ALOGD("getmtime() could not contact remote: %d\n", status);
480 return -1;
481 }
482 int32_t err = reply.readExceptionCode();
483 int64_t ret = reply.readInt64();
484 if (err < 0) {
485 ALOGD("getmtime() caught exception %d\n", err);
486 return -1;
487 }
488 return ret;
489 }
Kenny Root02254072013-03-20 11:48:19 -0700490
Kenny Rootd53bc922013-03-21 14:10:15 -0700491 virtual int32_t duplicate(const String16& srcKey, int32_t srcUid, const String16& destKey,
492 int32_t destUid)
Kenny Root02254072013-03-20 11:48:19 -0700493 {
494 Parcel data, reply;
495 data.writeInterfaceToken(IKeystoreService::getInterfaceDescriptor());
Kenny Rootd53bc922013-03-21 14:10:15 -0700496 data.writeString16(srcKey);
497 data.writeInt32(srcUid);
498 data.writeString16(destKey);
499 data.writeInt32(destUid);
500 status_t status = remote()->transact(BnKeystoreService::DUPLICATE, data, &reply);
Kenny Root02254072013-03-20 11:48:19 -0700501 if (status != NO_ERROR) {
Kenny Rootd53bc922013-03-21 14:10:15 -0700502 ALOGD("duplicate() could not contact remote: %d\n", status);
Kenny Root02254072013-03-20 11:48:19 -0700503 return -1;
504 }
505 int32_t err = reply.readExceptionCode();
506 int32_t ret = reply.readInt32();
507 if (err < 0) {
Kenny Rootd53bc922013-03-21 14:10:15 -0700508 ALOGD("duplicate() caught exception %d\n", err);
Kenny Root02254072013-03-20 11:48:19 -0700509 return -1;
510 }
511 return ret;
512 }
Kenny Root07438c82012-11-02 15:41:02 -0700513};
514
515IMPLEMENT_META_INTERFACE(KeystoreService, "android.security.keystore");
516
517// ----------------------------------------------------------------------
518
519status_t BnKeystoreService::onTransact(
520 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
521{
522 switch(code) {
523 case TEST: {
524 CHECK_INTERFACE(IKeystoreService, data, reply);
525 int32_t ret = test();
526 reply->writeNoException();
527 reply->writeInt32(ret);
528 return NO_ERROR;
529 } break;
530 case GET: {
531 CHECK_INTERFACE(IKeystoreService, data, reply);
532 String16 name = data.readString16();
533 void* out = NULL;
534 size_t outSize = 0;
535 int32_t ret = get(name, (uint8_t**) &out, &outSize);
536 reply->writeNoException();
537 if (ret == 1) {
538 reply->writeInt32(outSize);
539 void* buf = reply->writeInplace(outSize);
540 memcpy(buf, out, outSize);
541 free(out);
542 } else {
543 reply->writeInt32(-1);
544 }
545 return NO_ERROR;
546 } break;
547 case INSERT: {
548 CHECK_INTERFACE(IKeystoreService, data, reply);
549 String16 name = data.readString16();
550 ssize_t inSize = data.readInt32();
551 const void* in;
552 if (inSize >= 0 && (size_t) inSize <= data.dataAvail()) {
553 in = data.readInplace(inSize);
554 } else {
555 in = NULL;
556 inSize = 0;
557 }
Kenny Rootb88c3eb2013-02-13 14:43:43 -0800558 int uid = data.readInt32();
559 int32_t ret = insert(name, (const uint8_t*) in, (size_t) inSize, uid);
Kenny Root07438c82012-11-02 15:41:02 -0700560 reply->writeNoException();
561 reply->writeInt32(ret);
562 return NO_ERROR;
563 } break;
564 case DEL: {
565 CHECK_INTERFACE(IKeystoreService, data, reply);
566 String16 name = data.readString16();
Kenny Rootb88c3eb2013-02-13 14:43:43 -0800567 int uid = data.readInt32();
568 int32_t ret = del(name, uid);
Kenny Root07438c82012-11-02 15:41:02 -0700569 reply->writeNoException();
570 reply->writeInt32(ret);
571 return NO_ERROR;
572 } break;
573 case EXIST: {
574 CHECK_INTERFACE(IKeystoreService, data, reply);
575 String16 name = data.readString16();
Kenny Rootb88c3eb2013-02-13 14:43:43 -0800576 int uid = data.readInt32();
577 int32_t ret = exist(name, uid);
Kenny Root07438c82012-11-02 15:41:02 -0700578 reply->writeNoException();
579 reply->writeInt32(ret);
580 return NO_ERROR;
581 } break;
582 case SAW: {
583 CHECK_INTERFACE(IKeystoreService, data, reply);
584 String16 name = data.readString16();
Kenny Rootb88c3eb2013-02-13 14:43:43 -0800585 int uid = data.readInt32();
Kenny Root07438c82012-11-02 15:41:02 -0700586 Vector<String16> matches;
Kenny Rootb88c3eb2013-02-13 14:43:43 -0800587 int32_t ret = saw(name, uid, &matches);
Kenny Root07438c82012-11-02 15:41:02 -0700588 reply->writeNoException();
589 reply->writeInt32(matches.size());
590 Vector<String16>::const_iterator it = matches.begin();
591 for (; it != matches.end(); ++it) {
592 reply->writeString16(*it);
593 }
594 reply->writeInt32(ret);
595 return NO_ERROR;
596 } break;
597 case RESET: {
598 CHECK_INTERFACE(IKeystoreService, data, reply);
599 int32_t ret = reset();
600 reply->writeNoException();
601 reply->writeInt32(ret);
602 return NO_ERROR;
603 } break;
604 case PASSWORD: {
605 CHECK_INTERFACE(IKeystoreService, data, reply);
606 String16 pass = data.readString16();
607 int32_t ret = password(pass);
608 reply->writeNoException();
609 reply->writeInt32(ret);
610 return NO_ERROR;
611 } break;
612 case LOCK: {
613 CHECK_INTERFACE(IKeystoreService, data, reply);
614 int32_t ret = lock();
615 reply->writeNoException();
616 reply->writeInt32(ret);
617 return NO_ERROR;
618 } break;
619 case UNLOCK: {
620 CHECK_INTERFACE(IKeystoreService, data, reply);
621 String16 pass = data.readString16();
622 int32_t ret = unlock(pass);
623 reply->writeNoException();
624 reply->writeInt32(ret);
625 return NO_ERROR;
626 } break;
627 case ZERO: {
628 CHECK_INTERFACE(IKeystoreService, data, reply);
629 int32_t ret = zero();
630 reply->writeNoException();
631 reply->writeInt32(ret);
632 return NO_ERROR;
633 } break;
634 case GENERATE: {
635 CHECK_INTERFACE(IKeystoreService, data, reply);
636 String16 name = data.readString16();
Kenny Rootb88c3eb2013-02-13 14:43:43 -0800637 int uid = data.readInt32();
638 int32_t ret = generate(name, uid);
Kenny Root07438c82012-11-02 15:41:02 -0700639 reply->writeNoException();
640 reply->writeInt32(ret);
641 return NO_ERROR;
642 } break;
643 case IMPORT: {
644 CHECK_INTERFACE(IKeystoreService, data, reply);
645 String16 name = data.readString16();
646 ssize_t inSize = data.readInt32();
647 const void* in;
648 if (inSize >= 0 && (size_t) inSize <= data.dataAvail()) {
649 in = data.readInplace(inSize);
650 } else {
651 in = NULL;
652 inSize = 0;
653 }
Kenny Rootb88c3eb2013-02-13 14:43:43 -0800654 int uid = data.readInt32();
655 int32_t ret = import(name, (const uint8_t*) in, (size_t) inSize, uid);
Kenny Root07438c82012-11-02 15:41:02 -0700656 reply->writeNoException();
657 reply->writeInt32(ret);
658 return NO_ERROR;
659 } break;
660 case SIGN: {
661 CHECK_INTERFACE(IKeystoreService, data, reply);
662 String16 name = data.readString16();
663 ssize_t inSize = data.readInt32();
664 const void* in;
665 if (inSize >= 0 && (size_t) inSize <= data.dataAvail()) {
666 in = data.readInplace(inSize);
667 } else {
668 in = NULL;
669 inSize = 0;
670 }
671 void* out = NULL;
672 size_t outSize = 0;
673 int32_t ret = sign(name, (const uint8_t*) in, (size_t) inSize, (uint8_t**) &out, &outSize);
674 reply->writeNoException();
Kenny Rootb03c9fb2013-02-04 16:42:51 -0800675 if (outSize > 0 && out != NULL) {
676 reply->writeInt32(outSize);
677 void* buf = reply->writeInplace(outSize);
678 memcpy(buf, out, outSize);
679 free(out);
680 } else {
Kenny Roote289c402013-02-14 11:31:53 -0800681 reply->writeInt32(-1);
Kenny Rootb03c9fb2013-02-04 16:42:51 -0800682 }
Kenny Root07438c82012-11-02 15:41:02 -0700683 reply->writeInt32(ret);
684 return NO_ERROR;
685 } break;
686 case VERIFY: {
687 CHECK_INTERFACE(IKeystoreService, data, reply);
688 String16 name = data.readString16();
689 ssize_t inSize = data.readInt32();
690 const void* in;
691 if (inSize >= 0 && (size_t) inSize <= data.dataAvail()) {
692 in = data.readInplace(inSize);
693 } else {
694 in = NULL;
695 inSize = 0;
696 }
697 ssize_t sigSize = data.readInt32();
698 const void* sig;
699 if (sigSize >= 0 && (size_t) sigSize <= data.dataAvail()) {
700 sig = data.readInplace(sigSize);
701 } else {
702 sig = NULL;
703 sigSize = 0;
704 }
705 bool ret = verify(name, (const uint8_t*) in, (size_t) inSize, (const uint8_t*) sig,
706 (size_t) sigSize);
707 reply->writeNoException();
708 reply->writeInt32(ret ? 1 : 0);
709 return NO_ERROR;
710 } break;
711 case GET_PUBKEY: {
712 CHECK_INTERFACE(IKeystoreService, data, reply);
713 String16 name = data.readString16();
714 void* out = NULL;
715 size_t outSize = 0;
716 int32_t ret = get_pubkey(name, (unsigned char**) &out, &outSize);
717 reply->writeNoException();
Kenny Rootb03c9fb2013-02-04 16:42:51 -0800718 if (outSize > 0 && out != NULL) {
719 reply->writeInt32(outSize);
720 void* buf = reply->writeInplace(outSize);
721 memcpy(buf, out, outSize);
722 free(out);
723 } else {
Kenny Roote289c402013-02-14 11:31:53 -0800724 reply->writeInt32(-1);
Kenny Rootb03c9fb2013-02-04 16:42:51 -0800725 }
Kenny Root07438c82012-11-02 15:41:02 -0700726 reply->writeInt32(ret);
727 return NO_ERROR;
Kenny Rootb03c9fb2013-02-04 16:42:51 -0800728 } break;
Kenny Root07438c82012-11-02 15:41:02 -0700729 case DEL_KEY: {
730 CHECK_INTERFACE(IKeystoreService, data, reply);
731 String16 name = data.readString16();
Kenny Rootb88c3eb2013-02-13 14:43:43 -0800732 int uid = data.readInt32();
733 int32_t ret = del_key(name, uid);
Kenny Root07438c82012-11-02 15:41:02 -0700734 reply->writeNoException();
735 reply->writeInt32(ret);
736 return NO_ERROR;
737 } break;
738 case GRANT: {
739 CHECK_INTERFACE(IKeystoreService, data, reply);
740 String16 name = data.readString16();
741 int32_t granteeUid = data.readInt32();
742 int32_t ret = grant(name, granteeUid);
743 reply->writeNoException();
744 reply->writeInt32(ret);
745 return NO_ERROR;
746 } break;
747 case UNGRANT: {
748 CHECK_INTERFACE(IKeystoreService, data, reply);
749 String16 name = data.readString16();
750 int32_t granteeUid = data.readInt32();
751 int32_t ret = ungrant(name, granteeUid);
752 reply->writeNoException();
753 reply->writeInt32(ret);
754 return NO_ERROR;
755 } break;
756 case GETMTIME: {
757 CHECK_INTERFACE(IKeystoreService, data, reply);
758 String16 name = data.readString16();
759 int64_t ret = getmtime(name);
760 reply->writeNoException();
761 reply->writeInt64(ret);
762 return NO_ERROR;
763 } break;
Kenny Rootd53bc922013-03-21 14:10:15 -0700764 case DUPLICATE: {
Kenny Root02254072013-03-20 11:48:19 -0700765 CHECK_INTERFACE(IKeystoreService, data, reply);
Kenny Rootd53bc922013-03-21 14:10:15 -0700766 String16 srcKey = data.readString16();
767 int32_t srcUid = data.readInt32();
768 String16 destKey = data.readString16();
769 int32_t destUid = data.readInt32();
770 int32_t ret = duplicate(srcKey, srcUid, destKey, destUid);
Kenny Root02254072013-03-20 11:48:19 -0700771 reply->writeNoException();
772 reply->writeInt32(ret);
773 return NO_ERROR;
774 } break;
Kenny Root07438c82012-11-02 15:41:02 -0700775 default:
776 return BBinder::onTransact(code, data, reply, flags);
777 }
778}
779
780// ----------------------------------------------------------------------------
781
782}; // namespace android