Exemple #1
0
        /// <summary>
        /// Writes a sequence of bytes to the current stream. The data is encrypted first before sending out.
        /// </summary>
        /// <param name="buffer">The buffer to be sent.</param>
        /// <param name="offset">The offset in buffer at which to begin writing to the stream.</param>
        /// <param name="count">The number of bytes to be written.</param>
        /// <exception cref="IOException">Raised when attempting to read from/write to a remote connection which
        /// has been closed</exception>
        /// <exception cref="ArgumentOutOfRangeException">Raised when the offset incremented by count exceeds
        /// the length of buffer</exception>
        public override void Write(byte[] buffer, int offset, int count)
        {
            if (offset > buffer.Length - 1)
            {
                throw new ArgumentOutOfRangeException("offset");
            }

            if (offset + count > buffer.Length)
            {
                throw new ArgumentOutOfRangeException("count");
            }

            byte[] outBuffer = new byte[count];
            Array.Copy(buffer, offset, outBuffer, 0, count);

            // Encrypt message
            SecurityPackageContextStreamSizes streamSizes =
                (SecurityPackageContextStreamSizes)context.QueryContextAttributes("SECPKG_ATTR_STREAM_SIZES");
            SecurityBuffer messageBuffer = new SecurityBuffer(SecurityBufferType.Data, buffer);
            SecurityBuffer headerBuffer  = new SecurityBuffer(
                SecurityBufferType.StreamHeader,
                new byte[streamSizes.Header]);
            SecurityBuffer trailerBuffer = new SecurityBuffer(
                SecurityBufferType.StreamTrailer,
                new byte[streamSizes.Trailer]);
            SecurityBuffer emptyBuffer = new SecurityBuffer(SecurityBufferType.Empty, null);

            context.Encrypt(headerBuffer, messageBuffer, trailerBuffer, emptyBuffer);
            byte[] encryptedMsg = ArrayUtility.ConcatenateArrays(
                headerBuffer.Buffer,
                messageBuffer.Buffer,
                trailerBuffer.Buffer);
            serverStream.Write(encryptedMsg, 0, encryptedMsg.Length);
        }
Exemple #2
0
        /// <summary>
        /// Writes a sequence of bytes to the current stream. The data is encrypted first before sending out.
        /// </summary>
        /// <param name="buffer">The buffer to be sent.</param>
        /// <param name="offset">The offset in buffer at which to begin writing to the stream.</param>
        /// <param name="count">The number of bytes to be written.</param>
        /// <exception cref="IOException">Raised when attempting to read from/write to a remote connection which
        /// has been closed</exception>
        /// <exception cref="ArgumentOutOfRangeException">Raised when the offset incremented by count exceeds
        /// the length of buffer</exception>
        public override void Write(byte[] buffer, int offset, int count)
        {
            if (offset > buffer.Length - 1)
            {
                throw new ArgumentOutOfRangeException("offset");
            }

            if (offset + count > buffer.Length)
            {
                throw new ArgumentOutOfRangeException("count");
            }

            // Get stream attribute
            SecurityPackageContextStreamSizes streamSizes =
                (SecurityPackageContextStreamSizes)context.QueryContextAttributes("SECPKG_ATTR_STREAM_SIZES");

            int         chunckSize = (int)streamSizes.MaximumMessage;
            List <byte> byteList   = new List <byte>();

            while (count > 0)
            {
                int bufferSize = count;
                if (bufferSize > chunckSize)
                {
                    bufferSize = chunckSize;
                }

                byte[] outBuffer = new byte[bufferSize];
                Array.Copy(buffer, offset, outBuffer, 0, bufferSize);
                count  -= bufferSize;
                offset += bufferSize;

                // Encrypt Chunck
                SecurityBuffer messageBuffer = new SecurityBuffer(SecurityBufferType.Data, outBuffer);
                SecurityBuffer headerBuffer  = new SecurityBuffer(
                    SecurityBufferType.StreamHeader,
                    new byte[streamSizes.Header]);
                SecurityBuffer trailerBuffer = new SecurityBuffer(
                    SecurityBufferType.StreamTrailer,
                    new byte[streamSizes.Trailer]);
                SecurityBuffer emptyBuffer = new SecurityBuffer(SecurityBufferType.Empty, null);

                context.Encrypt(headerBuffer, messageBuffer, trailerBuffer, emptyBuffer);
                byte[] encryptedChunck = ArrayUtility.ConcatenateArrays(
                    headerBuffer.Buffer,
                    messageBuffer.Buffer,
                    trailerBuffer.Buffer);
                byteList.AddRange(encryptedChunck);
            }

            byte[] encryptedMsg = byteList.ToArray();
            serverStream.Write(encryptedMsg, 0, encryptedMsg.Length);
        }