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

Inherits:
HrrRbSsh::Algorithm::Publickey show all
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

Modules: PublicKeyBlob, Signature

Constant Summary collapse

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

Instance Method Summary collapse

Methods included from SubclassWithoutPreferenceListable

#[], #inherited, #list_supported

Constructor Details

#initialize(arg) ⇒ SshDss

Returns a new instance of SshDss.



13
14
15
16
17
18
19
# File 'lib/hrr_rb_ssh/algorithm/publickey/ssh_dss.rb', line 13

def initialize arg
  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



21
22
23
# File 'lib/hrr_rb_ssh/algorithm/publickey/ssh_dss.rb', line 21

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

#new_by_public_key_blob(public_key_blob) ⇒ Object



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

def new_by_public_key_blob public_key_blob
  public_key_blob_h = PublicKeyBlob.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



57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/hrr_rb_ssh/algorithm/publickey/ssh_dss.rb', line 57

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.encode signature_h
end

#to_pemObject



42
43
44
# File 'lib/hrr_rb_ssh/algorithm/publickey/ssh_dss.rb', line 42

def to_pem
  @publickey.public_key.to_pem
end

#to_public_key_blobObject



46
47
48
49
50
51
52
53
54
55
# File 'lib/hrr_rb_ssh/algorithm/publickey/ssh_dss.rb', line 46

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.encode(public_key_blob_h)
end

#verify(signature, signature_blob) ⇒ Object



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

def verify signature, signature_blob
  signature_h = Signature.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