git: 9ca2cfeb4e4d - main - libsecureboot: add support for more signatures

From: Simon J. Gerraty <sjg_at_FreeBSD.org>
Date: Wed, 04 Nov 2026 23:09:06 UTC
The branch main has been updated by sjg:

URL: https://cgit.FreeBSD.org/src/commit/?id=9ca2cfeb4e4de98be0f165a12dba00de970d6f58

commit 9ca2cfeb4e4de98be0f165a12dba00de970d6f58
Author:     Simon J. Gerraty <sjg@FreeBSD.org>
AuthorDate: 2026-11-04 23:07:09 +0000
Commit:     Simon J. Gerraty <sjg@FreeBSD.org>
CommitDate: 2026-11-04 23:07:09 +0000

    libsecureboot: add support for more signatures
    
    For 2027 we need to support new, stronger signatures and hashes.
    
    Re-work Makefile.inc and local.trust.mk to define
    VE_SIGN_URL_LIST or VE_SIGNATURE_LIST (can be derrived from
    VE_SIGN_URL_LIST) containing tuples of sig/method/hash/flags and
    for VE_SIGN_URL_LIST the same plus /url.
    
    From VE_SIGNATURE_LIST we derrive VE_SIGNATURE_EXT_LIST used to populate
    signature_ext_list which defines the order in which we look for
    signatures for a manifest.
    
    We also populate signature_info_list which maps a signature ext to the
    needed hash and flags to indicate whether that hash needs to be repeated.
    
    verify_sig will lookup signature_info_list, no match means not
    supported.  For asc we just call verify_asc() otherwise we call
    verify_ec() or verify_rsa() depending on the key type found in the
    certificate chain, we pass the signature_info entry to these so they
    know which hash to use and whether to repeat it.
    
    The need to repeat the hash is a consequence of sending a hash to a
    server using signature methods that insiste on hashing their input.
    To-date only our original ECDSA signatures use this.
    A FakeHash class added in 2012 avoids that wrinkle.
    
    This is a much more flexible arrangement and should be more
    readily usable by others.
    
    Sponsored by:   Hewlett Packard Enterprise Development LP.
    
    Reviewed by:    imp
    Differential Revision:  https://reviews.freebsd.org/D61007
---
 lib/libsecureboot/Makefile.inc         |  94 ++++++++++++++++++---------
 lib/libsecureboot/README.rst           |  47 +++++++++++++-
 lib/libsecureboot/libsecureboot-priv.h |  18 ++++++
 lib/libsecureboot/local.trust.mk       |  72 +++++++++++----------
 lib/libsecureboot/openpgp/Makefile.inc |   2 +-
 lib/libsecureboot/vesigned.c           |   6 +-
 lib/libsecureboot/vets.c               | 115 ++++++++++++++++++++++++++-------
 7 files changed, 254 insertions(+), 100 deletions(-)

diff --git a/lib/libsecureboot/Makefile.inc b/lib/libsecureboot/Makefile.inc
index 21ad019a0cb5..8d62cb3f755b 100644
--- a/lib/libsecureboot/Makefile.inc
+++ b/lib/libsecureboot/Makefile.inc
@@ -1,3 +1,5 @@
+# See README.rst for details on what we are doing here
+
 .if empty(BEARSSL)
 .include "../libbearssl/Makefile.inc"
 .endif
@@ -7,12 +9,13 @@ _${__this}_:
 
 libsecureboot_src:= ${.PARSEDIR}
 
-CFLAGS+= -I${libsecureboot_src}/h
-
-CFLAGS+= -DHAVE_BR_X509_TIME_CHECK
-
 .PATH: ${.PARSEDIR}
 
+CFLAGS+= -I. \
+	-I${libsecureboot_src}/h \
+	-DHAVE_BR_X509_TIME_CHECK \
+	${XCFLAGS.${.TARGET:T:R}:U}
+
 SRCS+= \
 	readfile.c \
 	brf.c \
@@ -23,8 +26,6 @@ SRCS+= \
 SRCS+= veta.c
 .endif
 
