blob: b00cf4e346e73b4ec273b67b4250ba595b45862e [file] [log] [blame]
Paul Stewart657356c2017-03-09 00:00:23 -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_hidl.h"
24
25#include <android/system/wifi/keystore/1.0/IKeystore.h>
26
27using android::hardware::hidl_vec;
28using android::hardware::Return;
29using android::sp;
30using android::system::wifi::keystore::V1_0::IKeystore;
31
32int32_t KeystoreBackendHidl::sign(
33 const char *key_id, const uint8_t* in, size_t len, uint8_t** reply,
34 size_t* reply_len) {
35 if (key_id == NULL || in == NULL || reply == NULL || reply_len == NULL) {
36 ALOGE("Null pointer argument passed");
37 return -1;
38 }
39
Roshan Piuse653c932017-03-29 10:08:47 -070040 sp<IKeystore> service = IKeystore::tryGetService();
Paul Stewart657356c2017-03-09 00:00:23 -080041 if (service == NULL) {
Paul Stewartbf7fc8d2017-03-10 16:22:03 -080042 ALOGE("could not contact keystore HAL");
Paul Stewart657356c2017-03-09 00:00:23 -080043 return -1;
44 }
45
46 bool success = false;
47 auto cb = [&](IKeystore::KeystoreStatusCode status,
48 hidl_vec<uint8_t> signedData) {
49 if (status == IKeystore::KeystoreStatusCode::SUCCESS) {
50 *reply_len = signedData.size();
51 *reply = signedData.releaseData();
52 success = true;
53 }
54 };
55 Return<void> ret = service->sign(
56 key_id, std::vector<uint8_t>(in, in + len), cb);
Roshan Pius30b220e2017-03-31 16:47:04 -070057 if (!ret.isOk() || !success) {
58 return 1;
59 }
60 return 0;
Paul Stewart657356c2017-03-09 00:00:23 -080061}
62
63int32_t KeystoreBackendHidl::get_pubkey(
64 const char *key_id, uint8_t** pubkey, size_t* pubkey_len) {
65 if (key_id == NULL || pubkey == NULL || pubkey_len == NULL) {
66 ALOGE("Null pointer argument passed");
67 return -1;
68 }
69
Roshan Piuse653c932017-03-29 10:08:47 -070070 sp<IKeystore> service = IKeystore::tryGetService();
Paul Stewart657356c2017-03-09 00:00:23 -080071 if (service == NULL) {
Paul Stewartbf7fc8d2017-03-10 16:22:03 -080072 ALOGE("could not contact keystore HAL");
Paul Stewart657356c2017-03-09 00:00:23 -080073 return -1;
74 }
75
76 bool success = false;
77 auto cb = [&](IKeystore::KeystoreStatusCode status,
78 hidl_vec<uint8_t> publicKey) {
79 if (status == IKeystore::KeystoreStatusCode::SUCCESS) {
80 *pubkey_len = publicKey.size();
81 *pubkey = publicKey.releaseData();
82 success = true;
83 }
84 };
85 Return<void> ret = service->getPublicKey(key_id, cb);
Roshan Pius30b220e2017-03-31 16:47:04 -070086 if (!ret.isOk() || !success) {
87 return 1;
88 }
89 return 0;
Paul Stewart657356c2017-03-09 00:00:23 -080090}