Click here to Skip to main content
15,884,472 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.7K   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.Net;
using System.Net.Sockets;
using System.Security.Cryptography.X509Certificates;
using VSSProtocol.States;

namespace VSSProtocol.Users
{
	/// <summary>
	/// Listens for connections from VSSP network clients. 
	/// </summary>
	public class VsspServer
	{
		/// <summary>
		/// Listen on this port.
		/// </summary>
		private int port;
		/// <summary>
		/// The underline tcp server.
		/// </summary>
		private readonly TcpListener server;
		/// <summary>
		/// Certificate that server uses.
		/// </summary>
		private readonly X509Certificate2 serversCertificate;
		/// <summary>
		/// Initializes a new instance of the VsspServer class that listens on the specified port.
		/// </summary>
		/// <param name="port">Listen on this port.</param>
		/// <param name="serversCertificate">Certificate that server uses.</param>
		public VsspServer(int port, X509Certificate2 serversCertificate)
		{
			if (serversCertificate == null) throw new ArgumentNullException("serversCertificate");
			this.port = port;
			this.serversCertificate = serversCertificate;
			// Local host.
			IPAddress localAddr = IPAddress.Parse("127.0.0.1");
			server = new TcpListener(localAddr, port);
			// Start listening for client requests.
			server.Start();
		}
		/// <summary>
		/// Accepts a pending connection request 
		/// </summary>
		/// <returns>A VsspClient used to send and receive data. </returns>
		/// <exception cref="SocketException">Use the SocketException.ErrorCode property to obtain the specific 
		/// error code. When you have obtained this code, you can refer to the Windows Sockets version 2 API 
		/// error code documentation in MSDN for a detailed description of the error.</exception>
		public VsspClient Accept()
		{
			// Accept connection.
			TcpClient client = server.AcceptTcpClient();
			
			VsspManager manager = new VsspManager(client.GetStream(), new HelloServerState(serversCertificate));
			// Start handshake.
			manager.StartHandshake();
			// Return new client.
			return new VsspClient(client, manager,"no", 100 );
		}


		///<summary>
		///Closes the server.
		///</summary>
		///
		///<exception cref="T:System.Net.Sockets.SocketException">Use the <see cref="P:System.Net.Sockets.SocketException.ErrorCode"></see> property to obtain the specific error code. When you have obtained this code, you can refer to the Windows�Sockets version 2 API error code documentation in MSDN for a detailed description of the error. </exception><PermissionSet><IPermission class="System.Security.Permissions.EnvironmentPermission, mscorlib, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Unrestricted="true" /><IPermission class="System.Security.Permissions.FileIOPermission, mscorlib, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Unrestricted="true" /><IPermission class="System.Security.Permissions.SecurityPermission, mscorlib, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Unrestricted="true" /></PermissionSet>
		public void Stop()
		{
			server.Stop();
		}
	}
}

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