-CFLAGS+= ${XCFLAGS.${.TARGET:T:R}:U}
-
 # we use a couple of files from ${BEARSSL}/tools
 BRSSL_CFLAGS+= -I${BEARSSL}/tools
 BRSSL_SRCS+= \
@@ -57,28 +58,44 @@ _2ndLAST_PEM_USE: .USE
 	sed -n "`grep -n .-BEGIN ${.ALLSRC:M*.pem} | tail -2 | \
 	sed 's,:.*,,' | xargs | (read a b; echo $$a,$$(($$b - 1)))`p" ${.ALLSRC:M*.pem} > ${.TARGET}
 
+# Suppress duplicate entries in a list
+# without changing the order
+M_unique ?= @x@$${"${.$$x}":?:$$x${.$$x::=1}}@
+
 # rules to populate the [tv]*.pem files we use to generate ta.h
-# and can add/alter VE_*_LIST as desired.
+# and VE_*_LIST as desired.
+# At minimum we expect this to populate VE_SIGNATURE_LIST with tuples of
+# sig/method/hash/flags in order of preference.
+# The / separator makes it easy to use combinations of :H and :T to
+# extract elements. 
+# XXXX change the format of VE_SIGNATURE_LIST and *lots* of :H
+# expressions below need to change.
 .-include "local.trust.mk"
 
-# list of hashes we support
-VE_HASH_LIST?= SHA256
-
 # list of signatures we support
-# some people don't trust ECDSA
-VE_SIGNATURE_LIST?= RSA
-
+# Each entry defines an sig/method/hash/flags
 # this list controls our search for signatures so will not be sorted
 # note: for X509 signatures we assume we can replace the trailing
 # "sig" with "certs" to find the certificate chain
 # eg. for manifest.esig we use manifest.ecerts
-VE_SIGNATURE_EXT_LIST?= sig
+VE_SIGNATURE_LIST?= sig/RSA/sha256/0
+
+.if ${VE_SIGNATURE_LIST:M*OPENPGP*} != ""
+.include "openpgp/Makefile.inc"
+.endif
+
+VE_SIGNATURE_LIST:= ${VE_SIGNATURE_LIST:${M_unique}}
+
+# list of hashes we support (2nd last element of each tuple)
+VE_HASH_LIST+= ${VE_SIGNATURE_LIST:H:T}
+VE_HASH_LIST:= ${VE_HASH_LIST:tu:O:u}
+
+# sig(ext) is first (4th last) element of each tuple
+VE_SIGNATURE_EXT_LIST?= ${VE_SIGNATURE_LIST:H:H:H}
 
 # needs to be yes for FIPS 140-2 compliance
 VE_SELF_TESTS?= no
 
-CFLAGS+= -I.
-
 .if ${VE_SIGNATURE_EXT_LIST:M*sig} != ""
 # this is what we use as our trust anchor
 CFLAGS+= -DTRUST_ANCHOR_STR=ta_PEM
@@ -88,17 +105,21 @@ XCFLAGS.vets+= -DVERIFY_CERTS_STR=vc_PEM
 .endif
 .endif
 
-# clean these up
-VE_HASH_LIST:= ${VE_HASH_LIST:tu:O:u}
-VE_SIGNATURE_LIST:= ${VE_SIGNATURE_LIST:tu:O:u}
+# method is 3rd last element of each tuple
+VE_SUPPORT_LIST+= ${VE_HASH_LIST} ${VE_SIGNATURE_LIST:H:H:T}
+# make sure we have the canonical names for the signature methods
+# we support. eg. VE_SIGNATURE_LIST might have RSA3K etc,
+# and we need RSA.
+.for x in ECDSA RSA OPENPGP
+.if ${VE_SUPPORT_LIST:tu:M*$x*} != ""
+VE_SUPPORT_LIST+= $x
+.endif
+.endfor
+# clean that up
+VE_SUPPORT_LIST:= ${VE_SUPPORT_LIST:tu:O:u}
 
 # define what we are supporting
-CFLAGS+= ${VE_HASH_LIST:@H@-DVE_$H_SUPPORT@} \
-	${VE_SIGNATURE_LIST:@S@-DVE_$S_SUPPORT@}
-
-.if ${VE_SIGNATURE_LIST:MOPENPGP} != ""
-.include "openpgp/Makefile.inc"
-.endif
+CFLAGS+= ${VE_SUPPORT_LIST:@H@-DVE_$H_SUPPORT@}
 
 .if ${VE_SELF_TESTS} != "no"
 # The input used for hash KATs
