Class: SSHData::PrivateKey::RSA

Inherits:
Base
  • Object
show all
Defined in:
lib/ssh_data/private_key/rsa.rb

Instance Attribute Summary collapse

Attributes inherited from Base

#algo, #comment, #public_key

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#issue_certificate

Constructor Details

#initialize(algo:, n:, e:, d:, iqmp:, p:, q:, comment:) ⇒ RSA

Returns a new instance of RSA.



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/ssh_data/private_key/rsa.rb', line 40

def initialize(algo:, n:, e:, d:, iqmp:, p:, q:, comment:)
  unless algo == PublicKey::ALGO_RSA
    raise DecodeError, "bad algorithm: #{algo.inspect}"
  end

  @n = n
  @e = e
  @d = d
  @iqmp = iqmp
  @p = p
  @q = q

  super(algo: algo, comment: comment)

  @openssl = OpenSSL::PKey::RSA.new(asn1.to_der)

  @public_key = PublicKey::RSA.new(algo: algo, e: e, n: n)
end

Instance Attribute Details

#dObject (readonly)

Returns the value of attribute d.



4
5
6
# File 'lib/ssh_data/private_key/rsa.rb', line 4

def d
  @d
end

#eObject (readonly)

Returns the value of attribute e.



4
5
6
# File 'lib/ssh_data/private_key/rsa.rb', line 4

def e
  @e
end

#iqmpObject (readonly)

Returns the value of attribute iqmp.



4
5
6
# File 'lib/ssh_data/private_key/rsa.rb', line 4

def iqmp
  @iqmp
end

#nObject (readonly)

Returns the value of attribute n.



4
5
6
# File 'lib/ssh_data/private_key/rsa.rb', line 4

def n
  @n
end

#opensslObject (readonly)

Returns the value of attribute openssl.



4
5
6
# File 'lib/ssh_data/private_key/rsa.rb', line 4

def openssl
  @openssl
end

#pObject (readonly)

Returns the value of attribute p.



4
5
6
# File 'lib/ssh_data/private_key/rsa.rb', line 4

def p
  @p
end

#qObject (readonly)

Returns the value of attribute q.



4
5
6
# File 'lib/ssh_data/private_key/rsa.rb', line 4

def q
  @q
end

Class Method Details

.from_openssl(key) ⇒ Object

Import an openssl private key.

key - An OpenSSL::PKey::RSA instance.

Returns a RSA instance.



27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/ssh_data/private_key/rsa.rb', line 27

def self.from_openssl(key)
  new(
    algo: PublicKey::ALGO_RSA,
    n: key.params["n"],
    e: key.params["e"],
    d: key.params["d"],
    iqmp: key.params["iqmp"],
    p: key.params["p"],
    q: key.params["q"],
    comment: "",
  )
end

.generate(size, unsafe_allow_small_key: false) ⇒ Object

Generate a new private key.

size - The Integer key size to generate. unsafe_allow_small_key: - Bool of whether to allow keys of less than

2048 bits.

Returns a PublicKey::Base subclass instance.



14
15
16
17
18
19
20
# File 'lib/ssh_data/private_key/rsa.rb', line 14

def self.generate(size, unsafe_allow_small_key: false)
  unless size >= 2048 || unsafe_allow_small_key
    raise AlgorithmError, "key too small"
  end

  from_openssl(OpenSSL::PKey::RSA.generate(size))
end

Instance Method Details

#sign(signed_data, algo: nil) ⇒ Object

Make an SSH signature.

signed_data - The String message over which to calculated the signature.

Returns a binary String signature.

Raises:



64
65
66
67
68
69
70
# File 'lib/ssh_data/private_key/rsa.rb', line 64

def sign(signed_data, algo: nil)
  algo ||= self.algo
  digest = PublicKey::RSA::ALGO_DIGESTS[algo]
  raise AlgorithmError if digest.nil?
  raw_sig = openssl.sign(digest.new, signed_data)
  Encoding.encode_signature(algo, raw_sig)
end