Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Invalid value for key 'Encrypt' #2019

Closed
qizu0302 opened this issue Apr 25, 2023 · 17 comments
Closed

Invalid value for key 'Encrypt' #2019

qizu0302 opened this issue Apr 25, 2023 · 17 comments
Labels
ℹ️ Needs more Info Waiting on additional information

Comments

@qizu0302
Copy link

I'm using Microsoft.Data.SqlClient 5.1.0, set SqlConnectionStringBuilder.Encrypt to SqlConnectionEncryptOption.Strict, it always pop up "Invalid value for key 'Encrypt' " error, I tried other values as well, like true/false/SqlConnectionEncryptOption.Optional, all failed.

image

Do you know the right value for 'Encrypt' value?
And if I want to set up a TLS 1.3 connection to SQL server, do I need to use the latest SQL Server release?

@ErikEJ
Copy link
Contributor

ErikEJ commented Apr 25, 2023

@qizu0302 Sure you are not referencing the System.data.SqlClient SqlConnectionStringBuilder?

@qizu0302
Copy link
Author

@ErikEJ Pretty sure I use M.D.S lib
image

@JRahnama
Copy link
Member

@qizu0302 not sure on your setup, I just tested with that version and it worked fine. Can you provide a repro please?

@JRahnama JRahnama added ℹ️ Needs more Info Waiting on additional information and removed untriaged labels Apr 25, 2023
@Wraith2
Copy link
Contributor

Wraith2 commented Apr 25, 2023

could this be the same issue as #2015 ?

@qizu0302
Copy link
Author

Here's my part of code:

configDatabaseConnection = new SqlConnectionStringBuilder();
configDatabaseConnection.DataSource = server;
configDatabaseConnection.InitialCatalog = configDatabase;
configDatabaseConnection.Encrypt = SqlConnectionEncryptOption.Strict;
configDatabaseConnection.TrustServerCertificate = true;
configDatabaseConnection.IntegratedSecurity = true;

configDatabaseConnection is used to set up the connection between SharePoint server and SQL Server, the output is below:

Exception: System.ArgumentException: Invalid value for key 'Encrypt'.
at Microsoft.Data.Common.DbConnectionStringBuilderUtil.ConvertToSqlConnectionEncryptOption(String keyword, Object value)
at Microsoft.Data.SqlClient.SqlConnectionStringBuilder.set_Item(String keyword, Object value)
at Microsoft.SharePoint.Utilities.SafeSqlConnectionStringBuilder..ctor(SqlConnectionStringBuilder builder, Boolean copy)
at Microsoft.SharePoint.Administration.SPConfigurationDatabase.Provision(SqlConnectionStringBuilder connectionString)
at Microsoft.SharePoint.Administration.SPFarmFactory.CreateConfigurationDatabase()
at Microsoft.SharePoint.Administration.SPFarmFactory.Create()
at Microsoft.SharePoint.Administration.SPFarm.Create(SqlConnectionStringBuilder configurationDatabase, SqlConnectionStringBuilder administrationContentDatabase, SqlConnectionStringBuilder siteMapDatabase, SqlConnectionStringBuilder timerServiceDatabase, IdentityType identityType, String farmUser, SecureString farmPassword, SecureString masterPassphrase)
at Microsoft.SharePoint.Administration.SPFarm.Create(SqlConnectionStringBuilder configurationDatabase, SqlConnectionStringBuilder administrationContentDatabase, String farmUser, SecureString farmPassword, SecureString masterPassphrase)
at Microsoft.SharePoint.PostSetupConfiguration.ConfigurationDatabaseTask.CreateOrConnectConfigDb()
at Microsoft.SharePoint.PostSetupConfiguration.ConfigurationDatabaseTask.Run()
at Microsoft.SharePoint.PostSetupConfiguration.TaskThread.ExecuteTask()

@qizu0302
Copy link
Author

@JRahnama does your PR aim at this issue? After your PR check in, how can I get the latest library? I mean which NuGet package version I need to use.

@Wraith2
Copy link
Contributor

Wraith2 commented Apr 26, 2023

@qizu0302
Copy link
Author

qizu0302 commented May 4, 2023

This is 5.10.0 version, I don't see it on https://www.nuget.org/packages?q=Microsoft.Data.SqlClient.
Only using this package in my lab also has some errors, so I need to clarify its dependency versions
image

@qizu0302
Copy link
Author

qizu0302 commented May 8, 2023

BTW, in order to use TLS 1.3, do I have to use SQL Server 2022 and SSMS 19.0?

@JRahnama
Copy link
Member

JRahnama commented May 8, 2023

for TLS 1.3 it has to be SQL Server 2022 and Encrypt type Strict. SSMS 19 yet to add support for it. You can use M.D.SqlClient to achieve that.

@qizu0302
Copy link
Author

I have a basic question, if I do following steps:

  1. Set 'Force Strict Encryption' to yes in Sql Server Configuration Manager
  2. Enable TLS 1.3 in \HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\SecurityProviders\SCHANNEL\Protocols\TLS 1.3\Server(Client)
  3. Set 'Encrypt connection' and 'Trust server certificate' in SSMS 19

Does it use TLS 1.3 when connecting to localDB via SSMS?

If I want to let a client to connect to SQL Server using TLS 1.3, is there any configurations need to do on SQL Server side besides setting specific configurations in ConnectionString and registry on client side?

@qizu0302
Copy link
Author

qizu0302 commented May 17, 2023

Another issue, set "Force strict Encryption" in Sql Server Configuration Manager, disable legacy SSL version, enable TLS 1.3 in registry, then Sql Server service can't start, unless I enable TLS 1.2 as well.
image

