Android Open Source - steamchat Asn Parser






From Project

Back to project page steamchat.

License

The source code is released under:

Apache License

If you think the Android project steamchat listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.kevelbreh.steamchat.steam.security;
/*from   w w w  .  j  av  a2  s. co m*/
import java.util.List;

/**
 * Created by kevin on 2014/08/12.
 */
public class AsnParser {

    final int _initialCount;
    final List<Byte> _octets;

    public AsnParser(List<Byte> values) {
        _octets = values;

        _initialCount = _octets.size();
    }

    public int currentPosition() {
        return _initialCount - _octets.size();
    }

    public int remainingBytes() {
        return _octets.size();
    }

    int GetLength() throws BerDecodeException {
        int length = 0;

        // Checkpoint
        final int position = currentPosition();

        final byte b = GetNextOctet();

        if (b == (b & 0x7f)) {
            return b;
        }
        int i = b & 0x7f;

        if (i > 4) {
            final StringBuilder sb = new StringBuilder("Invalid Length Encoding. ");
            sb.append(String.format("Length uses %d _octets", i));
            throw new BerDecodeException(sb.toString(), position);
        }

        while (0 != i--) {
            // shift left
            length <<= 8;

            length |= GetNextOctet();
        }

        return length & 0xFF;
    }

    public byte[] Next() throws BerDecodeException {
        final int position = currentPosition();

    /*byte b = */GetNextOctet();

        final int length = GetLength();
        if (length > remainingBytes()) {
            final StringBuilder sb = new StringBuilder("Incorrect Size. ");
            sb.append(String.format("Specified: %d, Remaining: %d", length, remainingBytes()));
            throw new BerDecodeException(sb.toString(), position);
        }

        return GetOctets(length);
    }

    public byte GetNextOctet() throws BerDecodeException {
        final int position = currentPosition();

        if (0 == remainingBytes()) {
            final StringBuilder sb = new StringBuilder("Incorrect Size. ");
            sb.append(String.format("Specified: %d, Remaining: %d", "1", remainingBytes()));
            throw new BerDecodeException(sb.toString(), position);
        }

        final byte b = GetOctets(1)[0];

        return b;
    }

    public byte[] GetOctets(int octetCount) throws BerDecodeException {
        final int position = currentPosition();

        if (octetCount > remainingBytes()) {
            final StringBuilder sb = new StringBuilder("Incorrect Size. ");
            sb.append(String.format("Specified: %d, Remaining: %d", octetCount, remainingBytes()));
            throw new BerDecodeException(sb.toString(), position);
        }

        final byte[] values = new byte[octetCount];

        for (int i = 0; i < octetCount; i++) {
            values[i] = _octets.remove(0);
        }

        return values;
    }

    public boolean IsNextNull() {
        return 0x05 == _octets.get(0);
    }

    public int NextNull() throws BerDecodeException {
        final int position = currentPosition();

        byte b = GetNextOctet();
        if (0x05 != b) {
            final StringBuilder sb = new StringBuilder("Expected Null. ");
            sb.append(String.format("Specified Identifier: %d", b));
            throw new BerDecodeException(sb.toString(), position);
        }

        // Next octet must be 0
        b = GetNextOctet();
        if (0x00 != b) {
            final StringBuilder sb = new StringBuilder("Null has non-zero size. ");
            sb.append(String.format("Size: %d", b));
            throw new BerDecodeException(sb.toString(), position);
        }

        return 0;
    }

    public boolean IsNextSequence() {
        return 0x30 == _octets.get(0);
    }

    public int nextSequence() throws BerDecodeException {
        final int position = currentPosition();

        final byte b = GetNextOctet();
        if (0x30 != b) {
            final StringBuilder sb = new StringBuilder("Expected Sequence. ");
            sb.append(String.format("Specified Identifier: %d", b));
            throw new BerDecodeException(sb.toString(), position);
        }

        final int length = GetLength();
        if (length > remainingBytes()) {
            final StringBuilder sb = new StringBuilder("Incorrect Sequence Size. ");
            sb.append(String.format("Specified: %d, Remaining: %d", length, remainingBytes()));
            throw new BerDecodeException(sb.toString(), position);
        }

        return length;
    }

    public boolean IsNextOctetString() {
        return 0x04 == _octets.get(0);
    }

