コード例 #1
0
        public static byte[] Create(InitialHandshakePacket handshake, string userName, string password, string database)
        {
            // TODO: verify server capabilities

            var writer = new PayloadWriter();

            writer.WriteInt32((int)(
                                  ProtocolCapabilities.Protocol41 |
                                  ProtocolCapabilities.LongPassword |
                                  ProtocolCapabilities.SecureConnection |
                                  ProtocolCapabilities.PluginAuth |
                                  ProtocolCapabilities.PluginAuthLengthEncodedClientData |
                                  ProtocolCapabilities.MultiStatements |
                                  ProtocolCapabilities.MultiResults |
                                  ProtocolCapabilities.PreparedStatementMultiResults |
                                  (string.IsNullOrWhiteSpace(database) ? 0 : ProtocolCapabilities.ConnectWithDatabase)));
            writer.WriteInt32(0x40000000);
            writer.WriteByte((byte)CharacterSet.Utf8Mb4Binary);
            writer.Write(new byte[23]);
            writer.WriteNullTerminatedString(userName);

            var authenticationResponse = AuthenticationUtility.CreateAuthenticationResponse(handshake.AuthPluginData, 0, password);

            writer.WriteByte((byte)authenticationResponse.Length);
            writer.Write(authenticationResponse);

            if (!string.IsNullOrWhiteSpace(database))
            {
                writer.WriteNullTerminatedString(database);
            }

            writer.WriteNullTerminatedString("mysql_native_password");

            return(writer.ToBytes());
        }
コード例 #2
0
        public static PayloadData Create(string user, byte[] authResponse, string schemaName)
        {
            var writer = new PayloadWriter();

            writer.WriteByte((byte)CommandKind.ChangeUser);
            writer.WriteNullTerminatedString(user);
            writer.WriteByte(checked ((byte)authResponse.Length));
            writer.Write(authResponse);
            writer.WriteNullTerminatedString(schemaName ?? "");
            writer.WriteByte((byte)CharacterSet.Utf8Mb4Binary);
            writer.WriteByte(0);
            writer.WriteNullTerminatedString("mysql_native_password");

            return(new PayloadData(new ArraySegment <byte>(writer.ToBytes())));
        }
コード例 #3
0
        public static PayloadData Create(string user, byte[] authResponse, string schemaName)
        {
            var writer = new PayloadWriter();

            writer.WriteByte((byte) CommandKind.ChangeUser);
            writer.WriteNullTerminatedString(user);
            writer.WriteByte(checked((byte) authResponse.Length));
            writer.Write(authResponse);
            writer.WriteNullTerminatedString(schemaName ?? "");
            writer.WriteByte((byte) CharacterSet.Utf8Mb4Binary);
            writer.WriteByte(0);
            writer.WriteNullTerminatedString("mysql_native_password");

            return new PayloadData(new ArraySegment<byte>(writer.ToBytes()));
        }
コード例 #4
0
		public static byte[] Create(InitialHandshakePacket handshake, string userName, string password, string database)
		{
			// TODO: verify server capabilities

			var writer = new PayloadWriter();

			writer.WriteInt32((int) (
				ProtocolCapabilities.Protocol41 |
				ProtocolCapabilities.LongPassword |
				ProtocolCapabilities.SecureConnection |
				ProtocolCapabilities.PluginAuth |
				ProtocolCapabilities.PluginAuthLengthEncodedClientData |
				ProtocolCapabilities.MultiStatements |
				ProtocolCapabilities.MultiResults |
				ProtocolCapabilities.PreparedStatementMultiResults |
				(string.IsNullOrWhiteSpace(database) ? 0 : ProtocolCapabilities.ConnectWithDatabase)));
			writer.WriteInt32(0x40000000);
			writer.WriteByte((byte) CharacterSet.Utf8Mb4Binary);
			writer.Write(new byte[23]);
			writer.WriteNullTerminatedString(userName);

			writer.WriteLengthEncodedInteger(20);
			var hashedPassword = AuthenticationUtility.HashPassword(handshake.AuthPluginData, 0, password);
			writer.Write(hashedPassword);

			if (!string.IsNullOrWhiteSpace(database))
				writer.WriteNullTerminatedString(database);

			writer.WriteNullTerminatedString("mysql_native_password");

			return writer.ToBytes();
		}
