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
18 #ifndef KEYSTORE_KEYSTORE_AIDL_HIDL_MARSHALLING_UTILS_H_
19 #define KEYSTORE_KEYSTORE_AIDL_HIDL_MARSHALLING_UTILS_H_
20
21 #include <binder/Parcel.h>
22 #include <keystore/keymaster_tags.h>
23 #include <utility>
24
25 namespace keystore {
26
27 template <typename Fn, typename... Args>
28 inline auto nullable(Fn fn, const android::Parcel& in, Args&&... args)
29 -> NullOr<decltype(fn(in, std::forward<Args>(args)...))> {
30 if (in.readInt32() != 1) {
31 return {};
32 }
33
34 return fn(in, std::forward<Args>(args)...);
35 }
36 template <typename Fn, typename Arg>
nullable(Fn fn,const NullOr<Arg> & arg,android::Parcel * out)37 inline android::status_t nullable(Fn fn, const NullOr<Arg>& arg, android::Parcel* out) {
38 if (!arg.isOk()) {
39 return out->writeInt32(0);
40 }
41 auto rc = out->writeInt32(1);
42 if (rc != ::android::OK) return rc;
43
44 return fn(arg.value(), out);
45 }
46 template <typename Fn, typename Arg>
nullable(Fn fn,Arg && arg,android::Parcel * out)47 inline android::status_t nullable(Fn fn, Arg&& arg, android::Parcel* out) {
48 auto rc = out->writeInt32(1);
49 if (rc != ::android::OK) return rc;
50
51 return fn(std::forward<Arg>(arg), out);
52 }
53
nullable(android::Parcel * out)54 inline android::status_t nullable(android::Parcel* out) {
55 return out->writeInt32(0);
56 }
57
58 /**
59 * makes a copy only if inPlace is false
60 */
61 hidl_vec<uint8_t> readKeymasterBlob(const android::Parcel& in, bool inPlace = true);
62 android::status_t writeKeymasterBlob(const hidl_vec<uint8_t>& blob, android::Parcel* out);
63
64 NullOr<hidl_vec<uint8_t>> readBlobAsByteArray(const android::Parcel& in, bool inPlace = true);
65 android::status_t writeBlobAsByteArray(const NullOr<const hidl_vec<uint8_t>&>& blob,
66 android::Parcel* out);
67
68 NullOr<KeyParameter> readKeyParameterFromParcel(const android::Parcel& in);
69 android::status_t writeKeyParameterToParcel(const KeyParameter& param, android::Parcel* out);
70
71 hidl_vec<KeyParameter> readParamSetFromParcel(const android::Parcel& in);
72 android::status_t writeParamSetToParcel(const hidl_vec<KeyParameter>& params, android::Parcel* out);
73
74 KeyCharacteristics readKeyCharacteristicsFromParcel(const android::Parcel& in);
75 android::status_t writeKeyCharacteristicsToParcel(const KeyCharacteristics& keyChara,
76 android::Parcel* out);
77
78 hidl_vec<hidl_vec<uint8_t>> readCertificateChainFromParcel(const android::Parcel& in);
79 android::status_t writeCertificateChainToParcel(const hidl_vec<hidl_vec<uint8_t>>& certs,
80 android::Parcel* out);
81 }
82
83 #endif // KEYSTORE_KEYSTORE_AIDL_HIDL_MARSHALLING_UTILS_H_
84