UpgradeToSslAsync() public méthode

public UpgradeToSslAsync ( SocketProtectionLevel protectionlevel, HostName validationHostName ) : UpgradeToSslOperation
protectionlevel SocketProtectionLevel
validationHostName HostName
Résultat UpgradeToSslOperation
Exemple #1
0
        /// <summary>
        /// Establishes a connection to the specified POP3 server.
        /// </summary>
        /// <remarks>
        /// <para>Establishes a connection to an POP3 or POP3/S server. If the schema
        /// in the uri is "pop", a clear-text connection is made and defaults to using
        /// port 110 if no port is specified in the URI. However, if the schema in the
        /// uri is "pops", an SSL connection is made using the
        /// <see cref="ClientCertificates"/> and defaults to port 995 unless a port
        /// is specified in the URI.</para>
        /// <para>It should be noted that when using a clear-text POP3 connection,
        /// if the server advertizes support for the STLS extension, the client
        /// will automatically switch into TLS mode before authenticating unless
        /// the <paramref name="uri"/> contains a query string to disable it.</para>
        /// If a successful connection is made, the <see cref="AuthenticationMechanisms"/>
        /// and <see cref="Capabilities"/> properties will be populated.
        /// </remarks>
        /// <param name="uri">The server URI. The <see cref="System.Uri.Scheme"/> should either
        /// be "pop" to make a clear-text connection or "pops" to make an SSL connection.</param>
        /// <param name="cancellationToken">A cancellation token.</param>
        /// <exception cref="System.ArgumentNullException">
        /// The <paramref name="uri"/> is <c>null</c>.
        /// </exception>
        /// <exception cref="System.ArgumentException">
        /// The <paramref name="uri"/> is not an absolute URI.
        /// </exception>
        /// <exception cref="System.ObjectDisposedException">
        /// The <see cref="Pop3Client"/> has been disposed.
        /// </exception>
        /// <exception cref="System.InvalidOperationException">
        /// The <see cref="Pop3Client"/> is already connected.
        /// </exception>
        /// <exception cref="System.OperationCanceledException">
        /// The operation was canceled via the cancellation token.
        /// </exception>
        /// <exception cref="System.IO.IOException">
        /// An I/O error occurred.
        /// </exception>
        /// <exception cref="Pop3CommandException">
        /// A POP3 command failed.
        /// </exception>
        /// <exception cref="Pop3ProtocolException">
        /// A POP3 protocol error occurred.
        /// </exception>
        public void Connect(Uri uri, CancellationToken cancellationToken)
        {
            CheckDisposed ();

            if (uri == null)
                throw new ArgumentNullException ("uri");

            if (!uri.IsAbsoluteUri)
                throw new ArgumentException ("The uri must be absolute.", "uri");

            if (IsConnected)
                throw new InvalidOperationException ("The Pop3Client is already connected.");

            var scheme = uri.Scheme.ToLowerInvariant ();
            var pops = scheme == "pops" || scheme == "pop3s";
            var port = uri.Port > 0 ? uri.Port : (pops ? 995 : 110);
            var query = uri.ParsedQuery ();
            #if !NETFX_CORE && !WINDOWS_APP && !WINDOWS_PHONE_APP
            var ipAddresses = Dns.GetHostAddresses (uri.DnsSafeHost);
            Socket socket = null;
            #endif
            Stream stream;
            string value;

            var starttls = !pops && (!query.TryGetValue ("starttls", out value) || Convert.ToBoolean (value));

            #if !NETFX_CORE && !WINDOWS_APP && !WINDOWS_PHONE_APP
            for (int i = 0; i < ipAddresses.Length; i++) {
                socket = new Socket (ipAddresses[i].AddressFamily, SocketType.Stream, ProtocolType.Tcp);

                cancellationToken.ThrowIfCancellationRequested ();

                try {
                    socket.Connect (ipAddresses[i], port);
                    break;
                } catch (Exception) {
                    if (i + 1 == ipAddresses.Length)
                        throw;
                }
            }

            if (pops) {
                var ssl = new SslStream (new NetworkStream (socket, true), false, ValidateRemoteCertificate);
                ssl.AuthenticateAsClient (uri.Host, ClientCertificates, SslProtocols.Default, true);
                stream = ssl;
            } else {
                stream = new NetworkStream (socket, true);
            }
            #else
            socket = new StreamSocket ();

            cancellationToken.ThrowIfCancellationRequested ();
            socket.ConnectAsync (new HostName (uri.DnsSafeHost), port.ToString (), pops ? SocketProtectionLevel.Tls12 : SocketProtectionLevel.PlainSocket)
                .AsTask (cancellationToken)
                .GetAwaiter ()
                .GetResult ();

            stream = new DuplexStream (socket.InputStream.AsStreamForRead (), socket.OutputStream.AsStreamForWrite ());
            #endif

            probed = ProbedCapabilities.None;
            host = uri.Host;

            logger.LogConnect (uri);

            engine.Connect (new Pop3Stream (stream, logger), cancellationToken);
            engine.QueryCapabilities (cancellationToken);

            if (starttls && (engine.Capabilities & Pop3Capabilities.StartTLS) != 0) {
                SendCommand (cancellationToken, "STLS");

            #if !NETFX_CORE && !WINDOWS_APP && !WINDOWS_PHONE_APP
                var tls = new SslStream (stream, false, ValidateRemoteCertificate);
                tls.AuthenticateAsClient (uri.Host, ClientCertificates, SslProtocols.Tls, true);
                engine.Stream.Stream = tls;
            #else
                socket.UpgradeToSslAsync (SocketProtectionLevel.Tls12, new HostName (uri.DnsSafeHost))
                    .AsTask (cancellationToken)
                    .GetAwaiter ()
                    .GetResult ();
            #endif

                // re-issue a CAPA command
                engine.QueryCapabilities (cancellationToken);
            }
        }
