| 1 | package net.minecraft.network.chat;
|
| 2 |
|
| 3 | import com.mojang.logging.LogUtils;
|
| 4 | import java.time.Instant;
|
| 5 | import java.util.UUID;
|
| 6 | import java.util.function.BooleanSupplier;
|
| 7 | import net.minecraft.util.SignatureValidator;
|
| 8 | import net.minecraft.util.Signer;
|
| 9 | import net.minecraft.world.entity.player.ProfilePublicKey;
|
| 10 | import org.jspecify.annotations.Nullable;
|
| 11 | import org.slf4j.Logger;
|
| 12 |
|
| 13 | public class SignedMessageChain {
|
| 14 | private static final Logger LOGGER = LogUtils.getLogger();
|
| 15 | private @Nullable SignedMessageLink nextLink;
|
| 16 | private Instant lastTimeStamp = Instant.EPOCH;
|
| 17 |
|
| 18 | public SignedMessageChain(final UUID profileId, final UUID sessionId) {
|
| 19 | this.nextLink = SignedMessageLink.root(profileId, sessionId);
|
| 20 | }
|
| 21 |
|
| 22 | public SignedMessageChain.Encoder encoder(final Signer signer) {
|
| 23 | return body -> {
|
| 24 | SignedMessageLink link = this.nextLink;
|
| 25 | if (link == null) {
|
| 26 | return null;
|
| 27 | }
|
| 28 |
|
| 29 | this.nextLink = link.advance();
|
| 30 | return new MessageSignature(signer.sign(output -> PlayerChatMessage.updateSignature(output, link, body)));
|
| 31 | };
|
| 32 | }
|
| 33 |
|
| 34 | public SignedMessageChain.Decoder decoder(final ProfilePublicKey profilePublicKey) {
|
| 35 | final SignatureValidator signatureValidator = profilePublicKey.createSignatureValidator();
|
| 36 | return new SignedMessageChain.Decoder() {
|
| 37 | @Override
|
| 38 | public PlayerChatMessage unpack(final @Nullable MessageSignature signature, final SignedMessageBody body) throws SignedMessageChain.DecodeException {
|
| 39 | if (signature == null) {
|
| 40 | throw new SignedMessageChain.DecodeException(SignedMessageChain.DecodeException.MISSING_PROFILE_KEY);
|
| 41 | }
|
| 42 |
|
| 43 | if (profilePublicKey.data().hasExpired()) {
|
| 44 | throw new SignedMessageChain.DecodeException(SignedMessageChain.DecodeException.EXPIRED_PROFILE_KEY);
|
| 45 | }
|
| 46 |
|
| 47 | SignedMessageLink link = SignedMessageChain.this.nextLink;
|
| 48 | if (link == null) {
|
| 49 | throw new SignedMessageChain.DecodeException(SignedMessageChain.DecodeException.CHAIN_BROKEN);
|
| 50 | }
|
| 51 |
|
| 52 | if (body.timeStamp().isBefore(SignedMessageChain.this.lastTimeStamp)) {
|
| 53 | this.setChainBroken();
|
| 54 | throw new SignedMessageChain.DecodeException(SignedMessageChain.DecodeException.OUT_OF_ORDER_CHAT);
|
| 55 | }
|
| 56 |
|
| 57 | SignedMessageChain.this.lastTimeStamp = body.timeStamp();
|
| 58 | PlayerChatMessage unpacked = new PlayerChatMessage(link, signature, body, null, FilterMask.PASS_THROUGH);
|
| 59 | if (!unpacked.verify(signatureValidator)) {
|
| 60 | this.setChainBroken();
|
| 61 | throw new SignedMessageChain.DecodeException(SignedMessageChain.DecodeException.INVALID_SIGNATURE);
|
| 62 | }
|
| 63 |
|
| 64 | if (unpacked.hasExpiredServer(Instant.now())) {
|
| 65 | SignedMessageChain.LOGGER.warn("Received expired chat: '{}'. Is the client/server system time unsynchronized?", body.content());
|
| 66 | }
|
| 67 |
|
| 68 | SignedMessageChain.this.nextLink = link.advance();
|
| 69 | return unpacked;
|
| 70 | }
|
| 71 |
|
| 72 | @Override
|
| 73 | public void setChainBroken() {
|
| 74 | SignedMessageChain.this.nextLink = null;
|
| 75 | }
|
| 76 | };
|
| 77 | }
|
| 78 |
|
| 79 | public static class DecodeException extends ThrowingComponent {
|
| 80 | private static final Component MISSING_PROFILE_KEY = Component.translatable("chat.disabled.missingProfileKey");
|
| 81 | private static final Component CHAIN_BROKEN = Component.translatable("chat.disabled.chain_broken");
|
| 82 | private static final Component EXPIRED_PROFILE_KEY = Component.translatable("chat.disabled.expiredProfileKey");
|
| 83 | private static final Component INVALID_SIGNATURE = Component.translatable("chat.disabled.invalid_signature");
|
| 84 | private static final Component OUT_OF_ORDER_CHAT = Component.translatable("chat.disabled.out_of_order_chat");
|
| 85 |
|
| 86 | public DecodeException(final Component component) {
|
| 87 | super(component);
|
| 88 | }
|
| 89 | }
|
| 90 |
|
| 91 | @FunctionalInterface
|
| 92 | public interface Decoder {
|
| 93 | static SignedMessageChain.Decoder unsigned(final UUID profileId, final BooleanSupplier enforcesSecureChat) {
|
| 94 | return (signature, body) -> {
|
| 95 | if (enforcesSecureChat.getAsBoolean()) {
|
| 96 | throw new SignedMessageChain.DecodeException(SignedMessageChain.DecodeException.MISSING_PROFILE_KEY);
|
| 97 | } else {
|
| 98 | return PlayerChatMessage.unsigned(profileId, body.content());
|
| 99 | }
|
| 100 | };
|
| 101 | }
|
| 102 |
|
| 103 | PlayerChatMessage unpack(@Nullable MessageSignature signature, SignedMessageBody body) throws SignedMessageChain.DecodeException;
|
| 104 |
|
| 105 | default void setChainBroken() {
|
| 106 | }
|
| 107 | }
|
| 108 |
|
| 109 | @FunctionalInterface
|
| 110 | public interface Encoder {
|
| 111 | SignedMessageChain.Encoder UNSIGNED = body -> null;
|
| 112 |
|
| 113 | @Nullable MessageSignature pack(SignedMessageBody body);
|
| 114 | }
|
| 115 | }
|
| 116 |
|