@@ -117,8 +138,8 @@ XCFLAGS.vets+= -DVE_HASH_KAT_STR=${VE_HASH_KAT_STR}
 XCFLAGS.vets+= -DVE_HASH_KAT_STRLEN=${VE_HASH_KAT_STRLEN}
 .endif
 
-# this should be updated occassionally this is 2019-01-01Z
-SOURCE_DATE_EPOCH?= 1546329600
+# this should be updated occassionally this is 2026-04-01Z
+SOURCE_DATE_EPOCH?= 1775001600
 .if ${MK_REPRODUCIBLE_BUILD} == "yes"
 BUILD_UTC?= ${SOURCE_DATE_EPOCH}
 .endif
@@ -143,6 +164,9 @@ BUILD_UTC?= ${${STAT:Ustat} -L -f %m ${BUILD_UTC_FILE}:L:sh}
 # using each supported hash method
 # to use as a Known Answer Test (needed for FIPS 140-2)
 #
+# We also populate signature_info_list describing the signatures
+# we support.
+#
 TA_PEM_LIST ?= ${.ALLSRC:N*crl*:Mt*.pem}
 VC_PEM_LIST ?= ${.ALLSRC:N*crl*:Mv*.pem}
 vets.o vets.po vets.pico: ta.h
@@ -157,15 +181,23 @@ ta.h:
 .endif
 	echo '#define BUILD_UTC ${BUILD_UTC}' >> ${.TARGET} ${.OODATE:MNOMETA_CMP}
 
+# This header contains signature info needed by verify_sig et al
+${.SUFFIXES:M*o:S,^,vets,}: vsi.h
+vsi.h:
+	@( echo '/* Autogenerated - DO NOT EDIT!!! */'; echo; \
+	echo 'static signature_info signature_info_list[] = {'; \
+	echo -e '${VE_SIGNATURE_LIST:@x@\t{ "${x:H:H:H}", "${x:H:H:T}", br_${x:H:T:tl}_ID, ${x:T} },${.newline}@}'; \
+	echo -e '\t{ NULL, NULL, 0, 0 },'; \
+	echo '};'; ) > ${.TARGET}
+
 # This header records our preference for signature extensions.
-vesigned.o vesigned.po vesigned.pico: vse.h
+${.SUFFIXES:M*o:S,^,vesigned,}: vse.h
 vse.h:
 	@( echo '/* Autogenerated - DO NOT EDIT!!! */'; echo; \
-	echo "static const char *signature_exts[] = {"; \
-	echo '${VE_SIGNATURE_EXT_LIST:O:u:@e@"$e",${.newline}@}'; \
+	echo "static const char *signature_ext_list[] = {"; \
+	echo '${VE_SIGNATURE_EXT_LIST:@e@"$e",${.newline}@}'; \
 	echo 'NULL };' ) > ${.TARGET}
 
-
 .for s in ${BRSSL_SRCS} ${BRSSL_DEPS}
 .ifdef BRSSL_SED
 $s: brssl.h
diff --git a/lib/libsecureboot/README.rst b/lib/libsecureboot/README.rst
index f1d3c5679d35..0d27462c8e29 100644
--- a/lib/libsecureboot/README.rst
+++ b/lib/libsecureboot/README.rst
@@ -3,6 +3,7 @@ libsecureboot
 
 This library depends one way or another on verifying detached digital
 signatures. 
+
 To do that, the necessary trust anchors need to be available.
 
 The simplest (and most attractive for an embedded system) is to
@@ -12,17 +13,57 @@ The makefile ``local.trust.mk`` is responsible for doing that.
 The file provided is just an example and depends on the environment
 here at Juniper.
 
