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_D_P =
-6
LABEL_D_Q =
-7
LABEL_Q_INV =
-8
KTY_RSA =
3

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(modulus_n:, public_exponent_e:, private_exponent_d: nil, prime_factor_p: nil, prime_factor_q: nil, d_p: nil, d_q: nil, q_inv: nil) ⇒ RSA

Returns a new instance of RSA


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

def initialize(
  modulus_n:,
  public_exponent_e:,
  private_exponent_d: nil,
  prime_factor_p: nil,
  prime_factor_q: nil,
  d_p: nil,
  d_q: nil,
  q_inv: nil
)
  if !modulus_n
    raise ArgumentError, "Required modulus_n is missing"
  elsif !public_exponent_e
    raise ArgumentError, "Required public_exponent_e is missing"
  else
    @modulus_n = modulus_n
    @public_exponent_e = public_exponent_e
    @private_exponent_d = private_exponent_d
    @prime_factor_p = prime_factor_p
    @prime_factor_q = prime_factor_q
    @d_p = d_p
    @d_q = d_q
    @q_inv = q_inv
  end
end

Instance Attribute Details

#d_pObject (readonly)

Returns the value of attribute d_p


42
43
44
# File 'lib/cose/key/rsa.rb', line 42

def d_p
  @d_p
end

#d_qObject (readonly)

Returns the value of attribute d_q


42
43
44
# File 'lib/cose/key/rsa.rb', line 42

def d_q
  @d_q
end

#modulus_nObject (readonly)

Returns the value of attribute modulus_n


42
43
44
# File 'lib/cose/key/rsa.rb', line 42

def modulus_n
  @modulus_n
end

#prime_factor_pObject (readonly)

Returns the value of attribute prime_factor_p


42
43
44
# File 'lib/cose/key/rsa.rb', line 42

def prime_factor_p
  @prime_factor_p
end

#prime_factor_qObject (readonly)

Returns the value of attribute prime_factor_q


42
43
44
# File 'lib/cose/key/rsa.rb', line 42

def prime_factor_q
  @prime_factor_q
end

#private_exponent_dObject (readonly)

Returns the value of attribute private_exponent_d


42
43
44
# File 'lib/cose/key/rsa.rb', line 42

def private_exponent_d
  @private_exponent_d
end

#public_exponent_eObject (readonly)

Returns the value of attribute public_exponent_e


42
43
44
# File 'lib/cose/key/rsa.rb', line 42

def public_exponent_e
  @public_exponent_e
end

#q_invObject (readonly)

Returns the value of attribute q_inv


42
43
44
# File 'lib/cose/key/rsa.rb', line 42

def q_inv
  @q_inv
end

Class Method Details

.from_map(map) ⇒ Object


103
104
105
106
107
# File 'lib/cose/key/rsa.rb', line 103

def self.from_map(map)
  enforce_type(map, KTY_RSA, "Not an RSA key")

  new(modulus_n: map[LABEL_N], public_exponent_e: map[LABEL_E])
end

.from_pkey(pkey) ⇒ Object


20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/cose/key/rsa.rb', line 20

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

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

  if pkey.private?
    attributes.merge!(
      private_exponent_d: params["d"].to_s(2),
      prime_factor_p: params["p"].to_s(2),
      prime_factor_q: params["q"].to_s(2),
      d_p: params["dmp1"].to_s(2),
      d_q: params["dmq1"].to_s(2),
      q_inv: params["iqmp"].to_s(2)
    )
  end

  new(attributes)
end

Instance Method Details

#serializeObject


79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/cose/key/rsa.rb', line 79

def serialize
  CBOR.encode(
    Base::LABEL_KTY => KTY_RSA,
    LABEL_N => modulus_n,
    LABEL_E => public_exponent_e,
    LABEL_D => private_exponent_d,
    LABEL_P => prime_factor_p,
    LABEL_Q => prime_factor_q,
    LABEL_D_P => d_p,
    LABEL_D_Q => d_q,
    LABEL_Q_INV => q_inv
  )
end

#to_pkeyObject


93
94
95
96
97
98
99
100
101
# File 'lib/cose/key/rsa.rb', line 93

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

  pkey.set_key(bn(modulus_n), bn(public_exponent_e), bn(private_exponent_d))
  pkey.set_factors(bn(prime_factor_p), bn(prime_factor_q))
  pkey.set_crt_params(bn(d_p), bn(d_q), bn(q_inv))

  pkey
end