See: http://dev.mysql.com/doc/internals/en/connection-phase-packets.html#packet-Protocol::HandshakeResponse
()
| 131 | |
| 132 | // See: http://dev.mysql.com/doc/internals/en/connection-phase-packets.html#packet-Protocol::HandshakeResponse |
| 133 | func (c *Conn) writeAuthHandshake() error { |
| 134 | if !slices.Contains(supportedAuthPlugins, c.authPluginName) { |
| 135 | return fmt.Errorf("unknown auth plugin name '%s'", c.authPluginName) |
| 136 | } |
| 137 | |
| 138 | // Set default client capabilities that reflect the abilities of this library |
| 139 | capability := CLIENT_PROTOCOL_41 | CLIENT_SECURE_CONNECTION | |
| 140 | CLIENT_LONG_PASSWORD | CLIENT_TRANSACTIONS | CLIENT_PLUGIN_AUTH |
| 141 | // Adjust client capability flags based on server support |
| 142 | capability |= c.capability & CLIENT_LONG_FLAG |
| 143 | // Adjust client capability flags on specific client requests |
| 144 | // Only flags that would make any sense setting and aren't handled elsewhere |
| 145 | // in the library are supported here |
| 146 | capability |= c.ccaps&CLIENT_FOUND_ROWS | c.ccaps&CLIENT_IGNORE_SPACE | |
| 147 | c.ccaps&CLIENT_MULTI_STATEMENTS | c.ccaps&CLIENT_MULTI_RESULTS | |
| 148 | c.ccaps&CLIENT_PS_MULTI_RESULTS | c.ccaps&CLIENT_CONNECT_ATTRS |
| 149 | |
| 150 | // To enable TLS / SSL |
| 151 | if c.tlsConfig != nil { |
| 152 | capability |= CLIENT_SSL |
| 153 | } |
| 154 | |
| 155 | auth, addNull, err := c.genAuthResponse(c.salt) |
| 156 | if err != nil { |
| 157 | return err |
| 158 | } |
| 159 | |
| 160 | // encode length of the auth plugin data |
| 161 | // here we use the Length-Encoded-Integer(LEI) as the data length may not fit into one byte |
| 162 | // see: https://dev.mysql.com/doc/internals/en/integer.html#length-encoded-integer |
| 163 | var authRespLEIBuf [9]byte |
| 164 | authRespLEI := AppendLengthEncodedInteger(authRespLEIBuf[:0], uint64(len(auth))) |
| 165 | if len(authRespLEI) > 1 { |
| 166 | // if the length can not be written in 1 byte, it must be written as a |
| 167 | // length encoded integer |
| 168 | capability |= CLIENT_PLUGIN_AUTH_LENENC_CLIENT_DATA |
| 169 | } |
| 170 | |
| 171 | // packet length |
| 172 | // capability 4 |
| 173 | // max-packet size 4 |
| 174 | // charset 1 |
| 175 | // reserved all[0] 23 |
| 176 | // username |
| 177 | // auth |
| 178 | // mysql_native_password + null-terminated |
| 179 | length := 4 + 4 + 1 + 23 + len(c.user) + 1 + len(authRespLEI) + len(auth) + 21 + 1 |
| 180 | if addNull { |
| 181 | length++ |
| 182 | } |
| 183 | // db name |
| 184 | if len(c.db) > 0 { |
| 185 | capability |= CLIENT_CONNECT_WITH_DB |
| 186 | length += len(c.db) + 1 |
| 187 | } |
| 188 | |
| 189 | data := make([]byte, length+4) |
| 190 |
no test coverage detected