mirror of
https://github.com/GeyserMC/Geyser.git
synced 2024-08-14 23:57:35 +00:00
Added RawSkins, Toppings and renamed the Floodgate plugin name
This commit is contained in:
parent
bb20b14e4c
commit
7fbc401dfa
16 changed files with 261 additions and 104 deletions
|
@ -26,7 +26,7 @@
|
|||
|
||||
package org.geysermc.floodgate.crypto;
|
||||
|
||||
import org.geysermc.floodgate.util.InvalidHeaderException;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
|
||||
import javax.crypto.Cipher;
|
||||
import javax.crypto.SecretKey;
|
||||
|
@ -35,12 +35,14 @@ import java.nio.ByteBuffer;
|
|||
import java.security.Key;
|
||||
import java.security.SecureRandom;
|
||||
|
||||
@RequiredArgsConstructor
|
||||
public final class AesCipher implements FloodgateCipher {
|
||||
private static final int IV_LENGTH = 12;
|
||||
public static final int IV_LENGTH = 12;
|
||||
private static final int TAG_BIT_LENGTH = 128;
|
||||
private static final String CIPHER_NAME = "AES/GCM/NoPadding";
|
||||
|
||||
private final SecureRandom secureRandom = new SecureRandom();
|
||||
private final Topping topping;
|
||||
private SecretKey secretKey;
|
||||
|
||||
public void init(Key key) {
|
||||
|
@ -62,32 +64,57 @@ public final class AesCipher implements FloodgateCipher {
|
|||
cipher.init(Cipher.ENCRYPT_MODE, secretKey, spec);
|
||||
byte[] cipherText = cipher.doFinal(data);
|
||||
|
||||
return ByteBuffer.allocate(iv.length + cipherText.length + HEADER_LENGTH)
|
||||
.put(IDENTIFIER).put(VERSION) // header
|
||||
if (topping != null) {
|
||||
iv = topping.encode(iv);
|
||||
cipherText = topping.encode(cipherText);
|
||||
}
|
||||
|
||||
return ByteBuffer.allocate(iv.length + cipherText.length + HEADER_LENGTH + 1)
|
||||
.put(IDENTIFIER) // header
|
||||
.put(iv)
|
||||
.put((byte) 0x21)
|
||||
.put(cipherText)
|
||||
.array();
|
||||
}
|
||||
|
||||
public byte[] decrypt(byte[] cipherTextWithIv) throws Exception {
|
||||
HeaderResult pair = checkHeader(cipherTextWithIv);
|
||||
if (pair.getVersion() != VERSION) {
|
||||
throw new InvalidHeaderException(
|
||||
"Expected version " + VERSION + ", got " + pair.getVersion()
|
||||
);
|
||||
}
|
||||
checkHeader(cipherTextWithIv);
|
||||
|
||||
Cipher cipher = Cipher.getInstance(CIPHER_NAME);
|
||||
|
||||
int bufferLength = cipherTextWithIv.length - HEADER_LENGTH;
|
||||
ByteBuffer buffer = ByteBuffer.wrap(cipherTextWithIv, HEADER_LENGTH, bufferLength);
|
||||
|
||||
byte[] iv = new byte[IV_LENGTH];
|
||||
int ivLength = IV_LENGTH;
|
||||
|
||||
if (topping != null) {
|
||||
int mark = buffer.position();
|
||||
|
||||
// we need the first index, the second is for the optional RawSkin
|
||||
boolean found = false;
|
||||
while (buffer.hasRemaining() && !found) {
|
||||
if (buffer.get() == 0x21) {
|
||||
found = true;
|
||||
}
|
||||
}
|
||||
|
||||
ivLength = buffer.position() - mark - 1; // don't include the splitter itself
|
||||
buffer.position(mark); // reset to the pre-while index
|
||||
}
|
||||
|
||||
byte[] iv = new byte[ivLength];
|
||||
buffer.get(iv);
|
||||
|
||||
buffer.position(buffer.position() + 1); // skip splitter
|
||||
|
||||
byte[] cipherText = new byte[buffer.remaining()];
|
||||
buffer.get(cipherText);
|
||||
|
||||
if (topping != null) {
|
||||
iv = topping.decode(iv);
|
||||
cipherText = topping.decode(cipherText);
|
||||
}
|
||||
|
||||
GCMParameterSpec spec = new GCMParameterSpec(TAG_BIT_LENGTH, iv);
|
||||
cipher.init(Cipher.DECRYPT_MODE, secretKey, spec);
|
||||
return cipher.doFinal(cipherText);
|
||||
|
|
|
@ -0,0 +1,40 @@
|
|||
/*
|
||||
* Copyright (c) 2019-2020 GeyserMC. http://geysermc.org
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
* THE SOFTWARE.
|
||||
*
|
||||
* @author GeyserMC
|
||||
* @link https://github.com/GeyserMC/Geyser
|
||||
*/
|
||||
|
||||
package org.geysermc.floodgate.crypto;
|
||||
|
||||
import java.util.Base64;
|
||||
|
||||
public final class Base64Topping implements Topping {
|
||||
@Override
|
||||
public byte[] encode(byte[] data) {
|
||||
return Base64.getEncoder().encode(data);
|
||||
}
|
||||
|
||||
@Override
|
||||
public byte[] decode(byte[] data) {
|
||||
return Base64.getDecoder().decode(data);
|
||||
}
|
||||
}
|
|
@ -28,7 +28,7 @@ package org.geysermc.floodgate.crypto;
|
|||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
||||
import org.geysermc.floodgate.util.InvalidHeaderException;
|
||||
import org.geysermc.floodgate.util.InvalidFormatException;
|
||||
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.security.Key;
|
||||
|
@ -38,9 +38,7 @@ import java.security.Key;
|
|||
*/
|
||||
public interface FloodgateCipher {
|
||||
byte[] IDENTIFIER = "Floodgate".getBytes(StandardCharsets.UTF_8);
|
||||
byte VERSION = 2;
|
||||
|
||||
int HEADER_LENGTH = IDENTIFIER.length + 1; // one byte for version
|
||||
int HEADER_LENGTH = IDENTIFIER.length;
|
||||
|
||||
/**
|
||||
* Initializes the instance by giving it the key it needs to encrypt or decrypt data
|
||||
|
@ -110,19 +108,20 @@ public interface FloodgateCipher {
|
|||
}
|
||||
|
||||
/**
|
||||
* Checks if the header is valid and return a IntPair containing the header version
|
||||
* and the index to start reading the actual encrypted data from.
|
||||
* Checks if the header is valid.
|
||||
* This method will throw an InvalidFormatException when the header is invalid.
|
||||
*
|
||||
* @param data the data to check
|
||||
* @return IntPair. x = version number, y = the index to start reading from.
|
||||
* @throws InvalidHeaderException when the header is invalid
|
||||
* @throws InvalidFormatException when the header is invalid
|
||||
*/
|
||||
default HeaderResult checkHeader(byte[] data) throws InvalidHeaderException {
|
||||
default void checkHeader(byte[] data) throws InvalidFormatException {
|
||||
final int identifierLength = IDENTIFIER.length;
|
||||
|
||||
if (data.length <= HEADER_LENGTH) {
|
||||
throw new InvalidHeaderException("Data length is smaller then header." +
|
||||
"Needed " + HEADER_LENGTH + ", got " + data.length);
|
||||
throw new InvalidFormatException("Data length is smaller then header." +
|
||||
"Needed " + HEADER_LENGTH + ", got " + data.length,
|
||||
true
|
||||
);
|
||||
}
|
||||
|
||||
for (int i = 0; i < identifierLength; i++) {
|
||||
|
@ -132,15 +131,15 @@ public interface FloodgateCipher {
|
|||
receivedIdentifier.append(b);
|
||||
}
|
||||
|
||||
throw new InvalidHeaderException(String.format(
|
||||
"Expected identifier %s, got %s",
|
||||
new String(IDENTIFIER, StandardCharsets.UTF_8),
|
||||
receivedIdentifier.toString()
|
||||
));
|
||||
throw new InvalidFormatException(
|
||||
String.format("Expected identifier %s, got %s",
|
||||
new String(IDENTIFIER, StandardCharsets.UTF_8),
|
||||
receivedIdentifier.toString()
|
||||
),
|
||||
true
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
return new HeaderResult(data[identifierLength], HEADER_LENGTH);
|
||||
}
|
||||
|
||||
static boolean hasHeader(String data) {
|
||||
|
|
|
@ -0,0 +1,31 @@
|
|||
/*
|
||||
* Copyright (c) 2019-2020 GeyserMC. http://geysermc.org
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
* THE SOFTWARE.
|
||||
*
|
||||
* @author GeyserMC
|
||||
* @link https://github.com/GeyserMC/Geyser
|
||||
*/
|
||||
|
||||
package org.geysermc.floodgate.crypto;
|
||||
|
||||
public interface Topping {
|
||||
byte[] encode(byte[] data);
|
||||
byte[] decode(byte[] data);
|
||||
}
|
|
@ -50,25 +50,23 @@ public final class BedrockData {
|
|||
private final LinkedPlayer linkedPlayer;
|
||||
private final int dataLength;
|
||||
|
||||
private RawSkin skin;
|
||||
|
||||
public BedrockData(String version, String username, String xuid, int deviceOs,
|
||||
String languageCode, int uiProfile, int inputMode, String ip,
|
||||
LinkedPlayer linkedPlayer, RawSkin skin) {
|
||||
LinkedPlayer linkedPlayer) {
|
||||
this(version, username, xuid, deviceOs, languageCode,
|
||||
inputMode, uiProfile, ip, linkedPlayer, EXPECTED_LENGTH, skin);
|
||||
inputMode, uiProfile, ip, linkedPlayer, EXPECTED_LENGTH);
|
||||
}
|
||||
|
||||
public BedrockData(String version, String username, String xuid, int deviceOs,
|
||||
String languageCode, int uiProfile, int inputMode, String ip, RawSkin skin) {
|
||||
this(version, username, xuid, deviceOs, languageCode, uiProfile, inputMode, ip, null, skin);
|
||||
String languageCode, int uiProfile, int inputMode, String ip) {
|
||||
this(version, username, xuid, deviceOs, languageCode, uiProfile, inputMode, ip, null);
|
||||
}
|
||||
|
||||
public boolean hasPlayerLink() {
|
||||
return linkedPlayer != null;
|
||||
}
|
||||
|
||||
public static BedrockData fromString(String data, String skin) {
|
||||
public static BedrockData fromString(String data) {
|
||||
String[] split = data.split("\0");
|
||||
if (split.length != EXPECTED_LENGTH) {
|
||||
return emptyData(split.length);
|
||||
|
@ -79,14 +77,10 @@ public final class BedrockData {
|
|||
return new BedrockData(
|
||||
split[0], split[1], split[2], Integer.parseInt(split[3]), split[4],
|
||||
Integer.parseInt(split[5]), Integer.parseInt(split[6]), split[7],
|
||||
linkedPlayer, split.length, RawSkin.parse(skin)
|
||||
linkedPlayer, split.length
|
||||
);
|
||||
}
|
||||
|
||||
public static BedrockData fromRawData(byte[] data, String skin) {
|
||||
return fromString(new String(data), skin);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
// The format is the same as the order of the fields in this class
|
||||
|
@ -96,6 +90,6 @@ public final class BedrockData {
|
|||
}
|
||||
|
||||
private static BedrockData emptyData(int dataLength) {
|
||||
return new BedrockData(null, null, null, -1, null, -1, -1, null, null, dataLength, null);
|
||||
return new BedrockData(null, null, null, -1, null, -1, -1, null, null, dataLength);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -26,16 +26,26 @@
|
|||
|
||||
package org.geysermc.floodgate.util;
|
||||
|
||||
public class InvalidHeaderException extends Exception {
|
||||
public InvalidHeaderException() {
|
||||
import lombok.Getter;
|
||||
|
||||
@Getter
|
||||
public class InvalidFormatException extends Exception {
|
||||
private boolean header = false;
|
||||
|
||||
public InvalidFormatException() {
|
||||
super();
|
||||
}
|
||||
|
||||
public InvalidHeaderException(String message) {
|
||||
public InvalidFormatException(String message) {
|
||||
super(message);
|
||||
}
|
||||
|
||||
public InvalidHeaderException(String message, Throwable cause) {
|
||||
public InvalidFormatException(String message, boolean header) {
|
||||
super(message);
|
||||
this.header = header;
|
||||
}
|
||||
|
||||
public InvalidFormatException(String message, Throwable cause) {
|
||||
super(message, cause);
|
||||
}
|
||||
}
|
|
@ -26,31 +26,62 @@
|
|||
package org.geysermc.floodgate.util;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.ToString;
|
||||
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.nio.ByteBuffer;
|
||||
import java.util.Base64;
|
||||
|
||||
@AllArgsConstructor
|
||||
public class RawSkin {
|
||||
@ToString
|
||||
public final class RawSkin {
|
||||
public int width;
|
||||
public int height;
|
||||
public byte[] data;
|
||||
|
||||
private RawSkin() {}
|
||||
|
||||
public static RawSkin parse(String data) {
|
||||
if (data == null) return null;
|
||||
String[] split = data.split(":");
|
||||
if (split.length != 3) return null;
|
||||
public static RawSkin decode(byte[] data) throws InvalidFormatException {
|
||||
if (data == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
int maxEncodedLength = 4 * (((64 * 64 * 4 + 8) + 2) / 3);
|
||||
// if the RawSkin is longer then the max Java Edition skin length
|
||||
if (data.length > maxEncodedLength) {
|
||||
throw new InvalidFormatException(
|
||||
"Encoded data cannot be longer then " + maxEncodedLength + " bytes!"
|
||||
);
|
||||
}
|
||||
|
||||
// if the encoded data doesn't even contain the width and height (8 bytes, 2 ints)
|
||||
if (data.length < 4 * ((8 + 2) / 3)) {
|
||||
throw new InvalidFormatException("Encoded data must be at least 12 bytes long!");
|
||||
}
|
||||
|
||||
data = Base64.getDecoder().decode(data);
|
||||
|
||||
ByteBuffer buffer = ByteBuffer.wrap(data);
|
||||
|
||||
RawSkin skin = new RawSkin();
|
||||
skin.width = Integer.parseInt(split[0]);
|
||||
skin.height = Integer.parseInt(split[1]);
|
||||
skin.data = split[2].getBytes(StandardCharsets.UTF_8);
|
||||
skin.width = buffer.getInt();
|
||||
skin.height = buffer.getInt();
|
||||
if (buffer.remaining() != (skin.width * skin.height * 4)) {
|
||||
throw new InvalidFormatException(String.format(
|
||||
"Expected skin length to be %s, got %s",
|
||||
(skin.width * skin.height * 4), buffer.remaining()
|
||||
));
|
||||
}
|
||||
skin.data = new byte[buffer.remaining()];
|
||||
buffer.get(skin.data);
|
||||
return skin;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return Integer.toString(width) + ':' + height + ':' + new String(data);
|
||||
public byte[] encode() {
|
||||
// 2 x int = 8 bytes
|
||||
ByteBuffer buffer = ByteBuffer.allocate(8 + data.length);
|
||||
buffer.putInt(width);
|
||||
buffer.putInt(height);
|
||||
buffer.put(data);
|
||||
return Base64.getEncoder().encode(buffer.array());
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue