1 /*
2  * Copyright (C) 2016 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 <errno.h>
18 #include <stdio.h>
19 #include <stdlib.h>
20 
21 #include <memory>
22 #include <string>
23 
24 #include <android-base/file.h>
25 #include <android-base/unique_fd.h>
26 #include <crypto_utils/android_pubkey.h>
27 #include <fec/io.h>
28 #include <openssl/obj_mac.h>
29 #include <openssl/rsa.h>
30 #include <openssl/sha.h>
31 #include <sparse/sparse.h>
32 
load_key(const char * path)33 static RSA* load_key(const char* path) {
34   std::string content;
35   if (!android::base::ReadFileToString(path, &content) ||
36       content.size() < ANDROID_PUBKEY_ENCODED_SIZE) {
37     fprintf(stderr, "Failed to load key from %s\n", path);
38     return nullptr;
39   }
40 
41   RSA* key = nullptr;
42   if (!android_pubkey_decode(reinterpret_cast<const uint8_t*>(content.c_str()),
43                              ANDROID_PUBKEY_ENCODED_SIZE, &key)) {
44     fprintf(stderr, "Failed to parse key!\n");
45     return nullptr;
46   }
47 
48   return key;
49 }
50 
verify_table(const char * key_path,const uint8_t * signature,size_t signature_size,const char * table,uint32_t table_length)51 static int verify_table(const char* key_path, const uint8_t* signature, size_t signature_size,
52                         const char* table, uint32_t table_length) {
53   // Hash the table
54   uint8_t hash_buf[SHA256_DIGEST_LENGTH];
55   SHA256(const_cast<uint8_t*>(reinterpret_cast<const uint8_t*>(table)), table_length, hash_buf);
56 
57   // Now get the public key from the keyfile
58   std::unique_ptr<RSA, decltype(&RSA_free)> key(load_key(key_path), RSA_free);
59   if (!key) {
60     fprintf(stderr, "Couldn't load verity keys\n");
61     return -1;
62   }
63 
64   // Verify the result
65   if (!RSA_verify(NID_sha256, hash_buf, sizeof(hash_buf), signature, signature_size, key.get())) {
66     fprintf(stderr, "Couldn't verify table\n");
67     return -1;
68   }
69 
70   return 0;
71 }
72 
main(int argc,char * argv[])73 int main(int argc, char* argv[]) {
74   if (argc != 4 || strcmp(argv[2], "-mincrypt") != 0) {
75     printf("Usage: %s <image> -mincrypt <verity_key>\n"
76            "  image       the image file (raw or sparse image) to be verified\n"
77            "  verity_key  the verity key in mincrypt format (/verity_key on device)\n", argv[0]);
78     return 2;
79   }
80 
81   // Get the raw image.
82   android::base::unique_fd fd(open(argv[1], O_RDONLY));
83   if (fd == -1) {
84     fprintf(stderr, "failed to open %s: %s\n", argv[1], strerror(errno));
85     return 1;
86   }
87 
88   struct sparse_file* file = sparse_file_import_auto(fd, false, false);
89   if (file == nullptr) {
90     fprintf(stderr, "failed to read file %s\n", argv[1]);
91     return 1;
92   }
93 
94   TemporaryFile tf;
95   if (sparse_file_write(file, tf.fd, false, false, false) < 0) {
96     fprintf(stderr, "failed to write output file\n");
97     return 1;
98   }
99   sparse_file_destroy(file);
100 
101   // Verify.
102   fec::io input(tf.path);
103   if (!input) {
104     return 1;
105   }
106 
107   fec_verity_metadata verity;
108   if (!input.get_verity_metadata(verity)) {
109     fprintf(stderr, "failed to get verity metadata\n");
110     return 1;
111   }
112 
113   int ret = verify_table(argv[3], verity.signature, sizeof(verity.signature),
114                          verity.table, verity.table_length);
115   printf("%s\n", ret == 0 ? "VERIFIED" : "FAILED");
116   return ret;
117 }
118