git: 2e065d74a5b0 - main - pkg: add a pkgsign_verify_data callback
- Go to: [ bottom of page ] [ top of archives ] [ this month ]
Date: Wed, 01 Jan 2025 21:11:43 UTC
The branch main has been updated by kevans: URL: https://cgit.FreeBSD.org/src/commit/?id=2e065d74a5b0ea32db7d4f6e3f78eaa17ee7685e commit 2e065d74a5b0ea32db7d4f6e3f78eaa17ee7685e Author: Kyle Evans <kevans@FreeBSD.org> AuthorDate: 2025-01-01 21:10:28 +0000 Commit: Kyle Evans <kevans@FreeBSD.org> 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, };