Home | History | Annotate | Download | only in keystore
      1 /*
      2 **
      3 ** Copyright 2017, 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 #include "include/keystore/KeystoreArguments.h"
     19 #include "keystore_aidl_hidl_marshalling_utils.h"
     20 
     21 #include <binder/Parcel.h>
     22 
     23 namespace android {
     24 namespace security {
     25 
     26 using ::android::security::KeystoreArg;
     27 using ::android::security::KeystoreArguments;
     28 
     29 const ssize_t MAX_GENERATE_ARGS = 3;
     30 status_t KeystoreArguments::readFromParcel(const android::Parcel* in) {
     31     ssize_t numArgs = in->readInt32();
     32     if (numArgs > MAX_GENERATE_ARGS) {
     33         return BAD_VALUE;
     34     }
     35     if (numArgs > 0) {
     36         for (size_t i = 0; i < static_cast<size_t>(numArgs); i++) {
     37             ssize_t inSize = in->readInt32();
     38             if (inSize >= 0 && static_cast<size_t>(inSize) <= in->dataAvail()) {
     39                 sp<KeystoreArg> arg = new KeystoreArg(in->readInplace(inSize), inSize);
     40                 args.push_back(arg);
     41             } else {
     42                 args.push_back(NULL);
     43             }
     44         }
     45     }
     46     return OK;
     47 };
     48 
     49 status_t KeystoreArguments::writeToParcel(android::Parcel* out) const {
     50     out->writeInt32(args.size());
     51     for (sp<KeystoreArg> item : args) {
     52         size_t keyLength = item->size();
     53         out->writeInt32(keyLength);
     54         void* buf = out->writeInplace(keyLength);
     55         memcpy(buf, item->data(), keyLength);
     56     }
     57     return OK;
     58 }
     59 
     60 }  // namespace security
     61 }  // namespace android
     62