• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <keystore/IKeystoreService.h>
18 #include <binder/IServiceManager.h>
19 
20 #include <keystore/keystore_get.h>
21 
22 using namespace android;
23 using namespace keystore;
24 
keystore_get(const char * key,size_t keyLength,uint8_t ** value)25 ssize_t keystore_get(const char *key, size_t keyLength, uint8_t** value) {
26     sp<IServiceManager> sm = defaultServiceManager();
27     sp<IBinder> binder = sm->getService(String16("android.security.keystore"));
28     sp<IKeystoreService> service = interface_cast<IKeystoreService>(binder);
29 
30     if (service == NULL) {
31         return -1;
32     }
33 
34     hidl_vec<uint8_t> result;
35     auto ret = service->get(String16(key, keyLength), -1, &result);
36     if (!ret.isOk()) return -1;
37 
38     if (value) {
39         *value = reinterpret_cast<uint8_t*>(malloc(result.size()));
40         if (!*value) return -1;
41         memcpy(*value, &result[0], result.size());
42     }
43     return result.size();
44 
45 }
46