1ac0ffbf62c8f87f1b42f660979b4213d429e51ccPaul Stewart/* Copyright 2017 The Android Open Source Project
2ac0ffbf62c8f87f1b42f660979b4213d429e51ccPaul Stewart *
3ac0ffbf62c8f87f1b42f660979b4213d429e51ccPaul Stewart * Redistribution and use in source and binary forms, with or without
4ac0ffbf62c8f87f1b42f660979b4213d429e51ccPaul Stewart * modification, are permitted provided that the following conditions
5ac0ffbf62c8f87f1b42f660979b4213d429e51ccPaul Stewart * are met:
6ac0ffbf62c8f87f1b42f660979b4213d429e51ccPaul Stewart * 1. Redistributions of source code must retain the above copyright
7ac0ffbf62c8f87f1b42f660979b4213d429e51ccPaul Stewart *    notice, this list of conditions and the following disclaimer.
8ac0ffbf62c8f87f1b42f660979b4213d429e51ccPaul Stewart * 2. Redistributions in binary form must reproduce the above copyright
9ac0ffbf62c8f87f1b42f660979b4213d429e51ccPaul Stewart *    notice, this list of conditions and the following disclaimer in the
10ac0ffbf62c8f87f1b42f660979b4213d429e51ccPaul Stewart *    documentation and/or other materials provided with the distribution.
11ac0ffbf62c8f87f1b42f660979b4213d429e51ccPaul Stewart *
12ac0ffbf62c8f87f1b42f660979b4213d429e51ccPaul Stewart * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND ANY
13ac0ffbf62c8f87f1b42f660979b4213d429e51ccPaul Stewart * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
14ac0ffbf62c8f87f1b42f660979b4213d429e51ccPaul Stewart * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
15ac0ffbf62c8f87f1b42f660979b4213d429e51ccPaul Stewart * DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR ANY
16ac0ffbf62c8f87f1b42f660979b4213d429e51ccPaul Stewart * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
17ac0ffbf62c8f87f1b42f660979b4213d429e51ccPaul Stewart * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
18ac0ffbf62c8f87f1b42f660979b4213d429e51ccPaul Stewart * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
19ac0ffbf62c8f87f1b42f660979b4213d429e51ccPaul Stewart * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
20ac0ffbf62c8f87f1b42f660979b4213d429e51ccPaul Stewart * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
21ac0ffbf62c8f87f1b42f660979b4213d429e51ccPaul Stewart * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */
22ac0ffbf62c8f87f1b42f660979b4213d429e51ccPaul Stewart
23ac0ffbf62c8f87f1b42f660979b4213d429e51ccPaul Stewart#include "keystore_backend_binder.h"
24ac0ffbf62c8f87f1b42f660979b4213d429e51ccPaul Stewart
25ac0ffbf62c8f87f1b42f660979b4213d429e51ccPaul Stewart#include <binder/IServiceManager.h>
26ac0ffbf62c8f87f1b42f660979b4213d429e51ccPaul Stewart#include <keystore/keystore.h>
27ac0ffbf62c8f87f1b42f660979b4213d429e51ccPaul Stewart#include <keystore/IKeystoreService.h>
28ac0ffbf62c8f87f1b42f660979b4213d429e51ccPaul Stewart#include <keystore/keystore_hidl_support.h>
29ac0ffbf62c8f87f1b42f660979b4213d429e51ccPaul Stewart
30ac0ffbf62c8f87f1b42f660979b4213d429e51ccPaul Stewartusing namespace android;
31ac0ffbf62c8f87f1b42f660979b4213d429e51ccPaul Stewartusing keystore::blob2hidlVec;
32ac0ffbf62c8f87f1b42f660979b4213d429e51ccPaul Stewartusing keystore::hidl_vec;
33ac0ffbf62c8f87f1b42f660979b4213d429e51ccPaul Stewart
34ac0ffbf62c8f87f1b42f660979b4213d429e51ccPaul Stewartnamespace {
35ac0ffbf62c8f87f1b42f660979b4213d429e51ccPaul Stewartconst char keystore_service_name[] = "android.security.keystore";
36ac0ffbf62c8f87f1b42f660979b4213d429e51ccPaul Stewart};
37ac0ffbf62c8f87f1b42f660979b4213d429e51ccPaul Stewart
38ac0ffbf62c8f87f1b42f660979b4213d429e51ccPaul Stewartint32_t KeystoreBackendBinder::sign(
39ac0ffbf62c8f87f1b42f660979b4213d429e51ccPaul Stewart        const char *key_id, const uint8_t* in, size_t len, uint8_t** reply,
40ac0ffbf62c8f87f1b42f660979b4213d429e51ccPaul Stewart        size_t* reply_len) {
41ac0ffbf62c8f87f1b42f660979b4213d429e51ccPaul Stewart    sp<IServiceManager> sm = defaultServiceManager();
42ac0ffbf62c8f87f1b42f660979b4213d429e51ccPaul Stewart    sp<IBinder> binder = sm->getService(String16(keystore_service_name));
43ac0ffbf62c8f87f1b42f660979b4213d429e51ccPaul Stewart    sp<IKeystoreService> service = interface_cast<IKeystoreService>(binder);
44ac0ffbf62c8f87f1b42f660979b4213d429e51ccPaul Stewart
45ac0ffbf62c8f87f1b42f660979b4213d429e51ccPaul Stewart    if (service == NULL) {
46ac0ffbf62c8f87f1b42f660979b4213d429e51ccPaul Stewart        ALOGE("could not contact keystore");
47ac0ffbf62c8f87f1b42f660979b4213d429e51ccPaul Stewart        return -1;
48ac0ffbf62c8f87f1b42f660979b4213d429e51ccPaul Stewart    }
49ac0ffbf62c8f87f1b42f660979b4213d429e51ccPaul Stewart
50ac0ffbf62c8f87f1b42f660979b4213d429e51ccPaul Stewart    auto inBlob = blob2hidlVec(in ,len);
51ac0ffbf62c8f87f1b42f660979b4213d429e51ccPaul Stewart    hidl_vec<uint8_t> reply_vec;
52ac0ffbf62c8f87f1b42f660979b4213d429e51ccPaul Stewart    auto ret = service->sign(String16(key_id), inBlob, &reply_vec);
53ac0ffbf62c8f87f1b42f660979b4213d429e51ccPaul Stewart    if (!ret.isOk()) {
54ac0ffbf62c8f87f1b42f660979b4213d429e51ccPaul Stewart        return -1;
55ac0ffbf62c8f87f1b42f660979b4213d429e51ccPaul Stewart    }
56ac0ffbf62c8f87f1b42f660979b4213d429e51ccPaul Stewart
57bf7fc8df768203bb2812fd1e28b8dab23560e381Paul Stewart    *reply = reply_vec.releaseData();
58ac0ffbf62c8f87f1b42f660979b4213d429e51ccPaul Stewart    *reply_len = reply_vec.size();
59ac0ffbf62c8f87f1b42f660979b4213d429e51ccPaul Stewart    return 0;
60ac0ffbf62c8f87f1b42f660979b4213d429e51ccPaul Stewart}
61ac0ffbf62c8f87f1b42f660979b4213d429e51ccPaul Stewart
62ac0ffbf62c8f87f1b42f660979b4213d429e51ccPaul Stewartint32_t KeystoreBackendBinder::get_pubkey(
63ac0ffbf62c8f87f1b42f660979b4213d429e51ccPaul Stewart        const char *key_id, uint8_t** pubkey, size_t* pubkey_len) {
64ac0ffbf62c8f87f1b42f660979b4213d429e51ccPaul Stewart    sp<IServiceManager> sm = defaultServiceManager();
65ac0ffbf62c8f87f1b42f660979b4213d429e51ccPaul Stewart    sp<IBinder> binder = sm->getService(String16(keystore_service_name));
66ac0ffbf62c8f87f1b42f660979b4213d429e51ccPaul Stewart    sp<IKeystoreService> service = interface_cast<IKeystoreService>(binder);
67ac0ffbf62c8f87f1b42f660979b4213d429e51ccPaul Stewart
68ac0ffbf62c8f87f1b42f660979b4213d429e51ccPaul Stewart    if (service == NULL) {
69ac0ffbf62c8f87f1b42f660979b4213d429e51ccPaul Stewart        ALOGE("could not contact keystore");
70ac0ffbf62c8f87f1b42f660979b4213d429e51ccPaul Stewart        return -1;
71ac0ffbf62c8f87f1b42f660979b4213d429e51ccPaul Stewart    }
72ac0ffbf62c8f87f1b42f660979b4213d429e51ccPaul Stewart
73ac0ffbf62c8f87f1b42f660979b4213d429e51ccPaul Stewart    hidl_vec<uint8_t> pubkey_vec;
74ac0ffbf62c8f87f1b42f660979b4213d429e51ccPaul Stewart    auto ret = service->get_pubkey(String16(key_id), &pubkey_vec);
75ac0ffbf62c8f87f1b42f660979b4213d429e51ccPaul Stewart    if (!ret.isOk()) {
76ac0ffbf62c8f87f1b42f660979b4213d429e51ccPaul Stewart        return -1;
77ac0ffbf62c8f87f1b42f660979b4213d429e51ccPaul Stewart    }
78ac0ffbf62c8f87f1b42f660979b4213d429e51ccPaul Stewart
79bf7fc8df768203bb2812fd1e28b8dab23560e381Paul Stewart    *pubkey = pubkey_vec.releaseData();
80ac0ffbf62c8f87f1b42f660979b4213d429e51ccPaul Stewart    *pubkey_len = pubkey_vec.size();
81ac0ffbf62c8f87f1b42f660979b4213d429e51ccPaul Stewart    return 0;
82ac0ffbf62c8f87f1b42f660979b4213d429e51ccPaul Stewart}
83