From nobody Thu Oct 05 15:11:31 2023 X-Original-To: dev-commits-src-main@mlmmj.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mlmmj.nyi.freebsd.org (Postfix) with ESMTP id 4S1ZlS2D68z4vjsw; Thu, 5 Oct 2023 15:11:32 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from mxrelay.nyi.freebsd.org (mxrelay.nyi.freebsd.org [IPv6:2610:1c1:1:606c::19:3]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256 client-signature RSA-PSS (4096 bits) client-digest SHA256) (Client CN "mxrelay.nyi.freebsd.org", Issuer "R3" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4S1ZlS1RYPz4YnZ; Thu, 5 Oct 2023 15:11:32 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1696518692; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=xp84SFSl/oFw7ookW6F8zp+BY8XWH+KvsetuIOwVKAo=; b=BbumE4T8KoT2uPcSdNnFXtdwFMs77SRpc1oEkT+ZbuDlviO6SPCuPNT3o7L0PPrpOeUR+X IJ3jZJPb32L1+3RHT85V1D1/jggZ/rSsLgDeqO+fAh4EKHvann9R6euiElJoVR9Ic3eNX+ mXxKBsDUZqUHTFYhNPFyK5M3N2cFEOA2B+mflrx+JbNQRVFISKiprnoi8YyBTxXMsTBdgS fLCz9VgIB/5mjzjvsL/GmEQLcHWVNgGExI1qGAvYPgq/0enxsxkYDMFOu04hzw7OGm0EP8 eRW1T6IMR+Lz4B8ji/Yxh5QWHtiOtHJLT201u04jOxXwHNLIaPHxMq7JcvDOWA== ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1696518692; a=rsa-sha256; cv=none; b=dmyUU2greLCqk11isig9VanpGCsMLuAv4+3HfrX8oAIJEELsgkZ2djaXDDYQlxGRC9/9lv 9JauHtQ0MFSQgRBbv3NCykUgG90pXGDZEaOnVRlX7hz/uCwX5kLqYK82BMYwM2kPitCLus 4yyOr/vQt5GPAEZG6q16owBeHF6x8iV0dSB+KiiNlr1NX8LdQnjnn6VMdsE6PvZHoO4oja e62EyUw+mVcdU1B9Qh57DjVwBW5BH4CfZxdnGxQvzuN5uNhMGnjV4Y0w7ejV3++JLZ9h9i uD2EdGOpSPzFRCY/M0kpKdU3w3tA17uBUHYveJQmh85UwlYIWM/QxzkimrCSlw== ARC-Authentication-Results: i=1; mx1.freebsd.org; none ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1696518692; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=xp84SFSl/oFw7ookW6F8zp+BY8XWH+KvsetuIOwVKAo=; b=yDRDEjVVSDWBPv7lGSfL0uCC0pR6iVziYYosd9jx/4HF8CDNQr7evpU7nRuOtTmtdJhUc2 qqoKnOu6ERlKR+UevhMM+QYtsPBS3HE2Vet7gK2RT5269X+DdxWBMMxD2R40RqI7sD/pMd p5P/+dsTBbLxn+5pPDWffA0o53cOFG66PCjEbjOWWXfquyzJfXm3Hb9FFBO+f2WGqEgO3j cfNn0hVN/iyGitPFjl6P3cMHk3Dde5F7Zcumw6dnNjj3AfHl88v5cEBNgUMvDpFUaAmIDy FXgMn8DDdq5kxQUN6skkAEY/Um3j8KDYLcSEKYVeMSHxR32XZ8PoMxSFsyG7eg== Received: from gitrepo.freebsd.org (gitrepo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:5]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (Client did not present a certificate) by mxrelay.nyi.freebsd.org (Postfix) with ESMTPS id 4S1ZlS0XV3z1Pcn; Thu, 5 Oct 2023 15:11:32 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from gitrepo.freebsd.org ([127.0.1.44]) by gitrepo.freebsd.org (8.17.1/8.17.1) with ESMTP id 395FBV8t072842; Thu, 5 Oct 2023 15:11:31 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.17.1/8.17.1/Submit) id 395FBVR6072839; Thu, 5 Oct 2023 15:11:31 GMT (envelope-from git) Date: Thu, 5 Oct 2023 15:11:31 GMT Message-Id: <202310051511.395FBVR6072839@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-main@FreeBSD.org From: Dag-Erling =?utf-8?Q?Sm=C3=B8rgrav?= Subject: git: a401c8cb26b2 - main - certctl: Split certificate bundles before processing. List-Id: Commit messages for the main branch of the src repository List-Archive: https://lists.freebsd.org/archives/dev-commits-src-main List-Help: List-Post: List-Subscribe: List-Unsubscribe: Sender: owner-dev-commits-src-main@freebsd.org X-BeenThere: dev-commits-src-main@freebsd.org MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Git-Committer: des X-Git-Repository: src X-Git-Refname: refs/heads/main X-Git-Reftype: branch X-Git-Commit: a401c8cb26b22688087ad7c5ee527718459df15a Auto-Submitted: auto-generated The branch main has been updated by des: URL: https://cgit.FreeBSD.org/src/commit/?id=a401c8cb26b22688087ad7c5ee527718459df15a commit a401c8cb26b22688087ad7c5ee527718459df15a Author: Dag-Erling Smørgrav AuthorDate: 2023-10-05 14:50:01 +0000 Commit: Dag-Erling Smørgrav CommitDate: 2023-10-05 15:11:22 +0000 certctl: Split certificate bundles before processing. This allows 'certctl rehash' to do the right thing when ca_root_nss is installed, instead of linking the entire bundle to the hash of the first certificate it contains. MFC after: 3 days Reviewed by: allanjude Differential Revision: https://reviews.freebsd.org/D42087 --- usr.sbin/certctl/certctl.sh | 99 ++++++++++++++++++++++++++++----------------- 1 file changed, 63 insertions(+), 36 deletions(-) diff --git a/usr.sbin/certctl/certctl.sh b/usr.sbin/certctl/certctl.sh index 02d055102c33..b7d3a95bc7d7 100755 --- a/usr.sbin/certctl/certctl.sh +++ b/usr.sbin/certctl/certctl.sh @@ -32,7 +32,6 @@ set -u : ${DESTDIR:=} : ${DISTBASE:=} -: ${FILEPAT:="\.pem$|\.crt$|\.cer$|\.crl$"} ############################################################ GLOBALS @@ -63,6 +62,16 @@ perform() fi } +cert_files_in() +{ + find -L "$@" -type f \( \ + -name '*.pem' -or \ + -name '*.crt' -or \ + -name '*.cer' -or \ + -name '*.crl' \ + \) 2>/dev/null +} + do_hash() { local hash @@ -93,23 +102,32 @@ get_decimal() return 0 } -create_trusted_link() +create_trusted() { local hash certhash otherfile otherhash local suffix + local link=${2:+-lm} hash=$(do_hash "$1") || return certhash=$(openssl x509 -sha1 -in "$1" -noout -fingerprint) for otherfile in $(find $UNTRUSTDESTDIR -name "$hash.*") ; do otherhash=$(openssl x509 -sha1 -in "$otherfile" -noout -fingerprint) if [ "$certhash" = "$otherhash" ] ; then - info "Skipping untrusted certificate $1 ($otherfile)" + info "Skipping untrusted certificate $hash ($otherfile)" return 1 fi done + for otherfile in $(find $CERTDESTDIR -name "$hash.*") ; do + otherhash=$(openssl x509 -sha1 -in "$otherfile" -noout -fingerprint) + if [ "$certhash" = "$otherhash" ] ; then + verbose "Skipping duplicate entry for certificate $hash" + return 0 + fi + done suffix=$(get_decimal "$CERTDESTDIR" "$hash") verbose "Adding $hash.$suffix to trust store" - perform install ${INSTALLFLAGS} -lrs "$(realpath "$1")" "$CERTDESTDIR/$hash.$suffix" + perform install ${INSTALLFLAGS} -m 0444 ${link} \ + "$(realpath "$1")" "$CERTDESTDIR/$hash.$suffix" } # Accepts either dot-hash form from `certctl list` or a path to a valid cert. @@ -137,6 +155,7 @@ resolve_certname() create_untrusted() { local srcfile filename + local link=${2:+-lm} set -- $(resolve_certname "$1") srcfile=$1 @@ -147,12 +166,13 @@ create_untrusted() fi verbose "Adding $filename to untrusted list" - perform install ${INSTALLFLAGS} -lrs "$srcfile" "$UNTRUSTDESTDIR/$filename" + perform install ${INSTALLFLAGS} -m 0444 ${link} \ + "$srcfile" "$UNTRUSTDESTDIR/$filename" } do_scan() { - local CFUNC CSEARCH CPATH CFILE + local CFUNC CSEARCH CPATH CFILE CERT SPLITDIR local oldIFS="$IFS" CFUNC="$1" CSEARCH="$2" @@ -160,14 +180,25 @@ do_scan() IFS=: set -- $CSEARCH IFS="$oldIFS" - for CPATH in "$@"; do - [ -d "$CPATH" ] || continue - info "Scanning $CPATH for certificates..." - for CFILE in $(ls -1 "${CPATH}" | grep -Ee "${FILEPAT}") ; do - [ -e "$CPATH/$CFILE" ] || continue - verbose "Reading $CFILE" - "$CFUNC" "$CPATH/$CFILE" - done + for CFILE in $(cert_files_in "$@") ; do + verbose "Reading $CFILE" + case $(grep -c '^Certificate:$' "$CFILE") in + 0) + ;; + 1) + "$CFUNC" "$CFILE" link + ;; + *) + verbose "Multiple certificates found, splitting..." + SPLITDIR=$(mktemp -d) + egrep '^[^#]' "$CFILE" | \ + split -p '^Certificate:$' - "$SPLITDIR/x" + for CERT in $(find "$SPLITDIR" -type f) ; do + "$CFUNC" "$CERT" + done + rm -rf "$SPLITDIR" + ;; + esac done } @@ -175,43 +206,39 @@ do_list() { local CFILE subject - if [ -e "$1" ] ; then - cd "$1" - for CFILE in *.[0-9] ; do - if [ ! -s "$CFILE" ] ; then - info "Unable to read $CFILE" - ERRORS=$((ERRORS + 1)) - continue - fi - subject= - if [ $VERBOSE -eq 0 ] ; then - subject=$(openssl x509 -noout -subject -nameopt multiline -in "$CFILE" | - sed -n '/commonName/s/.*= //p') - fi - [ "$subject" ] || - subject=$(openssl x509 -noout -subject -in "$CFILE") - printf "%s\t%s\n" "$CFILE" "$subject" - done - cd - - fi + for CFILE in $(find "$@" \( -type f -or -type l \) -name '*.[0-9]') ; do + if [ ! -s "$CFILE" ] ; then + info "Unable to read $CFILE" + ERRORS=$((ERRORS + 1)) + continue + fi + subject= + if ! "$VERBOSE" ; then + subject=$(openssl x509 -noout -subject -nameopt multiline -in "$CFILE" | sed -n '/commonName/s/.*= //p') + fi + if [ -z "$subject" ] ; then + subject=$(openssl x509 -noout -subject -in "$CFILE") + fi + printf "%s\t%s\n" "${CFILE##*/}" "$subject" + done } cmd_rehash() { if [ -e "$CERTDESTDIR" ] ; then - perform find "$CERTDESTDIR" -type link -delete + perform find "$CERTDESTDIR" \( -type f -or -type l \) -delete else perform install -d -m 0755 "$CERTDESTDIR" fi if [ -e "$UNTRUSTDESTDIR" ] ; then - perform find "$UNTRUSTDESTDIR" -type link -delete + perform find "$UNTRUSTDESTDIR" \( -type f -or -type l \) -delete else perform install -d -m 0755 "$UNTRUSTDESTDIR" fi do_scan create_untrusted "$UNTRUSTPATH" - do_scan create_trusted_link "$TRUSTPATH" + do_scan create_trusted "$TRUSTPATH" } cmd_list()