/* * Copyright (C) 2019 The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #define LOG_TAG "apexd" #include "apex_key.h" #include #include #include #include "apex_constants.h" #include "apex_file.h" #include "apexd_utils.h" #include "status_or.h" #include "string_log.h" namespace android { namespace apex { namespace { std::unordered_map gScannedApexKeys; using KeyPair = std::pair; StatusOr> collectEmbedddedApexKeysFromDir( const std::string& dir) { LOG(INFO) << "Scanning " << dir << " for embedded keys"; // list of pairs std::vector ret; if (access(dir.c_str(), F_OK) != 0 && errno == ENOENT) { LOG(INFO) << "... does not exist. Skipping"; return StatusOr>(std::move(ret)); } const bool scanBuiltinApexes = isPathForBuiltinApexes(dir); if (!scanBuiltinApexes) { return StatusOr>::MakeError( StringLog() << "Can't scan embedded APEX keys from " << dir); } StatusOr> apex_files = FindApexFilesByName(dir, scanBuiltinApexes); if (!apex_files.Ok()) { return StatusOr>::MakeError(apex_files.ErrorStatus()); } for (const auto& file : *apex_files) { StatusOr apex_file = ApexFile::Open(file); if (!apex_file.Ok()) { return StatusOr>::MakeError( StringLog() << "Failed to open " << file << " : " << apex_file.ErrorMessage()); } // name of the key is the name of the apex that the key is bundled in ret.push_back(std::make_pair(apex_file->GetManifest().name(), apex_file->GetBundledPublicKey())); } return StatusOr>(std::move(ret)); } Status updateScannedApexKeys(const std::vector& key_pairs) { for (const KeyPair& kp : key_pairs) { if (gScannedApexKeys.find(kp.first) == gScannedApexKeys.end()) { gScannedApexKeys.insert({kp.first, kp.second}); } else { const std::string& existing_key = gScannedApexKeys.at(kp.first); if (existing_key != kp.second) { return Status::Fail(StringLog() << "Key for package " << kp.first << " does not match with the existing key"); } } } return Status::Success(); } } // namespace Status collectApexKeys(bool scanExternalKeys) { for (const auto& dir : kApexPackageBuiltinDirs) { StatusOr> key_pairs = collectEmbedddedApexKeysFromDir(dir); if (!key_pairs.Ok()) { return Status::Fail(StringLog() << "Failed to collect keys from " << dir << " : " << key_pairs.ErrorMessage()); } Status st = updateScannedApexKeys(*key_pairs); if (!st.Ok()) { return st; } } return Status::Success(); } StatusOr getApexKey(const std::string& key_name) { if (gScannedApexKeys.find(key_name) == gScannedApexKeys.end()) { return StatusOr::MakeError( StringLog() << "No key found for package " << key_name); } return StatusOr(gScannedApexKeys[key_name]); } } // namespace apex } // namespace android