keystore_aidl_hidl_marshalling_utils.h 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. /*
  2. **
  3. ** Copyright 2016, The Android Open Source Project
  4. **
  5. ** Licensed under the Apache License, Version 2.0 (the "License");
  6. ** you may not use this file except in compliance with the License.
  7. ** You may obtain a copy of the License at
  8. **
  9. ** http://www.apache.org/licenses/LICENSE-2.0
  10. **
  11. ** Unless required by applicable law or agreed to in writing, software
  12. ** distributed under the License is distributed on an "AS IS" BASIS,
  13. ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. ** See the License for the specific language governing permissions and
  15. ** limitations under the License.
  16. */
  17. #ifndef KEYSTORE_KEYSTORE_AIDL_HIDL_MARSHALLING_UTILS_H_
  18. #define KEYSTORE_KEYSTORE_AIDL_HIDL_MARSHALLING_UTILS_H_
  19. #include <utility>
  20. #include <binder/Parcel.h>
  21. #include <keystore/keymaster_types.h>
  22. namespace keystore {
  23. template <typename Fn, typename... Args>
  24. inline auto nullable(Fn fn, const android::Parcel& in, Args&&... args)
  25. -> NullOr<decltype(fn(in, std::forward<Args>(args)...))> {
  26. if (in.readInt32() != 1) {
  27. return {};
  28. }
  29. return fn(in, std::forward<Args>(args)...);
  30. }
  31. template <typename Fn, typename Arg>
  32. inline android::status_t nullable(Fn fn, const NullOr<Arg>& arg, android::Parcel* out) {
  33. if (!arg.isOk()) {
  34. return out->writeInt32(0);
  35. }
  36. auto rc = out->writeInt32(1);
  37. if (rc != ::android::OK) return rc;
  38. return fn(arg.value(), out);
  39. }
  40. template <typename Fn, typename Arg>
  41. inline android::status_t nullable(Fn fn, Arg&& arg, android::Parcel* out) {
  42. auto rc = out->writeInt32(1);
  43. if (rc != ::android::OK) return rc;
  44. return fn(std::forward<Arg>(arg), out);
  45. }
  46. inline android::status_t nullable(android::Parcel* out) {
  47. return out->writeInt32(0);
  48. }
  49. /**
  50. * makes a copy only if inPlace is false
  51. */
  52. hidl_vec<uint8_t> readKeymasterBlob(const android::Parcel& in);
  53. android::status_t writeKeymasterBlob(const hidl_vec<uint8_t>& blob, android::Parcel* out);
  54. NullOr<hidl_vec<uint8_t>> readBlobAsByteArray(const android::Parcel& in, bool inPlace = true);
  55. android::status_t writeBlobAsByteArray(const NullOr<const hidl_vec<uint8_t>&>& blob,
  56. android::Parcel* out);
  57. NullOr<KeyParameter> readKeyParameterFromParcel(const android::Parcel& in);
  58. android::status_t writeKeyParameterToParcel(const KeyParameter& param, android::Parcel* out);
  59. hidl_vec<KeyParameter> readParamSetFromParcel(const android::Parcel& in);
  60. android::status_t writeParamSetToParcel(const hidl_vec<KeyParameter>& params, android::Parcel* out);
  61. hidl_vec<hidl_vec<uint8_t>> readCertificateChainFromParcel(const android::Parcel& in);
  62. }
  63. #endif // KEYSTORE_KEYSTORE_AIDL_HIDL_MARSHALLING_UTILS_H_