Paul Stewart | ac0ffbf | 2017-03-03 16:43:33 -0800 | [diff] [blame^] | 1 | /* Copyright 2017 The Android Open Source Project |
| 2 | * |
| 3 | * Redistribution and use in source and binary forms, with or without |
| 4 | * modification, are permitted provided that the following conditions |
| 5 | * are met: |
| 6 | * 1. Redistributions of source code must retain the above copyright |
| 7 | * notice, this list of conditions and the following disclaimer. |
| 8 | * 2. Redistributions in binary form must reproduce the above copyright |
| 9 | * notice, this list of conditions and the following disclaimer in the |
| 10 | * documentation and/or other materials provided with the distribution. |
| 11 | * |
| 12 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND ANY |
| 13 | * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
| 14 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
| 15 | * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR ANY |
| 16 | * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
| 17 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
| 18 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND |
| 19 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 20 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
| 21 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ |
| 22 | |
| 23 | #include "keystore_backend_binder.h" |
| 24 | |
| 25 | #include <binder/IServiceManager.h> |
| 26 | #include <keystore/keystore.h> |
| 27 | #include <keystore/IKeystoreService.h> |
| 28 | #include <keystore/keystore_hidl_support.h> |
| 29 | |
| 30 | using namespace android; |
| 31 | using keystore::blob2hidlVec; |
| 32 | using keystore::hidl_vec; |
| 33 | |
| 34 | namespace { |
| 35 | const char keystore_service_name[] = "android.security.keystore"; |
| 36 | }; |
| 37 | |
| 38 | int32_t KeystoreBackendBinder::sign( |
| 39 | const char *key_id, const uint8_t* in, size_t len, uint8_t** reply, |
| 40 | size_t* reply_len) { |
| 41 | sp<IServiceManager> sm = defaultServiceManager(); |
| 42 | sp<IBinder> binder = sm->getService(String16(keystore_service_name)); |
| 43 | sp<IKeystoreService> service = interface_cast<IKeystoreService>(binder); |
| 44 | |
| 45 | if (service == NULL) { |
| 46 | ALOGE("could not contact keystore"); |
| 47 | return -1; |
| 48 | } |
| 49 | |
| 50 | auto inBlob = blob2hidlVec(in ,len); |
| 51 | hidl_vec<uint8_t> reply_vec; |
| 52 | auto ret = service->sign(String16(key_id), inBlob, &reply_vec); |
| 53 | if (!ret.isOk()) { |
| 54 | return -1; |
| 55 | } |
| 56 | |
| 57 | *reply = reply_vec.data(); |
| 58 | *reply_len = reply_vec.size(); |
| 59 | return 0; |
| 60 | } |
| 61 | |
| 62 | int32_t KeystoreBackendBinder::get_pubkey( |
| 63 | const char *key_id, uint8_t** pubkey, size_t* pubkey_len) { |
| 64 | sp<IServiceManager> sm = defaultServiceManager(); |
| 65 | sp<IBinder> binder = sm->getService(String16(keystore_service_name)); |
| 66 | sp<IKeystoreService> service = interface_cast<IKeystoreService>(binder); |
| 67 | |
| 68 | if (service == NULL) { |
| 69 | ALOGE("could not contact keystore"); |
| 70 | return -1; |
| 71 | } |
| 72 | |
| 73 | hidl_vec<uint8_t> pubkey_vec; |
| 74 | auto ret = service->get_pubkey(String16(key_id), &pubkey_vec); |
| 75 | if (!ret.isOk()) { |
| 76 | return -1; |
| 77 | } |
| 78 | |
| 79 | *pubkey = pubkey_vec.data(); |
| 80 | *pubkey_len = pubkey_vec.size(); |
| 81 | return 0; |
| 82 | } |