2019-07-24 22:53:15 +00:00
|
|
|
package org.geysermc.connector.utils;
|
|
|
|
|
|
|
|
import com.fasterxml.jackson.databind.DeserializationFeature;
|
|
|
|
import com.fasterxml.jackson.databind.JsonNode;
|
|
|
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
|
|
|
import com.fasterxml.jackson.databind.node.JsonNodeType;
|
|
|
|
import com.nimbusds.jose.JWSObject;
|
|
|
|
import com.nukkitx.network.util.Preconditions;
|
2019-08-02 03:02:17 +00:00
|
|
|
import com.nukkitx.protocol.bedrock.packet.LoginPacket;
|
|
|
|
import com.nukkitx.protocol.bedrock.packet.ServerToClientHandshakePacket;
|
2019-07-24 22:53:15 +00:00
|
|
|
import com.nukkitx.protocol.bedrock.util.EncryptionUtils;
|
2019-12-21 17:35:48 +00:00
|
|
|
|
2019-08-02 03:02:17 +00:00
|
|
|
import net.minidev.json.JSONObject;
|
2019-12-21 17:35:48 +00:00
|
|
|
|
|
|
|
import org.geysermc.common.window.CustomFormBuilder;
|
|
|
|
import org.geysermc.common.window.CustomFormWindow;
|
|
|
|
import org.geysermc.common.window.FormWindow;
|
|
|
|
import org.geysermc.common.window.component.InputComponent;
|
|
|
|
import org.geysermc.common.window.component.LabelComponent;
|
|
|
|
import org.geysermc.common.window.response.CustomFormResponse;
|
2019-08-02 03:02:17 +00:00
|
|
|
import org.geysermc.connector.GeyserConnector;
|
|
|
|
import org.geysermc.connector.network.session.GeyserSession;
|
2019-12-21 17:35:48 +00:00
|
|
|
import org.geysermc.connector.network.session.auth.AuthData;
|
2019-08-02 03:02:17 +00:00
|
|
|
import org.geysermc.connector.network.session.cache.WindowCache;
|
2019-07-24 22:53:15 +00:00
|
|
|
|
2019-08-02 03:02:17 +00:00
|
|
|
import javax.crypto.SecretKey;
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.security.KeyPair;
|
|
|
|
import java.security.KeyPairGenerator;
|
|
|
|
import java.security.PublicKey;
|
2019-07-24 22:53:15 +00:00
|
|
|
import java.security.interfaces.ECPublicKey;
|
2019-08-02 03:02:17 +00:00
|
|
|
import java.security.spec.ECGenParameterSpec;
|
|
|
|
import java.util.UUID;
|
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
|
|
|
|
2019-08-02 03:02:17 +00:00
|
|
|
private static boolean validateChainData(JsonNode data) throws Exception {
|
2019-07-24 22:53:15 +00:00
|
|
|
ECPublicKey lastKey = null;
|
|
|
|
boolean validChain = false;
|
|
|
|
for (JsonNode node : data) {
|
|
|
|
JWSObject jwt = JWSObject.parse(node.asText());
|
|
|
|
|
|
|
|
if (!validChain) {
|
|
|
|
validChain = EncryptionUtils.verifyJwt(jwt, EncryptionUtils.getMojangPublicKey());
|
|
|
|
}
|
|
|
|
|
|
|
|
if (lastKey != null) {
|
|
|
|
EncryptionUtils.verifyJwt(jwt, lastKey);
|
|
|
|
}
|
|
|
|
|
|
|
|
JsonNode payloadNode = JSON_MAPPER.readTree(jwt.getPayload().toString());
|
|
|
|
JsonNode ipkNode = payloadNode.get("identityPublicKey");
|
|
|
|
Preconditions.checkState(ipkNode != null && ipkNode.getNodeType() == JsonNodeType.STRING, "identityPublicKey node is missing in chain");
|
|
|
|
lastKey = EncryptionUtils.generateKey(ipkNode.asText());
|
|
|
|
}
|
|
|
|
return validChain;
|
|
|
|
}
|
2019-08-02 03:02:17 +00:00
|
|
|
|
|
|
|
public static void encryptPlayerConnection(GeyserConnector connector, GeyserSession session, LoginPacket loginPacket) {
|
|
|
|
JsonNode certData;
|
|
|
|
try {
|
|
|
|
certData = JSON_MAPPER.readTree(loginPacket.getChainData().toByteArray());
|
|
|
|
} catch (IOException ex) {
|
|
|
|
throw new RuntimeException("Certificate JSON can not be read.");
|
|
|
|
}
|
|
|
|
|
|
|
|
JsonNode certChainData = certData.get("chain");
|
|
|
|
if (certChainData.getNodeType() != JsonNodeType.ARRAY) {
|
|
|
|
throw new RuntimeException("Certificate data is not valid");
|
|
|
|
}
|
|
|
|
|
2019-08-02 20:55:06 +00:00
|
|
|
encryptConnectionWithCert(connector, session, loginPacket.getSkinData().toString(), certChainData);
|
2019-08-02 03:02:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private static void encryptConnectionWithCert(GeyserConnector connector, GeyserSession session, String playerSkin, JsonNode certChainData) {
|
|
|
|
try {
|
|
|
|
boolean validChain = validateChainData(certChainData);
|
|
|
|
|
|
|
|
connector.getLogger().debug(String.format("Is player data valid? %s", validChain));
|
|
|
|
|
|
|
|
JWSObject jwt = JWSObject.parse(certChainData.get(certChainData.size() - 1).asText());
|
|
|
|
JsonNode payload = JSON_MAPPER.readTree(jwt.getPayload().toBytes());
|
|
|
|
|
|
|
|
if (payload.get("extraData").getNodeType() != JsonNodeType.OBJECT) {
|
|
|
|
throw new RuntimeException("AuthData was not found!");
|
|
|
|
}
|
|
|
|
|
|
|
|
JSONObject extraData = (JSONObject) jwt.getPayload().toJSONObject().get("extraData");
|
2019-12-21 17:35:48 +00:00
|
|
|
session.setAuthenticationData(new AuthData(extraData.getAsString("displayName"), UUID.fromString(extraData.getAsString("identity")), extraData.getAsString("XUID")));
|
2019-08-02 03:02:17 +00:00
|
|
|
|
|
|
|
if (payload.get("identityPublicKey").getNodeType() != JsonNodeType.STRING) {
|
|
|
|
throw new RuntimeException("Identity Public Key was not found!");
|
|
|
|
}
|
|
|
|
|
|
|
|
ECPublicKey identityPublicKey = EncryptionUtils.generateKey(payload.get("identityPublicKey").textValue());
|
|
|
|
JWSObject clientJwt = JWSObject.parse(playerSkin);
|
|
|
|
EncryptionUtils.verifyJwt(clientJwt, identityPublicKey);
|
|
|
|
|
|
|
|
if (EncryptionUtils.canUseEncryption()) {
|
|
|
|
LoginEncryptionUtils.startEncryptionHandshake(session, identityPublicKey);
|
|
|
|
}
|
|
|
|
} catch (Exception ex) {
|
|
|
|
session.disconnect("disconnectionScreen.internalError.cantConnect");
|
|
|
|
throw new RuntimeException("Unable to complete login", ex);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private static void startEncryptionHandshake(GeyserSession session, PublicKey key) throws Exception {
|
|
|
|
KeyPairGenerator generator = KeyPairGenerator.getInstance("EC");
|
|
|
|
generator.initialize(new ECGenParameterSpec("secp384r1"));
|
|
|
|
KeyPair serverKeyPair = generator.generateKeyPair();
|
|
|
|
|
|
|
|
byte[] token = EncryptionUtils.generateRandomToken();
|
|
|
|
SecretKey encryptionKey = EncryptionUtils.getSecretKey(serverKeyPair.getPrivate(), key, token);
|
2019-10-02 20:45:29 +00:00
|
|
|
session.getUpstream().getSession().enableEncryption(encryptionKey);
|
2019-08-02 03:02:17 +00:00
|
|
|
|
|
|
|
ServerToClientHandshakePacket packet = new ServerToClientHandshakePacket();
|
|
|
|
packet.setJwt(EncryptionUtils.createHandshakeJwt(serverKeyPair, token).serialize());
|
|
|
|
session.getUpstream().sendPacketImmediately(packet);
|
|
|
|
}
|
|
|
|
|
|
|
|
private static int AUTH_FORM_ID = 1337;
|
|
|
|
|
|
|
|
public static void showLoginWindow(GeyserSession session) {
|
|
|
|
CustomFormWindow window = new CustomFormBuilder("Login")
|
|
|
|
.addComponent(new LabelComponent("Minecraft: Java Edition account authentication."))
|
|
|
|
.addComponent(new LabelComponent("Enter the credentials for your Minecraft: Java Edition account below."))
|
|
|
|
.addComponent(new InputComponent("Email/Username", "account@geysermc.org", ""))
|
|
|
|
.addComponent(new InputComponent("Password", "123456", ""))
|
|
|
|
.build();
|
|
|
|
|
|
|
|
session.sendForm(window, AUTH_FORM_ID);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static boolean authenticateFromForm(GeyserSession session, GeyserConnector connector, String formData) {
|
|
|
|
WindowCache windowCache = session.getWindowCache();
|
|
|
|
if (!windowCache.getWindows().containsKey(AUTH_FORM_ID))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
FormWindow window = windowCache.getWindows().remove(AUTH_FORM_ID);
|
|
|
|
window.setResponse(formData.trim());
|
|
|
|
|
2019-12-21 17:35:48 +00:00
|
|
|
if (!session.isLoggedIn()) {
|
2019-08-02 03:02:17 +00:00
|
|
|
if (window instanceof CustomFormWindow) {
|
|
|
|
CustomFormWindow customFormWindow = (CustomFormWindow) window;
|
|
|
|
if (!customFormWindow.getTitle().equals("Login"))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
CustomFormResponse response = (CustomFormResponse) customFormWindow.getResponse();
|
2019-08-02 20:54:40 +00:00
|
|
|
if (response != null) {
|
|
|
|
String email = response.getInputResponses().get(2);
|
|
|
|
String password = response.getInputResponses().get(3);
|
|
|
|
|
|
|
|
session.authenticate(email, password);
|
|
|
|
}
|
2019-08-02 03:02:17 +00:00
|
|
|
|
|
|
|
// Clear windows so authentication data isn't accidentally cached
|
|
|
|
windowCache.getWindows().clear();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-07-24 22:53:15 +00:00
|
|
|
}
|