Page MenuHomeFreeBSD

D62408.1796432919.diff
No OneTemporary

Authored By
Unknown
Size
15 KB
Referenced Files
None
Subscribers
None

D62408.1796432919.diff

diff --git a/usr.sbin/pkg/Makefile b/usr.sbin/pkg/Makefile
--- a/usr.sbin/pkg/Makefile
+++ b/usr.sbin/pkg/Makefile
@@ -24,7 +24,7 @@
CONFSDIR= /etc/pkg
CONFSMODE= 644
PROG= pkg
-SRCS= pkg.c rsa.c dns_utils.c config.c ecc.c hash.c
+SRCS= pkg.c rsa.c dns_utils.c config.c ecc.c hash.c mldsa.c
MAN= pkg.7
CFLAGS+=-I${SRCTOP}/contrib/libucl/include
diff --git a/usr.sbin/pkg/hash.h b/usr.sbin/pkg/hash.h
--- 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/hash.c b/usr.sbin/pkg/hash.c
--- 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 @@
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/mldsa.c b/usr.sbin/pkg/mldsa.c
new file mode 100644
--- /dev/null
+++ b/usr.sbin/pkg/mldsa.c
@@ -0,0 +1,172 @@
+/*
+ * Copyright (c) 2026 Gordon Tetlow <gordon@FreeBSD.org>
+ *
+ * SPDX-License-Identifier: BSD-2-Clause
+ */
+
+/*
+ * ML-DSA-87 signature verification, matching the "mldsa" signer in pkg(8).
+ *
+ * As with the rsa signer, the signed message is the hex encoding of the
+ * file's digest (SHA-512 here) rather than the raw digest or the file itself,
+ * so that repositories signed by a local key and by signing_command verify
+ * the same way.
+ */
+
+#include <sys/param.h>
+#include <sys/types.h>
+
+#include <err.h>
+#include <stdbool.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+
+#include <openssl/err.h>
+#include <openssl/evp.h>
+#include <openssl/pem.h>
+
+#include "pkg.h"
+#include "hash.h"
+
+#if OPENSSL_VERSION_NUMBER >= 0x30500000L
+
+#define MLDSA_ALG "ML-DSA-87"
+
+static EVP_PKEY *
+load_public_key_file(const char *file)
+{
+ EVP_PKEY *pkey;
+ BIO *bp;
+ char errbuf[1024];
+
+ bp = BIO_new_file(file, "r");
+ if (bp == NULL) {
+ warnx("Unable to read %s", file);
+ return (NULL);
+ }
+
+ if ((pkey = PEM_read_bio_PUBKEY(bp, NULL, NULL, NULL)) == NULL)
+ warnx("%s", ERR_error_string(ERR_get_error(), errbuf));
+
+ BIO_free(bp);
+
+ return (pkey);
+}
+
+static EVP_PKEY *
+load_public_key_buf(const unsigned char *cert, int certlen)
+{
+ EVP_PKEY *pkey;
+ BIO *bp;
+ char errbuf[1024];
+
+ bp = BIO_new_mem_buf(cert, certlen);
+ if (bp == NULL) {
+ warnx("%s", ERR_error_string(ERR_get_error(), errbuf));
+ return (NULL);
+ }
+
+ if ((pkey = PEM_read_bio_PUBKEY(bp, NULL, NULL, NULL)) == NULL)
+ warnx("%s", ERR_error_string(ERR_get_error(), errbuf));
+
+ BIO_free(bp);
+
+ return (pkey);
+}
+
+static bool
+mldsa_verify_data(const struct pkgsign_ctx *ctx __unused,
+ const char *data, size_t datasz, const char *sigfile,
+ const unsigned char *key, int keylen, unsigned char *sig, int siglen)
+{
+ EVP_PKEY *pkey;
+ EVP_PKEY_CTX *pctx;
+ EVP_SIGNATURE *sig_alg;
+ char errbuf[1024];
+ bool ret;
+
+ pctx = NULL;
+ sig_alg = NULL;
+ ret = false;
+
+ if (sigfile != NULL)
+ pkey = load_public_key_file(sigfile);
+ else
+ pkey = load_public_key_buf(key, keylen);
+ if (pkey == NULL) {
+ warnx("Error reading public key");
+ goto cleanup;
+ }
+
+ if ((pctx = EVP_PKEY_CTX_new_from_pkey(NULL, pkey, NULL)) == NULL) {
+ warnx("%s", ERR_error_string(ERR_get_error(), errbuf));
+ goto error;
+ }
+
+ /*
+ * Fetching the specific algorithm ensures that the key really is an
+ * ML-DSA-87 key; any other key type fails to initialize.
+ */
+ if ((sig_alg = EVP_SIGNATURE_fetch(NULL, MLDSA_ALG, NULL)) == NULL) {
+ warnx("%s", ERR_error_string(ERR_get_error(), errbuf));
+ goto error;
+ }
+
+ if (EVP_PKEY_verify_message_init(pctx, sig_alg, NULL) != 1) {
+ warnx("%s", ERR_error_string(ERR_get_error(), errbuf));
+ goto error;
+ }
+
+ if (EVP_PKEY_verify(pctx, sig, siglen,
+ (const unsigned char *)data, datasz) != 1) {
+ warnx("%s", ERR_error_string(ERR_get_error(), errbuf));
+ goto error;
+ }
+
+ ret = true;
+ printf("done\n");
+ goto cleanup;
+
+error:
+ printf("failed\n");
+
+cleanup:
+ EVP_SIGNATURE_free(sig_alg);
+ EVP_PKEY_CTX_free(pctx);
+ EVP_PKEY_free(pkey);
+
+ return (ret);
+}
+
+static bool
+mldsa_verify_cert(const struct pkgsign_ctx *ctx, int fd,
+ const char *sigfile, const unsigned char *key, int keylen,
+ unsigned char *sig, int siglen)
+{
+ char *sha512;
+ bool ret;
+
+ if (lseek(fd, 0, SEEK_SET) == -1) {
+ warn("lseek");
+ return (false);
+ }
+ if ((sha512 = sha512_fd(fd)) == NULL) {
+ warnx("Error creating SHA512 hash for package");
+ return (false);
+ }
+
+ ret = mldsa_verify_data(ctx, sha512, strlen(sha512), sigfile, key,
+ keylen, sig, siglen);
+ free(sha512);
+
+ return (ret);
+}
+
+const struct pkgsign_ops pkgsign_mldsa = {
+ .pkgsign_verify_cert = mldsa_verify_cert,
+ .pkgsign_verify_data = mldsa_verify_data,
+};
+
+#endif /* OPENSSL_VERSION_NUMBER >= 0x30500000L */
diff --git a/usr.sbin/pkg/pkg.h b/usr.sbin/pkg/pkg.h
--- a/usr.sbin/pkg/pkg.h
+++ b/usr.sbin/pkg/pkg.h
@@ -32,6 +32,8 @@
#include <stdbool.h>
+#include <openssl/opensslv.h>
+
struct pkgsign_ctx {
const struct pkgsign_impl *impl;
};
@@ -53,6 +55,9 @@
extern const struct pkgsign_ops pkgsign_ecc;
extern const struct pkgsign_ops pkgsign_rsa;
+#if OPENSSL_VERSION_NUMBER >= 0x30500000L
+extern const struct pkgsign_ops pkgsign_mldsa;
+#endif
struct sig_cert {
char *name;
diff --git a/usr.sbin/pkg/pkg.7 b/usr.sbin/pkg/pkg.7
--- a/usr.sbin/pkg/pkg.7
+++ b/usr.sbin/pkg/pkg.7
@@ -22,7 +22,7 @@
.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
.\" SUCH DAMAGE.
.\"
-.Dd July 20, 2026
+.Dd November 28, 2026
.Dt PKG 7
.Os
.Sh NAME
@@ -251,6 +251,31 @@
then a signature will be required and validated against known
certificate fingerprints when bootstrapping
.Xr pkg 8 .
+If set to
+.Dv PUBKEY
+then a signature will be required and validated against the public key
+named by
+.Ev PUBKEY .
+.Pp
+The signature may have been made with any of the
+.Dq rsa ,
+.Dq ecc
+(also known as
+.Dq eddsa ) ,
+.Dq ecdsa ,
+or
+.Dq mldsa
+signers.
+The
+.Dq mldsa
+signer uses ML-DSA-87 over the hex-encoded SHA-512 digest of the package.
+.It Ev PUBKEY
+If
+.Sy SIGNATURE_TYPE
+is set to
+.Dv PUBKEY
+this value should be set to the path of the PEM-encoded public key used to
+verify the package signature.
.It Ev FINGERPRINTS
If
.Sy SIGNATURE_TYPE
diff --git a/usr.sbin/pkg/pkg.c b/usr.sbin/pkg/pkg.c
--- a/usr.sbin/pkg/pkg.c
+++ b/usr.sbin/pkg/pkg.c
@@ -77,11 +77,18 @@
.pi_name = "eddsa",
.pi_ops = &pkgsign_ecc,
},
+#if OPENSSL_VERSION_NUMBER >= 0x30500000L
+ {
+ .pi_name = "mldsa",
+ .pi_ops = &pkgsign_mldsa,
+ },
+#endif
};
typedef enum {
HASH_UNKNOWN,
HASH_SHA256,
+ HASH_SHA512,
} hash_t;
struct fingerprint {
@@ -413,6 +420,8 @@
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);
@@ -731,6 +740,13 @@
goto cleanup;
}
+ datasz = strlen(data);
+ } else if (strcmp(pk->sigtype, "mldsa") == 0) {
+ if ((data = sha512_fd(fd_pkg)) == NULL) {
+ warnx("Error creating SHA512 hash for package");
+ goto cleanup;
+ }
+
datasz = strlen(data);
} else {
if ((data = pkg_read_fd(fd_pkg, &datasz)) == NULL) {
@@ -764,6 +780,24 @@
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 +809,9 @@
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 +856,21 @@
/* 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 +880,7 @@
}
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;
@@ -852,7 +894,7 @@
}
if (pkgsign_new(sc->type, &sctx) != 0) {
- fprintf(stderr, "Failed to fetch 'rsa' signer\n");
+ fprintf(stderr, "Failed to fetch '%s' signer\n", sc->type);
goto cleanup;
}
@@ -867,7 +909,8 @@
ret = true;
cleanup:
- free(hash);
+ free(sha256);
+ free(sha512);
if (trusted)
free_fingerprint_list(trusted);
if (revoked)
diff --git a/usr.sbin/pkg/tests/pkg_test.sh b/usr.sbin/pkg/tests/pkg_test.sh
--- a/usr.sbin/pkg/tests/pkg_test.sh
+++ b/usr.sbin/pkg/tests/pkg_test.sh
@@ -252,6 +252,193 @@
"${pkg7}" --option ABI=FreeBSD:1:i386 --option OSVERSION=100000 config abi
}
+mldsa_require()
+{
+ openssl list -signature-algorithms 2>/dev/null | grep -q ML-DSA-87 ||
+ atf_skip "openssl(1) does not support ML-DSA-87"
+}
+
+# Generate an ML-DSA key pair named after $1 using parameter set $2.
+mldsa_genkey()
+{
+ atf_check openssl genpkey -algorithm "${2:-ML-DSA-87}" \
+ -out "$1.key" -outpubkey "$1.pub"
+}
+
+# Build a fake pkg.pkg whose pkg-static just reports how it was invoked.
+mldsa_mkpkg()
+{
+ atf_check mkdir -p stage/usr/local/sbin root
+ cat >stage/usr/local/sbin/pkg-static <<'EOF'
+#!/bin/sh
+echo "fake pkg-static $*"
+EOF
+ atf_check chmod 0755 stage/usr/local/sbin/pkg-static
+ atf_check tar -cPf pkg.pkg -C stage -s ',^usr,/usr,' usr
+}
+
+# Sign pkg.pkg with key $1, writing the raw signature to $2.
+mldsa_sign()
+{
+ sha512 -q pkg.pkg | tr -d '\n' >digest
+ atf_check openssl pkeyutl -sign -rawin -inkey "$1.key" \
+ -in digest -out "$2"
+}
+
+# Write pkg.pkg.pubkeysig from raw signature $1.
+mldsa_pubkeysig()
+{
+ printf '$PKGSIGN:mldsa$' >pkg.pkg.pubkeysig
+ cat "$1" >>pkg.pkg.pubkeysig
+}
+
+# Write pkg.pkg.sig in fingerprint format from key $1 and raw signature $2.
+mldsa_certsig()
+{
+ {
+ printf 'SIGNATURE\n'
+ cat "$2"
+ printf '\nTYPE\nmldsa\nCERT\n'
+ cat "$1.pub"
+ printf 'END\n'
+ } >pkg.pkg.sig
+}
+
+# Print a fingerprint file for $2 using hash function $1.
+mldsa_fingerprint()
+{
+ printf 'function: "%s"\nfingerprint: "%s"\n' "$1" "$($1 -q "$2")"
+}
+
+atf_test_case mldsa_pubkey_good
+mldsa_pubkey_good_head()
+{
+ atf_set descr "Bootstrap from a pkg.pkg with a valid mldsa pubkey signature"
+ atf_set require.progs "pkg ${pkg8} openssl sha512 tar"
+}
+mldsa_pubkey_good_body()
+{
+ pkg_prepare
+ mldsa_require
+ mldsa_genkey k
+ mldsa_mkpkg
+ mldsa_sign k sig
+ mldsa_pubkeysig sig
+ atf_check -o match:"Verifying signature with public key .*done" \
+ -o match:"fake pkg-static -r ${PWD}/root add -M \./pkg\.pkg" \
+ env SIGNATURE_TYPE=PUBKEY PUBKEY="${PWD}/k.pub" \
+ "${pkg7}" -r "${PWD}/root" add ./pkg.pkg
+}
+
+atf_test_case mldsa_pubkey_bad
+mldsa_pubkey_bad_head()
+{
+ atf_set descr "Reject a pkg.pkg modified after mldsa signing"
+ atf_set require.progs "pkg ${pkg8} openssl sha512 tar"
+}
+mldsa_pubkey_bad_body()
+{
+ pkg_prepare
+ mldsa_require
+ mldsa_genkey k
+ mldsa_mkpkg
+ mldsa_sign k sig
+ mldsa_pubkeysig sig
+ echo tampered >>pkg.pkg
+ atf_check -s exit:1 -o match:"failed" \
+ -e match:"Signature is not valid" \
+ env SIGNATURE_TYPE=PUBKEY PUBKEY="${PWD}/k.pub" \
+ "${pkg7}" -r "${PWD}/root" add ./pkg.pkg
+ atf_check test ! -e root/usr/local/sbin/pkg-static
+}
+
+atf_test_case mldsa_pubkey_wrongkey
+mldsa_pubkey_wrongkey_head()
+{
+ atf_set descr "Reject an mldsa signature checked against the wrong key"
+ atf_set require.progs "pkg ${pkg8} openssl sha512 tar"
+}
+mldsa_pubkey_wrongkey_body()
+{
+ pkg_prepare
+ mldsa_require
+ mldsa_genkey k
+ mldsa_genkey other
+ mldsa_genkey k65 ML-DSA-65
+ mldsa_mkpkg
+ mldsa_sign k sig
+ mldsa_pubkeysig sig
+ # A different ML-DSA-87 key.
+ atf_check -s exit:1 -o ignore -e match:"Signature is not valid" \
+ env SIGNATURE_TYPE=PUBKEY PUBKEY="${PWD}/other.pub" \
+ "${pkg7}" -r "${PWD}/root" add ./pkg.pkg
+ # An ML-DSA-65 key with a matching ML-DSA-65 signature is refused.
+ mldsa_sign k65 sig65
+ mldsa_pubkeysig sig65
+ atf_check -s exit:1 -o ignore -e match:"Signature is not valid" \
+ env SIGNATURE_TYPE=PUBKEY PUBKEY="${PWD}/k65.pub" \
+ "${pkg7}" -r "${PWD}/root" add ./pkg.pkg
+ atf_check test ! -e root/usr/local/sbin/pkg-static
+}
+
+atf_test_case mldsa_fingerprint_good
+mldsa_fingerprint_good_head()
+{
+ atf_set descr "Bootstrap from a pkg.pkg with a valid mldsa fingerprint signature"
+ atf_set require.progs "pkg ${pkg8} openssl sha512 tar"
+}
+mldsa_fingerprint_good_body()
+{
+ pkg_prepare
+ mldsa_require
+ mldsa_genkey k
+ mldsa_mkpkg
+ mldsa_sign k sig
+ mldsa_certsig k sig
+ atf_check mkdir -p keys/trusted keys/revoked
+ mldsa_fingerprint sha256 k.pub >keys/trusted/k
+ atf_check -o match:"Verifying signature with trusted certificate k\.\.\. done" \
+ -o match:"fake pkg-static" \
+ env SIGNATURE_TYPE=FINGERPRINTS FINGERPRINTS="${PWD}/keys" \
+ "${pkg7}" -r "${PWD}/root" add ./pkg.pkg
+
+ # Revoking the key must cause the bootstrap to fail.
+ atf_check mv keys/trusted/k keys/revoked/k
+ atf_check cp keys/revoked/k keys/trusted/k
+ atf_check -s exit:1 -o ignore -e match:"revoked certificate" \
+ env SIGNATURE_TYPE=FINGERPRINTS FINGERPRINTS="${PWD}/keys" \
+ "${pkg7}" -r "${PWD}/root" add ./pkg.pkg
+}
+
+atf_test_case mldsa_fingerprint_sha512
+mldsa_fingerprint_sha512_head()
+{
+ atf_set descr "Trust and revoke mldsa certificates by SHA-512 fingerprint"
+ atf_set require.progs "pkg ${pkg8} openssl sha512 tar"
+}
+mldsa_fingerprint_sha512_body()
+{
+ pkg_prepare
+ mldsa_require
+ mldsa_genkey k
+ mldsa_mkpkg
+ mldsa_sign k sig
+ mldsa_certsig k sig
+ atf_check mkdir -p keys/trusted keys/revoked
+ mldsa_fingerprint sha512 k.pub >keys/trusted/k
+ atf_check -o match:"Verifying signature with trusted certificate k\.\.\. done" \
+ -o match:"fake pkg-static" \
+ env SIGNATURE_TYPE=FINGERPRINTS FINGERPRINTS="${PWD}/keys" \
+ "${pkg7}" -r "${PWD}/root" add ./pkg.pkg
+
+ # A SHA-512 revocation applies even when trusted by SHA-256.
+ mldsa_fingerprint sha256 k.pub >keys/trusted/k
+ mldsa_fingerprint sha512 k.pub >keys/revoked/k
+ atf_check -s exit:1 -o ignore -e match:"revoked certificate" \
+ env SIGNATURE_TYPE=FINGERPRINTS FINGERPRINTS="${PWD}/keys" \
+ "${pkg7}" -r "${PWD}/root" add ./pkg.pkg
+}
+
atf_init_test_cases()
{
atf_add_test_case activation
@@ -260,4 +447,9 @@
atf_add_test_case add_jail
atf_add_test_case add_root
atf_add_test_case config
+ atf_add_test_case mldsa_fingerprint_good
+ atf_add_test_case mldsa_fingerprint_sha512
+ atf_add_test_case mldsa_pubkey_bad
+ atf_add_test_case mldsa_pubkey_good
+ atf_add_test_case mldsa_pubkey_wrongkey
}

File Metadata

Mime Type
text/plain
Expires
Sat, Dec 5, 1:08 AM (23 h, 23 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
42216518
Default Alt Text
D62408.1796432919.diff (15 KB)

Event Timeline