1657356c169c03498a789fd640cce3b3ffacf0c58Paul Stewart/* Copyright 2017 The Android Open Source Project
2657356c169c03498a789fd640cce3b3ffacf0c58Paul Stewart *
3657356c169c03498a789fd640cce3b3ffacf0c58Paul Stewart * Redistribution and use in source and binary forms, with or without
4657356c169c03498a789fd640cce3b3ffacf0c58Paul Stewart * modification, are permitted provided that the following conditions
5657356c169c03498a789fd640cce3b3ffacf0c58Paul Stewart * are met:
6657356c169c03498a789fd640cce3b3ffacf0c58Paul Stewart * 1. Redistributions of source code must retain the above copyright
7657356c169c03498a789fd640cce3b3ffacf0c58Paul Stewart *    notice, this list of conditions and the following disclaimer.
8657356c169c03498a789fd640cce3b3ffacf0c58Paul Stewart * 2. Redistributions in binary form must reproduce the above copyright
9657356c169c03498a789fd640cce3b3ffacf0c58Paul Stewart *    notice, this list of conditions and the following disclaimer in the
10657356c169c03498a789fd640cce3b3ffacf0c58Paul Stewart *    documentation and/or other materials provided with the distribution.
11657356c169c03498a789fd640cce3b3ffacf0c58Paul Stewart *
12657356c169c03498a789fd640cce3b3ffacf0c58Paul Stewart * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND ANY
13657356c169c03498a789fd640cce3b3ffacf0c58Paul Stewart * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
14657356c169c03498a789fd640cce3b3ffacf0c58Paul Stewart * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
15657356c169c03498a789fd640cce3b3ffacf0c58Paul Stewart * DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR ANY
16657356c169c03498a789fd640cce3b3ffacf0c58Paul Stewart * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
17657356c169c03498a789fd640cce3b3ffacf0c58Paul Stewart * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
18657356c169c03498a789fd640cce3b3ffacf0c58Paul Stewart * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
19657356c169c03498a789fd640cce3b3ffacf0c58Paul Stewart * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
20657356c169c03498a789fd640cce3b3ffacf0c58Paul Stewart * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
21657356c169c03498a789fd640cce3b3ffacf0c58Paul Stewart * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */
22657356c169c03498a789fd640cce3b3ffacf0c58Paul Stewart
23657356c169c03498a789fd640cce3b3ffacf0c58Paul Stewart#include "keystore_backend_hidl.h"
24657356c169c03498a789fd640cce3b3ffacf0c58Paul Stewart
25657356c169c03498a789fd640cce3b3ffacf0c58Paul Stewart#include <android/system/wifi/keystore/1.0/IKeystore.h>
264cb6f38017ec1d90142e75facf3ff0e0bbd3f077Steven Moreland#include <log/log.h>
27657356c169c03498a789fd640cce3b3ffacf0c58Paul Stewart
28657356c169c03498a789fd640cce3b3ffacf0c58Paul Stewartusing android::hardware::hidl_vec;
29657356c169c03498a789fd640cce3b3ffacf0c58Paul Stewartusing android::hardware::Return;
30657356c169c03498a789fd640cce3b3ffacf0c58Paul Stewartusing android::sp;
31657356c169c03498a789fd640cce3b3ffacf0c58Paul Stewartusing android::system::wifi::keystore::V1_0::IKeystore;
32657356c169c03498a789fd640cce3b3ffacf0c58Paul Stewart
33657356c169c03498a789fd640cce3b3ffacf0c58Paul Stewartint32_t KeystoreBackendHidl::sign(
34657356c169c03498a789fd640cce3b3ffacf0c58Paul Stewart        const char *key_id, const uint8_t* in, size_t len, uint8_t** reply,
35657356c169c03498a789fd640cce3b3ffacf0c58Paul Stewart        size_t* reply_len) {
36657356c169c03498a789fd640cce3b3ffacf0c58Paul Stewart    if (key_id == NULL || in == NULL || reply == NULL || reply_len == NULL) {
37657356c169c03498a789fd640cce3b3ffacf0c58Paul Stewart        ALOGE("Null pointer argument passed");
38657356c169c03498a789fd640cce3b3ffacf0c58Paul Stewart        return -1;
39657356c169c03498a789fd640cce3b3ffacf0c58Paul Stewart    }
40657356c169c03498a789fd640cce3b3ffacf0c58Paul Stewart
41e653c93db15400048605b49d74a21ec2c139afecRoshan Pius    sp<IKeystore> service = IKeystore::tryGetService();
42657356c169c03498a789fd640cce3b3ffacf0c58Paul Stewart    if (service == NULL) {
43bf7fc8df768203bb2812fd1e28b8dab23560e381Paul Stewart        ALOGE("could not contact keystore HAL");
44657356c169c03498a789fd640cce3b3ffacf0c58Paul Stewart        return -1;
45657356c169c03498a789fd640cce3b3ffacf0c58Paul Stewart    }
46657356c169c03498a789fd640cce3b3ffacf0c58Paul Stewart
47657356c169c03498a789fd640cce3b3ffacf0c58Paul Stewart    bool success = false;
48657356c169c03498a789fd640cce3b3ffacf0c58Paul Stewart    auto cb = [&](IKeystore::KeystoreStatusCode status,
49657356c169c03498a789fd640cce3b3ffacf0c58Paul Stewart                  hidl_vec<uint8_t> signedData) {
50657356c169c03498a789fd640cce3b3ffacf0c58Paul Stewart      if (status == IKeystore::KeystoreStatusCode::SUCCESS) {
51657356c169c03498a789fd640cce3b3ffacf0c58Paul Stewart          *reply_len = signedData.size();
52657356c169c03498a789fd640cce3b3ffacf0c58Paul Stewart          *reply = signedData.releaseData();
53657356c169c03498a789fd640cce3b3ffacf0c58Paul Stewart          success = true;
54657356c169c03498a789fd640cce3b3ffacf0c58Paul Stewart      }
55657356c169c03498a789fd640cce3b3ffacf0c58Paul Stewart    };
56657356c169c03498a789fd640cce3b3ffacf0c58Paul Stewart    Return<void> ret = service->sign(
57657356c169c03498a789fd640cce3b3ffacf0c58Paul Stewart        key_id, std::vector<uint8_t>(in, in + len), cb);
5830b220e734ea5c38a1e44dde71d513423e365ec8Roshan Pius    if (!ret.isOk() || !success) {
5930b220e734ea5c38a1e44dde71d513423e365ec8Roshan Pius        return 1;
6030b220e734ea5c38a1e44dde71d513423e365ec8Roshan Pius    }
6130b220e734ea5c38a1e44dde71d513423e365ec8Roshan Pius    return 0;
62657356c169c03498a789fd640cce3b3ffacf0c58Paul Stewart}
63657356c169c03498a789fd640cce3b3ffacf0c58Paul Stewart
64657356c169c03498a789fd640cce3b3ffacf0c58Paul Stewartint32_t KeystoreBackendHidl::get_pubkey(
65657356c169c03498a789fd640cce3b3ffacf0c58Paul Stewart        const char *key_id, uint8_t** pubkey, size_t* pubkey_len) {
66657356c169c03498a789fd640cce3b3ffacf0c58Paul Stewart    if (key_id == NULL || pubkey == NULL || pubkey_len == NULL) {
67657356c169c03498a789fd640cce3b3ffacf0c58Paul Stewart        ALOGE("Null pointer argument passed");
68657356c169c03498a789fd640cce3b3ffacf0c58Paul Stewart        return -1;
69657356c169c03498a789fd640cce3b3ffacf0c58Paul Stewart    }
70657356c169c03498a789fd640cce3b3ffacf0c58Paul Stewart
71e653c93db15400048605b49d74a21ec2c139afecRoshan Pius    sp<IKeystore> service = IKeystore::tryGetService();
72657356c169c03498a789fd640cce3b3ffacf0c58Paul Stewart    if (service == NULL) {
73bf7fc8df768203bb2812fd1e28b8dab23560e381Paul Stewart        ALOGE("could not contact keystore HAL");
74657356c169c03498a789fd640cce3b3ffacf0c58Paul Stewart        return -1;
75657356c169c03498a789fd640cce3b3ffacf0c58Paul Stewart    }
76657356c169c03498a789fd640cce3b3ffacf0c58Paul Stewart
77657356c169c03498a789fd640cce3b3ffacf0c58Paul Stewart    bool success = false;
78657356c169c03498a789fd640cce3b3ffacf0c58Paul Stewart    auto cb = [&](IKeystore::KeystoreStatusCode status,
79657356c169c03498a789fd640cce3b3ffacf0c58Paul Stewart                  hidl_vec<uint8_t> publicKey) {
80657356c169c03498a789fd640cce3b3ffacf0c58Paul Stewart      if (status == IKeystore::KeystoreStatusCode::SUCCESS) {
81657356c169c03498a789fd640cce3b3ffacf0c58Paul Stewart          *pubkey_len = publicKey.size();
82657356c169c03498a789fd640cce3b3ffacf0c58Paul Stewart          *pubkey = publicKey.releaseData();
83657356c169c03498a789fd640cce3b3ffacf0c58Paul Stewart          success = true;
84657356c169c03498a789fd640cce3b3ffacf0c58Paul Stewart      }
85657356c169c03498a789fd640cce3b3ffacf0c58Paul Stewart    };
86657356c169c03498a789fd640cce3b3ffacf0c58Paul Stewart    Return<void> ret = service->getPublicKey(key_id, cb);
8730b220e734ea5c38a1e44dde71d513423e365ec8Roshan Pius    if (!ret.isOk() || !success) {
8830b220e734ea5c38a1e44dde71d513423e365ec8Roshan Pius        return 1;
8930b220e734ea5c38a1e44dde71d513423e365ec8Roshan Pius    }
9030b220e734ea5c38a1e44dde71d513423e365ec8Roshan Pius    return 0;
91657356c169c03498a789fd640cce3b3ffacf0c58Paul Stewart}
92