示例#1
0
        internal byte[] EncodeRecord(ContentType contentType, IBufferOffsetSize buffer)
        {
            CheckValid();
            var protocol = HasNegotiatedProtocol ? NegotiatedProtocol : Configuration.RequestedProtocol;

            var output = new TlsStream();

            EncodeRecord(protocol, contentType, Session != null ? Session.Write : null, buffer, output);
            output.Finish();

            var result = new byte [output.Size];

            Buffer.BlockCopy(output.Buffer, output.Offset, result, 0, output.Size);
            return(result);
        }
示例#2
0
		static void EncodeRecord_internal (TlsProtocolCode protocol, ContentType contentType, CryptoParameters crypto, IBufferOffsetSize buffer, TlsStream output,
			int fragmentSize = MAX_FRAGMENT_SIZE)
		{
			var maxExtraBytes = crypto != null ? crypto.MaxExtraEncryptedBytes : 0;

			var offset = buffer.Offset;
			var remaining = buffer.Size;

			#if !INSTRUMENTATION
			fragmentSize = MAX_FRAGMENT_SIZE;
			#endif

			do {
				BufferOffsetSize fragment;

				var encryptedSize = crypto != null ? crypto.GetEncryptedSize (remaining) : remaining;
				if (encryptedSize <= fragmentSize)
					fragment = new BufferOffsetSize (buffer.Buffer, offset, remaining);
				else {
					fragment = new BufferOffsetSize (buffer.Buffer, offset, fragmentSize - maxExtraBytes);
					encryptedSize = crypto != null ? crypto.GetEncryptedSize (fragment.Size) : fragment.Size;
				}

				// Write tls message
				output.Write ((byte)contentType);
				output.Write ((short)protocol);
				output.Write ((short)encryptedSize);

				if (crypto != null) {
					output.MakeRoom (encryptedSize);
					var ret = crypto.Encrypt (contentType, fragment, output.GetRemaining ());
					output.Position += ret;
				} else {
					output.Write (fragment.Buffer, fragment.Offset, fragment.Size);
				}

				offset += fragment.Size;
				remaining -= fragment.Size;
			} while (remaining > 0);
		}
示例#3
0
		public static void EncodeRecord (TlsProtocolCode protocol, ContentType contentType, CryptoParameters crypto, IBufferOffsetSize buffer, TlsStream output)
		{
			EncodeRecord_internal (protocol, contentType, crypto, buffer, output);
		}
示例#4
0
		internal byte[] EncodeRecord (ContentType contentType, IBufferOffsetSize buffer)
		{
			CheckValid ();
			var protocol = HasNegotiatedProtocol ? NegotiatedProtocol : Configuration.RequestedProtocol;

			int fragmentSize = MAX_FRAGMENT_SIZE;
			#if INSTRUMENTATION
			if (HasInstrument (HandshakeInstrumentType.FragmentHandshakeMessages))
				fragmentSize = 512;
			#endif

			var output = new TlsStream ();
			EncodeRecord_internal (protocol, contentType, Session != null ? Session.Write : null, buffer, output, fragmentSize);
			output.Finish ();

			var result = new byte [output.Size];
			Buffer.BlockCopy (output.Buffer, output.Offset, result, 0, output.Size);
			return result;
		}
示例#5
0
        static internal void EncodeRecord(TlsProtocolCode protocol, ContentType contentType, CryptoParameters crypto, IBufferOffsetSize buffer, TlsStream output)
        {
            var maxExtraBytes = crypto != null ? crypto.MaxExtraEncryptedBytes : 0;

            var offset    = buffer.Offset;
            var remaining = buffer.Size;

            do
            {
                BufferOffsetSize fragment;

                var encryptedSize = crypto != null?crypto.GetEncryptedSize(remaining) : remaining;

                if (encryptedSize <= MAX_FRAGMENT_SIZE)
                {
                    fragment = new BufferOffsetSize(buffer.Buffer, offset, remaining);
                }
                else
                {
                    fragment      = new BufferOffsetSize(buffer.Buffer, offset, MAX_FRAGMENT_SIZE - maxExtraBytes);
                    encryptedSize = crypto != null?crypto.GetEncryptedSize(fragment.Size) : fragment.Size;
                }

                // Write tls message
                output.Write((byte)contentType);
                output.Write((short)protocol);
                output.Write((short)encryptedSize);

                if (crypto != null)
                {
                    output.MakeRoom(encryptedSize);
                    var ret = crypto.Encrypt(contentType, fragment, output.GetRemaining());
                    output.Position += ret;
                }
                else
                {
                    output.Write(fragment.Buffer, fragment.Offset, fragment.Size);
                }

                offset    += fragment.Size;
                remaining -= fragment.Size;
            } while (remaining > 0);
        }
