The branch main has been updated by gordon:

URL: 
https://cgit.FreeBSD.org/src/commit/?id=94a83b0be10654bccf0da7f4014dd847e0d925f5

commit 94a83b0be10654bccf0da7f4014dd847e0d925f5
Author:     Gordon Tetlow <[email protected]>
AuthorDate: 2026-12-04 17:00:10 +0000
Commit:     Gordon Tetlow <[email protected]>
CommitDate: 2026-12-04 17:20:18 +0000

    pkg: Accept SHA-512 certificate fingerprints
    
    Accept SHA-512 certificate fingerprints ("function: sha512") in the
    trusted and revoked fingerprint directories, as pkg(8) does.  Trusted
    and revoked fingerprints are each compared against the certificate
    digest of their own type, so a revocation of either type applies.
    
    Reviewed by:    kevans
    MFC after:      1 week
    Differential Revision:  https://reviews.freebsd.org/D62408
---
 usr.sbin/pkg/hash.c | 15 +++++++++++++++
 usr.sbin/pkg/hash.h |  2 ++
 usr.sbin/pkg/pkg.c  | 44 +++++++++++++++++++++++++++++++++++++-------
 3 files changed, 54 insertions(+), 7 deletions(-)

diff --git a/usr.sbin/pkg/hash.c b/usr.sbin/pkg/hash.c
index b3e6334da6a2..49f63e937214 100644
--- a/usr.sbin/pkg/hash.c
+++ b/usr.sbin/pkg/hash.c
@@ -28,6 +28,7 @@
  */
 
 #include <sha256.h>
+#include <sha512.h>
 #include <unistd.h>
 
 #include "hash.h"
@@ -45,3 +46,17 @@ sha256_fd(int fd)
 
        return (SHA256_Fd(fd, NULL));
 }
+
+char *
+sha512_buf(char *buf, size_t len)
+{
+
+       return (SHA512_Data(buf, len, NULL));
+}
+
+char *
+sha512_fd(int fd)
+{
+
+       return (SHA512_Fd(fd, NULL));
+}
diff --git a/usr.sbin/pkg/hash.h b/usr.sbin/pkg/hash.h
index 2ade4b7e7a1f..220c64b7ff61 100644
--- a/usr.sbin/pkg/hash.h
+++ b/usr.sbin/pkg/hash.h
@@ -30,3 +30,5 @@
 
 char *sha256_buf(char *buf, size_t len);
 char *sha256_fd(int fd);
+char *sha512_buf(char *buf, size_t len);
+char *sha512_fd(int fd);
diff --git a/usr.sbin/pkg/pkg.c b/usr.sbin/pkg/pkg.c
index b7fb81b246a7..cf7a9072f20c 100644
--- a/usr.sbin/pkg/pkg.c
+++ b/usr.sbin/pkg/pkg.c
@@ -82,6 +82,7 @@ static const struct pkgsign_impl {
 typedef enum {
        HASH_UNKNOWN,
        HASH_SHA256,
+       HASH_SHA512,
 } hash_t;
 
 struct fingerprint {
@@ -413,6 +414,8 @@ parse_fingerprint(ucl_object_t *obj)
 
        if (strcasecmp(function, "sha256") == 0)
                fct = HASH_SHA256;
+       else if (strcasecmp(function, "sha512") == 0)
+               fct = HASH_SHA512;
 
        if (fct == HASH_UNKNOWN) {
                warnx("Unsupported hashing function: %s", function);
@@ -764,6 +767,24 @@ cleanup:
        return (ret);
 }
 
+/*
+ * Compare a fingerprint against the certificate digest of the same type.
+ */
+static bool
+fingerprint_match(const struct fingerprint *f, const char *sha256,
+    const char *sha512)
+{
+
+       switch (f->type) {
+       case HASH_SHA256:
+               return (strcasecmp(f->hash, sha256) == 0);
+       case HASH_SHA512:
+               return (strcasecmp(f->hash, sha512) == 0);
+       default:
+               return (false);
+       }
+}
+
 static bool
 verify_signature(int fd_pkg, int fd_sig, struct repository *r)
 {
@@ -775,9 +796,9 @@ verify_signature(int fd_pkg, int fd_sig, struct repository 
*r)
        int trusted_count, revoked_count;
        const char *fingerprints;
        char path[MAXPATHLEN];
-       char *hash;
+       char *sha256, *sha512;
 
-       hash = NULL;
+       sha256 = sha512 = NULL;
        sc = NULL;
        sctx = NULL;
        trusted = revoked = NULL;
@@ -822,13 +843,21 @@ verify_signature(int fd_pkg, int fd_sig, struct 
repository *r)
        /* Explicitly mark as non-trusted until proven otherwise. */
        sc->trusted = false;
 
-       /* Parse signature and pubkey out of the certificate */
-       hash = sha256_buf(sc->cert, sc->certlen);
+       /*
+        * Fingerprints may be either the SHA-256 or the SHA-512 digest of the
+        * certificate, as with pkg(8).
+        */
+       sha256 = sha256_buf(sc->cert, sc->certlen);
+       sha512 = sha512_buf(sc->cert, sc->certlen);
+       if (sha256 == NULL || sha512 == NULL) {
+               warnx("Error hashing certificate");
+               goto cleanup;
+       }
 
        /* Check if this hash is revoked */
        if (revoked != NULL) {
                STAILQ_FOREACH(fingerprint, revoked, next) {
-                       if (strcasecmp(fingerprint->hash, hash) == 0) {
+                       if (fingerprint_match(fingerprint, sha256, sha512)) {
                                fprintf(stderr, "The package was signed with "
                                    "revoked certificate %s\n",
                                    fingerprint->name);
@@ -838,7 +867,7 @@ verify_signature(int fd_pkg, int fd_sig, struct repository 
*r)
        }
 
        STAILQ_FOREACH(fingerprint, trusted, next) {
-               if (strcasecmp(fingerprint->hash, hash) == 0) {
+               if (fingerprint_match(fingerprint, sha256, sha512)) {
                        sc->trusted = true;
                        sc->name = strdup(fingerprint->name);
                        break;
@@ -867,7 +896,8 @@ verify_signature(int fd_pkg, int fd_sig, struct repository 
*r)
        ret = true;
 
 cleanup:
-       free(hash);
+       free(sha256);
+       free(sha512);
        if (trusted)
                free_fingerprint_list(trusted);
        if (revoked)

Reply via email to