# # Copyright (c) 2025 Dag-Erling Smørgrav # # SPDX-License-Identifier: BSD-2-Clause # . $(atf_get_srcdir)/certctl.subr # Random sets of eight non-colliding names set1() { cat <"${filename}"- mv "${filename}"- "${filename}" done } certctl_setup() { export DESTDIR="$PWD" # Create input directories mkdir -p ${DESTDIR}${DISTBASE}/usr/share/certs/trusted mkdir -p ${DESTDIR}${DISTBASE}/usr/share/certs/untrusted mkdir -p ${DESTDIR}/usr/local/share/certs # Do not create output directories; certctl will take care of it #mkdir -p ${DESTDIR}${DISTBASE}/etc/ssl/certs #mkdir -p ${DESTDIR}${DISTBASE}/etc/ssl/untrusted # Generate a random key keyname="testkey" gen_key ${keyname} # Generate certificates :>metalog.expect :>trusted.expect :>untrusted.expect metalog() { echo ".${DISTBASE}$@ type=file" >>metalog.expect } trusted() { local crtname=$1 local filename=$2 printf "%s\t%s\n" "${filename}" "${crtname}" >>trusted.expect metalog "/etc/ssl/certs/${filename}" } untrusted() { local crtname=$1 local filename=$2 printf "%s\t%s\n" "${filename}" "${crtname}" >>untrusted.expect metalog "/etc/ssl/untrusted/${filename}" } set1 | while read crtname hash ; do gen_crt ${crtname} ${keyname} mv ${crtname}.crt ${DESTDIR}${DISTBASE}/usr/share/certs/trusted trusted "${crtname}" "${hash}.0" done local c=0 coll | while read crtname hash ; do gen_crt ${crtname} ${keyname} mv ${crtname}.crt ${DESTDIR}${DISTBASE}/usr/share/certs/trusted trusted "${crtname}" "${hash}.${c}" c=$((c+1)) done set2 | while read crtname hash ; do gen_crt ${crtname} ${keyname} openssl x509 -in ${crtname}.crt rm ${crtname}.crt trusted "${crtname}" "${hash}.0" done >usr/local/share/certs/bundle.crt set3 | while read crtname hash ; do gen_crt ${crtname} ${keyname} mv ${crtname}.crt ${DESTDIR}${DISTBASE}/usr/share/certs/untrusted untrusted "${crtname}" "${hash}.0" done metalog "/etc/ssl/cert.pem" unset -f untrusted unset -f trusted unset -f metalog sortfile *.expect } check_trusted() { local crtname=$1 local subject="$(subject ${crtname})" local c=${2:-1} atf_check -e ignore -o match:"found: ${c}\$" \ openssl storeutl -noout -subject "${subject}" \ ${DESTDIR}${DISTBASE}/etc/ssl/certs atf_check -e ignore -o not-match:"found: [1-9]" \ openssl storeutl -noout -subject "${subject}" \ ${DESTDIR}${DISTBASE}/etc/ssl/untrusted } check_untrusted() { local crtname=$1 local subject="$(subject ${crtname})" local c=${2:-1} atf_check -e ignore -o not-match:"found: [1-9]" \ openssl storeutl -noout -subject "${subject}" \ ${DESTDIR}/${DISTBASE}/etc/ssl/certs atf_check -e ignore -o match:"found: ${c}\$" \ openssl storeutl -noout -subject "${subject}" \ ${DESTDIR}/${DISTBASE}/etc/ssl/untrusted } check_in_bundle() { local b=${DISTBASE}${DISTBASE+/} local crtfile=$1 local line line=$(tail +5 "${crtfile}" | head -1) atf_check grep -q "${line}" ${DESTDIR}${DISTBASE}/etc/ssl/cert.pem } check_not_in_bundle() { local b=${DISTBASE}${DISTBASE+/} local crtfile=$1 local line line=$(tail +5 "${crtfile}" | head -1) atf_check -s exit:1 grep -q "${line}" etc/ssl/cert.pem } atf_test_case rehash rehash_head() { atf_set "descr" "Test the rehash command" atf_set "require.progs" certctl } rehash_body() { certctl_setup atf_check certctl rehash # Verify non-colliding trusted certificates (set1; set2) >trusted while read crtname hash ; do check_trusted "${crtname}" done coll while read crtname hash ; do check_trusted "${crtname}" $(wc -l untrusted while read crtname hash ; do check_untrusted "${crtname}" done metalog.short atf_check diff -u metalog.expect metalog.short # certctl gets DESTDIR and DISTBASE from command line rm -f metalog.orig atf_check env -uDESTDIR -uDISTBASE \ certctl -D ${DESTDIR} -d ${DISTBASE} -U -M metalog.orig rehash sed -E 's/(type=file) .*/\1/' metalog.orig | sort >metalog.short atf_check diff -u metalog.expect metalog.short # as above, but intentionally add trailing slashes rm -f metalog.orig atf_check env -uDESTDIR -uDISTBASE \ certctl -D ${DESTDIR}// -d ${DISTBASE}/ -U -M metalog.orig rehash sed -E 's/(type=file) .*/\1/' metalog.orig | sort >metalog.short atf_check diff -u metalog.expect metalog.short } atf_test_case misc misc_head() { atf_set "descr" "Test miscellaneous edge cases" atf_set "require.progs" certctl } misc_body() { # certctl rejects DISTBASE that does not begin with a slash atf_check -s exit:1 -e match:"begin with a slash" \ certctl -d base -n rehash atf_check -s exit:1 -e match:"begin with a slash" \ env DISTBASE=base certctl -n rehash } atf_init_test_cases() { atf_add_test_case rehash atf_add_test_case list atf_add_test_case trust atf_add_test_case untrust atf_add_test_case metalog atf_add_test_case misc }