1 /* 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 <android/system/wifi/keystore/1.0/IKeystore.h>
24 #include <log/log.h>
25 
26 #include <keystore/keystore_get.h>
27 
28 using namespace android;
29 
30 using android::hardware::hidl_string;
31 using android::hardware::hidl_vec;
32 using android::hardware::Return;
33 using android::sp;
34 using android::system::wifi::keystore::V1_0::IKeystore;
35 
keystore_get(const char * key,size_t keyLength,uint8_t ** value)36 ssize_t keystore_get(const char *key, size_t keyLength, uint8_t** value) {
37     if (key == NULL || keyLength == 0 || value == NULL) {
38         ALOGE("Null pointer argument passed");
39         return -1;
40     }
41 
42     sp<IKeystore> service = IKeystore::tryGetService();
43     if (service == NULL) {
44         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 }
61