-Within Juniper we use signing servers, which apart from signing things
+We expect ``local.trust.mk`` to set ``VE_SIGNATURE_LIST`` to a set of
+tuples as below, in order of preference.
+Each entry contains sig/alg/hash/flags:
+
+	r3sig/RSA/sha384/0
+	esig/ECDSA/sha256/1
+	acc/OPENPGP/sha256/0
+
+If using a signing server (such as that referenced below), it
+may instead define ``VE_SIGN_URL_LIST`` which is the same
+as the above, but with an extra element for the url to
+contact.  Thus, ``VE_SIGNATURE_LIST`` can be derrived from
+``${VE_SIGN_URL_LIST:H}``.
+
+From ``VE_SIGNATURE_LIST`` we derrive:
+
+``VE_SUPPORTED_LIST``
+
+	a list of hash and signature methods to support.
+	Each results in ``-DVE_<thing>_SUPPORT`` being added to ``CFLAGS``.
+
+``VE_SIGNATURE_EXT_LIST``
+
+	A list of signature extensions in order of preference.
+	This is used to populate ``signature_ext_list`` used by
+	``verify_signed()`` to find the most preferred signature
+	available.
+
+Finally use ``VE_SIGNATURE_LIST`` to populate ``signature_info_list``
+used by ``verify_sig()`` to lookup the ``signature_info``, 
+which provides the hash to use and whether the hash should be hashed
+again.
+
+Within Juniper/HPE we use signing servers, which apart from signing things
 provide access to the necessary trust anchors.
 That signing server is freely available - see
 http://www.crufty.net/sjg/docs/signing-server.htm
 
+The entry above for ``esig`` has non-zero ``flags`` to indicate
+that the hash needs to re-hashed.
+This is to deal with a quirk of our orignal ECDSA signature.  Since
+the signing sever expects to receive a hash, and the signature methods
+insist on hashing the input, the data is actually hashed twice.  Since
+then, I added a FakeHash class to avoid that.
+
 X.509 certificate chains offer a lot of flexibility over time and are
 a great solution for an embedded vendor like Juniper or even
 FreeBSD.org, but are probably overkill for personal or small site use.
 
-Setting up a CA for this is rather involved so I'll just provide a
-link below to suitable tutorial below.
+Setting up a CA for this is rather involved, so I'll just provide a
+link to a suitable tutorial below.
 
 Using OpenPGP is much simpler.
 
diff --git a/lib/libsecureboot/libsecureboot-priv.h b/lib/libsecureboot/libsecureboot-priv.h
index 039b03c70ca2..5973099ff4b2 100644
--- a/lib/libsecureboot/libsecureboot-priv.h
+++ b/lib/libsecureboot/libsecureboot-priv.h
@@ -37,6 +37,24 @@ typedef struct {
 	size_t		hash_size;
 } hash_data;
 
+/*
+ * When supporting multiple signature formats
+ * we need a mapping from sig (ext) to hash_id
+ * and a clue about the method doesn't hurt.
+ * flags can indicate whether we need to hash the hash
+ * (to match a potential use of signing server; to-date
+ * only our original use of ECDSA requires this).
+ */
+typedef struct {
+	const char	*sig;		/* extension */
+	const char	*method;	/* ECDSA,RSA */
+	int		hash_id;	/* br_<hash>_ID */
+	int		flags;
+} signature_info;
+
+/* signature_info flags */
+#define SIF_HASH_TWICE	1
+
 int ve_check_hash(br_hash_compat_context *, const br_hash_class *,
     const char *, const char *, size_t);
 
diff --git a/lib/libsecureboot/local.trust.mk b/lib/libsecureboot/local.trust.mk
index f2da3d3ac452..01874c0604e6 100644
--- a/lib/libsecureboot/local.trust.mk
+++ b/lib/libsecureboot/local.trust.mk
@@ -11,7 +11,8 @@
 #VE_DEBUG_LEVEL?=3
 #VE_VERBOSE_DEFAULT?=2
 
-VE_HASH_LIST?= \
+# just to be sure
+VE_HASH_LIST+= \
 	SHA256 \
 	SHA384 \
 
@@ -21,57 +22,63 @@ VE_SELF_TESTS?= yes
 SIGNER?= /opt/sigs/sign.py
 
 .if exists(${SIGNER})
