diff --git a/SOURCES/0001-lib-write-improve-key-collation-compatibility-with-Windows.patch b/SOURCES/0001-lib-write-improve-key-collation-compatibility-with-Windows.patch
new file mode 100644
index 0000000..9c82c26
--- /dev/null
+++ b/SOURCES/0001-lib-write-improve-key-collation-compatibility-with-Windows.patch
@@ -0,0 +1,80 @@
+From d5a522c0bb738efdd7cc1e762840b579fc9ea3de Mon Sep 17 00:00:00 2001
+From: Laszlo Ersek <lersek@redhat.com>
+Date: Fri, 10 Sep 2021 01:06:17 +0200
+Subject: [PATCH] lib: write: improve key collation compatibility with Windows
+
+There are multiple problems with using strcasecmp() for ordering registry
+keys:
+
+(1) strcasecmp() is influenced by LC_CTYPE.
+
+(2) strcasecmp() cannot implement case conversion for multibyte UTF-8
+    sequences.
+
+(3) Even with LC_CTYPE=POSIX and key names consisting solely of ASCII
+    characters, strcasecmp() converts characters to lowercase, for
+    comparison. But on Windows, the CompareStringOrdinal() function
+    converts characters to uppercase. This makes a difference when
+    comparing a letter to one of the characters that fall between 'Z'
+    (0x5A) and 'a' (0x61), namely {'[', '\\', ']', '^', '_', '`'}. For
+    example,
+
+      'c' (0x63) > '_' (0x5F)
+      'C' (0x43) < '_' (0x5F)
+
+Compare key names byte for byte, eliminating problems (1) and (3).
+
+Bugzilla: https://bugzilla.redhat.com/show_bug.cgi?id=1648520
+Signed-off-by: Laszlo Ersek <lersek@redhat.com>
+Message-Id: <20210909230617.31256-1-lersek@redhat.com>
+Acked-by: Richard W.M. Jones <rjones@redhat.com>
+---
+ lib/write.c | 32 +++++++++++++++++++++++++++++++-
+ 1 file changed, 31 insertions(+), 1 deletion(-)
+
+diff --git a/lib/write.c b/lib/write.c
+index 70105c9d9907..d9a13a3c18b6 100644
+--- a/lib/write.c
++++ b/lib/write.c
+@@ -462,7 +462,37 @@ compare_name_with_nk_name (hive_h *h, const char *name, hive_node_h nk_offs)
+     return 0;
+   }
+ 
+-  int r = strcasecmp (name, nname);
++  /* Perform a limited case-insensitive comparison. ASCII letters will be
++   * *upper-cased*. Multibyte sequences will produce nonsensical orderings.
++   */
++  int r = 0;
++  const char *s1 = name;
++  const char *s2 = nname;
++
++  for (;;) {
++    unsigned char c1 = *(s1++);
++    unsigned char c2 = *(s2++);
++
++    if (c1 >= 'a' && c1 <= 'z')
++      c1 = 'A' + (c1 - 'a');
++    if (c2 >= 'a' && c2 <= 'z')
++      c2 = 'A' + (c2 - 'a');
++    if (c1 < c2) {
++      /* Also covers the case when "name" is a prefix of "nname". */
++      r = -1;
++      break;
++    }
++    if (c1 > c2) {
++      /* Also covers the case when "nname" is a prefix of "name". */
++      r = 1;
++      break;
++    }
++    if (c1 == '\0') {
++      /* Both strings end. */
++      break;
++    }
++  }
++
+   free (nname);
+ 
+   return r;
+-- 
+2.19.1.3.g30247aa5d201
+
diff --git a/SPECS/hivex.spec b/SPECS/hivex.spec
index c5582b2..7a386e4 100644
--- a/SPECS/hivex.spec
+++ b/SPECS/hivex.spec
@@ -10,7 +10,7 @@
 
 Name:           hivex
 Version:        1.3.21
-Release:        2%{?dist}
+Release:        3%{?dist}
 Summary:        Read and write Windows Registry binary hive files
 
 License:        LGPLv2
@@ -26,6 +26,8 @@ Source1:        http://libguestfs.org/download/hivex/%{name}-%{version}.tar.gz.s
 Source2:       libguestfs.keyring
 %endif
 
+Patch0000:      0001-lib-write-improve-key-collation-compatibility-with-Windows.patch
+
 BuildRequires:  perl-interpreter
 BuildRequires:  perl-devel
 BuildRequires:  perl-generators
@@ -323,6 +325,10 @@ fi
 
 
 %changelog
+* Tue Sep 14 2021 Laszlo Ersek <lersek@redhat.com> - 1.3.21-3
+- Bring key collation order closer to that of Windows.
+- Resolves: RHBZ#1648524.
+
 * Mon Aug 09 2021 Mohan Boddu <mboddu@redhat.com> - 1.3.21-2
 - Rebuilt for IMA sigs, glibc 2.34, aarch64 flags
   Related: rhbz#1991688