blob: a6f8755706a6add89bba0766ce230f0485a2d66e [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
Rob Barnesbb6cabd2018-10-04 17:10:37 -060017#include <android/security/keystore/IKeystoreService.h>
Kenny Root07438c82012-11-02 15:41:02 -070018#include <binder/IServiceManager.h>
19
20#include <keystore/keystore_get.h>
Dmitry Dementyeva447b3c2017-10-27 23:09:53 -070021#include <vector>
Kenny Root07438c82012-11-02 15:41:02 -070022
23using namespace android;
Janis Danisevskisc7a9fa22016-10-13 18:43:45 +010024using namespace keystore;
Kenny Root07438c82012-11-02 15:41:02 -070025
Dmitry Dementyeva447b3c2017-10-27 23:09:53 -070026ssize_t keystore_get(const char* key, size_t keyLength, uint8_t** value) {
Kenny Root07438c82012-11-02 15:41:02 -070027 sp<IServiceManager> sm = defaultServiceManager();
28 sp<IBinder> binder = sm->getService(String16("android.security.keystore"));
Rob Barnesbb6cabd2018-10-04 17:10:37 -060029 sp<android::security::keystore::IKeystoreService> service =
30 interface_cast<android::security::keystore::IKeystoreService>(binder);
Kenny Root07438c82012-11-02 15:41:02 -070031
Yi Konge353f252018-07-30 01:38:39 -070032 if (service == nullptr) {
Kenny Root07438c82012-11-02 15:41:02 -070033 return -1;
34 }
35
Dmitry Dementyeva447b3c2017-10-27 23:09:53 -070036 ::std::vector<uint8_t> result;
Janis Danisevskisc7a9fa22016-10-13 18:43:45 +010037 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());
Kenny Root07438c82012-11-02 15:41:02 -070044 }
Janis Danisevskisc7a9fa22016-10-13 18:43:45 +010045 return result.size();
Kenny Root07438c82012-11-02 15:41:02 -070046}