lin
2025-07-31 065ea569db06206874bbfa18eb25ff6121aec09b
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
/*
 * 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 <unordered_map>
 
#include <android-base/file.h>
#include <android-base/strings.h>
 
#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<std::string, const std::string> gScannedApexKeys;
 
using KeyPair = std::pair<std::string, std::string>;
StatusOr<std::vector<KeyPair>> collectEmbedddedApexKeysFromDir(
    const std::string& dir) {
  LOG(INFO) << "Scanning " << dir << " for embedded keys";
  // list of <key_name, key_content> pairs
  std::vector<KeyPair> ret;
  if (access(dir.c_str(), F_OK) != 0 && errno == ENOENT) {
    LOG(INFO) << "... does not exist. Skipping";
    return StatusOr<std::vector<KeyPair>>(std::move(ret));
  }
  const bool scanBuiltinApexes = isPathForBuiltinApexes(dir);
  if (!scanBuiltinApexes) {
    return StatusOr<std::vector<KeyPair>>::MakeError(
        StringLog() << "Can't scan embedded APEX keys from " << dir);
  }
  StatusOr<std::vector<std::string>> apex_files =
      FindApexFilesByName(dir, scanBuiltinApexes);
  if (!apex_files.Ok()) {
    return StatusOr<std::vector<KeyPair>>::MakeError(apex_files.ErrorStatus());
  }
 
  for (const auto& file : *apex_files) {
    StatusOr<ApexFile> apex_file = ApexFile::Open(file);
    if (!apex_file.Ok()) {
      return StatusOr<std::vector<KeyPair>>::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::vector<KeyPair>>(std::move(ret));
}
 
Status updateScannedApexKeys(const std::vector<KeyPair>& 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<std::vector<KeyPair>> 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<const std::string> getApexKey(const std::string& key_name) {
  if (gScannedApexKeys.find(key_name) == gScannedApexKeys.end()) {
    return StatusOr<const std::string>::MakeError(
        StringLog() << "No key found for package " << key_name);
  }
  return StatusOr<const std::string>(gScannedApexKeys[key_name]);
}
 
}  // namespace apex
}  // namespace android