~ubuntu-branches/ubuntu/trusty/ruby-net-ssh/trusty-proposed

« back to all changes in this revision

Viewing changes to lib/net/ssh/transport/kex/diffie_hellman_group_exchange_sha1.rb

  • Committer: Bazaar Package Importer
  • Author(s): Lucas Nussbaum
  • Date: 2011-04-16 09:31:22 UTC
  • Revision ID: james.westby@ubuntu.com-20110416093122-rs6psd42v2hr371a
Tags: upstream-2.1.4
ImportĀ upstreamĀ versionĀ 2.1.4

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
require 'net/ssh/errors'
 
2
require 'net/ssh/transport/constants'
 
3
require 'net/ssh/transport/kex/diffie_hellman_group1_sha1'
 
4
 
 
5
module Net::SSH::Transport::Kex
 
6
 
 
7
  # A key-exchange service implementing the
 
8
  # "diffie-hellman-group-exchange-sha1" key-exchange algorithm.
 
9
  class DiffieHellmanGroupExchangeSHA1 < DiffieHellmanGroup1SHA1
 
10
    MINIMUM_BITS      = 1024
 
11
    MAXIMUM_BITS      = 8192
 
12
 
 
13
    KEXDH_GEX_GROUP   = 31
 
14
    KEXDH_GEX_INIT    = 32
 
15
    KEXDH_GEX_REPLY   = 33
 
16
    KEXDH_GEX_REQUEST = 34
 
17
 
 
18
    private
 
19
 
 
20
      # Compute the number of bits needed for the given number of bytes.
 
21
      def compute_need_bits
 
22
        need_bits = data[:need_bytes] * 8
 
23
        if need_bits < MINIMUM_BITS
 
24
          need_bits = MINIMUM_BITS
 
25
        elsif need_bits > MAXIMUM_BITS
 
26
          need_bits = MAXIMUM_BITS
 
27
        end
 
28
 
 
29
        data[:need_bits ] = need_bits
 
30
        data[:need_bytes] = need_bits / 8
 
31
      end
 
32
 
 
33
      # Returns the DH key parameters for the given session.
 
34
      def get_parameters
 
35
        compute_need_bits
 
36
 
 
37
        # request the DH key parameters for the given number of bits.
 
38
        buffer = Net::SSH::Buffer.from(:byte, KEXDH_GEX_REQUEST, :long, MINIMUM_BITS,
 
39
          :long, data[:need_bits], :long, MAXIMUM_BITS)
 
40
        connection.send_message(buffer)
 
41
 
 
42
        buffer = connection.next_message
 
43
        unless buffer.type == KEXDH_GEX_GROUP
 
44
          raise Net::SSH::Exception, "expected KEXDH_GEX_GROUP, got #{buffer.type}"
 
45
        end
 
46
 
 
47
        p = buffer.read_bignum
 
48
        g = buffer.read_bignum
 
49
 
 
50
        [p, g]
 
51
      end
 
52
 
 
53
      # Returns the INIT/REPLY constants used by this algorithm.
 
54
      def get_message_types
 
55
        [KEXDH_GEX_INIT, KEXDH_GEX_REPLY]
 
56
      end
 
57
 
 
58
      # Build the signature buffer to use when verifying a signature from
 
59
      # the server.
 
60
      def build_signature_buffer(result)
 
61
        response = Net::SSH::Buffer.new
 
62
        response.write_string data[:client_version_string],
 
63
                              data[:server_version_string],
 
64
                              data[:client_algorithm_packet],
 
65
                              data[:server_algorithm_packet],
 
66
                              result[:key_blob]
 
67
        response.write_long MINIMUM_BITS,
 
68
                            data[:need_bits],
 
69
                            MAXIMUM_BITS
 
70
        response.write_bignum dh.p, dh.g, dh.pub_key,
 
71
                              result[:server_dh_pubkey],
 
72
                              result[:shared_secret]
 
73
        response
 
74
      end
 
75
  end
 
76
 
 
77
end
 
 
b'\\ No newline at end of file'