Path: blob/main/usr.sbin/certctl/tests/certctl_test.sh
102637 views
#1# Copyright (c) 2025 Dag-Erling Smørgrav <[email protected]>2#3# SPDX-License-Identifier: BSD-2-Clause4#56. $(atf_get_srcdir)/certctl.subr78# Random sets of eight non-colliding names9set1()10{11cat <<EOF12AVOYKJHSLFHWPVQMKBHENUAHJTEGMCCB 0ca83bbe13UYSYXKDNNJTYOQPBGIKQDHRJYZHTDPKK 0d9a651214LODHGFXMZYKGOKAYGWTMMYQJYHDATDDM 4e6219f515NBBTQHJLHKBFFFWJTHHSNKOQYMGLHLPW 5dd76abc16BJFAQZXZHYQLIDDPCAQFPDMNXICUXBXW ad68573d17IOKNTHVEVVIJMNMYAVILMEMQQWLVRESN b577803d18BHGMAJJGNJPIVMHMFCUTJLGFROJICEKN c98a633819HCRFQMGDQJALMLUQNXMPGLXFLLJRODJW f50c637920EOF21}2223set2()24{25cat <<EOF26GOHKZTSKIPDSYNLMGYXGLROPTATELXIU 30789c8827YOOTYHEGHZIYFXOBLNKENPSJUDGOPJJU 7fadbc1328ETRINNYBGKIENAVGOKVJYFSSHFZIJZRH 8ed664af29DBFGMFFMRNLPQLQPOLXOEUVLCRXLRSWT 8f34355e30WFOPBQPLQFHDHZOUQFEIDGSYDUOTSNDQ ac0471df31HMNETZMGNIWRGXQCVZXVZGWSGFBRRDQC b32f147232SHFYBXDVAUACBFPPAIGDAQIAGYOYGMQE baca75fa33PCBGDNVPYCDGNRQSGRSLXFHYKXLAVLHW ddeeae0134EOF35}3637set3()38{39cat <<EOF40NJWIRLPWAIICVJBKXXHFHLCPAERZATRL 000aa2e541RJAENDPOCZQEVCPFUWOWDXPCSMYJPVYC 021b95a342PQUQDSWHBNVLBTNBGONYRLGZZVEFXVLO 071e8c5043VZEXRKJUPZSFBDWBOLUZXOGLNTEAPCZM 3af7bb9b44ZXOWOXQTXNZMAMZIWVFDZDJEWOOAGAOH 48d5c7cc45KQSFQYVJMFTMADIHJIWGSQISWKSHRYQO 509f5ba146AIECYSLWZOIEPJWWUTWSQXCNCIHHZHYI 8cb0c50347RFHWDJZEPOFLMPGXAHVEJFHCDODAPVEV 9ae4e04948EOF49}5051# Random set of three colliding names52collhash=f2888ce353coll()54{55cat <<EOF56EJFTZEOANQLOYPEHWWXBWEWEFVKHMSNA $collhash57LEMRWZAZLKZLPPSFLNLQZVGKKBEOFYWG $collhash58ZWUPHYWKKTVEFBJOLLPDAIKGRDFVXZID $collhash59EOF60}6162sortfile() {63for filename; do64sort "${filename}" >"${filename}"-65mv "${filename}"- "${filename}"66done67}6869certctl_setup()70{71export DESTDIR="$PWD"7273# Create input directories74mkdir -p ${DESTDIR}${DISTBASE}/usr/share/certs/trusted75mkdir -p ${DESTDIR}${DISTBASE}/usr/share/certs/untrusted76mkdir -p ${DESTDIR}/usr/local/share/certs7778# Do not create output directories; certctl will take care of it79#mkdir -p ${DESTDIR}${DISTBASE}/etc/ssl/certs80#mkdir -p ${DESTDIR}${DISTBASE}/etc/ssl/untrusted8182# Generate a random key83keyname="testkey"84gen_key ${keyname}8586# Generate certificates87:>metalog.expect88:>trusted.expect89:>untrusted.expect90metalog() {91echo ".${DISTBASE}$@ type=file" >>metalog.expect92}93trusted() {94local crtname=$195local filename=$296printf "%s\t%s\n" "${filename}" "${crtname}" >>trusted.expect97metalog "/etc/ssl/certs/${filename}"98}99untrusted() {100local crtname=$1101local filename=$2102printf "%s\t%s\n" "${filename}" "${crtname}" >>untrusted.expect103metalog "/etc/ssl/untrusted/${filename}"104}105set1 | while read crtname hash ; do106gen_crt ${crtname} ${keyname}107mv ${crtname}.crt ${DESTDIR}${DISTBASE}/usr/share/certs/trusted108trusted "${crtname}" "${hash}.0"109done110local c=0111coll | while read crtname hash ; do112gen_crt ${crtname} ${keyname}113mv ${crtname}.crt ${DESTDIR}${DISTBASE}/usr/share/certs/trusted114trusted "${crtname}" "${hash}.${c}"115c=$((c+1))116done117set2 | while read crtname hash ; do118gen_crt ${crtname} ${keyname}119openssl x509 -in ${crtname}.crt120rm ${crtname}.crt121trusted "${crtname}" "${hash}.0"122done >usr/local/share/certs/bundle.crt123set3 | while read crtname hash ; do124gen_crt ${crtname} ${keyname}125mv ${crtname}.crt ${DESTDIR}${DISTBASE}/usr/share/certs/untrusted126untrusted "${crtname}" "${hash}.0"127done128metalog "/etc/ssl/cert.pem"129unset -f untrusted130unset -f trusted131unset -f metalog132sortfile *.expect133}134135check_trusted() {136local crtname=$1137local subject="$(subject ${crtname})"138local c=${2:-1}139140atf_check -e ignore -o match:"found: ${c}\$" \141openssl storeutl -noout -subject "${subject}" \142${DESTDIR}${DISTBASE}/etc/ssl/certs143atf_check -e ignore -o not-match:"found: [1-9]" \144openssl storeutl -noout -subject "${subject}" \145${DESTDIR}${DISTBASE}/etc/ssl/untrusted146}147148check_untrusted() {149local crtname=$1150local subject="$(subject ${crtname})"151local c=${2:-1}152153atf_check -e ignore -o not-match:"found: [1-9]" \154openssl storeutl -noout -subject "${subject}" \155${DESTDIR}/${DISTBASE}/etc/ssl/certs156atf_check -e ignore -o match:"found: ${c}\$" \157openssl storeutl -noout -subject "${subject}" \158${DESTDIR}/${DISTBASE}/etc/ssl/untrusted159}160161check_in_bundle() {162local b=${DISTBASE}${DISTBASE+/}163local crtfile=$1164local line165166line=$(tail +5 "${crtfile}" | head -1)167atf_check grep -q "${line}" ${DESTDIR}${DISTBASE}/etc/ssl/cert.pem168}169170check_not_in_bundle() {171local b=${DISTBASE}${DISTBASE+/}172local crtfile=$1173local line174175line=$(tail +5 "${crtfile}" | head -1)176atf_check -s exit:1 grep -q "${line}" etc/ssl/cert.pem177}178179atf_test_case rehash180rehash_head()181{182atf_set "descr" "Test the rehash command"183}184rehash_body()185{186certctl_setup187atf_check certctl rehash188189# Verify non-colliding trusted certificates190(set1; set2) >trusted191while read crtname hash ; do192check_trusted "${crtname}"193done <trusted194195# Verify colliding trusted certificates196coll >coll197while read crtname hash ; do198check_trusted "${crtname}" $(wc -l <coll)199done <coll200201# Verify untrusted certificates202set3 >untrusted203while read crtname hash ; do204check_untrusted "${crtname}"205done <untrusted206207# Verify bundle208for f in etc/ssl/certs/*.? ; do209check_in_bundle "${f}"210done211for f in etc/ssl/untrusted/*.? ; do212check_not_in_bundle "${f}"213done214}215216atf_test_case list217list_head()218{219atf_set "descr" "Test the list and untrusted commands"220}221list_body()222{223certctl_setup224atf_check certctl rehash225226atf_check -o save:trusted.out certctl list227sortfile trusted.out228# the ordering of the colliding certificates is partly229# determined by fields that change every time we regenerate230# them, so ignore them in the diff231atf_check diff -u \232--ignore-matching-lines $collhash \233trusted.expect trusted.out234235atf_check -o save:untrusted.out certctl untrusted236sortfile untrusted.out237atf_check diff -u \238untrusted.expect untrusted.out239}240241atf_test_case trust242trust_head()243{244atf_set "descr" "Test the trust command"245}246trust_body()247{248certctl_setup249atf_check certctl rehash250crtname=$(set3 | (read crtname hash ; echo ${crtname}))251crtfile=usr/share/certs/untrusted/${crtname}.crt252check_untrusted ${crtname}253check_not_in_bundle ${crtfile}254atf_check -e match:"was previously untrusted" \255certctl trust ${crtfile}256check_trusted ${crtname}257check_in_bundle ${crtfile}258}259260atf_test_case untrust261untrust_head()262{263atf_set "descr" "Test the untrust command"264}265untrust_body()266{267certctl_setup268atf_check certctl rehash269crtname=$(set1 | (read crtname hash ; echo ${crtname}))270crtfile=usr/share/certs/trusted/${crtname}.crt271check_trusted "${crtname}"272check_in_bundle ${crtfile}273atf_check certctl untrust "${crtfile}"274check_untrusted "${crtname}"275check_not_in_bundle ${crtfile}276}277278atf_test_case metalog279metalog_head()280{281atf_set "descr" "Verify the metalog"282}283metalog_body()284{285export DISTBASE=/base286certctl_setup287288# certctl gets DESTDIR and DISTBASE from environment289rm -f metalog.orig290atf_check certctl -U -M metalog.orig rehash291sed -E 's/(type=file) .*/\1/' metalog.orig | sort >metalog.short292atf_check diff -u metalog.expect metalog.short293294# certctl gets DESTDIR and DISTBASE from command line295rm -f metalog.orig296atf_check env -uDESTDIR -uDISTBASE \297certctl -D ${DESTDIR} -d ${DISTBASE} -U -M metalog.orig rehash298sed -E 's/(type=file) .*/\1/' metalog.orig | sort >metalog.short299atf_check diff -u metalog.expect metalog.short300301# as above, but intentionally add trailing slashes302rm -f metalog.orig303atf_check env -uDESTDIR -uDISTBASE \304certctl -D ${DESTDIR}// -d ${DISTBASE}/ -U -M metalog.orig rehash305sed -E 's/(type=file) .*/\1/' metalog.orig | sort >metalog.short306atf_check diff -u metalog.expect metalog.short307}308309atf_test_case misc310misc_head()311{312atf_set "descr" "Test miscellaneous edge cases"313}314misc_body()315{316# certctl rejects DISTBASE that does not begin with a slash317atf_check -s exit:1 -e match:"begin with a slash" \318certctl -d base -n rehash319atf_check -s exit:1 -e match:"begin with a slash" \320env DISTBASE=base certctl -n rehash321}322323atf_init_test_cases()324{325atf_add_test_case rehash326atf_add_test_case list327atf_add_test_case trust328atf_add_test_case untrust329atf_add_test_case metalog330atf_add_test_case misc331}332333334