Skip to content

Commit d66bb40

Browse files
committedAug 14, 2015
Allow developers to override the allowed SSL protocols for IMAP, POP3, and SMTP
Fixes issue #229
1 parent f162386 commit d66bb40

File tree

4 files changed

+44
-31
lines changed

4 files changed

+44
-31
lines changed
 

‎MailKit/MailService.cs

+32
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
#if !NETFX_CORE
3434
using System.Net.Security;
3535
using System.Security.Cryptography.X509Certificates;
36+
using SslProtocols = System.Security.Authentication.SslProtocols;
3637
#endif
3738

3839
using MailKit.Security;
@@ -46,6 +47,12 @@ namespace MailKit {
4647
/// </remarks>
4748
public abstract class MailService : IMailService
4849
{
50+
#if NET_4_5 || __MOBILE__
51+
const SslProtocols DefaultSslProtocols = SslProtocols.Tls | SslProtocols.Tls11 | SslProtocols.Tls12;
52+
#elif !NETFX_CORE
53+
const SslProtocols DefaultSslProtocols = SslProtocols.Tls;
54+
#endif
55+
4956
/// <summary>
5057
/// Initializes a new instance of the <see cref="MailKit.MailService"/> class.
5158
/// </summary>
@@ -61,9 +68,21 @@ protected MailService (IProtocolLogger protocolLogger)
6168
if (protocolLogger == null)
6269
throw new ArgumentNullException ("protocolLogger");
6370

71+
SslProtocols = DefaultSslProtocols;
6472
ProtocolLogger = protocolLogger;
6573
}
6674

75+
/// <summary>
76+
/// Initializes a new instance of the <see cref="MailKit.MailService"/> class.
77+
/// </summary>
78+
/// <remarks>
79+
/// Initializes a new instance of the <see cref="MailKit.MailService"/> class.
80+
/// </remarks>
81+
protected MailService ()
82+
{
83+
SslProtocols = DefaultSslProtocols;
84+
}
85+
6786
/// <summary>
6887
/// Releases unmanaged resources and performs other cleanup operations before the
6988
/// <see cref="MailService"/> is reclaimed by garbage collection.
@@ -113,6 +132,19 @@ public IProtocolLogger ProtocolLogger {
113132
}
114133

115134
#if !NETFX_CORE
135+
/// <summary>
136+
/// Gets or sets the SSL/TLS protocols that the client is allowed to use.
137+
/// </summary>
138+
/// <remarks>
139+
/// <para>Gets or sets the SSL/TLS protocols that the client is allowed to use.</para>
140+
/// <para>This property should be set before calling any of the
141+
/// <a href="Overload_MailKit_MailService_Connect.htm">Connect</a> methods.</para>
142+
/// </remarks>
143+
/// <value>The ssl protocols.</value>
144+
public SslProtocols SslProtocols {
145+
get; set;
146+
}
147+
116148
/// <summary>
117149
/// Gets or sets the client SSL certificates.
118150
/// </summary>

‎MailKit/Net/Imap/ImapClient.cs

+4-10
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,6 @@
4242
using System.Net.Sockets;
4343
using System.Net.Security;
4444
using System.Security.Cryptography.X509Certificates;
45-
using SslProtocols = System.Security.Authentication.SslProtocols;
4645
#endif
4746

4847
using MailKit.Security;
@@ -67,11 +66,6 @@ public class ImapClient : MailStore
6766
{
6867
static readonly char[] ReservedUriCharacters = new [] { ';', '/', '?', ':', '@', '&', '=', '+', '$', ',' };
6968
const string HexAlphabet = "0123456789ABCDEF";
70-
#if NET_4_5 || __MOBILE__
71-
const SslProtocols DefaultSslProtocols = SslProtocols.Tls | SslProtocols.Tls11 | SslProtocols.Tls12;
72-
#elif !NETFX_CORE
73-
const SslProtocols DefaultSslProtocols = SslProtocols.Tls;
74-
#endif
7569
readonly ImapEngine engine;
7670
#if NETFX_CORE
7771
StreamSocket socket;
@@ -1196,7 +1190,7 @@ static void ComputeDefaultValues (string host, ref int port, ref SecureSocketOpt
11961190

11971191
if (options == SecureSocketOptions.SslOnConnect) {
11981192
var ssl = new SslStream (new NetworkStream (socket, true), false, ValidateRemoteCertificate);
1199-
ssl.AuthenticateAsClient (host, ClientCertificates, DefaultSslProtocols, true);
1193+
ssl.AuthenticateAsClient (host, ClientCertificates, SslProtocols, true);
12001194
stream = ssl;
12011195
} else {
12021196
stream = new NetworkStream (socket, true);
@@ -1246,7 +1240,7 @@ static void ComputeDefaultValues (string host, ref int port, ref SecureSocketOpt
12461240
if (ic.Response == ImapCommandResponse.Ok) {
12471241
#if !NETFX_CORE
12481242
var tls = new SslStream (stream, false, ValidateRemoteCertificate);
1249-
tls.AuthenticateAsClient (host, ClientCertificates, DefaultSslProtocols, true);
1243+
tls.AuthenticateAsClient (host, ClientCertificates, SslProtocols, true);
12501244
engine.Stream.Stream = tls;
12511245
#else
12521246
socket.UpgradeToSslAsync (SocketProtectionLevel.Tls12, new HostName (host))
@@ -1363,7 +1357,7 @@ static void ComputeDefaultValues (string host, ref int port, ref SecureSocketOpt
13631357

13641358
if (options == SecureSocketOptions.SslOnConnect) {
13651359
var ssl = new SslStream (new NetworkStream (socket, true), false, ValidateRemoteCertificate);
1366-
ssl.AuthenticateAsClient (host, ClientCertificates, DefaultSslProtocols, true);
1360+
ssl.AuthenticateAsClient (host, ClientCertificates, SslProtocols, true);
13671361
stream = ssl;
13681362
} else {
13691363
stream = new NetworkStream (socket, true);
@@ -1393,7 +1387,7 @@ static void ComputeDefaultValues (string host, ref int port, ref SecureSocketOpt
13931387

13941388
if (ic.Response == ImapCommandResponse.Ok) {
13951389
var tls = new SslStream (stream, false, ValidateRemoteCertificate);
1396-
tls.AuthenticateAsClient (host, ClientCertificates, DefaultSslProtocols, true);
1390+
tls.AuthenticateAsClient (host, ClientCertificates, SslProtocols, true);
13971391
engine.Stream.Stream = tls;
13981392

13991393
// Query the CAPABILITIES again if the server did not include an

‎MailKit/Net/Pop3/Pop3Client.cs

+4-11
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,6 @@
4545
using System.Net.Security;
4646
using System.Security.Cryptography;
4747
using System.Security.Cryptography.X509Certificates;
48-
using SslProtocols = System.Security.Authentication.SslProtocols;
4948
using MD5 = System.Security.Cryptography.MD5CryptoServiceProvider;
5049
#endif
5150

@@ -69,12 +68,6 @@ namespace MailKit.Net.Pop3 {
6968
/// </example>
7069
public class Pop3Client : MailSpool
7170
{
72-
#if NET_4_5 || __MOBILE__
73-
const SslProtocols DefaultSslProtocols = SslProtocols.Tls | SslProtocols.Tls11 | SslProtocols.Tls12;
74-
#elif !NETFX_CORE
75-
const SslProtocols DefaultSslProtocols = SslProtocols.Tls;
76-
#endif
77-
7871
[Flags]
7972
enum ProbedCapabilities : byte {
8073
None = 0,
@@ -779,7 +772,7 @@ static void ComputeDefaultValues (string host, ref int port, ref SecureSocketOpt
779772

780773
if (options == SecureSocketOptions.SslOnConnect) {
781774
var ssl = new SslStream (new NetworkStream (socket, true), false, ValidateRemoteCertificate);
782-
ssl.AuthenticateAsClient (host, ClientCertificates, DefaultSslProtocols, true);
775+
ssl.AuthenticateAsClient (host, ClientCertificates, SslProtocols, true);
783776
stream = ssl;
784777
} else {
785778
stream = new NetworkStream (socket, true);
@@ -824,7 +817,7 @@ static void ComputeDefaultValues (string host, ref int port, ref SecureSocketOpt
824817

825818
#if !NETFX_CORE
826819
var tls = new SslStream (stream, false, ValidateRemoteCertificate);
827-
tls.AuthenticateAsClient (host, ClientCertificates, DefaultSslProtocols, true);
820+
tls.AuthenticateAsClient (host, ClientCertificates, SslProtocols, true);
828821
engine.Stream.Stream = tls;
829822
#else
830823
socket.UpgradeToSslAsync (SocketProtectionLevel.Tls12, new HostName (host))
@@ -939,7 +932,7 @@ static void ComputeDefaultValues (string host, ref int port, ref SecureSocketOpt
939932

940933
if (options == SecureSocketOptions.SslOnConnect) {
941934
var ssl = new SslStream (new NetworkStream (socket, true), false, ValidateRemoteCertificate);
942-
ssl.AuthenticateAsClient (host, ClientCertificates, DefaultSslProtocols, true);
935+
ssl.AuthenticateAsClient (host, ClientCertificates, SslProtocols, true);
943936
stream = ssl;
944937
} else {
945938
stream = new NetworkStream (socket, true);
@@ -965,7 +958,7 @@ static void ComputeDefaultValues (string host, ref int port, ref SecureSocketOpt
965958
SendCommand (cancellationToken, "STLS");
966959

967960
var tls = new SslStream (stream, false, ValidateRemoteCertificate);
968-
tls.AuthenticateAsClient (host, ClientCertificates, DefaultSslProtocols, true);
961+
tls.AuthenticateAsClient (host, ClientCertificates, SslProtocols, true);
969962
engine.Stream.Stream = tls;
970963

971964
// re-issue a CAPA command

‎MailKit/Net/Smtp/SmtpClient.cs

+4-10
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,6 @@
4545
using System.Net.Sockets;
4646
using System.Net.Security;
4747
using System.Security.Cryptography.X509Certificates;
48-
using SslProtocols = System.Security.Authentication.SslProtocols;
4948
#endif
5049

5150
using MailKit.Security;
@@ -70,11 +69,6 @@ namespace MailKit.Net.Smtp {
7069
/// </example>
7170
public class SmtpClient : MailTransport
7271
{
73-
#if NET_4_5 || __MOBILE__
74-
const SslProtocols DefaultSslProtocols = SslProtocols.Tls | SslProtocols.Tls11 | SslProtocols.Tls12;
75-
#elif !NETFX_CORE
76-
const SslProtocols DefaultSslProtocols = SslProtocols.Tls;
77-
#endif
7872
static readonly byte[] EndData = Encoding.ASCII.GetBytes ("\r\n.\r\n");
7973
const int MaxLineLength = 998;
8074

@@ -810,7 +804,7 @@ static void ComputeDefaultValues (string host, ref int port, ref SecureSocketOpt
810804

811805
if (options == SecureSocketOptions.SslOnConnect) {
812806
var ssl = new SslStream (new NetworkStream (socket, true), false, ValidateRemoteCertificate);
813-
ssl.AuthenticateAsClient (host, ClientCertificates, DefaultSslProtocols, true);
807+
ssl.AuthenticateAsClient (host, ClientCertificates, SslProtocols, true);
814808
stream = ssl;
815809
} else {
816810
stream = new NetworkStream (socket, true);
@@ -862,7 +856,7 @@ static void ComputeDefaultValues (string host, ref int port, ref SecureSocketOpt
862856

863857
#if !NETFX_CORE
864858
var tls = new SslStream (stream, false, ValidateRemoteCertificate);
865-
tls.AuthenticateAsClient (host, ClientCertificates, DefaultSslProtocols, true);
859+
tls.AuthenticateAsClient (host, ClientCertificates, SslProtocols, true);
866860
Stream.Stream = tls;
867861
#else
868862
socket.UpgradeToSslAsync (SocketProtectionLevel.Tls12, new HostName (host))
@@ -990,7 +984,7 @@ static void ComputeDefaultValues (string host, ref int port, ref SecureSocketOpt
990984

991985
if (options == SecureSocketOptions.SslOnConnect) {
992986
var ssl = new SslStream (new NetworkStream (socket, true), false, ValidateRemoteCertificate);
993-
ssl.AuthenticateAsClient (host, ClientCertificates, DefaultSslProtocols, true);
987+
ssl.AuthenticateAsClient (host, ClientCertificates, SslProtocols, true);
994988
stream = ssl;
995989
} else {
996990
stream = new NetworkStream (socket, true);
@@ -1024,7 +1018,7 @@ static void ComputeDefaultValues (string host, ref int port, ref SecureSocketOpt
10241018
throw new SmtpCommandException (SmtpErrorCode.UnexpectedStatusCode, response.StatusCode, response.Response);
10251019

10261020
var tls = new SslStream (stream, false, ValidateRemoteCertificate);
1027-
tls.AuthenticateAsClient (host, ClientCertificates, DefaultSslProtocols, true);
1021+
tls.AuthenticateAsClient (host, ClientCertificates, SslProtocols, true);
10281022
Stream.Stream = tls;
10291023

10301024
// Send EHLO again and get the new list of supported extensions

0 commit comments

Comments
 (0)
Please sign in to comment.