1 #include <android-base/logging.h>
2 #include <binder/IServiceManager.h>
3 #include <keystore/IKeystoreService.h>
4 #include <private/android_filesystem_config.h>
5 
6 #include "include/wifikeystorehal/keystore.h"
7 
8 namespace android {
9 namespace system {
10 namespace wifi {
11 namespace keystore {
12 namespace V1_0 {
13 namespace implementation {
14 // Methods from ::android::hardware::wifi::keystore::V1_0::IKeystore follow.
getBlob(const hidl_string & key,getBlob_cb _hidl_cb)15 Return<void> Keystore::getBlob(const hidl_string& key, getBlob_cb _hidl_cb) {
16   sp<IKeystoreService> service = interface_cast<IKeystoreService>(
17           defaultServiceManager()->getService(
18                   String16("android.security.keystore")));
19   if (service == nullptr) {
20     _hidl_cb(KeystoreStatusCode::ERROR_UNKNOWN, {});
21     return Void();
22   }
23   hidl_vec<uint8_t> value;
24   // Retrieve the blob as wifi user.
25   auto ret = service->get(String16(key.c_str()), AID_WIFI, &value);
26   if (!ret.isOk()) {
27     _hidl_cb(KeystoreStatusCode::ERROR_UNKNOWN, {});
28     return Void();
29   }
30   _hidl_cb(KeystoreStatusCode::SUCCESS, value);
31   return Void();
32 }
33 
getPublicKey(const hidl_string & keyId,getPublicKey_cb _hidl_cb)34 Return<void> Keystore::getPublicKey(
35         const hidl_string& keyId, getPublicKey_cb _hidl_cb) {
36   sp<IKeystoreService> service = interface_cast<IKeystoreService>(
37           defaultServiceManager()->getService(
38                   String16("android.security.keystore")));
39   if (service == nullptr) {
40     _hidl_cb(KeystoreStatusCode::ERROR_UNKNOWN, {});
41     return Void();
42   }
43   hidl_vec<uint8_t> pubkey;
44   auto ret = service->get_pubkey(String16(keyId.c_str()), &pubkey);
45   if (!ret.isOk()) {
46     _hidl_cb(KeystoreStatusCode::ERROR_UNKNOWN, {});
47     return Void();
48   }
49   _hidl_cb(KeystoreStatusCode::SUCCESS, pubkey);
50   return Void();
51 }
52 
sign(const hidl_string & keyId,const hidl_vec<uint8_t> & dataToSign,sign_cb _hidl_cb)53 Return<void> Keystore::sign(
54         const hidl_string& keyId, const hidl_vec<uint8_t>& dataToSign,
55         sign_cb _hidl_cb) {
56   sp<IKeystoreService> service = interface_cast<IKeystoreService>(
57           defaultServiceManager()->getService(
58                   String16("android.security.keystore")));
59   if (service == nullptr) {
60     _hidl_cb(KeystoreStatusCode::ERROR_UNKNOWN, {});
61     return Void();
62   }
63   hidl_vec<uint8_t> signedData;
64   auto ret = service->sign(String16(keyId.c_str()), dataToSign, &signedData);
65   if (!ret.isOk()) {
66     _hidl_cb(KeystoreStatusCode::ERROR_UNKNOWN, {});
67     return Void();
68   }
69   _hidl_cb(KeystoreStatusCode::SUCCESS, signedData);
70   return Void();
71 }
72 
HIDL_FETCH_IKeystore(const char *)73 IKeystore* HIDL_FETCH_IKeystore(const char* /* name */) {
74     return new Keystore();
75 }
76 }  // namespace implementation
77 }  // namespace V1_0
78 }  // namespace keystore
79 }  // namespace wifi
80 }  // namespace system
81 }  // namespace android
82