Class: SSHData::PrivateKey::DSA

Inherits:
Base
  • Object
show all
Defined in:
lib/ssh_data/private_key/dsa.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:, p:, q:, g:, x:, y:, comment:) ⇒ DSA

Returns a new instance of DSA.



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

def initialize(algo:, p:, q:, g:, x:, y:, comment:)
  unless algo == PublicKey::ALGO_DSA
    raise DecodeError, "bad algorithm: #{algo.inspect}"
  end

  @p = p
  @q = q
  @g = g
  @x = x
  @y = y

  super(algo: algo, comment: comment)

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

  @public_key = PublicKey::DSA.new(algo: algo, p: p, q: q, g: g, y: y)
end

Instance Attribute Details

#gObject (readonly)

Returns the value of attribute g.



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

def g
  @g
end

#opensslObject (readonly)

Returns the value of attribute openssl.



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

def openssl
  @openssl
end

#pObject (readonly)

Returns the value of attribute p.



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

def p
  @p
end

#qObject (readonly)

Returns the value of attribute q.



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

def q
  @q
end

#xObject (readonly)

Returns the value of attribute x.



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

def x
  @x
end

#yObject (readonly)

Returns the value of attribute y.



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

def y
  @y
end

Class Method Details

.from_openssl(key) ⇒ Object

Import an openssl private key.

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

Returns a DSA instance.



30
31
32
33
34
35
36
37
38
39
40
# File 'lib/ssh_data/private_key/dsa.rb', line 30

def self.from_openssl(key)
  new(
    algo: PublicKey::ALGO_DSA,
    p: key.params["p"],
    q: key.params["q"],
    g: key.params["g"],
    y: key.params["pub_key"],
    x: key.params["priv_key"],
    comment: "",
  )
end

.generateObject

Generate a new private key.

Returns a PublicKey::Base subclass instance.



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/ssh_data/private_key/dsa.rb', line 9

def self.generate
  openssl_key =
    if defined?(OpenSSL::PKey.generate_parameters)
      dsa_parameters = OpenSSL::PKey.generate_parameters("DSA", {
        dsa_paramgen_bits: 1024,
        dsa_paramgen_q_bits: 160
      })

      OpenSSL::PKey.generate_key(dsa_parameters)
    else
      OpenSSL::PKey::DSA.generate(1024)
    end

  from_openssl(openssl_key)
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:



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

def sign(signed_data, algo: nil)
  algo ||= self.algo
  raise AlgorithmError unless algo == self.algo
  openssl_sig = openssl.sign(OpenSSL::Digest::SHA1.new, signed_data)
  raw_sig = PublicKey::DSA.ssh_signature(openssl_sig)
  Encoding.encode_signature(algo, raw_sig)
end