blob: 8fb7f80d0269a6fa03314a1b834058580d6654f6 [file] [log] [blame]
Kenny Root07438c82012-11-02 15:41:02 -07001/*
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
22using namespace android;
Janis Danisevskisc7a9fa22016-10-13 18:43:45 +010023using namespace keystore;
Kenny Root07438c82012-11-02 15:41:02 -070024
25ssize_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
Janis Danisevskisc7a9fa22016-10-13 18:43:45 +010034 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());
Kenny Root07438c82012-11-02 15:41:02 -070042 }
Janis Danisevskisc7a9fa22016-10-13 18:43:45 +010043 return result.size();
44
Kenny Root07438c82012-11-02 15:41:02 -070045}