2020-01-09 03:05:42 +00:00
|
|
|
/*
|
2022-01-01 19:03:05 +00:00
|
|
|
* Copyright (c) 2019-2022 GeyserMC. http://geysermc.org
|
2020-01-09 03:05:42 +00:00
|
|
|
*
|
|
|
|
* 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
|
|
|
|
*/
|
|
|
|
|
2021-11-20 23:29:46 +00:00
|
|
|
package org.geysermc.geyser.util;
|
2019-07-24 22:53:15 +00:00
|
|
|
|
|
|
|
import com.fasterxml.jackson.databind.DeserializationFeature;
|
|
|
|
import com.fasterxml.jackson.databind.JsonNode;
|
|
|
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
2021-01-11 20:52:02 +00:00
|
|
|
import com.github.steveice10.mc.auth.service.MsaAuthenticationService;
|
2022-10-30 00:23:21 +00:00
|
|
|
import org.cloudburstmc.protocol.bedrock.packet.LoginPacket;
|
|
|
|
import org.cloudburstmc.protocol.bedrock.packet.ServerToClientHandshakePacket;
|
2023-06-03 09:47:50 +00:00
|
|
|
import org.cloudburstmc.protocol.bedrock.util.ChainValidationResult;
|
|
|
|
import org.cloudburstmc.protocol.bedrock.util.ChainValidationResult.IdentityData;
|
2022-10-30 00:23:21 +00:00
|
|
|
import org.cloudburstmc.protocol.bedrock.util.EncryptionUtils;
|
2022-01-22 11:20:52 +00:00
|
|
|
import org.geysermc.cumulus.form.ModalForm;
|
|
|
|
import org.geysermc.cumulus.form.SimpleForm;
|
2022-05-28 21:45:35 +00:00
|
|
|
import org.geysermc.cumulus.response.SimpleFormResponse;
|
|
|
|
import org.geysermc.cumulus.response.result.FormResponseResult;
|
|
|
|
import org.geysermc.cumulus.response.result.ValidFormResponseResult;
|
2021-11-20 21:34:30 +00:00
|
|
|
import org.geysermc.geyser.GeyserImpl;
|
2021-11-22 19:52:26 +00:00
|
|
|
import org.geysermc.geyser.session.GeyserSession;
|
2021-11-20 23:29:46 +00:00
|
|
|
import org.geysermc.geyser.session.auth.AuthData;
|
|
|
|
import org.geysermc.geyser.session.auth.BedrockClientData;
|
2022-02-26 20:45:56 +00:00
|
|
|
import org.geysermc.geyser.text.ChatColor;
|
2021-11-20 23:29:46 +00:00
|
|
|
import org.geysermc.geyser.text.GeyserLocale;
|
2019-07-24 22:53:15 +00:00
|
|
|
|
2019-08-02 03:02:17 +00:00
|
|
|
import javax.crypto.SecretKey;
|
|
|
|
import java.security.KeyPair;
|
|
|
|
import java.security.PublicKey;
|
2022-10-30 03:02:11 +00:00
|
|
|
import java.util.List;
|
2022-05-28 21:45:35 +00:00
|
|
|
import java.util.function.BiConsumer;
|
2019-07-24 22:53:15 +00:00
|
|
|
|
|
|
|
public class LoginEncryptionUtils {
|
2019-08-02 03:02:17 +00:00
|
|
|
private static final ObjectMapper JSON_MAPPER = new ObjectMapper().disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
|
2019-07-24 22:53:15 +00:00
|
|
|
|
2021-05-02 17:07:04 +00:00
|
|
|
private static boolean HAS_SENT_ENCRYPTION_MESSAGE = false;
|
|
|
|
|
2021-11-22 19:52:26 +00:00
|
|
|
public static void encryptPlayerConnection(GeyserSession session, LoginPacket loginPacket) {
|
2023-06-03 09:47:50 +00:00
|
|
|
encryptConnectionWithCert(session, loginPacket.getExtra(), loginPacket.getChain());
|
2019-08-02 03:02:17 +00:00
|
|
|
}
|
|
|
|
|
2023-06-03 09:47:50 +00:00
|
|
|
private static void encryptConnectionWithCert(GeyserSession session, String clientData, List<String> certChainData) {
|
2019-08-02 03:02:17 +00:00
|
|
|
try {
|
2021-11-20 21:34:30 +00:00
|
|
|
GeyserImpl geyser = session.getGeyser();
|
2021-08-30 17:55:01 +00:00
|
|
|
|
2023-06-03 09:47:50 +00:00
|
|
|
ChainValidationResult result = EncryptionUtils.validateChain(certChainData);
|
2019-08-02 03:02:17 +00:00
|
|
|
|
2023-06-03 09:47:50 +00:00
|
|
|
geyser.getLogger().debug(String.format("Is player data signed? %s", result.signed()));
|
2019-08-02 03:02:17 +00:00
|
|
|
|
2023-06-03 09:47:50 +00:00
|
|
|
if (!result.signed() && !session.getGeyser().getConfig().isEnableProxyConnections()) {
|
2021-11-20 23:29:46 +00:00
|
|
|
session.disconnect(GeyserLocale.getLocaleStringLog("geyser.network.remote.invalid_xbox_account"));
|
2020-08-12 15:42:02 +00:00
|
|
|
return;
|
|
|
|
}
|
2019-08-02 03:02:17 +00:00
|
|
|
|
2023-06-03 09:47:50 +00:00
|
|
|
IdentityData extraData = result.identityClaims().extraData;
|
|
|
|
session.setAuthenticationData(new AuthData(extraData.displayName, extraData.identity, extraData.xuid));
|
2022-04-21 01:37:50 +00:00
|
|
|
session.setCertChainData(certChainData);
|
|
|
|
|
2023-06-03 09:47:50 +00:00
|
|
|
PublicKey identityPublicKey = result.identityClaims().parsedIdentityPublicKey();
|
2019-08-02 03:02:17 +00:00
|
|
|
|
2023-06-03 09:47:50 +00:00
|
|
|
byte[] clientDataPayload = EncryptionUtils.verifyClientData(clientData, identityPublicKey);
|
|
|
|
if (clientDataPayload == null) {
|
|
|
|
throw new IllegalStateException("Client data isn't signed by the given chain data");
|
|
|
|
}
|
2019-08-02 03:02:17 +00:00
|
|
|
|
2023-06-03 09:47:50 +00:00
|
|
|
JsonNode clientDataJson = JSON_MAPPER.readTree(clientDataPayload);
|
2020-09-19 12:21:54 +00:00
|
|
|
BedrockClientData data = JSON_MAPPER.convertValue(clientDataJson, BedrockClientData.class);
|
2022-04-21 01:37:50 +00:00
|
|
|
data.setOriginalString(clientData);
|
2020-08-20 03:13:05 +00:00
|
|
|
session.setClientData(data);
|
2019-11-30 12:34:45 +00:00
|
|
|
|
2023-04-06 17:26:28 +00:00
|
|
|
try {
|
2023-06-03 09:47:50 +00:00
|
|
|
startEncryptionHandshake(session, identityPublicKey);
|
2023-04-06 17:26:28 +00:00
|
|
|
} catch (Throwable e) {
|
|
|
|
// An error can be thrown on older Java 8 versions about an invalid key
|
|
|
|
if (geyser.getConfig().isDebugMode()) {
|
|
|
|
e.printStackTrace();
|
2021-05-02 17:07:04 +00:00
|
|
|
}
|
2023-04-06 17:26:28 +00:00
|
|
|
|
2021-11-20 21:34:30 +00:00
|
|
|
sendEncryptionFailedMessage(geyser);
|
2019-08-02 03:02:17 +00:00
|
|
|
}
|
|
|
|
} catch (Exception ex) {
|
|
|
|
session.disconnect("disconnectionScreen.internalError.cantConnect");
|
|
|
|
throw new RuntimeException("Unable to complete login", ex);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-22 19:52:26 +00:00
|
|
|
private static void startEncryptionHandshake(GeyserSession session, PublicKey key) throws Exception {
|
2023-06-03 09:47:50 +00:00
|
|
|
KeyPair serverKeyPair = EncryptionUtils.createKeyPair();
|
2019-08-02 03:02:17 +00:00
|
|
|
byte[] token = EncryptionUtils.generateRandomToken();
|
|
|
|
|
|
|
|
ServerToClientHandshakePacket packet = new ServerToClientHandshakePacket();
|
2023-06-03 09:47:50 +00:00
|
|
|
packet.setJwt(EncryptionUtils.createHandshakeJwt(serverKeyPair, token));
|
2020-05-05 15:51:43 +00:00
|
|
|
session.sendUpstreamPacketImmediately(packet);
|
2022-12-23 21:18:48 +00:00
|
|
|
|
|
|
|
SecretKey encryptionKey = EncryptionUtils.getSecretKey(serverKeyPair.getPrivate(), key, token);
|
|
|
|
session.getUpstream().getSession().enableEncryption(encryptionKey);
|
2019-08-02 03:02:17 +00:00
|
|
|
}
|
|
|
|
|
2021-11-20 21:34:30 +00:00
|
|
|
private static void sendEncryptionFailedMessage(GeyserImpl geyser) {
|
2021-05-02 17:07:04 +00:00
|
|
|
if (!HAS_SENT_ENCRYPTION_MESSAGE) {
|
2021-11-20 23:29:46 +00:00
|
|
|
geyser.getLogger().warning(GeyserLocale.getLocaleStringLog("geyser.network.encryption.line_1"));
|
|
|
|
geyser.getLogger().warning(GeyserLocale.getLocaleStringLog("geyser.network.encryption.line_2", "https://geysermc.org/supported_java"));
|
2021-05-02 17:07:04 +00:00
|
|
|
HAS_SENT_ENCRYPTION_MESSAGE = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-22 19:52:26 +00:00
|
|
|
public static void buildAndShowLoginWindow(GeyserSession session) {
|
2021-11-30 03:04:02 +00:00
|
|
|
if (session.isLoggedIn()) {
|
|
|
|
// Can happen if a window is cancelled during dimension switch
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-01-11 20:52:02 +00:00
|
|
|
// Set DoDaylightCycle to false so the time doesn't accelerate while we're here
|
|
|
|
session.setDaylightCycle(false);
|
|
|
|
|
2020-10-29 23:58:17 +00:00
|
|
|
session.sendForm(
|
|
|
|
SimpleForm.builder()
|
2022-01-16 21:09:53 +00:00
|
|
|
.translator(GeyserLocale::getPlayerLocaleString, session.locale())
|
2020-10-29 23:58:17 +00:00
|
|
|
.title("geyser.auth.login.form.notice.title")
|
|
|
|
.content("geyser.auth.login.form.notice.desc")
|
2021-02-01 11:02:49 +00:00
|
|
|
.button("geyser.auth.login.form.notice.btn_login.microsoft")
|
2020-10-29 23:58:17 +00:00
|
|
|
.button("geyser.auth.login.form.notice.btn_disconnect")
|
2022-05-29 21:39:40 +00:00
|
|
|
.closedOrInvalidResultHandler(() -> buildAndShowLoginWindow(session))
|
|
|
|
.validResultHandler((response) -> {
|
|
|
|
if (response.clickedButtonId() == 0) {
|
2022-09-15 19:53:03 +00:00
|
|
|
session.authenticateWithMicrosoftCode();
|
2021-02-01 11:02:49 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-01-16 21:09:53 +00:00
|
|
|
session.disconnect(GeyserLocale.getPlayerLocaleString("geyser.auth.login.form.disconnect", session.locale()));
|
2021-02-01 11:02:49 +00:00
|
|
|
}));
|
2019-08-02 03:02:17 +00:00
|
|
|
}
|
|
|
|
|
2022-03-03 23:52:26 +00:00
|
|
|
/**
|
|
|
|
* Build a window that explains the user's credentials will be saved to the system.
|
|
|
|
*/
|
|
|
|
public static void buildAndShowConsentWindow(GeyserSession session) {
|
2022-03-07 01:35:04 +00:00
|
|
|
String locale = session.locale();
|
2022-06-08 12:09:14 +00:00
|
|
|
|
2022-03-03 23:52:26 +00:00
|
|
|
session.sendForm(
|
|
|
|
SimpleForm.builder()
|
2022-06-08 12:09:14 +00:00
|
|
|
.translator(LoginEncryptionUtils::translate, locale)
|
2022-03-03 23:52:26 +00:00
|
|
|
.title("%gui.signIn")
|
2022-05-28 21:45:35 +00:00
|
|
|
.content("""
|
|
|
|
geyser.auth.login.save_token.warning
|
|
|
|
|
|
|
|
geyser.auth.login.save_token.proceed""")
|
2022-03-03 23:52:26 +00:00
|
|
|
.button("%gui.ok")
|
|
|
|
.button("%gui.decline")
|
2022-05-29 21:39:40 +00:00
|
|
|
.resultHandler(authenticateOrKickHandler(session))
|
2022-05-28 21:45:35 +00:00
|
|
|
);
|
2022-03-03 23:52:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public static void buildAndShowTokenExpiredWindow(GeyserSession session) {
|
2022-03-07 01:35:04 +00:00
|
|
|
String locale = session.locale();
|
2022-06-08 12:09:14 +00:00
|
|
|
|
2022-03-03 23:52:26 +00:00
|
|
|
session.sendForm(
|
|
|
|
SimpleForm.builder()
|
2022-06-08 12:09:14 +00:00
|
|
|
.translator(LoginEncryptionUtils::translate, locale)
|
2022-05-28 21:45:35 +00:00
|
|
|
.title("geyser.auth.login.form.expired")
|
|
|
|
.content("""
|
|
|
|
geyser.auth.login.save_token.expired
|
|
|
|
|
|
|
|
geyser.auth.login.save_token.proceed""")
|
2022-03-03 23:52:26 +00:00
|
|
|
.button("%gui.ok")
|
2022-05-29 21:39:40 +00:00
|
|
|
.resultHandler(authenticateOrKickHandler(session))
|
2022-05-28 21:45:35 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
private static BiConsumer<SimpleForm, FormResponseResult<SimpleFormResponse>> authenticateOrKickHandler(GeyserSession session) {
|
|
|
|
return (form, genericResult) -> {
|
|
|
|
if (genericResult instanceof ValidFormResponseResult<SimpleFormResponse> result &&
|
|
|
|
result.response().clickedButtonId() == 0) {
|
|
|
|
session.authenticateWithMicrosoftCode(true);
|
|
|
|
} else {
|
|
|
|
session.disconnect("%disconnect.quitting");
|
|
|
|
}
|
|
|
|
};
|
2022-03-03 23:52:26 +00:00
|
|
|
}
|
|
|
|
|
2021-01-11 20:52:02 +00:00
|
|
|
/**
|
|
|
|
* Shows the code that a user must input into their browser
|
|
|
|
*/
|
2021-11-22 19:52:26 +00:00
|
|
|
public static void buildAndShowMicrosoftCodeWindow(GeyserSession session, MsaAuthenticationService.MsCodeResponse msCode) {
|
2022-06-08 12:09:14 +00:00
|
|
|
String locale = session.locale();
|
|
|
|
|
2022-02-26 20:45:56 +00:00
|
|
|
StringBuilder message = new StringBuilder("%xbox.signin.website\n")
|
|
|
|
.append(ChatColor.AQUA)
|
|
|
|
.append("%xbox.signin.url")
|
|
|
|
.append(ChatColor.RESET)
|
|
|
|
.append("\n%xbox.signin.enterCode\n")
|
|
|
|
.append(ChatColor.GREEN)
|
|
|
|
.append(msCode.user_code);
|
|
|
|
int timeout = session.getGeyser().getConfig().getPendingAuthenticationTimeout();
|
|
|
|
if (timeout != 0) {
|
|
|
|
message.append("\n\n")
|
|
|
|
.append(ChatColor.RESET)
|
2022-03-07 01:35:04 +00:00
|
|
|
.append(GeyserLocale.getPlayerLocaleString("geyser.auth.login.timeout", session.locale(), String.valueOf(timeout)));
|
2022-02-26 20:45:56 +00:00
|
|
|
}
|
2022-05-28 15:09:20 +00:00
|
|
|
|
2021-02-01 11:02:49 +00:00
|
|
|
session.sendForm(
|
|
|
|
ModalForm.builder()
|
|
|
|
.title("%xbox.signin")
|
2022-02-26 20:45:56 +00:00
|
|
|
.content(message.toString())
|
2021-02-01 11:02:49 +00:00
|
|
|
.button1("%gui.done")
|
|
|
|
.button2("%menu.disconnect")
|
2022-09-15 19:53:03 +00:00
|
|
|
.closedOrInvalidResultHandler(() -> buildAndShowLoginWindow(session))
|
2022-05-29 21:39:40 +00:00
|
|
|
.validResultHandler((response) -> {
|
2022-01-22 11:20:52 +00:00
|
|
|
if (response.clickedButtonId() == 1) {
|
2022-06-08 12:09:14 +00:00
|
|
|
session.disconnect(GeyserLocale.getPlayerLocaleString("geyser.auth.login.form.disconnect", locale));
|
2021-01-11 20:52:02 +00:00
|
|
|
}
|
2021-02-01 11:02:49 +00:00
|
|
|
})
|
2021-02-01 23:46:46 +00:00
|
|
|
);
|
2019-08-02 03:02:17 +00:00
|
|
|
}
|
2022-05-28 21:45:35 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
This checks per line if there is something to be translated, and it skips Bedrock translation keys (%)
|
|
|
|
*/
|
|
|
|
private static String translate(String key, String locale) {
|
|
|
|
StringBuilder newValue = new StringBuilder();
|
|
|
|
int previousIndex = 0;
|
|
|
|
while (previousIndex < key.length()) {
|
|
|
|
int nextIndex = key.indexOf('\n', previousIndex);
|
|
|
|
int endIndex = nextIndex == -1 ? key.length() : nextIndex;
|
|
|
|
|
|
|
|
// if there is more to this line than just a new line char
|
|
|
|
if (endIndex - previousIndex > 1) {
|
|
|
|
String substring = key.substring(previousIndex, endIndex);
|
|
|
|
if (key.charAt(previousIndex) != '%') {
|
|
|
|
newValue.append(GeyserLocale.getPlayerLocaleString(substring, locale));
|
|
|
|
} else {
|
|
|
|
newValue.append(substring);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
newValue.append('\n');
|
|
|
|
|
|
|
|
previousIndex = endIndex + 1;
|
|
|
|
}
|
|
|
|
return newValue.toString();
|
|
|
|
}
|
2019-07-24 22:53:15 +00:00
|
|
|
}
|