Home | History | Annotate | Download | only in keystore
      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 <utility>
     22 
     23 #include <binder/Parcel.h>
     24 
     25 #include <keystore/keymaster_types.h>
     26 
     27 namespace keystore {
     28 
     29 template <typename Fn, typename... Args>
     30 inline 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 }
     38 template <typename Fn, typename Arg>
     39 inline 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 }
     48 template <typename Fn, typename Arg>
     49 inline 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 
     56 inline 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  */
     63 hidl_vec<uint8_t> readKeymasterBlob(const android::Parcel& in, bool inPlace = true);
     64 android::status_t writeKeymasterBlob(const hidl_vec<uint8_t>& blob, android::Parcel* out);
     65 
     66 NullOr<hidl_vec<uint8_t>> readBlobAsByteArray(const android::Parcel& in, bool inPlace = true);
     67 android::status_t writeBlobAsByteArray(const NullOr<const hidl_vec<uint8_t>&>& blob,
     68                                        android::Parcel* out);
     69 
     70 NullOr<KeyParameter> readKeyParameterFromParcel(const android::Parcel& in);
     71 android::status_t writeKeyParameterToParcel(const KeyParameter& param, android::Parcel* out);
     72 
     73 hidl_vec<KeyParameter> readParamSetFromParcel(const android::Parcel& in);
     74 android::status_t writeParamSetToParcel(const hidl_vec<KeyParameter>& params, android::Parcel* out);
     75 
     76 hidl_vec<hidl_vec<uint8_t>> readCertificateChainFromParcel(const android::Parcel& in);
     77 }
     78 
     79 #endif  // KEYSTORE_KEYSTORE_AIDL_HIDL_MARSHALLING_UTILS_H_
     80