| Kenny Root | 07438c8 | 2012-11-02 15:41:02 -0700 | [diff] [blame] | 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 |  | 
| Rob Barnes | bb6cabd | 2018-10-04 17:10:37 -0600 | [diff] [blame] | 17 | #include <android/security/keystore/IKeystoreService.h> | 
| Kenny Root | 07438c8 | 2012-11-02 15:41:02 -0700 | [diff] [blame] | 18 | #include <binder/IServiceManager.h> | 
|  | 19 |  | 
|  | 20 | #include <keystore/keystore_get.h> | 
| Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 21 | #include <vector> | 
| Kenny Root | 07438c8 | 2012-11-02 15:41:02 -0700 | [diff] [blame] | 22 |  | 
|  | 23 | using namespace android; | 
| Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 24 | using namespace keystore; | 
| Kenny Root | 07438c8 | 2012-11-02 15:41:02 -0700 | [diff] [blame] | 25 |  | 
| Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 26 | ssize_t keystore_get(const char* key, size_t keyLength, uint8_t** value) { | 
| Kenny Root | 07438c8 | 2012-11-02 15:41:02 -0700 | [diff] [blame] | 27 | sp<IServiceManager> sm = defaultServiceManager(); | 
|  | 28 | sp<IBinder> binder = sm->getService(String16("android.security.keystore")); | 
| Rob Barnes | bb6cabd | 2018-10-04 17:10:37 -0600 | [diff] [blame] | 29 | sp<android::security::keystore::IKeystoreService> service = | 
|  | 30 | interface_cast<android::security::keystore::IKeystoreService>(binder); | 
| Kenny Root | 07438c8 | 2012-11-02 15:41:02 -0700 | [diff] [blame] | 31 |  | 
| Yi Kong | e353f25 | 2018-07-30 01:38:39 -0700 | [diff] [blame] | 32 | if (service == nullptr) { | 
| Kenny Root | 07438c8 | 2012-11-02 15:41:02 -0700 | [diff] [blame] | 33 | return -1; | 
|  | 34 | } | 
|  | 35 |  | 
| Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 36 | ::std::vector<uint8_t> result; | 
| Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 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()); | 
| Kenny Root | 07438c8 | 2012-11-02 15:41:02 -0700 | [diff] [blame] | 44 | } | 
| Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 45 | return result.size(); | 
| Kenny Root | 07438c8 | 2012-11-02 15:41:02 -0700 | [diff] [blame] | 46 | } |