コード例 #5
0
        public static PayloadData Create(string databaseName)
        {
            var writer = new PayloadWriter();

            writer.WriteByte((byte)CommandKind.InitDatabase);
            writer.Write(Encoding.UTF8.GetBytes(databaseName));

            return(new PayloadData(new ArraySegment <byte>(writer.ToBytes())));
        }
コード例 #6
0
        public static PayloadData Create(uint statementId, IList<StatementParameter> parameters)
        {
            var writer = new PayloadWriter();

            writer.WriteByte((byte) CommandKind.ExecuteStatement);
            writer.WriteUInt32(statementId);
            writer.WriteByte(0); // flags == CURSOR_TYPE_NO_CURSOR
            writer.WriteUInt32(1); // iteration count is always 1
            if (parameters.Count > 0)
            {
                int parametersProcessed = 0;
                while (parametersProcessed < parameters.Count)
                {
                    byte nullBitmap = 0;
                    for (int i = 0; i < Math.Min(8, parameters.Count - parametersProcessed); i++)
                        if (parameters[parametersProcessed + i].IsNull)
                            nullBitmap |= (byte) (1 << i);
                    writer.WriteByte(nullBitmap);
                    parametersProcessed += 8;
                }

                writer.WriteByte((byte) (parameters.Count == 0 ? 0 : 1)); // new parameters bound
                foreach (var parameter in parameters)
                {
                    writer.WriteByte((byte) parameter.Type);
                    writer.WriteByte((byte) (parameter.IsUnsigned ? 0x80 : 0));
                }
                foreach (var parameter in parameters)
                    writer.Write(parameter.Data);
            }

            return new PayloadData(new ArraySegment<byte>(writer.ToBytes()));
        }
コード例 #7
0
        internal static PayloadWriter CapabilitiesPayload(ConnectionSettings cs, ProtocolCapabilities additionalCapabilities = 0)
        {
            var writer = new PayloadWriter();

            writer.WriteInt32((int)(
                                  ProtocolCapabilities.Protocol41 |
                                  ProtocolCapabilities.LongPassword |
                                  ProtocolCapabilities.SecureConnection |
                                  ProtocolCapabilities.PluginAuth |
                                  ProtocolCapabilities.PluginAuthLengthEncodedClientData |
                                  ProtocolCapabilities.MultiStatements |
                                  ProtocolCapabilities.MultiResults |
                                  ProtocolCapabilities.PreparedStatementMultiResults |
                                  (string.IsNullOrWhiteSpace(cs.Database) ? 0 : ProtocolCapabilities.ConnectWithDatabase) |
                                  (cs.UseAffectedRows ? 0 : ProtocolCapabilities.FoundRows) |
                                  (cs.UseCompression ? ProtocolCapabilities.Compress : ProtocolCapabilities.None) |
                                  additionalCapabilities));
            writer.WriteInt32(0x40000000);
            writer.WriteByte((byte)CharacterSet.Utf8Mb4Binary);
            writer.Write(new byte[23]);

            return(writer);
        }
コード例 #8
0
        public static PayloadData Create(uint statementId, IList <StatementParameter> parameters)
        {
            var writer = new PayloadWriter();

            writer.WriteByte((byte)CommandKind.ExecuteStatement);
            writer.WriteUInt32(statementId);
            writer.WriteByte(0);             // flags == CURSOR_TYPE_NO_CURSOR
            writer.WriteUInt32(1);           // iteration count is always 1
            if (parameters.Count > 0)
            {
                int parametersProcessed = 0;
                while (parametersProcessed < parameters.Count)
                {
                    byte nullBitmap = 0;
                    for (int i = 0; i < Math.Min(8, parameters.Count - parametersProcessed); i++)
                    {
                        if (parameters[parametersProcessed + i].IsNull)
                        {
                            nullBitmap |= (byte)(1 << i);
                        }
                    }
                    writer.WriteByte(nullBitmap);
                    parametersProcessed += 8;
                }

                writer.WriteByte((byte)(parameters.Count == 0 ? 0 : 1));                  // new parameters bound
                foreach (var parameter in parameters)
                {
                    writer.WriteByte((byte)parameter.Type);
                    writer.WriteByte((byte)(parameter.IsUnsigned ? 0x80 : 0));
                }
                foreach (var parameter in parameters)
                {
                    writer.Write(parameter.Data);
                }
            }

            return(new PayloadData(new ArraySegment <byte>(writer.ToBytes())));
        }