Skip to content
This repository has been archived by the owner on May 25, 2020. It is now read-only.

Commit

Permalink
Try different TLS versions to allow download if TLS1.0 is disabled on…
Browse files Browse the repository at this point in the history
… the server fixes luckyrat#20
  • Loading branch information
haidelber committed Jan 6, 2019
1 parent 4082458 commit 05da5cd
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 0 deletions.
3 changes: 3 additions & 0 deletions KeePassFaviconDownloader.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,13 @@
<Reference Include="System" />
<Reference Include="System.Data" />
<Reference Include="System.Drawing" />
<Reference Include="System.Net" />
<Reference Include="System.Security" />
<Reference Include="System.Windows.Forms" />
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="TlsExtensions.cs" />
<Compile Include="KeePassFaviconDownloaderExt.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
Expand Down
10 changes: 10 additions & 0 deletions KeePassFaviconDownloaderExt.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ public sealed class KeePassFaviconDownloaderExt : Plugin
{
// The plugin remembers its host in this variable.
private IPluginHost m_host = null;
private static SecurityProtocolType originalSecurityProtocol = System.Net.ServicePointManager.SecurityProtocol;

public override string UpdateUrl
{
Expand Down Expand Up @@ -439,13 +440,22 @@ private Uri getFromFaviconExplicitLocation(Uri fullURI, ref MemoryStream ms, ref
/// <param name="message">Any error message is sent back through this string.</param>
/// <returns></returns>
private bool getFavicon(Uri uri, ref MemoryStream ms, ref string message)
{
return getFaviconWithSecurityProtocol(uri, ref ms, ref message, originalSecurityProtocol) ||
(originalSecurityProtocol != SecurityProtocolTypeExtensions.Tls12 && getFaviconWithSecurityProtocol(uri, ref ms, ref message, SecurityProtocolTypeExtensions.Tls12)) ||
(originalSecurityProtocol != SecurityProtocolTypeExtensions.Tls11 && getFaviconWithSecurityProtocol(uri, ref ms, ref message, SecurityProtocolTypeExtensions.Tls11));
}

private bool getFaviconWithSecurityProtocol(Uri uri, ref MemoryStream ms, ref string message, SecurityProtocolType securityProtocolType)
{
Stream s = null;
Image img = null;
MemoryStream memStream = new MemoryStream();

try
{
ServicePointManager.SecurityProtocol = securityProtocolType;

WebRequest webreq = WebRequest.Create(uri);
((HttpWebRequest)webreq).UserAgent = "Mozilla/5.0 (Windows 6.1; rv:27.0) Gecko/20100101 Firefox/27.0";
((HttpWebRequest)webreq).CookieContainer = new System.Net.CookieContainer();
Expand Down
26 changes: 26 additions & 0 deletions TlsExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
using System.Security.Authentication;

namespace System.Net
{
/// <summary>
/// https://support.microsoft.com/en-gb/help/3154517/support-for-tls-system-default-versions-included-in-the-net-framework
/// </summary>
public static class SecurityProtocolTypeExtensions
{
public const SecurityProtocolType Tls12 = (SecurityProtocolType)SslProtocolsExtensions.Tls12;
public const SecurityProtocolType Tls11 = (SecurityProtocolType)SslProtocolsExtensions.Tls11;
public const SecurityProtocolType SystemDefault = (SecurityProtocolType)0;
}
}

namespace System.Security.Authentication
{
/// <summary>
/// https://support.microsoft.com/en-gb/help/3154517/support-for-tls-system-default-versions-included-in-the-net-framework
/// </summary>
public static class SslProtocolsExtensions
{
public const SslProtocols Tls12 = (SslProtocols)0x00000C00;
public const SslProtocols Tls11 = (SslProtocols)0x00000300;
}
}

0 comments on commit 05da5cd

Please sign in to comment.