Exemple #2
0
        /// <summary>
        /// Establishes a connection to the specified SMTP server.
        /// </summary>
        /// <remarks>
        /// <para>Establishes a connection to an SMTP or SMTP/S server. If the schema
        /// in the uri is "smtp", a clear-text connection is made and defaults to using
        /// port 25 if no port is specified in the URI. However, if the schema in the
        /// uri is "smtps", an SSL connection is made using the
        /// <see cref="ClientCertificates"/> and defaults to port 465 unless a port
        /// is specified in the URI.</para>
        /// <para>It should be noted that when using a clear-text SMTP connection,
        /// if the server advertizes support for the STARTTLS extension, the client
        /// will automatically switch into TLS mode before authenticating unless the
        /// <paramref name="uri"/> contains a query string to disable it.</para>
        /// If a successful connection is made, the <see cref="AuthenticationMechanisms"/>
        /// and <see cref="Capabilities"/> properties will be populated.
        /// </remarks>
        /// <param name="uri">The server URI. The <see cref="System.Uri.Scheme"/> should either
        /// be "smtp" to make a clear-text connection or "smtps" to make an SSL connection.</param>
        /// <param name="cancellationToken">A cancellation token.</param>
        /// <exception cref="System.ArgumentNullException">
        /// <para>The <paramref name="uri"/> is <c>null</c>.</para>
        /// </exception>
        /// <exception cref="System.ArgumentException">
        /// The <paramref name="uri"/> is not an absolute URI.
        /// </exception>
        /// <exception cref="System.ObjectDisposedException">
        /// The <see cref="SmtpClient"/> has been disposed.
        /// </exception>
        /// <exception cref="System.InvalidOperationException">
        /// The <see cref="SmtpClient"/> is already connected.
        /// </exception>
        /// <exception cref="System.OperationCanceledException">
        /// The operation was canceled.
        /// </exception>
        /// <exception cref="System.IO.IOException">
        /// An I/O error occurred.
        /// </exception>
        /// <exception cref="SmtpCommandException">
        /// An SMTP command failed.
        /// </exception>
        /// <exception cref="SmtpProtocolException">
        /// An SMTP protocol error occurred.
        /// </exception>
        public void Connect(Uri uri, CancellationToken cancellationToken = default (CancellationToken))
        {
            CheckDisposed ();

            if (uri == null)
                throw new ArgumentNullException ("uri");

            if (!uri.IsAbsoluteUri)
                throw new ArgumentException ("The uri must be absolute.", "uri");

            if (IsConnected)
                throw new InvalidOperationException ("The SmtpClient is already connected.");

            capabilities = SmtpCapabilities.None;
            authmechs.Clear ();
            MaxSize = 0;

            var smtps = uri.Scheme.ToLowerInvariant () == "smtps";
            var port = uri.Port > 0 ? uri.Port : (smtps ? 465 : 25);
            var query = uri.ParsedQuery ();
            SmtpResponse response = null;
            string value;

            var starttls = !smtps && (!query.TryGetValue ("starttls", out value) || Convert.ToBoolean (value));

            #if !NETFX_CORE
            var ipAddresses = Dns.GetHostAddresses (uri.DnsSafeHost);
            Socket socket = null;

            for (int i = 0; i < ipAddresses.Length; i++) {
                socket = new Socket (ipAddresses[i].AddressFamily, SocketType.Stream, ProtocolType.Tcp);

                cancellationToken.ThrowIfCancellationRequested ();

                try {
                    socket.Connect (ipAddresses[i], port);
                    localEndPoint = socket.LocalEndPoint;
                    break;
                } catch {
                    if (i + 1 == ipAddresses.Length)
                        throw;
                }
            }

            if (smtps) {
                var ssl = new SslStream (new NetworkStream (socket, true), false, ValidateRemoteCertificate);
                ssl.AuthenticateAsClient (uri.Host, ClientCertificates, SslProtocols.Default, true);
                stream = ssl;
            } else {
                stream = new NetworkStream (socket, true);
            }
            #else
            socket = new StreamSocket ();

            cancellationToken.ThrowIfCancellationRequested ();
            socket.ConnectAsync (new HostName (uri.DnsSafeHost), port.ToString (), smtps ? SocketProtectionLevel.Tls12 : SocketProtectionLevel.PlainSocket)
                .AsTask (cancellationToken)
                .GetAwaiter ()
                .GetResult ();

            stream = new DuplexStream (socket.InputStream.AsStreamForRead (), socket.OutputStream.AsStreamForWrite ());
            #endif

            if (stream.CanTimeout) {
                stream.WriteTimeout = timeout;
                stream.ReadTimeout = timeout;
            }
            host = uri.Host;

            logger.LogConnect (uri);

            try {
                // read the greeting
                response = ReadResponse (cancellationToken);

                if (response.StatusCode != SmtpStatusCode.ServiceReady)
                    throw new SmtpCommandException (SmtpErrorCode.UnexpectedStatusCode, response.StatusCode, response.Response);

                // Send EHLO and get a list of supported extensions
                Ehlo (cancellationToken);

                if (starttls && (capabilities & SmtpCapabilities.StartTLS) != 0) {
                    response = SendCommand ("STARTTLS", cancellationToken);
                    if (response.StatusCode != SmtpStatusCode.ServiceReady)
                        throw new SmtpCommandException (SmtpErrorCode.UnexpectedStatusCode, response.StatusCode, response.Response);

            #if !NETFX_CORE
                    var tls = new SslStream (stream, false, ValidateRemoteCertificate);
                    tls.AuthenticateAsClient (uri.Host, ClientCertificates, SslProtocols.Tls, true);
                    stream = tls;
            #else
                    socket.UpgradeToSslAsync (SocketProtectionLevel.Tls12, new HostName (uri.DnsSafeHost))
                        .AsTask (cancellationToken)
                        .GetAwaiter ()
                        .GetResult ();
            #endif

                    // Send EHLO again and get the new list of supported extensions
                    Ehlo (cancellationToken);
                }

                IsConnected = true;
            } catch {
                stream.Dispose ();
                stream = null;
                throw;
            }
        }
