Exemplo n.º 1
0
        byte[] _CreateAlert(Alert alert)
        {
            var buffer = new BufferOffsetSize(2);

            buffer.Buffer [0] = (byte)alert.Level;
            buffer.Buffer [1] = (byte)alert.Description;

            return(EncodeRecord(ContentType.Alert, buffer));
        }
Exemplo n.º 2
0
		public BufferOffsetSize[] GetBufferArray ()
		{
			int count = 0;
			for (var ptr = first; ptr != null; ptr = ptr.next)
				count++;
			var array = new BufferOffsetSize [count];
			count = 0;
			for (var ptr = first; ptr != null; ptr = ptr.next)
				array [count++] = ptr;
			return array;
		}
Exemplo n.º 3
0
		public BufferOffsetSize GetBuffer ()
		{
			int totalSize = 0;
			for (var ptr = first; ptr != null; ptr = ptr.next)
				totalSize += ptr.Size;

			var outBuffer = new BufferOffsetSize (new byte [totalSize]);
			int offset = 0;
			for (var ptr = first; ptr != null; ptr = ptr.next) {
				Buffer.BlockCopy (ptr.Buffer, ptr.Offset, outBuffer.Buffer, offset, ptr.Size);
				offset += ptr.Size;
			}
			return outBuffer;
		}
Exemplo n.º 4
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);
        }
Exemplo n.º 5
0
        public BufferOffsetSize[] GetBufferArray()
        {
            int count = 0;

            for (var ptr = first; ptr != null; ptr = ptr.next)
            {
                count++;
            }
            var array = new BufferOffsetSize [count];

            count = 0;
            for (var ptr = first; ptr != null; ptr = ptr.next)
            {
                array [count++] = ptr;
            }
            return(array);
        }
Exemplo n.º 6
0
        public BufferOffsetSize GetBuffer()
        {
            int totalSize = 0;

            for (var ptr = first; ptr != null; ptr = ptr.next)
            {
                totalSize += ptr.Size;
            }

            var outBuffer = new BufferOffsetSize(new byte [totalSize]);
            int offset    = 0;

            for (var ptr = first; ptr != null; ptr = ptr.next)
            {
                Buffer.BlockCopy(ptr.Buffer, ptr.Offset, outBuffer.Buffer, offset, ptr.Size);
                offset += ptr.Size;
            }
            return(outBuffer);
        }
Exemplo n.º 7
0
		public void TestDecryptWithInvalidPadding2 (TestContext ctx, [TestHost] IEncryptionTestHost host)
		{
			var input = GetBuffer (Data11Result);

			var modified = new TlsBuffer (input.Size);
			modified.Write (input.Buffer);

			// Flip a bit in the last byte, this will affect the padding size.
			modified.Buffer [modified.Size - 1] ^= 0x01;

			input = new BufferOffsetSize (modified.Buffer, 0, modified.Size);

			try {
				host.Decrypt (input);
				ctx.AssertFail ("#1");
			} catch (Exception ex) {
				ctx.Assert (ex, Is.InstanceOf<TlsException> (), "#2");
				var tlsEx = (TlsException)ex;
				ctx.Assert (tlsEx.Alert.Level, Is.EqualTo (AlertLevel.Fatal), "#3");
				ctx.Assert (tlsEx.Alert.Description, Is.EqualTo (AlertDescription.BadRecordMAC), "#4");
			}
		}
Exemplo n.º 8
0
		public void TestDecryptWithInvalidPadding (TestContext ctx, [TestHost] IEncryptionTestHost host)
		{
			var input = GetBuffer (ExtraPaddingResult);

			var modified = new TlsBuffer (input.Size);
			modified.Write (input.Buffer);

			var theOffset = modified.Size - (2 * host.BlockSize) - 5;
			modified.Buffer [theOffset] ^= 0x01;

			input = new BufferOffsetSize (modified.Buffer, 0, modified.Size);

			try {
				host.Decrypt (input);
				ctx.AssertFail ("#1");
			} catch (Exception ex) {
				ctx.Assert (ex, Is.InstanceOf<TlsException> (), "#2");
				var tlsEx = (TlsException)ex;
				ctx.Assert (tlsEx.Alert.Level, Is.EqualTo (AlertLevel.Fatal), "#3");
				ctx.Assert (tlsEx.Alert.Description, Is.EqualTo (AlertDescription.BadRecordMAC), "#4");
			}
		}