Sha256: 9e2b5db6257957ace5c903cb0582e19d5bd3bf405f4f2393b76758688b795306

Contents?: true

Size: 1.67 KB

Versions: 21

Compression:

Stored size: 1.67 KB

Contents

require "base64"
require "openssl"

require "vagrant/util/retryable"

module Vagrant
  module Util
    class Keypair
      extend Retryable

      # Creates an SSH keypair and returns it.
      #
      # @param [String] password Password for the key, or nil for no password.
      # @return [Array<String, String, String>] PEM-encoded public and private key,
      #   respectively. The final element is the OpenSSH encoded public
      #   key.
      def self.create(password=nil)
        # This sometimes fails with RSAError. It is inconsistent and strangely
        # sleeps seem to fix it. We just retry this a few times. See GH-5056
        rsa_key = nil
        retryable(on: OpenSSL::PKey::RSAError, sleep: 2, tries: 5) do
          rsa_key = OpenSSL::PKey::RSA.new(2048)
        end

        public_key  = rsa_key.public_key
        private_key = rsa_key.to_pem

        if password
          cipher      = OpenSSL::Cipher::Cipher.new('des3')
          private_key = rsa_key.to_pem(cipher, password)
        end

        # Generate the binary necessary for the OpenSSH public key.
        binary = [7].pack("N")
        binary += "ssh-rsa"
        ["e", "n"].each do |m|
          val  = public_key.send(m)
          data = val.to_s(2)

          first_byte = data[0,1].unpack("c").first
          if val < 0
            data[0] = [0x80 & first_byte].pack("c")
          elsif first_byte < 0
            data = 0.chr + data
          end

          binary += [data.length].pack("N") + data
        end

        openssh_key = "ssh-rsa #{Base64.encode64(binary).gsub("\n", "")} vagrant"
        public_key  = public_key.to_pem
        return [public_key, private_key, openssh_key]
      end
    end
  end
end

Version data entries

21 entries across 21 versions & 3 rubygems

Version Path
vagrant-aws-mkubenka-0.7.2.pre.22 vendor/bundle/ruby/2.3.0/bundler/gems/vagrant-f3fdbf414272/lib/vagrant/util/keypair.rb
vagrant-aws-mkubenka-0.7.2.pre.16 vendor/bundle/ruby/2.3.0/bundler/gems/vagrant-1ee58c40e3f5/lib/vagrant/util/keypair.rb
vagrant-unbundled-2.0.0.1 lib/vagrant/util/keypair.rb
vagrant-unbundled-1.9.8.1 lib/vagrant/util/keypair.rb
vagrant-unbundled-1.9.7.1 lib/vagrant/util/keypair.rb
vagrant-aws-mkubenka-0.7.2.pre.14 vendor/bundle/ruby/2.3.0/bundler/gems/vagrant-5333e60e2d38/lib/vagrant/util/keypair.rb
vagrant-aws-mkubenka-0.7.2.pre.11 vendor/bundle/ruby/2.3.0/bundler/gems/vagrant-5333e60e2d38/lib/vagrant/util/keypair.rb
vagrant-aws-mkubenka-0.7.2.pre.10 vendor/bundle/ruby/2.3.0/bundler/gems/vagrant-5333e60e2d38/lib/vagrant/util/keypair.rb
vagrant-aws-mkubenka-0.7.2.pre.9 vendor/bundle/ruby/2.3.0/bundler/gems/vagrant-5333e60e2d38/lib/vagrant/util/keypair.rb
vagrant-unbundled-1.9.5.1 lib/vagrant/util/keypair.rb
vagrant-unbundled-1.9.1.1 lib/vagrant/util/keypair.rb
vagrant-compose-yaml-0.1.3 vendor/bundle/ruby/2.2.0/bundler/gems/vagrant-dbb756c7b6da/lib/vagrant/util/keypair.rb
vagrant-compose-yaml-0.1.2 vendor/bundle/ruby/2.2.0/bundler/gems/vagrant-dbb756c7b6da/lib/vagrant/util/keypair.rb
vagrant-compose-yaml-0.1.1 vendor/bundle/ruby/2.2.0/bundler/gems/vagrant-dbb756c7b6da/lib/vagrant/util/keypair.rb
vagrant-compose-yaml-0.1.0 vendor/bundle/ruby/2.2.0/bundler/gems/vagrant-dbb756c7b6da/lib/vagrant/util/keypair.rb
vagrant-unbundled-1.8.5.2 lib/vagrant/util/keypair.rb
vagrant-unbundled-1.8.5.1 lib/vagrant/util/keypair.rb
vagrant-unbundled-1.8.4.2 lib/vagrant/util/keypair.rb
vagrant-unbundled-1.8.4.1 lib/vagrant/util/keypair.rb
vagrant-unbundled-1.8.1.2 lib/vagrant/util/keypair.rb