Exemple #3
0
		/// <summary>
		/// Establishes a connection to the specified SMTP or SMTP/S server.
		/// </summary>
		/// <remarks>
		/// <para>Establishes a connection to the specified SMTP or SMTP/S server.</para>
		/// <para>If the <paramref name="port"/> has a value of <c>0</c>, then the
		/// <paramref name="options"/> parameter is used to determine the default port to
		/// connect to. The default port used with <see cref="SecureSocketOptions.SslOnConnect"/>
		/// is <c>465</c>. All other values will use a default port of <c>25</c>.</para>
		/// <para>If the <paramref name="options"/> has a value of
		/// <see cref="SecureSocketOptions.Auto"/>, then the <paramref name="port"/> is used
		/// to determine the default security options. If the <paramref name="port"/> has a value
		/// of <c>465</c>, then the default options used will be
		/// <see cref="SecureSocketOptions.SslOnConnect"/>. All other values will use
		/// <see cref="SecureSocketOptions.StartTlsWhenAvailable"/>.</para>
		/// <para>Once a connection is established, properties such as
		/// <see cref="AuthenticationMechanisms"/> and <see cref="Capabilities"/> will be
		/// populated.</para>
		/// <para>Note: The connection established by any of the
		/// <a href="Overload_MailKit_Net_Smtp_SmtpClient_Connect.htm">Connect</a>
		/// methods may be re-used if an application wishes to send multiple messages
		/// to the same SMTP server. Since connecting and authenticating can be expensive
		/// operations, re-using a connection can significantly improve performance when
		/// sending a large number of messages to the same SMTP server over a short
		/// period of time.</para>
		/// </remarks>
		/// <example>
		/// <code language="c#" source="Examples\SmtpExamples.cs" region="SendMessage"/>
		/// </example>
		/// <param name="host">The host name to connect to.</param>
		/// <param name="port">The port to connect to. If the specified port is <c>0</c>, then the default port will be used.</param>
		/// <param name="options">The secure socket options to when connecting.</param>
		/// <param name="cancellationToken">The cancellation token.</param>
		/// <exception cref="System.ArgumentNullException">
		/// <paramref name="host"/> is <c>null</c>.
		/// </exception>
		/// <exception cref="System.ArgumentOutOfRangeException">
		/// <paramref name="port"/> is not between <c>0</c> and <c>65535</c>.
		/// </exception>
		/// <exception cref="System.ArgumentException">
		/// The <paramref name="host"/> is a zero-length string.
		/// </exception>
		/// <exception cref="System.ObjectDisposedException">
		/// The <see cref="SmtpClient"/> has been disposed.
		/// </exception>
		/// <exception cref="System.InvalidOperationException">
		/// The <see cref="SmtpClient"/> is already connected.
		/// </exception>
		/// <exception cref="System.NotSupportedException">
		/// <paramref name="options"/> was set to
		/// <see cref="MailKit.Security.SecureSocketOptions.StartTls"/>
		/// and the SMTP server does not support the STARTTLS extension.
		/// </exception>
		/// <exception cref="System.OperationCanceledException">
		/// The operation was canceled.
		/// </exception>
		/// <exception cref="System.IO.IOException">
		/// An I/O error occurred.
		/// </exception>
		/// <exception cref="SmtpCommandException">
		/// An SMTP command failed.
		/// </exception>
		/// <exception cref="SmtpProtocolException">
		/// An SMTP protocol error occurred.
		/// </exception>
		public override void Connect (string host, int port = 0, SecureSocketOptions options = SecureSocketOptions.Auto, CancellationToken cancellationToken = default (CancellationToken))
		{
			if (host == null)
				throw new ArgumentNullException ("host");

			if (host.Length == 0)
				throw new ArgumentException ("The host name cannot be empty.", "host");

			if (port < 0 || port > 65535)
				throw new ArgumentOutOfRangeException ("port");
			
			CheckDisposed ();

			if (IsConnected)
				throw new InvalidOperationException ("The SmtpClient is already connected.");

			capabilities = SmtpCapabilities.None;
			AuthenticationMechanisms.Clear ();
			MaxSize = 0;

			SmtpResponse response;
			Stream stream;
			bool starttls;
			Uri uri;

			ComputeDefaultValues (host, ref port, ref options, out uri, out starttls);

#if !NETFX_CORE
			var ipAddresses = Dns.GetHostAddresses (host);
			Socket socket = null;

			for (int i = 0; i < ipAddresses.Length; i++) {
				socket = new Socket (ipAddresses[i].AddressFamily, SocketType.Stream, ProtocolType.Tcp);

				try {
					cancellationToken.ThrowIfCancellationRequested ();

					if (LocalEndPoint != null)
						socket.Bind (LocalEndPoint);

					socket.Connect (ipAddresses[i], port);
					break;
				} catch (OperationCanceledException) {
					socket.Dispose ();
					socket = null;
					throw;
				} catch {
					socket.Dispose ();
					socket = null;

					if (i + 1 == ipAddresses.Length)
						throw;
				}
			}

			if (socket == null)
				throw new IOException (string.Format ("Failed to resolve host: {0}", host));

			this.host = host;

			if (options == SecureSocketOptions.SslOnConnect) {
				var ssl = new SslStream (new NetworkStream (socket, true), false, ValidateRemoteCertificate);
				ssl.AuthenticateAsClient (host, ClientCertificates, SslProtocols, true);
				stream = ssl;
			} else {
				stream = new NetworkStream (socket, true);
			}
#else
			var protection = options == SecureSocketOptions.SslOnConnect ? SocketProtectionLevel.Tls12 : SocketProtectionLevel.PlainSocket;
			var socket = new StreamSocket ();

			try {
				cancellationToken.ThrowIfCancellationRequested ();
				socket.ConnectAsync (new HostName (host), port.ToString (), protection)
					.AsTask (cancellationToken)
					.GetAwaiter ()
					.GetResult ();
			} catch {
				socket.Dispose ();
				throw;
			}

			stream = new DuplexStream (socket.InputStream.AsStreamForRead (0), socket.OutputStream.AsStreamForWrite (0));
#endif

			if (stream.CanTimeout) {
				stream.WriteTimeout = timeout;
				stream.ReadTimeout = timeout;
			}

			ProtocolLogger.LogConnect (uri);

			Stream = new SmtpStream (stream, socket, ProtocolLogger);

			try {
				// read the greeting
				response = Stream.ReadResponse (cancellationToken);

				if (response.StatusCode != SmtpStatusCode.ServiceReady)
					throw new SmtpCommandException (SmtpErrorCode.UnexpectedStatusCode, response.StatusCode, response.Response);

				// Send EHLO and get a list of supported extensions
				Ehlo (cancellationToken);

				if (options == SecureSocketOptions.StartTls && (capabilities & SmtpCapabilities.StartTLS) == 0)
					throw new NotSupportedException ("The SMTP server does not support the STARTTLS extension.");

				if (starttls && (capabilities & SmtpCapabilities.StartTLS) != 0) {
					response = SendCommand ("STARTTLS", cancellationToken);
					if (response.StatusCode != SmtpStatusCode.ServiceReady)
						throw new SmtpCommandException (SmtpErrorCode.UnexpectedStatusCode, response.StatusCode, response.Response);

#if !NETFX_CORE
					var tls = new SslStream (stream, false, ValidateRemoteCertificate);
					tls.AuthenticateAsClient (host, ClientCertificates, SslProtocols, true);
					Stream.Stream = tls;
#else
					socket.UpgradeToSslAsync (SocketProtectionLevel.Tls12, new HostName (host))
						.AsTask (cancellationToken)
						.GetAwaiter ()
						.GetResult ();
#endif

					// Send EHLO again and get the new list of supported extensions
					Ehlo (cancellationToken);
				}

				connected = true;
			} catch {
				Stream.Dispose ();
				Stream = null;
				throw;
			}

			OnConnected ();
		}
