Back to home page

OSCL-LXR

 
 

    


0001 #!/bin/bash
0002 # SPDX-License-Identifier: GPL-2.0
0003 #
0004 # Copyright © 2020, Microsoft Corporation. All rights reserved.
0005 #
0006 # Author: Mickaël Salaün <mic@linux.microsoft.com>
0007 #
0008 # Compute and print the To Be Signed (TBS) hash of a certificate.  This is used
0009 # as description of keys in the blacklist keyring to identify certificates.
0010 # This output should be redirected, without newline, in a file (hash0.txt) and
0011 # signed to create a PKCS#7 file (hash0.p7s).  Both of these files can then be
0012 # loaded in the kernel with.
0013 #
0014 # Exemple on a workstation:
0015 # ./print-cert-tbs-hash.sh certificate-to-invalidate.pem > hash0.txt
0016 # openssl smime -sign -in hash0.txt -inkey builtin-private-key.pem \
0017 #               -signer builtin-certificate.pem -certfile certificate-chain.pem \
0018 #               -noattr -binary -outform DER -out hash0.p7s
0019 #
0020 # Exemple on a managed system:
0021 # keyctl padd blacklist "$(< hash0.txt)" %:.blacklist < hash0.p7s
0022 
0023 set -u -e -o pipefail
0024 
0025 CERT="${1:-}"
0026 BASENAME="$(basename -- "${BASH_SOURCE[0]}")"
0027 
0028 if [ $# -ne 1 ] || [ ! -f "${CERT}" ]; then
0029         echo "usage: ${BASENAME} <certificate>" >&2
0030         exit 1
0031 fi
0032 
0033 # Checks that it is indeed a certificate (PEM or DER encoded) and exclude the
0034 # optional PEM text header.
0035 if ! PEM="$(openssl x509 -inform DER -in "${CERT}" 2>/dev/null || openssl x509 -in "${CERT}")"; then
0036         echo "ERROR: Failed to parse certificate" >&2
0037         exit 1
0038 fi
0039 
0040 # TBSCertificate starts at the second entry.
0041 # Cf. https://tools.ietf.org/html/rfc3280#section-4.1
0042 #
0043 # Exemple of first lines printed by openssl asn1parse:
0044 #    0:d=0  hl=4 l= 763 cons: SEQUENCE
0045 #    4:d=1  hl=4 l= 483 cons: SEQUENCE
0046 #    8:d=2  hl=2 l=   3 cons: cont [ 0 ]
0047 #   10:d=3  hl=2 l=   1 prim: INTEGER           :02
0048 #   13:d=2  hl=2 l=  20 prim: INTEGER           :3CEB2CB8818D968AC00EEFE195F0DF9665328B7B
0049 #   35:d=2  hl=2 l=  13 cons: SEQUENCE
0050 #   37:d=3  hl=2 l=   9 prim: OBJECT            :sha256WithRSAEncryption
0051 RANGE_AND_DIGEST_RE='
0052 2s/^\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;
0053 7s/^\s*[0-9]\+:d=\s*[0-9]\+\s\+hl=\s*[0-9]\+\s\+l=\s*[0-9]\+\s\+prim:\s*OBJECT\s*:\(.*\)$/\1/p;
0054 '
0055 
0056 RANGE_AND_DIGEST=($(echo "${PEM}" | \
0057         openssl asn1parse -in - | \
0058         sed -n -e "${RANGE_AND_DIGEST_RE}"))
0059 
0060 if [ "${#RANGE_AND_DIGEST[@]}" != 3 ]; then
0061         echo "ERROR: Failed to parse TBSCertificate." >&2
0062         exit 1
0063 fi
0064 
0065 OFFSET="${RANGE_AND_DIGEST[0]}"
0066 END="$(( OFFSET + RANGE_AND_DIGEST[1] ))"
0067 DIGEST="${RANGE_AND_DIGEST[2]}"
0068 
0069 # The signature hash algorithm is used by Linux to blacklist certificates.
0070 # Cf. crypto/asymmetric_keys/x509_cert_parser.c:x509_note_pkey_algo()
0071 DIGEST_MATCH=""
0072 while read -r DIGEST_ITEM; do
0073         if [ -z "${DIGEST_ITEM}" ]; then
0074                 break
0075         fi
0076         if echo "${DIGEST}" | grep -qiF "${DIGEST_ITEM}"; then
0077                 DIGEST_MATCH="${DIGEST_ITEM}"
0078                 break
0079         fi
0080 done < <(openssl list -digest-commands | tr ' ' '\n' | sort -ur)
0081 
0082 if [ -z "${DIGEST_MATCH}" ]; then
0083         echo "ERROR: Unknown digest algorithm: ${DIGEST}" >&2
0084         exit 1
0085 fi
0086 
0087 echo "${PEM}" | \
0088         openssl x509 -in - -outform DER | \
0089         dd "bs=1" "skip=${OFFSET}" "count=${END}" "status=none" | \
0090         openssl dgst "-${DIGEST_MATCH}" - | \
0091         awk '{printf "tbs:" $2}'