An output Stream for a TLS 1.0 connection.
Inheritance: Org.BouncyCastle.Utilities.IO.BaseOutputStream
Exemplo n.º 1
0
        /// <summary>Connects to the remote system.</summary>
        /// <param name="verifyer">Will be used when a certificate is received to verify
        /// that this certificate is accepted by the client.</param>
        /// <exception cref="IOException">If handshake was not successful</exception>
        public void Connect(
			ICertificateVerifyer verifyer)
        {
            this.verifyer = verifyer;

            /*
            * Send Client hello
            *
            * First, generate some random data.
            */
            this.clientRandom = new byte[32];

            /*
            * TLS 1.0 requires a unix-timestamp in the first 4 bytes
            */
            int t = (int)(DateTimeUtilities.CurrentUnixMs() / 1000L);
            this.clientRandom[0] = (byte)(t >> 24);
            this.clientRandom[1] = (byte)(t >> 16);
            this.clientRandom[2] = (byte)(t >> 8);
            this.clientRandom[3] = (byte)t;

            random.NextBytes(this.clientRandom, 4, 28);

            MemoryStream outStr = new MemoryStream();
            TlsUtilities.WriteVersion(outStr);
            outStr.Write(this.clientRandom, 0, this.clientRandom.Length);

            /*
            * Length of Session id
            */
            TlsUtilities.WriteUint8((short)0, outStr);

            /*
            * Cipher suites
            */
            TlsCipherSuiteManager.WriteCipherSuites(outStr);

            /*
            * Compression methods, just the null method.
            */
            byte[] compressionMethods = new byte[]{0x00};
            TlsUtilities.WriteUint8((short)compressionMethods.Length, outStr);
            outStr.Write(compressionMethods,0, compressionMethods.Length);

            MemoryStream bos = new MemoryStream();
            TlsUtilities.WriteUint8(HP_CLIENT_HELLO, bos);
            TlsUtilities.WriteUint24((int) outStr.Length, bos);
            byte[] outBytes = outStr.ToArray();
            bos.Write(outBytes, 0, outBytes.Length);
            byte[] message = bos.ToArray();
            rs.WriteMessage(RL_HANDSHAKE, message, 0, message.Length);
            connection_state = CS_CLIENT_HELLO_SEND;

            /*
            * We will now read data, until we have completed the handshake.
            */
            while (connection_state != CS_DONE)
            {
                rs.ReadData();
            }

            this.tlsInputStream = new TlsInputStream(this);
            this.tlsOutputStream = new TlsOuputStream(this);
        }
Exemplo n.º 2
0
        /// <summary>Connects to the remote system.</summary>
        /// <param name="verifyer">Will be used when a certificate is received to verify
        /// that this certificate is accepted by the client.</param>
        /// <exception cref="IOException">If handshake was not successful</exception>
        public virtual void Connect(
            ICertificateVerifyer verifyer)
        {
            this.verifyer = verifyer;

            /*
             * Send Client hello
             *
             * First, generate some random data.
             */
            this.clientRandom = new byte[32];

            /*
             * TLS 1.0 requires a unix-timestamp in the first 4 bytes
             */
            int t = (int)(DateTimeUtilities.CurrentUnixMs() / 1000L);

            this.clientRandom[0] = (byte)(t >> 24);
            this.clientRandom[1] = (byte)(t >> 16);
            this.clientRandom[2] = (byte)(t >> 8);
            this.clientRandom[3] = (byte)t;

            random.NextBytes(this.clientRandom, 4, 28);


            MemoryStream outStr = new MemoryStream();

            TlsUtilities.WriteVersion(outStr);
            outStr.Write(this.clientRandom, 0, this.clientRandom.Length);

            /*
             * Length of Session id
             */
            TlsUtilities.WriteUint8((short)0, outStr);

            /*
             * Cipher suites
             */
            TlsCipherSuiteManager.WriteCipherSuites(outStr);

            /*
             * Compression methods, just the null method.
             */
            byte[] compressionMethods = new byte[] { 0x00 };
            TlsUtilities.WriteUint8((short)compressionMethods.Length, outStr);
            outStr.Write(compressionMethods, 0, compressionMethods.Length);


            MemoryStream bos = new MemoryStream();

            TlsUtilities.WriteUint8(HP_CLIENT_HELLO, bos);
            TlsUtilities.WriteUint24((int)outStr.Length, bos);
            byte[] outBytes = outStr.ToArray();
            bos.Write(outBytes, 0, outBytes.Length);
            byte[] message = bos.ToArray();
            rs.WriteMessage(RL_HANDSHAKE, message, 0, message.Length);
            connection_state = CS_CLIENT_HELLO_SEND;

            /*
             * We will now read data, until we have completed the handshake.
             */
            while (connection_state != CS_DONE)
            {
                rs.ReadData();
            }

            this.tlsInputStream  = new TlsInputStream(this);
            this.tlsOutputStream = new TlsOuputStream(this);
        }
