| Paul Stewart | 657356c | 2017-03-09 00:00:23 -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_hidl.h" | 
 | 24 |  | 
 | 25 | #include <android/system/wifi/keystore/1.0/IKeystore.h> | 
| Steven Moreland | 4cb6f38 | 2017-04-06 11:48:19 -0700 | [diff] [blame] | 26 | #include <log/log.h> | 
| Paul Stewart | 657356c | 2017-03-09 00:00:23 -0800 | [diff] [blame] | 27 |  | 
 | 28 | using android::hardware::hidl_vec; | 
 | 29 | using android::hardware::Return; | 
 | 30 | using android::sp; | 
 | 31 | using android::system::wifi::keystore::V1_0::IKeystore; | 
 | 32 |  | 
 | 33 | int32_t KeystoreBackendHidl::sign( | 
 | 34 |         const char *key_id, const uint8_t* in, size_t len, uint8_t** reply, | 
 | 35 |         size_t* reply_len) { | 
| Yi Kong | e353f25 | 2018-07-30 01:38:39 -0700 | [diff] [blame] | 36 |     if (key_id == nullptr || in == nullptr || reply == nullptr || reply_len == nullptr) { | 
| Paul Stewart | 657356c | 2017-03-09 00:00:23 -0800 | [diff] [blame] | 37 |         ALOGE("Null pointer argument passed"); | 
 | 38 |         return -1; | 
 | 39 |     } | 
 | 40 |  | 
| Roshan Pius | e653c93 | 2017-03-29 10:08:47 -0700 | [diff] [blame] | 41 |     sp<IKeystore> service = IKeystore::tryGetService(); | 
| Yi Kong | e353f25 | 2018-07-30 01:38:39 -0700 | [diff] [blame] | 42 |     if (service == nullptr) { | 
| Paul Stewart | bf7fc8d | 2017-03-10 16:22:03 -0800 | [diff] [blame] | 43 |         ALOGE("could not contact keystore HAL"); | 
| Paul Stewart | 657356c | 2017-03-09 00:00:23 -0800 | [diff] [blame] | 44 |         return -1; | 
 | 45 |     } | 
 | 46 |  | 
 | 47 |     bool success = false; | 
 | 48 |     auto cb = [&](IKeystore::KeystoreStatusCode status, | 
 | 49 |                   hidl_vec<uint8_t> signedData) { | 
 | 50 |       if (status == IKeystore::KeystoreStatusCode::SUCCESS) { | 
 | 51 |           *reply_len = signedData.size(); | 
 | 52 |           *reply = signedData.releaseData(); | 
 | 53 |           success = true; | 
 | 54 |       } | 
 | 55 |     }; | 
 | 56 |     Return<void> ret = service->sign( | 
 | 57 |         key_id, std::vector<uint8_t>(in, in + len), cb); | 
| Roshan Pius | 30b220e | 2017-03-31 16:47:04 -0700 | [diff] [blame] | 58 |     if (!ret.isOk() || !success) { | 
 | 59 |         return 1; | 
 | 60 |     } | 
 | 61 |     return 0; | 
| Paul Stewart | 657356c | 2017-03-09 00:00:23 -0800 | [diff] [blame] | 62 | } | 
 | 63 |  | 
 | 64 | int32_t KeystoreBackendHidl::get_pubkey( | 
 | 65 |         const char *key_id, uint8_t** pubkey, size_t* pubkey_len) { | 
| Yi Kong | e353f25 | 2018-07-30 01:38:39 -0700 | [diff] [blame] | 66 |     if (key_id == nullptr || pubkey == nullptr || pubkey_len == nullptr) { | 
| Paul Stewart | 657356c | 2017-03-09 00:00:23 -0800 | [diff] [blame] | 67 |         ALOGE("Null pointer argument passed"); | 
 | 68 |         return -1; | 
 | 69 |     } | 
 | 70 |  | 
| Roshan Pius | e653c93 | 2017-03-29 10:08:47 -0700 | [diff] [blame] | 71 |     sp<IKeystore> service = IKeystore::tryGetService(); | 
| Yi Kong | e353f25 | 2018-07-30 01:38:39 -0700 | [diff] [blame] | 72 |     if (service == nullptr) { | 
| Paul Stewart | bf7fc8d | 2017-03-10 16:22:03 -0800 | [diff] [blame] | 73 |         ALOGE("could not contact keystore HAL"); | 
| Paul Stewart | 657356c | 2017-03-09 00:00:23 -0800 | [diff] [blame] | 74 |         return -1; | 
 | 75 |     } | 
 | 76 |  | 
 | 77 |     bool success = false; | 
 | 78 |     auto cb = [&](IKeystore::KeystoreStatusCode status, | 
 | 79 |                   hidl_vec<uint8_t> publicKey) { | 
 | 80 |       if (status == IKeystore::KeystoreStatusCode::SUCCESS) { | 
 | 81 |           *pubkey_len = publicKey.size(); | 
 | 82 |           *pubkey = publicKey.releaseData(); | 
 | 83 |           success = true; | 
 | 84 |       } | 
 | 85 |     }; | 
 | 86 |     Return<void> ret = service->getPublicKey(key_id, cb); | 
| Roshan Pius | 30b220e | 2017-03-31 16:47:04 -0700 | [diff] [blame] | 87 |     if (!ret.isOk() || !success) { | 
 | 88 |         return 1; | 
 | 89 |     } | 
 | 90 |     return 0; | 
| Paul Stewart | 657356c | 2017-03-09 00:00:23 -0800 | [diff] [blame] | 91 | } |