Kyle Zhang | de0a90b | 2023-06-15 00:16:42 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2023 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 | |
| 17 | #define LOG_TAG "DrmRemotelyProvisionedComponent" |
| 18 | #include "DrmRemotelyProvisionedComponent.h" |
Robert Shih | 1195d77 | 2023-07-10 11:54:57 -0700 | [diff] [blame] | 19 | |
| 20 | #include <android-base/properties.h> |
| 21 | #include <cppbor.h> |
| 22 | #include <cppbor_parse.h> |
Kyle Zhang | de0a90b | 2023-06-15 00:16:42 +0000 | [diff] [blame] | 23 | #include <log/log.h> |
Robert Shih | 1195d77 | 2023-07-10 11:54:57 -0700 | [diff] [blame] | 24 | #include <map> |
| 25 | #include <string> |
Kyle Zhang | de0a90b | 2023-06-15 00:16:42 +0000 | [diff] [blame] | 26 | |
| 27 | namespace android::mediadrm { |
| 28 | DrmRemotelyProvisionedComponent::DrmRemotelyProvisionedComponent(std::shared_ptr<IDrmPlugin> drm, |
| 29 | std::string drmVendor, |
| 30 | std::string drmDesc) |
| 31 | : mDrm(std::move(drm)), mDrmVendor(std::move(drmVendor)), mDrmDesc(std::move(drmDesc)) {} |
| 32 | ScopedAStatus DrmRemotelyProvisionedComponent::getHardwareInfo(RpcHardwareInfo* info) { |
| 33 | info->versionNumber = 3; |
| 34 | info->rpcAuthorName = mDrmVendor; |
| 35 | info->supportedEekCurve = RpcHardwareInfo::CURVE_NONE; |
| 36 | info->supportedNumKeysInCsr = RpcHardwareInfo::MIN_SUPPORTED_NUM_KEYS_IN_CSR; |
| 37 | info->uniqueId = mDrmDesc; |
| 38 | return ScopedAStatus::ok(); |
| 39 | } |
| 40 | |
| 41 | ScopedAStatus DrmRemotelyProvisionedComponent::generateEcdsaP256KeyPair(bool, MacedPublicKey*, |
| 42 | std::vector<uint8_t>*) { |
| 43 | return ScopedAStatus(AStatus_fromServiceSpecificErrorWithMessage( |
| 44 | IRemotelyProvisionedComponent::STATUS_REMOVED, |
| 45 | "generateEcdsaP256KeyPair not supported.")); |
| 46 | } |
| 47 | |
| 48 | ScopedAStatus DrmRemotelyProvisionedComponent::generateCertificateRequest( |
| 49 | bool, const std::vector<MacedPublicKey>&, const std::vector<uint8_t>&, |
| 50 | const std::vector<uint8_t>&, DeviceInfo*, ProtectedData*, std::vector<uint8_t>*) { |
| 51 | return ScopedAStatus(AStatus_fromServiceSpecificErrorWithMessage( |
| 52 | IRemotelyProvisionedComponent::STATUS_REMOVED, |
| 53 | "generateCertificateRequest not supported.")); |
| 54 | } |
| 55 | |
Robert Shih | 1195d77 | 2023-07-10 11:54:57 -0700 | [diff] [blame] | 56 | ScopedAStatus DrmRemotelyProvisionedComponent::getVerifiedDeviceInfo(cppbor::Map& deviceInfoMap) { |
| 57 | std::vector<uint8_t> verifiedDeviceInfo; |
| 58 | auto status = mDrm->getPropertyByteArray("verifiedDeviceInfo", &verifiedDeviceInfo); |
| 59 | if (!status.isOk()) { |
| 60 | ALOGE("getPropertyByteArray verifiedDeviceInfo failed. Details: [%s].", |
| 61 | status.getDescription().c_str()); |
| 62 | return status; |
| 63 | } |
| 64 | |
| 65 | auto [parsed, _, err] = cppbor::parse( |
| 66 | reinterpret_cast<const uint8_t*>(verifiedDeviceInfo.data()), verifiedDeviceInfo.size()); |
| 67 | |
| 68 | if (!parsed || !parsed->asMap()) { |
| 69 | ALOGE("Failed to parse the verified device info cbor: %s", err.c_str()); |
| 70 | return ScopedAStatus(AStatus_fromServiceSpecificErrorWithMessage( |
| 71 | IRemotelyProvisionedComponent::STATUS_FAILED, |
| 72 | "Failed to parse the verified device info cbor.")); |
| 73 | } |
| 74 | |
| 75 | const cppbor::Map* verifiedDeviceInfoMap = parsed->asMap(); |
| 76 | for (size_t i = 0; i < verifiedDeviceInfoMap->size(); i++) { |
| 77 | auto& [keyItem, valueItem] = (*verifiedDeviceInfoMap)[i]; |
| 78 | ALOGI("Found device info %s", keyItem->asTstr()->value().data()); |
| 79 | if (valueItem != nullptr && valueItem->asTstr() != nullptr && |
| 80 | valueItem->asTstr()->value().empty()) { |
| 81 | ALOGI("Value is empty. Skip"); |
| 82 | continue; |
| 83 | } |
| 84 | deviceInfoMap.add(keyItem->clone(), valueItem->clone()); |
| 85 | } |
| 86 | |
| 87 | return ScopedAStatus::ok(); |
| 88 | } |
| 89 | |
| 90 | ScopedAStatus DrmRemotelyProvisionedComponent::getDeviceInfo(std::vector<uint8_t>* deviceInfo) { |
| 91 | auto deviceInfoMap = cppbor::Map(); |
| 92 | auto status = getVerifiedDeviceInfo(deviceInfoMap); |
| 93 | if (!status.isOk()) { |
| 94 | ALOGE("getVerifiedDeviceInfo failed. Details: [%s].", status.getDescription().c_str()); |
| 95 | return status; |
| 96 | } |
| 97 | const std::map<std::string, std::string> keyToProp{{"brand", "ro.product.brand"}, |
| 98 | {"manufacturer", "ro.product.manufacturer"}, |
| 99 | {"model", "ro.product.model"}, |
| 100 | {"device", "ro.product.device"}, |
| 101 | {"product", "ro.product.name"}}; |
| 102 | for (auto i : keyToProp) { |
| 103 | auto key = i.first; |
| 104 | auto prop = i.second; |
| 105 | const auto& val= deviceInfoMap.get(key); |
| 106 | if (val == nullptr || val->asTstr()->value().empty()) { |
| 107 | std::string propValue = android::base::GetProperty(prop, ""); |
| 108 | if (propValue.empty()) { |
| 109 | ALOGE("Failed to get OS property %s", prop.c_str()); |
| 110 | return ScopedAStatus(AStatus_fromServiceSpecificErrorWithMessage( |
| 111 | IRemotelyProvisionedComponent::STATUS_FAILED, |
| 112 | "Failed to get OS property.")); |
| 113 | } |
| 114 | deviceInfoMap.add(cppbor::Tstr(key), cppbor::Tstr(propValue)); |
| 115 | ALOGI("use OS property %s: %s", prop.c_str(), propValue.c_str()); |
| 116 | } else { |
| 117 | ALOGI("use verified key %s: %s", key.c_str(), val->asTstr()->value().data()); |
| 118 | } |
| 119 | } |
| 120 | deviceInfoMap.canonicalize(); |
| 121 | *deviceInfo = deviceInfoMap.encode(); |
| 122 | return ScopedAStatus::ok(); |
| 123 | } |
| 124 | |
Kyle Zhang | de0a90b | 2023-06-15 00:16:42 +0000 | [diff] [blame] | 125 | ScopedAStatus DrmRemotelyProvisionedComponent::generateCertificateRequestV2( |
| 126 | const std::vector<MacedPublicKey>&, const std::vector<uint8_t>& challenge, |
Robert Shih | 1195d77 | 2023-07-10 11:54:57 -0700 | [diff] [blame] | 127 | std::vector<uint8_t>* out) { |
| 128 | // access csr input/output via setPropertyByteArray/getPropertyByteArray |
Kyle Zhang | de0a90b | 2023-06-15 00:16:42 +0000 | [diff] [blame] | 129 | auto status = mDrm->setPropertyByteArray("certificateSigningRequestChallenge", challenge); |
| 130 | if (!status.isOk()) { |
| 131 | ALOGE("setPropertyByteArray certificateSigningRequestChallenge failed. Details: [%s].", |
| 132 | status.getDescription().c_str()); |
| 133 | return status; |
| 134 | } |
| 135 | |
Robert Shih | 1195d77 | 2023-07-10 11:54:57 -0700 | [diff] [blame] | 136 | std::vector<uint8_t> bcc; |
| 137 | status = mDrm->getPropertyByteArray("bootCertificateChain", &bcc); |
Kyle Zhang | de0a90b | 2023-06-15 00:16:42 +0000 | [diff] [blame] | 138 | if (!status.isOk()) { |
Robert Shih | 1195d77 | 2023-07-10 11:54:57 -0700 | [diff] [blame] | 139 | ALOGE("getPropertyByteArray bootCertificateChain failed. Details: [%s].", |
Kyle Zhang | de0a90b | 2023-06-15 00:16:42 +0000 | [diff] [blame] | 140 | status.getDescription().c_str()); |
| 141 | return status; |
| 142 | } |
| 143 | |
Robert Shih | 1195d77 | 2023-07-10 11:54:57 -0700 | [diff] [blame] | 144 | std::vector<uint8_t> deviceInfo; |
| 145 | status = getDeviceInfo(&deviceInfo); |
| 146 | if (!status.isOk()) { |
| 147 | ALOGE("getDeviceInfo failed. Details: [%s].", status.getDescription().c_str()); |
| 148 | return status; |
| 149 | } |
| 150 | |
| 151 | status = mDrm->setPropertyByteArray("deviceInfo", deviceInfo); |
| 152 | if (!status.isOk()) { |
| 153 | ALOGE("setPropertyByteArray deviceInfo failed. Details: [%s].", |
| 154 | status.getDescription().c_str()); |
| 155 | return status; |
| 156 | } |
| 157 | |
| 158 | std::vector<uint8_t> deviceSignedCsrPayload; |
| 159 | status = mDrm->getPropertyByteArray("deviceSignedCsrPayload", &deviceSignedCsrPayload); |
| 160 | if (!status.isOk()) { |
| 161 | ALOGE("getPropertyByteArray deviceSignedCsrPayload failed. Details: [%s].", |
| 162 | status.getDescription().c_str()); |
| 163 | return status; |
| 164 | } |
| 165 | |
| 166 | // assemble AuthenticatedRequest (definition in IRemotelyProvisionedComponent.aidl) |
| 167 | *out = cppbor::Array() |
| 168 | .add(1 /* version */) |
| 169 | .add(cppbor::Map() /* UdsCerts */) |
| 170 | .add(cppbor::EncodedItem(std::move(bcc))) |
| 171 | .add(cppbor::EncodedItem(std::move(deviceSignedCsrPayload))) |
| 172 | .encode(); |
Kyle Zhang | de0a90b | 2023-06-15 00:16:42 +0000 | [diff] [blame] | 173 | return ScopedAStatus::ok(); |
| 174 | } |
| 175 | } // namespace android::mediadrm |