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>
(cherry picked from commit 3ec5f6bcc3)

Part-of: <https://gitlab.freedesktop.org/xorg/xserver/-/merge_requests/2076>
This commit is contained in:
Alan Coopersmith 2025-09-21 10:48:00 -07:00 committed by Olivier Fourdan
parent 3eeebac6d5
commit e20ae90a95
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 Adam Jackson
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 © 2007-2009 Red Hat, Inc.
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-2010 Mikhail Gusarov
* 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
* copy of this software and associated documentation files (the "Software"),
@ -256,13 +259,43 @@ x_sha1_final(void *ctx, unsigned char result[20])
#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 <openssl/sha.h>
#endif
#ifdef USE_EVP
static EVP_MD *sha1 = NULL;
#endif
void *
x_sha1_init(void)
{
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));
if (!ctx)
@ -272,6 +305,7 @@ x_sha1_init(void)
free(ctx);
return NULL;
}
#endif
return ctx;
}
@ -279,11 +313,19 @@ int
x_sha1_update(void *ctx, void *data, int size)
{
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;
ret = SHA1_Update(sha_ctx, data, size);
if (!ret)
free(sha_ctx);
#endif
return ret;
}
@ -291,10 +333,18 @@ int
x_sha1_final(void *ctx, unsigned char result[20])
{
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;
ret = SHA1_Final(result, sha_ctx);
free(sha_ctx);
#endif
return ret;
}