-OPENPGP_SIGNER?= ${SIGNER:H}/openpgp-sign.py
-OPENPGP_SIGN_FLAGS= -a
-OPENPGP_SIGN_HOST?= localhost
+SIGNER_asc?= ${SIGNER:H}/openpgp-sign.py
+SIGN_FLAGS_asc= -a
+SIGN_HOST_asc?= localhost
 SIGN_HOST ?= localhost
 
-# A list of name/ext/url tuples.
-# name should be one of ECDSA, OPENPGP or RSA, they can be repeated
+# A list of ext/method/hash/flags/url tuples.
+# method should include one of ECDSA, OPENPGP or RSA, they can be repeated
 # Order of ext list implies runtime preference so do not sort!
 VE_SIGN_URL_LIST?= \
-	ECDSA/esig/${SIGN_HOST}:${133%y:L:localtime} \
-	RSA/rsig/${SIGN_HOST}:${163%y:L:localtime} \
-	OPENPGP/asc/${OPENPGP_SIGN_HOST}:1234 \
+	esig/ECDSA/sha256/1/${SIGN_HOST}:${133%y:L:localtime} \
+	rsig/RSA/sha256/0/${SIGN_HOST}:${163%y:L:localtime} \
+	asc/OPENPGP/sha256/0/${SIGN_HOST_asc}:1234 \
 
-.for sig ext url in ${VE_SIGN_URL_LIST:@x@${x:H:H} ${x:H:T} ${x:T}@}
-SIGN_${sig}:= ${PYTHON} ${${sig}_SIGNER:U${SIGNER}} -u ${url} ${${sig}_SIGN_FLAGS:U-h sha256}
+# Same as VE_SIGN_URL_LIST minus the url
+VE_SIGNATURE_LIST += ${VE_SIGN_URL_LIST:H}
 
-VE_SIGNATURE_LIST+= ${sig}
-VE_SIGNATURE_EXT_LIST+= ${ext}
+.if !target(_SIGN_USE)
+_SIGN_USE:      .USE
+        @${SIGN_${.TARGET:E}} -o ${.TARGET} ${.ALLSRC:M*${.TARGET:T:R}:u}
+.endif
 
-_SIGN_${sig}_USE:	.USE
-	${SIGN_${sig}} ${.ALLSRC}
+.if !target(_TA_USE)
+_TA_USE:       .USE
+	@if test ${.TARGET:E} = pem; then \
+		${SIGN_${.TARGET:R:S,certs,sig,}} -C ${.TARGET}; \
+	else \
+		${SIGN_${.TARGET:E}} -C ${.TARGET}; \
+	fi
+.endif
 
-_TA_${sig}_USE:       .USE
-	${SIGN_${sig}} -C ${.TARGET}
+# we define SIGN_esig etc for use by _SIGN_USE and _TA_USE
+.for sig method hash flags url in ${VE_SIGN_URL_LIST:S,/, ,g}
+SIGN_${sig}?= ${PYTHON} ${SIGNER_${sig}:U${SIGNER}} -h ${hash} -u ${url} ${SIGN_FLAGS_${sig}:U}
 
-.if ${sig} == "OPENPGP"
-ta_${sig:tl}.${ext}: _TA_${sig}_USE
-ta_${ext}.h: ta_${sig:tl}.${ext}
+.if ${sig} == "asc"
+ta_${method:tl}.${sig}: _TA_USE
+ta_${sig}.h: ta_${method:tl}.${sig}
 .else
-${ext:S/sig/certs/}.pem: _TA_${sig}_USE
+${sig:S/sig/certs/}.pem: _TA_USE
 # the last cert in the chain is the one we want
-ta_${ext}.pem: ${ext:S/sig/certs/}.pem _LAST_PEM_USE
-ta.h: ta_${ext}.pem
+ta_${sig}.pem: ${sig:S/sig/certs/}.pem _LAST_PEM_USE
+ta.h: ta_${sig}.pem
 .if ${VE_SELF_TESTS} != "no"
 # we use the 2nd last cert to test verification
-vc_${ext}.pem: ${ext:S/sig/certs/}.pem _2ndLAST_PEM_USE
-ta.h: vc_${ext}.pem
+vc_${sig}.pem: ${sig:S/sig/certs/}.pem _2ndLAST_PEM_USE
+ta.h: vc_${sig}.pem
 .endif
 .endif
 .endfor
 
