IKeystoreService.cpp revision 2ecc7a1efbb21d86d38b9e0348dfbf0e1213d920
107438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root/*
207438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root**
307438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root** Copyright 2008, The Android Open Source Project
407438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root**
507438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root** Licensed under the Apache License, Version 2.0 (the "License");
607438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root** you may not use this file except in compliance with the License.
707438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root** You may obtain a copy of the License at
807438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root**
907438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root**     http://www.apache.org/licenses/LICENSE-2.0
1007438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root**
1107438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root** Unless required by applicable law or agreed to in writing, software
1207438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root** distributed under the License is distributed on an "AS IS" BASIS,
1307438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1407438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root** See the License for the specific language governing permissions and
1507438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root** limitations under the License.
1607438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root*/
1707438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root
1807438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root#include <stdint.h>
1907438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root#include <sys/types.h>
2007438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root
2107438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root#define LOG_TAG "KeystoreService"
2207438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root#include <utils/Log.h>
2307438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root
2407438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root#include <binder/Parcel.h>
2507438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root#include <binder/IPCThreadState.h>
2607438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root#include <binder/IServiceManager.h>
2707438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root
2807438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root#include <keystore/IKeystoreService.h>
2907438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root
3007438c8d7256d3788dac323b4d0055f201e0bec9Kenny Rootnamespace android {
3107438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root
3207438c8d7256d3788dac323b4d0055f201e0bec9Kenny Rootclass BpKeystoreService: public BpInterface<IKeystoreService>
3307438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root{
3407438c8d7256d3788dac323b4d0055f201e0bec9Kenny Rootpublic:
3507438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root    BpKeystoreService(const sp<IBinder>& impl)
3607438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        : BpInterface<IKeystoreService>(impl)
3707438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root    {
3807438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root    }
3907438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root
4007438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root    // test ping
4107438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root    virtual int32_t test()
4207438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root    {
4307438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        Parcel data, reply;
4407438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        data.writeInterfaceToken(IKeystoreService::getInterfaceDescriptor());
4507438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        status_t status = remote()->transact(BnKeystoreService::TEST, data, &reply);
4607438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        if (status != NO_ERROR) {
4707438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            ALOGD("test() could not contact remote: %d\n", status);
4807438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            return -1;
4907438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        }
5007438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        int32_t err = reply.readExceptionCode();
5107438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        int32_t ret = reply.readInt32();
5207438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        if (err < 0) {
5307438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            ALOGD("test() caught exception %d\n", err);
5407438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            return -1;
5507438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        }
5607438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        return ret;
5707438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root    }
5807438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root
5907438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root    virtual int32_t get(const String16& name, uint8_t** item, size_t* itemLength)
6007438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root    {
6107438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        Parcel data, reply;
6207438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        data.writeInterfaceToken(IKeystoreService::getInterfaceDescriptor());
6307438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        data.writeString16(name);
6407438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        status_t status = remote()->transact(BnKeystoreService::GET, data, &reply);
6507438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        if (status != NO_ERROR) {
6607438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            ALOGD("get() could not contact remote: %d\n", status);
6707438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            return -1;
6807438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        }
6907438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        int32_t err = reply.readExceptionCode();
7007438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        ssize_t len = reply.readInt32();
7107438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        if (len >= 0 && (size_t) len <= reply.dataAvail()) {
7207438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            size_t ulen = (size_t) len;
7307438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            const void* buf = reply.readInplace(ulen);
7407438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            *item = (uint8_t*) malloc(ulen);
7507438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            if (*item != NULL) {
7607438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root                memcpy(*item, buf, ulen);
7707438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root                *itemLength = ulen;
7807438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            } else {
7907438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root                ALOGE("out of memory allocating output array in get");
8007438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root                *itemLength = 0;
8107438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            }
8207438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        } else {
8307438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            *itemLength = 0;
8407438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        }
8507438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        if (err < 0) {
8607438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            ALOGD("get() caught exception %d\n", err);
8707438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            return -1;
8807438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        }
8907438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        return 0;
9007438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root    }
9107438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root
92b88c3eb96625513df4cc998d739d17266ebaf89fKenny Root    virtual int32_t insert(const String16& name, const uint8_t* item, size_t itemLength, int uid)
9307438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root    {
9407438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        Parcel data, reply;
9507438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        data.writeInterfaceToken(IKeystoreService::getInterfaceDescriptor());
9607438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        data.writeString16(name);
9707438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        data.writeInt32(itemLength);
9807438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        void* buf = data.writeInplace(itemLength);
9907438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        memcpy(buf, item, itemLength);
100b88c3eb96625513df4cc998d739d17266ebaf89fKenny Root        data.writeInt32(uid);
10107438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        status_t status = remote()->transact(BnKeystoreService::INSERT, data, &reply);
10207438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        if (status != NO_ERROR) {
10307438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            ALOGD("import() could not contact remote: %d\n", status);
10407438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            return -1;
10507438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        }
10607438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        int32_t err = reply.readExceptionCode();
10707438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        int32_t ret = reply.readInt32();
10807438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        if (err < 0) {
10907438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            ALOGD("import() caught exception %d\n", err);
11007438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            return -1;
11107438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        }
11207438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        return ret;
11307438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root    }
11407438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root
115b88c3eb96625513df4cc998d739d17266ebaf89fKenny Root    virtual int32_t del(const String16& name, int uid)
11607438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root    {
11707438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        Parcel data, reply;
11807438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        data.writeInterfaceToken(IKeystoreService::getInterfaceDescriptor());
11907438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        data.writeString16(name);
120b88c3eb96625513df4cc998d739d17266ebaf89fKenny Root        data.writeInt32(uid);
12107438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        status_t status = remote()->transact(BnKeystoreService::DEL, data, &reply);
12207438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        if (status != NO_ERROR) {
12307438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            ALOGD("del() could not contact remote: %d\n", status);
12407438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            return -1;
12507438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        }
12607438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        int32_t err = reply.readExceptionCode();
12707438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        int32_t ret = reply.readInt32();
12807438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        if (err < 0) {
12907438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            ALOGD("del() caught exception %d\n", err);
13007438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            return -1;
13107438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        }
13207438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        return ret;
13307438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root    }
13407438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root
135b88c3eb96625513df4cc998d739d17266ebaf89fKenny Root    virtual int32_t exist(const String16& name, int uid)
13607438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root    {
13707438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        Parcel data, reply;
13807438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        data.writeInterfaceToken(IKeystoreService::getInterfaceDescriptor());
13907438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        data.writeString16(name);
140b88c3eb96625513df4cc998d739d17266ebaf89fKenny Root        data.writeInt32(uid);
14107438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        status_t status = remote()->transact(BnKeystoreService::EXIST, data, &reply);
14207438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        if (status != NO_ERROR) {
14307438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            ALOGD("exist() could not contact remote: %d\n", status);
14407438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            return -1;
14507438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        }
14607438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        int32_t err = reply.readExceptionCode();
14707438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        int32_t ret = reply.readInt32();
14807438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        if (err < 0) {
14907438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            ALOGD("exist() caught exception %d\n", err);
15007438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            return -1;
15107438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        }
15207438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        return ret;
15307438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root    }
15407438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root
155b88c3eb96625513df4cc998d739d17266ebaf89fKenny Root    virtual int32_t saw(const String16& name, int uid, Vector<String16>* matches)
15607438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root    {
15707438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        Parcel data, reply;
15807438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        data.writeInterfaceToken(IKeystoreService::getInterfaceDescriptor());
15907438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        data.writeString16(name);
160b88c3eb96625513df4cc998d739d17266ebaf89fKenny Root        data.writeInt32(uid);
16107438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        status_t status = remote()->transact(BnKeystoreService::SAW, data, &reply);
16207438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        if (status != NO_ERROR) {
16307438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            ALOGD("saw() could not contact remote: %d\n", status);
16407438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            return -1;
16507438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        }
16607438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        int32_t err = reply.readExceptionCode();
16707438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        int32_t numMatches = reply.readInt32();
16807438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        for (int32_t i = 0; i < numMatches; i++) {
16907438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            matches->push(reply.readString16());
17007438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        }
17107438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        int32_t ret = reply.readInt32();
17207438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        if (err < 0) {
17307438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            ALOGD("saw() caught exception %d\n", err);
17407438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            return -1;
17507438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        }
17607438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        return ret;
17707438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root    }
17807438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root
17907438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root    virtual int32_t reset()
18007438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root    {
18107438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        Parcel data, reply;
18207438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        data.writeInterfaceToken(IKeystoreService::getInterfaceDescriptor());
18307438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        status_t status = remote()->transact(BnKeystoreService::RESET, data, &reply);
18407438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        if (status != NO_ERROR) {
18507438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            ALOGD("reset() could not contact remote: %d\n", status);
18607438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            return -1;
18707438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        }
18807438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        int32_t err = reply.readExceptionCode();
18907438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        int32_t ret = reply.readInt32();
19007438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        if (err < 0) {
19107438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            ALOGD("reset() caught exception %d\n", err);
19207438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            return -1;
19307438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        }
19407438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        return ret;
19507438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root    }
19607438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root
19707438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root    virtual int32_t password(const String16& password)
19807438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root    {
19907438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        Parcel data, reply;
20007438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        data.writeInterfaceToken(IKeystoreService::getInterfaceDescriptor());
20107438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        data.writeString16(password);
20207438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        status_t status = remote()->transact(BnKeystoreService::PASSWORD, data, &reply);
20307438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        if (status != NO_ERROR) {
20407438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            ALOGD("password() could not contact remote: %d\n", status);
20507438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            return -1;
20607438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        }
20707438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        int32_t err = reply.readExceptionCode();
20807438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        int32_t ret = reply.readInt32();
20907438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        if (err < 0) {
21007438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            ALOGD("password() caught exception %d\n", err);
21107438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            return -1;
21207438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        }
21307438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        return ret;
21407438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root    }
21507438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root
21607438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root    virtual int32_t lock()
21707438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root    {
21807438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        Parcel data, reply;
21907438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        data.writeInterfaceToken(IKeystoreService::getInterfaceDescriptor());
22007438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        status_t status = remote()->transact(BnKeystoreService::LOCK, data, &reply);
22107438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        if (status != NO_ERROR) {
22207438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            ALOGD("lock() could not contact remote: %d\n", status);
22307438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            return -1;
22407438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        }
22507438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        int32_t err = reply.readExceptionCode();
22607438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        int32_t ret = reply.readInt32();
22707438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        if (err < 0) {
22807438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            ALOGD("lock() caught exception %d\n", err);
22907438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            return -1;
23007438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        }
23107438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        return ret;
23207438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root    }
23307438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root
23407438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root    virtual int32_t unlock(const String16& password)
23507438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root    {
23607438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        Parcel data, reply;
23707438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        data.writeInterfaceToken(IKeystoreService::getInterfaceDescriptor());
23807438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        data.writeString16(password);
23907438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        status_t status = remote()->transact(BnKeystoreService::UNLOCK, data, &reply);
24007438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        if (status != NO_ERROR) {
24107438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            ALOGD("unlock() could not contact remote: %d\n", status);
24207438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            return -1;
24307438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        }
24407438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        int32_t err = reply.readExceptionCode();
24507438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        int32_t ret = reply.readInt32();
24607438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        if (err < 0) {
24707438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            ALOGD("unlock() caught exception %d\n", err);
24807438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            return -1;
24907438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        }
25007438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        return ret;
25107438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root    }
25207438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root
25307438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root    virtual int32_t zero()
25407438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root    {
25507438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        Parcel data, reply;
25607438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        data.writeInterfaceToken(IKeystoreService::getInterfaceDescriptor());
25707438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        status_t status = remote()->transact(BnKeystoreService::ZERO, data, &reply);
25807438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        if (status != NO_ERROR) {
25907438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            ALOGD("zero() could not contact remote: %d\n", status);
26007438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            return -1;
26107438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        }
26207438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        int32_t err = reply.readExceptionCode();
26307438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        int32_t ret = reply.readInt32();
26407438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        if (err < 0) {
26507438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            ALOGD("zero() caught exception %d\n", err);
26607438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            return -1;
26707438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        }
26807438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        return ret;
26907438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root    }
27007438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root
271b88c3eb96625513df4cc998d739d17266ebaf89fKenny Root    virtual int32_t generate(const String16& name, int uid)
27207438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root    {
27307438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        Parcel data, reply;
27407438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        data.writeInterfaceToken(IKeystoreService::getInterfaceDescriptor());
27507438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        data.writeString16(name);
276b88c3eb96625513df4cc998d739d17266ebaf89fKenny Root        data.writeInt32(uid);
27707438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        status_t status = remote()->transact(BnKeystoreService::GENERATE, data, &reply);
27807438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        if (status != NO_ERROR) {
27907438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            ALOGD("generate() could not contact remote: %d\n", status);
28007438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            return -1;
28107438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        }
28207438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        int32_t err = reply.readExceptionCode();
28307438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        int32_t ret = reply.readInt32();
28407438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        if (err < 0) {
28507438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            ALOGD("generate() caught exception %d\n", err);
28607438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            return -1;
28707438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        }
28807438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        return ret;
28907438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root    }
29007438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root
291b88c3eb96625513df4cc998d739d17266ebaf89fKenny Root    virtual int32_t import(const String16& name, const uint8_t* key, size_t keyLength, int uid)
29207438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root    {
29307438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        Parcel data, reply;
29407438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        data.writeInterfaceToken(IKeystoreService::getInterfaceDescriptor());
29507438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        data.writeString16(name);
29607438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        data.writeInt32(keyLength);
29707438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        void* buf = data.writeInplace(keyLength);
29807438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        memcpy(buf, key, keyLength);
299b88c3eb96625513df4cc998d739d17266ebaf89fKenny Root        data.writeInt32(uid);
30007438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        status_t status = remote()->transact(BnKeystoreService::IMPORT, data, &reply);
30107438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        if (status != NO_ERROR) {
30207438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            ALOGD("import() could not contact remote: %d\n", status);
30307438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            return -1;
30407438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        }
30507438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        int32_t err = reply.readExceptionCode();
30607438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        int32_t ret = reply.readInt32();
30707438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        if (err < 0) {
30807438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            ALOGD("import() caught exception %d\n", err);
30907438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            return -1;
31007438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        }
31107438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        return ret;
31207438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root    }
31307438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root
31407438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root    virtual int32_t sign(const String16& name, const uint8_t* in, size_t inLength, uint8_t** out,
31507438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            size_t* outLength)
31607438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root    {
31707438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        Parcel data, reply;
31807438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        data.writeInterfaceToken(IKeystoreService::getInterfaceDescriptor());
31907438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        data.writeString16(name);
32007438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        data.writeInt32(inLength);
32107438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        void* buf = data.writeInplace(inLength);
32207438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        memcpy(buf, in, inLength);
32307438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        status_t status = remote()->transact(BnKeystoreService::SIGN, data, &reply);
32407438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        if (status != NO_ERROR) {
32507438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            ALOGD("import() could not contact remote: %d\n", status);
32607438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            return -1;
32707438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        }
32807438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        int32_t err = reply.readExceptionCode();
32907438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        ssize_t len = reply.readInt32();
33007438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        if (len >= 0 && (size_t) len <= reply.dataAvail()) {
33107438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            size_t ulen = (size_t) len;
33207438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            const void* outBuf = reply.readInplace(ulen);
33307438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            *out = (uint8_t*) malloc(ulen);
33407438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            if (*out != NULL) {
33507438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root                memcpy((void*) *out, outBuf, ulen);
33607438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root                *outLength = ulen;
33707438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            } else {
33807438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root                ALOGE("out of memory allocating output array in sign");
33907438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root                *outLength = 0;
34007438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            }
34107438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        } else {
34207438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            *outLength = 0;
34307438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        }
34407438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        if (err < 0) {
34507438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            ALOGD("import() caught exception %d\n", err);
34607438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            return -1;
34707438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        }
34807438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        return 0;
34907438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root    }
35007438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root
35107438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root    virtual int32_t verify(const String16& name, const uint8_t* in, size_t inLength,
35207438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            const uint8_t* signature, size_t signatureLength)
35307438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root    {
35407438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        Parcel data, reply;
35507438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        void* buf;
35607438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root
35707438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        data.writeInterfaceToken(IKeystoreService::getInterfaceDescriptor());
35807438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        data.writeString16(name);
35907438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        data.writeInt32(inLength);
36007438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        buf = data.writeInplace(inLength);
36107438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        memcpy(buf, in, inLength);
36207438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        data.writeInt32(signatureLength);
36307438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        buf = data.writeInplace(signatureLength);
36407438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        memcpy(buf, signature, signatureLength);
36507438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        status_t status = remote()->transact(BnKeystoreService::VERIFY, data, &reply);
36607438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        if (status != NO_ERROR) {
36707438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            ALOGD("verify() could not contact remote: %d\n", status);
36807438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            return -1;
36907438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        }
37007438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        int32_t err = reply.readExceptionCode();
37107438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        int32_t ret = reply.readInt32();
37207438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        if (err < 0) {
37307438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            ALOGD("verify() caught exception %d\n", err);
37407438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            return -1;
37507438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        }
37607438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        return ret;
37707438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root    }
37807438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root
37907438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root    virtual int32_t get_pubkey(const String16& name, uint8_t** pubkey, size_t* pubkeyLength)
38007438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root    {
38107438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        Parcel data, reply;
38207438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        data.writeInterfaceToken(IKeystoreService::getInterfaceDescriptor());
38307438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        data.writeString16(name);
38407438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        status_t status = remote()->transact(BnKeystoreService::GET_PUBKEY, data, &reply);
38507438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        if (status != NO_ERROR) {
38607438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            ALOGD("get_pubkey() could not contact remote: %d\n", status);
38707438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            return -1;
38807438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        }
38907438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        int32_t err = reply.readExceptionCode();
39007438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        ssize_t len = reply.readInt32();
39107438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        if (len >= 0 && (size_t) len <= reply.dataAvail()) {
39207438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            size_t ulen = (size_t) len;
39307438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            const void* buf = reply.readInplace(ulen);
39407438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            *pubkey = (uint8_t*) malloc(ulen);
39507438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            if (*pubkey != NULL) {
39607438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root                memcpy(*pubkey, buf, ulen);
39707438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root                *pubkeyLength = ulen;
39807438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            } else {
39907438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root                ALOGE("out of memory allocating output array in get_pubkey");
40007438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root                *pubkeyLength = 0;
40107438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            }
40207438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        } else {
40307438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            *pubkeyLength = 0;
40407438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        }
40507438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        if (err < 0) {
40607438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            ALOGD("get_pubkey() caught exception %d\n", err);
40707438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            return -1;
40807438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        }
40907438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        return 0;
41007438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root     }
41107438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root
412b88c3eb96625513df4cc998d739d17266ebaf89fKenny Root    virtual int32_t del_key(const String16& name, int uid)
41307438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root    {
41407438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        Parcel data, reply;
41507438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        data.writeInterfaceToken(IKeystoreService::getInterfaceDescriptor());
41607438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        data.writeString16(name);
417b88c3eb96625513df4cc998d739d17266ebaf89fKenny Root        data.writeInt32(uid);
41807438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        status_t status = remote()->transact(BnKeystoreService::DEL_KEY, data, &reply);
41907438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        if (status != NO_ERROR) {
42007438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            ALOGD("del_key() could not contact remote: %d\n", status);
42107438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            return -1;
42207438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        }
42307438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        int32_t err = reply.readExceptionCode();
42407438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        int32_t ret = reply.readInt32();
42507438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        if (err < 0) {
42607438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            ALOGD("del_key() caught exception %d\n", err);
42707438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            return -1;
42807438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        }
42907438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        return ret;
43007438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root    }
43107438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root
43207438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root    virtual int32_t grant(const String16& name, int32_t granteeUid)
43307438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root    {
43407438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        Parcel data, reply;
43507438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        data.writeInterfaceToken(IKeystoreService::getInterfaceDescriptor());
43607438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        data.writeString16(name);
43707438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        data.writeInt32(granteeUid);
43807438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        status_t status = remote()->transact(BnKeystoreService::GRANT, data, &reply);
43907438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        if (status != NO_ERROR) {
44007438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            ALOGD("grant() could not contact remote: %d\n", status);
44107438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            return -1;
44207438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        }
44307438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        int32_t err = reply.readExceptionCode();
44407438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        int32_t ret = reply.readInt32();
44507438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        if (err < 0) {
44607438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            ALOGD("grant() caught exception %d\n", err);
44707438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            return -1;
44807438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        }
44907438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        return ret;
45007438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root    }
45107438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root
45207438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root    virtual int32_t ungrant(const String16& name, int32_t granteeUid)
45307438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root    {
45407438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        Parcel data, reply;
45507438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        data.writeInterfaceToken(IKeystoreService::getInterfaceDescriptor());
45607438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        data.writeString16(name);
45707438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        data.writeInt32(granteeUid);
45807438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        status_t status = remote()->transact(BnKeystoreService::UNGRANT, data, &reply);
45907438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        if (status != NO_ERROR) {
46007438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            ALOGD("ungrant() could not contact remote: %d\n", status);
46107438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            return -1;
46207438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        }
46307438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        int32_t err = reply.readExceptionCode();
46407438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        int32_t ret = reply.readInt32();
46507438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        if (err < 0) {
46607438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            ALOGD("ungrant() caught exception %d\n", err);
46707438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            return -1;
46807438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        }
46907438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        return ret;
47007438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root    }
47107438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root
47207438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root    int64_t getmtime(const String16& name)
47307438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root    {
47407438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        Parcel data, reply;
47507438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        data.writeInterfaceToken(IKeystoreService::getInterfaceDescriptor());
47607438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        data.writeString16(name);
47707438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        status_t status = remote()->transact(BnKeystoreService::GETMTIME, data, &reply);
47807438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        if (status != NO_ERROR) {
47907438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            ALOGD("getmtime() could not contact remote: %d\n", status);
48007438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            return -1;
48107438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        }
48207438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        int32_t err = reply.readExceptionCode();
48307438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        int64_t ret = reply.readInt64();
48407438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        if (err < 0) {
48507438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            ALOGD("getmtime() caught exception %d\n", err);
48607438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            return -1;
48707438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        }
48807438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        return ret;
48907438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root    }
4900225407783ee339164a0cd8ca5ef04c99d27c59aKenny Root
491d53bc92f1cc4eb669ec015480cebe5ae7aaaf7cfKenny Root    virtual int32_t duplicate(const String16& srcKey, int32_t srcUid, const String16& destKey,
492d53bc92f1cc4eb669ec015480cebe5ae7aaaf7cfKenny Root            int32_t destUid)
4930225407783ee339164a0cd8ca5ef04c99d27c59aKenny Root    {
4940225407783ee339164a0cd8ca5ef04c99d27c59aKenny Root        Parcel data, reply;
4950225407783ee339164a0cd8ca5ef04c99d27c59aKenny Root        data.writeInterfaceToken(IKeystoreService::getInterfaceDescriptor());
496d53bc92f1cc4eb669ec015480cebe5ae7aaaf7cfKenny Root        data.writeString16(srcKey);
497d53bc92f1cc4eb669ec015480cebe5ae7aaaf7cfKenny Root        data.writeInt32(srcUid);
498d53bc92f1cc4eb669ec015480cebe5ae7aaaf7cfKenny Root        data.writeString16(destKey);
499d53bc92f1cc4eb669ec015480cebe5ae7aaaf7cfKenny Root        data.writeInt32(destUid);
500d53bc92f1cc4eb669ec015480cebe5ae7aaaf7cfKenny Root        status_t status = remote()->transact(BnKeystoreService::DUPLICATE, data, &reply);
5010225407783ee339164a0cd8ca5ef04c99d27c59aKenny Root        if (status != NO_ERROR) {
502d53bc92f1cc4eb669ec015480cebe5ae7aaaf7cfKenny Root            ALOGD("duplicate() could not contact remote: %d\n", status);
5030225407783ee339164a0cd8ca5ef04c99d27c59aKenny Root            return -1;
5040225407783ee339164a0cd8ca5ef04c99d27c59aKenny Root        }
5050225407783ee339164a0cd8ca5ef04c99d27c59aKenny Root        int32_t err = reply.readExceptionCode();
5060225407783ee339164a0cd8ca5ef04c99d27c59aKenny Root        int32_t ret = reply.readInt32();
5070225407783ee339164a0cd8ca5ef04c99d27c59aKenny Root        if (err < 0) {
508d53bc92f1cc4eb669ec015480cebe5ae7aaaf7cfKenny Root            ALOGD("duplicate() caught exception %d\n", err);
5090225407783ee339164a0cd8ca5ef04c99d27c59aKenny Root            return -1;
5100225407783ee339164a0cd8ca5ef04c99d27c59aKenny Root        }
5110225407783ee339164a0cd8ca5ef04c99d27c59aKenny Root        return ret;
5120225407783ee339164a0cd8ca5ef04c99d27c59aKenny Root    }
5134306123e81371bd8bd85f77c2375d29ac53ff771Kenny Root
5144306123e81371bd8bd85f77c2375d29ac53ff771Kenny Root    virtual int32_t is_hardware_backed()
5154306123e81371bd8bd85f77c2375d29ac53ff771Kenny Root    {
5164306123e81371bd8bd85f77c2375d29ac53ff771Kenny Root        Parcel data, reply;
5174306123e81371bd8bd85f77c2375d29ac53ff771Kenny Root        data.writeInterfaceToken(IKeystoreService::getInterfaceDescriptor());
5184306123e81371bd8bd85f77c2375d29ac53ff771Kenny Root        status_t status = remote()->transact(BnKeystoreService::IS_HARDWARE_BACKED, data, &reply);
5194306123e81371bd8bd85f77c2375d29ac53ff771Kenny Root        if (status != NO_ERROR) {
5204306123e81371bd8bd85f77c2375d29ac53ff771Kenny Root            ALOGD("is_hardware_backed() could not contact remote: %d\n", status);
5214306123e81371bd8bd85f77c2375d29ac53ff771Kenny Root            return -1;
5224306123e81371bd8bd85f77c2375d29ac53ff771Kenny Root        }
5234306123e81371bd8bd85f77c2375d29ac53ff771Kenny Root        int32_t err = reply.readExceptionCode();
5244306123e81371bd8bd85f77c2375d29ac53ff771Kenny Root        int32_t ret = reply.readInt32();
5254306123e81371bd8bd85f77c2375d29ac53ff771Kenny Root        if (err < 0) {
5264306123e81371bd8bd85f77c2375d29ac53ff771Kenny Root            ALOGD("is_hardware_backed() caught exception %d\n", err);
5274306123e81371bd8bd85f77c2375d29ac53ff771Kenny Root            return -1;
5284306123e81371bd8bd85f77c2375d29ac53ff771Kenny Root        }
5294306123e81371bd8bd85f77c2375d29ac53ff771Kenny Root        return ret;
5304306123e81371bd8bd85f77c2375d29ac53ff771Kenny Root    }
5312ecc7a1efbb21d86d38b9e0348dfbf0e1213d920Kenny Root
5322ecc7a1efbb21d86d38b9e0348dfbf0e1213d920Kenny Root    virtual int32_t clear_uid(int64_t uid)
5332ecc7a1efbb21d86d38b9e0348dfbf0e1213d920Kenny Root    {
5342ecc7a1efbb21d86d38b9e0348dfbf0e1213d920Kenny Root        Parcel data, reply;
5352ecc7a1efbb21d86d38b9e0348dfbf0e1213d920Kenny Root        data.writeInterfaceToken(IKeystoreService::getInterfaceDescriptor());
5362ecc7a1efbb21d86d38b9e0348dfbf0e1213d920Kenny Root        data.writeInt64(uid);
5372ecc7a1efbb21d86d38b9e0348dfbf0e1213d920Kenny Root        status_t status = remote()->transact(BnKeystoreService::CLEAR_UID, data, &reply);
5382ecc7a1efbb21d86d38b9e0348dfbf0e1213d920Kenny Root        if (status != NO_ERROR) {
5392ecc7a1efbb21d86d38b9e0348dfbf0e1213d920Kenny Root            ALOGD("clear_uid() could not contact remote: %d\n", status);
5402ecc7a1efbb21d86d38b9e0348dfbf0e1213d920Kenny Root            return -1;
5412ecc7a1efbb21d86d38b9e0348dfbf0e1213d920Kenny Root        }
5422ecc7a1efbb21d86d38b9e0348dfbf0e1213d920Kenny Root        int32_t err = reply.readExceptionCode();
5432ecc7a1efbb21d86d38b9e0348dfbf0e1213d920Kenny Root        int32_t ret = reply.readInt32();
5442ecc7a1efbb21d86d38b9e0348dfbf0e1213d920Kenny Root        if (err < 0) {
5452ecc7a1efbb21d86d38b9e0348dfbf0e1213d920Kenny Root            ALOGD("clear_uid() caught exception %d\n", err);
5462ecc7a1efbb21d86d38b9e0348dfbf0e1213d920Kenny Root            return -1;
5472ecc7a1efbb21d86d38b9e0348dfbf0e1213d920Kenny Root        }
5482ecc7a1efbb21d86d38b9e0348dfbf0e1213d920Kenny Root        return ret;
5492ecc7a1efbb21d86d38b9e0348dfbf0e1213d920Kenny Root    }
55007438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root};
55107438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root
55207438c8d7256d3788dac323b4d0055f201e0bec9Kenny RootIMPLEMENT_META_INTERFACE(KeystoreService, "android.security.keystore");
55307438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root
55407438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root// ----------------------------------------------------------------------
55507438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root
55607438c8d7256d3788dac323b4d0055f201e0bec9Kenny Rootstatus_t BnKeystoreService::onTransact(
55707438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root    uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
55807438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root{
55907438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root    switch(code) {
56007438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        case TEST: {
56107438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            CHECK_INTERFACE(IKeystoreService, data, reply);
56207438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            int32_t ret = test();
56307438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            reply->writeNoException();
56407438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            reply->writeInt32(ret);
56507438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            return NO_ERROR;
56607438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        } break;
56707438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        case GET: {
56807438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            CHECK_INTERFACE(IKeystoreService, data, reply);
56907438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            String16 name = data.readString16();
57007438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            void* out = NULL;
57107438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            size_t outSize = 0;
57207438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            int32_t ret = get(name, (uint8_t**) &out, &outSize);
57307438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            reply->writeNoException();
57407438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            if (ret == 1) {
57507438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root                reply->writeInt32(outSize);
57607438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root                void* buf = reply->writeInplace(outSize);
57707438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root                memcpy(buf, out, outSize);
57807438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root                free(out);
57907438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            } else {
58007438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root                reply->writeInt32(-1);
58107438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            }
58207438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            return NO_ERROR;
58307438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        } break;
58407438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        case INSERT: {
58507438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            CHECK_INTERFACE(IKeystoreService, data, reply);
58607438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            String16 name = data.readString16();
58707438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            ssize_t inSize = data.readInt32();
58807438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            const void* in;
58907438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            if (inSize >= 0 && (size_t) inSize <= data.dataAvail()) {
59007438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root                in = data.readInplace(inSize);
59107438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            } else {
59207438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root                in = NULL;
59307438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root                inSize = 0;
59407438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            }
595b88c3eb96625513df4cc998d739d17266ebaf89fKenny Root            int uid = data.readInt32();
596b88c3eb96625513df4cc998d739d17266ebaf89fKenny Root            int32_t ret = insert(name, (const uint8_t*) in, (size_t) inSize, uid);
59707438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            reply->writeNoException();
59807438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            reply->writeInt32(ret);
59907438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            return NO_ERROR;
60007438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        } break;
60107438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        case DEL: {
60207438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            CHECK_INTERFACE(IKeystoreService, data, reply);
60307438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            String16 name = data.readString16();
604b88c3eb96625513df4cc998d739d17266ebaf89fKenny Root            int uid = data.readInt32();
605b88c3eb96625513df4cc998d739d17266ebaf89fKenny Root            int32_t ret = del(name, uid);
60607438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            reply->writeNoException();
60707438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            reply->writeInt32(ret);
60807438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            return NO_ERROR;
60907438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        } break;
61007438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        case EXIST: {
61107438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            CHECK_INTERFACE(IKeystoreService, data, reply);
61207438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            String16 name = data.readString16();
613b88c3eb96625513df4cc998d739d17266ebaf89fKenny Root            int uid = data.readInt32();
614b88c3eb96625513df4cc998d739d17266ebaf89fKenny Root            int32_t ret = exist(name, uid);
61507438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            reply->writeNoException();
61607438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            reply->writeInt32(ret);
61707438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            return NO_ERROR;
61807438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        } break;
61907438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        case SAW: {
62007438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            CHECK_INTERFACE(IKeystoreService, data, reply);
62107438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            String16 name = data.readString16();
622b88c3eb96625513df4cc998d739d17266ebaf89fKenny Root            int uid = data.readInt32();
62307438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            Vector<String16> matches;
624b88c3eb96625513df4cc998d739d17266ebaf89fKenny Root            int32_t ret = saw(name, uid, &matches);
62507438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            reply->writeNoException();
62607438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            reply->writeInt32(matches.size());
62707438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            Vector<String16>::const_iterator it = matches.begin();
62807438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            for (; it != matches.end(); ++it) {
62907438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root                reply->writeString16(*it);
63007438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            }
63107438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            reply->writeInt32(ret);
63207438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            return NO_ERROR;
63307438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        } break;
63407438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        case RESET: {
63507438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            CHECK_INTERFACE(IKeystoreService, data, reply);
63607438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            int32_t ret = reset();
63707438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            reply->writeNoException();
63807438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            reply->writeInt32(ret);
63907438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            return NO_ERROR;
64007438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        } break;
64107438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        case PASSWORD: {
64207438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            CHECK_INTERFACE(IKeystoreService, data, reply);
64307438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            String16 pass = data.readString16();
64407438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            int32_t ret = password(pass);
64507438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            reply->writeNoException();
64607438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            reply->writeInt32(ret);
64707438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            return NO_ERROR;
64807438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        } break;
64907438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        case LOCK: {
65007438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            CHECK_INTERFACE(IKeystoreService, data, reply);
65107438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            int32_t ret = lock();
65207438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            reply->writeNoException();
65307438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            reply->writeInt32(ret);
65407438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            return NO_ERROR;
65507438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        } break;
65607438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        case UNLOCK: {
65707438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            CHECK_INTERFACE(IKeystoreService, data, reply);
65807438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            String16 pass = data.readString16();
65907438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            int32_t ret = unlock(pass);
66007438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            reply->writeNoException();
66107438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            reply->writeInt32(ret);
66207438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            return NO_ERROR;
66307438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        } break;
66407438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        case ZERO: {
66507438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            CHECK_INTERFACE(IKeystoreService, data, reply);
66607438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            int32_t ret = zero();
66707438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            reply->writeNoException();
66807438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            reply->writeInt32(ret);
66907438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            return NO_ERROR;
67007438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        } break;
67107438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        case GENERATE: {
67207438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            CHECK_INTERFACE(IKeystoreService, data, reply);
67307438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            String16 name = data.readString16();
674b88c3eb96625513df4cc998d739d17266ebaf89fKenny Root            int uid = data.readInt32();
675b88c3eb96625513df4cc998d739d17266ebaf89fKenny Root            int32_t ret = generate(name, uid);
67607438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            reply->writeNoException();
67707438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            reply->writeInt32(ret);
67807438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            return NO_ERROR;
67907438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        } break;
68007438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        case IMPORT: {
68107438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            CHECK_INTERFACE(IKeystoreService, data, reply);
68207438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            String16 name = data.readString16();
68307438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            ssize_t inSize = data.readInt32();
68407438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            const void* in;
68507438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            if (inSize >= 0 && (size_t) inSize <= data.dataAvail()) {
68607438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root                in = data.readInplace(inSize);
68707438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            } else {
68807438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root                in = NULL;
68907438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root                inSize = 0;
69007438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            }
691b88c3eb96625513df4cc998d739d17266ebaf89fKenny Root            int uid = data.readInt32();
692b88c3eb96625513df4cc998d739d17266ebaf89fKenny Root            int32_t ret = import(name, (const uint8_t*) in, (size_t) inSize, uid);
69307438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            reply->writeNoException();
69407438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            reply->writeInt32(ret);
69507438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            return NO_ERROR;
69607438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        } break;
69707438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        case SIGN: {
69807438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            CHECK_INTERFACE(IKeystoreService, data, reply);
69907438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            String16 name = data.readString16();
70007438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            ssize_t inSize = data.readInt32();
70107438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            const void* in;
70207438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            if (inSize >= 0 && (size_t) inSize <= data.dataAvail()) {
70307438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root                in = data.readInplace(inSize);
70407438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            } else {
70507438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root                in = NULL;
70607438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root                inSize = 0;
70707438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            }
70807438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            void* out = NULL;
70907438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            size_t outSize = 0;
71007438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            int32_t ret = sign(name, (const uint8_t*) in, (size_t) inSize, (uint8_t**) &out, &outSize);
71107438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            reply->writeNoException();
712b03c9fb5f9c058a8ae0485c986a8ab934ab73eaaKenny Root            if (outSize > 0 && out != NULL) {
713b03c9fb5f9c058a8ae0485c986a8ab934ab73eaaKenny Root                reply->writeInt32(outSize);
714b03c9fb5f9c058a8ae0485c986a8ab934ab73eaaKenny Root                void* buf = reply->writeInplace(outSize);
715b03c9fb5f9c058a8ae0485c986a8ab934ab73eaaKenny Root                memcpy(buf, out, outSize);
716b03c9fb5f9c058a8ae0485c986a8ab934ab73eaaKenny Root                free(out);
717b03c9fb5f9c058a8ae0485c986a8ab934ab73eaaKenny Root            } else {
718e289c404b9d2735fbd67c42086e33c972b46aa33Kenny Root                reply->writeInt32(-1);
719b03c9fb5f9c058a8ae0485c986a8ab934ab73eaaKenny Root            }
72007438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            reply->writeInt32(ret);
72107438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            return NO_ERROR;
72207438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        } break;
72307438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        case VERIFY: {
72407438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            CHECK_INTERFACE(IKeystoreService, data, reply);
72507438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            String16 name = data.readString16();
72607438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            ssize_t inSize = data.readInt32();
72707438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            const void* in;
72807438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            if (inSize >= 0 && (size_t) inSize <= data.dataAvail()) {
72907438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root                in = data.readInplace(inSize);
73007438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            } else {
73107438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root                in = NULL;
73207438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root                inSize = 0;
73307438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            }
73407438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            ssize_t sigSize = data.readInt32();
73507438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            const void* sig;
73607438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            if (sigSize >= 0 && (size_t) sigSize <= data.dataAvail()) {
73707438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root                sig = data.readInplace(sigSize);
73807438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            } else {
73907438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root                sig = NULL;
74007438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root                sigSize = 0;
74107438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            }
74207438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            bool ret = verify(name, (const uint8_t*) in, (size_t) inSize, (const uint8_t*) sig,
74307438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root                    (size_t) sigSize);
74407438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            reply->writeNoException();
74507438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            reply->writeInt32(ret ? 1 : 0);
74607438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            return NO_ERROR;
74707438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        } break;
74807438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        case GET_PUBKEY: {
74907438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            CHECK_INTERFACE(IKeystoreService, data, reply);
75007438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            String16 name = data.readString16();
75107438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            void* out = NULL;
75207438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            size_t outSize = 0;
75307438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            int32_t ret = get_pubkey(name, (unsigned char**) &out, &outSize);
75407438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            reply->writeNoException();
755b03c9fb5f9c058a8ae0485c986a8ab934ab73eaaKenny Root            if (outSize > 0 && out != NULL) {
756b03c9fb5f9c058a8ae0485c986a8ab934ab73eaaKenny Root                reply->writeInt32(outSize);
757b03c9fb5f9c058a8ae0485c986a8ab934ab73eaaKenny Root                void* buf = reply->writeInplace(outSize);
758b03c9fb5f9c058a8ae0485c986a8ab934ab73eaaKenny Root                memcpy(buf, out, outSize);
759b03c9fb5f9c058a8ae0485c986a8ab934ab73eaaKenny Root                free(out);
760b03c9fb5f9c058a8ae0485c986a8ab934ab73eaaKenny Root            } else {
761e289c404b9d2735fbd67c42086e33c972b46aa33Kenny Root                reply->writeInt32(-1);
762b03c9fb5f9c058a8ae0485c986a8ab934ab73eaaKenny Root            }
76307438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            reply->writeInt32(ret);
76407438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            return NO_ERROR;
765b03c9fb5f9c058a8ae0485c986a8ab934ab73eaaKenny Root        } break;
76607438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        case DEL_KEY: {
76707438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            CHECK_INTERFACE(IKeystoreService, data, reply);
76807438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            String16 name = data.readString16();
769b88c3eb96625513df4cc998d739d17266ebaf89fKenny Root            int uid = data.readInt32();
770b88c3eb96625513df4cc998d739d17266ebaf89fKenny Root            int32_t ret = del_key(name, uid);
77107438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            reply->writeNoException();
77207438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            reply->writeInt32(ret);
77307438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            return NO_ERROR;
77407438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        } break;
77507438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        case GRANT: {
77607438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            CHECK_INTERFACE(IKeystoreService, data, reply);
77707438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            String16 name = data.readString16();
77807438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            int32_t granteeUid = data.readInt32();
77907438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            int32_t ret = grant(name, granteeUid);
78007438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            reply->writeNoException();
78107438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            reply->writeInt32(ret);
78207438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            return NO_ERROR;
78307438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        } break;
78407438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        case UNGRANT: {
78507438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            CHECK_INTERFACE(IKeystoreService, data, reply);
78607438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            String16 name = data.readString16();
78707438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            int32_t granteeUid = data.readInt32();
78807438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            int32_t ret = ungrant(name, granteeUid);
78907438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            reply->writeNoException();
79007438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            reply->writeInt32(ret);
79107438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            return NO_ERROR;
79207438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        } break;
79307438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        case GETMTIME: {
79407438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            CHECK_INTERFACE(IKeystoreService, data, reply);
79507438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            String16 name = data.readString16();
79607438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            int64_t ret = getmtime(name);
79707438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            reply->writeNoException();
79807438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            reply->writeInt64(ret);
79907438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            return NO_ERROR;
80007438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        } break;
801d53bc92f1cc4eb669ec015480cebe5ae7aaaf7cfKenny Root        case DUPLICATE: {
8020225407783ee339164a0cd8ca5ef04c99d27c59aKenny Root            CHECK_INTERFACE(IKeystoreService, data, reply);
803d53bc92f1cc4eb669ec015480cebe5ae7aaaf7cfKenny Root            String16 srcKey = data.readString16();
804d53bc92f1cc4eb669ec015480cebe5ae7aaaf7cfKenny Root            int32_t srcUid = data.readInt32();
805d53bc92f1cc4eb669ec015480cebe5ae7aaaf7cfKenny Root            String16 destKey = data.readString16();
806d53bc92f1cc4eb669ec015480cebe5ae7aaaf7cfKenny Root            int32_t destUid = data.readInt32();
807d53bc92f1cc4eb669ec015480cebe5ae7aaaf7cfKenny Root            int32_t ret = duplicate(srcKey, srcUid, destKey, destUid);
8080225407783ee339164a0cd8ca5ef04c99d27c59aKenny Root            reply->writeNoException();
8090225407783ee339164a0cd8ca5ef04c99d27c59aKenny Root            reply->writeInt32(ret);
8100225407783ee339164a0cd8ca5ef04c99d27c59aKenny Root            return NO_ERROR;
8110225407783ee339164a0cd8ca5ef04c99d27c59aKenny Root        } break;
8124306123e81371bd8bd85f77c2375d29ac53ff771Kenny Root        case IS_HARDWARE_BACKED: {
8134306123e81371bd8bd85f77c2375d29ac53ff771Kenny Root            CHECK_INTERFACE(IKeystoreService, data, reply);
8144306123e81371bd8bd85f77c2375d29ac53ff771Kenny Root            int32_t ret = is_hardware_backed();
8154306123e81371bd8bd85f77c2375d29ac53ff771Kenny Root            reply->writeNoException();
8164306123e81371bd8bd85f77c2375d29ac53ff771Kenny Root            reply->writeInt32(ret);
8174306123e81371bd8bd85f77c2375d29ac53ff771Kenny Root            return NO_ERROR;
8184306123e81371bd8bd85f77c2375d29ac53ff771Kenny Root        }
8192ecc7a1efbb21d86d38b9e0348dfbf0e1213d920Kenny Root        case CLEAR_UID: {
8202ecc7a1efbb21d86d38b9e0348dfbf0e1213d920Kenny Root            CHECK_INTERFACE(IKeystoreService, data, reply);
8212ecc7a1efbb21d86d38b9e0348dfbf0e1213d920Kenny Root            int64_t uid = data.readInt64();
8222ecc7a1efbb21d86d38b9e0348dfbf0e1213d920Kenny Root            int32_t ret = clear_uid(uid);
8232ecc7a1efbb21d86d38b9e0348dfbf0e1213d920Kenny Root            reply->writeNoException();
8242ecc7a1efbb21d86d38b9e0348dfbf0e1213d920Kenny Root            reply->writeInt32(ret);
8252ecc7a1efbb21d86d38b9e0348dfbf0e1213d920Kenny Root            return NO_ERROR;
8262ecc7a1efbb21d86d38b9e0348dfbf0e1213d920Kenny Root        }
82707438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        default:
82807438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            return BBinder::onTransact(code, data, reply, flags);
82907438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root    }
83007438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root}
83107438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root
83207438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root// ----------------------------------------------------------------------------
83307438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root
83407438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root}; // namespace android
835