1 /*
2  * Copyright (C) 2012 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include <android/security/keystore/IKeystoreService.h>
18 #include <binder/IServiceManager.h>
19 
20 #include <keystore/keystore_get.h>
21 #include <vector>
22 
23 using namespace android;
24 using namespace keystore;
25 
keystore_get(const char * key,size_t keyLength,uint8_t ** value)26 ssize_t keystore_get(const char* key, size_t keyLength, uint8_t** value) {
27     sp<IServiceManager> sm = defaultServiceManager();
28     sp<IBinder> binder = sm->getService(String16("android.security.keystore"));
29     sp<android::security::keystore::IKeystoreService> service =
30         interface_cast<android::security::keystore::IKeystoreService>(binder);
31 
32     if (service == nullptr) {
33         return -1;
34     }
35 
36     ::std::vector<uint8_t> result;
37     auto ret = service->get(String16(key, keyLength), -1, &result);
38     if (!ret.isOk()) return -1;
39 
40     if (value) {
41         *value = reinterpret_cast<uint8_t*>(malloc(result.size()));
42         if (!*value) return -1;
43         memcpy(*value, &result[0], result.size());
44     }
45     return result.size();
46 }
47