-# cleanup duplicates
-VE_SIGNATURE_LIST:= ${VE_SIGNATURE_LIST:O:u}
-
 .if target(ta_asc.h)
 XCFLAGS.opgp_key+= -DHAVE_TA_ASC_H
 
 .if ${VE_SELF_TESTS} != "no"
 # for self test
 vc_openpgp.asc: ta_openpgp.asc
-	${SIGN_OPENPGP} ${.ALLSRC:M*.asc}
+	${SIGN_asc} ${.ALLSRC:M*.asc}
 	mv ta_openpgp.asc.asc ${.TARGET}
 
 ta_asc.h: vc_openpgp.asc
@@ -79,7 +86,6 @@ ta_asc.h: vc_openpgp.asc
 .endif
 
 .else
-VE_SIGNATURE_LIST?= RSA
 
 # you need to provide t*.pem or t*.asc files for each trust anchor
 # below assumes they are named ta_${ext}.pem eg ta_esig.pem for ECDSA
@@ -93,14 +99,10 @@ TRUST_ANCHORS!= cd ${.CURDIR} && 'ls' -1 *.pem t*.asc 2> /dev/null || echo
 .if ${TRUST_ANCHORS:T:Mt*.pem} != ""
 ta.h: ${TRUST_ANCHORS:M*.pem}
 VE_SIGNATURE_EXT_LIST?= ${TRUST_ANCHORS:T:Mt*.pem:R:S/ta_//}
-.if ${VE_SIGNATURE_EXT_LIST:Mesig} != ""
-VE_SIGNATURE_LIST+= ECDSA
-.endif
 .endif
 
 .if ${TRUST_ANCHORS:T:Mt*.asc} != ""
-VE_SIGNATURE_LIST+= OPENPGP
-VE_SIGNATURE_EXT_LIST+= asc
+VE_SIGNATURE_LIST+= asc/OPENPGP/sha256/0
 ta_asc.h: ${TRUST_ANCHORS:M*.asc}
 .endif
 # we take the mtime of this as our baseline time
diff --git a/lib/libsecureboot/openpgp/Makefile.inc b/lib/libsecureboot/openpgp/Makefile.inc
index 3a216d205fe3..f7f781f97eb7 100644
--- a/lib/libsecureboot/openpgp/Makefile.inc
+++ b/lib/libsecureboot/openpgp/Makefile.inc
@@ -26,7 +26,7 @@ TA_ASC_LIST ?= ${.ALLSRC:Mt*.asc}
 VC_ASC_LIST ?= ${.ALLSRC:Mv*.asc}
 
 ta_asc.h: 
-.if ${VE_SIGNATURE_LIST:MOPENPGP} != ""
+.if ${VE_SIGNATURE_LIST:M*OPENPGP*} != ""
 	@( echo '/* Autogenerated - DO NOT EDIT!!! */'; echo; \
 	echo "#define HAVE_TA_ASC 1"; \
 	set -- ${TA_ASC_LIST:@f@$f ${f:T:R}@}; \
diff --git a/lib/libsecureboot/vesigned.c b/lib/libsecureboot/vesigned.c
index 4015923eabfd..58dd6e490122 100644
--- a/lib/libsecureboot/vesigned.c
+++ b/lib/libsecureboot/vesigned.c
@@ -44,15 +44,11 @@ verify_signed(const char *filename, int flags)
 	char buf[MAXPATHLEN];
 	const char **se;
 
-	for (se = signature_exts; *se; se++) {
+	for (se = signature_ext_list; *se; se++) {
 		snprintf(buf, sizeof(buf), "%s.%s", filename, *se);
 		if (stat(buf, &st) < 0 || !S_ISREG(st.st_mode))
 			continue;
 		DEBUG_PRINTF(5, ("verify_signed: %s\n", buf));
-#ifdef VE_OPENPGP_SUPPORT
-		if (strncmp(*se, "asc", 3) == 0)
-			return (verify_asc(buf, flags));
-#endif
 		return (verify_sig(buf, flags));
 	}
 	return (NULL);
diff --git a/lib/libsecureboot/vets.c b/lib/libsecureboot/vets.c
index 67d27d567485..57578eb2eccc 100644
--- a/lib/libsecureboot/vets.c
+++ b/lib/libsecureboot/vets.c
@@ -36,6 +36,7 @@
 #include "libsecureboot-priv.h"
 #include <brssl.h>
 #include <ta.h>
+#include <vsi.h>
 
 #ifndef TRUST_ANCHOR_STR
 # define TRUST_ANCHOR_STR ta_PEM
@@ -779,15 +780,15 @@ hexdigest(char *buf, size_t bufsz, unsigned char *foo, size_t foo_len)
  */
 #ifdef VE_ECDSA_SUPPORT
 static unsigned char *
-verify_ec(br_x509_pkey *pk, const char *file, const char *sigfile)
+verify_ec(br_x509_pkey *pk, const char *file, const char *sigfile,
+    signature_info *sip)
 {
-#ifdef VE_ECDSA_HASH_AGAIN
 	char *hex, hexbuf[br_sha512_SIZE * 2 + 2];
-#endif
 	unsigned char rhbuf[br_sha512_SIZE];
-	br_sha256_context ctx;
+	const br_hash_class *md;
+	br_hash_compat_context mctx;
 	unsigned char *fcp, *scp;
-	size_t flen, slen, plen;
+	size_t flen, hlen, plen, slen;
 	pem_object *po;
 	const br_ec_impl *ec;
 	br_ecdsa_vrfy vrfy;
@@ -803,21 +804,39 @@ verify_ec(br_x509_pkey *pk, const char *file, const char *sigfile)
 		free(scp);
 		return (NULL);
 	}
-	br_sha256_init(&ctx);
-	br_sha256_update(&ctx, fcp, flen);
-	br_sha256_out(&ctx, rhbuf);
-#ifdef VE_ECDSA_HASH_AGAIN
-	hex = hexdigest(hexbuf, sizeof(hexbuf), rhbuf, br_sha256_SIZE);
-	/* now hash that */
-	if (hex) {
-		br_sha256_init(&ctx);
-		br_sha256_update(&ctx, hex, strlen(hex));
-		br_sha256_out(&ctx, rhbuf);
+	switch (sip->hash_id) {
+	case br_sha256_ID:
+		md = &br_sha256_vtable;
+		hlen = br_sha256_SIZE;
+		break;
+	case br_sha384_ID:
+		md = &br_sha384_vtable;
+		hlen = br_sha384_SIZE;
+		break;
+	case br_sha512_ID:
+		md = &br_sha512_vtable;
+		hlen = br_sha512_SIZE;
+		break;
+	default:
+		free(fcp);
+		free(scp);
+		return (NULL);
+	}
+	md->init(&mctx.vtable);
+	md->update(&mctx.vtable, fcp, flen);
+	md->out(&mctx.vtable, rhbuf);
+	if ((sip->flags & SIF_HASH_TWICE) != 0) {
+		hex = hexdigest(hexbuf, sizeof(hexbuf), rhbuf, hlen);
+		/* now hash that */
+		if (hex) {
+			md->init(&mctx.vtable);
+			md->update(&mctx.vtable, hex, strlen(hex));
+			md->out(&mctx.vtable, rhbuf);
+		}
 	}
-#endif
 	ec = br_ec_get_default();
 	vrfy = br_ecdsa_vrfy_asn1_get_default();
-	if (!vrfy(ec, rhbuf, br_sha256_SIZE, &pk->key.ec, po->data,
+	if (!vrfy(ec, rhbuf, hlen, &pk->key.ec, po->data,
 		po->data_len)) {
 		free(fcp);
 		fcp = NULL;
@@ -877,8 +896,10 @@ verify_rsa_digest (br_rsa_public_key *pkey,
  */
 #ifdef VE_RSA_SUPPORT
 static unsigned char *
-verify_rsa(br_x509_pkey *pk,  const char *file, const char *sigfile)
+verify_rsa(br_x509_pkey *pk,  const char *file, const char *sigfile,
+    signature_info *sip)
 {
+	char *hex, hexbuf[br_sha512_SIZE * 2 + 2];
 	unsigned char rhbuf[br_sha512_SIZE];
 	const unsigned char *hash_oid;
 	const br_hash_class *md;
@@ -899,24 +920,46 @@ verify_rsa(br_x509_pkey *pk,  const char *file, const char *sigfile)
 		return (NULL);
 	}
 
-	switch (po->data_len) {
-#if defined(UNIT_TEST) && defined(VE_DEPRECATED_RSA_SHA1_SUPPORT)
-	case 256:
-		// this is our old deprecated sig method
+	switch (sip->hash_id) {
+#ifdef VE_SHA1_SUPPORT			/* deprecated */
+	case br_sha1_ID:
 		md = &br_sha1_vtable;
 		hlen = br_sha1_SIZE;
 		hash_oid = BR_HASH_OID_SHA1;
 		break;
 #endif
-	default:
+	case br_sha256_ID:
 		md = &br_sha256_vtable;
 		hlen = br_sha256_SIZE;
 		hash_oid = BR_HASH_OID_SHA256;
 		break;
+	case br_sha384_ID:
+		md = &br_sha384_vtable;
+		hlen = br_sha384_SIZE;
+		hash_oid = BR_HASH_OID_SHA384;
+		break;
+	case br_sha512_ID:
+		md = &br_sha512_vtable;
+		hlen = br_sha512_SIZE;
+		hash_oid = BR_HASH_OID_SHA512;
+		break;
+	default:
+		free(fcp);
+		free(scp);
+		return (NULL);
 	}
 	md->init(&mctx.vtable);
 	md->update(&mctx.vtable, fcp, flen);
 	md->out(&mctx.vtable, rhbuf);
+	if ((sip->flags & SIF_HASH_TWICE) != 0) {
+		hex = hexdigest(hexbuf, sizeof(hexbuf), rhbuf, hlen);
+		/* now hash that */
+		if (hex) {
+			md->init(&mctx.vtable);
+			md->update(&mctx.vtable, hex, strlen(hex));
+			md->out(&mctx.vtable, rhbuf);
+		}
+	}
 	if (!verify_rsa_digest(&pk->key.rsa, hash_oid,
 		rhbuf, hlen, po->data, po->data_len)) {
 		free(fcp);
@@ -951,9 +994,31 @@ verify_sig(const char *sigfile, int flags)
 	char pbuf[MAXPATHLEN];
 	char *cp;
 	unsigned char *ucp;
+	signature_info *sip;
+	const char *sig;
 	size_t n;
 
 	DEBUG_PRINTF(5, ("verify_sig: %s\n", sigfile));
+
+	/* find the signature_info for this signature ext */
+	if ((sig = strrchr(sigfile, '.')) != NULL) {
+		sig++;
+
+		for (sip = signature_info_list; sip->sig != NULL; sip++) {
+			if (strcmp(sig, sip->sig) == 0)
+				break;
+		}
+	} else
+		sip = NULL;
+
+	if (sip == NULL || sip->sig == NULL)
+		return NULL;		/* not valid */
+	if (strcmp(sig, "asc") == 0)
+#ifdef VE_OPENPGP_SUPPORT
+		return verify_asc(sigfile, flags);
+#else
+		return NULL;		/* we shouldn't get here */
+#endif
 	n = strlcpy(pbuf, sigfile, sizeof(pbuf));
 	if (n > (sizeof(pbuf) - 5) || strcmp(&sigfile[n - 3], "sig") != 0)
 		return (NULL);
@@ -984,12 +1049,12 @@ verify_sig(const char *sigfile, int flags)
 	switch (pk->key_type) {
 #ifdef VE_ECDSA_SUPPORT
 	case BR_KEYTYPE_EC:
-		ucp = verify_ec(pk, pbuf, sigfile);
+		ucp = verify_ec(pk, pbuf, sigfile, sip);
 		break;
 #endif
 #ifdef VE_RSA_SUPPORT
 	case BR_KEYTYPE_RSA:
-		ucp = verify_rsa(pk, pbuf, sigfile);
+		ucp = verify_rsa(pk, pbuf, sigfile, sip);
 		break;
 #endif
 	default: