Click here to Skip to main content
15,881,172 members
Articles / Programming Languages / C#

Creating a secure channel

Rate me:
Please Sign up or sign in to vote.
4.90/5 (33 votes)
24 May 2008CDDL13 min read 117.5K   2.9K   86  
The purpose of this article is to explain how a secure channel is built. The article will explain the structure of a Very Simple Secured Protocol (VSSP) that sits above the TCP/IP layer.
using System;
using System.IO;
using System.Net.Sockets;
using System.Security.Cryptography.X509Certificates;
using VSSProtocol.States;

namespace VSSProtocol.Users
{
	/// <summary>
	/// Provides client connections for vssp network services.
	/// </summary>
	public class VsspClient
	{
		#region Private variables
		/// <summary>
		/// Manages the connection on the VSSP side.
		/// </summary>
		private readonly VsspManager manager;
		/// <summary>
		/// Host To which we connected.
		/// </summary>
		private readonly String host;
		/// <summary>
		/// Port to which we connected.
		/// </summary>
		private readonly int port;
		/// <summary>
		/// The underlying tcp connection.
		/// </summary>
		private readonly TcpClient tcpClient;

		#endregion

		#region Constructors
		/// <summary>
		/// Initializes a new instance of the VsspClient class and connects to the specified port on the specified host. 
		/// </summary>
		/// <param name="host">The DNS name of the remote host to which you intend to connect.</param>
		/// <param name="port">The port number of the remote host to which you intend to connect.</param>
		/// <param name="checkCertificate">Checks whether the certificate received from server is trusted or not</param>
		public VsspClient(string host, int port, IsCertificateTrusted checkCertificate)
		{
			if (host == null) throw new ArgumentNullException("host");

			this.host = host;
			this.port = port;

			// Connect to it using TCP.
			tcpClient = new TcpClient(host, port);
			manager = new VsspManager(tcpClient.GetStream(), new HelloClientState(checkCertificate));
			manager.StartHandshake();
		}
		/// <summary>
		/// Initializes a new instance of the VsspClient class and assume that connection was already done.
		/// </summary>
		/// <param name="tcpClient">Underline TcpClient</param>
		/// <param name="manager">Manager to use.</param>
		/// <param name="host">Host used</param>
		/// <param name="port">Port used.</param>
		internal VsspClient(TcpClient tcpClient, VsspManager manager, String host, int port)
		{
			if (tcpClient == null) throw new ArgumentNullException("tcpClient");
			if (manager == null) throw new ArgumentNullException("manager");
			if (host == null) throw new ArgumentNullException("host");

			this.manager = manager;
			this.tcpClient = tcpClient;
			this.host = host;
			this.port = port;
		} 
		#endregion

		#region Public properties
		/// <summary>
		/// Port to which we connected.
		/// </summary>
		public int Port
		{
			get { return port; }
		}

		/// <summary>
		/// Host To which we connected.
		/// </summary>
		public string Host
		{
			get { return host; }
		}
		/// <summary>
		/// Is the client closed.
		/// </summary>
		public bool IsClosed
		{
			get { return manager.GetStream().ClosedConnection; }
		}

		#endregion

		#region Public methods
		/// <summary>
		/// Get the stream used for connection.
		/// </summary>
		public Stream GetStream()
		{
			return manager.GetStream();
		}
		/// <summary>
		/// Disconnect and dispose object.
		/// </summary>
		public void Close()
		{
			manager.GetStream().Close();
		} 
		#endregion
	}
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Common Development and Distribution License (CDDL)


Written By
Software Developer
Israel Israel
A computer science master student at Bar Ilan University under the supervision of Dr. Gal Kaminka.
Dealing mainly with trajectory mining.

Comments and Discussions