#!/bin/sh # Helper script to generate .sig files for use with the Raspberry Pi bootloader. # This has been implemented in a separate script in order to have avoid having # a hard dependency on OpenSSL. set -e OPENSSL=${OPENSSL:-openssl} die() { echo "$@" >&2 exit 1 } TMP_DIR="" cleanup() { if [ -d "${TMP_DIR}" ]; then rm -rf "${TMP_DIR}" fi } checkDependencies() { if ! command -v sha256sum > /dev/null; then die "sha256sum not found. Try installing the coreutilities package." fi if [ -n "${KEY}" ] || [ "${VERIFY}" = 1 ]; then if ! command -v openssl > /dev/null; then die "openssl not found. Try installing the openssl package." fi if ! command -v xxd > /dev/null; then die "xxd not found. Try installing the xxd package." fi fi } usage() { cat < "${OUTPUT}" # Include the update-timestamp echo "ts: $(date -u +%s)" >> "${OUTPUT}" if [ -n "${KEY}" ]; then [ -f "${KEY}" ] || die "RSA private \"${KEY}\" not found" "${OPENSSL}" dgst -sign "${KEY}" -keyform PEM -sha256 -out "${SIG_TMP}" "${IMAGE}" echo "rsa2048: $(xxd -c 4096 -p < "${SIG_TMP}")" >> "${OUTPUT}" fi } verifySig() { TMP_DIR=$(mktemp -d) sig_file="${1}" [ -f "${sig_file}" ] || die "Signature file ${sig_file} not found" sig_hex="$(grep rsa2048 "${sig_file}" | cut -f 2 -d ' ')" [ -n "${sig_hex}" ] || die "No RSA signature in ${sig_file}" echo ${sig_hex} | xxd -c 4096 -p -r > "${TMP_DIR}/sig.bin" "${OPENSSL}" dgst -verify "${KEY}" -signature "${TMP_DIR}/sig.bin" "${IMAGE}" || die "${IMAGE} not verified" } OUTPUT="" VERIFY=0 while getopts i:k:ho:v: option; do case "${option}" in i) IMAGE="${OPTARG}" ;; k) KEY="${OPTARG}" ;; o) OUTPUT="${OPTARG}" ;; v) SIGNATURE="${OPTARG}" VERIFY=1 ;; h) usage ;; *) echo "Unknown argument \"${option}\"" usage ;; esac done trap cleanup EXIT checkDependencies [ -n "${IMAGE}" ] || usage [ -f "${IMAGE}" ] || die "Source image \"${IMAGE}\" not found" if [ "${VERIFY}" = 1 ]; then verifySig "${SIGNATURE}" else [ -n "${OUTPUT}" ] || usage writeSig fi