private RpcResponseMessage CreateResponse(RpcRequestMessage request)
        {
            byte[] kmsRequestData = request.Data;
            byte[] responseBytes  = RequestMessageHandler.HandleRequest(kmsRequestData);

            RpcResponseMessage response = new RpcResponseMessage {
                Data = responseBytes
            };
            int envelopeLength = response.Data.Length;

            response.Version            = request.Version;
            response.VersionMinor       = request.VersionMinor;
            response.PacketType         = 0x02;
            response.PacketFlags        = 0x03;
            response.DataRepresentation = request.DataRepresentation;
            response.FragLength         = (ushort)(24 + envelopeLength);
            response.AuthLength         = request.AuthLength;
            response.CallId             = request.CallId;
            response.AllocHint          = (uint)envelopeLength;
            response.ContextId          = request.ContextId;
            response.CancelCount        = 0x00;
            response.Opnum = (byte)request.Opnum;

            return(response);
        }
        public byte[] HandleRequest(byte[] b)
        {
            RpcRequestMessage request = CreateRequest(b);

            RpcResponseMessage response = CreateResponse(request);

            byte[] responseBytes = CreateResponseArray(response);

            return(responseBytes);
        }
 private static RpcRequestMessage CreateRequest(byte[] b)
 {
     using (MemoryStream stream = new MemoryStream(b))
     {
         using (BinaryReader binaryReader = new BinaryReader(stream))
         {
             RpcRequestMessage request = new RpcRequestMessage {
                 Version = binaryReader.ReadByte(), VersionMinor = binaryReader.ReadByte(), PacketType = binaryReader.ReadByte(), PacketFlags = binaryReader.ReadByte(), DataRepresentation = binaryReader.ReadUInt32(), FragLength = binaryReader.ReadUInt16(), AuthLength = binaryReader.ReadUInt16(), CallId = binaryReader.ReadUInt32(), AllocHint = binaryReader.ReadUInt32(), ContextId = binaryReader.ReadUInt16(), Opnum = binaryReader.ReadUInt16()
             };
             request.Data = binaryReader.ReadBytes((int)request.AllocHint);
             return(request);
         }
     }
 }