Class: COSE::Key::RSA

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

Constant Summary collapse

LABEL_N =
-1
LABEL_E =
-2
LABEL_D =
-3
LABEL_P =
-4
LABEL_Q =
-5
LABEL_DP =
-6
LABEL_DQ =
-7
LABEL_QINV =
-8
KTY_RSA =
3

Constants inherited from Base

Base::LABEL_ALG, Base::LABEL_BASE_IV, Base::LABEL_KEY_OPS, Base::LABEL_KID, Base::LABEL_KTY

Instance Attribute Summary collapse

Attributes inherited from Base

#alg, #base_iv, #key_ops, #kid

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

deserialize, from_map, #serialize

Constructor Details

#initialize(n:, e:, d: nil, p: nil, q: nil, dp: nil, dq: nil, qinv: nil, **keyword_arguments) ⇒ RSA

rubocop:disable Naming/MethodParameterName



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/cose/key/rsa.rb', line 50

def initialize(n:, e:, d: nil, p: nil, q: nil, dp: nil, dq: nil, qinv: nil, **keyword_arguments) # rubocop:disable Naming/MethodParameterName
  super(**keyword_arguments)

  if !n
    raise ArgumentError, "Required public field n is missing"
  elsif !e
    raise ArgumentError, "Required public field e is missing"
  else
    private_fields = { d: d, p: p, q: q, dp: dp, dq: dq, qinv: qinv }

    if private_fields.values.all?(&:nil?) || private_fields.values.none?(&:nil?)
      @n = n
      @e = e
      @d = d
      @p = p
      @q = q
      @dp = dp
      @dq = dq
      @qinv = qinv
    else
      missing = private_fields.detect { |_k, v| v.nil? }[0]
      raise ArgumentError, "Incomplete private fields, #{missing} is missing"
    end
  end
end

Instance Attribute Details

#dObject (readonly)

Returns the value of attribute d.



48
49
50
# File 'lib/cose/key/rsa.rb', line 48

def d
  @d
end

#dpObject (readonly)

Returns the value of attribute dp.



48
49
50
# File 'lib/cose/key/rsa.rb', line 48

def dp
  @dp
end

#dqObject (readonly)

Returns the value of attribute dq.



48
49
50
# File 'lib/cose/key/rsa.rb', line 48

def dq
  @dq
end

#eObject (readonly)

Returns the value of attribute e.



48
49
50
# File 'lib/cose/key/rsa.rb', line 48

def e
  @e
end

#nObject (readonly)

Returns the value of attribute n.



48
49
50
# File 'lib/cose/key/rsa.rb', line 48

def n
  @n
end

#pObject (readonly)

Returns the value of attribute p.



48
49
50
# File 'lib/cose/key/rsa.rb', line 48

def p
  @p
end

#qObject (readonly)

Returns the value of attribute q.



48
49
50
# File 'lib/cose/key/rsa.rb', line 48

def q
  @q
end

#qinvObject (readonly)

Returns the value of attribute qinv.



48
49
50
# File 'lib/cose/key/rsa.rb', line 48

def qinv
  @qinv
end

Class Method Details

.enforce_type(map) ⇒ Object



20
21
22
23
24
# File 'lib/cose/key/rsa.rb', line 20

def self.enforce_type(map)
  if map[LABEL_KTY] != KTY_RSA
    raise "Not an RSA key"
  end
end

.from_pkey(pkey) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/cose/key/rsa.rb', line 26

def self.from_pkey(pkey)
  params = pkey.params

  attributes = {
    n: params["n"].to_s(2),
    e: params["e"].to_s(2)
  }

  if pkey.private?
    attributes.merge!(
      d: params["d"].to_s(2),
      p: params["p"].to_s(2),
      q: params["q"].to_s(2),
      dp: params["dmp1"].to_s(2),
      dq: params["dmq1"].to_s(2),
      qinv: params["iqmp"].to_s(2)
    )
  end

  new(**attributes)
end

.keyword_arguments_for_initialize(map) ⇒ Object



121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/cose/key/rsa.rb', line 121

def self.keyword_arguments_for_initialize(map)
  {
    n: map[LABEL_N],
    e: map[LABEL_E],
    d: map[LABEL_D],
    p: map[LABEL_P],
    q: map[LABEL_Q],
    dp: map[LABEL_DP],
    dq: map[LABEL_DQ],
    qinv: map[LABEL_QINV]
  }
end

Instance Method Details

#mapObject



76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/cose/key/rsa.rb', line 76

def map
  super.merge(
    Base::LABEL_KTY => KTY_RSA,
    LABEL_N => n,
    LABEL_E => e,
    LABEL_D => d,
    LABEL_P => p,
    LABEL_Q => q,
    LABEL_DP => dp,
    LABEL_DQ => dq,
    LABEL_QINV => qinv
  ).compact
end

#to_pkeyObject



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/cose/key/rsa.rb', line 90

def to_pkey
  pkey = OpenSSL::PKey::RSA.new

  if pkey.respond_to?(:set_key)
    pkey.set_key(bn(n), bn(e), bn(d))
  else
    pkey.n = bn(n)
    pkey.e = bn(e)
    pkey.d = bn(d)
  end

  if private?
    if pkey.respond_to?(:set_factors)
      pkey.set_factors(bn(p), bn(q))
    else
      pkey.p = bn(p)
      pkey.q = bn(q)
    end

    if pkey.respond_to?(:set_crt_params)
      pkey.set_crt_params(bn(dp), bn(dq), bn(qinv))
    else
      pkey.dmp1 = bn(dp)
      pkey.dmq1 = bn(dq)
      pkey.iqmp = bn(qinv)
    end
  end

  pkey
end