Aditya Wazir | 730a133 | 2021-05-19 11:01:36 +0530 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright (C) 2021 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | #ifndef KEYSTORECOMMON_H |
| 17 | #define KEYSTORECOMMON_H |
| 18 | |
| 19 | #include <binder/Parcel.h> |
| 20 | #include <binder/Parcelable.h> |
| 21 | #include <keystore/KeyAttestationPackageInfo.h> |
| 22 | #include <keystore/Signature.h> |
| 23 | #include <vector> |
| 24 | |
| 25 | #include "fuzzer/FuzzedDataProvider.h" |
| 26 | |
| 27 | using namespace android; |
| 28 | using namespace std; |
| 29 | using ::content::pm::Signature; |
| 30 | using ::security::keymaster::KeyAttestationPackageInfo; |
| 31 | |
| 32 | constexpr size_t kSignatureSizeMin = 1; |
| 33 | constexpr size_t kSignatureSizeMax = 1000; |
| 34 | constexpr size_t kRandomStringLength = 256; |
| 35 | constexpr size_t kSignatureVectorSizeMin = 1; |
| 36 | constexpr size_t kSignatureVectorSizeMax = 1000; |
| 37 | |
| 38 | struct PackageInfoData { |
| 39 | string packageName; |
| 40 | int64_t versionCode; |
| 41 | KeyAttestationPackageInfo::SharedSignaturesVector sharedSignaturesVector; |
| 42 | }; |
| 43 | |
| 44 | inline void invokeReadWriteParcel(Parcelable* obj) { |
| 45 | Parcel parcel; |
| 46 | obj->writeToParcel(&parcel); |
| 47 | parcel.setDataPosition(0); |
| 48 | obj->readFromParcel(&parcel); |
| 49 | } |
| 50 | |
| 51 | inline vector<uint8_t> initSignatureData(FuzzedDataProvider* fdp) { |
| 52 | size_t signatureSize = fdp->ConsumeIntegralInRange(kSignatureSizeMin, kSignatureSizeMax); |
| 53 | vector<uint8_t> signatureData = fdp->ConsumeBytes<uint8_t>(signatureSize); |
| 54 | return signatureData; |
| 55 | } |
| 56 | |
| 57 | inline PackageInfoData initPackageInfoData(FuzzedDataProvider* fdp) { |
| 58 | PackageInfoData packageInfoData; |
| 59 | packageInfoData.packageName = fdp->ConsumeRandomLengthString(kRandomStringLength); |
| 60 | packageInfoData.versionCode = fdp->ConsumeIntegral<int64_t>(); |
| 61 | size_t signatureVectorSize = |
| 62 | fdp->ConsumeIntegralInRange(kSignatureVectorSizeMin, kSignatureVectorSizeMax); |
| 63 | KeyAttestationPackageInfo::SignaturesVector signatureVector; |
| 64 | for (size_t size = 0; size < signatureVectorSize; ++size) { |
| 65 | bool shouldUseParameterizedConstructor = fdp->ConsumeBool(); |
| 66 | if (shouldUseParameterizedConstructor) { |
| 67 | vector<uint8_t> signatureData = initSignatureData(fdp); |
| 68 | signatureVector.push_back(make_optional<Signature>(signatureData)); |
| 69 | } else { |
| 70 | signatureVector.push_back(std::nullopt); |
| 71 | } |
| 72 | } |
| 73 | packageInfoData.sharedSignaturesVector = |
| 74 | make_shared<KeyAttestationPackageInfo::SignaturesVector>(move(signatureVector)); |
| 75 | return packageInfoData; |
| 76 | } |
| 77 | #endif // KEYSTORECOMMON_H |