blob: ea72197a4aba2036559f329e0f8bacbf725e9b6f [file] [log] [blame]
Janis Danisevskisc7a9fa22016-10-13 18:43:45 +01001/*
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
18#ifndef KEYSTORE_KEYSTORE_AIDL_HIDL_MARSHALLING_UTILS_H_
19#define KEYSTORE_KEYSTORE_AIDL_HIDL_MARSHALLING_UTILS_H_
20
Janis Danisevskisc7a9fa22016-10-13 18:43:45 +010021#include <utility>
22
Shawn Willdenbb22a6c2017-12-06 19:35:28 -070023#include <binder/Parcel.h>
24
25#include <keystore/keymaster_types.h>
26
Janis Danisevskisc7a9fa22016-10-13 18:43:45 +010027namespace keystore {
28
29template <typename Fn, typename... Args>
30inline auto nullable(Fn fn, const android::Parcel& in, Args&&... args)
31 -> NullOr<decltype(fn(in, std::forward<Args>(args)...))> {
32 if (in.readInt32() != 1) {
33 return {};
34 }
35
36 return fn(in, std::forward<Args>(args)...);
37}
38template <typename Fn, typename Arg>
39inline android::status_t nullable(Fn fn, const NullOr<Arg>& arg, android::Parcel* out) {
40 if (!arg.isOk()) {
41 return out->writeInt32(0);
42 }
43 auto rc = out->writeInt32(1);
44 if (rc != ::android::OK) return rc;
45
46 return fn(arg.value(), out);
47}
48template <typename Fn, typename Arg>
49inline android::status_t nullable(Fn fn, Arg&& arg, android::Parcel* out) {
50 auto rc = out->writeInt32(1);
51 if (rc != ::android::OK) return rc;
52
53 return fn(std::forward<Arg>(arg), out);
54}
55
56inline android::status_t nullable(android::Parcel* out) {
57 return out->writeInt32(0);
58}
59
60/**
61 * makes a copy only if inPlace is false
62 */
Rob Barnesbb6cabd2018-10-04 17:10:37 -060063hidl_vec<uint8_t> readKeymasterBlob(const android::Parcel& in);
Janis Danisevskisc7a9fa22016-10-13 18:43:45 +010064android::status_t writeKeymasterBlob(const hidl_vec<uint8_t>& blob, android::Parcel* out);
65
66NullOr<hidl_vec<uint8_t>> readBlobAsByteArray(const android::Parcel& in, bool inPlace = true);
67android::status_t writeBlobAsByteArray(const NullOr<const hidl_vec<uint8_t>&>& blob,
68 android::Parcel* out);
69
70NullOr<KeyParameter> readKeyParameterFromParcel(const android::Parcel& in);
71android::status_t writeKeyParameterToParcel(const KeyParameter& param, android::Parcel* out);
72
73hidl_vec<KeyParameter> readParamSetFromParcel(const android::Parcel& in);
74android::status_t writeParamSetToParcel(const hidl_vec<KeyParameter>& params, android::Parcel* out);
75
Janis Danisevskisc7a9fa22016-10-13 18:43:45 +010076hidl_vec<hidl_vec<uint8_t>> readCertificateChainFromParcel(const android::Parcel& in);
Janis Danisevskisc7a9fa22016-10-13 18:43:45 +010077}
78
79#endif // KEYSTORE_KEYSTORE_AIDL_HIDL_MARSHALLING_UTILS_H_