Sha256: 9dd77329dd9c151f69896793cf74df90441071892943896e0dc902ad0c43f7f5

Contents?: true

Size: 1.25 KB

Versions: 9

Compression:

Stored size: 1.25 KB

Contents

# frozen_string_literal: true

require "cbor"
require "cose/error"
require "cose/security_message"
require "cose/signature"

module COSE
  class Sign < SecurityMessage
    CONTEXT = "Signature"

    attr_reader :payload, :signatures

    def self.keyword_arguments_for_initialize(decoded)
      { payload: decoded[0], signatures: decoded[1].map { |s| COSE::Signature.from_array(s) } }
    end

    def self.tag
      98
    end

    def initialize(payload:, signatures:, **keyword_arguments)
      super(**keyword_arguments)

      @payload = payload
      @signatures = signatures
    end

    def verify(key, external_aad = nil)
      signature = signatures.detect { |s| s.headers.kid == key.kid }

      if signature
        signature.algorithm.verify(key, signature.signature, verification_data(signature, external_aad))
      else
        raise(COSE::Error, "No signature matches key kid")
      end
    end

    private

    def verification_data(signature, external_aad = nil)
      @verification_data ||=
        CBOR.encode(
          [
            CONTEXT,
            serialized_map(protected_headers),
            serialized_map(signature.protected_headers),
            external_aad || ZERO_LENGTH_BIN_STRING,
            payload
          ]
        )
    end
  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
cose-1.3.1 lib/cose/sign.rb
cose-1.3.0 lib/cose/sign.rb
cose-1.2.1 lib/cose/sign.rb
cose-1.2.0 lib/cose/sign.rb
cose-1.1.0 lib/cose/sign.rb
cose-1.0.0 lib/cose/sign.rb
cose-0.11.0 lib/cose/sign.rb
cose-0.10.0 lib/cose/sign.rb
cose-0.9.0 lib/cose/sign.rb