Exemple #4
0
		/// <summary>
		/// Establish a connection to the specified IMAP server.
		/// </summary>
		/// <remarks>
		/// <para>Establishes a connection to the specified IMAP or IMAP/S server.</para>
		/// <para>If the <paramref name="port"/> has a value of <c>0</c>, then the
		/// <paramref name="options"/> parameter is used to determine the default port to
		/// connect to. The default port used with <see cref="SecureSocketOptions.SslOnConnect"/>
		/// is <c>993</c>. All other values will use a default port of <c>143</c>.</para>
		/// <para>If the <paramref name="options"/> has a value of
		/// <see cref="SecureSocketOptions.Auto"/>, then the <paramref name="port"/> is used
		/// to determine the default security options. If the <paramref name="port"/> has a value
		/// of <c>993</c>, then the default options used will be
		/// <see cref="SecureSocketOptions.SslOnConnect"/>. All other values will use
		/// <see cref="SecureSocketOptions.StartTlsWhenAvailable"/>.</para>
		/// <para>Once a connection is established, properties such as
		/// <see cref="AuthenticationMechanisms"/> and <see cref="Capabilities"/> will be
		/// populated.</para>
		/// </remarks>
		/// <example>
		/// <code language="c#" source="Examples\ImapExamples.cs" region="DownloadMessages"/>
		/// </example>
		/// <param name="host">The host name to connect to.</param>
		/// <param name="port">The port to connect to. If the specified port is <c>0</c>, then the default port will be used.</param>
		/// <param name="options">The secure socket options to when connecting.</param>
		/// <param name="cancellationToken">The cancellation token.</param>
		/// <exception cref="System.ArgumentNullException">
		/// <paramref name="host"/> is <c>null</c>.
		/// </exception>
		/// <exception cref="System.ArgumentOutOfRangeException">
		/// <paramref name="port"/> is not between <c>0</c> and <c>65535</c>.
		/// </exception>
		/// <exception cref="System.ArgumentException">
		/// The <paramref name="host"/> is a zero-length string.
		/// </exception>
		/// <exception cref="System.ObjectDisposedException">
		/// The <see cref="ImapClient"/> has been disposed.
		/// </exception>
		/// <exception cref="System.InvalidOperationException">
		/// The <see cref="ImapClient"/> is already connected.
		/// </exception>
		/// <exception cref="System.NotSupportedException">
		/// <paramref name="options"/> was set to
		/// <see cref="MailKit.Security.SecureSocketOptions.StartTls"/>
		/// and the IMAP server does not support the STARTTLS extension.
		/// </exception>
		/// <exception cref="System.OperationCanceledException">
		/// The operation was canceled via the cancellation token.
		/// </exception>
		/// <exception cref="System.IO.IOException">
		/// An I/O error occurred.
		/// </exception>
		/// <exception cref="ImapProtocolException">
		/// An IMAP protocol error occurred.
		/// </exception>
		public override void Connect (string host, int port = 0, SecureSocketOptions options = SecureSocketOptions.Auto, CancellationToken cancellationToken = default (CancellationToken))
		{
			if (host == null)
				throw new ArgumentNullException ("host");

			if (host.Length == 0)
				throw new ArgumentException ("The host name cannot be empty.", "host");

			if (port < 0 || port > 65535)
				throw new ArgumentOutOfRangeException ("port");

			CheckDisposed ();

			if (IsConnected)
				throw new InvalidOperationException ("The ImapClient is already connected.");

			Stream stream;
			bool starttls;
			Uri uri;

			ComputeDefaultValues (host, ref port, ref options, out uri, out starttls);

#if !NETFX_CORE
			var ipAddresses = Dns.GetHostAddresses (host);
			Socket socket = null;

			for (int i = 0; i < ipAddresses.Length; i++) {
				socket = new Socket (ipAddresses[i].AddressFamily, SocketType.Stream, ProtocolType.Tcp);

				try {
					cancellationToken.ThrowIfCancellationRequested ();
					socket.Connect (ipAddresses[i], port);
					break;
				} catch (OperationCanceledException) {
					socket.Dispose ();
					throw;
				} catch {
					socket.Dispose ();

					if (i + 1 == ipAddresses.Length)
						throw;
				}
			}

			if (socket == null)
				throw new IOException (string.Format ("Failed to resolve host: {0}", host));
			
			engine.Uri = uri;

			if (options == SecureSocketOptions.SslOnConnect) {
				var ssl = new SslStream (new NetworkStream (socket, true), false, ValidateRemoteCertificate);
				ssl.AuthenticateAsClient (host, ClientCertificates, DefaultSslProtocols, true);
				stream = ssl;
			} else {
				stream = new NetworkStream (socket, true);
			}
#else
			var protection = options == SecureSocketOptions.SslOnConnect ? SocketProtectionLevel.Tls12 : SocketProtectionLevel.PlainSocket;
			socket = new StreamSocket ();

			try {
				cancellationToken.ThrowIfCancellationRequested ();
				socket.ConnectAsync (new HostName (host), port.ToString (), protection)
					.AsTask (cancellationToken)
					.GetAwaiter ()
					.GetResult ();
			} catch {
				socket.Dispose ();
				socket = null;
				throw;
			}

			stream = new DuplexStream (socket.InputStream.AsStreamForRead (0), socket.OutputStream.AsStreamForWrite (0));
			engine.Uri = uri;
#endif

			if (stream.CanTimeout) {
				stream.WriteTimeout = timeout;
				stream.ReadTimeout = timeout;
			}

			ProtocolLogger.LogConnect (uri);

			engine.Connect (new ImapStream (stream, socket, ProtocolLogger), cancellationToken);

			try {
				// Only query the CAPABILITIES if the greeting didn't include them.
				if (engine.CapabilitiesVersion == 0)
					engine.QueryCapabilities (cancellationToken);
				
				if (options == SecureSocketOptions.StartTls && (engine.Capabilities & ImapCapabilities.StartTLS) == 0)
					throw new NotSupportedException ("The IMAP server does not support the STARTTLS extension.");
				
				if (starttls && (engine.Capabilities & ImapCapabilities.StartTLS) != 0) {
					var ic = engine.QueueCommand (cancellationToken, null, "STARTTLS\r\n");

					engine.Wait (ic);

					if (ic.Response == ImapCommandResponse.Ok) {
#if !NETFX_CORE
						var tls = new SslStream (stream, false, ValidateRemoteCertificate);
						tls.AuthenticateAsClient (host, ClientCertificates, DefaultSslProtocols, true);
						engine.Stream.Stream = tls;
#else
						socket.UpgradeToSslAsync (SocketProtectionLevel.Tls12, new HostName (host))
							.AsTask (cancellationToken)
							.GetAwaiter ()
							.GetResult ();
#endif

						// Query the CAPABILITIES again if the server did not include an
						// untagged CAPABILITIES response to the STARTTLS command.
						if (engine.CapabilitiesVersion == 1)
							engine.QueryCapabilities (cancellationToken);
					} else if (options == SecureSocketOptions.StartTls) {
						throw ImapCommandException.Create ("STARTTLS", ic);
					}
				}
			} catch {
				engine.Disconnect ();
				throw;
			}

			engine.Disconnected += OnEngineDisconnected;
			OnConnected ();
		}
