From 20e1535b28b13fa1015556a84ccaeb47e7f498c6 Mon Sep 17 00:00:00 2001 From: Lawrence Cheung <31262254+lcheunglci@users.noreply.github.com> Date: Mon, 27 Mar 2023 15:41:03 -0400 Subject: [PATCH] [5.1.1] Fix | Addressing failure on providing correct error message when symmetric key decryption fails using Always Encrypted. (#1968) --- .../netcore/src/Microsoft/Data/SqlClient/TdsParser.cs | 7 +++++++ .../netfx/src/Microsoft/Data/SqlClient/TdsParser.cs | 7 +++++++ 2 files changed, 14 insertions(+) diff --git a/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/TdsParser.cs b/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/TdsParser.cs index 8217604c72..ba650cc932 100644 --- a/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/TdsParser.cs +++ b/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/TdsParser.cs @@ -6070,6 +6070,13 @@ internal bool TryReadSqlValue(SqlBuffer value, SqlMetaDataPriv md, int length, T } catch (Exception e) { + if (stateObj is not null) + { + // call to decrypt column keys has failed. The data wont be decrypted. + // Not setting the value to false, forces the driver to look for column value. + // Packet received from Key Vault will throws invalid token header. + stateObj.HasPendingData = false; + } throw SQL.ColumnDecryptionFailed(columnName, null, e); } } diff --git a/src/Microsoft.Data.SqlClient/netfx/src/Microsoft/Data/SqlClient/TdsParser.cs b/src/Microsoft.Data.SqlClient/netfx/src/Microsoft/Data/SqlClient/TdsParser.cs index 70673ef343..466414c425 100644 --- a/src/Microsoft.Data.SqlClient/netfx/src/Microsoft/Data/SqlClient/TdsParser.cs +++ b/src/Microsoft.Data.SqlClient/netfx/src/Microsoft/Data/SqlClient/TdsParser.cs @@ -6884,6 +6884,13 @@ internal bool TryReadSqlValue(SqlBuffer value, } catch (Exception e) { + if (stateObj is not null) + { + // call to decrypt column keys has failed. The data wont be decrypted. + // Not setting the value to false, forces the driver to look for column value. + // Packet received from Key Vault will throws invalid token header. + stateObj._pendingData = false; + } throw SQL.ColumnDecryptionFailed(columnName, null, e); } }