Skip to content

COSE::Key::EC2#to_pkey raises NoMethodError for a key that has d but no x and y #84

Open

Description

@santiagorodriguez96

Summary

COSE::Key::EC2.new accepts a private-only key (a key that has d but no x and y). But #to_pkey always builds the public point from x and y, so it fails on that key with a NoMethodError.

A private-only key is valid COSE. RFC 9053, section 7.1.1 says:

For private keys, it is REQUIRED that 'crv' and 'd' be present in the structure. For private keys, it is RECOMMENDED that 'x' and 'y' also be present, but they can be recomputed from the required elements, and omitting them saves on space.

Steps to reproduce

require "cose"

pkey = OpenSSL::PKey::EC.generate("prime256v1")
key = COSE::Key::EC2.new(crv: 1, d: pkey.private_key.to_s(2))

key.to_pkey

Expected behavior

#to_pkey returns an OpenSSL::PKey::EC. The library derives the public point from d.

Actual behavior

NoMethodError: undefined method 'length' for nil
  lib/cose/key/ec2.rb:121:in 'COSE::Key::EC2#pad_coordinate'
  lib/cose/key/ec2.rb:73:in 'COSE::Key::EC2#to_pkey'

When x and y are nil, pad_coordinate calls length on nil and fails.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions