os: Use EVP APIs when building with OpenSSL 3

Avoids deprecation warnings for old SHA1 APIs in OpenSSL 3.0 and later

Closes: #1845
Signed-off-by: Alan Coopersmith <alan.coopersmith@oracle.com>
Part-of: <https://gitlab.freedesktop.org/xorg/xserver/-/merge_requests/2066>
This commit is contained in:
Alan Coopersmith 2025-09-21 10:48:00 -07:00 committed by Marge Bot
parent 7013984b5f
commit 3ec5f6bcc3
2 changed files with 53 additions and 3 deletions

View file

@ -16,7 +16,7 @@ Copyright © 2006 Nokia Corporation
Copyright © 2006-2008 Peter Hutterer Copyright © 2006-2008 Peter Hutterer
Copyright © 2006 Adam Jackson Copyright © 2006 Adam Jackson
Copyright © 2009-2010 NVIDIA Corporation Copyright © 2009-2010 NVIDIA Corporation
Copyright © 1987, 2003-2006, 2008-2010 Oracle and/or its affiliates. Copyright © 1987, 2003-2006, 2008-2010, 2025 Oracle and/or its affiliates.
Copyright © 1999 Keith Packard Copyright © 1999 Keith Packard
Copyright © 2007-2009 Red Hat, Inc. Copyright © 2007-2009 Red Hat, Inc.
Copyright © 2005-2008 Daniel Stone Copyright © 2005-2008 Daniel Stone

View file

@ -1,7 +1,10 @@
/* Copyright © 2007 Carl Worth /* SPDX-License-Identifier: MIT
*
* Copyright © 2007 Carl Worth
* Copyright © 2009 Jeremy Huddleston, Julien Cristau, and Matthieu Herrb * Copyright © 2009 Jeremy Huddleston, Julien Cristau, and Matthieu Herrb
* Copyright © 2009-2010 Mikhail Gusarov * Copyright © 2009-2010 Mikhail Gusarov
* Copyright © 2012 Yaakov Selkowitz and Keith Packard * Copyright © 2012 Yaakov Selkowitz and Keith Packard
* Copyright (c) 2025, Oracle and/or its affiliates.
* *
* Permission is hereby granted, free of charge, to any person obtaining a * Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"), * copy of this software and associated documentation files (the "Software"),
@ -13,7 +16,7 @@
* The above copyright notice and this permission notice (including the next * The above copyright notice and this permission notice (including the next
* paragraph) shall be included in all copies or substantial portions of the * paragraph) shall be included in all copies or substantial portions of the
* Software. * Software.
* *
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
@ -254,13 +257,43 @@ x_sha1_final(void *ctx, unsigned char result[20])
#else /* Use OpenSSL's libcrypto */ #else /* Use OpenSSL's libcrypto */
#include <openssl/opensslv.h>
#if OPENSSL_VERSION_MAJOR >= 3
#define USE_EVP
#endif
#ifdef USE_EVP
#include <openssl/evp.h>
#else
#include <stddef.h> /* buggy openssl/sha.h wants size_t */ #include <stddef.h> /* buggy openssl/sha.h wants size_t */
#include <openssl/sha.h> #include <openssl/sha.h>
#endif
#ifdef USE_EVP
static EVP_MD *sha1 = NULL;
#endif
void * void *
x_sha1_init(void) x_sha1_init(void)
{ {
int ret; int ret;
#ifdef USE_EVP
EVP_MD_CTX *ctx;
if (sha1 == NULL) {
sha1 = EVP_MD_fetch(NULL, "SHA1", NULL);
if (sha1 == NULL)
return NULL;
}
ctx = EVP_MD_CTX_new();
if (ctx == NULL)
return NULL;
ret = EVP_DigestInit_ex2(ctx, sha1, NULL);
if (!ret) {
EVP_MD_CTX_free(ctx);
return NULL;
}
#else
SHA_CTX *ctx = malloc(sizeof(*ctx)); SHA_CTX *ctx = malloc(sizeof(*ctx));
if (!ctx) if (!ctx)
@ -270,6 +303,7 @@ x_sha1_init(void)
free(ctx); free(ctx);
return NULL; return NULL;
} }
#endif
return ctx; return ctx;
} }
@ -277,11 +311,19 @@ int
x_sha1_update(void *ctx, void *data, int size) x_sha1_update(void *ctx, void *data, int size)
{ {
int ret; int ret;
#ifdef USE_EVP
EVP_MD_CTX *sha_ctx = ctx;
ret = EVP_DigestUpdate(sha_ctx, data, size);
if (!ret)
EVP_MD_CTX_free(sha_ctx);
#else
SHA_CTX *sha_ctx = ctx; SHA_CTX *sha_ctx = ctx;
ret = SHA1_Update(sha_ctx, data, size); ret = SHA1_Update(sha_ctx, data, size);
if (!ret) if (!ret)
free(sha_ctx); free(sha_ctx);
#endif
return ret; return ret;
} }
@ -289,10 +331,18 @@ int
x_sha1_final(void *ctx, unsigned char result[20]) x_sha1_final(void *ctx, unsigned char result[20])
{ {
int ret; int ret;
#ifdef USE_EVP
EVP_MD_CTX *sha_ctx = ctx;
unsigned int result_len = 20; /* size of result buffer */
ret = EVP_DigestFinal_ex(sha_ctx, result, &result_len);
EVP_MD_CTX_free(sha_ctx);
#else
SHA_CTX *sha_ctx = ctx; SHA_CTX *sha_ctx = ctx;
ret = SHA1_Final(result, sha_ctx); ret = SHA1_Final(result, sha_ctx);
free(sha_ctx); free(sha_ctx);
#endif
return ret; return ret;
} }