IKeystoreService.cpp revision 0225407783ee339164a0cd8ca5ef04c99d27c59a
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
4910225407783ee339164a0cd8ca5ef04c99d27c59aKenny Root    virtual int32_t migrate(const String16& name, int32_t targetUid)
4920225407783ee339164a0cd8ca5ef04c99d27c59aKenny Root    {
4930225407783ee339164a0cd8ca5ef04c99d27c59aKenny Root        Parcel data, reply;
4940225407783ee339164a0cd8ca5ef04c99d27c59aKenny Root        data.writeInterfaceToken(IKeystoreService::getInterfaceDescriptor());
4950225407783ee339164a0cd8ca5ef04c99d27c59aKenny Root        data.writeString16(name);
4960225407783ee339164a0cd8ca5ef04c99d27c59aKenny Root        data.writeInt32(targetUid);
4970225407783ee339164a0cd8ca5ef04c99d27c59aKenny Root        status_t status = remote()->transact(BnKeystoreService::MIGRATE, data, &reply);
4980225407783ee339164a0cd8ca5ef04c99d27c59aKenny Root        if (status != NO_ERROR) {
4990225407783ee339164a0cd8ca5ef04c99d27c59aKenny Root            ALOGD("migrate() could not contact remote: %d\n", status);
5000225407783ee339164a0cd8ca5ef04c99d27c59aKenny Root            return -1;
5010225407783ee339164a0cd8ca5ef04c99d27c59aKenny Root        }
5020225407783ee339164a0cd8ca5ef04c99d27c59aKenny Root        int32_t err = reply.readExceptionCode();
5030225407783ee339164a0cd8ca5ef04c99d27c59aKenny Root        int32_t ret = reply.readInt32();
5040225407783ee339164a0cd8ca5ef04c99d27c59aKenny Root        if (err < 0) {
5050225407783ee339164a0cd8ca5ef04c99d27c59aKenny Root            ALOGD("migrate() caught exception %d\n", err);
5060225407783ee339164a0cd8ca5ef04c99d27c59aKenny Root            return -1;
5070225407783ee339164a0cd8ca5ef04c99d27c59aKenny Root        }
5080225407783ee339164a0cd8ca5ef04c99d27c59aKenny Root        return ret;
5090225407783ee339164a0cd8ca5ef04c99d27c59aKenny Root    }
51007438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root};
51107438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root
51207438c8d7256d3788dac323b4d0055f201e0bec9Kenny RootIMPLEMENT_META_INTERFACE(KeystoreService, "android.security.keystore");
51307438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root
51407438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root// ----------------------------------------------------------------------
51507438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root
51607438c8d7256d3788dac323b4d0055f201e0bec9Kenny Rootstatus_t BnKeystoreService::onTransact(
51707438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root    uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
51807438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root{
51907438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root    switch(code) {
52007438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        case TEST: {
52107438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            CHECK_INTERFACE(IKeystoreService, data, reply);
52207438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            int32_t ret = test();
52307438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            reply->writeNoException();
52407438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            reply->writeInt32(ret);
52507438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            return NO_ERROR;
52607438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        } break;
52707438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        case GET: {
52807438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            CHECK_INTERFACE(IKeystoreService, data, reply);
52907438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            String16 name = data.readString16();
53007438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            void* out = NULL;
53107438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            size_t outSize = 0;
53207438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            int32_t ret = get(name, (uint8_t**) &out, &outSize);
53307438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            reply->writeNoException();
53407438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            if (ret == 1) {
53507438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root                reply->writeInt32(outSize);
53607438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root                void* buf = reply->writeInplace(outSize);
53707438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root                memcpy(buf, out, outSize);
53807438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root                free(out);
53907438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            } else {
54007438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root                reply->writeInt32(-1);
54107438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            }
54207438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            return NO_ERROR;
54307438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        } break;
54407438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        case INSERT: {
54507438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            CHECK_INTERFACE(IKeystoreService, data, reply);
54607438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            String16 name = data.readString16();
54707438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            ssize_t inSize = data.readInt32();
54807438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            const void* in;
54907438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            if (inSize >= 0 && (size_t) inSize <= data.dataAvail()) {
55007438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root                in = data.readInplace(inSize);
55107438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            } else {
55207438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root                in = NULL;
55307438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root                inSize = 0;
55407438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            }
555b88c3eb96625513df4cc998d739d17266ebaf89fKenny Root            int uid = data.readInt32();
556b88c3eb96625513df4cc998d739d17266ebaf89fKenny Root            int32_t ret = insert(name, (const uint8_t*) in, (size_t) inSize, uid);
55707438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            reply->writeNoException();
55807438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            reply->writeInt32(ret);
55907438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            return NO_ERROR;
56007438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        } break;
56107438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        case DEL: {
56207438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            CHECK_INTERFACE(IKeystoreService, data, reply);
56307438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            String16 name = data.readString16();
564b88c3eb96625513df4cc998d739d17266ebaf89fKenny Root            int uid = data.readInt32();
565b88c3eb96625513df4cc998d739d17266ebaf89fKenny Root            int32_t ret = del(name, uid);
56607438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            reply->writeNoException();
56707438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            reply->writeInt32(ret);
56807438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            return NO_ERROR;
56907438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        } break;
57007438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        case EXIST: {
57107438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            CHECK_INTERFACE(IKeystoreService, data, reply);
57207438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            String16 name = data.readString16();
573b88c3eb96625513df4cc998d739d17266ebaf89fKenny Root            int uid = data.readInt32();
574b88c3eb96625513df4cc998d739d17266ebaf89fKenny Root            int32_t ret = exist(name, uid);
57507438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            reply->writeNoException();
57607438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            reply->writeInt32(ret);
57707438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            return NO_ERROR;
57807438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        } break;
57907438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        case SAW: {
58007438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            CHECK_INTERFACE(IKeystoreService, data, reply);
58107438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            String16 name = data.readString16();
582b88c3eb96625513df4cc998d739d17266ebaf89fKenny Root            int uid = data.readInt32();
58307438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            Vector<String16> matches;
584b88c3eb96625513df4cc998d739d17266ebaf89fKenny Root            int32_t ret = saw(name, uid, &matches);
58507438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            reply->writeNoException();
58607438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            reply->writeInt32(matches.size());
58707438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            Vector<String16>::const_iterator it = matches.begin();
58807438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            for (; it != matches.end(); ++it) {
58907438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root                reply->writeString16(*it);
59007438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            }
59107438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            reply->writeInt32(ret);
59207438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            return NO_ERROR;
59307438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        } break;
59407438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        case RESET: {
59507438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            CHECK_INTERFACE(IKeystoreService, data, reply);
59607438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            int32_t ret = reset();
59707438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            reply->writeNoException();
59807438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            reply->writeInt32(ret);
59907438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            return NO_ERROR;
60007438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        } break;
60107438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        case PASSWORD: {
60207438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            CHECK_INTERFACE(IKeystoreService, data, reply);
60307438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            String16 pass = data.readString16();
60407438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            int32_t ret = password(pass);
60507438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            reply->writeNoException();
60607438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            reply->writeInt32(ret);
60707438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            return NO_ERROR;
60807438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        } break;
60907438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        case LOCK: {
61007438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            CHECK_INTERFACE(IKeystoreService, data, reply);
61107438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            int32_t ret = lock();
61207438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            reply->writeNoException();
61307438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            reply->writeInt32(ret);
61407438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            return NO_ERROR;
61507438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        } break;
61607438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        case UNLOCK: {
61707438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            CHECK_INTERFACE(IKeystoreService, data, reply);
61807438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            String16 pass = data.readString16();
61907438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            int32_t ret = unlock(pass);
62007438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            reply->writeNoException();
62107438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            reply->writeInt32(ret);
62207438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            return NO_ERROR;
62307438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        } break;
62407438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        case ZERO: {
62507438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            CHECK_INTERFACE(IKeystoreService, data, reply);
62607438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            int32_t ret = zero();
62707438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            reply->writeNoException();
62807438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            reply->writeInt32(ret);
62907438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            return NO_ERROR;
63007438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        } break;
63107438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        case GENERATE: {
63207438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            CHECK_INTERFACE(IKeystoreService, data, reply);
63307438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            String16 name = data.readString16();
634b88c3eb96625513df4cc998d739d17266ebaf89fKenny Root            int uid = data.readInt32();
635b88c3eb96625513df4cc998d739d17266ebaf89fKenny Root            int32_t ret = generate(name, uid);
63607438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            reply->writeNoException();
63707438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            reply->writeInt32(ret);
63807438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            return NO_ERROR;
63907438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        } break;
64007438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        case IMPORT: {
64107438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            CHECK_INTERFACE(IKeystoreService, data, reply);
64207438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            String16 name = data.readString16();
64307438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            ssize_t inSize = data.readInt32();
64407438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            const void* in;
64507438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            if (inSize >= 0 && (size_t) inSize <= data.dataAvail()) {
64607438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root                in = data.readInplace(inSize);
64707438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            } else {
64807438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root                in = NULL;
64907438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root                inSize = 0;
65007438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            }
651b88c3eb96625513df4cc998d739d17266ebaf89fKenny Root            int uid = data.readInt32();
652b88c3eb96625513df4cc998d739d17266ebaf89fKenny Root            int32_t ret = import(name, (const uint8_t*) in, (size_t) inSize, uid);
65307438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            reply->writeNoException();
65407438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            reply->writeInt32(ret);
65507438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            return NO_ERROR;
65607438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        } break;
65707438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        case SIGN: {
65807438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            CHECK_INTERFACE(IKeystoreService, data, reply);
65907438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            String16 name = data.readString16();
66007438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            ssize_t inSize = data.readInt32();
66107438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            const void* in;
66207438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            if (inSize >= 0 && (size_t) inSize <= data.dataAvail()) {
66307438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root                in = data.readInplace(inSize);
66407438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            } else {
66507438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root                in = NULL;
66607438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root                inSize = 0;
66707438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            }
66807438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            void* out = NULL;
66907438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            size_t outSize = 0;
67007438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            int32_t ret = sign(name, (const uint8_t*) in, (size_t) inSize, (uint8_t**) &out, &outSize);
67107438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            reply->writeNoException();
672b03c9fb5f9c058a8ae0485c986a8ab934ab73eaaKenny Root            if (outSize > 0 && out != NULL) {
673b03c9fb5f9c058a8ae0485c986a8ab934ab73eaaKenny Root                reply->writeInt32(outSize);
674b03c9fb5f9c058a8ae0485c986a8ab934ab73eaaKenny Root                void* buf = reply->writeInplace(outSize);
675b03c9fb5f9c058a8ae0485c986a8ab934ab73eaaKenny Root                memcpy(buf, out, outSize);
676b03c9fb5f9c058a8ae0485c986a8ab934ab73eaaKenny Root                free(out);
677b03c9fb5f9c058a8ae0485c986a8ab934ab73eaaKenny Root            } else {
678e289c404b9d2735fbd67c42086e33c972b46aa33Kenny Root                reply->writeInt32(-1);
679b03c9fb5f9c058a8ae0485c986a8ab934ab73eaaKenny Root            }
68007438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            reply->writeInt32(ret);
68107438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            return NO_ERROR;
68207438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        } break;
68307438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        case VERIFY: {
68407438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            CHECK_INTERFACE(IKeystoreService, data, reply);
68507438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            String16 name = data.readString16();
68607438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            ssize_t inSize = data.readInt32();
68707438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            const void* in;
68807438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            if (inSize >= 0 && (size_t) inSize <= data.dataAvail()) {
68907438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root                in = data.readInplace(inSize);
69007438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            } else {
69107438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root                in = NULL;
69207438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root                inSize = 0;
69307438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            }
69407438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            ssize_t sigSize = data.readInt32();
69507438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            const void* sig;
69607438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            if (sigSize >= 0 && (size_t) sigSize <= data.dataAvail()) {
69707438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root                sig = data.readInplace(sigSize);
69807438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            } else {
69907438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root                sig = NULL;
70007438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root                sigSize = 0;
70107438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            }
70207438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            bool ret = verify(name, (const uint8_t*) in, (size_t) inSize, (const uint8_t*) sig,
70307438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root                    (size_t) sigSize);
70407438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            reply->writeNoException();
70507438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            reply->writeInt32(ret ? 1 : 0);
70607438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            return NO_ERROR;
70707438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        } break;
70807438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        case GET_PUBKEY: {
70907438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            CHECK_INTERFACE(IKeystoreService, data, reply);
71007438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            String16 name = data.readString16();
71107438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            void* out = NULL;
71207438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            size_t outSize = 0;
71307438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            int32_t ret = get_pubkey(name, (unsigned char**) &out, &outSize);
71407438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            reply->writeNoException();
715b03c9fb5f9c058a8ae0485c986a8ab934ab73eaaKenny Root            if (outSize > 0 && out != NULL) {
716b03c9fb5f9c058a8ae0485c986a8ab934ab73eaaKenny Root                reply->writeInt32(outSize);
717b03c9fb5f9c058a8ae0485c986a8ab934ab73eaaKenny Root                void* buf = reply->writeInplace(outSize);
718b03c9fb5f9c058a8ae0485c986a8ab934ab73eaaKenny Root                memcpy(buf, out, outSize);
719b03c9fb5f9c058a8ae0485c986a8ab934ab73eaaKenny Root                free(out);
720b03c9fb5f9c058a8ae0485c986a8ab934ab73eaaKenny Root            } else {
721e289c404b9d2735fbd67c42086e33c972b46aa33Kenny Root                reply->writeInt32(-1);
722b03c9fb5f9c058a8ae0485c986a8ab934ab73eaaKenny Root            }
72307438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            reply->writeInt32(ret);
72407438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            return NO_ERROR;
725b03c9fb5f9c058a8ae0485c986a8ab934ab73eaaKenny Root        } break;
72607438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        case DEL_KEY: {
72707438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            CHECK_INTERFACE(IKeystoreService, data, reply);
72807438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            String16 name = data.readString16();
729b88c3eb96625513df4cc998d739d17266ebaf89fKenny Root            int uid = data.readInt32();
730b88c3eb96625513df4cc998d739d17266ebaf89fKenny Root            int32_t ret = del_key(name, uid);
73107438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            reply->writeNoException();
73207438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            reply->writeInt32(ret);
73307438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            return NO_ERROR;
73407438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        } break;
73507438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        case GRANT: {
73607438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            CHECK_INTERFACE(IKeystoreService, data, reply);
73707438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            String16 name = data.readString16();
73807438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            int32_t granteeUid = data.readInt32();
73907438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            int32_t ret = grant(name, granteeUid);
74007438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            reply->writeNoException();
74107438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            reply->writeInt32(ret);
74207438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            return NO_ERROR;
74307438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        } break;
74407438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        case UNGRANT: {
74507438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            CHECK_INTERFACE(IKeystoreService, data, reply);
74607438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            String16 name = data.readString16();
74707438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            int32_t granteeUid = data.readInt32();
74807438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            int32_t ret = ungrant(name, granteeUid);
74907438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            reply->writeNoException();
75007438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            reply->writeInt32(ret);
75107438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            return NO_ERROR;
75207438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        } break;
75307438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        case GETMTIME: {
75407438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            CHECK_INTERFACE(IKeystoreService, data, reply);
75507438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            String16 name = data.readString16();
75607438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            int64_t ret = getmtime(name);
75707438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            reply->writeNoException();
75807438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            reply->writeInt64(ret);
75907438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            return NO_ERROR;
76007438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        } break;
7610225407783ee339164a0cd8ca5ef04c99d27c59aKenny Root        case MIGRATE: {
7620225407783ee339164a0cd8ca5ef04c99d27c59aKenny Root            CHECK_INTERFACE(IKeystoreService, data, reply);
7630225407783ee339164a0cd8ca5ef04c99d27c59aKenny Root            String16 name = data.readString16();
7640225407783ee339164a0cd8ca5ef04c99d27c59aKenny Root            int32_t targetUid = data.readInt32();
7650225407783ee339164a0cd8ca5ef04c99d27c59aKenny Root            int32_t ret = migrate(name, targetUid);
7660225407783ee339164a0cd8ca5ef04c99d27c59aKenny Root            reply->writeNoException();
7670225407783ee339164a0cd8ca5ef04c99d27c59aKenny Root            reply->writeInt32(ret);
7680225407783ee339164a0cd8ca5ef04c99d27c59aKenny Root            return NO_ERROR;
7690225407783ee339164a0cd8ca5ef04c99d27c59aKenny Root        } break;
77007438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        default:
77107438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            return BBinder::onTransact(code, data, reply, flags);
77207438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root    }
77307438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root}
77407438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root
77507438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root// ----------------------------------------------------------------------------
77607438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root
77707438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root}; // namespace android
778