This repository has been archived by the owner on Jan 9, 2023. It is now read-only.
/
EncryptionResponse.java
224 lines (210 loc) · 8.3 KB
/
EncryptionResponse.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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
package dev.floffah.gamermode.server.packet.login;
import com.google.common.io.ByteArrayDataInput;
import com.google.common.io.ByteArrayDataOutput;
import com.google.common.io.ByteStreams;
import dev.floffah.gamermode.datatype.Identifier;
import dev.floffah.gamermode.datatype.VarInt;
import dev.floffah.gamermode.datatype.util.StringUtil;
import dev.floffah.gamermode.error.UUIDMismatchException;
import dev.floffah.gamermode.server.packet.BasePacket;
import dev.floffah.gamermode.server.packet.Packet;
import dev.floffah.gamermode.server.packet.PacketType;
import dev.floffah.gamermode.server.packet.play.JoinGame;
import dev.floffah.gamermode.server.packet.play.info.ServerDifficulty;
import dev.floffah.gamermode.server.packet.play.state.PlayerAbilitiesClientBound;
import java.io.IOException;
import java.math.BigInteger;
import java.nio.charset.StandardCharsets;
import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Arrays;
import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import dev.floffah.gamermode.server.socket.ConnectionState;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.format.NamedTextColor;
/**
* Incoming login packet for processing the client's encryption response
*/
@Packet(
name = "LoginEncryptionResponse",
id = 0x01,
type = PacketType.SERVERBOUND,
state = ConnectionState.LOGIN
)
public class EncryptionResponse extends BasePacket {
@Override
public void process(int len, ByteArrayDataInput in) throws IOException {
// temporary cipher
Cipher tempCipher;
// read the shared secret
int sharedSecretLength = VarInt.from(in).intValue();
byte[] sharedSecret = new byte[sharedSecretLength];
in.readFully(sharedSecret, 0, sharedSecretLength);
// read the verify token
int verifyTokenLength = VarInt.from(in).intValue();
byte[] verifyToken = new byte[verifyTokenLength];
in.readFully(verifyToken, 0, verifyTokenLength);
// initialise the temporary cipher
try {
tempCipher = Cipher.getInstance("RSA");
} catch (NoSuchPaddingException | NoSuchAlgorithmException e) {
this.conn.getSocketManager()
.getServer()
.getLogger()
.error("Error while creating a temporary cipher", e);
this.conn.disconnect(
Component.text(e.getMessage()).color(NamedTextColor.RED)
);
return;
}
// decrypt the shared secret
try {
tempCipher.init(
Cipher.DECRYPT_MODE,
this.conn.getKeyPair().getPrivate()
);
this.conn.setSharedSecret(
new SecretKeySpec(tempCipher.doFinal(sharedSecret), "AES")
);
} catch (
IllegalBlockSizeException
| BadPaddingException
| InvalidKeyException e
) {
this.conn.getSocketManager()
.getServer()
.getLogger()
.error("Error occurred while decrypting the shared secret", e);
this.conn.disconnect(
Component.text(e.getMessage()).color(NamedTextColor.RED)
);
return;
}
// decrypt the verify token
byte[] clientVerify;
try {
tempCipher.init(
Cipher.DECRYPT_MODE,
this.conn.getKeyPair().getPrivate()
);
clientVerify = tempCipher.doFinal(verifyToken);
} catch (
IllegalBlockSizeException
| BadPaddingException
| InvalidKeyException e
) {
this.conn.getSocketManager()
.getServer()
.getLogger()
.error(
"Error occurred while decrypting the encryption verification token",
e
);
this.conn.disconnect(
Component.text(e.getMessage()).color(NamedTextColor.RED)
);
return;
}
// make sure its the same as the server sent
if (!Arrays.equals(this.conn.getVerifyToken(), clientVerify)) {
this.conn.disconnect(
Component
.text("Invalid verify token")
.color(NamedTextColor.RED)
);
return;
}
// initialise the cipher used for the rest of communication
try {
Cipher encryptCypher = Cipher.getInstance("AES/CFB8/NoPadding");
encryptCypher.init(
Cipher.ENCRYPT_MODE,
this.conn.getSharedSecret(),
new IvParameterSpec(this.conn.getSharedSecret().getEncoded())
);
this.conn.setEncryptCipher(encryptCypher);
Cipher decryptCypher = Cipher.getInstance("AES/CFB8/NoPadding");
decryptCypher.init(
Cipher.DECRYPT_MODE,
this.conn.getSharedSecret(),
new IvParameterSpec(this.conn.getSharedSecret().getEncoded())
);
this.conn.setDecryptCipher(decryptCypher);
} catch (
InvalidAlgorithmParameterException
| NoSuchPaddingException
| InvalidKeyException
| NoSuchAlgorithmException e
) {
this.conn.getSocketManager()
.getServer()
.getLogger()
.error(
"Error occurred while initiating encryption and decryption cipher",
e
);
this.conn.disconnect(
Component
.text(e.getMessage())
.color(NamedTextColor.DARK_RED)
);
return;
}
// hash the server id, shared secret, and public key
try {
MessageDigest digest = MessageDigest.getInstance("SHA-1");
digest.reset();
digest.update(
this.conn.getSocketManager()
.getServer()
.getServerId()
.getBytes(StandardCharsets.US_ASCII)
);
digest.update(this.conn.getSharedSecret().getEncoded());
digest.update(this.conn.getKeyPair().getPublic().getEncoded());
BigInteger hashIntForm = new BigInteger(digest.digest());
String hash = hashIntForm.toString(16);
if (!hash.startsWith("-") && hashIntForm.signum() == -1) hash =
"-" + hash;
this.conn.setSessionHash(hash);
} catch (NoSuchAlgorithmException e) {
this.conn.getSocketManager()
.getServer()
.getLogger()
.error("Error occurred while generating a session hash", e);
this.conn.disconnect(
Component.text(e.getMessage()).color(NamedTextColor.RED)
);
return;
}
// enable encryption
this.conn.setEncrypted(true);
this.conn.getBaseIn().enableDecryption(this.conn.getDecryptCipher());
this.conn.getBaseOut().enableEncryption(this.conn.getEncryptCipher());
try {
this.conn.getPlayer().getProfile().doHasJoined();
} catch (UUIDMismatchException e) {
this.conn.getSocketManager()
.getServer()
.getLogger()
.error(
"Error occurred while notifying the Mojang API of the player joining",
e
);
this.conn.disconnect(
Component.text(e.getMessage()).color(NamedTextColor.RED)
);
return;
}
// send packets
this.conn.send(new LoginSuccess());
this.conn.send(new JoinGame());
}
}