Search in sources :

Example 1 with IsaacRandomPair

use of org.apollo.util.security.IsaacRandomPair in project apollo by apollo-rsps.

the class LoginDecoder method decodePayload.

/**
 * Decodes in the payload state.
 *
 * @param ctx The channel handler context.
 * @param buffer The buffer.
 * @param out The {@link List} of objects to pass forward through the pipeline.
 */
private void decodePayload(ChannelHandlerContext ctx, ByteBuf buffer, List<Object> out) {
    if (buffer.readableBytes() >= loginLength) {
        ByteBuf payload = buffer.readBytes(loginLength);
        int version = 255 - payload.readUnsignedByte();
        int release = payload.readUnsignedShort();
        int memoryStatus = payload.readUnsignedByte();
        if (memoryStatus != 0 && memoryStatus != 1) {
            logger.fine("Login memoryStatus (" + memoryStatus + ") not in expected range of [0, 1].");
            writeResponseCode(ctx, LoginConstants.STATUS_LOGIN_SERVER_REJECTED_SESSION);
            return;
        }
        boolean lowMemory = memoryStatus == 1;
        int[] crcs = new int[FileSystemConstants.ARCHIVE_COUNT];
        for (int index = 0; index < 9; index++) {
            crcs[index] = payload.readInt();
        }
        int length = payload.readUnsignedByte();
        if (length != loginLength - 41) {
            logger.fine("Login packet unexpected length (" + length + ")");
            writeResponseCode(ctx, LoginConstants.STATUS_LOGIN_SERVER_REJECTED_SESSION);
            return;
        }
        ByteBuf secure = payload.readBytes(length);
        BigInteger value = new BigInteger(secure.array());
        value = value.modPow(NetworkConstants.RSA_EXPONENT, NetworkConstants.RSA_MODULUS);
        secure = Unpooled.wrappedBuffer(value.toByteArray());
        int id = secure.readUnsignedByte();
        if (id != 10) {
            logger.fine("Unable to read id from secure payload.");
            writeResponseCode(ctx, LoginConstants.STATUS_LOGIN_SERVER_REJECTED_SESSION);
            return;
        }
        long clientSeed = secure.readLong();
        long reportedSeed = secure.readLong();
        if (reportedSeed != serverSeed) {
            logger.fine("Reported seed differed from server seed.");
            writeResponseCode(ctx, LoginConstants.STATUS_LOGIN_SERVER_REJECTED_SESSION);
            return;
        }
        int uid = secure.readInt();
        String username = BufferUtil.readString(secure);
        String password = BufferUtil.readString(secure);
        InetSocketAddress socketAddress = (InetSocketAddress) ctx.channel().remoteAddress();
        String hostAddress = InetAddresses.toAddrString(socketAddress.getAddress());
        if (password.length() < 6 || password.length() > 20 || username.isEmpty() || username.length() > 12) {
            logger.fine("Username ('" + username + "') or password did not pass validation.");
            writeResponseCode(ctx, LoginConstants.STATUS_INVALID_CREDENTIALS);
            return;
        }
        int[] seed = new int[4];
        seed[0] = (int) (clientSeed >> 32);
        seed[1] = (int) clientSeed;
        seed[2] = (int) (serverSeed >> 32);
        seed[3] = (int) serverSeed;
        IsaacRandom decodingRandom = new IsaacRandom(seed);
        for (int index = 0; index < seed.length; index++) {
            seed[index] += 50;
        }
        IsaacRandom encodingRandom = new IsaacRandom(seed);
        PlayerCredentials credentials = new PlayerCredentials(username, password, usernameHash, uid, hostAddress);
        IsaacRandomPair randomPair = new IsaacRandomPair(encodingRandom, decodingRandom);
        out.add(new LoginRequest(credentials, randomPair, reconnecting, lowMemory, release, crcs, version));
    }
}
Also used : InetSocketAddress(java.net.InetSocketAddress) BigInteger(java.math.BigInteger) IsaacRandom(org.apollo.util.security.IsaacRandom) IsaacRandomPair(org.apollo.util.security.IsaacRandomPair) ByteBuf(io.netty.buffer.ByteBuf) PlayerCredentials(org.apollo.util.security.PlayerCredentials)

Example 2 with IsaacRandomPair

use of org.apollo.util.security.IsaacRandomPair in project apollo by apollo-rsps.

the class LoginSession method sendLoginSuccess.

/**
 * Sends a succesfull {@link LoginResponse} to the client.
 *
 * @param player The {@link Player} that successfully logged in.
 */
public void sendLoginSuccess(Player player) {
    IsaacRandomPair randomPair = request.getRandomPair();
    boolean flagged = false;
    GameSession session = new GameSession(channel, context, player, request.isReconnecting());
    channel.attr(ApolloHandler.SESSION_KEY).set(session);
    player.setSession(session);
    int rights = player.getPrivilegeLevel().toInteger();
    channel.writeAndFlush(new LoginResponse(LoginConstants.STATUS_OK, rights, flagged));
    Release release = context.getRelease();
    channel.pipeline().addFirst("messageEncoder", new GameMessageEncoder(release));
    channel.pipeline().addBefore("messageEncoder", "gameEncoder", new GamePacketEncoder(randomPair.getEncodingRandom()));
    channel.pipeline().addBefore("handler", "gameDecoder", new GamePacketDecoder(randomPair.getDecodingRandom(), context.getRelease()));
    channel.pipeline().addAfter("gameDecoder", "messageDecoder", new GameMessageDecoder(release));
    channel.pipeline().remove("loginDecoder");
    channel.pipeline().remove("loginEncoder");
}
Also used : LoginResponse(org.apollo.net.codec.login.LoginResponse) GameMessageDecoder(org.apollo.net.codec.game.GameMessageDecoder) GamePacketEncoder(org.apollo.net.codec.game.GamePacketEncoder) GamePacketDecoder(org.apollo.net.codec.game.GamePacketDecoder) GameMessageEncoder(org.apollo.net.codec.game.GameMessageEncoder) IsaacRandomPair(org.apollo.util.security.IsaacRandomPair) Release(org.apollo.net.release.Release)

Aggregations

IsaacRandomPair (org.apollo.util.security.IsaacRandomPair)2 ByteBuf (io.netty.buffer.ByteBuf)1 BigInteger (java.math.BigInteger)1 InetSocketAddress (java.net.InetSocketAddress)1 GameMessageDecoder (org.apollo.net.codec.game.GameMessageDecoder)1 GameMessageEncoder (org.apollo.net.codec.game.GameMessageEncoder)1 GamePacketDecoder (org.apollo.net.codec.game.GamePacketDecoder)1 GamePacketEncoder (org.apollo.net.codec.game.GamePacketEncoder)1 LoginResponse (org.apollo.net.codec.login.LoginResponse)1 Release (org.apollo.net.release.Release)1 IsaacRandom (org.apollo.util.security.IsaacRandom)1 PlayerCredentials (org.apollo.util.security.PlayerCredentials)1