示例#6
0
 public static void EncodeRecord(TlsProtocolCode protocol, ContentType contentType, CryptoParameters crypto, IBufferOffsetSize buffer, TlsStream output)
 {
     EncodeRecord_internal(protocol, contentType, crypto, buffer, output);
 }
		public void TestMultiFragment (TestContext ctx, [TestHost] IEncryptionTestHost host)
		{
			// Padding will push us above the maximum fragment size.
			var size = MAX_FRAGMENT_SIZE - host.MinExtraEncryptedBytes + 1;
			var encryptedSize = host.GetEncryptedSize (size);
			ctx.Assert (encryptedSize, Is.GreaterThan (MAX_FRAGMENT_SIZE));

			var buffer = GetBuffer (MultiFragmentName, 0, size);
			var output = new TlsStream ();
			host.EncryptRecord (ContentType.ApplicationData, buffer, output);
			ctx.Assert (output.Position, Is.GreaterThanOrEqualTo (size + 2 * host.MinExtraEncryptedBytes + 10), "#2a");
			ctx.Assert (output.Position, Is.LessThanOrEqualTo (size + 2 * host.MaxExtraEncryptedBytes + 10), "#2b");
			ctx.Assert (output.Offset, Is.EqualTo (0), "#3");

			output.Position = 0;
			ctx.Assert (output.ReadByte (), Is.EqualTo ((byte)ContentType.ApplicationData), "#4a");
			ctx.Assert (output.ReadInt16 (), Is.EqualTo ((short)TlsProtocolCode.Tls12), "#4b");

			var firstChunkSize = (int)output.ReadInt16 ();
			ctx.Assert (firstChunkSize, Is.GreaterThanOrEqualTo (MAX_FRAGMENT_SIZE - host.MaxExtraEncryptedBytes - 1), "#4c");
			ctx.Assert (firstChunkSize, Is.LessThanOrEqualTo (MAX_FRAGMENT_SIZE), "#4d");

			output.Position += firstChunkSize;

			ctx.Assert (output.ReadByte (), Is.EqualTo ((byte)ContentType.ApplicationData), "#5a");
			ctx.Assert (output.ReadInt16 (), Is.EqualTo ((short)TlsProtocolCode.Tls12), "#5b");

			var secondChunkSize = (int)output.ReadInt16 ();
			ctx.Assert (secondChunkSize, Is.GreaterThanOrEqualTo (encryptedSize - firstChunkSize + host.MinExtraEncryptedBytes), "#5c");
			ctx.Assert (secondChunkSize, Is.LessThanOrEqualTo (encryptedSize - firstChunkSize + host.MaxExtraEncryptedBytes), "#5d");
			output.Position += secondChunkSize;

			WriteAndCheckOutput (ctx, MultiFragmentResult, new BufferOffsetSize (output.Buffer, 0, output.Position));
		}
		public void TestRecord (TestContext ctx, [TestHost] IEncryptionTestHost host)
		{
			var buffer = GetBuffer (TestDataName);

			var output = new TlsStream ();
			host.EncryptRecord (ContentType.ApplicationData, buffer, output);

			ctx.Assert (output.Position, Is.GreaterThanOrEqualTo (buffer.Size + host.MinExtraEncryptedBytes + 5), "#2a");
			ctx.Assert (output.Position, Is.LessThanOrEqualTo (buffer.Size + host.MaxExtraEncryptedBytes + 5), "#2b");

			var encryptedSize = host.GetEncryptedSize (buffer.Size);
			ctx.Assert (output.Position, Is.EqualTo (encryptedSize + 5), "#2c");

			output.Position = 0;
			ctx.Assert (output.ReadByte (), Is.EqualTo ((byte)ContentType.ApplicationData), "#4a");
			ctx.Assert (output.ReadInt16 (), Is.EqualTo ((short)TlsProtocolCode.Tls12), "#4b");
			ctx.Assert (output.ReadInt16 (), Is.EqualTo ((short)encryptedSize), "#4c");
			output.Position += encryptedSize;

			WriteAndCheckOutput (ctx, RecordResult, new BufferOffsetSize (output.Buffer, 0, output.Position));
		}
示例#9
0
		byte[] EncodeRecord_internal (ContentType contentType, IBufferOffsetSize buffer, int fragmentSize = MAX_FRAGMENT_SIZE)
		{
			CheckValid ();
			var protocol = HasNegotiatedProtocol ? NegotiatedProtocol : Configuration.RequestedProtocol;

			var output = new TlsStream ();
			EncodeRecord_internal (protocol, contentType, Session != null ? Session.Write : null, buffer, output, fragmentSize);
			output.Finish ();

			var result = new byte [output.Size];
			Buffer.BlockCopy (output.Buffer, output.Offset, result, 0, output.Size);
			return result;
		}