blob: d5cb1a5ffc3da7fc91cb7aa58092f5641ad14e5f [file] [log] [blame]
Jooyung Han54b88d42021-04-14 18:46:14 +09001/*
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
17#include <sys/stat.h>
18#include <sys/types.h>
19#include <unistd.h>
20
21#include <fstream>
22#include <iostream>
23#include <string>
24
25#include <android-base/file.h>
26#include <android-base/result.h>
27#include <json/json.h>
28
29#include "microdroid/signature.h"
30
31using android::base::Dirname;
32using android::base::ErrnoError;
33using android::base::Error;
34using android::base::Result;
35using android::microdroid::MicrodroidSignature;
36using android::microdroid::WriteMicrodroidSignature;
37
38Result<uint32_t> GetFileSize(const std::string& path) {
39 struct stat st;
40 if (lstat(path.c_str(), &st) == -1) {
41 return ErrnoError() << "Can't lstat " << path;
42 }
43 return static_cast<uint32_t>(st.st_size);
44}
45
46// config JSON schema:
47// {
48// "apexes": [
49// {
50// "name": string, // the apex name
51// "path": string, // the path to the apex file
52// // absolute or relative to the config file
53// "publicKey": string, // optional
54// "rootDigest": string, // optional
55// }
56// ]
57// }
58
59Result<MicrodroidSignature> LoadConfig(const std::string& config_file) {
60 MicrodroidSignature signature;
61 signature.set_version(1);
62
63 const std::string dirname = Dirname(config_file);
64 std::ifstream in(config_file);
65 Json::CharReaderBuilder builder;
66 Json::Value root;
67 Json::String errs;
68 if (!parseFromStream(builder, in, &root, &errs)) {
69 return Error() << "bad config: " << errs;
70 }
71
72 for (const Json::Value& apex : root["apexes"]) {
73 auto apex_signature = signature.add_apexes();
74
75 Json::Value name = apex["name"];
76 Json::Value path = apex["path"];
77 Json::Value publicKey = apex["publicKey"];
78 Json::Value rootDigest = apex["rootDigest"];
79
80 if (name.isString()) {
81 apex_signature->set_name(name.asString());
82 } else {
83 return Error() << "bad config: apexes.name should be a string: " << path;
84 }
85
86 if (path.isString()) {
87 std::string apex_path = path.asString();
88
89 // resolve path with the config_file's dirname if not absolute
90 bool is_absolute = !apex_path.empty() && apex_path[0] == '/';
91 if (!is_absolute) {
92 apex_path = dirname + "/" + apex_path;
93 }
94
95 auto file_size = GetFileSize(apex_path);
96 if (!file_size.ok()) {
97 return Error() << "I/O error: " << file_size.error();
98 }
99 apex_signature->set_size(file_size.value());
100 } else {
101 return Error() << "bad config: apexes.path should be a string: " << path;
102 }
103
104 if (publicKey.isString()) {
105 apex_signature->set_publickey(publicKey.asString());
106 } else if (!publicKey.isNull()) {
107 return Error() << "bad config: apexes.publicKey should be a string or null: "
108 << publicKey;
109 }
110
111 if (rootDigest.isString()) {
112 apex_signature->set_rootdigest(rootDigest.asString());
113 } else if (!rootDigest.isNull()) {
114 return Error() << "bad config: apexes.rootDigest should be a string or null: "
115 << rootDigest;
116 }
117 }
118
119 return signature;
120}
121
122int main(int argc, char** argv) {
123 if (argc != 3) {
124 std::cerr << "Usage: " << argv[0] << " <config> <output>\n";
125 return 1;
126 }
127
128 auto config = LoadConfig(argv[1]);
129 if (!config.ok()) {
130 std::cerr << config.error() << '\n';
131 return 1;
132 }
133
134 std::ofstream out(argv[2]);
135 auto result = WriteMicrodroidSignature(*config, out);
136 if (!result.ok()) {
137 std::cerr << result.error() << '\n';
138 return 1;
139 }
140 return 0;
141}