Exemple #5
0
        /// <summary>
        /// Establishes a connection to the specified IMAP server.
        /// </summary>
        /// <remarks>
        /// <para>Establishes a connection to an IMAP or IMAP/S server. If the schema
        /// in the uri is "imap", a clear-text connection is made and defaults to using
        /// port 143 if no port is specified in the URI. However, if the schema in the
        /// uri is "imaps", an SSL connection is made using the
        /// <see cref="ClientCertificates"/> and defaults to port 993 unless a port
        /// is specified in the URI.</para>
        /// <para>It should be noted that when using a clear-text IMAP connection,
        /// if the server advertizes support for the STARTTLS extension, the client
        /// will automatically switch into TLS mode before authenticating unless the
        /// <paramref name="uri"/> contains a query string to disable it.</para>
        /// <para>If the IMAP server advertizes the COMPRESS extension and either does not
        /// support the STARTTLS extension or the <paramref name="uri"/> explicitly disabled
        /// the use of the STARTTLS extension, then the client will automatically opt into
        /// using a compressed data connection to optimize bandwidth usage unless the
        /// <paramref name="uri"/> contains a query string to explicitly disable it.</para>
        /// <para>If a successful connection is made, the <see cref="AuthenticationMechanisms"/>
        /// and <see cref="Capabilities"/> properties will be populated.</para>
        /// </remarks>
        /// <param name="uri">The server URI. The <see cref="System.Uri.Scheme"/> should either
        /// be "imap" to make a clear-text connection or "imaps" to make an SSL connection.</param>
        /// <param name="cancellationToken">A cancellation token.</param>
        /// <exception cref="System.ArgumentNullException">
        /// The <paramref name="uri"/> is <c>null</c>.
        /// </exception>
        /// <exception cref="System.ArgumentException">
        /// The <paramref name="uri"/> is not an absolute URI.
        /// </exception>
        /// <exception cref="System.ObjectDisposedException">
        /// The <see cref="ImapClient"/> has been disposed.
        /// </exception>
        /// <exception cref="System.InvalidOperationException">
        /// The <see cref="ImapClient"/> is already connected.
        /// </exception>
        /// <exception cref="System.OperationCanceledException">
        /// The operation was canceled via the cancellation token.
        /// </exception>
        /// <exception cref="System.IO.IOException">
        /// An I/O error occurred.
        /// </exception>
        /// <exception cref="ImapProtocolException">
        /// An IMAP protocol error occurred.
        /// </exception>
        public void Connect(Uri uri, CancellationToken cancellationToken)
        {
            CheckDisposed ();

            if (uri == null)
                throw new ArgumentNullException ("uri");

            if (!uri.IsAbsoluteUri)
                throw new ArgumentException ("The uri must be absolute.", "uri");

            if (IsConnected)
                throw new InvalidOperationException ("The ImapClient is already connected.");

            var imaps = uri.Scheme.ToLowerInvariant () == "imaps";
            var port = uri.Port > 0 ? uri.Port : (imaps ? 993 : 143);
            var query = uri.ParsedQuery ();
            Stream stream;
            string value;

            var starttls = !imaps && (!query.TryGetValue ("starttls", out value) || Convert.ToBoolean (value));
            var compress = !imaps && (!query.TryGetValue ("compress", out value) || Convert.ToBoolean (value));

            #if !NETFX_CORE
            var ipAddresses = Dns.GetHostAddresses (uri.DnsSafeHost);
            Socket socket = null;

            for (int i = 0; i < ipAddresses.Length; i++) {
                socket = new Socket (ipAddresses[i].AddressFamily, SocketType.Stream, ProtocolType.Tcp);

                cancellationToken.ThrowIfCancellationRequested ();

                try {
                    socket.Connect (ipAddresses[i], port);
                    break;
                } catch {
                    if (i + 1 == ipAddresses.Length)
                        throw;
                }
            }

            if (imaps) {
                var ssl = new SslStream (new NetworkStream (socket, true), false, ValidateRemoteCertificate);
                ssl.AuthenticateAsClient (uri.Host, ClientCertificates, SslProtocols.Default, true);
                stream = ssl;
            } else {
                stream = new NetworkStream (socket, true);
            }
            #else
            socket = new StreamSocket ();

            cancellationToken.ThrowIfCancellationRequested ();
            socket.ConnectAsync (new HostName (uri.DnsSafeHost), port.ToString (), imaps ? SocketProtectionLevel.Tls12 : SocketProtectionLevel.PlainSocket)
                .AsTask (cancellationToken)
                .GetAwaiter ()
                .GetResult ();

            stream = new DuplexStream (socket.InputStream.AsStreamForRead (), socket.OutputStream.AsStreamForWrite ());
            #endif
            host = uri.Host;

            logger.LogConnect (uri);

            engine.Connect (new ImapStream (stream, logger), cancellationToken);

            // Only query the CAPABILITIES if the greeting didn't include them.
            if (engine.CapabilitiesVersion == 0)
                engine.QueryCapabilities (cancellationToken);

            if (starttls && (engine.Capabilities & ImapCapabilities.StartTLS) != 0) {
                var ic = engine.QueueCommand (cancellationToken, null, "STARTTLS\r\n");

                engine.Wait (ic);

                if (ic.Result == ImapCommandResult.Ok) {
            #if !NETFX_CORE
                    var tls = new SslStream (stream, false, ValidateRemoteCertificate);
                    tls.AuthenticateAsClient (uri.Host, ClientCertificates, SslProtocols.Tls, true);
                    engine.Stream.Stream = tls;
            #else
                    socket.UpgradeToSslAsync (SocketProtectionLevel.Tls12, new HostName (uri.DnsSafeHost))
                        .AsTask (cancellationToken)
                        .GetAwaiter ()
                        .GetResult ();
            #endif

                    // Query the CAPABILITIES again if the server did not include an
                    // untagged CAPABILITIES response to the STARTTLS command.
                    if (engine.CapabilitiesVersion == 1)
                        engine.QueryCapabilities (cancellationToken);
                }
            } else if (compress && (engine.Capabilities & ImapCapabilities.Compress) != 0) {
                var ic = engine.QueueCommand (cancellationToken, null, "COMPRESS DEFLATE\r\n");

                engine.Wait (ic);

                if (ic.Result == ImapCommandResult.Ok) {
                    var unzip = new DeflateStream (stream, CompressionMode.Decompress);
                    var zip = new DeflateStream (stream, CompressionMode.Compress);

                    engine.Stream.Stream = new DuplexStream (unzip, zip);

                    // Query the CAPABILITIES again if the server did not include an
                    // untagged CAPABILITIES response to the COMPRESS command.
                    if (engine.CapabilitiesVersion == 1)
                        engine.QueryCapabilities (cancellationToken);
                }
            }
        }