The cert I use is a self-signed one generated on Sql Server, does this issue relate to the cert?
image

@ErikEJ
Copy link
Contributor

ErikEJ commented May 17, 2023

SSMS 19.0 does not support TLS 1.3

@JRahnama
Copy link
Member

I have a basic question, if I do following steps:

  1. Set 'Force Strict Encryption' to yes in Sql Server Configuration Manager
  2. Enable TLS 1.3 in \HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\SecurityProviders\SCHANNEL\Protocols\TLS 1.3\Server(Client)
  3. Set 'Encrypt connection' and 'Trust server certificate' in SSMS 19

Does it use TLS 1.3 when connecting to localDB via SSMS?

If I want to let a client to connect to SQL Server using TLS 1.3, is there any configurations need to do on SQL Server side besides setting specific configurations in ConnectionString and registry on client side?

Encryption types are not supported on LocalDB. Also to add more SSMS 19, as @ErikEJ mentioned, is not supporting Strict and TLS 1.3. From what I know, you can check and confirm with SSMS team, it will be added in next version of SSMS.

@qizu0302
Copy link
Author

qizu0302 commented May 18, 2023

I create a self-signed cert on Sql Server via
New-SelfSignedCertificate -Subject "CN=SQL-2022.contoso.local" -KeyAlgorithm RSA -KeyLength 2048 -CertStoreLocation "cert:\LocalMachine\My" -TextExtension @("2.5.29.17={text}DNS=SQL-2022.contoso.local&IPAddress=127.0.0.1&IPAddress=::1") -HashAlgorithm "SHA256" -Type SSLServerAuthentication -Provider "Microsoft RSA SChannel Cryptographic Provider"

then import this cert on my client side to be its trusted CA, do following settings in my code

configDatabaseConnection.Encrypt = SqlConnectionEncryptOption.Strict;
configDatabaseConnection.TrustServerCertificate = true;
configDatabaseConnection.HostNameInCertificate = "SQL-2022.contoso.local";

I enable TLS 1.3 and disable legacy versions on client server, set "Force strict encryption" to "yes" on Sql Server.
But when I try to set up a strict connection between client app and Sql Server, it shows

Microsoft.Data.SqlClient.SqlException: 'A connection was successfully established with the server, but then an error occurred during the pre-login handshake. (provider: TCP Provider, error: 0 - An existing connection was forcibly closed by the remote host.)''

According to https://learn.microsoft.com/en-us/sql/relational-databases/security/networking/tds-8-and-tls-1-3?view=sql-server-ver16#differences-between-tls-12-and-tls-13, RSA key exchange is removed in TLS 1.3 and my cert uses RSA as KeyAlgorithm, is this the root cause? If so, what else algorithms can I use?

@yuanhe772
Copy link

Hi team, any updates on this? Im using Microsoft.Data.SqlClient=v5.2.0 and Microsoft.EntityFrameworkCore=v6.0.26, and Im getting a similar error that SqlConnectionStringBuilder cannot recognize "Strict" as the Encrypt option

"InnerException":{"ClassName":"System.FormatException","Message":"String 'Strict' was not recognized as a valid Boolean.","Data":null,"InnerException":null,"HelpURL":null,"StackTraceString":"
   at System.Boolean.Parse(ReadOnlySpan`1 value)\r\n
   at System.Boolean.Parse(String value)\r\n
   at Microsoft.Data.SqlClient.SqlConnectionStringBuilder.set_Item(String keyword, Object value)\r\n
   at System.Data.Common.DbConnectionStringBuilder.set_ConnectionString(String value)\r\n
   at Microsoft.Data.SqlClient.SqlConnectionStringBuilder..ctor(String connectionString)\r\n
   at Microsoft.EntityFrameworkCore.SqlServer.Storage.Internal.SqlServerConnection.<>c.<get_IsMultipleActiveResultSetsEnabled>b__7_0(String cs)\r\n
   at System.Collections.Concurrent.ConcurrentDictionary`2.GetOrAdd(TKey key, Func`2 valueFactory)\r\n
   at Microsoft.EntityFrameworkCore.SqlServer.Query.Internal.SqlServerCompiledQueryCacheKeyGenerator.GenerateCacheKey(Expression query, Boolean async)\r\n
   at Microsoft.EntityFrameworkCore.Query.Internal.QueryCompiler.ExecuteAsync[TResult](Expression query, CancellationToken cancellationToken)\r\n
   at Microsoft.EntityFrameworkCore.Query.Internal.EntityQueryProvider.ExecuteAsync[TResult](Expression expression, CancellationToken cancellationToken)\r\n
   at Microsoft.EntityFrameworkCore.EntityFrameworkQueryableExtensions.ExecuteAsync[TSource,TResult](MethodInfo operatorMethodInfo, IQueryable`1 source, Expression expression, CancellationToken cancellationToken)\r\n
   at Microsoft.EntityFrameworkCore.EntityFrameworkQueryableExtensions.ExecuteAsync[TSource,TResult](MethodInfo operatorMethodInfo, IQueryable`1 source, CancellationToken cancellationToken)\r\n
   at Microsoft.EntityFrameworkCore.EntityFrameworkQueryableExtensions.SingleOrDefaultAsync[TSource](IQueryable`1 source, CancellationToken cancellationToken)\r\n

@cheenamalhotra
Copy link
Member

Please update EF Core to v7 to use MDS 5.1+. Closing as compatibility error.

@cheenamalhotra cheenamalhotra closed this as not planned Won't fix, can't repro, duplicate, stale Jul 3, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
ℹ️ Needs more Info Waiting on additional information
Projects
Development

No branches or pull requests

6 participants