IKeystoreService.cpp revision 96427baf0094d50047049d329b0779c3c910402c
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
3296427baf0094d50047049d329b0779c3c910402cKenny RootKeystoreArg::KeystoreArg(const void* data, size_t len)
3396427baf0094d50047049d329b0779c3c910402cKenny Root    : mData(data), mSize(len) {
3496427baf0094d50047049d329b0779c3c910402cKenny Root}
3596427baf0094d50047049d329b0779c3c910402cKenny Root
3696427baf0094d50047049d329b0779c3c910402cKenny RootKeystoreArg::~KeystoreArg() {
3796427baf0094d50047049d329b0779c3c910402cKenny Root}
3896427baf0094d50047049d329b0779c3c910402cKenny Root
3996427baf0094d50047049d329b0779c3c910402cKenny Rootconst void *KeystoreArg::data() const {
4096427baf0094d50047049d329b0779c3c910402cKenny Root    return mData;
4196427baf0094d50047049d329b0779c3c910402cKenny Root}
4296427baf0094d50047049d329b0779c3c910402cKenny Root
4396427baf0094d50047049d329b0779c3c910402cKenny Rootsize_t KeystoreArg::size() const {
4496427baf0094d50047049d329b0779c3c910402cKenny Root    return mSize;
4596427baf0094d50047049d329b0779c3c910402cKenny Root}
4696427baf0094d50047049d329b0779c3c910402cKenny Root
4707438c8d7256d3788dac323b4d0055f201e0bec9Kenny Rootclass BpKeystoreService: public BpInterface<IKeystoreService>
4807438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root{
4907438c8d7256d3788dac323b4d0055f201e0bec9Kenny Rootpublic:
5007438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root    BpKeystoreService(const sp<IBinder>& impl)
5107438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        : BpInterface<IKeystoreService>(impl)
5207438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root    {
5307438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root    }
5407438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root
5507438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root    // test ping
5607438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root    virtual int32_t test()
5707438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root    {
5807438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        Parcel data, reply;
5907438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        data.writeInterfaceToken(IKeystoreService::getInterfaceDescriptor());
6007438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        status_t status = remote()->transact(BnKeystoreService::TEST, data, &reply);
6107438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        if (status != NO_ERROR) {
6207438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            ALOGD("test() could not contact remote: %d\n", status);
6307438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            return -1;
6407438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        }
6507438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        int32_t err = reply.readExceptionCode();
6607438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        int32_t ret = reply.readInt32();
6707438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        if (err < 0) {
6807438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            ALOGD("test() caught exception %d\n", err);
6907438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            return -1;
7007438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        }
7107438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        return ret;
7207438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root    }
7307438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root
7407438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root    virtual int32_t get(const String16& name, uint8_t** item, size_t* itemLength)
7507438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root    {
7607438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        Parcel data, reply;
7707438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        data.writeInterfaceToken(IKeystoreService::getInterfaceDescriptor());
7807438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        data.writeString16(name);
7907438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        status_t status = remote()->transact(BnKeystoreService::GET, data, &reply);
8007438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        if (status != NO_ERROR) {
8107438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            ALOGD("get() could not contact remote: %d\n", status);
8207438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            return -1;
8307438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        }
8407438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        int32_t err = reply.readExceptionCode();
8507438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        ssize_t len = reply.readInt32();
8607438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        if (len >= 0 && (size_t) len <= reply.dataAvail()) {
8707438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            size_t ulen = (size_t) len;
8807438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            const void* buf = reply.readInplace(ulen);
8907438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            *item = (uint8_t*) malloc(ulen);
9007438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            if (*item != NULL) {
9107438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root                memcpy(*item, buf, ulen);
9207438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root                *itemLength = ulen;
9307438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            } else {
9407438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root                ALOGE("out of memory allocating output array in get");
9507438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root                *itemLength = 0;
9607438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            }
9707438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        } else {
9807438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            *itemLength = 0;
9907438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        }
10007438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        if (err < 0) {
10107438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            ALOGD("get() caught exception %d\n", err);
10207438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            return -1;
10307438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        }
10407438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        return 0;
10507438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root    }
10607438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root
1070c540aad5915e6aa34345049be96f28b64d0e84cKenny Root    virtual int32_t insert(const String16& name, const uint8_t* item, size_t itemLength, int uid,
1080c540aad5915e6aa34345049be96f28b64d0e84cKenny Root            int32_t flags)
10907438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root    {
11007438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        Parcel data, reply;
11107438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        data.writeInterfaceToken(IKeystoreService::getInterfaceDescriptor());
11207438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        data.writeString16(name);
11307438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        data.writeInt32(itemLength);
11407438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        void* buf = data.writeInplace(itemLength);
11507438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        memcpy(buf, item, itemLength);
116b88c3eb96625513df4cc998d739d17266ebaf89fKenny Root        data.writeInt32(uid);
1170c540aad5915e6aa34345049be96f28b64d0e84cKenny Root        data.writeInt32(flags);
11807438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        status_t status = remote()->transact(BnKeystoreService::INSERT, data, &reply);
11907438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        if (status != NO_ERROR) {
12007438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            ALOGD("import() could not contact remote: %d\n", status);
12107438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            return -1;
12207438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        }
12307438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        int32_t err = reply.readExceptionCode();
12407438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        int32_t ret = reply.readInt32();
12507438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        if (err < 0) {
12607438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            ALOGD("import() caught exception %d\n", err);
12707438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            return -1;
12807438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        }
12907438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        return ret;
13007438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root    }
13107438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root
132b88c3eb96625513df4cc998d739d17266ebaf89fKenny Root    virtual int32_t del(const String16& name, int uid)
13307438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root    {
13407438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        Parcel data, reply;
13507438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        data.writeInterfaceToken(IKeystoreService::getInterfaceDescriptor());
13607438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        data.writeString16(name);
137b88c3eb96625513df4cc998d739d17266ebaf89fKenny Root        data.writeInt32(uid);
13807438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        status_t status = remote()->transact(BnKeystoreService::DEL, data, &reply);
13907438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        if (status != NO_ERROR) {
14007438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            ALOGD("del() could not contact remote: %d\n", status);
14107438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            return -1;
14207438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        }
14307438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        int32_t err = reply.readExceptionCode();
14407438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        int32_t ret = reply.readInt32();
14507438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        if (err < 0) {
14607438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            ALOGD("del() caught exception %d\n", err);
14707438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            return -1;
14807438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        }
14907438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        return ret;
15007438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root    }
15107438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root
152b88c3eb96625513df4cc998d739d17266ebaf89fKenny Root    virtual int32_t exist(const String16& name, int uid)
15307438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root    {
15407438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        Parcel data, reply;
15507438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        data.writeInterfaceToken(IKeystoreService::getInterfaceDescriptor());
15607438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        data.writeString16(name);
157b88c3eb96625513df4cc998d739d17266ebaf89fKenny Root        data.writeInt32(uid);
15807438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        status_t status = remote()->transact(BnKeystoreService::EXIST, data, &reply);
15907438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        if (status != NO_ERROR) {
16007438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            ALOGD("exist() could not contact remote: %d\n", status);
16107438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            return -1;
16207438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        }
16307438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        int32_t err = reply.readExceptionCode();
16407438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        int32_t ret = reply.readInt32();
16507438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        if (err < 0) {
16607438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            ALOGD("exist() caught exception %d\n", err);
16707438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            return -1;
16807438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        }
16907438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        return ret;
17007438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root    }
17107438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root
172b88c3eb96625513df4cc998d739d17266ebaf89fKenny Root    virtual int32_t saw(const String16& name, int uid, Vector<String16>* matches)
17307438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root    {
17407438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        Parcel data, reply;
17507438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        data.writeInterfaceToken(IKeystoreService::getInterfaceDescriptor());
17607438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        data.writeString16(name);
177b88c3eb96625513df4cc998d739d17266ebaf89fKenny Root        data.writeInt32(uid);
17807438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        status_t status = remote()->transact(BnKeystoreService::SAW, data, &reply);
17907438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        if (status != NO_ERROR) {
18007438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            ALOGD("saw() could not contact remote: %d\n", status);
18107438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            return -1;
18207438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        }
18307438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        int32_t err = reply.readExceptionCode();
18407438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        int32_t numMatches = reply.readInt32();
18507438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        for (int32_t i = 0; i < numMatches; i++) {
18607438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            matches->push(reply.readString16());
18707438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        }
18807438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        int32_t ret = reply.readInt32();
18907438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        if (err < 0) {
19007438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            ALOGD("saw() caught exception %d\n", err);
19107438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            return -1;
19207438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        }
19307438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        return ret;
19407438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root    }
19507438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root
19607438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root    virtual int32_t reset()
19707438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root    {
19807438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        Parcel data, reply;
19907438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        data.writeInterfaceToken(IKeystoreService::getInterfaceDescriptor());
20007438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        status_t status = remote()->transact(BnKeystoreService::RESET, data, &reply);
20107438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        if (status != NO_ERROR) {
20207438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            ALOGD("reset() could not contact remote: %d\n", status);
20307438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            return -1;
20407438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        }
20507438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        int32_t err = reply.readExceptionCode();
20607438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        int32_t ret = reply.readInt32();
20707438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        if (err < 0) {
20807438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            ALOGD("reset() caught exception %d\n", err);
20907438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            return -1;
21007438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        }
21107438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        return ret;
21207438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root    }
21307438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root
21407438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root    virtual int32_t password(const String16& password)
21507438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root    {
21607438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        Parcel data, reply;
21707438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        data.writeInterfaceToken(IKeystoreService::getInterfaceDescriptor());
21807438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        data.writeString16(password);
21907438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        status_t status = remote()->transact(BnKeystoreService::PASSWORD, data, &reply);
22007438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        if (status != NO_ERROR) {
22107438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            ALOGD("password() could not contact remote: %d\n", status);
22207438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            return -1;
22307438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        }
22407438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        int32_t err = reply.readExceptionCode();
22507438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        int32_t ret = reply.readInt32();
22607438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        if (err < 0) {
22707438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            ALOGD("password() caught exception %d\n", err);
22807438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            return -1;
22907438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        }
23007438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        return ret;
23107438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root    }
23207438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root
23307438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root    virtual int32_t lock()
23407438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root    {
23507438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        Parcel data, reply;
23607438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        data.writeInterfaceToken(IKeystoreService::getInterfaceDescriptor());
23707438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        status_t status = remote()->transact(BnKeystoreService::LOCK, data, &reply);
23807438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        if (status != NO_ERROR) {
23907438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            ALOGD("lock() could not contact remote: %d\n", status);
24007438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            return -1;
24107438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        }
24207438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        int32_t err = reply.readExceptionCode();
24307438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        int32_t ret = reply.readInt32();
24407438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        if (err < 0) {
24507438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            ALOGD("lock() caught exception %d\n", err);
24607438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            return -1;
24707438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        }
24807438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        return ret;
24907438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root    }
25007438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root
25107438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root    virtual int32_t unlock(const String16& password)
25207438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root    {
25307438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        Parcel data, reply;
25407438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        data.writeInterfaceToken(IKeystoreService::getInterfaceDescriptor());
25507438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        data.writeString16(password);
25607438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        status_t status = remote()->transact(BnKeystoreService::UNLOCK, data, &reply);
25707438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        if (status != NO_ERROR) {
25807438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            ALOGD("unlock() could not contact remote: %d\n", status);
25907438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            return -1;
26007438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        }
26107438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        int32_t err = reply.readExceptionCode();
26207438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        int32_t ret = reply.readInt32();
26307438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        if (err < 0) {
26407438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            ALOGD("unlock() caught exception %d\n", err);
26507438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            return -1;
26607438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        }
26707438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        return ret;
26807438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root    }
26907438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root
27007438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root    virtual int32_t zero()
27107438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root    {
27207438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        Parcel data, reply;
27307438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        data.writeInterfaceToken(IKeystoreService::getInterfaceDescriptor());
27407438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        status_t status = remote()->transact(BnKeystoreService::ZERO, data, &reply);
27507438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        if (status != NO_ERROR) {
27607438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            ALOGD("zero() could not contact remote: %d\n", status);
27707438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            return -1;
27807438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        }
27907438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        int32_t err = reply.readExceptionCode();
28007438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        int32_t ret = reply.readInt32();
28107438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        if (err < 0) {
28207438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            ALOGD("zero() caught exception %d\n", err);
28307438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            return -1;
28407438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        }
28507438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        return ret;
28607438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root    }
28707438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root
28896427baf0094d50047049d329b0779c3c910402cKenny Root    virtual int32_t generate(const String16& name, int32_t uid, int32_t keyType, int32_t keySize,
28996427baf0094d50047049d329b0779c3c910402cKenny Root            int32_t flags, Vector<sp<KeystoreArg> >* args)
29007438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root    {
29107438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        Parcel data, reply;
29207438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        data.writeInterfaceToken(IKeystoreService::getInterfaceDescriptor());
29307438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        data.writeString16(name);
294b88c3eb96625513df4cc998d739d17266ebaf89fKenny Root        data.writeInt32(uid);
29596427baf0094d50047049d329b0779c3c910402cKenny Root        data.writeInt32(keyType);
29696427baf0094d50047049d329b0779c3c910402cKenny Root        data.writeInt32(keySize);
2970c540aad5915e6aa34345049be96f28b64d0e84cKenny Root        data.writeInt32(flags);
29896427baf0094d50047049d329b0779c3c910402cKenny Root        data.writeInt32(args->size());
29996427baf0094d50047049d329b0779c3c910402cKenny Root        for (Vector<sp<KeystoreArg> >::iterator it = args->begin(); it != args->end(); ++it) {
30096427baf0094d50047049d329b0779c3c910402cKenny Root            sp<KeystoreArg> item = *it;
30196427baf0094d50047049d329b0779c3c910402cKenny Root            size_t keyLength = item->size();
30296427baf0094d50047049d329b0779c3c910402cKenny Root            data.writeInt32(keyLength);
30396427baf0094d50047049d329b0779c3c910402cKenny Root            void* buf = data.writeInplace(keyLength);
30496427baf0094d50047049d329b0779c3c910402cKenny Root            memcpy(buf, item->data(), keyLength);
30596427baf0094d50047049d329b0779c3c910402cKenny Root        }
30607438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        status_t status = remote()->transact(BnKeystoreService::GENERATE, data, &reply);
30707438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        if (status != NO_ERROR) {
30807438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            ALOGD("generate() could not contact remote: %d\n", status);
30907438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            return -1;
31007438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        }
31107438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        int32_t err = reply.readExceptionCode();
31207438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        int32_t ret = reply.readInt32();
31307438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        if (err < 0) {
31407438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            ALOGD("generate() caught exception %d\n", err);
31507438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            return -1;
31607438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        }
31707438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        return ret;
31807438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root    }
31907438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root
3200c540aad5915e6aa34345049be96f28b64d0e84cKenny Root    virtual int32_t import(const String16& name, const uint8_t* key, size_t keyLength, int uid,
3210c540aad5915e6aa34345049be96f28b64d0e84cKenny Root            int flags)
32207438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root    {
32307438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        Parcel data, reply;
32407438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        data.writeInterfaceToken(IKeystoreService::getInterfaceDescriptor());
32507438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        data.writeString16(name);
32607438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        data.writeInt32(keyLength);
32707438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        void* buf = data.writeInplace(keyLength);
32807438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        memcpy(buf, key, keyLength);
329b88c3eb96625513df4cc998d739d17266ebaf89fKenny Root        data.writeInt32(uid);
3300c540aad5915e6aa34345049be96f28b64d0e84cKenny Root        data.writeInt32(flags);
33107438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        status_t status = remote()->transact(BnKeystoreService::IMPORT, data, &reply);
33207438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        if (status != NO_ERROR) {
33307438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            ALOGD("import() could not contact remote: %d\n", status);
33407438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            return -1;
33507438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        }
33607438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        int32_t err = reply.readExceptionCode();
33707438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        int32_t ret = reply.readInt32();
33807438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        if (err < 0) {
33907438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            ALOGD("import() caught exception %d\n", err);
34007438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            return -1;
34107438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        }
34207438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        return ret;
34307438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root    }
34407438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root
34507438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root    virtual int32_t sign(const String16& name, const uint8_t* in, size_t inLength, uint8_t** out,
34607438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            size_t* outLength)
34707438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root    {
34807438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        Parcel data, reply;
34907438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        data.writeInterfaceToken(IKeystoreService::getInterfaceDescriptor());
35007438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        data.writeString16(name);
35107438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        data.writeInt32(inLength);
35207438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        void* buf = data.writeInplace(inLength);
35307438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        memcpy(buf, in, inLength);
35407438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        status_t status = remote()->transact(BnKeystoreService::SIGN, data, &reply);
35507438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        if (status != NO_ERROR) {
35607438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            ALOGD("import() could not contact remote: %d\n", status);
35707438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            return -1;
35807438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        }
35907438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        int32_t err = reply.readExceptionCode();
36007438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        ssize_t len = reply.readInt32();
36107438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        if (len >= 0 && (size_t) len <= reply.dataAvail()) {
36207438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            size_t ulen = (size_t) len;
36307438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            const void* outBuf = reply.readInplace(ulen);
36407438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            *out = (uint8_t*) malloc(ulen);
36507438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            if (*out != NULL) {
36607438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root                memcpy((void*) *out, outBuf, ulen);
36707438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root                *outLength = ulen;
36807438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            } else {
36907438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root                ALOGE("out of memory allocating output array in sign");
37007438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root                *outLength = 0;
37107438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            }
37207438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        } else {
37307438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            *outLength = 0;
37407438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        }
37507438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        if (err < 0) {
37607438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            ALOGD("import() caught exception %d\n", err);
37707438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            return -1;
37807438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        }
37907438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        return 0;
38007438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root    }
38107438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root
38207438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root    virtual int32_t verify(const String16& name, const uint8_t* in, size_t inLength,
38307438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            const uint8_t* signature, size_t signatureLength)
38407438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root    {
38507438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        Parcel data, reply;
38607438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        void* buf;
38707438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root
38807438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        data.writeInterfaceToken(IKeystoreService::getInterfaceDescriptor());
38907438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        data.writeString16(name);
39007438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        data.writeInt32(inLength);
39107438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        buf = data.writeInplace(inLength);
39207438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        memcpy(buf, in, inLength);
39307438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        data.writeInt32(signatureLength);
39407438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        buf = data.writeInplace(signatureLength);
39507438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        memcpy(buf, signature, signatureLength);
39607438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        status_t status = remote()->transact(BnKeystoreService::VERIFY, data, &reply);
39707438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        if (status != NO_ERROR) {
39807438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            ALOGD("verify() could not contact remote: %d\n", status);
39907438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            return -1;
40007438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        }
40107438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        int32_t err = reply.readExceptionCode();
40207438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        int32_t ret = reply.readInt32();
40307438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        if (err < 0) {
40407438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            ALOGD("verify() caught exception %d\n", err);
40507438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            return -1;
40607438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        }
40707438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        return ret;
40807438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root    }
40907438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root
41007438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root    virtual int32_t get_pubkey(const String16& name, uint8_t** pubkey, size_t* pubkeyLength)
41107438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root    {
41207438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        Parcel data, reply;
41307438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        data.writeInterfaceToken(IKeystoreService::getInterfaceDescriptor());
41407438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        data.writeString16(name);
41507438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        status_t status = remote()->transact(BnKeystoreService::GET_PUBKEY, data, &reply);
41607438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        if (status != NO_ERROR) {
41707438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            ALOGD("get_pubkey() could not contact remote: %d\n", status);
41807438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            return -1;
41907438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        }
42007438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        int32_t err = reply.readExceptionCode();
42107438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        ssize_t len = reply.readInt32();
42207438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        if (len >= 0 && (size_t) len <= reply.dataAvail()) {
42307438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            size_t ulen = (size_t) len;
42407438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            const void* buf = reply.readInplace(ulen);
42507438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            *pubkey = (uint8_t*) malloc(ulen);
42607438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            if (*pubkey != NULL) {
42707438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root                memcpy(*pubkey, buf, ulen);
42807438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root                *pubkeyLength = ulen;
42907438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            } else {
43007438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root                ALOGE("out of memory allocating output array in get_pubkey");
43107438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root                *pubkeyLength = 0;
43207438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            }
43307438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        } else {
43407438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            *pubkeyLength = 0;
43507438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        }
43607438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        if (err < 0) {
43707438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            ALOGD("get_pubkey() caught exception %d\n", err);
43807438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            return -1;
43907438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        }
44007438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        return 0;
44107438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root     }
44207438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root
443b88c3eb96625513df4cc998d739d17266ebaf89fKenny Root    virtual int32_t del_key(const String16& name, int uid)
44407438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root    {
44507438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        Parcel data, reply;
44607438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        data.writeInterfaceToken(IKeystoreService::getInterfaceDescriptor());
44707438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        data.writeString16(name);
448b88c3eb96625513df4cc998d739d17266ebaf89fKenny Root        data.writeInt32(uid);
44907438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        status_t status = remote()->transact(BnKeystoreService::DEL_KEY, data, &reply);
45007438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        if (status != NO_ERROR) {
45107438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            ALOGD("del_key() could not contact remote: %d\n", status);
45207438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            return -1;
45307438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        }
45407438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        int32_t err = reply.readExceptionCode();
45507438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        int32_t ret = reply.readInt32();
45607438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        if (err < 0) {
45707438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            ALOGD("del_key() caught exception %d\n", err);
45807438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            return -1;
45907438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        }
46007438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        return ret;
46107438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root    }
46207438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root
46307438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root    virtual int32_t grant(const String16& name, int32_t granteeUid)
46407438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root    {
46507438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        Parcel data, reply;
46607438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        data.writeInterfaceToken(IKeystoreService::getInterfaceDescriptor());
46707438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        data.writeString16(name);
46807438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        data.writeInt32(granteeUid);
46907438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        status_t status = remote()->transact(BnKeystoreService::GRANT, data, &reply);
47007438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        if (status != NO_ERROR) {
47107438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            ALOGD("grant() could not contact remote: %d\n", status);
47207438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            return -1;
47307438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        }
47407438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        int32_t err = reply.readExceptionCode();
47507438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        int32_t ret = reply.readInt32();
47607438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        if (err < 0) {
47707438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            ALOGD("grant() caught exception %d\n", err);
47807438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            return -1;
47907438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        }
48007438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        return ret;
48107438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root    }
48207438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root
48307438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root    virtual int32_t ungrant(const String16& name, int32_t granteeUid)
48407438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root    {
48507438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        Parcel data, reply;
48607438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        data.writeInterfaceToken(IKeystoreService::getInterfaceDescriptor());
48707438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        data.writeString16(name);
48807438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        data.writeInt32(granteeUid);
48907438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        status_t status = remote()->transact(BnKeystoreService::UNGRANT, data, &reply);
49007438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        if (status != NO_ERROR) {
49107438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            ALOGD("ungrant() could not contact remote: %d\n", status);
49207438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            return -1;
49307438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        }
49407438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        int32_t err = reply.readExceptionCode();
49507438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        int32_t ret = reply.readInt32();
49607438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        if (err < 0) {
49707438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            ALOGD("ungrant() caught exception %d\n", err);
49807438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            return -1;
49907438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        }
50007438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        return ret;
50107438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root    }
50207438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root
50307438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root    int64_t getmtime(const String16& name)
50407438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root    {
50507438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        Parcel data, reply;
50607438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        data.writeInterfaceToken(IKeystoreService::getInterfaceDescriptor());
50707438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        data.writeString16(name);
50807438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        status_t status = remote()->transact(BnKeystoreService::GETMTIME, data, &reply);
50907438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        if (status != NO_ERROR) {
51007438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            ALOGD("getmtime() could not contact remote: %d\n", status);
51107438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            return -1;
51207438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        }
51307438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        int32_t err = reply.readExceptionCode();
51407438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        int64_t ret = reply.readInt64();
51507438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        if (err < 0) {
51607438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            ALOGD("getmtime() caught exception %d\n", err);
51707438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            return -1;
51807438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        }
51907438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        return ret;
52007438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root    }
5210225407783ee339164a0cd8ca5ef04c99d27c59aKenny Root
522d53bc92f1cc4eb669ec015480cebe5ae7aaaf7cfKenny Root    virtual int32_t duplicate(const String16& srcKey, int32_t srcUid, const String16& destKey,
523d53bc92f1cc4eb669ec015480cebe5ae7aaaf7cfKenny Root            int32_t destUid)
5240225407783ee339164a0cd8ca5ef04c99d27c59aKenny Root    {
5250225407783ee339164a0cd8ca5ef04c99d27c59aKenny Root        Parcel data, reply;
5260225407783ee339164a0cd8ca5ef04c99d27c59aKenny Root        data.writeInterfaceToken(IKeystoreService::getInterfaceDescriptor());
527d53bc92f1cc4eb669ec015480cebe5ae7aaaf7cfKenny Root        data.writeString16(srcKey);
528d53bc92f1cc4eb669ec015480cebe5ae7aaaf7cfKenny Root        data.writeInt32(srcUid);
529d53bc92f1cc4eb669ec015480cebe5ae7aaaf7cfKenny Root        data.writeString16(destKey);
530d53bc92f1cc4eb669ec015480cebe5ae7aaaf7cfKenny Root        data.writeInt32(destUid);
531d53bc92f1cc4eb669ec015480cebe5ae7aaaf7cfKenny Root        status_t status = remote()->transact(BnKeystoreService::DUPLICATE, data, &reply);
5320225407783ee339164a0cd8ca5ef04c99d27c59aKenny Root        if (status != NO_ERROR) {
533d53bc92f1cc4eb669ec015480cebe5ae7aaaf7cfKenny Root            ALOGD("duplicate() could not contact remote: %d\n", status);
5340225407783ee339164a0cd8ca5ef04c99d27c59aKenny Root            return -1;
5350225407783ee339164a0cd8ca5ef04c99d27c59aKenny Root        }
5360225407783ee339164a0cd8ca5ef04c99d27c59aKenny Root        int32_t err = reply.readExceptionCode();
5370225407783ee339164a0cd8ca5ef04c99d27c59aKenny Root        int32_t ret = reply.readInt32();
5380225407783ee339164a0cd8ca5ef04c99d27c59aKenny Root        if (err < 0) {
539d53bc92f1cc4eb669ec015480cebe5ae7aaaf7cfKenny Root            ALOGD("duplicate() caught exception %d\n", err);
5400225407783ee339164a0cd8ca5ef04c99d27c59aKenny Root            return -1;
5410225407783ee339164a0cd8ca5ef04c99d27c59aKenny Root        }
5420225407783ee339164a0cd8ca5ef04c99d27c59aKenny Root        return ret;
5430225407783ee339164a0cd8ca5ef04c99d27c59aKenny Root    }
5444306123e81371bd8bd85f77c2375d29ac53ff771Kenny Root
5454306123e81371bd8bd85f77c2375d29ac53ff771Kenny Root    virtual int32_t is_hardware_backed()
5464306123e81371bd8bd85f77c2375d29ac53ff771Kenny Root    {
5474306123e81371bd8bd85f77c2375d29ac53ff771Kenny Root        Parcel data, reply;
5484306123e81371bd8bd85f77c2375d29ac53ff771Kenny Root        data.writeInterfaceToken(IKeystoreService::getInterfaceDescriptor());
5494306123e81371bd8bd85f77c2375d29ac53ff771Kenny Root        status_t status = remote()->transact(BnKeystoreService::IS_HARDWARE_BACKED, data, &reply);
5504306123e81371bd8bd85f77c2375d29ac53ff771Kenny Root        if (status != NO_ERROR) {
5514306123e81371bd8bd85f77c2375d29ac53ff771Kenny Root            ALOGD("is_hardware_backed() could not contact remote: %d\n", status);
5524306123e81371bd8bd85f77c2375d29ac53ff771Kenny Root            return -1;
5534306123e81371bd8bd85f77c2375d29ac53ff771Kenny Root        }
5544306123e81371bd8bd85f77c2375d29ac53ff771Kenny Root        int32_t err = reply.readExceptionCode();
5554306123e81371bd8bd85f77c2375d29ac53ff771Kenny Root        int32_t ret = reply.readInt32();
5564306123e81371bd8bd85f77c2375d29ac53ff771Kenny Root        if (err < 0) {
5574306123e81371bd8bd85f77c2375d29ac53ff771Kenny Root            ALOGD("is_hardware_backed() caught exception %d\n", err);
5584306123e81371bd8bd85f77c2375d29ac53ff771Kenny Root            return -1;
5594306123e81371bd8bd85f77c2375d29ac53ff771Kenny Root        }
5604306123e81371bd8bd85f77c2375d29ac53ff771Kenny Root        return ret;
5614306123e81371bd8bd85f77c2375d29ac53ff771Kenny Root    }
5622ecc7a1efbb21d86d38b9e0348dfbf0e1213d920Kenny Root
5632ecc7a1efbb21d86d38b9e0348dfbf0e1213d920Kenny Root    virtual int32_t clear_uid(int64_t uid)
5642ecc7a1efbb21d86d38b9e0348dfbf0e1213d920Kenny Root    {
5652ecc7a1efbb21d86d38b9e0348dfbf0e1213d920Kenny Root        Parcel data, reply;
5662ecc7a1efbb21d86d38b9e0348dfbf0e1213d920Kenny Root        data.writeInterfaceToken(IKeystoreService::getInterfaceDescriptor());
5672ecc7a1efbb21d86d38b9e0348dfbf0e1213d920Kenny Root        data.writeInt64(uid);
5682ecc7a1efbb21d86d38b9e0348dfbf0e1213d920Kenny Root        status_t status = remote()->transact(BnKeystoreService::CLEAR_UID, data, &reply);
5692ecc7a1efbb21d86d38b9e0348dfbf0e1213d920Kenny Root        if (status != NO_ERROR) {
5702ecc7a1efbb21d86d38b9e0348dfbf0e1213d920Kenny Root            ALOGD("clear_uid() could not contact remote: %d\n", status);
5712ecc7a1efbb21d86d38b9e0348dfbf0e1213d920Kenny Root            return -1;
5722ecc7a1efbb21d86d38b9e0348dfbf0e1213d920Kenny Root        }
5732ecc7a1efbb21d86d38b9e0348dfbf0e1213d920Kenny Root        int32_t err = reply.readExceptionCode();
5742ecc7a1efbb21d86d38b9e0348dfbf0e1213d920Kenny Root        int32_t ret = reply.readInt32();
5752ecc7a1efbb21d86d38b9e0348dfbf0e1213d920Kenny Root        if (err < 0) {
5762ecc7a1efbb21d86d38b9e0348dfbf0e1213d920Kenny Root            ALOGD("clear_uid() caught exception %d\n", err);
5772ecc7a1efbb21d86d38b9e0348dfbf0e1213d920Kenny Root            return -1;
5782ecc7a1efbb21d86d38b9e0348dfbf0e1213d920Kenny Root        }
5792ecc7a1efbb21d86d38b9e0348dfbf0e1213d920Kenny Root        return ret;
5802ecc7a1efbb21d86d38b9e0348dfbf0e1213d920Kenny Root    }
58107438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root};
58207438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root
58307438c8d7256d3788dac323b4d0055f201e0bec9Kenny RootIMPLEMENT_META_INTERFACE(KeystoreService, "android.security.keystore");
58407438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root
58507438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root// ----------------------------------------------------------------------
58607438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root
58707438c8d7256d3788dac323b4d0055f201e0bec9Kenny Rootstatus_t BnKeystoreService::onTransact(
58807438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root    uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
58907438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root{
59007438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root    switch(code) {
59107438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        case TEST: {
59207438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            CHECK_INTERFACE(IKeystoreService, data, reply);
59307438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            int32_t ret = test();
59407438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            reply->writeNoException();
59507438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            reply->writeInt32(ret);
59607438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            return NO_ERROR;
59707438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        } break;
59807438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        case GET: {
59907438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            CHECK_INTERFACE(IKeystoreService, data, reply);
60007438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            String16 name = data.readString16();
60107438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            void* out = NULL;
60207438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            size_t outSize = 0;
60307438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            int32_t ret = get(name, (uint8_t**) &out, &outSize);
60407438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            reply->writeNoException();
60507438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            if (ret == 1) {
60607438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root                reply->writeInt32(outSize);
60707438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root                void* buf = reply->writeInplace(outSize);
60807438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root                memcpy(buf, out, outSize);
60907438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root                free(out);
61007438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            } else {
61107438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root                reply->writeInt32(-1);
61207438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            }
61307438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            return NO_ERROR;
61407438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        } break;
61507438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        case INSERT: {
61607438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            CHECK_INTERFACE(IKeystoreService, data, reply);
61707438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            String16 name = data.readString16();
61807438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            ssize_t inSize = data.readInt32();
61907438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            const void* in;
62007438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            if (inSize >= 0 && (size_t) inSize <= data.dataAvail()) {
62107438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root                in = data.readInplace(inSize);
62207438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            } else {
62307438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root                in = NULL;
62407438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root                inSize = 0;
62507438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            }
626b88c3eb96625513df4cc998d739d17266ebaf89fKenny Root            int uid = data.readInt32();
6270c540aad5915e6aa34345049be96f28b64d0e84cKenny Root            int32_t flags = data.readInt32();
6280c540aad5915e6aa34345049be96f28b64d0e84cKenny Root            int32_t ret = insert(name, (const uint8_t*) in, (size_t) inSize, uid, flags);
62907438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            reply->writeNoException();
63007438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            reply->writeInt32(ret);
63107438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            return NO_ERROR;
63207438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        } break;
63307438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        case DEL: {
63407438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            CHECK_INTERFACE(IKeystoreService, data, reply);
63507438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            String16 name = data.readString16();
636b88c3eb96625513df4cc998d739d17266ebaf89fKenny Root            int uid = data.readInt32();
637b88c3eb96625513df4cc998d739d17266ebaf89fKenny Root            int32_t ret = del(name, uid);
63807438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            reply->writeNoException();
63907438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            reply->writeInt32(ret);
64007438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            return NO_ERROR;
64107438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        } break;
64207438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        case EXIST: {
64307438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            CHECK_INTERFACE(IKeystoreService, data, reply);
64407438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            String16 name = data.readString16();
645b88c3eb96625513df4cc998d739d17266ebaf89fKenny Root            int uid = data.readInt32();
646b88c3eb96625513df4cc998d739d17266ebaf89fKenny Root            int32_t ret = exist(name, uid);
64707438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            reply->writeNoException();
64807438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            reply->writeInt32(ret);
64907438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            return NO_ERROR;
65007438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        } break;
65107438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        case SAW: {
65207438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            CHECK_INTERFACE(IKeystoreService, data, reply);
65307438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            String16 name = data.readString16();
654b88c3eb96625513df4cc998d739d17266ebaf89fKenny Root            int uid = data.readInt32();
65507438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            Vector<String16> matches;
656b88c3eb96625513df4cc998d739d17266ebaf89fKenny Root            int32_t ret = saw(name, uid, &matches);
65707438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            reply->writeNoException();
65807438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            reply->writeInt32(matches.size());
65907438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            Vector<String16>::const_iterator it = matches.begin();
66007438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            for (; it != matches.end(); ++it) {
66107438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root                reply->writeString16(*it);
66207438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            }
66307438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            reply->writeInt32(ret);
66407438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            return NO_ERROR;
66507438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        } break;
66607438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        case RESET: {
66707438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            CHECK_INTERFACE(IKeystoreService, data, reply);
66807438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            int32_t ret = reset();
66907438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            reply->writeNoException();
67007438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            reply->writeInt32(ret);
67107438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            return NO_ERROR;
67207438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        } break;
67307438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        case PASSWORD: {
67407438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            CHECK_INTERFACE(IKeystoreService, data, reply);
67507438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            String16 pass = data.readString16();
67607438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            int32_t ret = password(pass);
67707438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            reply->writeNoException();
67807438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            reply->writeInt32(ret);
67907438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            return NO_ERROR;
68007438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        } break;
68107438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        case LOCK: {
68207438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            CHECK_INTERFACE(IKeystoreService, data, reply);
68307438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            int32_t ret = lock();
68407438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            reply->writeNoException();
68507438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            reply->writeInt32(ret);
68607438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            return NO_ERROR;
68707438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        } break;
68807438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        case UNLOCK: {
68907438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            CHECK_INTERFACE(IKeystoreService, data, reply);
69007438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            String16 pass = data.readString16();
69107438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            int32_t ret = unlock(pass);
69207438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            reply->writeNoException();
69307438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            reply->writeInt32(ret);
69407438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            return NO_ERROR;
69507438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        } break;
69607438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        case ZERO: {
69707438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            CHECK_INTERFACE(IKeystoreService, data, reply);
69807438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            int32_t ret = zero();
69907438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            reply->writeNoException();
70007438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            reply->writeInt32(ret);
70107438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            return NO_ERROR;
70207438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        } break;
70307438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        case GENERATE: {
70407438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            CHECK_INTERFACE(IKeystoreService, data, reply);
70507438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            String16 name = data.readString16();
70696427baf0094d50047049d329b0779c3c910402cKenny Root            int32_t uid = data.readInt32();
70796427baf0094d50047049d329b0779c3c910402cKenny Root            int32_t keyType = data.readInt32();
70896427baf0094d50047049d329b0779c3c910402cKenny Root            int32_t keySize = data.readInt32();
7090c540aad5915e6aa34345049be96f28b64d0e84cKenny Root            int32_t flags = data.readInt32();
71096427baf0094d50047049d329b0779c3c910402cKenny Root            Vector<sp<KeystoreArg> > args;
71196427baf0094d50047049d329b0779c3c910402cKenny Root            ssize_t numArgs = data.readInt32();
71296427baf0094d50047049d329b0779c3c910402cKenny Root            if (numArgs > 0) {
71396427baf0094d50047049d329b0779c3c910402cKenny Root                for (size_t i = 0; i < (size_t) numArgs; i++) {
71496427baf0094d50047049d329b0779c3c910402cKenny Root                    ssize_t inSize = data.readInt32();
71596427baf0094d50047049d329b0779c3c910402cKenny Root                    if (inSize >= 0 && (size_t) inSize <= data.dataAvail()) {
71696427baf0094d50047049d329b0779c3c910402cKenny Root                        sp<KeystoreArg> arg = new KeystoreArg(data.readInplace(inSize), inSize);
71796427baf0094d50047049d329b0779c3c910402cKenny Root                        args.push_back(arg);
71896427baf0094d50047049d329b0779c3c910402cKenny Root                    } else {
71996427baf0094d50047049d329b0779c3c910402cKenny Root                        args.push_back(NULL);
72096427baf0094d50047049d329b0779c3c910402cKenny Root                    }
72196427baf0094d50047049d329b0779c3c910402cKenny Root                }
72296427baf0094d50047049d329b0779c3c910402cKenny Root            }
72396427baf0094d50047049d329b0779c3c910402cKenny Root            int32_t ret = generate(name, uid, keyType, keySize, flags, &args);
72407438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            reply->writeNoException();
72507438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            reply->writeInt32(ret);
72607438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            return NO_ERROR;
72707438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        } break;
72807438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        case IMPORT: {
72907438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            CHECK_INTERFACE(IKeystoreService, data, reply);
73007438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            String16 name = data.readString16();
73107438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            ssize_t inSize = data.readInt32();
73207438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            const void* in;
73307438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            if (inSize >= 0 && (size_t) inSize <= data.dataAvail()) {
73407438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root                in = data.readInplace(inSize);
73507438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            } else {
73607438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root                in = NULL;
73707438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root                inSize = 0;
73807438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            }
739b88c3eb96625513df4cc998d739d17266ebaf89fKenny Root            int uid = data.readInt32();
7400c540aad5915e6aa34345049be96f28b64d0e84cKenny Root            int32_t flags = data.readInt32();
7410c540aad5915e6aa34345049be96f28b64d0e84cKenny Root            int32_t ret = import(name, (const uint8_t*) in, (size_t) inSize, uid, flags);
74207438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            reply->writeNoException();
74307438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            reply->writeInt32(ret);
74407438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            return NO_ERROR;
74507438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        } break;
74607438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        case SIGN: {
74707438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            CHECK_INTERFACE(IKeystoreService, data, reply);
74807438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            String16 name = data.readString16();
74907438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            ssize_t inSize = data.readInt32();
75007438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            const void* in;
75107438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            if (inSize >= 0 && (size_t) inSize <= data.dataAvail()) {
75207438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root                in = data.readInplace(inSize);
75307438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            } else {
75407438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root                in = NULL;
75507438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root                inSize = 0;
75607438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            }
75707438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            void* out = NULL;
75807438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            size_t outSize = 0;
75907438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            int32_t ret = sign(name, (const uint8_t*) in, (size_t) inSize, (uint8_t**) &out, &outSize);
76007438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            reply->writeNoException();
761b03c9fb5f9c058a8ae0485c986a8ab934ab73eaaKenny Root            if (outSize > 0 && out != NULL) {
762b03c9fb5f9c058a8ae0485c986a8ab934ab73eaaKenny Root                reply->writeInt32(outSize);
763b03c9fb5f9c058a8ae0485c986a8ab934ab73eaaKenny Root                void* buf = reply->writeInplace(outSize);
764b03c9fb5f9c058a8ae0485c986a8ab934ab73eaaKenny Root                memcpy(buf, out, outSize);
765b03c9fb5f9c058a8ae0485c986a8ab934ab73eaaKenny Root                free(out);
766b03c9fb5f9c058a8ae0485c986a8ab934ab73eaaKenny Root            } else {
767e289c404b9d2735fbd67c42086e33c972b46aa33Kenny Root                reply->writeInt32(-1);
768b03c9fb5f9c058a8ae0485c986a8ab934ab73eaaKenny Root            }
76907438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            reply->writeInt32(ret);
77007438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            return NO_ERROR;
77107438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        } break;
77207438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        case VERIFY: {
77307438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            CHECK_INTERFACE(IKeystoreService, data, reply);
77407438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            String16 name = data.readString16();
77507438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            ssize_t inSize = data.readInt32();
77607438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            const void* in;
77707438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            if (inSize >= 0 && (size_t) inSize <= data.dataAvail()) {
77807438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root                in = data.readInplace(inSize);
77907438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            } else {
78007438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root                in = NULL;
78107438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root                inSize = 0;
78207438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            }
78307438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            ssize_t sigSize = data.readInt32();
78407438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            const void* sig;
78507438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            if (sigSize >= 0 && (size_t) sigSize <= data.dataAvail()) {
78607438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root                sig = data.readInplace(sigSize);
78707438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            } else {
78807438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root                sig = NULL;
78907438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root                sigSize = 0;
79007438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            }
79107438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            bool ret = verify(name, (const uint8_t*) in, (size_t) inSize, (const uint8_t*) sig,
79207438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root                    (size_t) sigSize);
79307438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            reply->writeNoException();
79407438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            reply->writeInt32(ret ? 1 : 0);
79507438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            return NO_ERROR;
79607438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        } break;
79707438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        case GET_PUBKEY: {
79807438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            CHECK_INTERFACE(IKeystoreService, data, reply);
79907438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            String16 name = data.readString16();
80007438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            void* out = NULL;
80107438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            size_t outSize = 0;
80207438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            int32_t ret = get_pubkey(name, (unsigned char**) &out, &outSize);
80307438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            reply->writeNoException();
804b03c9fb5f9c058a8ae0485c986a8ab934ab73eaaKenny Root            if (outSize > 0 && out != NULL) {
805b03c9fb5f9c058a8ae0485c986a8ab934ab73eaaKenny Root                reply->writeInt32(outSize);
806b03c9fb5f9c058a8ae0485c986a8ab934ab73eaaKenny Root                void* buf = reply->writeInplace(outSize);
807b03c9fb5f9c058a8ae0485c986a8ab934ab73eaaKenny Root                memcpy(buf, out, outSize);
808b03c9fb5f9c058a8ae0485c986a8ab934ab73eaaKenny Root                free(out);
809b03c9fb5f9c058a8ae0485c986a8ab934ab73eaaKenny Root            } else {
810e289c404b9d2735fbd67c42086e33c972b46aa33Kenny Root                reply->writeInt32(-1);
811b03c9fb5f9c058a8ae0485c986a8ab934ab73eaaKenny Root            }
81207438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            reply->writeInt32(ret);
81307438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            return NO_ERROR;
814b03c9fb5f9c058a8ae0485c986a8ab934ab73eaaKenny Root        } break;
81507438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        case DEL_KEY: {
81607438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            CHECK_INTERFACE(IKeystoreService, data, reply);
81707438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            String16 name = data.readString16();
818b88c3eb96625513df4cc998d739d17266ebaf89fKenny Root            int uid = data.readInt32();
819b88c3eb96625513df4cc998d739d17266ebaf89fKenny Root            int32_t ret = del_key(name, uid);
82007438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            reply->writeNoException();
82107438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            reply->writeInt32(ret);
82207438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            return NO_ERROR;
82307438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        } break;
82407438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        case GRANT: {
82507438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            CHECK_INTERFACE(IKeystoreService, data, reply);
82607438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            String16 name = data.readString16();
82707438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            int32_t granteeUid = data.readInt32();
82807438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            int32_t ret = grant(name, granteeUid);
82907438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            reply->writeNoException();
83007438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            reply->writeInt32(ret);
83107438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            return NO_ERROR;
83207438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        } break;
83307438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        case UNGRANT: {
83407438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            CHECK_INTERFACE(IKeystoreService, data, reply);
83507438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            String16 name = data.readString16();
83607438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            int32_t granteeUid = data.readInt32();
83707438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            int32_t ret = ungrant(name, granteeUid);
83807438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            reply->writeNoException();
83907438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            reply->writeInt32(ret);
84007438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            return NO_ERROR;
84107438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        } break;
84207438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        case GETMTIME: {
84307438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            CHECK_INTERFACE(IKeystoreService, data, reply);
84407438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            String16 name = data.readString16();
84507438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            int64_t ret = getmtime(name);
84607438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            reply->writeNoException();
84707438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            reply->writeInt64(ret);
84807438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            return NO_ERROR;
84907438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        } break;
850d53bc92f1cc4eb669ec015480cebe5ae7aaaf7cfKenny Root        case DUPLICATE: {
8510225407783ee339164a0cd8ca5ef04c99d27c59aKenny Root            CHECK_INTERFACE(IKeystoreService, data, reply);
852d53bc92f1cc4eb669ec015480cebe5ae7aaaf7cfKenny Root            String16 srcKey = data.readString16();
853d53bc92f1cc4eb669ec015480cebe5ae7aaaf7cfKenny Root            int32_t srcUid = data.readInt32();
854d53bc92f1cc4eb669ec015480cebe5ae7aaaf7cfKenny Root            String16 destKey = data.readString16();
855d53bc92f1cc4eb669ec015480cebe5ae7aaaf7cfKenny Root            int32_t destUid = data.readInt32();
856d53bc92f1cc4eb669ec015480cebe5ae7aaaf7cfKenny Root            int32_t ret = duplicate(srcKey, srcUid, destKey, destUid);
8570225407783ee339164a0cd8ca5ef04c99d27c59aKenny Root            reply->writeNoException();
8580225407783ee339164a0cd8ca5ef04c99d27c59aKenny Root            reply->writeInt32(ret);
8590225407783ee339164a0cd8ca5ef04c99d27c59aKenny Root            return NO_ERROR;
8600225407783ee339164a0cd8ca5ef04c99d27c59aKenny Root        } break;
8614306123e81371bd8bd85f77c2375d29ac53ff771Kenny Root        case IS_HARDWARE_BACKED: {
8624306123e81371bd8bd85f77c2375d29ac53ff771Kenny Root            CHECK_INTERFACE(IKeystoreService, data, reply);
8634306123e81371bd8bd85f77c2375d29ac53ff771Kenny Root            int32_t ret = is_hardware_backed();
8644306123e81371bd8bd85f77c2375d29ac53ff771Kenny Root            reply->writeNoException();
8654306123e81371bd8bd85f77c2375d29ac53ff771Kenny Root            reply->writeInt32(ret);
8664306123e81371bd8bd85f77c2375d29ac53ff771Kenny Root            return NO_ERROR;
8674306123e81371bd8bd85f77c2375d29ac53ff771Kenny Root        }
8682ecc7a1efbb21d86d38b9e0348dfbf0e1213d920Kenny Root        case CLEAR_UID: {
8692ecc7a1efbb21d86d38b9e0348dfbf0e1213d920Kenny Root            CHECK_INTERFACE(IKeystoreService, data, reply);
8702ecc7a1efbb21d86d38b9e0348dfbf0e1213d920Kenny Root            int64_t uid = data.readInt64();
8712ecc7a1efbb21d86d38b9e0348dfbf0e1213d920Kenny Root            int32_t ret = clear_uid(uid);
8722ecc7a1efbb21d86d38b9e0348dfbf0e1213d920Kenny Root            reply->writeNoException();
8732ecc7a1efbb21d86d38b9e0348dfbf0e1213d920Kenny Root            reply->writeInt32(ret);
8742ecc7a1efbb21d86d38b9e0348dfbf0e1213d920Kenny Root            return NO_ERROR;
8752ecc7a1efbb21d86d38b9e0348dfbf0e1213d920Kenny Root        }
87607438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        default:
87707438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            return BBinder::onTransact(code, data, reply, flags);
87807438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root    }
87907438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root}
88007438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root
88107438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root// ----------------------------------------------------------------------------
88207438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root
88307438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root}; // namespace android
884