[vlc-commits] contrib: libarchive: use bcrypt rather than wincrypt

Steve Lhomme git at videolan.org
Fri Jun 19 15:48:02 CEST 2020


vlc/vlc-3.0 | branch: master | Steve Lhomme <robux4 at ycbcr.xyz> | Thu Apr  5 14:49:26 2018 +0200| [2bbd0ea48ffdc9f951e3f69e1c710f080eab69ff] | committer: Steve Lhomme

contrib: libarchive: use bcrypt rather than wincrypt

(cherry picked from commit a2bbb0e0caab57f25410a92473ab1333bf128d75)
Signed-off-by: Steve Lhomme <robux4 at ycbcr.xyz>

> http://git.videolan.org/gitweb.cgi/vlc/vlc-3.0.git/?a=commit;h=2bbd0ea48ffdc9f951e3f69e1c710f080eab69ff
---

 .../src/libarchive/libarchive-win32-bcrypt.patch   | 145 +++++++++++++++++++++
 contrib/src/libarchive/rules.mak                   |   1 +
 2 files changed, 146 insertions(+)

diff --git a/contrib/src/libarchive/libarchive-win32-bcrypt.patch b/contrib/src/libarchive/libarchive-win32-bcrypt.patch
new file mode 100644
index 0000000000..4fb37a7073
--- /dev/null
+++ b/contrib/src/libarchive/libarchive-win32-bcrypt.patch
@@ -0,0 +1,145 @@
+From adea9385d6e57cdc11e073bdcf214012c261f5f6 Mon Sep 17 00:00:00 2001
+From: Steve Lhomme <robux4 at ycbcr.xyz>
+Date: Fri, 27 Mar 2020 11:40:51 +0100
+Subject: [PATCH 3/4] use bcrypt rather than wincrypt
+
+---
+ configure.ac                |  4 ++--
+ libarchive/archive_random.c | 26 ++++++++++----------------
+ libarchive/archive_util.c   | 20 ++++++++++----------
+ 3 files changed, 22 insertions(+), 28 deletions(-)
+
+diff --git a/configure.ac b/configure.ac
+index c517b17c..7312c4d5 100644
+--- a/configure.ac
++++ b/configure.ac
+@@ -295,7 +295,7 @@ AC_CHECK_HEADERS([sys/time.h sys/utime.h sys/utsname.h sys/vfs.h sys/xattr.h])
+ AC_CHECK_HEADERS([time.h unistd.h utime.h wchar.h wctype.h])
+ AC_CHECK_HEADERS([windows.h])
+ # check windows.h first; the other headers require it.
+-AC_CHECK_HEADERS([wincrypt.h winioctl.h],[],[],
++AC_CHECK_HEADERS([bcrypt.h winioctl.h],[],[],
+ [[#ifdef HAVE_WINDOWS_H
+ # include <windows.h>
+ #endif
+@@ -1066,7 +1066,7 @@ AC_DEFUN([CRYPTO_CHECK_WIN], [
+     AC_LINK_IFELSE([AC_LANG_SOURCE([
+ #define ARCHIVE_$1_COMPILE_TEST
+ #include <windows.h>
+-#include <wincrypt.h>
++#include <bcrypt.h>
+ 
+ int
+ main(int argc, char **argv)
+diff --git a/libarchive/archive_random.c b/libarchive/archive_random.c
+index 65ea6915..c39d9f54 100644
+--- a/libarchive/archive_random.c
++++ b/libarchive/archive_random.c
+@@ -58,8 +58,8 @@ static void arc4random_buf(void *, size_t);
+ #include "archive.h"
+ #include "archive_random_private.h"
+ 
+-#if defined(HAVE_WINCRYPT_H) && !defined(__CYGWIN__)
+-#include <wincrypt.h>
++#if defined(HAVE_BCRYPT_H) && !defined(__CYGWIN__)
++#include <bcrypt.h>
+ #endif
+ 
+ #ifndef O_CLOEXEC
+@@ -74,20 +74,14 @@ static void arc4random_buf(void *, size_t);
+ int
+ archive_random(void *buf, size_t nbytes)
+ {
+-#if defined(_WIN32) && !defined(__CYGWIN__)
+-	HCRYPTPROV hProv;
+-	BOOL success;
+-
+-	success = CryptAcquireContext(&hProv, NULL, NULL, PROV_RSA_FULL,
+-	    CRYPT_VERIFYCONTEXT);
+-	if (!success && GetLastError() == (DWORD)NTE_BAD_KEYSET) {
+-		success = CryptAcquireContext(&hProv, NULL, NULL,
+-		    PROV_RSA_FULL, CRYPT_NEWKEYSET);
+-	}
+-	if (success) {
+-		success = CryptGenRandom(hProv, (DWORD)nbytes, (BYTE*)buf);
+-		CryptReleaseContext(hProv, 0);
+-		if (success)
++#if defined(HAVE_BCRYPT_H) && !defined(__CYGWIN__)
++	BCRYPT_ALG_HANDLE algo_handle;
++	NTSTATUS ret = BCryptOpenAlgorithmProvider(&algo_handle, BCRYPT_RNG_ALGORITHM,
++	                                           MS_PRIMITIVE_PROVIDER, 0);
++	if (BCRYPT_SUCCESS(ret)) {
++		ret = BCryptGenRandom(algo_handle, buf, nbytes, 0);
++		BCryptCloseAlgorithmProvider(algo_handle, 0);
++		if (BCRYPT_SUCCESS(ret))
+ 			return ARCHIVE_OK;
+ 	}
+ 	/* TODO: Does this case really happen? */
+diff --git a/libarchive/archive_util.c b/libarchive/archive_util.c
+index 3399c0b5..38e4a839 100644
+--- a/libarchive/archive_util.c
++++ b/libarchive/archive_util.c
+@@ -42,8 +42,8 @@ __FBSDID("$FreeBSD: head/lib/libarchive/archive_util.c 201098 2009-12-28 02:58:1
+ #ifdef HAVE_STRING_H
+ #include <string.h>
+ #endif
+-#if defined(HAVE_WINCRYPT_H) && !defined(__CYGWIN__)
+-#include <wincrypt.h>
++#if defined(HAVE_BCRYPT_H) && !defined(__CYGWIN__)
++#include <bcrypt.h>
+ #endif
+ #ifdef HAVE_ZLIB_H
+ #include <zlib.h>
+@@ -233,14 +233,14 @@ __archive_mktemp(const char *tmpdir)
+ 		L'm', L'n', L'o', L'p', L'q', L'r', L's', L't',
+ 		L'u', L'v', L'w', L'x', L'y', L'z'
+ 	};
+-	HCRYPTPROV hProv;
++	BCRYPT_ALG_HANDLE algo_handle;
+ 	struct archive_wstring temp_name;
+ 	wchar_t *ws;
+ 	DWORD attr;
+ 	wchar_t *xp, *ep;
+ 	int fd;
+ 
+-	hProv = (HCRYPTPROV)NULL;
++	algo_handle = NULL;
+ 	fd = -1;
+ 	ws = NULL;
+ 	archive_string_init(&temp_name);
+@@ -302,8 +302,8 @@ __archive_mktemp(const char *tmpdir)
+ 	ep = temp_name.s + archive_strlen(&temp_name);
+ 	xp = ep - wcslen(suffix);
+ 
+-	if (!CryptAcquireContext(&hProv, NULL, NULL, PROV_RSA_FULL,
+-		CRYPT_VERIFYCONTEXT)) {
++	if (!BCRYPT_SUCCESS(BCryptOpenAlgorithmProvider(&algo_handle, BCRYPT_RNG_ALGORITHM,
++	                                           MS_PRIMITIVE_PROVIDER, 0))) {
+ 		la_dosmaperr(GetLastError());
+ 		goto exit_tmpfile;
+ 	}
+@@ -314,8 +314,8 @@ __archive_mktemp(const char *tmpdir)
+ 
+ 		/* Generate a random file name through CryptGenRandom(). */
+ 		p = xp;
+-		if (!CryptGenRandom(hProv, (DWORD)(ep - p)*sizeof(wchar_t),
+-		    (BYTE*)p)) {
++		if (!BCRYPT_SUCCESS(BCryptGenRandom(algo_handle, p,
++		                                   (DWORD)(ep - p)*sizeof(wchar_t), 0))) {
+ 			la_dosmaperr(GetLastError());
+ 			goto exit_tmpfile;
+ 		}
+@@ -355,8 +355,8 @@ __archive_mktemp(const char *tmpdir)
+ 			break;/* success! */
+ 	}
+ exit_tmpfile:
+-	if (hProv != (HCRYPTPROV)NULL)
+-		CryptReleaseContext(hProv, 0);
++	if (algo_handle != (HCRYPTPROV)NULL)
++		BCryptCloseAlgorithmProvider(algo_handle, 0);
+ 	free(ws);
+ 	archive_wstring_free(&temp_name);
+ 	return (fd);
+-- 
+2.26.0.windows.1
+
diff --git a/contrib/src/libarchive/rules.mak b/contrib/src/libarchive/rules.mak
index fd9fe7c8f6..1920820bee 100644
--- a/contrib/src/libarchive/rules.mak
+++ b/contrib/src/libarchive/rules.mak
@@ -22,6 +22,7 @@ ifdef HAVE_ANDROID
 endif
 ifdef HAVE_WINSTORE
 	$(APPLY) $(SRC)/libarchive/no-windows-files.patch
+	$(APPLY) $(SRC)/libarchive/libarchive-win32-bcrypt.patch
 	$(APPLY) $(SRC)/libarchive/winrt.patch
 endif
 	$(APPLY) $(SRC)/libarchive/fix-types.patch



More information about the vlc-commits mailing list