From nobody Sat Jan 11 02:49:04 2025 X-Original-To: dev-commits-src-branches@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 4YVNJd1JNJz5l4gr; Sat, 11 Jan 2025 02:49:05 +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 4YVNJd02LRz4J0K; Sat, 11 Jan 2025 02:49:05 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1736563745; 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=9XWERTuLtwp1O7uDqza/NdwTS+MzmyCfglTS1R4V/Fg=; b=eSE3Qun4cFIQqOg6sUoxyZxI1DvJcirqOby4jt2b2B8ZkH6M3BSHlYrNCybA51AEFFndEg W+XjiJJENkDwbH31mCAW54rWGTHzMu4i/++wLdKZWeu8G/XMHn2XGD0zSqLE5PR6D217tD +nZmk2N2NqaPOIW6kHK6Ls9VxlFyZ+OGmSwT+oJRqiN4Zi09Dy/D2lH3YpDWTsQHCLS8Qj i0xoqhTcakGz0IbHFNkTOSk8yN7ku52DJ5ZqN7FNlZxMmabRWt584zo1RubzOa+7j+CTE1 7zLh2JVIbfvBFHABVeWMxS0kB74FNV+vP/RuhO3WfwZqUQYgukCR3/HK5veUpA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1736563745; 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=9XWERTuLtwp1O7uDqza/NdwTS+MzmyCfglTS1R4V/Fg=; b=vSw/5ijgtHCd7CQ8BzgpthhC+SVNjsQLsoGIlnhN3VGkkXBZcAMyzb7XvH24tgG5H8XOpD /8BWqWHbY3kwpNfetp52Im+ATLCRMxDjckV5BhP8/ySKcwrvuiMnL4qb2aQVEoxm/4OUxP 2CaSol5HvwtoTKLxt1QatlWlfJ4rPHES5Cqz38+3XIn5AkqHZhqO0EZO+xbwNe0CeBbJjG tyGjRitBJ2TFi2/RtdsWj3BfnkXlP+eYM1WsbxI0dA3ZBvqCpt9dVNB6RytJg5pnJnih9l xEYgcgmN3Cli+Wfk38CNI7M+vZ7j2n0lOjjRgoVVcBbD6KF+AiELZLBJZI9cxQ== ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1736563745; a=rsa-sha256; cv=none; b=Xjtw38UWdz5BbAYDthUs+4870I/UYLxCHROWKULcA9L8PM2uwZnCfGP2sVo5BD0QBVQBAp Rva4H8GZdJke5XHd7RmhISuYkKljwHCobWJw9UEOfeXknpRIJPz6R2K1JPopdCMeacorsD R/dfyFHYLpcFHZpLWqEXPvGXm0jtEk7uxEr3jRQL2XqO46gg7MEGja+iJY7qn1UmSrrZBp 8+BeH1yGYhP+nEscwpxsfcVOVxA751f9ZHTjLqAZVxt+IpON/nUVSb6bJXX91UgfGZ75yQ QMTkuuYU5Gsr6XLCyioEFjbux34a1c1YE2yVXYIatkgUsi0dKROfjkgzUydU7Q== ARC-Authentication-Results: i=1; mx1.freebsd.org; none 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 4YVNJc6mF2zgNQ; Sat, 11 Jan 2025 02:49:04 +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 50B2n4R0066747; Sat, 11 Jan 2025 02:49:04 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.18.1/8.18.1/Submit) id 50B2n48x066744; Sat, 11 Jan 2025 02:49:04 GMT (envelope-from git) Date: Sat, 11 Jan 2025 02:49:04 GMT Message-Id: <202501110249.50B2n48x066744@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-branches@FreeBSD.org From: Kyle Evans Subject: git: 4e33c2e91835 - stable/13 - pkg: add a pkgsign_verify_data callback List-Id: Commits to the stable branches of the FreeBSD src repository List-Archive: https://lists.freebsd.org/archives/dev-commits-src-branches List-Help: List-Post: List-Subscribe: List-Unsubscribe: X-BeenThere: dev-commits-src-branches@freebsd.org Sender: owner-dev-commits-src-branches@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/stable/13 X-Git-Reftype: branch X-Git-Commit: 4e33c2e91835162f994589bceff9a16ed4613d49 Auto-Submitted: auto-generated The branch stable/13 has been updated by kevans: URL: https://cgit.FreeBSD.org/src/commit/?id=4e33c2e91835162f994589bceff9a16ed4613d49 commit 4e33c2e91835162f994589bceff9a16ed4613d49 Author: Kyle Evans AuthorDate: 2025-01-01 21:10:28 +0000 Commit: Kyle Evans CommitDate: 2025-01-11 02:48:31 +0000 pkg: add a pkgsign_verify_data callback This will be used to verify raw payloads, as if signed by pkg-key(8). It will be used specifically in pkg(7) to verify .pubkeysig as published by poudriere. Amend verify_pubsignature() now to use it. For the RSA signer, we need to verify using a sha256 of the data instead of the data itself. Reviewed by: bapt (cherry picked from commit 2e065d74a5b0ea32db7d4f6e3f78eaa17ee7685e) --- usr.sbin/pkg/pkg.c | 30 +++++++++++++++++++++++++++++- usr.sbin/pkg/pkg.h | 4 ++++ usr.sbin/pkg/rsa.c | 50 ++++++++++++++++++++++++++++++++------------------ 3 files changed, 65 insertions(+), 19 deletions(-) diff --git a/usr.sbin/pkg/pkg.c b/usr.sbin/pkg/pkg.c index 5cc9c3b8dbfe..8980cca2bb2d 100644 --- a/usr.sbin/pkg/pkg.c +++ b/usr.sbin/pkg/pkg.c @@ -141,6 +141,17 @@ pkgsign_verify_cert(const struct pkgsign_ctx *ctx, int fd, const char *sigfile, key, keylen, sig, siglen)); } +static bool +pkgsign_verify_data(const struct pkgsign_ctx *ctx, const char *data, + size_t datasz, const char *sigfile, const unsigned char *key, int keylen, + unsigned char *sig, int siglen) +{ + + return ((*ctx->impl->pi_ops->pkgsign_verify_data)(ctx, data, datasz, + sigfile, key, keylen, sig, siglen)); +} + + static int extract_pkg_static(int fd, char *p, int sz) { @@ -573,12 +584,15 @@ verify_pubsignature(int fd_pkg, int fd_sig) { struct pubkey *pk; const char *pubkey; + char *data; struct pkgsign_ctx *sctx; + size_t datasz; bool ret; pk = NULL; pubkey = NULL; sctx = NULL; + data = NULL; ret = false; if (config_string(PUBKEY, &pubkey) != 0) { warnx("No CONFIG_PUBKEY defined"); @@ -590,6 +604,19 @@ verify_pubsignature(int fd_pkg, int fd_sig) goto cleanup; } + if (lseek(fd_pkg, 0, SEEK_SET) == -1) { + warn("lseek"); + goto cleanup; + } + + /* Future types shouldn't do this. */ + if ((data = sha256_fd(fd_pkg)) == NULL) { + warnx("Error creating SHA256 hash for package"); + goto cleanup; + } + + datasz = strlen(data); + if (pkgsign_new("rsa", &sctx) != 0) { warnx("Failed to fetch 'rsa' signer"); goto cleanup; @@ -597,7 +624,7 @@ verify_pubsignature(int fd_pkg, int fd_sig) /* Verify the signature. */ printf("Verifying signature with public key %s... ", pubkey); - if (pkgsign_verify_cert(sctx, fd_pkg, pubkey, NULL, 0, pk->sig, + if (pkgsign_verify_data(sctx, data, datasz, pubkey, NULL, 0, pk->sig, pk->siglen) == false) { fprintf(stderr, "Signature is not valid\n"); goto cleanup; @@ -606,6 +633,7 @@ verify_pubsignature(int fd_pkg, int fd_sig) ret = true; cleanup: + free(data); if (pk) { free(pk->sig); free(pk); diff --git a/usr.sbin/pkg/pkg.h b/usr.sbin/pkg/pkg.h index 2d0dab96a20f..b9fe9b5fa566 100644 --- a/usr.sbin/pkg/pkg.h +++ b/usr.sbin/pkg/pkg.h @@ -40,11 +40,15 @@ struct pkgsign_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); +typedef bool pkgsign_verify_data_cb(const struct pkgsign_ctx *, + const char *, size_t, 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; + pkgsign_verify_data_cb *pkgsign_verify_data; }; extern const struct pkgsign_ops pkgsign_rsa; diff --git a/usr.sbin/pkg/rsa.c b/usr.sbin/pkg/rsa.c index b6345cdcecb8..b28f44ec1953 100644 --- a/usr.sbin/pkg/rsa.c +++ b/usr.sbin/pkg/rsa.c @@ -78,33 +78,20 @@ load_public_key_buf(const unsigned char *cert, int certlen) } 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) +rsa_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_MD_CTX *mdctx; EVP_PKEY *pkey; - char *sha256; char errbuf[1024]; bool ret; - sha256 = NULL; pkey = NULL; mdctx = NULL; ret = false; - SSL_load_error_strings(); - /* Compute SHA256 of the package. */ - if (lseek(fd, 0, 0) == -1) { - warn("lseek"); - goto cleanup; - } - if ((sha256 = sha256_fd(fd)) == NULL) { - warnx("Error creating SHA256 hash for package"); - goto cleanup; - } - if (sigfile != NULL) { if ((pkey = load_public_key_file(sigfile)) == NULL) { warnx("Error reading public key"); @@ -127,7 +114,7 @@ rsa_verify_cert(const struct pkgsign_ctx *ctx __unused, int fd, warnx("%s", ERR_error_string(ERR_get_error(), errbuf)); goto error; } - if (EVP_DigestVerifyUpdate(mdctx, sha256, strlen(sha256)) != 1) { + if (EVP_DigestVerifyUpdate(mdctx, data, datasz) != 1) { warnx("%s", ERR_error_string(ERR_get_error(), errbuf)); goto error; } @@ -145,7 +132,6 @@ error: printf("failed\n"); cleanup: - free(sha256); if (pkey) EVP_PKEY_free(pkey); if (mdctx) @@ -155,6 +141,34 @@ cleanup: return (ret); } +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) +{ + char *sha256; + bool ret; + + sha256 = NULL; + + /* Compute SHA256 of the package. */ + if (lseek(fd, 0, 0) == -1) { + warn("lseek"); + return (false); + } + if ((sha256 = sha256_fd(fd)) == NULL) { + warnx("Error creating SHA256 hash for package"); + return (false); + } + + ret = rsa_verify_data(ctx, sha256, strlen(sha256), sigfile, key, keylen, + sig, siglen); + free(sha256); + + return (ret); +} + const struct pkgsign_ops pkgsign_rsa = { .pkgsign_verify_cert = rsa_verify_cert, + .pkgsign_verify_data = rsa_verify_data, };