| 1 | package net.minecraft.world.entity.player;
|
| 2 |
|
| 3 | import com.mojang.serialization.Codec;
|
| 4 | import com.mojang.serialization.codecs.RecordCodecBuilder;
|
| 5 | import io.netty.buffer.ByteBuf;
|
| 6 | import java.nio.ByteBuffer;
|
| 7 | import java.nio.ByteOrder;
|
| 8 | import java.security.PublicKey;
|
| 9 | import java.time.Duration;
|
| 10 | import java.time.Instant;
|
| 11 | import java.util.Arrays;
|
| 12 | import java.util.UUID;
|
| 13 | import net.minecraft.network.chat.Component;
|
| 14 | import net.minecraft.network.chat.ThrowingComponent;
|
| 15 | import net.minecraft.network.codec.ByteBufCodecs;
|
| 16 | import net.minecraft.network.codec.StreamCodec;
|
| 17 | import net.minecraft.util.Crypt;
|
| 18 | import net.minecraft.util.ExtraCodecs;
|
| 19 | import net.minecraft.util.SignatureValidator;
|
| 20 |
|
| 21 | public record ProfilePublicKey(ProfilePublicKey.Data data) {
|
| 22 | public static final Component EXPIRED_PROFILE_PUBLIC_KEY = Component.translatable("multiplayer.disconnect.expired_public_key");
|
| 23 | private static final Component INVALID_SIGNATURE = Component.translatable("multiplayer.disconnect.invalid_public_key_signature");
|
| 24 | public static final Duration EXPIRY_GRACE_PERIOD = Duration.ofHours(8L);
|
| 25 | public static final Codec<ProfilePublicKey> TRUSTED_CODEC = ProfilePublicKey.Data.CODEC.xmap(ProfilePublicKey::new, ProfilePublicKey::data);
|
| 26 |
|
| 27 | public static ProfilePublicKey createValidated(final SignatureValidator validator, final UUID profileId, final ProfilePublicKey.Data data) throws ProfilePublicKey.ValidationException {
|
| 28 | if (!data.validateSignature(validator, profileId)) {
|
| 29 | throw new ProfilePublicKey.ValidationException(INVALID_SIGNATURE);
|
| 30 | } else {
|
| 31 | return new ProfilePublicKey(data);
|
| 32 | }
|
| 33 | }
|
| 34 |
|
| 35 | public SignatureValidator createSignatureValidator() {
|
| 36 | return SignatureValidator.from(this.data.key, "SHA256withRSA");
|
| 37 | }
|
| 38 |
|
| 39 | public record Data(Instant expiresAt, PublicKey key, byte[] keySignature) {
|
| 40 | private static final int MAX_KEY_SIGNATURE_SIZE = 4096;
|
| 41 | public static final Codec<ProfilePublicKey.Data> CODEC = RecordCodecBuilder.create(
|
| 42 | i -> i.group(
|
| 43 | ExtraCodecs.INSTANT_ISO8601.fieldOf("expires_at").forGetter(ProfilePublicKey.Data::expiresAt),
|
| 44 | Crypt.PUBLIC_KEY_CODEC.fieldOf("key").forGetter(ProfilePublicKey.Data::key),
|
| 45 | ExtraCodecs.BASE64_STRING.fieldOf("signature_v2").forGetter(ProfilePublicKey.Data::keySignature)
|
| 46 | )
|
| 47 | .apply(i, ProfilePublicKey.Data::new)
|
| 48 | );
|
| 49 | public static final StreamCodec<ByteBuf, ProfilePublicKey.Data> STREAM_CODEC = StreamCodec.composite(
|
| 50 | ByteBufCodecs.INSTANT,
|
| 51 | ProfilePublicKey.Data::expiresAt,
|
| 52 | ByteBufCodecs.PUBLIC_KEY,
|
| 53 | ProfilePublicKey.Data::key,
|
| 54 | ByteBufCodecs.byteArray(4096),
|
| 55 | ProfilePublicKey.Data::keySignature,
|
| 56 | ProfilePublicKey.Data::new
|
| 57 | );
|
| 58 |
|
| 59 | private boolean validateSignature(final SignatureValidator validator, final UUID profileId) {
|
| 60 | return validator.validate(this.signedPayload(profileId), this.keySignature);
|
| 61 | }
|
| 62 |
|
| 63 | private byte[] signedPayload(final UUID profileId) {
|
| 64 | byte[] keyBytes = this.key.getEncoded();
|
| 65 | byte[] signedPayload = new byte[24 + keyBytes.length];
|
| 66 | ByteBuffer buffer = ByteBuffer.wrap(signedPayload).order(ByteOrder.BIG_ENDIAN);
|
| 67 | buffer.putLong(profileId.getMostSignificantBits()).putLong(profileId.getLeastSignificantBits()).putLong(this.expiresAt.toEpochMilli()).put(keyBytes);
|
| 68 | return signedPayload;
|
| 69 | }
|
| 70 |
|
| 71 | public boolean hasExpired() {
|
| 72 | return this.expiresAt.isBefore(Instant.now());
|
| 73 | }
|
| 74 |
|
| 75 | public boolean hasExpired(final Duration gracePeriod) {
|
| 76 | return this.expiresAt.plus(gracePeriod).isBefore(Instant.now());
|
| 77 | }
|
| 78 |
|
| 79 | @Override
|
| 80 | public boolean equals(final Object o) {
|
| 81 | return !(o instanceof ProfilePublicKey.Data data)
|
| 82 | ? false
|
| 83 | : this.expiresAt.equals(data.expiresAt) && this.key.equals(data.key) && Arrays.equals(this.keySignature, data.keySignature);
|
| 84 | }
|
| 85 | }
|
| 86 |
|
| 87 | public static class ValidationException extends ThrowingComponent {
|
| 88 | public ValidationException(final Component component) {
|
| 89 | super(component);
|
| 90 | }
|
| 91 | }
|
| 92 | }
|
| 93 |
|