The branch main has been updated by gordon: URL: https://cgit.FreeBSD.org/src/commit/?id=354fe37781c7bacc9672bc7e4c8426eabbd8bb2e
commit 354fe37781c7bacc9672bc7e4c8426eabbd8bb2e Author: Gordon Tetlow <[email protected]> AuthorDate: 2026-12-04 17:00:10 +0000 Commit: Gordon Tetlow <[email protected]> CommitDate: 2026-12-04 17:20:22 +0000 pkg: Add support for mldsa (ML-DSA-87) signatures pkg(8) gained an "mldsa" signer backed by OpenSSL's ML-DSA-87 implementation. Teach the bootstrap to verify it so that a repository signed with it can be bootstrapped. As in pkg(8), the signed message is the hex-encoded SHA-512 digest of pkg.pkg, for both PUBKEY and FINGERPRINTS verification, and only ML-DSA-87 keys are accepted. The signer is only built when OpenSSL is 3.5 or later. While here, report the actual signer type rather than "rsa" when the signer for a fingerprint signature cannot be found, and document SIGNATURE_TYPE=PUBKEY and PUBKEY in pkg(7). Reviewed by: kevans MFC after: 1 week Relnotes: yes Differential Revision: https://reviews.freebsd.org/D62408 --- usr.sbin/pkg/Makefile | 2 +- usr.sbin/pkg/mldsa.c | 172 ++++++++++++++++++++++++++++++++++++ usr.sbin/pkg/pkg.7 | 14 ++- usr.sbin/pkg/pkg.c | 15 +++- usr.sbin/pkg/pkg.h | 5 ++ usr.sbin/pkg/tests/pkg_test.sh | 192 +++++++++++++++++++++++++++++++++++++++++ 6 files changed, 397 insertions(+), 3 deletions(-) diff --git a/usr.sbin/pkg/Makefile b/usr.sbin/pkg/Makefile index 101664257831..838d3e1b3a35 100644 --- a/usr.sbin/pkg/Makefile +++ b/usr.sbin/pkg/Makefile @@ -24,7 +24,7 @@ CONFSNAME_${PKGCONF}= ${PKGCONF:C/\.conf.+$/.conf/} 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/mldsa.c b/usr.sbin/pkg/mldsa.c new file mode 100644 index 000000000000..ff9af448cbec --- /dev/null +++ b/usr.sbin/pkg/mldsa.c @@ -0,0 +1,172 @@ +/* + * Copyright (c) 2026 Gordon Tetlow <[email protected]> + * + * 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.7 b/usr.sbin/pkg/pkg.7 index 8214eb2586f7..c68c5761ebf0 100644 --- 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,18 @@ If set to 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 . +.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 index cf7a9072f20c..51723f1776df 100644 --- a/usr.sbin/pkg/pkg.c +++ b/usr.sbin/pkg/pkg.c @@ -77,6 +77,12 @@ static const struct pkgsign_impl { .pi_name = "eddsa", .pi_ops = &pkgsign_ecc, }, +#if OPENSSL_VERSION_NUMBER >= 0x30500000L + { + .pi_name = "mldsa", + .pi_ops = &pkgsign_mldsa, + }, +#endif }; typedef enum { @@ -734,6 +740,13 @@ verify_pubsignature(int fd_pkg, int fd_sig, struct repository *r) 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) { @@ -881,7 +894,7 @@ verify_signature(int fd_pkg, int fd_sig, struct repository *r) } 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; } diff --git a/usr.sbin/pkg/pkg.h b/usr.sbin/pkg/pkg.h index f74f97ce795b..539db58c9173 100644 --- 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 @@ struct pkgsign_ops { 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/tests/pkg_test.sh b/usr.sbin/pkg/tests/pkg_test.sh index 649de3415e1f..87df53912520 100644 --- a/usr.sbin/pkg/tests/pkg_test.sh +++ b/usr.sbin/pkg/tests/pkg_test.sh @@ -252,6 +252,193 @@ config_body() "${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_init_test_cases() 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 }
