From nobody Thu Mar 9 16:43:23 2023 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 4PXZkM50DRz3x7dQ; Thu, 9 Mar 2023 16:43:23 +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 4PXZkM4S5Hz47jJ; Thu, 9 Mar 2023 16:43:23 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1678380203; 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=AQfbJS74es/61+cVziHiYGj62EIlxBbjjNAfzbS/U1M=; b=E3SWxwbRSP3pDMvRcSJWpXSIOyOqrKsfeUvxuLmDQsi3upP5C2FjIdlgjRzBsuh9YfamdV 1B52KSUJoINYbj+lAIb2iaCzBah14+PWDzvftkOqcN11nlYwtebyoHHCJ2D3FHy8OhcNFJ ZcIySjvson3flo/Y42+htZmL/VPtFpFM9Oi5yfKCoEqY/83zr0a/0xmycKpMdY7mzEeOUn I3OzwtRylLXmjjIwi7nuKHEsf6qZCtBGlsWWsSGYiO1a+kVyqQP6Icb43opoCLqYimlHR8 iJVstUYGzJ5nvnQf35B8QJYyRHtKvGIYURZ5AR8jEoIQ8MSMyaNukGwrTY/XhQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1678380203; 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=AQfbJS74es/61+cVziHiYGj62EIlxBbjjNAfzbS/U1M=; b=HvJjAcxIEJr7k1r3Zo3ivt6UndZPjmWxhTNlvktfBAcfHcsLDZYy5AnVO9fgcq8iuA+WvS oonNtQAj4z2iqUACgXHab7Tf8t0dhOeY+yhzjdPLfWur4onOTS9jbg3gn4myc8EvW6xZfL l8IskWsyQ6EpG1tM2aNOU/RfvVOakWhW1oA1uO87FbHTNUu8UTdpfUwItG22Va1ugfRRii xhKl0FtJbq1+EFCnU8ctqi5pydoBK2OcNmiwS4EepRARnEz+xXSZ8QjMoJs7C7ACXgm7eg nzGzgIU0dpoCehqcrUstbNO5IvBl/w2IR/bJ6quz5uUevKLZpDYMD0UwYmLkdw== ARC-Authentication-Results: i=1; mx1.freebsd.org; none ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1678380203; a=rsa-sha256; cv=none; b=L/VdnCGlWJgSnbo+y+vvO9I4xDsxwopS1ixlG4pUw/RVwMmpd/UaJiDG/e4RnthwCsWd2J 3eNCOvPCtzhMjg0x3ajRLz8tYUoTdeyIsIF/+9CLafM/WP+nKP4bCMfzusuv1TCat8Y6VD nn9tnGDjLgPLOQVxouoJ7zE8Aigza5rGZGsBB9eYrIxOBcvibEr/jxZbTeWSDkLa20rKb+ IhDzsbLmvPSEZyNe7xj3KSIUu8LTDBlair/BYsg/3I1vA0f510g12voM7lu+fNctawW4lg uB+YKgyP63vycG6/7ejdhdaIjkSSScRW7XzU41XcyshMA+oRSQOCC3OUT+oZEQ== 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 4PXZkM3VTkzFrv; Thu, 9 Mar 2023 16:43:23 +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 329GhNdV046905; Thu, 9 Mar 2023 16:43:23 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.16.1/8.16.1/Submit) id 329GhNWr046904; Thu, 9 Mar 2023 16:43:23 GMT (envelope-from git) Date: Thu, 9 Mar 2023 16:43:23 GMT Message-Id: <202303091643.329GhNWr046904@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-main@FreeBSD.org From: Baptiste Daroussin Subject: git: b2654064c2d1 - main - pkg(7): use libmd for sha256 instead of openssl 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: Sender: owner-dev-commits-src-all@freebsd.org X-BeenThere: dev-commits-src-all@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/main X-Git-Reftype: branch X-Git-Commit: b2654064c2d11a1ee36667b3ff8b0f4d2536af74 Auto-Submitted: auto-generated X-ThisMailContainsUnwantedMimeParts: N The branch main has been updated by bapt: URL: https://cgit.FreeBSD.org/src/commit/?id=b2654064c2d11a1ee36667b3ff8b0f4d2536af74 commit b2654064c2d11a1ee36667b3ff8b0f4d2536af74 Author: Baptiste Daroussin AuthorDate: 2023-03-09 16:38:30 +0000 Commit: Baptiste Daroussin CommitDate: 2023-03-09 16:43:01 +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 --- 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 7b574dc42db2..89870f2af683 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; @@ -402,83 +403,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) {