• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2019 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 "fsverity_init"
18 
19 #include <sys/types.h>
20 
21 #include <filesystem>
22 #include <string>
23 
24 #include <android-base/file.h>
25 #include <android-base/logging.h>
26 #include <android-base/properties.h>
27 #include <android-base/strings.h>
28 #include <log/log.h>
29 #include <mini_keyctl_utils.h>
30 
LoadKeyToKeyring(key_serial_t keyring_id,const char * desc,const char * data,size_t size)31 bool LoadKeyToKeyring(key_serial_t keyring_id, const char* desc, const char* data, size_t size) {
32     key_serial_t key = add_key("asymmetric", desc, data, size, keyring_id);
33     if (key < 0) {
34         PLOG(ERROR) << "Failed to add key";
35         return false;
36     }
37     return true;
38 }
39 
LoadKeyFromStdin(key_serial_t keyring_id,const char * keyname)40 bool LoadKeyFromStdin(key_serial_t keyring_id, const char* keyname) {
41     std::string content;
42     if (!android::base::ReadFdToString(STDIN_FILENO, &content)) {
43         LOG(ERROR) << "Failed to read key from stdin";
44         return false;
45     }
46     if (!LoadKeyToKeyring(keyring_id, keyname, content.c_str(), content.size())) {
47         LOG(ERROR) << "Failed to load key from stdin";
48         return false;
49     }
50     return true;
51 }
52 
LoadKeyFromFile(key_serial_t keyring_id,const char * keyname,const std::string & path)53 void LoadKeyFromFile(key_serial_t keyring_id, const char* keyname, const std::string& path) {
54     LOG(INFO) << "LoadKeyFromFile path=" << path << " keyname=" << keyname;
55     std::string content;
56     if (!android::base::ReadFileToString(path, &content)) {
57         LOG(ERROR) << "Failed to read key from " << path;
58         return;
59     }
60     if (!LoadKeyToKeyring(keyring_id, keyname, content.c_str(), content.size())) {
61         LOG(ERROR) << "Failed to load key from " << path;
62     }
63 }
64 
LoadKeyFromDirectory(key_serial_t keyring_id,const char * keyname_prefix,const char * dir)65 void LoadKeyFromDirectory(key_serial_t keyring_id, const char* keyname_prefix, const char* dir) {
66     if (!std::filesystem::exists(dir)) {
67         return;
68     }
69     int counter = 0;
70     for (const auto& entry : std::filesystem::directory_iterator(dir)) {
71         if (!android::base::EndsWithIgnoreCase(entry.path().c_str(), ".der")) continue;
72         std::string keyname = keyname_prefix + std::to_string(counter);
73         counter++;
74         LoadKeyFromFile(keyring_id, keyname.c_str(), entry.path());
75     }
76 }
77 
LoadKeyFromVerifiedPartitions(key_serial_t keyring_id)78 void LoadKeyFromVerifiedPartitions(key_serial_t keyring_id) {
79     // NB: Directories need to be synced with FileIntegrityService.java in
80     // frameworks/base.
81     LoadKeyFromDirectory(keyring_id, "fsv_system_", "/system/etc/security/fsverity");
82     LoadKeyFromDirectory(keyring_id, "fsv_product_", "/product/etc/security/fsverity");
83 }
84