Sha256: 8edbb75b23f05411041d47b884bfba2b6fe344cee225586e79a95f6b914dae2f

Contents?: true

Size: 1.14 KB

Versions: 1

Compression:

Stored size: 1.14 KB

Contents

require "cbor"
require "cose/key/ec2"
require "cose/key/okp"
require "cose/key/rsa"
require "cose/key/symmetric"
require "openssl"

module COSE
  class UnknownKeyType < StandardError; end

  module Key
    def self.serialize(pkey)
      from_pkey(pkey).serialize
    end

    def self.from_pkey(pkey)
      case pkey
      when OpenSSL::PKey::EC, OpenSSL::PKey::EC::Point
        COSE::Key::EC2.from_pkey(pkey)
      when OpenSSL::PKey::RSA
        COSE::Key::RSA.from_pkey(pkey)
      else
        raise "Unsupported #{pkey.class} object"
      end
    end

    def self.deserialize(data)
      map = CBOR.decode(data)

      case map[Base::LABEL_KTY]
      when COSE::Key::OKP::KTY_OKP
        COSE::Key::OKP.from_map(map)
      when COSE::Key::EC2::KTY_EC2
        COSE::Key::EC2.from_map(map)
      when COSE::Key::RSA::KTY_RSA
        COSE::Key::RSA.from_map(map)
      when COSE::Key::Symmetric::KTY_SYMMETRIC
        COSE::Key::Symmetric.from_map(map)
      when nil
        raise UnknownKeyType, "Missing required key type kty label"
      else
        raise UnknownKeyType, "Unsupported or unknown key type #{map[Base::LABEL_KTY]}"
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
cose-0.8.0 lib/cose/key.rb