    public int NextOctetString() throws BerDecodeException {
        final int position = currentPosition();

        final byte b = GetNextOctet();
        if (0x04 != b) {
            final StringBuilder sb = new StringBuilder("Expected Octet String. ");
            sb.append(String.format("Specified Identifier: %d", b));
            throw new BerDecodeException(sb.toString(), position);
        }

        final int length = GetLength();
        if (length > remainingBytes()) {
            final StringBuilder sb = new StringBuilder("Incorrect Octet String Size. ");
            sb.append(String.format("Specified: %d, Remaining: %d", length, remainingBytes()));
            throw new BerDecodeException(sb.toString(), position);
        }

        return length;
    }

    public boolean IsNextBitString() {
        return 0x03 == _octets.get(0);
    }

    public int NextBitString() throws BerDecodeException {
        final int position = currentPosition();

        byte b = GetNextOctet();
        if (0x03 != b) {
            final StringBuilder sb = new StringBuilder("Expected Bit String. ");
            sb.append(String.format("Specified Identifier: %d", b));
            throw new BerDecodeException(sb.toString(), position);
        }

        int length = GetLength();

        // We need to consume unused bits, which is the first
        //   octet of the remaing values
        b = _octets.get(0);
        _octets.remove(0);
        length--;

        if (0x00 != b) {
            throw new BerDecodeException("The first octet of BitString must be 0", position);
        }

        return length;
    }

    public boolean IsNextInteger() {
        return 0x02 == _octets.get(0);
    }

    public byte[] nextInteger() throws BerDecodeException {
        final int position = currentPosition();

        final byte b = GetNextOctet();
        if (0x02 != b) {
            final StringBuilder sb = new StringBuilder("Expected Integer. ");
            sb.append(String.format("Specified Identifier: %d", b));
            throw new BerDecodeException(sb.toString(), position);
        }

        final int length = GetLength();
        if (length > remainingBytes()) {
            final StringBuilder sb = new StringBuilder("Incorrect Integer Size. ");
            sb.append(String.format("Specified: %d, Remaining: %d", length, remainingBytes()));
            throw new BerDecodeException(sb.toString(), position);
        }

        return GetOctets(length);
    }

    public byte[] nextOID() throws BerDecodeException {
        final int position = currentPosition();

        final byte b = GetNextOctet();
        if (0x06 != b) {
            final StringBuilder sb = new StringBuilder("Expected Object Identifier. ");
            sb.append(String.format("Specified Identifier: %d", b));
            throw new BerDecodeException(sb.toString(), position);
        }

        final int length = GetLength();
        if (length > remainingBytes()) {
            final StringBuilder sb = new StringBuilder("Incorrect Object Identifier Size. ");
            sb.append(String.format("Specified: %d, Remaining: %d", length, remainingBytes()));
            throw new BerDecodeException(sb.toString(), position);
        }

        final byte[] values = new byte[length];

        for (int i = 0; i < length; i++) {
            values[i] = _octets.get(0);
            _octets.remove(0);
        }

        return values;
    }
}




Java Source Code List