Exemple #6
0
        public IAsyncOperation<string> Connect() {
            var window = CoreWindow.GetForCurrentThread();

            return Task.Run<string>(async () =>
            {
                try
                {
                    var socket = this.socket = new Windows.Networking.Sockets.StreamSocket();

                    await socket.ConnectAsync(new HostName("talk.google.com"), "5222", SocketProtectionLevel.PlainSocket);

                    await log(window, "connected!");

                    reader = new DataReader(socket.InputStream);
                    writer = new DataWriter(socket.OutputStream);

                    reader.InputStreamOptions = InputStreamOptions.Partial;

                    Write("<?xml version='1.0'?>\n<stream:stream to='" + server + "' xmlns='jabber:client' xmlns:stream='http://etherx.jabber.org/streams' version='1.0'>");

                    xmlStream = new XmlStream();
                    bool shouldRead = true;

                    xmlStream.SetCallback(async (promptRead, data) => {
                        await log(window, "data " + data);

                        if (promptRead)
                        {
                            if (shouldRead)
                            {
                                await log(window, "prompt read");

                                await reader.LoadAsync(4096);
                                var buffer = new byte[reader.UnconsumedBufferLength];
                                reader.ReadBytes(buffer);
                                await log(window, "in " + Encoding.UTF8.GetString(buffer, 0, buffer.Length));
                                xmlStream.Update(buffer, 0, buffer.Length);
                            }
                            else
                            {
                                await log(window, "read blocked");
                            }
                        }
                        else if (data.IndexOf("stream:features") != -1)
                        {
                            Write("<starttls xmlns='urn:ietf:params:xml:ns:xmpp-tls' />");
                        }
                        else if (data.IndexOf("proceed") != -1)
                        {
                            await log(window, "SSL Strength: " + socket.Information.ProtectionLevel);

                            writer.DetachStream();
                            reader.DetachStream();

                            shouldRead = false;

                            if (server == "gmail.com")
                            {
                                await socket.UpgradeToSslAsync(SocketProtectionLevel.Ssl, new Windows.Networking.HostName("gmail.com"));
                            }
                            else
                            {
                                await socket.UpgradeToSslAsync(SocketProtectionLevel.Ssl, new Windows.Networking.HostName("talk.google.com"));
                            }

                            writer = new DataWriter(socket.OutputStream);
                            reader = new DataReader(socket.InputStream);

                            reader.InputStreamOptions = InputStreamOptions.Partial;

                            await log(window, "upgraded!");
                            await log(window, "SSL Strength: " + socket.Information.ProtectionLevel);

                            Write("<?xml version='1.0'?>\n<stream:stream to='" + server + "' xmlns='jabber:client' xmlns:stream='http://etherx.jabber.org/streams' version='1.0'>");

                            xmlStream.SetCallback(async (shouldRead2, data2) =>
                            {
                                await log(window, "data " + data2);

                                if (shouldRead2)
                                {
                                    await reader.LoadAsync(4096);
                                    var buffer = new byte[reader.UnconsumedBufferLength];
                                    reader.ReadBytes(buffer);
                                    await log(window, "in " + Encoding.UTF8.GetString(buffer, 0, buffer.Length));
                                    xmlStream.Update(buffer, 0, buffer.Length);
                                }
                                else if (data2.Contains("X-GOOGLE-TOKEN"))
                                {
                                    var token = Convert.ToBase64String(Encoding.UTF8.GetBytes('\x00' + this.username + '\x00' + this.auth));
                                    Write("<auth xmlns='urn:ietf:params:xml:ns:xmpp-sasl' mechanism='X-GOOGLE-TOKEN'>" + token + "</auth>");
                                }
                                else if (data2.Contains("failure"))
                                {
                                    if (Disconnect != null) Disconnect(this, "auth failure");
                                }
                                else if (data2.Contains("success"))
                                {
                                    var messageEvent = Message;

                                    xmlStream.SetCallback(async (shouldRead3, data3) =>
                                    {
                                        if (shouldRead3)
                                        {
                                            await reader.LoadAsync(4096);
                                            var buffer = new byte[reader.UnconsumedBufferLength];
                                            reader.ReadBytes(buffer);
                                            await log(window, "in " + Encoding.UTF8.GetString(buffer, 0, buffer.Length));
                                            xmlStream.Update(buffer, 0, buffer.Length);
                                        }
                                        else if (data3 == "</stream:stream>")
                                        {
                                            await disconnect(window, "end of stream");
                                        }
                                        else if (!data3.StartsWith("<stream:stream"))
                                        {
                                            await message(window, data3);
                                        }
                                    });

                                    Write("<?xml version='1.0'?>\n<stream:stream to='" + server + "' xmlns='jabber:client' xmlns:stream='http://etherx.jabber.org/streams' version='1.0'>");
                                }
                                else if (!data2.StartsWith("<stream:stream"))
                                {
                                    await log(window, "Ummm not sure what to do with '" + data2 + "'. flee.");
                                    if (Disconnect != null) Disconnect(this, "protocol error");
                                }
                            });
                        }
                        else if (!data.StartsWith("<stream:stream"))
                        {
                            await log(window, "Ummm not sure what to do with '" + data + "'. flee.");
                            if (Disconnect != null) Disconnect(this, "protocol error");
                        }
                    });

                    return "ok";
                }
                catch (Exception e)
                {
                    return e.ToString();
                }
            }).AsAsyncOperation<string>();
        }
