Class: COSE::SecurityMessage

Inherits:
Object
  • Object
show all
Defined in:
lib/cose/security_message.rb,
lib/cose/security_message/headers.rb

Direct Known Subclasses

Encrypt, Encrypt0, Mac0, Recipient, Sign, Sign1, Signature

Defined Under Namespace

Classes: Headers

Constant Summary collapse

ZERO_LENGTH_BIN_STRING =
"".b

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(protected_headers:, unprotected_headers:) ⇒ SecurityMessage

Returns a new instance of SecurityMessage.



44
45
46
47
# File 'lib/cose/security_message.rb', line 44

def initialize(protected_headers:, unprotected_headers:)
  @protected_headers = protected_headers
  @unprotected_headers = unprotected_headers
end

Instance Attribute Details

#protected_headersObject (readonly)

Returns the value of attribute protected_headers.



12
13
14
# File 'lib/cose/security_message.rb', line 12

def protected_headers
  @protected_headers
end

#unprotected_headersObject (readonly)

Returns the value of attribute unprotected_headers.



12
13
14
# File 'lib/cose/security_message.rb', line 12

def unprotected_headers
  @unprotected_headers
end

Class Method Details

.deserialize(cbor) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/cose/security_message.rb', line 14

def self.deserialize(cbor)
  decoded = CBOR.decode(cbor)

  if decoded.is_a?(CBOR::Tagged)
    if respond_to?(:tag) && tag != decoded.tag
      raise(COSE::Error, "Invalid CBOR tag")
    end

    decoded = decoded.value
  end

  from_array(decoded)
end

.deserialize_headers(data) ⇒ Object



36
37
38
39
40
41
42
# File 'lib/cose/security_message.rb', line 36

def self.deserialize_headers(data)
  if data == ZERO_LENGTH_BIN_STRING
    {}
  else
    CBOR.decode(data)
  end
end

.from_array(array) ⇒ Object



28
29
30
31
32
33
34
# File 'lib/cose/security_message.rb', line 28

def self.from_array(array)
  new(
    protected_headers: deserialize_headers(array[0]),
    unprotected_headers: array[1],
    **keyword_arguments_for_initialize(array[2..-1])
  )
end

Instance Method Details

#algorithmObject



49
50
51
# File 'lib/cose/security_message.rb', line 49

def algorithm
  @algorithm ||= COSE::Algorithm.find(headers.alg) || raise(COSE::Error, "Unsupported algorithm '#{headers.alg}'")
end

#headersObject



53
54
55
# File 'lib/cose/security_message.rb', line 53

def headers
  @headers ||= Headers.new(protected_headers, unprotected_headers)
end