1 /*
2 * Copyright (C) 2020 The Android Open Source Project
3 *
4 * Permission is hereby granted, free of charge, to any person
5 * obtaining a copy of this software and associated documentation
6 * files (the "Software"), to deal in the Software without
7 * restriction, including without limitation the rights to use, copy,
8 * modify, merge, publish, distribute, sublicense, and/or sell copies
9 * of the Software, and to permit persons to whom the Software is
10 * furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice shall be
13 * included in all copies or substantial portions of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
19 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
20 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
21 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 * SOFTWARE.
23 */
24
25 #include <libavb/avb_crypto.h>
26 #include <libavb/avb_rsa.h>
27 #include <libavb/avb_sha.h>
28 #include <libavb/avb_util.h>
29
30 #include "avb_aftl_types.h"
31 #include "avb_aftl_util.h"
32 #include "avb_aftl_validate.h"
33
34 /* Verifies that the logged VBMeta hash matches the one on device. */
avb_aftl_verify_vbmeta_hash(uint8_t * vbmeta,size_t vbmeta_size,AftlIcpEntry * icp_entry)35 bool avb_aftl_verify_vbmeta_hash(uint8_t* vbmeta,
36 size_t vbmeta_size,
37 AftlIcpEntry* icp_entry) {
38 uint8_t vbmeta_hash[AVB_AFTL_HASH_SIZE];
39
40 avb_assert(vbmeta != NULL && icp_entry != NULL);
41 if (!avb_aftl_sha256(vbmeta, vbmeta_size, vbmeta_hash)) return false;
42
43 /* Only SHA256 hashes are currently supported. If the vbmeta hash
44 size is not AVB_AFTL_HASH_SIZE, return false. */
45 if (icp_entry->annotation_leaf->annotation->vbmeta_hash_size !=
46 AVB_AFTL_HASH_SIZE) {
47 avb_error("Invalid VBMeta hash size.\n");
48 return false;
49 }
50
51 /* Return whether the calculated VBMeta hash matches the stored one. */
52 return avb_safe_memcmp(vbmeta_hash,
53 icp_entry->annotation_leaf->annotation->vbmeta_hash,
54 AVB_AFTL_HASH_SIZE) == 0;
55 }
56
57 /* Verifies the Merkle tree root hash. */
avb_aftl_verify_icp_root_hash(AftlIcpEntry * icp_entry)58 bool avb_aftl_verify_icp_root_hash(AftlIcpEntry* icp_entry) {
59 uint8_t leaf_hash[AVB_AFTL_HASH_SIZE];
60 uint8_t result_hash[AVB_AFTL_HASH_SIZE];
61
62 avb_assert(icp_entry != NULL);
63 /* Calculate the RFC 6962 hash of the seed entry. */
64 if (!avb_aftl_rfc6962_hash_leaf(icp_entry->annotation_leaf_raw,
65 icp_entry->annotation_leaf_size,
66 leaf_hash)) {
67 return false;
68 }
69 /* Calculate the Merkle tree's root hash. */
70 if (!avb_aftl_root_from_icp(icp_entry->leaf_index,
71 icp_entry->log_root_descriptor.tree_size,
72 icp_entry->proofs,
73 icp_entry->proof_hash_count,
74 leaf_hash,
75 AVB_AFTL_HASH_SIZE,
76 result_hash))
77 return false;
78
79 /* Return whether the calculated root hash matches the stored one. */
80 return (avb_safe_memcmp(result_hash,
81 icp_entry->log_root_descriptor.root_hash,
82 AVB_AFTL_HASH_SIZE) == 0);
83 }
84
85 /* Verifies the log root signature for the transparency log submission. */
avb_aftl_verify_entry_signature(const uint8_t * key,size_t key_num_bytes,AftlIcpEntry * icp_entry)86 bool avb_aftl_verify_entry_signature(const uint8_t* key,
87 size_t key_num_bytes,
88 AftlIcpEntry* icp_entry) {
89 uint8_t* sig;
90 size_t sig_num_bytes;
91 uint8_t log_root_hash[AVB_AFTL_HASH_SIZE];
92 size_t log_root_hash_num_bytes;
93 const AvbAlgorithmData* algorithm_data;
94
95 avb_assert(key != NULL && icp_entry != NULL);
96
97 /* Extract the log root signature from the AftlIcpEntry. */
98 sig = icp_entry->log_root_signature;
99 if (sig == NULL) {
100 avb_error("Invalid log root signature.\n");
101 return false;
102 }
103 sig_num_bytes = icp_entry->log_root_sig_size;
104 log_root_hash_num_bytes = AVB_AFTL_HASH_SIZE;
105
106 /* Calculate the SHA256 of the TrillianLogRootDescriptor. */
107 if (!avb_aftl_sha256(icp_entry->log_root_descriptor_raw,
108 icp_entry->log_root_descriptor_size,
109 log_root_hash))
110 return false;
111
112 /* algorithm_data is used to calculate the padding for signature verification.
113 */
114 algorithm_data = avb_get_algorithm_data(AVB_ALGORITHM_TYPE_SHA256_RSA4096);
115 if (algorithm_data == NULL) {
116 avb_error("Failed to get algorithm data.\n");
117 return false;
118 }
119
120 return avb_rsa_verify(key,
121 key_num_bytes,
122 sig,
123 sig_num_bytes,
124 log_root_hash,
125 log_root_hash_num_bytes,
126 algorithm_data->padding,
127 algorithm_data->padding_len);
128 }
129