Exemple #7
0
        //------------------------------------------------------------------------------------------------------------------------
        SimpleActionResult _sockConnection(string RemoteHost, int RemotePort)
        {
            //declares
            bool isSecured = false;
            string sslProtocol = "";
            var result = new SimpleActionResult()
            {
                IsSuccessful = false,
                Message = "",
            };

            lock (this)
            {
                //create socket
#if NETFX
                _sock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
#elif UNIVERSAL
                _sock = new StreamSocket();
#endif

                //Try to connect
                try
                {
                    //attemp connection
#if NETFX
                    _sock.Connect(RemoteHost, RemotePort);
#elif UNIVERSAL
                    SocketSetup?.Invoke(this, _sock);
                    _sock.Control.KeepAlive = true;
                    _sock.ConnectAsync(new Windows.Networking.HostName(RemoteHost), RemotePort.ToStringInvariant(), SocketProtectionLevel.PlainSocket).AsTask().Wait();
#endif
                }
                catch (Exception ex)
                {
                    DebugEx.TraceError(ex, "Connection Error");
                    result.Message = ex.Message;
                    try { Close("Connection Error"); } catch { }
                    return result;
                }


                //create network stream
#if NETFX
                //Stream _netstream = new BufferedStream(new NetworkStream(_sock, true));
                Stream _netstream = new NetworkStream(_sock, true);
#endif

                //Wrap with a secure stream?
                if (Secure)
                {
#if NETFX
                    //create ssl stream
                    var sslstream = new SslStream(_netstream, false);
#endif
                    //decide on certifacte server name
                    var remCertHostName = !string.IsNullOrWhiteSpace(CertificateServerName) ? CertificateServerName : RemoteHost;

                    try
                    {
#if NETFX            
                        //collect certificates
                        var certs = Yodiwo.Tools.Certificates.CollectCertificates();
                        if (CustomCertificates != null)
                            foreach (var c in CustomCertificates)
                                certs.Add(c);

                        //try authenticate
                        sslstream.AuthenticateAsClient(remCertHostName,
                                                        certs,
                                                        System.Security.Authentication.SslProtocols.Tls | System.Security.Authentication.SslProtocols.Tls11 | System.Security.Authentication.SslProtocols.Tls12,
                                                        true
                                                        );


                        //checks
                        if (!sslstream.IsAuthenticated)
                        {
                            DebugEx.Assert("Not authenticated");
                            throw new Exception("Not authenticated");
                        }
                        if (!sslstream.IsEncrypted)
                        {
                            DebugEx.Assert("No encryption");
                            throw new Exception("Not encryption");
                        }

                        //get info
                        isSecured = true;
                        sslProtocol = sslstream.SslProtocol.ToStringInvariant();

                        //use this stream from now on
                        _netstream = sslstream;
#elif UNIVERSAL
                        _sock.UpgradeToSslAsync(SocketProtectionLevel.Tls12, new Windows.Networking.HostName(remCertHostName)).AsTask().Wait();
                        var _isSecured = _sock.Information.ProtectionLevel == SocketProtectionLevel.Tls10 ||
                                     _sock.Information.ProtectionLevel == SocketProtectionLevel.Tls11 ||
                                     _sock.Information.ProtectionLevel == SocketProtectionLevel.Tls12;
                        if (!_isSecured)
                            throw new Exception("Connection not secured (" + _sock.Information.ProtectionLevel + ")");
#endif
                    }
                    catch (Exception ex)
                    {
                        DebugEx.TraceError(ex, "Certificate not accepted, " + ex.Message);
                        result.Message = "Certificate not accepted, " + ex.Message;
                        if (ex.InnerException != null)
                            result.Message += "  (inner msg=" + ex.InnerException.Message + ")";
                        try { Close("Certificate not accepted, " + ex.Message); } catch { }
#if NETFX
                        try { _netstream?.Close(); _netstream?.Dispose(); } catch { }
                        try { sslstream?.Close(); sslstream?.Dispose(); } catch { }
                        try { _sock?.Close(); } catch { }
#endif
                        try { _sock?.Dispose(); } catch { }
                        return result;
                    }
                }


                //write packers
#if NETFX
                var _nodelay = _sock.NoDelay;
                _sock.NoDelay = true; //Disable the Nagle Algorithm
                _netstream.WriteByte((byte)this.SupportedChannelSerializationModes);
                _netstream.WriteByte((byte)this.PreferredChannelSerializationModes);
                _sock.NoDelay = _nodelay; //Restore (default:enable) the Nagle Algorithm
#elif UNIVERSAL
                {
                    var wStream = _sock.OutputStream.AsStreamForWrite();
                    wStream.WriteByte((byte)this.SupportedChannelSerializationModes);
                    wStream.WriteByte((byte)this.PreferredChannelSerializationModes);
                    wStream.Flush();
                }
#endif

                //read final packer
                var packerType = ChannelSerializationMode.Unkown;
#if NETFX
                packerType = (ChannelSerializationMode)_netstream.ReadByte();
#elif UNIVERSAL
                packerType = (ChannelSerializationMode)_sock.InputStream.AsStreamForRead().ReadByte();
#endif
                if (!this.SupportedChannelSerializationModes.HasFlag(packerType))
                {
                    DebugEx.Assert("Invalid ChannelSerializationMode. Server uses  " + packerType);
                    result.Message = "Invalid ChannelSerializationMode. Server uses  " + packerType;
                    try { Close("Invalid ChannelSerializationMode. Server uses  " + packerType); } catch { }
#if NETFX
                    try { _netstream?.Close(); _netstream?.Dispose(); } catch { }
                    try { _sock?.Close(); } catch { }
#endif
                    try { _sock?.Dispose(); } catch { }
                    return result;
                }
                //select serialization mode
                _ChannelSerializationMode = packerType;

                //setup info
                try
                {
#if NETFX
                    this.LocalHost = _sock.LocalEndPoint.GetIPAddress().ToString();
                    this.RemotePort = _sock.LocalEndPoint.GetPort().ToStringInvariant();
#elif UNIVERSAL
                    this.LocalHost = _sock.Information.LocalAddress.ToString();
                    this.RemotePort = _sock.Information.LocalPort;
#endif
                }
                catch { }

                //setup info
                this.RemoteHost = RemoteHost;
                this.RemotePort = RemotePort.ToStringInvariant();

                //log
                DebugEx.TraceLog("YPClient (socks) new connection to " + RemoteHost + ":" + RemotePort + " (Secure=" + isSecured + ",SSL=" + sslProtocol + ")");

                //create stream
#if NETFX
                SetupStream(_netstream);
#elif UNIVERSAL
                SetupStream();
#endif
                result.IsSuccessful = true;
                return result;
            }
        }