IKeystoreService.cpp revision d53bc92f1cc4eb669ec015480cebe5ae7aaaf7cf
107438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root/*
207438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root**
307438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root** Copyright 2008, The Android Open Source Project
407438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root**
507438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root** Licensed under the Apache License, Version 2.0 (the "License");
607438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root** you may not use this file except in compliance with the License.
707438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root** You may obtain a copy of the License at
807438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root**
907438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root**     http://www.apache.org/licenses/LICENSE-2.0
1007438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root**
1107438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root** Unless required by applicable law or agreed to in writing, software
1207438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root** distributed under the License is distributed on an "AS IS" BASIS,
1307438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1407438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root** See the License for the specific language governing permissions and
1507438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root** limitations under the License.
1607438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root*/
1707438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root
1807438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root#include <stdint.h>
1907438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root#include <sys/types.h>
2007438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root
2107438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root#define LOG_TAG "KeystoreService"
2207438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root#include <utils/Log.h>
2307438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root
2407438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root#include <binder/Parcel.h>
2507438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root#include <binder/IPCThreadState.h>
2607438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root#include <binder/IServiceManager.h>
2707438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root
2807438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root#include <keystore/IKeystoreService.h>
2907438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root
3007438c8d7256d3788dac323b4d0055f201e0bec9Kenny Rootnamespace android {
3107438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root
3207438c8d7256d3788dac323b4d0055f201e0bec9Kenny Rootclass BpKeystoreService: public BpInterface<IKeystoreService>
3307438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root{
3407438c8d7256d3788dac323b4d0055f201e0bec9Kenny Rootpublic:
3507438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root    BpKeystoreService(const sp<IBinder>& impl)
3607438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        : BpInterface<IKeystoreService>(impl)
3707438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root    {
3807438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root    }
3907438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root
4007438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root    // test ping
4107438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root    virtual int32_t test()
4207438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root    {
4307438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        Parcel data, reply;
4407438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        data.writeInterfaceToken(IKeystoreService::getInterfaceDescriptor());
4507438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        status_t status = remote()->transact(BnKeystoreService::TEST, data, &reply);
4607438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        if (status != NO_ERROR) {
4707438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            ALOGD("test() could not contact remote: %d\n", status);
4807438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            return -1;
4907438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        }
5007438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        int32_t err = reply.readExceptionCode();
5107438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        int32_t ret = reply.readInt32();
5207438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        if (err < 0) {
5307438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            ALOGD("test() caught exception %d\n", err);
5407438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            return -1;
5507438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        }
5607438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        return ret;
5707438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root    }
5807438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root
5907438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root    virtual int32_t get(const String16& name, uint8_t** item, size_t* itemLength)
6007438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root    {
6107438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        Parcel data, reply;
6207438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        data.writeInterfaceToken(IKeystoreService::getInterfaceDescriptor());
6307438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        data.writeString16(name);
6407438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        status_t status = remote()->transact(BnKeystoreService::GET, data, &reply);
6507438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        if (status != NO_ERROR) {
6607438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            ALOGD("get() could not contact remote: %d\n", status);
6707438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            return -1;
6807438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        }
6907438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        int32_t err = reply.readExceptionCode();
7007438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        ssize_t len = reply.readInt32();
7107438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        if (len >= 0 && (size_t) len <= reply.dataAvail()) {
7207438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            size_t ulen = (size_t) len;
7307438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            const void* buf = reply.readInplace(ulen);
7407438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            *item = (uint8_t*) malloc(ulen);
7507438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            if (*item != NULL) {
7607438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root                memcpy(*item, buf, ulen);
7707438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root                *itemLength = ulen;
7807438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            } else {
7907438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root                ALOGE("out of memory allocating output array in get");
8007438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root                *itemLength = 0;
8107438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            }
8207438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        } else {
8307438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            *itemLength = 0;
8407438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        }
8507438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        if (err < 0) {
8607438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            ALOGD("get() caught exception %d\n", err);
8707438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            return -1;
8807438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        }
8907438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        return 0;
9007438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root    }
9107438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root
92b88c3eb96625513df4cc998d739d17266ebaf89fKenny Root    virtual int32_t insert(const String16& name, const uint8_t* item, size_t itemLength, int uid)
9307438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root    {
9407438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        Parcel data, reply;
9507438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        data.writeInterfaceToken(IKeystoreService::getInterfaceDescriptor());
9607438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        data.writeString16(name);
9707438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        data.writeInt32(itemLength);
9807438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        void* buf = data.writeInplace(itemLength);
9907438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        memcpy(buf, item, itemLength);
100b88c3eb96625513df4cc998d739d17266ebaf89fKenny Root        data.writeInt32(uid);
10107438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        status_t status = remote()->transact(BnKeystoreService::INSERT, data, &reply);
10207438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        if (status != NO_ERROR) {
10307438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            ALOGD("import() could not contact remote: %d\n", status);
10407438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            return -1;
10507438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        }
10607438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        int32_t err = reply.readExceptionCode();
10707438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        int32_t ret = reply.readInt32();
10807438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        if (err < 0) {
10907438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            ALOGD("import() caught exception %d\n", err);
11007438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            return -1;
11107438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        }
11207438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        return ret;
11307438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root    }
11407438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root
115b88c3eb96625513df4cc998d739d17266ebaf89fKenny Root    virtual int32_t del(const String16& name, int uid)
11607438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root    {
11707438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        Parcel data, reply;
11807438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        data.writeInterfaceToken(IKeystoreService::getInterfaceDescriptor());
11907438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        data.writeString16(name);
120b88c3eb96625513df4cc998d739d17266ebaf89fKenny Root        data.writeInt32(uid);
12107438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        status_t status = remote()->transact(BnKeystoreService::DEL, data, &reply);
12207438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        if (status != NO_ERROR) {
12307438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            ALOGD("del() could not contact remote: %d\n", status);
12407438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            return -1;
12507438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        }
12607438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        int32_t err = reply.readExceptionCode();
12707438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        int32_t ret = reply.readInt32();
12807438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        if (err < 0) {
12907438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            ALOGD("del() caught exception %d\n", err);
13007438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            return -1;
13107438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        }
13207438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        return ret;
13307438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root    }
13407438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root
135b88c3eb96625513df4cc998d739d17266ebaf89fKenny Root    virtual int32_t exist(const String16& name, int uid)
13607438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root    {
13707438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        Parcel data, reply;
13807438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        data.writeInterfaceToken(IKeystoreService::getInterfaceDescriptor());
13907438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        data.writeString16(name);
140b88c3eb96625513df4cc998d739d17266ebaf89fKenny Root        data.writeInt32(uid);
14107438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        status_t status = remote()->transact(BnKeystoreService::EXIST, data, &reply);
14207438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        if (status != NO_ERROR) {
14307438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            ALOGD("exist() could not contact remote: %d\n", status);
14407438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            return -1;
14507438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        }
14607438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        int32_t err = reply.readExceptionCode();
14707438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        int32_t ret = reply.readInt32();
14807438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        if (err < 0) {
14907438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            ALOGD("exist() caught exception %d\n", err);
15007438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            return -1;
15107438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        }
15207438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        return ret;
15307438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root    }
15407438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root
155b88c3eb96625513df4cc998d739d17266ebaf89fKenny Root    virtual int32_t saw(const String16& name, int uid, Vector<String16>* matches)
15607438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root    {
15707438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        Parcel data, reply;
15807438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        data.writeInterfaceToken(IKeystoreService::getInterfaceDescriptor());
15907438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        data.writeString16(name);
160b88c3eb96625513df4cc998d739d17266ebaf89fKenny Root        data.writeInt32(uid);
16107438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        status_t status = remote()->transact(BnKeystoreService::SAW, data, &reply);
16207438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        if (status != NO_ERROR) {
16307438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            ALOGD("saw() could not contact remote: %d\n", status);
16407438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            return -1;
16507438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        }
16607438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        int32_t err = reply.readExceptionCode();
16707438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        int32_t numMatches = reply.readInt32();
16807438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        for (int32_t i = 0; i < numMatches; i++) {
16907438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            matches->push(reply.readString16());
17007438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        }
17107438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        int32_t ret = reply.readInt32();
17207438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        if (err < 0) {
17307438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            ALOGD("saw() caught exception %d\n", err);
17407438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            return -1;
17507438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        }
17607438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        return ret;
17707438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root    }
17807438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root
17907438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root    virtual int32_t reset()
18007438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root    {
18107438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        Parcel data, reply;
18207438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        data.writeInterfaceToken(IKeystoreService::getInterfaceDescriptor());
18307438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        status_t status = remote()->transact(BnKeystoreService::RESET, data, &reply);
18407438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        if (status != NO_ERROR) {
18507438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            ALOGD("reset() could not contact remote: %d\n", status);
18607438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            return -1;
18707438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        }
18807438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        int32_t err = reply.readExceptionCode();
18907438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        int32_t ret = reply.readInt32();
19007438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        if (err < 0) {
19107438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            ALOGD("reset() caught exception %d\n", err);
19207438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            return -1;
19307438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        }
19407438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        return ret;
19507438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root    }
19607438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root
19707438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root    virtual int32_t password(const String16& password)
19807438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root    {
19907438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        Parcel data, reply;
20007438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        data.writeInterfaceToken(IKeystoreService::getInterfaceDescriptor());
20107438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        data.writeString16(password);
20207438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        status_t status = remote()->transact(BnKeystoreService::PASSWORD, data, &reply);
20307438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        if (status != NO_ERROR) {
20407438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            ALOGD("password() could not contact remote: %d\n", status);
20507438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            return -1;
20607438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        }
20707438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        int32_t err = reply.readExceptionCode();
20807438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        int32_t ret = reply.readInt32();
20907438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        if (err < 0) {
21007438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            ALOGD("password() caught exception %d\n", err);
21107438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            return -1;
21207438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        }
21307438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        return ret;
21407438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root    }
21507438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root
21607438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root    virtual int32_t lock()
21707438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root    {
21807438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        Parcel data, reply;
21907438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        data.writeInterfaceToken(IKeystoreService::getInterfaceDescriptor());
22007438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        status_t status = remote()->transact(BnKeystoreService::LOCK, data, &reply);
22107438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        if (status != NO_ERROR) {
22207438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            ALOGD("lock() could not contact remote: %d\n", status);
22307438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            return -1;
22407438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        }
22507438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        int32_t err = reply.readExceptionCode();
22607438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        int32_t ret = reply.readInt32();
22707438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        if (err < 0) {
22807438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            ALOGD("lock() caught exception %d\n", err);
22907438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            return -1;
23007438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        }
23107438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        return ret;
23207438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root    }
23307438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root
23407438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root    virtual int32_t unlock(const String16& password)
23507438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root    {
23607438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        Parcel data, reply;
23707438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        data.writeInterfaceToken(IKeystoreService::getInterfaceDescriptor());
23807438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        data.writeString16(password);
23907438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        status_t status = remote()->transact(BnKeystoreService::UNLOCK, data, &reply);
24007438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        if (status != NO_ERROR) {
24107438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            ALOGD("unlock() could not contact remote: %d\n", status);
24207438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            return -1;
24307438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        }
24407438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        int32_t err = reply.readExceptionCode();
24507438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        int32_t ret = reply.readInt32();
24607438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        if (err < 0) {
24707438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            ALOGD("unlock() caught exception %d\n", err);
24807438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            return -1;
24907438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        }
25007438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        return ret;
25107438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root    }
25207438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root
25307438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root    virtual int32_t zero()
25407438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root    {
25507438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        Parcel data, reply;
25607438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        data.writeInterfaceToken(IKeystoreService::getInterfaceDescriptor());
25707438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        status_t status = remote()->transact(BnKeystoreService::ZERO, data, &reply);
25807438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        if (status != NO_ERROR) {
25907438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            ALOGD("zero() could not contact remote: %d\n", status);
26007438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            return -1;
26107438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        }
26207438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        int32_t err = reply.readExceptionCode();
26307438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        int32_t ret = reply.readInt32();
26407438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        if (err < 0) {
26507438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            ALOGD("zero() caught exception %d\n", err);
26607438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            return -1;
26707438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        }
26807438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        return ret;
26907438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root    }
27007438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root
271b88c3eb96625513df4cc998d739d17266ebaf89fKenny Root    virtual int32_t generate(const String16& name, int uid)
27207438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root    {
27307438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        Parcel data, reply;
27407438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        data.writeInterfaceToken(IKeystoreService::getInterfaceDescriptor());
27507438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        data.writeString16(name);
276b88c3eb96625513df4cc998d739d17266ebaf89fKenny Root        data.writeInt32(uid);
27707438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        status_t status = remote()->transact(BnKeystoreService::GENERATE, data, &reply);
27807438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        if (status != NO_ERROR) {
27907438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            ALOGD("generate() could not contact remote: %d\n", status);
28007438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            return -1;
28107438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        }
28207438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        int32_t err = reply.readExceptionCode();
28307438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        int32_t ret = reply.readInt32();
28407438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        if (err < 0) {
28507438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            ALOGD("generate() caught exception %d\n", err);
28607438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            return -1;
28707438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        }
28807438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        return ret;
28907438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root    }
29007438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root
291b88c3eb96625513df4cc998d739d17266ebaf89fKenny Root    virtual int32_t import(const String16& name, const uint8_t* key, size_t keyLength, int uid)
29207438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root    {
29307438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        Parcel data, reply;
29407438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        data.writeInterfaceToken(IKeystoreService::getInterfaceDescriptor());
29507438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        data.writeString16(name);
29607438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        data.writeInt32(keyLength);
29707438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        void* buf = data.writeInplace(keyLength);
29807438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        memcpy(buf, key, keyLength);
299b88c3eb96625513df4cc998d739d17266ebaf89fKenny Root        data.writeInt32(uid);
30007438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        status_t status = remote()->transact(BnKeystoreService::IMPORT, data, &reply);
30107438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        if (status != NO_ERROR) {
30207438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            ALOGD("import() could not contact remote: %d\n", status);
30307438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            return -1;
30407438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        }
30507438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        int32_t err = reply.readExceptionCode();
30607438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        int32_t ret = reply.readInt32();
30707438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        if (err < 0) {
30807438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            ALOGD("import() caught exception %d\n", err);
30907438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            return -1;
31007438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        }
31107438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        return ret;
31207438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root    }
31307438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root
31407438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root    virtual int32_t sign(const String16& name, const uint8_t* in, size_t inLength, uint8_t** out,
31507438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            size_t* outLength)
31607438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root    {
31707438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        Parcel data, reply;
31807438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        data.writeInterfaceToken(IKeystoreService::getInterfaceDescriptor());
31907438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        data.writeString16(name);
32007438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        data.writeInt32(inLength);
32107438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        void* buf = data.writeInplace(inLength);
32207438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        memcpy(buf, in, inLength);
32307438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        status_t status = remote()->transact(BnKeystoreService::SIGN, data, &reply);
32407438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        if (status != NO_ERROR) {
32507438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            ALOGD("import() could not contact remote: %d\n", status);
32607438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            return -1;
32707438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        }
32807438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        int32_t err = reply.readExceptionCode();
32907438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        ssize_t len = reply.readInt32();
33007438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        if (len >= 0 && (size_t) len <= reply.dataAvail()) {
33107438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            size_t ulen = (size_t) len;
33207438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            const void* outBuf = reply.readInplace(ulen);
33307438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            *out = (uint8_t*) malloc(ulen);
33407438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            if (*out != NULL) {
33507438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root                memcpy((void*) *out, outBuf, ulen);
33607438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root                *outLength = ulen;
33707438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            } else {
33807438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root                ALOGE("out of memory allocating output array in sign");
33907438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root                *outLength = 0;
34007438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            }
34107438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        } else {
34207438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            *outLength = 0;
34307438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        }
34407438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        if (err < 0) {
34507438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            ALOGD("import() caught exception %d\n", err);
34607438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            return -1;
34707438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        }
34807438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        return 0;
34907438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root    }
35007438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root
35107438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root    virtual int32_t verify(const String16& name, const uint8_t* in, size_t inLength,
35207438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            const uint8_t* signature, size_t signatureLength)
35307438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root    {
35407438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        Parcel data, reply;
35507438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        void* buf;
35607438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root
35707438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        data.writeInterfaceToken(IKeystoreService::getInterfaceDescriptor());
35807438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        data.writeString16(name);
35907438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        data.writeInt32(inLength);
36007438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        buf = data.writeInplace(inLength);
36107438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        memcpy(buf, in, inLength);
36207438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        data.writeInt32(signatureLength);
36307438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        buf = data.writeInplace(signatureLength);
36407438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        memcpy(buf, signature, signatureLength);
36507438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        status_t status = remote()->transact(BnKeystoreService::VERIFY, data, &reply);
36607438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        if (status != NO_ERROR) {
36707438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            ALOGD("verify() could not contact remote: %d\n", status);
36807438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            return -1;
36907438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        }
37007438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        int32_t err = reply.readExceptionCode();
37107438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        int32_t ret = reply.readInt32();
37207438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        if (err < 0) {
37307438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            ALOGD("verify() caught exception %d\n", err);
37407438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            return -1;
37507438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        }
37607438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        return ret;
37707438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root    }
37807438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root
37907438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root    virtual int32_t get_pubkey(const String16& name, uint8_t** pubkey, size_t* pubkeyLength)
38007438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root    {
38107438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        Parcel data, reply;
38207438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        data.writeInterfaceToken(IKeystoreService::getInterfaceDescriptor());
38307438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        data.writeString16(name);
38407438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        status_t status = remote()->transact(BnKeystoreService::GET_PUBKEY, data, &reply);
38507438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        if (status != NO_ERROR) {
38607438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            ALOGD("get_pubkey() could not contact remote: %d\n", status);
38707438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            return -1;
38807438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        }
38907438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        int32_t err = reply.readExceptionCode();
39007438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        ssize_t len = reply.readInt32();
39107438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        if (len >= 0 && (size_t) len <= reply.dataAvail()) {
39207438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            size_t ulen = (size_t) len;
39307438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            const void* buf = reply.readInplace(ulen);
39407438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            *pubkey = (uint8_t*) malloc(ulen);
39507438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            if (*pubkey != NULL) {
39607438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root                memcpy(*pubkey, buf, ulen);
39707438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root                *pubkeyLength = ulen;
39807438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            } else {
39907438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root                ALOGE("out of memory allocating output array in get_pubkey");
40007438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root                *pubkeyLength = 0;
40107438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            }
40207438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        } else {
40307438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            *pubkeyLength = 0;
40407438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        }
40507438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        if (err < 0) {
40607438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            ALOGD("get_pubkey() caught exception %d\n", err);
40707438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            return -1;
40807438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        }
40907438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        return 0;
41007438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root     }
41107438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root
412b88c3eb96625513df4cc998d739d17266ebaf89fKenny Root    virtual int32_t del_key(const String16& name, int uid)
41307438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root    {
41407438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        Parcel data, reply;
41507438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        data.writeInterfaceToken(IKeystoreService::getInterfaceDescriptor());
41607438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        data.writeString16(name);
417b88c3eb96625513df4cc998d739d17266ebaf89fKenny Root        data.writeInt32(uid);
41807438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        status_t status = remote()->transact(BnKeystoreService::DEL_KEY, data, &reply);
41907438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        if (status != NO_ERROR) {
42007438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            ALOGD("del_key() could not contact remote: %d\n", status);
42107438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            return -1;
42207438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        }
42307438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        int32_t err = reply.readExceptionCode();
42407438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        int32_t ret = reply.readInt32();
42507438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        if (err < 0) {
42607438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            ALOGD("del_key() caught exception %d\n", err);
42707438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            return -1;
42807438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        }
42907438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        return ret;
43007438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root    }
43107438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root
43207438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root    virtual int32_t grant(const String16& name, int32_t granteeUid)
43307438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root    {
43407438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        Parcel data, reply;
43507438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        data.writeInterfaceToken(IKeystoreService::getInterfaceDescriptor());
43607438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        data.writeString16(name);
43707438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        data.writeInt32(granteeUid);
43807438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        status_t status = remote()->transact(BnKeystoreService::GRANT, data, &reply);
43907438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        if (status != NO_ERROR) {
44007438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            ALOGD("grant() could not contact remote: %d\n", status);
44107438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            return -1;
44207438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        }
44307438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        int32_t err = reply.readExceptionCode();
44407438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        int32_t ret = reply.readInt32();
44507438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        if (err < 0) {
44607438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            ALOGD("grant() caught exception %d\n", err);
44707438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            return -1;
44807438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        }
44907438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        return ret;
45007438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root    }
45107438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root
45207438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root    virtual int32_t ungrant(const String16& name, int32_t granteeUid)
45307438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root    {
45407438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        Parcel data, reply;
45507438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        data.writeInterfaceToken(IKeystoreService::getInterfaceDescriptor());
45607438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        data.writeString16(name);
45707438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        data.writeInt32(granteeUid);
45807438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        status_t status = remote()->transact(BnKeystoreService::UNGRANT, data, &reply);
45907438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        if (status != NO_ERROR) {
46007438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            ALOGD("ungrant() could not contact remote: %d\n", status);
46107438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            return -1;
46207438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        }
46307438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        int32_t err = reply.readExceptionCode();
46407438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        int32_t ret = reply.readInt32();
46507438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        if (err < 0) {
46607438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            ALOGD("ungrant() caught exception %d\n", err);
46707438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            return -1;
46807438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        }
46907438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        return ret;
47007438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root    }
47107438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root
47207438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root    int64_t getmtime(const String16& name)
47307438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root    {
47407438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        Parcel data, reply;
47507438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        data.writeInterfaceToken(IKeystoreService::getInterfaceDescriptor());
47607438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        data.writeString16(name);
47707438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        status_t status = remote()->transact(BnKeystoreService::GETMTIME, data, &reply);
47807438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        if (status != NO_ERROR) {
47907438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            ALOGD("getmtime() could not contact remote: %d\n", status);
48007438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            return -1;
48107438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        }
48207438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        int32_t err = reply.readExceptionCode();
48307438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        int64_t ret = reply.readInt64();
48407438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        if (err < 0) {
48507438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            ALOGD("getmtime() caught exception %d\n", err);
48607438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            return -1;
48707438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        }
48807438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        return ret;
48907438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root    }
4900225407783ee339164a0cd8ca5ef04c99d27c59aKenny Root
491d53bc92f1cc4eb669ec015480cebe5ae7aaaf7cfKenny Root    virtual int32_t duplicate(const String16& srcKey, int32_t srcUid, const String16& destKey,
492d53bc92f1cc4eb669ec015480cebe5ae7aaaf7cfKenny Root            int32_t destUid)
4930225407783ee339164a0cd8ca5ef04c99d27c59aKenny Root    {
4940225407783ee339164a0cd8ca5ef04c99d27c59aKenny Root        Parcel data, reply;
4950225407783ee339164a0cd8ca5ef04c99d27c59aKenny Root        data.writeInterfaceToken(IKeystoreService::getInterfaceDescriptor());
496d53bc92f1cc4eb669ec015480cebe5ae7aaaf7cfKenny Root        data.writeString16(srcKey);
497d53bc92f1cc4eb669ec015480cebe5ae7aaaf7cfKenny Root        data.writeInt32(srcUid);
498d53bc92f1cc4eb669ec015480cebe5ae7aaaf7cfKenny Root        data.writeString16(destKey);
499d53bc92f1cc4eb669ec015480cebe5ae7aaaf7cfKenny Root        data.writeInt32(destUid);
500d53bc92f1cc4eb669ec015480cebe5ae7aaaf7cfKenny Root        status_t status = remote()->transact(BnKeystoreService::DUPLICATE, data, &reply);
5010225407783ee339164a0cd8ca5ef04c99d27c59aKenny Root        if (status != NO_ERROR) {
502d53bc92f1cc4eb669ec015480cebe5ae7aaaf7cfKenny Root            ALOGD("duplicate() could not contact remote: %d\n", status);
5030225407783ee339164a0cd8ca5ef04c99d27c59aKenny Root            return -1;
5040225407783ee339164a0cd8ca5ef04c99d27c59aKenny Root        }
5050225407783ee339164a0cd8ca5ef04c99d27c59aKenny Root        int32_t err = reply.readExceptionCode();
5060225407783ee339164a0cd8ca5ef04c99d27c59aKenny Root        int32_t ret = reply.readInt32();
5070225407783ee339164a0cd8ca5ef04c99d27c59aKenny Root        if (err < 0) {
508d53bc92f1cc4eb669ec015480cebe5ae7aaaf7cfKenny Root            ALOGD("duplicate() caught exception %d\n", err);
5090225407783ee339164a0cd8ca5ef04c99d27c59aKenny Root            return -1;
5100225407783ee339164a0cd8ca5ef04c99d27c59aKenny Root        }
5110225407783ee339164a0cd8ca5ef04c99d27c59aKenny Root        return ret;
5120225407783ee339164a0cd8ca5ef04c99d27c59aKenny Root    }
51307438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root};
51407438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root
51507438c8d7256d3788dac323b4d0055f201e0bec9Kenny RootIMPLEMENT_META_INTERFACE(KeystoreService, "android.security.keystore");
51607438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root
51707438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root// ----------------------------------------------------------------------
51807438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root
51907438c8d7256d3788dac323b4d0055f201e0bec9Kenny Rootstatus_t BnKeystoreService::onTransact(
52007438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root    uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
52107438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root{
52207438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root    switch(code) {
52307438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        case TEST: {
52407438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            CHECK_INTERFACE(IKeystoreService, data, reply);
52507438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            int32_t ret = test();
52607438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            reply->writeNoException();
52707438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            reply->writeInt32(ret);
52807438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            return NO_ERROR;
52907438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        } break;
53007438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        case GET: {
53107438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            CHECK_INTERFACE(IKeystoreService, data, reply);
53207438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            String16 name = data.readString16();
53307438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            void* out = NULL;
53407438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            size_t outSize = 0;
53507438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            int32_t ret = get(name, (uint8_t**) &out, &outSize);
53607438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            reply->writeNoException();
53707438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            if (ret == 1) {
53807438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root                reply->writeInt32(outSize);
53907438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root                void* buf = reply->writeInplace(outSize);
54007438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root                memcpy(buf, out, outSize);
54107438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root                free(out);
54207438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            } else {
54307438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root                reply->writeInt32(-1);
54407438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            }
54507438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            return NO_ERROR;
54607438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        } break;
54707438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        case INSERT: {
54807438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            CHECK_INTERFACE(IKeystoreService, data, reply);
54907438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            String16 name = data.readString16();
55007438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            ssize_t inSize = data.readInt32();
55107438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            const void* in;
55207438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            if (inSize >= 0 && (size_t) inSize <= data.dataAvail()) {
55307438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root                in = data.readInplace(inSize);
55407438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            } else {
55507438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root                in = NULL;
55607438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root                inSize = 0;
55707438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            }
558b88c3eb96625513df4cc998d739d17266ebaf89fKenny Root            int uid = data.readInt32();
559b88c3eb96625513df4cc998d739d17266ebaf89fKenny Root            int32_t ret = insert(name, (const uint8_t*) in, (size_t) inSize, uid);
56007438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            reply->writeNoException();
56107438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            reply->writeInt32(ret);
56207438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            return NO_ERROR;
56307438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        } break;
56407438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        case DEL: {
56507438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            CHECK_INTERFACE(IKeystoreService, data, reply);
56607438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            String16 name = data.readString16();
567b88c3eb96625513df4cc998d739d17266ebaf89fKenny Root            int uid = data.readInt32();
568b88c3eb96625513df4cc998d739d17266ebaf89fKenny Root            int32_t ret = del(name, uid);
56907438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            reply->writeNoException();
57007438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            reply->writeInt32(ret);
57107438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            return NO_ERROR;
57207438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        } break;
57307438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        case EXIST: {
57407438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            CHECK_INTERFACE(IKeystoreService, data, reply);
57507438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            String16 name = data.readString16();
576b88c3eb96625513df4cc998d739d17266ebaf89fKenny Root            int uid = data.readInt32();
577b88c3eb96625513df4cc998d739d17266ebaf89fKenny Root            int32_t ret = exist(name, uid);
57807438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            reply->writeNoException();
57907438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            reply->writeInt32(ret);
58007438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            return NO_ERROR;
58107438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        } break;
58207438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        case SAW: {
58307438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            CHECK_INTERFACE(IKeystoreService, data, reply);
58407438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            String16 name = data.readString16();
585b88c3eb96625513df4cc998d739d17266ebaf89fKenny Root            int uid = data.readInt32();
58607438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            Vector<String16> matches;
587b88c3eb96625513df4cc998d739d17266ebaf89fKenny Root            int32_t ret = saw(name, uid, &matches);
58807438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            reply->writeNoException();
58907438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            reply->writeInt32(matches.size());
59007438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            Vector<String16>::const_iterator it = matches.begin();
59107438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            for (; it != matches.end(); ++it) {
59207438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root                reply->writeString16(*it);
59307438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            }
59407438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            reply->writeInt32(ret);
59507438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            return NO_ERROR;
59607438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        } break;
59707438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        case RESET: {
59807438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            CHECK_INTERFACE(IKeystoreService, data, reply);
59907438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            int32_t ret = reset();
60007438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            reply->writeNoException();
60107438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            reply->writeInt32(ret);
60207438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            return NO_ERROR;
60307438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        } break;
60407438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        case PASSWORD: {
60507438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            CHECK_INTERFACE(IKeystoreService, data, reply);
60607438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            String16 pass = data.readString16();
60707438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            int32_t ret = password(pass);
60807438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            reply->writeNoException();
60907438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            reply->writeInt32(ret);
61007438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            return NO_ERROR;
61107438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        } break;
61207438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        case LOCK: {
61307438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            CHECK_INTERFACE(IKeystoreService, data, reply);
61407438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            int32_t ret = lock();
61507438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            reply->writeNoException();
61607438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            reply->writeInt32(ret);
61707438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            return NO_ERROR;
61807438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        } break;
61907438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        case UNLOCK: {
62007438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            CHECK_INTERFACE(IKeystoreService, data, reply);
62107438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            String16 pass = data.readString16();
62207438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            int32_t ret = unlock(pass);
62307438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            reply->writeNoException();
62407438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            reply->writeInt32(ret);
62507438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            return NO_ERROR;
62607438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        } break;
62707438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        case ZERO: {
62807438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            CHECK_INTERFACE(IKeystoreService, data, reply);
62907438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            int32_t ret = zero();
63007438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            reply->writeNoException();
63107438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            reply->writeInt32(ret);
63207438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            return NO_ERROR;
63307438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        } break;
63407438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        case GENERATE: {
63507438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            CHECK_INTERFACE(IKeystoreService, data, reply);
63607438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            String16 name = data.readString16();
637b88c3eb96625513df4cc998d739d17266ebaf89fKenny Root            int uid = data.readInt32();
638b88c3eb96625513df4cc998d739d17266ebaf89fKenny Root            int32_t ret = generate(name, uid);
63907438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            reply->writeNoException();
64007438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            reply->writeInt32(ret);
64107438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            return NO_ERROR;
64207438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        } break;
64307438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        case IMPORT: {
64407438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            CHECK_INTERFACE(IKeystoreService, data, reply);
64507438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            String16 name = data.readString16();
64607438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            ssize_t inSize = data.readInt32();
64707438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            const void* in;
64807438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            if (inSize >= 0 && (size_t) inSize <= data.dataAvail()) {
64907438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root                in = data.readInplace(inSize);
65007438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            } else {
65107438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root                in = NULL;
65207438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root                inSize = 0;
65307438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            }
654b88c3eb96625513df4cc998d739d17266ebaf89fKenny Root            int uid = data.readInt32();
655b88c3eb96625513df4cc998d739d17266ebaf89fKenny Root            int32_t ret = import(name, (const uint8_t*) in, (size_t) inSize, uid);
65607438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            reply->writeNoException();
65707438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            reply->writeInt32(ret);
65807438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            return NO_ERROR;
65907438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        } break;
66007438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        case SIGN: {
66107438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            CHECK_INTERFACE(IKeystoreService, data, reply);
66207438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            String16 name = data.readString16();
66307438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            ssize_t inSize = data.readInt32();
66407438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            const void* in;
66507438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            if (inSize >= 0 && (size_t) inSize <= data.dataAvail()) {
66607438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root                in = data.readInplace(inSize);
66707438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            } else {
66807438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root                in = NULL;
66907438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root                inSize = 0;
67007438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            }
67107438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            void* out = NULL;
67207438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            size_t outSize = 0;
67307438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            int32_t ret = sign(name, (const uint8_t*) in, (size_t) inSize, (uint8_t**) &out, &outSize);
67407438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            reply->writeNoException();
675b03c9fb5f9c058a8ae0485c986a8ab934ab73eaaKenny Root            if (outSize > 0 && out != NULL) {
676b03c9fb5f9c058a8ae0485c986a8ab934ab73eaaKenny Root                reply->writeInt32(outSize);
677b03c9fb5f9c058a8ae0485c986a8ab934ab73eaaKenny Root                void* buf = reply->writeInplace(outSize);
678b03c9fb5f9c058a8ae0485c986a8ab934ab73eaaKenny Root                memcpy(buf, out, outSize);
679b03c9fb5f9c058a8ae0485c986a8ab934ab73eaaKenny Root                free(out);
680b03c9fb5f9c058a8ae0485c986a8ab934ab73eaaKenny Root            } else {
681e289c404b9d2735fbd67c42086e33c972b46aa33Kenny Root                reply->writeInt32(-1);
682b03c9fb5f9c058a8ae0485c986a8ab934ab73eaaKenny Root            }
68307438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            reply->writeInt32(ret);
68407438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            return NO_ERROR;
68507438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        } break;
68607438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        case VERIFY: {
68707438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            CHECK_INTERFACE(IKeystoreService, data, reply);
68807438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            String16 name = data.readString16();
68907438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            ssize_t inSize = data.readInt32();
69007438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            const void* in;
69107438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            if (inSize >= 0 && (size_t) inSize <= data.dataAvail()) {
69207438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root                in = data.readInplace(inSize);
69307438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            } else {
69407438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root                in = NULL;
69507438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root                inSize = 0;
69607438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            }
69707438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            ssize_t sigSize = data.readInt32();
69807438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            const void* sig;
69907438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            if (sigSize >= 0 && (size_t) sigSize <= data.dataAvail()) {
70007438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root                sig = data.readInplace(sigSize);
70107438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            } else {
70207438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root                sig = NULL;
70307438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root                sigSize = 0;
70407438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            }
70507438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            bool ret = verify(name, (const uint8_t*) in, (size_t) inSize, (const uint8_t*) sig,
70607438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root                    (size_t) sigSize);
70707438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            reply->writeNoException();
70807438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            reply->writeInt32(ret ? 1 : 0);
70907438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            return NO_ERROR;
71007438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        } break;
71107438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        case GET_PUBKEY: {
71207438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            CHECK_INTERFACE(IKeystoreService, data, reply);
71307438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            String16 name = data.readString16();
71407438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            void* out = NULL;
71507438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            size_t outSize = 0;
71607438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            int32_t ret = get_pubkey(name, (unsigned char**) &out, &outSize);
71707438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            reply->writeNoException();
718b03c9fb5f9c058a8ae0485c986a8ab934ab73eaaKenny Root            if (outSize > 0 && out != NULL) {
719b03c9fb5f9c058a8ae0485c986a8ab934ab73eaaKenny Root                reply->writeInt32(outSize);
720b03c9fb5f9c058a8ae0485c986a8ab934ab73eaaKenny Root                void* buf = reply->writeInplace(outSize);
721b03c9fb5f9c058a8ae0485c986a8ab934ab73eaaKenny Root                memcpy(buf, out, outSize);
722b03c9fb5f9c058a8ae0485c986a8ab934ab73eaaKenny Root                free(out);
723b03c9fb5f9c058a8ae0485c986a8ab934ab73eaaKenny Root            } else {
724e289c404b9d2735fbd67c42086e33c972b46aa33Kenny Root                reply->writeInt32(-1);
725b03c9fb5f9c058a8ae0485c986a8ab934ab73eaaKenny Root            }
72607438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            reply->writeInt32(ret);
72707438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            return NO_ERROR;
728b03c9fb5f9c058a8ae0485c986a8ab934ab73eaaKenny Root        } break;
72907438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        case DEL_KEY: {
73007438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            CHECK_INTERFACE(IKeystoreService, data, reply);
73107438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            String16 name = data.readString16();
732b88c3eb96625513df4cc998d739d17266ebaf89fKenny Root            int uid = data.readInt32();
733b88c3eb96625513df4cc998d739d17266ebaf89fKenny Root            int32_t ret = del_key(name, uid);
73407438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            reply->writeNoException();
73507438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            reply->writeInt32(ret);
73607438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            return NO_ERROR;
73707438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        } break;
73807438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        case GRANT: {
73907438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            CHECK_INTERFACE(IKeystoreService, data, reply);
74007438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            String16 name = data.readString16();
74107438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            int32_t granteeUid = data.readInt32();
74207438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            int32_t ret = grant(name, granteeUid);
74307438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            reply->writeNoException();
74407438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            reply->writeInt32(ret);
74507438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            return NO_ERROR;
74607438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        } break;
74707438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        case UNGRANT: {
74807438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            CHECK_INTERFACE(IKeystoreService, data, reply);
74907438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            String16 name = data.readString16();
75007438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            int32_t granteeUid = data.readInt32();
75107438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            int32_t ret = ungrant(name, granteeUid);
75207438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            reply->writeNoException();
75307438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            reply->writeInt32(ret);
75407438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            return NO_ERROR;
75507438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        } break;
75607438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        case GETMTIME: {
75707438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            CHECK_INTERFACE(IKeystoreService, data, reply);
75807438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            String16 name = data.readString16();
75907438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            int64_t ret = getmtime(name);
76007438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            reply->writeNoException();
76107438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            reply->writeInt64(ret);
76207438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            return NO_ERROR;
76307438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        } break;
764d53bc92f1cc4eb669ec015480cebe5ae7aaaf7cfKenny Root        case DUPLICATE: {
7650225407783ee339164a0cd8ca5ef04c99d27c59aKenny Root            CHECK_INTERFACE(IKeystoreService, data, reply);
766d53bc92f1cc4eb669ec015480cebe5ae7aaaf7cfKenny Root            String16 srcKey = data.readString16();
767d53bc92f1cc4eb669ec015480cebe5ae7aaaf7cfKenny Root            int32_t srcUid = data.readInt32();
768d53bc92f1cc4eb669ec015480cebe5ae7aaaf7cfKenny Root            String16 destKey = data.readString16();
769d53bc92f1cc4eb669ec015480cebe5ae7aaaf7cfKenny Root            int32_t destUid = data.readInt32();
770d53bc92f1cc4eb669ec015480cebe5ae7aaaf7cfKenny Root            int32_t ret = duplicate(srcKey, srcUid, destKey, destUid);
7710225407783ee339164a0cd8ca5ef04c99d27c59aKenny Root            reply->writeNoException();
7720225407783ee339164a0cd8ca5ef04c99d27c59aKenny Root            reply->writeInt32(ret);
7730225407783ee339164a0cd8ca5ef04c99d27c59aKenny Root            return NO_ERROR;
7740225407783ee339164a0cd8ca5ef04c99d27c59aKenny Root        } break;
77507438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root        default:
77607438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root            return BBinder::onTransact(code, data, reply, flags);
77707438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root    }
77807438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root}
77907438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root
78007438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root// ----------------------------------------------------------------------------
78107438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root
78207438c8d7256d3788dac323b4d0055f201e0bec9Kenny Root}; // namespace android
783