Class: HrrRbSsh::Algorithm::Publickey::SshDss

Inherits:
HrrRbSsh::Algorithm::Publickey show all
Includes:
Loggable
Defined in:
lib/hrr_rb_ssh/algorithm/publickey/ssh_dss.rb,
lib/hrr_rb_ssh/algorithm/publickey/ssh_dss/signature.rb,
lib/hrr_rb_ssh/algorithm/publickey/ssh_dss/public_key_blob.rb

Defined Under Namespace

Classes: PublicKeyBlob, Signature

Constant Summary collapse

NAME =
'ssh-dss'
DIGEST =
'sha1'

Instance Attribute Summary

Attributes included from Loggable

#log_key, #logger

Instance Method Summary collapse

Methods included from Loggable

#log_debug, #log_error, #log_fatal, #log_info, #log_warn

Methods included from SubclassWithoutPreferenceListable

#[], #inherited, #list_supported

Constructor Details

#initialize(arg, logger: nil) ⇒ SshDss

Returns a new instance of SshDss.



15
16
17
18
19
20
21
22
# File 'lib/hrr_rb_ssh/algorithm/publickey/ssh_dss.rb', line 15

def initialize arg, logger: nil
  self.logger = logger
  begin
    new_by_key_str arg
  rescue OpenSSL::PKey::DSAError
    new_by_public_key_blob arg
  end
end

Instance Method Details

#new_by_key_str(key_str) ⇒ Object



24
25
26
# File 'lib/hrr_rb_ssh/algorithm/publickey/ssh_dss.rb', line 24

def new_by_key_str key_str
  @publickey = OpenSSL::PKey::DSA.new(key_str)
end

#new_by_public_key_blob(public_key_blob) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/hrr_rb_ssh/algorithm/publickey/ssh_dss.rb', line 28

def new_by_public_key_blob public_key_blob
  public_key_blob_h = PublicKeyBlob.new(logger: logger).decode public_key_blob
  @publickey = OpenSSL::PKey::DSA.new
  if @publickey.respond_to?(:set_pqg)
    @publickey.set_pqg public_key_blob_h[:'p'], public_key_blob_h[:'q'], public_key_blob_h[:'g']
  else
    @publickey.p = public_key_blob_h[:'p']
    @publickey.q = public_key_blob_h[:'q']
    @publickey.g = public_key_blob_h[:'g']
  end
  if @publickey.respond_to?(:set_key)
    @publickey.set_key public_key_blob_h[:'y'], nil
  else
    @publickey.pub_key = public_key_blob_h[:'y']
  end
end

#sign(signature_blob) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/hrr_rb_ssh/algorithm/publickey/ssh_dss.rb', line 60

def sign signature_blob
  hash = OpenSSL::Digest.digest(self.class::DIGEST, signature_blob)
  sign_der = @publickey.syssign(hash)
  sign_asn1 = OpenSSL::ASN1.decode sign_der
  sign_r = sign_asn1.value[0].value.to_s(2).rjust(20, ["00"].pack("H"))
  sign_s = sign_asn1.value[1].value.to_s(2).rjust(20, ["00"].pack("H"))
  signature_h = {
    :'public key algorithm name' => self.class::NAME,
    :'signature blob'            => (sign_r + sign_s),
  }
  Signature.new(logger: logger).encode signature_h
end

#to_pemObject



45
46
47
# File 'lib/hrr_rb_ssh/algorithm/publickey/ssh_dss.rb', line 45

def to_pem
  @publickey.public_key.to_pem
end

#to_public_key_blobObject



49
50
51
52
53
54
55
56
57
58
# File 'lib/hrr_rb_ssh/algorithm/publickey/ssh_dss.rb', line 49

def to_public_key_blob
  public_key_blob_h = {
    :'public key algorithm name' => self.class::NAME,
    :'p'                         => @publickey.p.to_i,
    :'q'                         => @publickey.q.to_i,
    :'g'                         => @publickey.g.to_i,
    :'y'                         => @publickey.pub_key.to_i,
  }
  PublicKeyBlob.new(logger: logger).encode public_key_blob_h
end

#verify(signature, signature_blob) ⇒ Object



73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/hrr_rb_ssh/algorithm/publickey/ssh_dss.rb', line 73

def verify signature, signature_blob
  signature_h = Signature.new(logger: logger).decode signature
  sign_r = signature_h[:'signature blob'][ 0, 20]
  sign_s = signature_h[:'signature blob'][20, 20]
  sign_asn1 = OpenSSL::ASN1::Sequence.new(
    [
      OpenSSL::ASN1::Integer.new(OpenSSL::BN.new(sign_r, 2)),
      OpenSSL::ASN1::Integer.new(OpenSSL::BN.new(sign_s, 2)),
    ]
  )
  sign_der = sign_asn1.to_der
  hash = OpenSSL::Digest.digest(self.class::DIGEST, signature_blob)
  signature_h[:'public key algorithm name'] == self.class::NAME && @publickey.sysverify(hash, sign_der)
end