Skip to content

Commit

Permalink
(GH-458) Support TLS v1.2
Browse files Browse the repository at this point in the history
The .NET Framework supports TLS v1.2 as of version 4.5 -
http://msdn.microsoft.com/en-us/library/system.security.authentication.sslprotocols(v=vs.110).aspx
but the security protocol is not set to use it out of the box.

Set the SecurityProtocol to start with the highest encryption available
and move down from there.

Choco is compiled on .NET 4.0, but .NET 4.5 is an in place upgrade,
which gives us access to set the proper security protocols by
converting the enumeration values directly into `SecurityProtocolType`.
This will fail when running on .NET 4.0, so fall back to using TLS,
then SSLv3 with a warning recommending folks upgrade to .NET 4.5 at
their earliest convenience.
  • Loading branch information
ferventcoder committed Sep 2, 2016
1 parent f4ae1c9 commit 2d39d97
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/chocolatey/chocolatey.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@
<Compile Include="infrastructure.app\events\HandlePackageResultCompletedMessage.cs" />
<Compile Include="infrastructure.app\services\FileTypeDetectorService.cs" />
<Compile Include="infrastructure.app\services\IFileTypeDetectorService.cs" />
<Compile Include="infrastructure\registration\SecurityProtocol.cs" />
<Compile Include="infrastructure\results\PowerShellExecutionResults.cs" />
<Compile Include="infrastructure.app\tasks\RemovePendingPackagesTask.cs" />
<Compile Include="infrastructure.app\templates\ChocolateyBeforeModifyTemplate.cs" />
Expand Down
3 changes: 3 additions & 0 deletions src/chocolatey/infrastructure.app/runners/GenericRunner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ namespace chocolatey.infrastructure.app.runners
using events;
using filesystem;
using infrastructure.events;
using infrastructure.registration;
using infrastructure.tasks;
using SimpleInjector;
using adapters;
Expand Down Expand Up @@ -135,6 +136,8 @@ public void run(ChocolateyConfiguration config, Container container, bool isCons

fail_when_license_is_missing_or_invalid_if_requested(config);

SecurityProtocol.set_protocol();

EventManager.publish(new PreRunMessage(config));

var command = find_command(config, container, isConsole, parseArgs);
Expand Down
55 changes: 55 additions & 0 deletions src/chocolatey/infrastructure/registration/SecurityProtocol.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
// Copyright © 2011 - Present RealDimensions Software, LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
//
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

namespace chocolatey.infrastructure.registration
{
using System;
using System.Net;
using logging;

public sealed class SecurityProtocol
{
private const int TLS_1_1 = 768;
private const int TLS_1_2 = 3072;

public static void set_protocol()
{
try
{
// We can't address the protocols directly when built with .NET
// Framework 4.0. However if someone is running .NET 4.5 or
// greater, they have in-place upgrades for System.dll, which
// will allow us to set these protocols directly.
const SecurityProtocolType tls11 = (SecurityProtocolType)TLS_1_1;
const SecurityProtocolType tls12 = (SecurityProtocolType)TLS_1_2;
ServicePointManager.SecurityProtocol = tls12 | tls11 | SecurityProtocolType.Tls | SecurityProtocolType.Ssl3;
}
catch (Exception)
{
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls | SecurityProtocolType.Ssl3;
"chocolatey".Log().Warn(ChocolateyLoggers.Important,
@" !!WARNING!!
Choco prefers to use TLS v1.2 if it is available, but this client is
running on .NET 4.0, which uses an older SSL. It's using TLS 1.0 or
earlier, which makes it susceptible to BEAST and also doesn't
implement the 1/n-1 record splitting mitigation for Cipher-Block
Chaining. Upgrade to at least .NET 4.5 at your earliest convenience.
For more information you should visit https://www.howsmyssl.com/");

}
}
}
}

1 comment on commit 2d39d97

@ferventcoder
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some of this code was originally at #459

Please sign in to comment.