#!/bin/bash1# SPDX-License-Identifier: GPL-2.02#3# Copyright © 2020, Microsoft Corporation. All rights reserved.4#5# Author: Mickaël Salaün <[email protected]>6#7# Compute and print the To Be Signed (TBS) hash of a certificate. This is used8# as description of keys in the blacklist keyring to identify certificates.9# This output should be redirected, without newline, in a file (hash0.txt) and10# signed to create a PKCS#7 file (hash0.p7s). Both of these files can then be11# loaded in the kernel with.12#13# Exemple on a workstation:14# ./print-cert-tbs-hash.sh certificate-to-invalidate.pem > hash0.txt15# openssl smime -sign -in hash0.txt -inkey builtin-private-key.pem \16# -signer builtin-certificate.pem -certfile certificate-chain.pem \17# -noattr -binary -outform DER -out hash0.p7s18#19# Exemple on a managed system:20# keyctl padd blacklist "$(< hash0.txt)" %:.blacklist < hash0.p7s2122set -u -e -o pipefail2324CERT="${1:-}"25BASENAME="$(basename -- "${BASH_SOURCE[0]}")"2627if [ $# -ne 1 ] || [ ! -f "${CERT}" ]; then28echo "usage: ${BASENAME} <certificate>" >&229exit 130fi3132# Checks that it is indeed a certificate (PEM or DER encoded) and exclude the33# optional PEM text header.34if ! PEM="$(openssl x509 -inform DER -in "${CERT}" 2>/dev/null || openssl x509 -in "${CERT}")"; then35echo "ERROR: Failed to parse certificate" >&236exit 137fi3839# TBSCertificate starts at the second entry.40# Cf. https://tools.ietf.org/html/rfc3280#section-4.141#42# Exemple of first lines printed by openssl asn1parse:43# 0:d=0 hl=4 l= 763 cons: SEQUENCE44# 4:d=1 hl=4 l= 483 cons: SEQUENCE45# 8:d=2 hl=2 l= 3 cons: cont [ 0 ]46# 10:d=3 hl=2 l= 1 prim: INTEGER :0247# 13:d=2 hl=2 l= 20 prim: INTEGER :3CEB2CB8818D968AC00EEFE195F0DF9665328B7B48# 35:d=2 hl=2 l= 13 cons: SEQUENCE49# 37:d=3 hl=2 l= 9 prim: OBJECT :sha256WithRSAEncryption50RANGE_AND_DIGEST_RE='512s/^\s*\([0-9]\+\):d=\s*[0-9]\+\s\+hl=\s*[0-9]\+\s\+l=\s*\([0-9]\+\)\s\+cons:\s*SEQUENCE\s*$/\1 \2/p;527s/^\s*[0-9]\+:d=\s*[0-9]\+\s\+hl=\s*[0-9]\+\s\+l=\s*[0-9]\+\s\+prim:\s*OBJECT\s*:\(.*\)$/\1/p;53'5455RANGE_AND_DIGEST=($(echo "${PEM}" | \56openssl asn1parse -in - | \57sed -n -e "${RANGE_AND_DIGEST_RE}"))5859if [ "${#RANGE_AND_DIGEST[@]}" != 3 ]; then60echo "ERROR: Failed to parse TBSCertificate." >&261exit 162fi6364OFFSET="${RANGE_AND_DIGEST[0]}"65END="$(( OFFSET + RANGE_AND_DIGEST[1] ))"66DIGEST="${RANGE_AND_DIGEST[2]}"6768# The signature hash algorithm is used by Linux to blacklist certificates.69# Cf. crypto/asymmetric_keys/x509_cert_parser.c:x509_note_pkey_algo()70DIGEST_MATCH=""71while read -r DIGEST_ITEM; do72if [ -z "${DIGEST_ITEM}" ]; then73break74fi75if echo "${DIGEST}" | grep -qiF "${DIGEST_ITEM}"; then76DIGEST_MATCH="${DIGEST_ITEM}"77break78fi79done < <(openssl list -digest-commands | tr ' ' '\n' | sort -ur)8081if [ -z "${DIGEST_MATCH}" ]; then82echo "ERROR: Unknown digest algorithm: ${DIGEST}" >&283exit 184fi8586echo "${PEM}" | \87openssl x509 -in - -outform DER | \88dd "bs=1" "skip=${OFFSET}" "count=${END}" "status=none" | \89openssl dgst "-${DIGEST_MATCH}" - | \90awk '{printf "tbs:" $2}'919293