Class: COSE::Key::RSA

Inherits:
COSE::Key show all
Defined in:
lib/cose/key/rsa.rb

Constant Summary collapse

N =
-1
E =
-2
D =
-3
P =
-4
Q =
-5
DP =
-6
DQ =
-7
QI =
-8
ALGS =
{
  PS256: -37,
  PS384: -38,
  PS512: -39,
  RSAES_OAEP_SHA1: -40,
  RSAES_OAEP_SHA256: -41,
  RSAES_OAEP_SHA512: -42
}

Constants inherited from COSE::Key

ALG, BASE_IV, KID, KTY, KTY_EC2, KTY_OKP, KTY_RSA, KTY_SYMMETRIC, OPS

Instance Attribute Summary collapse

Attributes inherited from COSE::Key

#alg, #base_iv, #kid, #kty, #ops, #raw

Instance Method Summary collapse

Methods inherited from COSE::Key

decode, detect

Constructor Details

#initialize(attrs = {}) ⇒ RSA

Returns a new instance of RSA.



24
25
26
27
28
29
30
31
32
33
34
# File 'lib/cose/key/rsa.rb', line 24

def initialize(attrs = {})
  super
  self.n = attrs[N]
  self.e = attrs[E]
  self.d = attrs[D]
  self.p = attrs[P]
  self.q = attrs[Q]
  self.dp = attrs[DP]
  self.dq = attrs[DQ]
  self.qi = attrs[QI]
end

Instance Attribute Details

#dObject

Returns the value of attribute d.



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

def d
  @d
end

#dpObject

Returns the value of attribute dp.



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

def dp
  @dp
end

#dqObject

Returns the value of attribute dq.



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

def dq
  @dq
end

#eObject

Returns the value of attribute e.



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

def e
  @e
end

#nObject

Returns the value of attribute n.



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

def n
  @n
end

#pObject

Returns the value of attribute p.



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

def p
  @p
end

#qObject

Returns the value of attribute q.



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

def q
  @q
end

#qiObject

Returns the value of attribute qi.



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

def qi
  @qi
end

Instance Method Details

#alg_keyObject



36
37
38
39
# File 'lib/cose/key/rsa.rb', line 36

def alg_key
  ALGS.invert[alg] or
  raise UknownAlgorithm, 'Unknown Algorithm'
end

#digestObject



41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/cose/key/rsa.rb', line 41

def digest
  case alg_key
  when :RSAES_OAEP_SHA1
    OpenSSL::Digest::SHA1
  when :PS256, :RSAES_OAEP_SHA256
    OpenSSL::Digest::SHA256
  when :PS384
    OpenSSL::Digest::SHA384
  when :PS512, :RSAES_OAEP_SHA512
    OpenSSL::Digest::SHA512
  end.new
end

#to_keyObject



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/cose/key/rsa.rb', line 54

def to_key
  # Public key
  data_sequence = OpenSSL::ASN1::Sequence([
    OpenSSL::ASN1::Integer(n),
    OpenSSL::ASN1::Integer(e),
  ])

  if d && p && q && dp && dq && qi
    data_sequence = OpenSSL::ASN1::Sequence([
      OpenSSL::ASN1::Integer(0),
      OpenSSL::ASN1::Integer(n),
      OpenSSL::ASN1::Integer(e),
      OpenSSL::ASN1::Integer(d),
      OpenSSL::ASN1::Integer(p),
      OpenSSL::ASN1::Integer(q),
      OpenSSL::ASN1::Integer(dp),
      OpenSSL::ASN1::Integer(dq),
      OpenSSL::ASN1::Integer(qi),
    ])
  end

  asn1 = OpenSSL::ASN1::Sequence(data_sequence)
  OpenSSL::PKey::RSA.new(asn1.to_der)
  key = OpenSSL::PKey::RSA.new
  if key.respond_to? :set_key
    key.set_key n, e, d
    key.set_factors p, q if p && q
    key.set_crt_params dp, dq, qi if dp && dq && qi
  else
    key.e = e
    key.n = n
    key.d = d if d
    key.p = p if p
    key.q = q if q
    key.dmp1 = dp if dp
    key.dmq1 = dq if dq
    key.iqmp = qi if qi
  end
  key
end

#verify(signature, signature_base_string) ⇒ Object



95
96
97
# File 'lib/cose/key/rsa.rb', line 95

def verify(signature, signature_base_string)
  to_key.verify_pss digest, signature, signature_base_string
end