From nobody Fri May 19 08:17:27 2023 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 4QN07q4vdzz4BfCQ; Fri, 19 May 2023 08:17:27 +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 "R3" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4QN07q4Rlsz4FqX; Fri, 19 May 2023 08:17:27 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1684484247; 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=qfnOjyjK7WfdZiB6cre+AZ5Z+vBD/aS55uSE2H8teeo=; b=ttdAEjmcJtkT1DgytiRmfk/M1JBzylSnlyl4YJsNbI+d8hJ4o7UOwLK4qdbAxQOhla8H2a QlBZGCJuxOiSFbLwVipoj+2Tjohvhm9rjv81Ufg79K/yqXmiAT7tIDdItPTZSFPzfTF8Ll M2TNko5S4SaX95oBSVeF9Gn7O4x+Np31ncPHWFL+qbEsPiM9iejbrBWGzjlGihasqoDZ/F odjCXJHOmrSi3yjc44OxOYjrbaAK+J651CWnd/8yFzTqO+dqZIoa07y6DpNaD4zrRhCWOa Wbkc3L7RSS+zj6DneU8wOJKPzSOBfXYakaglBF6BR7GFFtRDM/HtEE95scx35A== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1684484247; 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=qfnOjyjK7WfdZiB6cre+AZ5Z+vBD/aS55uSE2H8teeo=; b=LdpUx67x/LBo0t5SI3nr1xXAH944cV2Q4A3p+oYBYxN6u3jYzIBaHyjS8+XoAySKXJiRV7 27clbmABN+qGGrQFJSZ+0Q0Dtc/Nq6QCQNhHVTTHNIOON4RIY3lcf9s8KBf/ER/f8b3Rru WvL3RjD7FNuO3eqU/g7hVs44ZCsUGWvDoERivvNre7NuqYfP2Eqo3mNWzWTyEW8tzX8N9w Lr7NlPGnCJs1W9n+5wYbT7faVoH0pUE1uPPAXL0Lb3AzEiqo9RDwtJ4ZSl76+Ie0fq/Tfo LCAXmtPeg++W3QIgI7eP21ZPHT31wHzKriGeDVjSfCrGPy18YtC8uerN5jX2XQ== ARC-Authentication-Results: i=1; mx1.freebsd.org; none ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1684484247; a=rsa-sha256; cv=none; b=N4jeXDbLBvM6MGyHokZCSnJLEaG4hs1rjp7JsnIqe7M3pPlNKZPfip92S9zJbrJCFE8Wny pC4YKcFmvcq/hd9f8qOdooYa0BzFSl79ELITv45PvYuwFCUvrckyPDs+ojQ3aZr9xG57Rm oZ0knxnwxLU+gVmwTeR5j9x44a+o9d1d1c/0cdZkS40ohFCzvSvNm7GEi9u/7aiA0p8RE6 lvhBBN//aBfRgarrcB//BJEjcaYCT0zyhw4/uXeMCw7cC/iE6VTSJRxsdaipR+xwYolqiH TdFvIsvdsjidzD1J026di72O0jynGfUZ0+d5wxTk/aD14n72W3Z7JaO2Spus9w== 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 4QN07q3Vq7zKvm; Fri, 19 May 2023 08:17:27 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from gitrepo.freebsd.org ([127.0.1.44]) by gitrepo.freebsd.org (8.16.1/8.16.1) with ESMTP id 34J8HRdQ018179; Fri, 19 May 2023 08:17:27 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.16.1/8.16.1/Submit) id 34J8HRpr018178; Fri, 19 May 2023 08:17:27 GMT (envelope-from git) Date: Fri, 19 May 2023 08:17:27 GMT Message-Id: <202305190817.34J8HRpr018178@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-branches@FreeBSD.org From: Baptiste Daroussin Subject: git: 7bc2192380e1 - stable/13 - pkg(7): use libmd for sha256 instead of openssl 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: Sender: owner-dev-commits-src-branches@freebsd.org X-BeenThere: dev-commits-src-branches@freebsd.org MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Git-Committer: bapt X-Git-Repository: src X-Git-Refname: refs/heads/stable/13 X-Git-Reftype: branch X-Git-Commit: 7bc2192380e10f3469ab4eda69ade324aae976cd Auto-Submitted: auto-generated X-ThisMailContainsUnwantedMimeParts: N The branch stable/13 has been updated by bapt: URL: https://cgit.FreeBSD.org/src/commit/?id=7bc2192380e10f3469ab4eda69ade324aae976cd commit 7bc2192380e10f3469ab4eda69ade324aae976cd Author: Baptiste Daroussin AuthorDate: 2023-03-09 16:38:30 +0000 Commit: Baptiste Daroussin CommitDate: 2023-05-19 08:16:51 +0000 pkg(7): use libmd for sha256 instead of openssl OpenSSL 3.0 has deprecated the sha256 api, let's use libmd which has the same API instead. In order to avoid the collision in definitions (sha256.h cannot be included in the same file as a file where openssl headers has been included) let's move the sha256 related code in its own file PR: 270023 Reported by: ngie (cherry picked from commit b2654064c2d11a1ee36667b3ff8b0f4d2536af74) --- usr.sbin/pkg/Makefile | 4 +- usr.sbin/pkg/hash.c | 112 ++++++++++++++++++++++++++++++++++++++++++++++++++ usr.sbin/pkg/hash.h | 32 +++++++++++++++ usr.sbin/pkg/pkg.c | 78 +---------------------------------- 4 files changed, 147 insertions(+), 79 deletions(-) diff --git a/usr.sbin/pkg/Makefile b/usr.sbin/pkg/Makefile index e40265146657..895438982309 100644 --- a/usr.sbin/pkg/Makefile +++ b/usr.sbin/pkg/Makefile @@ -23,11 +23,11 @@ CONFSNAME_${PKGCONF}= ${PKGCONF:C/\.conf.+$/.conf/} CONFSDIR= /etc/pkg CONFSMODE= 644 PROG= pkg -SRCS= pkg.c dns_utils.c config.c +SRCS= pkg.c dns_utils.c config.c hash.c MAN= pkg.7 CFLAGS+=-I${SRCTOP}/contrib/libucl/include .PATH: ${SRCTOP}/contrib/libucl/include -LIBADD= archive fetch ucl crypto ssl util +LIBADD= archive fetch ucl crypto ssl util md .include diff --git a/usr.sbin/pkg/hash.c b/usr.sbin/pkg/hash.c new file mode 100644 index 000000000000..47bcae6c5b6a --- /dev/null +++ b/usr.sbin/pkg/hash.c @@ -0,0 +1,112 @@ +/*- + * SPDX-License-Identifier: BSD-2-Clause-FreeBSD + * + * Copyright (c) 2012-2014 Baptiste Daroussin + * Copyright (c) 2013 Bryan Drewery + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#include +#include +#include +#include + +#include "hash.h" + +static void +sha256_hash(unsigned char hash[SHA256_DIGEST_LENGTH], + char out[SHA256_DIGEST_LENGTH * 2 + 1]) +{ + int i; + + for (i = 0; i < SHA256_DIGEST_LENGTH; i++) + sprintf(out + (i * 2), "%02x", hash[i]); + + out[SHA256_DIGEST_LENGTH * 2] = '\0'; +} + +void +sha256_buf(char *buf, size_t len, char out[SHA256_DIGEST_LENGTH * 2 + 1]) +{ + unsigned char hash[SHA256_DIGEST_LENGTH]; + SHA256_CTX sha256; + + out[0] = '\0'; + + SHA256_Init(&sha256); + SHA256_Update(&sha256, buf, len); + SHA256_Final(hash, &sha256); + sha256_hash(hash, out); +} + +int +sha256_fd(int fd, char out[SHA256_DIGEST_LENGTH * 2 + 1]) +{ + int my_fd; + FILE *fp; + char buffer[BUFSIZ]; + unsigned char hash[SHA256_DIGEST_LENGTH]; + size_t r; + int ret; + SHA256_CTX sha256; + + fp = NULL; + ret = 1; + + out[0] = '\0'; + + /* Duplicate the fd so that fclose(3) does not close it. */ + if ((my_fd = dup(fd)) == -1) { + warnx("dup"); + goto cleanup; + } + + if ((fp = fdopen(my_fd, "rb")) == NULL) { + warnx("fdopen"); + goto cleanup; + } + + SHA256_Init(&sha256); + + while ((r = fread(buffer, 1, BUFSIZ, fp)) > 0) + SHA256_Update(&sha256, buffer, r); + + if (ferror(fp) != 0) { + warnx("fread"); + goto cleanup; + } + + SHA256_Final(hash, &sha256); + sha256_hash(hash, out); + ret = 0; + +cleanup: + if (fp != NULL) + fclose(fp); + else if (my_fd != -1) + close(my_fd); + (void)lseek(fd, 0, SEEK_SET); + + return (ret); +} diff --git a/usr.sbin/pkg/hash.h b/usr.sbin/pkg/hash.h new file mode 100644 index 000000000000..dcdc458b5e61 --- /dev/null +++ b/usr.sbin/pkg/hash.h @@ -0,0 +1,32 @@ +/*- + * SPDX-License-Identifier: BSD-2-Clause-FreeBSD + * + * Copyright (c) 2023 Baptiste Daroussin + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + */ + +#pragma once + +void sha256_buf(char *buf, size_t len, char out[]); +int sha256_fd(int fd, char out[]); diff --git a/usr.sbin/pkg/pkg.c b/usr.sbin/pkg/pkg.c index f3c338c6961e..7c4749f8089e 100644 --- a/usr.sbin/pkg/pkg.c +++ b/usr.sbin/pkg/pkg.c @@ -56,6 +56,7 @@ __FBSDID("$FreeBSD$"); #include "dns_utils.h" #include "config.h" +#include "hash.h" struct sig_cert { char *name; @@ -400,83 +401,6 @@ load_fingerprints(const char *path, int *count) return (fingerprints); } -static void -sha256_hash(unsigned char hash[SHA256_DIGEST_LENGTH], - char out[SHA256_DIGEST_LENGTH * 2 + 1]) -{ - int i; - - for (i = 0; i < SHA256_DIGEST_LENGTH; i++) - sprintf(out + (i * 2), "%02x", hash[i]); - - out[SHA256_DIGEST_LENGTH * 2] = '\0'; -} - -static void -sha256_buf(char *buf, size_t len, char out[SHA256_DIGEST_LENGTH * 2 + 1]) -{ - unsigned char hash[SHA256_DIGEST_LENGTH]; - SHA256_CTX sha256; - - out[0] = '\0'; - - SHA256_Init(&sha256); - SHA256_Update(&sha256, buf, len); - SHA256_Final(hash, &sha256); - sha256_hash(hash, out); -} - -static int -sha256_fd(int fd, char out[SHA256_DIGEST_LENGTH * 2 + 1]) -{ - int my_fd; - FILE *fp; - char buffer[BUFSIZ]; - unsigned char hash[SHA256_DIGEST_LENGTH]; - size_t r; - int ret; - SHA256_CTX sha256; - - fp = NULL; - ret = 1; - - out[0] = '\0'; - - /* Duplicate the fd so that fclose(3) does not close it. */ - if ((my_fd = dup(fd)) == -1) { - warnx("dup"); - goto cleanup; - } - - if ((fp = fdopen(my_fd, "rb")) == NULL) { - warnx("fdopen"); - goto cleanup; - } - - SHA256_Init(&sha256); - - while ((r = fread(buffer, 1, BUFSIZ, fp)) > 0) - SHA256_Update(&sha256, buffer, r); - - if (ferror(fp) != 0) { - warnx("fread"); - goto cleanup; - } - - SHA256_Final(hash, &sha256); - sha256_hash(hash, out); - ret = 0; - -cleanup: - if (fp != NULL) - fclose(fp); - else if (my_fd != -1) - close(my_fd); - (void)lseek(fd, 0, SEEK_SET); - - return (ret); -} - static EVP_PKEY * load_public_key_file(const char *file) {