blob: 155201fd1a207a5ea54a19626ecc1632ea1103bd [file] [log] [blame]
Paul Stewartbf7fc8d2017-03-10 16:22:03 -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
Paul Stewartbf7fc8d2017-03-10 16:22:03 -080023#include <android/system/wifi/keystore/1.0/IKeystore.h>
Steven Moreland4cb6f382017-04-06 11:48:19 -070024#include <log/log.h>
Paul Stewartbf7fc8d2017-03-10 16:22:03 -080025
26#include <keystore/keystore_get.h>
27
28using namespace android;
29
30using android::hardware::hidl_string;
31using android::hardware::hidl_vec;
32using android::hardware::Return;
33using android::sp;
34using android::system::wifi::keystore::V1_0::IKeystore;
35
36ssize_t keystore_get(const char *key, size_t keyLength, uint8_t** value) {
Yi Konge353f252018-07-30 01:38:39 -070037 if (key == nullptr || keyLength == 0 || value == nullptr) {
Paul Stewartbf7fc8d2017-03-10 16:22:03 -080038 ALOGE("Null pointer argument passed");
39 return -1;
40 }
41
Roshan Piuse653c932017-03-29 10:08:47 -070042 sp<IKeystore> service = IKeystore::tryGetService();
Yi Konge353f252018-07-30 01:38:39 -070043 if (service == nullptr) {
Paul Stewartbf7fc8d2017-03-10 16:22:03 -080044 ALOGE("could not contact keystore HAL");
45 return -1;
46 }
47
48 ssize_t return_size;
49 bool success = false;
50 auto cb = [&](IKeystore::KeystoreStatusCode status, hidl_vec<uint8_t> returnedValue) {
51 if (status == IKeystore::KeystoreStatusCode::SUCCESS) {
52 return_size = returnedValue.size();
53 *value = returnedValue.releaseData();
54 success = true;
55 }
56 };
57
58 Return<void> ret = service->getBlob(hidl_string(key, keyLength), cb);
59 return ret.isOk() && success ? return_size : -1;
60}