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