Class: COSE::Key::EC2

Inherits:
Base
  • Object
show all
Defined in:
lib/cose/key/ec2.rb

Constant Summary collapse

ALG_LABEL =
3
CRV_LABEL =
-1
D_LABEL =
-4
X_LABEL =
-2
Y_LABEL =
-3
KTY_EC2 =
2
CRV_P256 =
1
CRV_P384 =
2
CRV_P521 =
3
PKEY_CURVES =
{
  CRV_P256 => "prime256v1",
  CRV_P384 => "secp384r1",
  CRV_P521 => "secp521r1"
}.freeze

Constants inherited from Base

Base::LABEL_KTY

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

deserialize, enforce_type

Constructor Details

#initialize(algorithm: nil, curve:, d_coordinate: nil, x_coordinate:, y_coordinate:) ⇒ EC2


58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/cose/key/ec2.rb', line 58

def initialize(algorithm: nil, curve:, d_coordinate: nil, x_coordinate:, y_coordinate:)
  if !curve
    raise ArgumentError, "Required curve is missing"
  elsif !x_coordinate
    raise ArgumentError, "Required x-coordinate is missing"
  elsif !y_coordinate
    raise ArgumentError, "Required y-coordinate is missing"
  else
    @algorithm = algorithm
    @curve = curve
    @d_coordinate = d_coordinate
    @x_coordinate = x_coordinate
    @y_coordinate = y_coordinate
  end
end

Instance Attribute Details

#algorithmObject (readonly)

Returns the value of attribute algorithm


56
57
58
# File 'lib/cose/key/ec2.rb', line 56

def algorithm
  @algorithm
end

#curveObject (readonly)

Returns the value of attribute curve


56
57
58
# File 'lib/cose/key/ec2.rb', line 56

def curve
  @curve
end

#d_coordinateObject (readonly)

Returns the value of attribute d_coordinate


56
57
58
# File 'lib/cose/key/ec2.rb', line 56

def d_coordinate
  @d_coordinate
end

#x_coordinateObject (readonly)

Returns the value of attribute x_coordinate


56
57
58
# File 'lib/cose/key/ec2.rb', line 56

def x_coordinate
  @x_coordinate
end

#y_coordinateObject (readonly)

Returns the value of attribute y_coordinate


56
57
58
# File 'lib/cose/key/ec2.rb', line 56

def y_coordinate
  @y_coordinate
end

Class Method Details

.from_map(map) ⇒ Object


102
103
104
105
106
107
108
109
110
111
112
# File 'lib/cose/key/ec2.rb', line 102

def self.from_map(map)
  enforce_type(map, KTY_EC2, "Not an EC2 key")

  new(
    algorithm: map[ALG_LABEL],
    curve: map[CRV_LABEL],
    d_coordinate: map[D_LABEL],
    x_coordinate: map[X_LABEL],
    y_coordinate: map[Y_LABEL]
  )
end

.from_pkey(pkey) ⇒ Object


27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/cose/key/ec2.rb', line 27

def self.from_pkey(pkey)
  curve = PKEY_CURVES.key(pkey.group.curve_name) || raise("Unsupported EC curve #{pkey.group.curve_name}")

  case pkey
  when OpenSSL::PKey::EC::Point
    public_key = pkey
  when OpenSSL::PKey::EC
    public_key = pkey.public_key
    private_key = pkey.private_key
  else
    raise "Unsupported"
  end

  if public_key
    bytes = public_key.to_bn.to_s(2)[1..-1]

    coordinate_length = bytes.size / 2

    x_coordinate = bytes[0..(coordinate_length - 1)]
    y_coordinate = bytes[coordinate_length..-1]
  end

  if private_key
    d_coordinate = private_key.to_s(2)
  end

  new(curve: curve, x_coordinate: x_coordinate, y_coordinate: y_coordinate, d_coordinate: d_coordinate)
end

Instance Method Details

#serializeObject


74
75
76
77
78
79
80
81
82
# File 'lib/cose/key/ec2.rb', line 74

def serialize
  CBOR.encode(
    Base::LABEL_KTY => KTY_EC2,
    CRV_LABEL => curve,
    X_LABEL => x_coordinate,
    Y_LABEL => y_coordinate,
    D_LABEL => d_coordinate
  )
end

#to_pkeyObject


84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/cose/key/ec2.rb', line 84

def to_pkey
  if PKEY_CURVES[curve]
    group = OpenSSL::PKey::EC::Group.new(PKEY_CURVES[curve])
    pkey = OpenSSL::PKey::EC.new(group)
    public_key_bn = OpenSSL::BN.new("\x04" + x_coordinate + y_coordinate, 2)
    public_key_point = OpenSSL::PKey::EC::Point.new(group, public_key_bn)
    pkey.public_key = public_key_point

    if d_coordinate
      pkey.private_key = OpenSSL::BN.new(d_coordinate, 2)
    end

    pkey
  else
    raise "Unsupported curve #{curve}"
  end
end