blob: f9e7be0b6a6481605eab6f1ec95b155f3146f4f7 [file] [log] [blame]
Paul Stewartac0ffbf2017-03-03 16:43:33 -08001/* Copyright 2017 The Android Open Source Project
2 *
3 * Redistribution and use in source and binary forms, with or without
4 * modification, are permitted provided that the following conditions
5 * are met:
6 * 1. Redistributions of source code must retain the above copyright
7 * notice, this list of conditions and the following disclaimer.
8 * 2. Redistributions in binary form must reproduce the above copyright
9 * notice, this list of conditions and the following disclaimer in the
10 * documentation and/or other materials provided with the distribution.
11 *
12 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND ANY
13 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
14 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
15 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR ANY
16 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
17 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
18 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
19 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
20 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
21 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */
22
23#include "keystore_backend_binder.h"
24
Dmitry Dementyeva447b3c2017-10-27 23:09:53 -070025#include <android/security/IKeystoreService.h>
Paul Stewartac0ffbf2017-03-03 16:43:33 -080026#include <binder/IServiceManager.h>
27#include <keystore/keystore.h>
Paul Stewartac0ffbf2017-03-03 16:43:33 -080028#include <keystore/keystore_hidl_support.h>
29
Dmitry Dementyeva447b3c2017-10-27 23:09:53 -070030using android::security::IKeystoreService;
Paul Stewartac0ffbf2017-03-03 16:43:33 -080031using namespace android;
32using keystore::blob2hidlVec;
33using keystore::hidl_vec;
34
35namespace {
36const char keystore_service_name[] = "android.security.keystore";
37};
38
Dmitry Dementyeva447b3c2017-10-27 23:09:53 -070039int32_t KeystoreBackendBinder::sign(const char* key_id, const uint8_t* in, size_t len,
40 uint8_t** reply, size_t* reply_len) {
Paul Stewartac0ffbf2017-03-03 16:43:33 -080041 sp<IServiceManager> sm = defaultServiceManager();
42 sp<IBinder> binder = sm->getService(String16(keystore_service_name));
43 sp<IKeystoreService> service = interface_cast<IKeystoreService>(binder);
44
45 if (service == NULL) {
46 ALOGE("could not contact keystore");
47 return -1;
48 }
49
Dmitry Dementyeva447b3c2017-10-27 23:09:53 -070050 auto inBlob = blob2hidlVec(in, len);
51 std::vector<uint8_t> reply_vec;
Paul Stewartac0ffbf2017-03-03 16:43:33 -080052 auto ret = service->sign(String16(key_id), inBlob, &reply_vec);
53 if (!ret.isOk()) {
54 return -1;
55 }
56
Dmitry Dementyeva447b3c2017-10-27 23:09:53 -070057 hidl_vec<uint8_t> reply_hidl(reply_vec); // makes copy
58 *reply = reply_hidl.releaseData();
Paul Stewartac0ffbf2017-03-03 16:43:33 -080059 *reply_len = reply_vec.size();
60 return 0;
61}
62
Dmitry Dementyeva447b3c2017-10-27 23:09:53 -070063int32_t KeystoreBackendBinder::get_pubkey(const char* key_id, uint8_t** pubkey,
64 size_t* pubkey_len) {
Paul Stewartac0ffbf2017-03-03 16:43:33 -080065 sp<IServiceManager> sm = defaultServiceManager();
66 sp<IBinder> binder = sm->getService(String16(keystore_service_name));
67 sp<IKeystoreService> service = interface_cast<IKeystoreService>(binder);
68
69 if (service == NULL) {
70 ALOGE("could not contact keystore");
71 return -1;
72 }
73
Dmitry Dementyeva447b3c2017-10-27 23:09:53 -070074 std::vector<uint8_t> pubkey_vec;
Paul Stewartac0ffbf2017-03-03 16:43:33 -080075 auto ret = service->get_pubkey(String16(key_id), &pubkey_vec);
76 if (!ret.isOk()) {
77 return -1;
78 }
79
Dmitry Dementyeva447b3c2017-10-27 23:09:53 -070080 hidl_vec<uint8_t> hidl_pubkey(pubkey_vec); // makes copy
81 *pubkey = hidl_pubkey.releaseData(); // caller should clean up memory.
Paul Stewartac0ffbf2017-03-03 16:43:33 -080082 *pubkey_len = pubkey_vec.size();
83 return 0;
84}