Skip to content

Commit

Permalink
Load multiple Mojang public keys from JSON file
Browse files Browse the repository at this point in the history
As of 1.20, the game now loads profile property public keys and player
certificate public keys dynamically from
https://api.minecraftservices.com/publickeys. That route is currently
replying with more keys than just Mojang's original `ylB4B6m5` key, so
we should embed all of these into authlib-injector rather than just the
one.

This may not be a good solution long-term if the keys change, ideally
the keys could be fetched dynamically like the vanilla game does.
  • Loading branch information
evan-goode committed Dec 4, 2024
1 parent 7ef9e62 commit 0f75d90
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 13 deletions.
4 changes: 4 additions & 0 deletions src/main/java/moe/yushi/authlibinjector/AuthlibInjector.java
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,10 @@ private static ClassTransformer createTransformer(APIMetadata config) {
transformer.units.add(new YggdrasilKeyTransformUnit());
YggdrasilKeyTransformUnit.PLAYER_CERTIFICATE_PUBLIC_KEYS.addAll(config.getPlayerCertificateKeys());
YggdrasilKeyTransformUnit.PROFILE_PROPERTY_PUBLIC_KEYS.addAll(config.getProfilePropertyKeys());

log(INFO, "Loaded " + YggdrasilKeyTransformUnit.PLAYER_CERTIFICATE_PUBLIC_KEYS.size() + " player certificate public keys");
log(INFO, "Loaded " + YggdrasilKeyTransformUnit.PROFILE_PROPERTY_PUBLIC_KEYS.size() + " profile property public keys");

transformer.units.add(new VelocityProfileKeyTransformUnit());
transformer.units.add(new BungeeCordProfileKeyTransformUnit());
MainArgumentsTransformer.getArgumentsListeners().add(new AccountTypeTransformer()::transform);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,50 +18,77 @@

import static java.lang.invoke.MethodHandles.publicLookup;
import static java.lang.invoke.MethodType.methodType;
import static java.util.Optional.ofNullable;
import static java.util.stream.Collectors.toSet;
import static moe.yushi.authlibinjector.util.IOUtils.asBytes;
import static moe.yushi.authlibinjector.util.IOUtils.asString;
import static moe.yushi.authlibinjector.util.Logging.Level.DEBUG;
import static org.objectweb.asm.Opcodes.ALOAD;
import static org.objectweb.asm.Opcodes.ARETURN;
import static org.objectweb.asm.Opcodes.ASM9;
import static org.objectweb.asm.Opcodes.IRETURN;
import java.io.IOException;
import java.io.InputStream;
import java.io.UncheckedIOException;
import java.lang.invoke.MethodHandle;
import java.security.GeneralSecurityException;
import java.security.InvalidKeyException;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.Signature;
import java.util.Base64;
import java.util.List;
import java.util.HashSet;
import java.util.Set;
import java.util.Optional;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.CopyOnWriteArraySet;
import org.objectweb.asm.ClassVisitor;
import org.objectweb.asm.MethodVisitor;
import moe.yushi.authlibinjector.internal.org.json.simple.JSONArray;
import moe.yushi.authlibinjector.internal.org.json.simple.JSONObject;
import moe.yushi.authlibinjector.transform.CallbackMethod;
import moe.yushi.authlibinjector.transform.TransformContext;
import moe.yushi.authlibinjector.transform.TransformUnit;
import moe.yushi.authlibinjector.util.JsonUtils;
import moe.yushi.authlibinjector.util.KeyUtils;
import moe.yushi.authlibinjector.util.Logging;
import moe.yushi.authlibinjector.util.Logging.Level;

public class YggdrasilKeyTransformUnit implements TransformUnit {

public static final List<PublicKey> PLAYER_CERTIFICATE_PUBLIC_KEYS = new CopyOnWriteArrayList<>();
public static final List<PublicKey> PROFILE_PROPERTY_PUBLIC_KEYS = new CopyOnWriteArrayList<>();
public static final Set<PublicKey> PLAYER_CERTIFICATE_PUBLIC_KEYS = new CopyOnWriteArraySet<>();
public static final Set<PublicKey> PROFILE_PROPERTY_PUBLIC_KEYS = new CopyOnWriteArraySet<>();

static {
PublicKey mojangPublicKey = loadMojangPublicKey();
PLAYER_CERTIFICATE_PUBLIC_KEYS.add(mojangPublicKey);
PROFILE_PROPERTY_PUBLIC_KEYS.add(mojangPublicKey);
// Load Mojang public keys from JSON file obtained from
// https://api.minecraftservices.com/publickeys
try (InputStream in = YggdrasilKeyTransformUnit.class.getResourceAsStream("/mojang_publickeys.json")) {
JSONObject keysJson = JsonUtils.asJsonObject(JsonUtils.parseJson(asString(asBytes(in))));

Set<PublicKey> playerCertificateKeys =
ofNullable(keysJson.get("playerCertificateKeys"))
.map(JsonUtils::asJsonArray)
.map(k -> ParseJSONPublicKeys(k))
.orElseGet(HashSet::new);
PLAYER_CERTIFICATE_PUBLIC_KEYS.addAll(playerCertificateKeys);

Set<PublicKey> profilePropertyKeys =
ofNullable(keysJson.get("profilePropertyKeys"))
.map(JsonUtils::asJsonArray)
.map(k -> ParseJSONPublicKeys(k))
.orElseGet(HashSet::new);
PROFILE_PROPERTY_PUBLIC_KEYS.addAll(profilePropertyKeys);
} catch (IOException | UncheckedIOException e) {
throw new RuntimeException("Failed to load Mojang public keys", e);
}
}

private static PublicKey loadMojangPublicKey() {
try (InputStream in = YggdrasilKeyTransformUnit.class.getResourceAsStream("/mojang_publickey.der")) {
return KeyUtils.parseX509PublicKey(asBytes(in));
} catch (GeneralSecurityException | IOException e) {
throw new RuntimeException("Failed to load Mojang public key", e);
}
private static Set<PublicKey> ParseJSONPublicKeys(JSONArray array) {
return array.stream()
.map(JsonUtils::asJsonObject)
.map(p -> p.get("publicKey"))
.map(JsonUtils::asJsonString)
.map(KeyUtils::parseSignaturePublicKeyBase64DER)
.collect(toSet());
}

@CallbackMethod
Expand Down
8 changes: 8 additions & 0 deletions src/main/java/moe/yushi/authlibinjector/util/KeyUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,14 @@ public static PublicKey parseSignaturePublicKey(String pem) throws UncheckedIOEx
}
}

public static PublicKey parseSignaturePublicKeyBase64DER(String base64Der) throws UncheckedIOException {
try {
return parseX509PublicKey(Base64.getDecoder().decode(base64Der));
} catch (IllegalArgumentException | GeneralSecurityException e) {
throw newUncheckedIOException("Bad signature public key", e);
}
}

private KeyUtils() {}

}
Binary file removed src/main/resources/mojang_publickey.der
Binary file not shown.
12 changes: 12 additions & 0 deletions src/main/resources/mojang_publickeys.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"profilePropertyKeys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