From nobody Wed Jan 01 21:11:41 2025 X-Original-To: dev-commits-src-all@mlmmj.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mlmmj.nyi.freebsd.org (Postfix) with ESMTP id 4YNjFV3TNYz5jD12; Wed, 01 Jan 2025 21:11:42 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from mxrelay.nyi.freebsd.org (mxrelay.nyi.freebsd.org [IPv6:2610:1c1:1:606c::19:3]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256 client-signature RSA-PSS (4096 bits) client-digest SHA256) (Client CN "mxrelay.nyi.freebsd.org", Issuer "R11" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4YNjFT4t60z4k5T; Wed, 1 Jan 2025 21:11:41 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1735765901; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=qRpJ9Jw5juY/a/2gkU3hAFoiNYZ1fGHyq2j9jyyeseA=; b=mIaudVPNeSbw23IAWH54/sDPkrwt3MUcAHE7S0hFy4ECPFuDZf16X8i7uKK17mOOZCnmFO 9Ndu8oFw1xKdYVFXQtBDbN39sP5qnDC51u7ERro/ZGWWu3S/Q7t8X86feJXF6PLsdpd42v rSA+TIcxEm2FVtsvxCvT6RKFa6Fqtn4mAFNDP64uzjySKar0bxKYlBWmZC8UpJUE3csgdv 0X3CwQmGDbDK78yahTUop/UT8osM/bqYyL0v6QngthgexdXogReWw9OJnTGIua05igDLuU eFSseYuUL1bd9Ek41GU5d8y95KVqRPBEeVN5JrKgqunskmg/zNIYXCgwrbUsXw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1735765901; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=qRpJ9Jw5juY/a/2gkU3hAFoiNYZ1fGHyq2j9jyyeseA=; b=F7BW/R+EE1M5J8+OPqKMA1r2OJCGO2MntasrXZPoHDvTYAKC0rzuGn8ccCIlGUvPzwiM64 TxiAP3PtxQW5ttWrwV0XN9BWkqiaolG9Z9AFYUrSXxJkX8Yi5orQRUbke612fizk/ECH5a fU1Vp6FGj6s2GRSPefamBanHImVIXzOaWZXhOSk3ExvZICPtAIL/ZRp+9cnitbvCFYNExQ Q5g6ZbEgYg5sK83x7MHxWH0yKgd3cIP5HegeY80he+EAWjIpe+/04fL2jD8uZTfht0U2ug mKsqKwLn5byilBl8nXvpJ6/sQzsClWBx3C/c1zaqDrq084pI0vQouGO5V5kPfg== ARC-Authentication-Results: i=1; mx1.freebsd.org; none ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1735765901; a=rsa-sha256; cv=none; b=AQzLUDNFMb8EGsIorC7JhdcaXhA/cCfAI6/qH4E/16+T6HGtFhiHZ8LAa4nkv43qmtLbG6 DWahm0vsLZxUFnToyiKoV3YRpikqQDR+4JhHUyVn8PwQ1R73K3OQCabiSanXucnP76vezV GoHoUQrx9JPYDpqhVOYBKIDHPEO3UTD4Wy/+GkIfOOMLZwj5vUELszuvmC70ZGA4JbXwbW 1xK8uZq8aYCAdOvGLlYV8no/1WhxyCWj/raUDuKBcQ7PBcxwjuSuxGaYmUELztUzKWOGE8 w22+7uFQbDjPQwowi4z8uwTtX7OOhw90sqt9T56vtG7r2NQifo8NZdy0efSgZg== Received: from gitrepo.freebsd.org (gitrepo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:5]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (Client did not present a certificate) by mxrelay.nyi.freebsd.org (Postfix) with ESMTPS id 4YNjFT4MZSzgZQ; Wed, 1 Jan 2025 21:11:41 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from gitrepo.freebsd.org ([127.0.1.44]) by gitrepo.freebsd.org (8.18.1/8.18.1) with ESMTP id 501LBf2k080074; Wed, 1 Jan 2025 21:11:41 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.18.1/8.18.1/Submit) id 501LBfZg080071; Wed, 1 Jan 2025 21:11:41 GMT (envelope-from git) Date: Wed, 1 Jan 2025 21:11:41 GMT Message-Id: <202501012111.501LBfZg080071@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-main@FreeBSD.org From: Kyle Evans Subject: git: 5862580ded35 - main - pkg: abstract rsa out behind a pkgsign API List-Id: Commit messages for all branches of the src repository List-Archive: https://lists.freebsd.org/archives/dev-commits-src-all List-Help: List-Post: List-Subscribe: List-Unsubscribe: X-BeenThere: dev-commits-src-all@freebsd.org Sender: owner-dev-commits-src-all@FreeBSD.org MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Git-Committer: kevans X-Git-Repository: src X-Git-Refname: refs/heads/main X-Git-Reftype: branch X-Git-Commit: 5862580ded35e23581291a2e1052f04428369ead Auto-Submitted: auto-generated The branch main has been updated by kevans: URL: https://cgit.FreeBSD.org/src/commit/?id=5862580ded35e23581291a2e1052f04428369ead commit 5862580ded35e23581291a2e1052f04428369ead Author: Kyle Evans AuthorDate: 2025-01-01 21:10:27 +0000 Commit: Kyle Evans CommitDate: 2025-01-01 21:11:23 +0000 pkg: abstract rsa out behind a pkgsign API This mirrors a change we made in pkg(8), and will be used to next add another signer that does ECC. Reviewed by: bapt, emaste Differential Revision: https://reviews.freebsd.org/D48106 --- usr.sbin/pkg/pkg.c | 86 ++++++++++++++++++++++++++++++++++++++++++++++++++++-- usr.sbin/pkg/pkg.h | 22 ++++++++++++-- usr.sbin/pkg/rsa.c | 11 +++++-- 3 files changed, 110 insertions(+), 9 deletions(-) diff --git a/usr.sbin/pkg/pkg.c b/usr.sbin/pkg/pkg.c index 24a4f6516307..4d476f7653f7 100644 --- a/usr.sbin/pkg/pkg.c +++ b/usr.sbin/pkg/pkg.c @@ -34,6 +34,7 @@ #include #include +#include #include #include #include @@ -54,6 +55,16 @@ #include "config.h" #include "hash.h" +static const struct pkgsign_impl { + const char *pi_name; + const struct pkgsign_ops *pi_ops; +} pkgsign_builtins[] = { + { + .pi_name = "rsa", + .pi_ops = &pkgsign_rsa, + }, +}; + typedef enum { HASH_UNKNOWN, HASH_SHA256, @@ -76,6 +87,61 @@ STAILQ_HEAD(fingerprint_list, fingerprint); static int debug; +static int +pkgsign_new(const char *name, struct pkgsign_ctx **ctx) +{ + const struct pkgsign_impl *impl; + const struct pkgsign_ops *ops; + struct pkgsign_ctx *nctx; + size_t ctx_size; + int ret; + + assert(*ctx == NULL); + ops = NULL; + for (size_t i = 0; i < nitems(pkgsign_builtins); i++) { + impl = &pkgsign_builtins[i]; + + if (strcmp(name, impl->pi_name) == 0) { + ops = impl->pi_ops; + break; + } + } + + if (ops == NULL) + return (ENOENT); + + ctx_size = ops->pkgsign_ctx_size; + if (ctx_size == 0) + ctx_size = sizeof(*nctx); + assert(ctx_size >= sizeof(*nctx)); + + nctx = calloc(1, ctx_size); + if (nctx == NULL) + err(EXIT_FAILURE, "calloc"); + nctx->impl = impl; + + ret = 0; + if (ops->pkgsign_new != NULL) + ret = (*ops->pkgsign_new)(name, nctx); + + if (ret != 0) { + free(nctx); + return (ret); + } + + *ctx = nctx; + return (0); +} + +static bool +pkgsign_verify_cert(const struct pkgsign_ctx *ctx, int fd, const char *sigfile, + const unsigned char *key, int keylen, unsigned char *sig, int siglen) +{ + + return ((*ctx->impl->pi_ops->pkgsign_verify_cert)(ctx, fd, sigfile, + key, keylen, sig, siglen)); +} + static int extract_pkg_static(int fd, char *p, int sz) { @@ -494,10 +560,12 @@ verify_pubsignature(int fd_pkg, int fd_sig) { struct pubkey *pk; const char *pubkey; + struct pkgsign_ctx *sctx; bool ret; pk = NULL; pubkey = NULL; + sctx = NULL; ret = false; if (config_string(PUBKEY, &pubkey) != 0) { warnx("No CONFIG_PUBKEY defined"); @@ -509,9 +577,14 @@ verify_pubsignature(int fd_pkg, int fd_sig) goto cleanup; } + if (pkgsign_new("rsa", &sctx) != 0) { + warnx("Failed to fetch 'rsa' signer"); + goto cleanup; + } + /* Verify the signature. */ printf("Verifying signature with public key %s... ", pubkey); - if (rsa_verify_cert(fd_pkg, pubkey, NULL, 0, pk->sig, + if (pkgsign_verify_cert(sctx, fd_pkg, pubkey, NULL, 0, pk->sig, pk->siglen) == false) { fprintf(stderr, "Signature is not valid\n"); goto cleanup; @@ -534,6 +607,7 @@ verify_signature(int fd_pkg, int fd_sig) struct fingerprint_list *trusted, *revoked; struct fingerprint *fingerprint; struct sig_cert *sc; + struct pkgsign_ctx *sctx; bool ret; int trusted_count, revoked_count; const char *fingerprints; @@ -542,6 +616,7 @@ verify_signature(int fd_pkg, int fd_sig) hash = NULL; sc = NULL; + sctx = NULL; trusted = revoked = NULL; ret = false; @@ -605,10 +680,15 @@ verify_signature(int fd_pkg, int fd_sig) goto cleanup; } + if (pkgsign_new("rsa", &sctx) != 0) { + fprintf(stderr, "Failed to fetch 'rsa' signer\n"); + goto cleanup; + } + /* Verify the signature. */ printf("Verifying signature with trusted certificate %s... ", sc->name); - if (rsa_verify_cert(fd_pkg, NULL, sc->cert, sc->certlen, sc->sig, - sc->siglen) == false) { + if (pkgsign_verify_cert(sctx, fd_pkg, NULL, sc->cert, sc->certlen, + sc->sig, sc->siglen) == false) { fprintf(stderr, "Signature is not valid\n"); goto cleanup; } diff --git a/usr.sbin/pkg/pkg.h b/usr.sbin/pkg/pkg.h index 01f69f5a825b..d42f5a95d327 100644 --- a/usr.sbin/pkg/pkg.h +++ b/usr.sbin/pkg/pkg.h @@ -30,6 +30,25 @@ #ifndef _PKG_H #define _PKG_H +#include + +struct pkgsign_ctx { + const struct pkgsign_impl *impl; +}; + +/* Tentatively won't be needing to free any state, all allocated in the ctx. */ +typedef int pkgsign_new_cb(const char *, struct pkgsign_ctx *); +typedef bool pkgsign_verify_cert_cb(const struct pkgsign_ctx *, int, + const char *, const unsigned char *, int, unsigned char *, int); + +struct pkgsign_ops { + size_t pkgsign_ctx_size; + pkgsign_new_cb *pkgsign_new; + pkgsign_verify_cert_cb *pkgsign_verify_cert; +}; + +extern const struct pkgsign_ops pkgsign_rsa; + struct sig_cert { char *name; unsigned char *sig; @@ -44,7 +63,4 @@ struct pubkey { int siglen; }; -bool rsa_verify_cert(int, const char *, const unsigned char *, int, - unsigned char *, int); - #endif /* _PKG_H */ diff --git a/usr.sbin/pkg/rsa.c b/usr.sbin/pkg/rsa.c index afc446a6ad06..b6345cdcecb8 100644 --- a/usr.sbin/pkg/rsa.c +++ b/usr.sbin/pkg/rsa.c @@ -77,9 +77,10 @@ load_public_key_buf(const unsigned char *cert, int certlen) return (pkey); } -bool -rsa_verify_cert(int fd, const char *sigfile, const unsigned char *key, - int keylen, unsigned char *sig, int siglen) +static bool +rsa_verify_cert(const struct pkgsign_ctx *ctx __unused, int fd, + const char *sigfile, const unsigned char *key, int keylen, + unsigned char *sig, int siglen) { EVP_MD_CTX *mdctx; EVP_PKEY *pkey; @@ -153,3 +154,7 @@ cleanup: return (ret); } + +const struct pkgsign_ops pkgsign_rsa = { + .pkgsign_verify_cert = rsa_verify_cert, +};