13
0
geforkt von Mirrors/Velocity

Avoid a memory copy in creating modern-style forwarding data

Dieser Commit ist enthalten in:
Andrew Steinborn 2019-07-29 06:04:37 -04:00
Ursprung 1da51b8ffb
Commit 04efd16a83

Datei anzeigen

@ -117,32 +117,28 @@ public class LoginSessionHandler implements MinecraftSessionHandler {
private static ByteBuf createForwardingData(byte[] hmacSecret, String address,
GameProfile profile) {
ByteBuf dataToForward = Unpooled.buffer();
ByteBuf finalData = Unpooled.buffer();
ByteBuf forwarded = Unpooled.buffer(2048);
try {
ProtocolUtils.writeVarInt(dataToForward, VelocityConstants.FORWARDING_VERSION);
ProtocolUtils.writeString(dataToForward, address);
ProtocolUtils.writeUuid(dataToForward, profile.getId());
ProtocolUtils.writeString(dataToForward, profile.getName());
ProtocolUtils.writeProperties(dataToForward, profile.getProperties());
ProtocolUtils.writeVarInt(forwarded, VelocityConstants.FORWARDING_VERSION);
ProtocolUtils.writeString(forwarded, address);
ProtocolUtils.writeUuid(forwarded, profile.getId());
ProtocolUtils.writeString(forwarded, profile.getName());
ProtocolUtils.writeProperties(forwarded, profile.getProperties());
SecretKey key = new SecretKeySpec(hmacSecret, "HmacSHA256");
Mac mac = Mac.getInstance("HmacSHA256");
mac.init(key);
mac.update(dataToForward.array(), dataToForward.arrayOffset(), dataToForward.readableBytes());
mac.update(forwarded.array(), forwarded.arrayOffset(), forwarded.readableBytes());
byte[] sig = mac.doFinal();
finalData.writeBytes(sig);
finalData.writeBytes(dataToForward);
return finalData;
return Unpooled.wrappedBuffer(Unpooled.wrappedBuffer(sig), forwarded);
} catch (InvalidKeyException e) {
finalData.release();
forwarded.release();
throw new RuntimeException("Unable to authenticate data", e);
} catch (NoSuchAlgorithmException e) {
// Should never happen
finalData.release();
forwarded.release();
throw new AssertionError(e);
} finally {
dataToForward.release();
}
}
}