blob: 0c8b598bde90ef3a1c99c30ab9650779dca3f8fc [file] [log] [blame]
Randall Spanglerd7f0f932015-05-19 13:41:09 -07001/* Copyright 2015 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 * Externally-callable APIs
6 * (Kernel portion)
7 */
8
9#include "2sysincludes.h"
10#include "2api.h"
11#include "2misc.h"
12#include "2nvstorage.h"
13#include "2secdata.h"
14#include "2sha.h"
15#include "2rsa.h"
16#include "vb2_common.h"
17
18int vb2api_kernel_phase1(struct vb2_context *ctx)
19{
20 struct vb2_shared_data *sd = vb2_get_sd(ctx);
21 struct vb2_workbuf wb;
22 uint8_t *key_data;
23 uint32_t key_size;
24 int rv;
25
26 vb2_workbuf_from_ctx(ctx, &wb);
27
28 /* Initialize secure kernel data and read version */
29 rv = vb2_secdatak_init(ctx);
30 if (!rv) {
31 rv = vb2_secdatak_get(ctx, VB2_SECDATAK_VERSIONS,
32 &sd->kernel_version_secdatak);
33 }
34
35 if (rv) {
36 if (ctx->flags & VB2_CONTEXT_RECOVERY_MODE) {
37 /* Ignore failure to get kernel version in recovery */
38 sd->kernel_version_secdatak = 0;
39 } else {
40 vb2_fail(ctx, VB2_RECOVERY_SECDATAK_INIT, rv);
41 return rv;
42 }
43 }
44
45 /* Find the key to use to verify the kernel keyblock */
46 if (ctx->flags & VB2_CONTEXT_RECOVERY_MODE) {
47 /* Recovery key from GBB */
48 struct vb2_gbb_header *gbb;
49 uint32_t key_offset;
50
51 /* Read GBB header into next chunk of work buffer */
52 gbb = vb2_workbuf_alloc(&wb, sizeof(*gbb));
53 if (!gbb)
54 return VB2_ERROR_GBB_WORKBUF;
55
56 rv = vb2_read_gbb_header(ctx, gbb);
57 if (rv)
58 return rv;
59
60 /* Only need the recovery key position and size */
61 key_offset = gbb->recovery_key_offset;
62 key_size = gbb->recovery_key_size;
63
64 /* Free the GBB header */
65 vb2_workbuf_free(&wb, sizeof(*gbb));
66
67 /* Load the recovery key itself */
68 key_data = vb2_workbuf_alloc(&wb, key_size);
69 if (!key_data)
70 return VB2_ERROR_API_KPHASE1_WORKBUF_REC_KEY;
71
72 rv = vb2ex_read_resource(ctx, VB2_RES_GBB, key_offset,
73 key_data, key_size);
74 if (rv)
75 return rv;
76 } else {
77 /* Kernel subkey from firmware preamble */
78 struct vb2_fw_preamble *pre;
79 struct vb2_packed_key *pre_key, *packed_key;
80
81 /* Make sure we have a firmware preamble loaded */
82 if (!sd->workbuf_preamble_size)
83 return VB2_ERROR_API_KPHASE1_PREAMBLE;
84
85 pre = (struct vb2_fw_preamble *)
86 (ctx->workbuf + sd->workbuf_preamble_offset);
87 pre_key = &pre->kernel_subkey;
88
89 /*
90 * At this point, we no longer need the packed firmware
91 * data key, firmware preamble, or hash data. So move the
92 * kernel key from the preamble down after the shared data.
93 */
94 key_data = (uint8_t *)(sd + 1);
95 packed_key = (struct vb2_packed_key *)key_data;
96 memmove(packed_key, pre_key, sizeof(*packed_key));
97 packed_key->key_offset = sizeof(*packed_key);
98 memmove(key_data + packed_key->key_offset,
99 (uint8_t *)pre_key + pre_key->key_offset,
100 pre_key->key_size);
101
102 key_size = packed_key->key_offset + packed_key->key_size;
103 }
104
105 /* Firmware stage structs are no longer present */
106 sd->workbuf_data_key_size = 0;
107 sd->workbuf_preamble_size = 0;
108 sd->workbuf_hash_size = 0;
109
110 /*
111 * Kernel key will persist in the workbuf after we return.
112 *
113 * Work buffer now contains:
114 * - vb2_shared_data
115 * - kernel key
116 */
117 sd->workbuf_kernel_key_offset =
118 vb2_offset_of(ctx->workbuf, key_data);
119 sd->workbuf_kernel_key_size = key_size;
120 ctx->workbuf_used = sd->workbuf_kernel_key_offset +
121 sd->workbuf_kernel_key_size;
122
123 return VB2_SUCCESS;
124}
125
126int vb2api_load_kernel_vblock(struct vb2_context *ctx)
127{
128 int rv;
129
130 /* Verify kernel keyblock */
131 rv = vb2_load_kernel_keyblock(ctx);
132 if (rv)
133 return rv;
134
135 /* Verify kernel preamble */
136 rv = vb2_load_kernel_preamble(ctx);
137 if (rv)
138 return rv;
139
140 return VB2_SUCCESS;
141}
142
143int vb2api_get_kernel_size(struct vb2_context *ctx,
144 uint32_t *offset_ptr,
145 uint32_t *size_ptr)
146{
147 struct vb2_shared_data *sd = vb2_get_sd(ctx);
148 const struct vb2_kernel_preamble *pre;
149
150 /* Get preamble pointer */
151 if (!sd->workbuf_preamble_size)
152 return VB2_ERROR_API_GET_KERNEL_SIZE_PREAMBLE;
153
154 pre = (const struct vb2_kernel_preamble *)
155 (ctx->workbuf + sd->workbuf_preamble_offset);
156
157 if (offset_ptr) {
158 /* The kernel implicitly follows the preamble */
159 *offset_ptr = sd->vblock_preamble_offset +
160 sd->workbuf_preamble_size;
161 }
162
163 if (size_ptr) {
164 /* Expect the kernel to be the size of data we signed */
165 *size_ptr = pre->body_signature.data_size;
166 }
167
168 return VB2_SUCCESS;
169}
170
171int vb2api_verify_kernel_data(struct vb2_context *ctx,
172 const void *buf,
173 uint32_t size)
174{
175 struct vb2_shared_data *sd = vb2_get_sd(ctx);
176 struct vb2_kernel_preamble *pre;
177 struct vb2_digest_context *dc;
178 struct vb2_public_key key;
179 struct vb2_workbuf wb;
180
181 uint8_t *digest;
182 uint32_t digest_size;
183
184 int rv;
185
186 vb2_workbuf_from_ctx(ctx, &wb);
187
188 /* Get preamble pointer */
189 if (!sd->workbuf_preamble_size)
190 return VB2_ERROR_API_VERIFY_KDATA_PREAMBLE;
191
192 pre = (struct vb2_kernel_preamble *)
193 (ctx->workbuf + sd->workbuf_preamble_offset);
194
195 /* Make sure we were passed the right amount of data */
196 if (size != pre->body_signature.data_size)
197 return VB2_ERROR_API_VERIFY_KDATA_SIZE;
198
199 /* Allocate workbuf space for the hash */
200 dc = vb2_workbuf_alloc(&wb, sizeof(*dc));
201 if (!dc)
202 return VB2_ERROR_API_VERIFY_KDATA_WORKBUF;
203
204 /*
205 * Unpack the kernel data key to see which hashing algorithm we
206 * should use.
207 *
208 * TODO: really, the kernel body should be hashed, and not signed,
209 * because the signature we're checking is already signed as part of
210 * the kernel preamble. But until we can change the signing scripts,
211 * we're stuck with a signature here instead of a hash.
212 */
213 if (!sd->workbuf_data_key_size)
214 return VB2_ERROR_API_VERIFY_KDATA_KEY;
215
216 rv = vb2_unpack_key(&key,
217 ctx->workbuf + sd->workbuf_data_key_offset,
218 sd->workbuf_data_key_size);
219 if (rv)
220 return rv;
221
222 rv = vb2_digest_init(dc, key.hash_alg);
223 if (rv)
224 return rv;
225
226 rv = vb2_digest_extend(dc, buf, size);
227 if (rv)
228 return rv;
229
230 digest_size = vb2_digest_size(key.hash_alg);
231 digest = vb2_workbuf_alloc(&wb, digest_size);
232 if (!digest)
233 return VB2_ERROR_API_CHECK_HASH_WORKBUF_DIGEST;
234
235 rv = vb2_digest_finalize(dc, digest, digest_size);
236 if (rv)
237 return rv;
238
239 /*
240 * The body signature is currently a *signature* of the body data, not
241 * just its hash. So we need to verify the signature.
242 */
243
244 /*
245 * Check digest vs. signature. Note that this destroys the signature.
246 * That's ok, because we only check each signature once per boot.
247 */
248 return vb2_verify_digest(&key, &pre->body_signature, digest, &wb);
249}
250
251int vb2api_kernel_phase3(struct vb2_context *ctx)
252{
253 struct vb2_shared_data *sd = vb2_get_sd(ctx);
254 int rv;
255
256 /*
257 * If the kernel is a newer version than in secure storage, and the
258 * kernel signature is valid, and we're not in recovery mode, and we're
259 * allowed to, roll forward the version in secure storage.
260 */
261 if (sd->kernel_version > sd->kernel_version_secdatak &&
262 (sd->flags & VB2_SD_FLAG_KERNEL_SIGNED) &&
263 !(ctx->flags & VB2_CONTEXT_RECOVERY_MODE) &&
264 (ctx->flags & VB2_CONTEXT_ALLOW_KERNEL_ROLL_FORWARD)) {
265 rv = vb2_secdatak_set(ctx, VB2_SECDATAK_VERSIONS,
266 sd->kernel_version);
267 if (rv)
268 return rv;
269 sd->kernel_version_secdatak = sd->kernel_version;
270 }
271
272 return VB2_SUCCESS;
273}