com.kevelbreh.steamchat.SteamChat.java
com.kevelbreh.steamchat.account.AuthenticatorService.java
com.kevelbreh.steamchat.account.Authenticator.java
com.kevelbreh.steamchat.account.SteamAccount.java
com.kevelbreh.steamchat.activity.AuthenticationActivity.java
com.kevelbreh.steamchat.activity.ConversationActivity.java
com.kevelbreh.steamchat.activity.FriendsActivity.java
com.kevelbreh.steamchat.activity.InteractionsActivity.java
com.kevelbreh.steamchat.activity.LauncherActivity.java
com.kevelbreh.steamchat.activity.SettingsActivity.java
com.kevelbreh.steamchat.fragment.ConversationFragment.java
com.kevelbreh.steamchat.fragment.FriendsFragment.java
com.kevelbreh.steamchat.fragment.InteractionsFragment.java
com.kevelbreh.steamchat.fragment.SettingsFragment.java
com.kevelbreh.steamchat.provider.SteamProviderUtils.java
com.kevelbreh.steamchat.provider.SteamProvider.java
com.kevelbreh.steamchat.provider.content.InteractionContentItem.java
com.kevelbreh.steamchat.provider.content.PersonaContentItem.java
com.kevelbreh.steamchat.provider.content.UserContentItem.java
com.kevelbreh.steamchat.steam2.SteamConnection.java
com.kevelbreh.steamchat.steam2.SteamEventBus.java
com.kevelbreh.steamchat.steam2.SteamService.java
com.kevelbreh.steamchat.steam2.handler.ConnectionHandler.java
com.kevelbreh.steamchat.steam2.handler.FriendHandler.java
com.kevelbreh.steamchat.steam2.handler.MessageHandler.java
com.kevelbreh.steamchat.steam2.handler.UserHandler.java
com.kevelbreh.steamchat.steam2.packet.Packet.java
com.kevelbreh.steamchat.steam2.packet.ProtoPacket.java
com.kevelbreh.steamchat.steam.SteamClient.java
com.kevelbreh.steamchat.steam.SteamID.java
com.kevelbreh.steamchat.steam.SteamServiceHandler.java
com.kevelbreh.steamchat.steam.SteamService.java
com.kevelbreh.steamchat.steam.handler2.ConnectionHandler.java
com.kevelbreh.steamchat.steam.handler2.FriendHandler.java
com.kevelbreh.steamchat.steam.handler2.Handler.java
com.kevelbreh.steamchat.steam.handler2.UserHandler.java
com.kevelbreh.steamchat.steam.handler.AEventHandler.java
com.kevelbreh.steamchat.steam.handler.AuthenticationHandler.java
com.kevelbreh.steamchat.steam.handler.FriendsHandler.java
com.kevelbreh.steamchat.steam.handler.IEventHandler.java
com.kevelbreh.steamchat.steam.handler.IHandler.java
com.kevelbreh.steamchat.steam.handler.MessageDebugHandler.java
com.kevelbreh.steamchat.steam.language.Language.java
com.kevelbreh.steamchat.steam.language.Message.java
com.kevelbreh.steamchat.steam.network.TCPConnection.java
com.kevelbreh.steamchat.steam.network.packet.ChannelEncryptRequest.java
com.kevelbreh.steamchat.steam.network.packet.ChannelEncryptResponse.java
com.kevelbreh.steamchat.steam.network.packet.ChannelEncryptResult.java
com.kevelbreh.steamchat.steam.network.packet.ClientLogOnResponse.java
com.kevelbreh.steamchat.steam.network.packet.ClientLogOn.java
com.kevelbreh.steamchat.steam.network.packet.HeartBeat.java
com.kevelbreh.steamchat.steam.network.packet.MultiPacket.java
com.kevelbreh.steamchat.steam.network.packet.Packet.java
com.kevelbreh.steamchat.steam.network.packet.ProtoPacket.java
com.kevelbreh.steamchat.steam.proto.DescriptorsProto.java
com.kevelbreh.steamchat.steam.proto.EncryptedAppTicketProto.java
com.kevelbreh.steamchat.steam.proto.SteamMessagesBaseProto.java
com.kevelbreh.steamchat.steam.proto.SteamMessagesClientServerProto.java
com.kevelbreh.steamchat.steam.security.AsnKeyParser.java
com.kevelbreh.steamchat.steam.security.AsnParser.java
com.kevelbreh.steamchat.steam.security.BerDecodeException.java
com.kevelbreh.steamchat.steam.security.Cryptography.java
com.kevelbreh.steamchat.steam.security.NetEncryption.java
com.kevelbreh.steamchat.steam.security.PublicKey.java
com.kevelbreh.steamchat.steam.security.RSA.java
com.kevelbreh.steamchat.steam.util.BinaryReader.java
com.kevelbreh.steamchat.steam.util.BinaryWriter.java
com.kevelbreh.steamchat.steam.util.BitVector64.java
com.kevelbreh.steamchat.steam.util.JenkinsHash.java
com.kevelbreh.steamchat.util.AServiceActivity.java
com.kevelbreh.steamchat.util.Dump.java
com.kevelbreh.steamchat.util.MiscUtils.java
com.kevelbreh.steamchat.widget.adapter.ChatAdapter.java
com.kevelbreh.steamchat.widget.adapter.ConversationAdapter.java
com.kevelbreh.steamchat.widget.adapter.FriendAdapter.java
com.kevelbreh.steamchat.widget.view.AvatarView.java
com.kevelbreh.steamchat.widget.view.FriendInteractionsView.java