Skip to content

Instantly share code, notes, and snippets.

@tiberiosantos
Created October 9, 2020 23:30
Show Gist options
  • Save tiberiosantos/8e641b22f679582612c539175bca8bb0 to your computer and use it in GitHub Desktop.
Save tiberiosantos/8e641b22f679582612c539175bca8bb0 to your computer and use it in GitHub Desktop.
Script to create and sign linux kernel modules with a UEFI MOK key
#!/bin/bash
#
# Script to sign linux kernel modules with a UEFIMOK key
#
# Reference: https://wiki.debian.org/SecureBoot
#
# The MIT License (MIT)
#
# Copyright (c) 2020 Tiberio A. Santos
#
# Permission is hereby granted, free of charge, to any person obtaining
# a copy of this software and associated documentation files (the "Software"),
# to deal in the Software without restriction, including without limitation
# the rights to use, copy, modify, merge, publish, distribute, sublicense,
# and/or sell copies of the Software, and to permit persons to whom the
# Software is furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
green="\033[01;32m"
yellow="\033[01;33m"
normal="\033[00m"
priv=/etc/ssl/private/MOK.priv
der=/etc/ssl/private/MOK.der
_install() {
echo 'Creating new key ... '
openssl req -new -x509 -newkey rsa:2048 \
-keyout $priv -outform DER -out $der \
-days 36500 -subj "/CN=My Name/" -nodes
echo 'Importing the keys. A password will be asked'
mokutil --import $der
mokutil --list-new
echo 'A reboot is needed to finished the installation ... '
read -p 'Continue? (y\n) ?' CONT
if [[ "$CONT" = "y" ]]; then
reboot
fi
}
_sign() {
for m in /lib/modules/$(uname -r)/updates/dkms/*.ko; do
/usr/lib/linux-kbuild-$(uname -r | awk -F. '{print $1 "." $2}')/scripts/sign-file \
sha256 $priv $der "$m"
done
}
case "$1" in
install)
_install
;;
sign)
_sign
;;
*)
echo -e "
Usage $0 (${yellow}install${normal} | ${yellow}sign${normal})
${yellow}OPTIONS${normal}
${green}install${normal}: create and import a new MOK certificate.
${green}sign${normal}: sign all dkms modules with the MOK keys.
"
;;
esac
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment