Click here to Skip to main content
15,896,207 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 118.3K   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 VSSProtocol.DataLayer;

namespace VSSProtocol.Messages
{
	/// <summary>
	/// An helper class used for easy generatinon of hello client messages.
	/// The message includes the suit and a nonce number.
	/// The first byte is the suit and the rest is the nonce.
	/// Pay attention that 
	/// </summary>
	internal class HelloClientMessage : DataPacket
	{
		/// <summary>
		/// What is the size of nonce that is assumed.
		/// </summary>
		public static readonly int NONCE_SIZE_BYTES = 8;

		/// <summary>
		/// Chosen suit. 
		/// </summary>
		private readonly Suits.Suit chosenSuit;
		/// <summary>
		/// Nonce.
		/// </summary>
		private readonly byte[] nonce;

		#region Factory methods.
		/// <summary>
		/// Create new ClientHello message.
		/// </summary>
		/// <param name="chosenSuit">Chosen suit or suits.</param>
		/// <param name="nonce">Random value.</param>
		/// <returns>Message</returns>
		public static HelloClientMessage CreateMessage(Suits.Suit chosenSuit, byte[] nonce)
		{
			return new HelloClientMessage(chosenSuit, nonce);
		}

		/// <summary>
		/// Create new message from a packet.
		/// </summary>
		/// <param name="p">Packet to create from.</param>
		/// <returns>ClientHello message.</returns>
		/// <exception cref="InvalidDataException">Message is not of type ClientHello.</exception>
		public static HelloClientMessage CreateMessage(DataPacket p)
		{
			// Check that data packet is actually of type ClientHello.
			if (p.PacketType == PacketTypeEnum.ClientHello)
			{
				return new HelloClientMessage(p.Data);
			}
			else
			{
				throw new InvalidDataException("Message is not of type ClientHello.");
			}
		}
		#endregion

		#region Private constructors
		private HelloClientMessage(Suits.Suit chosenSuit, byte[] nonce)
			: base(PacketTypeEnum.ClientHello)
		{
			if (nonce == null)
			{
				throw new ArgumentNullException();
			}

			this.nonce = nonce;
			this.chosenSuit = chosenSuit;
			// Get encoded message.
			byte[] message = getMessage(nonce, chosenSuit);

			setData(message);
		}

		/// <summary>
		/// Gets bytes representation of nonce and suit.
		/// First byte is the suite and the rest is the nonce. Nonce size allowed is NONCE_SIZE_BYTES
		/// </summary>
		/// <param name="nonce">Random value.</param>
		/// <param name="chosenSuit">Suit</param>
		/// <returns>Data encoded as byte array.</returns>
		/// <exception cref="ArgumentOutOfRangeException">Nonce size allowed is NONCE_SIZE_BYTES</exception>
		internal static byte[] getMessage(byte[] nonce, Suits.Suit chosenSuit)
		{
			byte[] nonceByte = nonce;
			// Check that its NONCE_SIZE_BYTES bytes long.
			if (nonceByte.Length != NONCE_SIZE_BYTES)
			{
				throw new ArgumentOutOfRangeException("Nonce should be " + NONCE_SIZE_BYTES + " bytes long");
			}
			byte[] message = new byte[nonceByte.Length + 1]; // One for suit.

			// Prepare the byte array that I wish to send.
			message[0] = (byte)chosenSuit;
			Array.Copy(nonceByte, 0, message, 1, nonceByte.Length);
			return message;
		}

		/// <summary>
		/// Convert a raw message into something readable.
		/// </summary>
		/// <param name="message">Message to convert.</param>
		private HelloClientMessage(byte[] message)
			: base(PacketTypeEnum.ClientHello)
		{
			setData(message);
			// Get chosen suit.
			chosenSuit = (Suits.Suit)message[0];
			// Get nonce value.
			nonce = new byte[NONCE_SIZE_BYTES];
			Array.Copy(message, 1, nonce, 0, message.Length - 1);
		} 
		#endregion

		/// <summary>
		/// Chosen suit. 
		/// </summary>
		public Suits.Suit ChosenSuit
		{
			get { return chosenSuit; }
		}

		/// <summary>
		/// Nonce.
		/// </summary>
		public byte[] Nonce
		{
			get { return nonce; }
		}
	}
}

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