Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions src/main/java/cn/nukkit/utils/ClientChainData.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import java.security.interfaces.ECPublicKey;
import java.security.spec.InvalidKeySpecException;
import java.security.spec.X509EncodedKeySpec;
import java.time.Instant;
import java.util.*;

/**
Expand Down Expand Up @@ -263,6 +264,7 @@ private boolean verifyChain(List<String> chains) throws Exception {
ECPublicKey lastKey = null;
boolean mojangKeyVerified = false;
Iterator<String> iterator = chains.iterator();
long epoch = Instant.now().getEpochSecond();
while (iterator.hasNext()) {
JWSObject jws = JWSObject.parse(iterator.next());

Expand Down Expand Up @@ -292,6 +294,22 @@ private boolean verifyChain(List<String> chains) throws Exception {
}

Map<String, Object> payload = jws.getPayload().toJSONObject();

// chain expiry check
Object chainExpiresObj = payload.get("exp");
long chainExpires;
if (chainExpiresObj instanceof Long) {
chainExpires = (Long)chainExpiresObj;
} else if (chainExpiresObj instanceof Integer) {
chainExpires = (Integer)chainExpiresObj;
} else {
throw new RuntimeException("Unsupported expiry time format");
}
if (chainExpires < epoch) {
// chain has already expires
return false;
}

Object base64key = payload.get("identityPublicKey");
if (!(base64key instanceof String)) {
throw new RuntimeException("No key found");
Expand Down
3 changes: 2 additions & 1 deletion src/test/java/cn/nukkit/test/ClientChainDataTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,14 @@ void testGetter() throws Exception {
data.getXUID(), data.getCurrentInputMode(),
data.getDefaultInputMode(), data.getUIProfile()
);
// xuid is null because of the chain used for test has already expired
String expecting = "userName=lmlstarqaq, clientUUID=8323afe1-641e-3b61-9a92-d5d20b279065, " +
"identityPublicKey=MHYwEAYHKoZIzj0CAQYFK4EEACIDYgAE4lyvA1iVhV2u3pLQqJAjJnJZSlSjib8mM1uB5h5yqOBSvCHW+nZxDmkOAW6MS1GA7yGHitGmfS4jW/yUISUdWvLzEWJYOzphb3GNh5J1oLJRwESc5278i4MEDk1y21/q, " +
"clientId=-6315607246631494544, " +
"serverAddress=192.168.1.108:19132, deviceModel=iPhone6,2, " +
"deviceOS=2, gameVersion=1.1.0, " +
"guiScale=0, languageCode=zh_CN, " +
"xuid=2535465134455915, currentInputMode=2, " +
"xuid=null, currentInputMode=2, " +
"defaultInputMode=2, UIProfile=1";
assertEquals(got, expecting);
}
Expand Down