]> code.delx.au - refind/blob - mok/mok.c
Fix for refind-install script to handle a wider range of disk devices,
[refind] / mok / mok.c
1 /* mok/mok.c
2 *
3 * Based mostly on shim.c by Matthew J. Garrett/Red Hat (see below
4 * copyright notice).
5 *
6 * Code to perform Secure Boot verification of boot loader programs
7 * using the Shim program and its Machine Owner Keys (MOKs), to
8 * supplement standard Secure Boot checks performed by the firmware.
9 *
10 */
11
12 /*
13 * shim - trivial UEFI first-stage bootloader
14 *
15 * Copyright 2012 Red Hat, Inc <mjg@redhat.com>
16 *
17 * Redistribution and use in source and binary forms, with or without
18 * modification, are permitted provided that the following conditions
19 * are met:
20 *
21 * Redistributions of source code must retain the above copyright
22 * notice, this list of conditions and the following disclaimer.
23 *
24 * Redistributions in binary form must reproduce the above copyright
25 * notice, this list of conditions and the following disclaimer in the
26 * documentation and/or other materials provided with the
27 * distribution.
28 *
29 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
30 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
31 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
32 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
33 * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
34 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
35 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
36 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
37 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
38 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
39 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
40 * OF THE POSSIBILITY OF SUCH DAMAGE.
41 *
42 * Significant portions of this code are derived from Tianocore
43 * (http://tianocore.sf.net) and are Copyright 2009-2012 Intel
44 * Corporation.
45 */
46
47 #include "global.h"
48 #include "mok.h"
49 #include "../include/refit_call_wrapper.h"
50 #include "../refind/lib.h"
51 #include "../refind/screen.h"
52
53
54 /*
55 * Check whether we're in Secure Boot and user mode
56 */
57 BOOLEAN secure_mode (VOID)
58 {
59 EFI_STATUS status;
60 EFI_GUID global_var = EFI_GLOBAL_VARIABLE;
61 UINTN charsize = sizeof(char);
62 UINT8 *sb = NULL, *setupmode = NULL;
63
64 status = EfivarGetRaw(&global_var, L"SecureBoot", (CHAR8 **) &sb, &charsize);
65 /* FIXME - more paranoia here? */
66 if (status != EFI_SUCCESS || charsize != sizeof(CHAR8) || *sb != 1) {
67 return FALSE;
68 }
69
70 status = EfivarGetRaw(&global_var, L"SetupMode", (CHAR8 **) &setupmode, &charsize);
71 if (status == EFI_SUCCESS && charsize == sizeof(CHAR8) && *setupmode == 1) {
72 return FALSE;
73 }
74
75 return TRUE;
76 } // secure_mode()
77
78 // Returns TRUE if the shim program is available to verify binaries,
79 // FALSE if not
80 BOOLEAN ShimLoaded(void) {
81 SHIM_LOCK *shim_lock;
82 EFI_GUID ShimLockGuid = SHIM_LOCK_GUID;
83
84 return (refit_call3_wrapper(BS->LocateProtocol, &ShimLockGuid, NULL, (VOID**) &shim_lock) == EFI_SUCCESS);
85 } // ShimLoaded()
86
87 // The following is based on the grub_linuxefi_secure_validate() function in Fedora's
88 // version of GRUB 2.
89 // Returns TRUE if the specified data is validated by Shim's MOK, FALSE otherwise
90 BOOLEAN ShimValidate (VOID *data, UINT32 size)
91 {
92 SHIM_LOCK *shim_lock;
93 EFI_GUID ShimLockGuid = SHIM_LOCK_GUID;
94
95 if ((data != NULL) && (refit_call3_wrapper(BS->LocateProtocol, &ShimLockGuid, NULL, (VOID**) &shim_lock) == EFI_SUCCESS)) {
96 if (!shim_lock)
97 return FALSE;
98
99 if (shim_lock->shim_verify(data, size) == EFI_SUCCESS)
100 return TRUE;
101 }
102
103 return FALSE;
104 } // BOOLEAN ShimValidate()