From c7080ffcc139c3c1833c34ccaa8683efc562e5f9 Mon Sep 17 00:00:00 2001
From: Jakub Jelen <jjelen@redhat.com>
Date: Fri, 15 May 2026 17:01:21 +0200
Subject: [PATCH] CVE-2026-59847 libcrypto: Fix tag verification of
 AES-GCM ciphers
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

EVP_DecryptFinal() returns 0 errors, which was wrongly checked since
its introduction.

Reported by Ben Smyth discuss@bensmyth.com

Signed-off-by: Jakub Jelen <jjelen@redhat.com>
Reviewed-by: Pavol Žáčik <pzacik@redhat.com>
(cherry picked from commit 6d6cb6cb4d1684bb3f2b9fd23f3635e79d1e5495)

Index: libssh-0.10.6/src/libcrypto.c
===================================================================
--- libssh-0.10.6.orig/src/libcrypto.c
+++ libssh-0.10.6/src/libcrypto.c
@@ -586,7 +586,7 @@ evp_cipher_aead_encrypt(struct ssh_ciphe
     rc = EVP_EncryptFinal(cipher->ctx,
                           NULL,
                           &tmplen);
-    if (rc < 0) {
+    if (rc != 1) {
         SSH_LOG(SSH_LOG_WARNING, "EVP_EncryptFinal failed: Failed to create a tag");
         return;
     }
@@ -674,7 +674,7 @@ evp_cipher_aead_decrypt(struct ssh_ciphe
     rc = EVP_DecryptFinal(cipher->ctx,
                           NULL,
                           &outlen);
-    if (rc < 0) {
+    if (rc != 1 || outlen != 0) {
         SSH_LOG(SSH_LOG_WARNING, "EVP_DecryptFinal failed: Failed authentication");
         return SSH_ERROR;
     }
