From nobody Wed Jan 01 21:11:43 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 4YNjFX5j8Xz5jCrb; Wed, 01 Jan 2025 21:11:44 +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 4YNjFW6t6Nz4k78; Wed, 1 Jan 2025 21:11:43 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1735765904; 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=DyHtgSmtxS7eT+IXO4mo6+KxCQbAf1NDOsYRwEe4NnY=; b=d8GeWhXSRCqYp/U0jwwZ/USpHF2ug/dz2bjmftqemlYjRhFDOIucsv7HI3Umkl8VihgMdX hecol0x/VHjPo63oKRg0/v13w28crNk2kRKb1bnIZAzr9VSlC79bPXYE6Xe2d0yJMsVnQ6 6QYiQ74sezFD43A/bfe8u7hwNK/KL2j6W6AWWnTjCXKYNM+D6lHhL7ZmmB0BkarEB/UgN1 R2/GVFDtzTC4PolPVgpeqgXA2852k7hbXnwupLI9NLHYOwoEvQKpYaCjtYIHAYYsGNviPf Vz3MQ81rEWocW1pNeM12Gt4kVUBOOF8CDwSHKFLv9zGq1mTmHHn1fQJE6JRDhQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1735765904; 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=DyHtgSmtxS7eT+IXO4mo6+KxCQbAf1NDOsYRwEe4NnY=; b=INRbHQxprtSQI3cO96F1ottN7O67GENsJFce86kMeTthwd72mrrDYR+SVLyWCXrETCHfvz Y5R6QHcqlakhSCs243SR3zejeYoZWUmlUNzcWMLA1N7IUkcnaBgqlNiJ4iQkrbysv4dZld +hudfpCehwNLZbbkiNrlkKn297pVvX9uwcSrNZJZ/zUTfxgR/AlpwjNArXrkMttbjkvGna e0RTYSwU2HhiNONjqUZEHbwWpTD5fS3p2nw22CcEu1kRp8DDCvCsvSyKv5fato2giTJTaz zh8qI1gVtt03+kJF9xlKD9i5IwyUwu5kmw/2TzomdRlfNmw5aLxN//c55UNAwg== ARC-Authentication-Results: i=1; mx1.freebsd.org; none ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1735765904; a=rsa-sha256; cv=none; b=M6lUnA/2sksDopFqaehXAUSHa1BslRFtH+P+2WwNdzb8tzO5XsuMuONOGyKCl4m74oqlv9 pChvU2/vnoPmx1gWknqgFpnJ4blYoi0EPwjpPBeNQKEm5IJFu5eAK+SkfOJuhtg/jenl6i Qn7IOewQCpIWhpBy1Oj/baBfpjsqeNwvyxZ5qu+nXaH5Wz7DBK+qRp+XtZf5i1tsjMbOWN aOGP1UjPewSF81C/HGS9rkkdkjt8P8Z3QIN6d6xIAFus+wU1KUDjla2dHl/4mAkvfJnZru GlLIArGtzz7b72SDw2QDtZMgvaKq+b+y+DooKRHageKy5UIocMxIA9f3v64c1A== 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 4YNjFW6QmMzgZS; Wed, 1 Jan 2025 21:11:43 +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 501LBhb0080164; Wed, 1 Jan 2025 21:11:43 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.18.1/8.18.1/Submit) id 501LBhYg080161; Wed, 1 Jan 2025 21:11:43 GMT (envelope-from git) Date: Wed, 1 Jan 2025 21:11:43 GMT Message-Id: <202501012111.501LBhYg080161@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: 2e065d74a5b0 - main - pkg: add a pkgsign_verify_data callback 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: 2e065d74a5b0ea32db7d4f6e3f78eaa17ee7685e Auto-Submitted: auto-generated The branch main has been updated by kevans: URL: https://cgit.FreeBSD.org/src/commit/?id=2e065d74a5b0ea32db7d4f6e3f78eaa17ee7685e commit 2e065d74a5b0ea32db7d4f6e3f78eaa17ee7685e Author: Kyle Evans AuthorDate: 2025-01-01 21:10:28 +0000 Commit: Kyle Evans CommitDate: 2025-01-01 21:11:23 +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 Differential Revision: https://reviews.freebsd.org/D48109 --- 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 6d0a75d2786a..db15e7b6ab22 100644 --- a/usr.sbin/pkg/pkg.c +++ b/usr.sbin/pkg/pkg.c @@ -142,6 +142,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) { @@ -574,12 +585,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"); @@ -591,6 +605,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; @@ -598,7 +625,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; @@ -607,6 +634,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, };