/
HandshakeResponse.java
196 lines (167 loc) · 7.2 KB
/
HandshakeResponse.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
// SPDX-License-Identifier: Apache-2.0
// Copyright (c) 2020-2022 MariaDB Corporation Ab
package org.mariadb.r2dbc.message.client;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.ByteBufAllocator;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.util.Map;
import java.util.Properties;
import org.mariadb.r2dbc.MariadbConnectionFactoryProvider;
import org.mariadb.r2dbc.authentication.addon.ClearPasswordPluginFlow;
import org.mariadb.r2dbc.authentication.standard.CachingSha2PasswordFlow;
import org.mariadb.r2dbc.authentication.standard.NativePasswordPluginFlow;
import org.mariadb.r2dbc.message.ClientMessage;
import org.mariadb.r2dbc.message.Context;
import org.mariadb.r2dbc.message.server.InitialHandshakePacket;
import org.mariadb.r2dbc.message.server.Sequencer;
import org.mariadb.r2dbc.util.BufferUtils;
import org.mariadb.r2dbc.util.HostAddress;
import org.mariadb.r2dbc.util.constants.Capabilities;
import reactor.core.publisher.Mono;
public final class HandshakeResponse implements ClientMessage {
private final InitialHandshakePacket initialHandshakePacket;
private final String username;
private final CharSequence password;
private final String database;
private final Map<String, String> connectionAttributes;
private final HostAddress hostAddress;
private final long clientCapabilities;
public HandshakeResponse(
InitialHandshakePacket initialHandshakePacket,
String username,
CharSequence password,
String database,
Map<String, String> connectionAttributes,
HostAddress hostAddress,
long clientCapabilities) {
this.initialHandshakePacket = initialHandshakePacket;
this.username = username;
this.password = password;
this.database = database;
this.connectionAttributes = connectionAttributes;
this.hostAddress = hostAddress;
this.clientCapabilities = clientCapabilities;
}
/**
* Default collation used for string exchanges with server. Always return 4 bytes utf8 collation
* for server that permit it.
*
* @param serverLanguage server default collation
* @param majorVersion server major version
* @param minorVersion server minor version
* @return collation byte
*/
public static byte decideLanguage(short serverLanguage, int majorVersion, int minorVersion) {
// return current server utf8mb4 collation
if (serverLanguage == 45 // utf8mb4_general_ci
|| serverLanguage == 46 // utf8mb4_bin
|| (serverLanguage >= 224 && serverLanguage <= 247)) {
return (byte) serverLanguage;
}
return (byte) ((majorVersion == 5 && minorVersion <= 1) ? 33 : 224);
}
@Override
public Mono<ByteBuf> encode(Context context, ByteBufAllocator allocator) {
byte exchangeCharset =
decideLanguage(
initialHandshakePacket.getDefaultCollation(),
initialHandshakePacket.getMajorServerVersion(),
initialHandshakePacket.getMinorServerVersion());
ByteBuf buf = allocator.buffer(4096);
final byte[] authData;
String authenticationPluginType = initialHandshakePacket.getAuthenticationPluginType();
switch (authenticationPluginType) {
case ClearPasswordPluginFlow.TYPE:
// TODO check that SSL is enable
authData =
(password == null) ? new byte[0] : password.toString().getBytes(StandardCharsets.UTF_8);
break;
case CachingSha2PasswordFlow.TYPE:
authenticationPluginType = CachingSha2PasswordFlow.TYPE;
authData =
(password == null)
? new byte[0]
: CachingSha2PasswordFlow.sha256encryptPassword(
password, initialHandshakePacket.getSeed());
break;
default:
authenticationPluginType = NativePasswordPluginFlow.TYPE;
authData = NativePasswordPacket.encrypt(password, initialHandshakePacket.getSeed());
break;
}
buf.writeIntLE((int) clientCapabilities);
buf.writeIntLE(1024 * 1024 * 1024);
buf.writeByte(exchangeCharset); // 1
buf.writeZero(19); // 19
buf.writeIntLE((int) (clientCapabilities >> 32)); // Maria extended flag
// to permit SSO
buf.writeCharSequence(
(username != null && !username.isEmpty()) ? username : System.getProperty("user.name"),
StandardCharsets.UTF_8);
buf.writeZero(1);
if ((initialHandshakePacket.getCapabilities() & Capabilities.PLUGIN_AUTH_LENENC_CLIENT_DATA)
!= 0) {
buf.writeBytes(BufferUtils.encodeLength(authData.length));
buf.writeBytes(authData);
} else if ((initialHandshakePacket.getCapabilities() & Capabilities.SECURE_CONNECTION) != 0) {
buf.writeByte((byte) authData.length);
buf.writeBytes(authData);
} else {
buf.writeBytes(authData);
buf.writeZero(1);
}
if ((clientCapabilities & Capabilities.CONNECT_WITH_DB) != 0) {
buf.writeCharSequence(database, StandardCharsets.UTF_8);
buf.writeZero(1);
}
if ((initialHandshakePacket.getCapabilities() & Capabilities.PLUGIN_AUTH) != 0) {
buf.writeCharSequence(authenticationPluginType, StandardCharsets.UTF_8);
buf.writeZero(1);
}
if ((initialHandshakePacket.getCapabilities() & Capabilities.CONNECT_ATTRS) != 0) {
ByteBuf bufAttributes = allocator.buffer(2048);
writeConnectAttributes(bufAttributes, connectionAttributes, hostAddress);
buf.writeBytes(BufferUtils.encodeLength(bufAttributes.writerIndex()));
buf.writeBytes(bufAttributes, 0, bufAttributes.writerIndex());
bufAttributes.release();
}
return Mono.just(buf);
}
@Override
public Sequencer getSequencer() {
return initialHandshakePacket.getSequencer();
}
private void writeConnectAttributes(
ByteBuf buf, Map<String, String> connectionAttributes, HostAddress hostAddress) {
BufferUtils.writeLengthEncode("_client_name", buf);
BufferUtils.writeLengthEncode(MariadbConnectionFactoryProvider.MARIADB_DRIVER, buf);
final Properties properties = new Properties();
try (InputStream inputStream =
getClass().getClassLoader().getResourceAsStream("project" + ".properties")) {
properties.load(inputStream);
BufferUtils.writeLengthEncode("_client_version", buf);
BufferUtils.writeLengthEncode(properties.getProperty("version"), buf);
} catch (IOException ie) {
// eat
}
BufferUtils.writeLengthEncode("_server_host", buf);
BufferUtils.writeLengthEncode(hostAddress != null ? hostAddress.getHost() : "", buf);
BufferUtils.writeLengthEncode("_os", buf);
BufferUtils.writeLengthEncode(System.getProperty("os.name"), buf);
BufferUtils.writeLengthEncode("_thread", buf);
BufferUtils.writeLengthEncode(Long.toString(Thread.currentThread().getId()), buf);
BufferUtils.writeLengthEncode("_java_vendor", buf);
BufferUtils.writeLengthEncode(System.getProperty("java.vendor"), buf);
BufferUtils.writeLengthEncode("_java_version", buf);
BufferUtils.writeLengthEncode(System.getProperty("java.version"), buf);
if (connectionAttributes != null && !connectionAttributes.isEmpty()) {
connectionAttributes.forEach(
(key, val) -> {
BufferUtils.writeLengthEncode(key, buf);
BufferUtils.writeLengthEncode(val, buf);
});
}
}
}