Exemplo n.º 3
0
		/// <summary>Connects to the remote system.</summary>
		/// <param name="verifyer">Will be used when a certificate is received to verify
		/// that this certificate is accepted by the client.</param>
		/// <exception cref="IOException">If handshake was not successful</exception>
		public virtual void Connect(
			ICertificateVerifyer verifyer)
		{
			this.verifyer = verifyer;

			/*
			* Send Client hello
			*
			* First, generate some random data.
			*/
			this.clientRandom = new byte[32];

			/*
			* TLS 1.0 requires a unix-timestamp in the first 4 bytes
			*/
			int t = (int)(DateTimeUtilities.CurrentUnixMs() / 1000L);
			this.clientRandom[0] = (byte)(t >> 24);
			this.clientRandom[1] = (byte)(t >> 16);
			this.clientRandom[2] = (byte)(t >> 8);
			this.clientRandom[3] = (byte)t;

			random.NextBytes(this.clientRandom, 4, 28);


			MemoryStream outStr = new MemoryStream();
			TlsUtilities.WriteVersion(outStr);
			outStr.Write(this.clientRandom, 0, this.clientRandom.Length);

			/*
			* Length of Session id
			*/
			TlsUtilities.WriteUint8((short)0, outStr);

			/*
			* Cipher suites
			*/
			TlsCipherSuiteManager.WriteCipherSuites(outStr);

			/*
			* Compression methods, just the null method.
			*/
			byte[] compressionMethods = new byte[]{0x00};
			TlsUtilities.WriteOpaque8(compressionMethods, outStr);

			/*
			* Extensions
			*/
			// TODO Collect extensions from client
			// Int32 -> byte[]
			Hashtable clientExtensions = new Hashtable();

			// TODO[SRP]
//			{
//				MemoryStream srpData = new MemoryStream();
//				TlsUtilities.WriteOpaque8(SRP_identity, srpData);
//
//				// TODO[SRP] RFC5054 2.8.1: ExtensionType.srp = 12
//				clientExtensions[12] = srpData.ToArray();
//			}

			this.extendedClientHello = (clientExtensions.Count > 0);

			if (extendedClientHello)
			{
				MemoryStream ext = new MemoryStream();

				foreach (int extType in clientExtensions.Keys)
				{
					byte[] extValue = (byte[])clientExtensions[extType];

					TlsUtilities.WriteUint16(extType, ext);
					TlsUtilities.WriteOpaque16(extValue, ext);
				}

				TlsUtilities.WriteOpaque16(ext.ToArray(), outStr);
			}

			MemoryStream bos = new MemoryStream();
			TlsUtilities.WriteUint8(HP_CLIENT_HELLO, bos);
			TlsUtilities.WriteUint24((int) outStr.Length, bos);
			byte[] outBytes = outStr.ToArray();
			bos.Write(outBytes, 0, outBytes.Length);
			byte[] message = bos.ToArray();
			rs.WriteMessage(RL_HANDSHAKE, message, 0, message.Length);
			connection_state = CS_CLIENT_HELLO_SEND;

			/*
			* We will now read data, until we have completed the handshake.
			*/
			while (connection_state != CS_DONE)
			{
				rs.ReadData();
			}

			this.tlsInputStream = new TlsInputStream(this);
			this.tlsOutputStream = new TlsOuputStream(this);
		}