blob: 9122114772a82ba232b379f4949e0f9136f2f80a [file] [log] [blame]
Randall Spangler6f1b82a2014-12-03 12:29:37 -08001/* Copyright (c) 2014 The Chromium OS Authors. All rights reserved.
2 * Use of this source code is governed by a BSD-style license that can be
3 * found in the LICENSE file.
4 *
5 * Common functions between firmware and kernel verified boot.
6 * (Firmware portion)
7 */
8
9#include "2sysincludes.h"
10#include "2rsa.h"
11#include "2sha.h"
12#include "vb2_common.h"
13
14uint8_t *vb2_signature_data(struct vb2_signature *sig)
15{
16 return (uint8_t *)sig + sig->sig_offset;
17}
18
19int vb2_verify_member_inside(const void *parent, size_t parent_size,
20 const void *member, size_t member_size,
21 ptrdiff_t member_data_offset,
22 size_t member_data_size)
23{
24 const uintptr_t parent_end = (uintptr_t)parent + parent_size;
25 const ptrdiff_t member_offs = vb2_offset_of(parent, member);
26 const ptrdiff_t member_end_offs = member_offs + member_size;
27 const ptrdiff_t data_offs = member_offs + member_data_offset;
28 const ptrdiff_t data_end_offs = data_offs + member_data_size;
29
30 /* Make sure parent doesn't wrap */
Yunlian Jiang710485a2015-04-15 10:26:20 -070031 if (parent_end < (uintptr_t)parent)
Randall Spangler6f1b82a2014-12-03 12:29:37 -080032 return VB2_ERROR_INSIDE_PARENT_WRAPS;
33
34 /*
35 * Make sure the member is fully contained in the parent and doesn't
36 * wrap. Use >, not >=, since member_size = 0 is possible.
37 */
Yunlian Jiang710485a2015-04-15 10:26:20 -070038 if (member_end_offs < member_offs)
Randall Spangler6f1b82a2014-12-03 12:29:37 -080039 return VB2_ERROR_INSIDE_MEMBER_WRAPS;
40 if (member_offs < 0 || member_offs > parent_size ||
41 member_end_offs > parent_size)
42 return VB2_ERROR_INSIDE_MEMBER_OUTSIDE;
43
44 /* Make sure the member data is after the member */
45 if (member_data_size > 0 && data_offs < member_end_offs)
46 return VB2_ERROR_INSIDE_DATA_OVERLAP;
47
48 /* Make sure parent fully contains member data, if any */
Yunlian Jiang710485a2015-04-15 10:26:20 -070049 if (data_end_offs < data_offs)
Randall Spangler6f1b82a2014-12-03 12:29:37 -080050 return VB2_ERROR_INSIDE_DATA_WRAPS;
51 if (data_offs < 0 || data_offs > parent_size ||
52 data_end_offs > parent_size)
53 return VB2_ERROR_INSIDE_DATA_OUTSIDE;
54
55 return VB2_SUCCESS;
56}
57
58int vb2_verify_signature_inside(const void *parent,
59 uint32_t parent_size,
60 const struct vb2_signature *sig)
61{
62 return vb2_verify_member_inside(parent, parent_size,
63 sig, sizeof(*sig),
64 sig->sig_offset, sig->sig_size);
65}
66
67int vb2_verify_digest(const struct vb2_public_key *key,
68 struct vb2_signature *sig,
69 const uint8_t *digest,
70 const struct vb2_workbuf *wb)
71{
72 uint8_t *sig_data = vb2_signature_data(sig);
73
74 if (sig->sig_size != vb2_rsa_sig_size(key->sig_alg)) {
75 VB2_DEBUG("Wrong data signature size for algorithm, "
76 "sig_size=%d, expected %d for algorithm %d.\n",
77 sig->sig_size, vb2_rsa_sig_size(key->sig_alg),
78 key->sig_alg);
79 return VB2_ERROR_VDATA_SIG_SIZE;
80 }
81
82 return vb2_rsa_verify_digest(key, sig_data, digest, wb);
83}
84
85int vb2_verify_data(const uint8_t *data,
86 uint32_t size,
87 struct vb2_signature *sig,
88 const struct vb2_public_key *key,
89 const struct vb2_workbuf *wb)
90{
91 struct vb2_workbuf wblocal = *wb;
92 struct vb2_digest_context *dc;
93 uint8_t *digest;
94 uint32_t digest_size;
95 int rv;
96
97 if (sig->data_size > size) {
98 VB2_DEBUG("Data buffer smaller than length of signed data.\n");
99 return VB2_ERROR_VDATA_NOT_ENOUGH_DATA;
100 }
101
102 /* Digest goes at start of work buffer */
103 digest_size = vb2_digest_size(key->hash_alg);
104 if (!digest_size)
105 return VB2_ERROR_VDATA_DIGEST_SIZE;
106
107 digest = vb2_workbuf_alloc(&wblocal, digest_size);
108 if (!digest)
109 return VB2_ERROR_VDATA_WORKBUF_DIGEST;
110
111 /* Hashing requires temp space for the context */
112 dc = vb2_workbuf_alloc(&wblocal, sizeof(*dc));
113 if (!dc)
114 return VB2_ERROR_VDATA_WORKBUF_HASHING;
115
116 rv = vb2_digest_init(dc, key->hash_alg);
117 if (rv)
118 return rv;
119
120 rv = vb2_digest_extend(dc, data, sig->data_size);
121 if (rv)
122 return rv;
123
124 rv = vb2_digest_finalize(dc, digest, digest_size);
125 if (rv)
126 return rv;
127
128 vb2_workbuf_free(&wblocal, sizeof(*dc));
129
130 return vb2_verify_digest(key, sig, digest, &wblocal);
131}
132
Randall Spanglerb87d1ec2015-05-19 12:45:20 -0700133int vb2_check_keyblock(const struct vb2_keyblock *block,
134 uint32_t size,
135 const struct vb2_signature *sig)
Randall Spangler6f1b82a2014-12-03 12:29:37 -0800136{
Randall Spangler6f1b82a2014-12-03 12:29:37 -0800137 if(size < sizeof(*block)) {
138 VB2_DEBUG("Not enough space for key block header.\n");
139 return VB2_ERROR_KEYBLOCK_TOO_SMALL_FOR_HEADER;
140 }
Randall Spanglerb87d1ec2015-05-19 12:45:20 -0700141
Randall Spangler6f1b82a2014-12-03 12:29:37 -0800142 if (memcmp(block->magic, KEY_BLOCK_MAGIC, KEY_BLOCK_MAGIC_SIZE)) {
143 VB2_DEBUG("Not a valid verified boot key block.\n");
144 return VB2_ERROR_KEYBLOCK_MAGIC;
145 }
Randall Spanglerb87d1ec2015-05-19 12:45:20 -0700146
Randall Spangler6f1b82a2014-12-03 12:29:37 -0800147 if (block->header_version_major != KEY_BLOCK_HEADER_VERSION_MAJOR) {
148 VB2_DEBUG("Incompatible key block header version.\n");
149 return VB2_ERROR_KEYBLOCK_HEADER_VERSION;
150 }
Randall Spanglerb87d1ec2015-05-19 12:45:20 -0700151
Randall Spangler6f1b82a2014-12-03 12:29:37 -0800152 if (size < block->keyblock_size) {
153 VB2_DEBUG("Not enough data for key block.\n");
154 return VB2_ERROR_KEYBLOCK_SIZE;
155 }
156
Randall Spangler6f1b82a2014-12-03 12:29:37 -0800157 if (vb2_verify_signature_inside(block, block->keyblock_size, sig)) {
158 VB2_DEBUG("Key block signature off end of block\n");
159 return VB2_ERROR_KEYBLOCK_SIG_OUTSIDE;
160 }
161
162 /* Make sure advertised signature data sizes are sane. */
163 if (block->keyblock_size < sig->data_size) {
164 VB2_DEBUG("Signature calculated past end of block\n");
165 return VB2_ERROR_KEYBLOCK_SIGNED_TOO_MUCH;
166 }
167
Randall Spangler6f1b82a2014-12-03 12:29:37 -0800168 /* Verify we signed enough data */
169 if (sig->data_size < sizeof(struct vb2_keyblock)) {
170 VB2_DEBUG("Didn't sign enough data\n");
171 return VB2_ERROR_KEYBLOCK_SIGNED_TOO_LITTLE;
172 }
173
174 /* Verify data key is inside the block and inside signed data */
175 if (vb2_verify_packed_key_inside(block, block->keyblock_size,
176 &block->data_key)) {
177 VB2_DEBUG("Data key off end of key block\n");
178 return VB2_ERROR_KEYBLOCK_DATA_KEY_OUTSIDE;
179 }
180 if (vb2_verify_packed_key_inside(block, sig->data_size,
181 &block->data_key)) {
182 VB2_DEBUG("Data key off end of signed data\n");
183 return VB2_ERROR_KEYBLOCK_DATA_KEY_UNSIGNED;
184 }
185
Randall Spanglerb87d1ec2015-05-19 12:45:20 -0700186 return VB2_SUCCESS;
187}
188
189int vb2_verify_keyblock(struct vb2_keyblock *block,
190 uint32_t size,
191 const struct vb2_public_key *key,
192 const struct vb2_workbuf *wb)
193{
194 struct vb2_signature *sig = &block->keyblock_signature;
195 int rv;
196
197 /* Sanity check keyblock before attempting signature check of data */
198 rv = vb2_check_keyblock(block, size, sig);
199 if (rv)
200 return rv;
201
202 VB2_DEBUG("Checking key block signature...\n");
203 rv = vb2_verify_data((const uint8_t *)block, size, sig, key, wb);
204 if (rv) {
205 VB2_DEBUG("Invalid key block signature.\n");
206 return VB2_ERROR_KEYBLOCK_SIG_INVALID;
207 }
208
Randall Spangler6f1b82a2014-12-03 12:29:37 -0800209 /* Success */
210 return VB2_SUCCESS;
211}
212
213int vb2_verify_fw_preamble(struct vb2_fw_preamble *preamble,
214 uint32_t size,
215 const struct vb2_public_key *key,
216 const struct vb2_workbuf *wb)
217{
218 struct vb2_signature *sig = &preamble->preamble_signature;
219
220 VB2_DEBUG("Verifying preamble.\n");
221
222 /* Sanity checks before attempting signature of data */
223 if(size < sizeof(*preamble)) {
224 VB2_DEBUG("Not enough data for preamble header\n");
225 return VB2_ERROR_PREAMBLE_TOO_SMALL_FOR_HEADER;
226 }
227 if (preamble->header_version_major !=
228 FIRMWARE_PREAMBLE_HEADER_VERSION_MAJOR) {
229 VB2_DEBUG("Incompatible firmware preamble header version.\n");
230 return VB2_ERROR_PREAMBLE_HEADER_VERSION;
231 }
232
233 if (preamble->header_version_minor < 1) {
234 VB2_DEBUG("Only preamble header 2.1+ supported\n");
235 return VB2_ERROR_PREAMBLE_HEADER_OLD;
236 }
237
238 if (size < preamble->preamble_size) {
239 VB2_DEBUG("Not enough data for preamble.\n");
240 return VB2_ERROR_PREAMBLE_SIZE;
241 }
242
243 /* Check signature */
244 if (vb2_verify_signature_inside(preamble, preamble->preamble_size,
245 sig)) {
246 VB2_DEBUG("Preamble signature off end of preamble\n");
247 return VB2_ERROR_PREAMBLE_SIG_OUTSIDE;
248 }
249
250 /* Make sure advertised signature data sizes are sane. */
251 if (preamble->preamble_size < sig->data_size) {
252 VB2_DEBUG("Signature calculated past end of the block\n");
253 return VB2_ERROR_PREAMBLE_SIGNED_TOO_MUCH;
254 }
255
256 if (vb2_verify_data((const uint8_t *)preamble, size, sig, key, wb)) {
257 VB2_DEBUG("Preamble signature validation failed\n");
258 return VB2_ERROR_PREAMBLE_SIG_INVALID;
259 }
260
261 /* Verify we signed enough data */
262 if (sig->data_size < sizeof(struct vb2_fw_preamble)) {
263 VB2_DEBUG("Didn't sign enough data\n");
264 return VB2_ERROR_PREAMBLE_SIGNED_TOO_LITTLE;
265 }
266
267 /* Verify body signature is inside the signed data */
268 if (vb2_verify_signature_inside(preamble, sig->data_size,
269 &preamble->body_signature)) {
270 VB2_DEBUG("Firmware body signature off end of preamble\n");
271 return VB2_ERROR_PREAMBLE_BODY_SIG_OUTSIDE;
272 }
273
274 /* Verify kernel subkey is inside the signed data */
275 if (vb2_verify_packed_key_inside(preamble, sig->data_size,
276 &preamble->kernel_subkey)) {
277 VB2_DEBUG("Kernel subkey off end of preamble\n");
278 return VB2_ERROR_PREAMBLE_KERNEL_SUBKEY_OUTSIDE;
279 }
280
281 /* Success */
282 return VB2_SUCCESS;
283}