Class: Unmagic::Passkeys::WebAuthn::CborDecoder

Inherits:
Object
  • Object
show all
Defined in:
lib/unmagic/passkeys/web_authn/cbor_decoder.rb

Overview

Action Pack WebAuthn CBOR Decoder

Decodes Concise Binary Object Representation (CBOR) data as specified in RFC 8949. CBOR is a binary data format used by WebAuthn for encoding authenticator data and attestation objects.

Usage

The decoder accepts either a binary string or an array of bytes:

# From binary string
Unmagic::Passkeys::WebAuthn::CborDecoder.decode("\x83\x01\x02\x03")
# => [1, 2, 3]

# From byte array
Unmagic::Passkeys::WebAuthn::CborDecoder.decode([0x83, 0x01, 0x02, 0x03])
# => [1, 2, 3]

Supported Types

The decoder supports the following CBOR types:

Integers

Unsigned (major type 0) and negative (major type 1) integers of any size.

Byte strings

Binary data (major type 2), returned as ASCII-8BIT encoded strings.

Text strings

UTF-8 text (major type 3), returned as UTF-8 encoded strings.

Arrays

Ordered collections (major type 4) of any CBOR values.

Maps

Key-value pairs (major type 5) with any CBOR values as keys and values.

Floats

IEEE 754 half (16-bit), single (32-bit), and double (64-bit) precision.

Simple values

false, true, null, and undefined (both decoded as nil).

Indefinite length

Streaming byte strings, text strings, arrays, and maps.

Tags (major type 6) are recognized but their semantic meaning is ignored; the tagged value is returned directly.

Errors

Raises InvalidCborError when encountering malformed or unsupported CBOR data.

Constant Summary collapse

UNSIGNED_INTEGER_TYPE =

Major types

0
NEGATIVE_INTEGER_TYPE =
1
BYTE_STRING_TYPE =
2
TEXT_STRING_TYPE =
3
ARRAY_TYPE =
4
MAP_TYPE =
5
TAG_TYPE =
6
FLOAT_OR_SIMPLE_TYPE =
7
SIMPLE_VALUE_RANGE =

Additional information values

0..23
SINGLE_BYTE_VALUE_FOLLOWS =
24
TWO_BYTE_VALUE_FOLLOWS =
25
FOUR_BYTE_VALUE_FOLLOWS =
26
EIGHT_BYTE_VALUE_FOLLOWS =
27
RESERVED_VALUE_RANGE =
28..30
INDEFINITE_LENGTH_MAJOR_TYPE =
31
SIMPLE_FALSE_VALUE =

Simple values

20
SIMPLE_TRUE_VALUE =
21
SIMPLE_NULL_VALUE =
22
SIMPLE_UNDEFINED_VALUE =
23
BREAK_CODE =

Flow control

0xFF
MAX_DEPTH =

Limits

16
MAX_SIZE =
10.megabytes
POSITIVE_BIGNUM_TAG =

Tags

2
NEGATIVE_BIGNUM_TAG =
3

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(bytes, max_depth: MAX_DEPTH, max_size: MAX_SIZE) ⇒ CborDecoder

:nodoc:



101
102
103
104
105
106
107
108
# File 'lib/unmagic/passkeys/web_authn/cbor_decoder.rb', line 101

def initialize(bytes, max_depth: MAX_DEPTH, max_size: MAX_SIZE) # :nodoc:
  raise Unmagic::Passkeys::WebAuthn::InvalidCborError, "Input exceeds maximum size" if bytes.length > max_size

  @bytes = bytes
  @max_depth = max_depth
  @position = 0
  @depth = 0
end

Class Method Details

.decode(bytes, **args) ⇒ Object

Decodes a CBOR-encoded byte sequence into a Ruby object.

Unmagic::Passkeys::WebAuthn::CborDecoder.decode("\xa2\x61a\x01\x61b\x02")
# => {"a" => 1, "b" => 2}


95
96
97
98
# File 'lib/unmagic/passkeys/web_authn/cbor_decoder.rb', line 95

def decode(bytes, **args)
  bytes = bytes.bytes if bytes.respond_to?(:bytes)
  new(bytes, **args).decode
end

Instance Method Details

#decodeObject

Decodes the next CBOR data item from the byte sequence.



111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/unmagic/passkeys/web_authn/cbor_decoder.rb', line 111

def decode
  raise Unmagic::Passkeys::WebAuthn::InvalidCborError, "Unexpected end of input" if @position >= @bytes.length
  raise Unmagic::Passkeys::WebAuthn::InvalidCborError, "Maximum nesting depth exceeded" if @depth >= @max_depth

  @depth += 1

  result = case major_type
  when UNSIGNED_INTEGER_TYPE then decode_unsigned_integer
  when NEGATIVE_INTEGER_TYPE then decode_negative_integer
  when BYTE_STRING_TYPE then decode_byte_string
  when TEXT_STRING_TYPE then decode_text_string
  when ARRAY_TYPE then decode_array
  when MAP_TYPE then decode_map
  when TAG_TYPE then decode_tag
  when FLOAT_OR_SIMPLE_TYPE then decode_float_or_simple
  end

  @depth -= 1
  result
end