Class CBORDecoder

java.lang.Object
org.bouncycastle.cbor.CBORDecoder

public class CBORDecoder extends Object
A decoder for Deterministically Encoded CBOR (RFC 8949, Sections 4.2.1 and 4.2.2), restricted to the profile needed by CBOR-based PKI formats such as C509 (draft-ietf-cose-cbor-encoded-cert-20).

The decoder is deliberately strict: indefinite-length items, non-shortest-form arguments, floating-point values, simple values other than false, true, null and undefined, malformed UTF-8 in text strings, and maps whose keys are not in bytewise lexicographic order are all rejected with an IOException. Accepting any of these would allow the same data item to have more than one encoding, which for natively signed structures (where the signature is computed over the CBOR bytes) would make signatures malleable.

All read methods honour a throws IOException contract for malformed input; no RuntimeException escapes for any input byte sequence. Declared lengths are validated against the number of bytes actually remaining before any allocation is made, so a short crafted header cannot drive an over-allocation.

  • Constructor Summary

    Constructors
    Constructor
    Description
    CBORDecoder(byte[] encoding)
    Base constructor.
    CBORDecoder(byte[] encoding, int off, int len)
    Constructor for reading a slice of a larger array.
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    Throw an exception unless all input has been consumed.
    boolean
    Return true if at least one more data item (or part of one) is available.
    boolean
    Return true if the next data item is the simple value null (0xf6).
    boolean
    Return true if the next data item is the simple value undefined (0xf7).
    int
    Return the major type (0 to 7) of the next data item without consuming it.
    int
    Read an array header (major type 4), returning the number of elements that follow.
    Read an integer (major type 0 or 1) of any magnitude CBOR supports (-2^64 to 2^64-1).
    boolean
    Read a boolean simple value (0xf4 or 0xf5).
    byte[]
    Read a byte string (major type 2).
    byte[]
    Read one complete data item, of whatever type, returning its encoding verbatim.
    int
    Read an integer (major type 0 or 1) in the range of a Java int.
    long
    Read an integer (major type 0 or 1) in the range of a Java long.
    int
    Read a map header (major type 5), returning the number of key/value pairs that follow.
    void
    Read the simple value null (0xf6).
    long
    Read a tag (major type 6), returning the tag number.
    Read a text string (major type 3), validating that it is well-formed UTF-8.
    void
    Read the simple value undefined (0xf7).
    long
    Read an unsigned integer (major type 0) in the range 0 to 2^63-1.

    Methods inherited from class Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • CBORDecoder

      public CBORDecoder(byte[] encoding)
      Base constructor.
      Parameters:
      encoding - the encoded CBOR data to read from.
    • CBORDecoder

      public CBORDecoder(byte[] encoding, int off, int len)
      Constructor for reading a slice of a larger array.
      Parameters:
      encoding - the array containing the encoded CBOR data.
      off - the offset of the first byte of CBOR data.
      len - the number of bytes of CBOR data.
  • Method Details

    • hasNext

      public boolean hasNext()
      Return true if at least one more data item (or part of one) is available.
    • expectEnd

      public void expectEnd() throws IOException
      Throw an exception unless all input has been consumed.
      Throws:
      IOException
    • peekMajorType

      public int peekMajorType() throws IOException
      Return the major type (0 to 7) of the next data item without consuming it.
      Throws:
      IOException
    • nextIsNull

      public boolean nextIsNull() throws IOException
      Return true if the next data item is the simple value null (0xf6).
      Throws:
      IOException
    • nextIsUndefined

      public boolean nextIsUndefined() throws IOException
      Return true if the next data item is the simple value undefined (0xf7).
      Throws:
      IOException
    • readUnsignedInteger

      public long readUnsignedInteger() throws IOException
      Read an unsigned integer (major type 0) in the range 0 to 2^63-1.
      Throws:
      IOException - if the next item is not an unsigned integer in range.
    • readInteger

      public long readInteger() throws IOException
      Read an integer (major type 0 or 1) in the range of a Java long.
      Throws:
      IOException - if the next item is not an integer, or is outside the long range.
    • readInt

      public int readInt() throws IOException
      Read an integer (major type 0 or 1) in the range of a Java int.
      Throws:
      IOException - if the next item is not an integer, or is outside the int range.
    • readBigInteger

      public BigInteger readBigInteger() throws IOException
      Read an integer (major type 0 or 1) of any magnitude CBOR supports (-2^64 to 2^64-1).
      Throws:
      IOException - if the next item is not an integer.
    • readByteString

      public byte[] readByteString() throws IOException
      Read a byte string (major type 2).
      Throws:
      IOException
    • readTextString

      public String readTextString() throws IOException
      Read a text string (major type 3), validating that it is well-formed UTF-8.
      Throws:
      IOException
    • readArrayHeader

      public int readArrayHeader() throws IOException
      Read an array header (major type 4), returning the number of elements that follow. The count is validated against the bytes remaining (every element occupies at least one byte), so a crafted count cannot drive an oversized allocation in the caller.
      Throws:
      IOException
    • readMapHeader

      public int readMapHeader() throws IOException
      Read a map header (major type 5), returning the number of key/value pairs that follow.
      Throws:
      IOException
    • readTag

      public long readTag() throws IOException
      Read a tag (major type 6), returning the tag number. The tag content follows as the next data item.
      Throws:
      IOException
    • readBoolean

      public boolean readBoolean() throws IOException
      Read a boolean simple value (0xf4 or 0xf5).
      Throws:
      IOException
    • readNull

      public void readNull() throws IOException
      Read the simple value null (0xf6).
      Throws:
      IOException
    • readUndefined

      public void readUndefined() throws IOException
      Read the simple value undefined (0xf7).
      Throws:
      IOException
    • readEncodedItem

      public byte[] readEncodedItem() throws IOException
      Read one complete data item, of whatever type, returning its encoding verbatim. The item is fully validated as it is skipped: all nested items must be deterministically encoded, nesting is bounded, and map keys must be sorted in bytewise lexicographic order of their encodings (RFC 8949 Section 4.2.1).
      Throws:
      IOException