keystore_backend_hidl.cpp 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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. #include "keystore_backend_hidl.h"
  23. #include <android/system/wifi/keystore/1.0/IKeystore.h>
  24. #include <log/log.h>
  25. using android::hardware::hidl_vec;
  26. using android::hardware::Return;
  27. using android::sp;
  28. using android::system::wifi::keystore::V1_0::IKeystore;
  29. int32_t KeystoreBackendHidl::sign(
  30. const char *key_id, const uint8_t* in, size_t len, uint8_t** reply,
  31. size_t* reply_len) {
  32. if (key_id == nullptr || in == nullptr || reply == nullptr || reply_len == nullptr) {
  33. ALOGE("Null pointer argument passed");
  34. return -1;
  35. }
  36. sp<IKeystore> service = IKeystore::tryGetService();
  37. if (service == nullptr) {
  38. ALOGE("could not contact keystore HAL");
  39. return -1;
  40. }
  41. bool success = false;
  42. auto cb = [&](IKeystore::KeystoreStatusCode status,
  43. hidl_vec<uint8_t> signedData) {
  44. if (status == IKeystore::KeystoreStatusCode::SUCCESS) {
  45. *reply_len = signedData.size();
  46. *reply = signedData.releaseData();
  47. success = true;
  48. }
  49. };
  50. Return<void> ret = service->sign(
  51. key_id, std::vector<uint8_t>(in, in + len), cb);
  52. if (!ret.isOk() || !success) {
  53. return 1;
  54. }
  55. return 0;
  56. }
  57. int32_t KeystoreBackendHidl::get_pubkey(
  58. const char *key_id, uint8_t** pubkey, size_t* pubkey_len) {
  59. if (key_id == nullptr || pubkey == nullptr || pubkey_len == nullptr) {
  60. ALOGE("Null pointer argument passed");
  61. return -1;
  62. }
  63. sp<IKeystore> service = IKeystore::tryGetService();
  64. if (service == nullptr) {
  65. ALOGE("could not contact keystore HAL");
  66. return -1;
  67. }
  68. bool success = false;
  69. auto cb = [&](IKeystore::KeystoreStatusCode status,
  70. hidl_vec<uint8_t> publicKey) {
  71. if (status == IKeystore::KeystoreStatusCode::SUCCESS) {
  72. *pubkey_len = publicKey.size();
  73. *pubkey = publicKey.releaseData();
  74. success = true;
  75. }
  76. };
  77. Return<void> ret = service->getPublicKey(key_id, cb);
  78. if (!ret.isOk() || !success) {
  79. return 1;
  80. }
  81. return 0;
  82. }