Class: Net::SSH::KeyFactory

### Home Classes Methods

### In Files

- lib/net/ssh/key_factory.rb

### Parent

Object

### Methods

- ::get
- ::load_data_private_key
- ::load_data_public_key
- ::load_private_key
- ::load_public_key

### Included Modules

- Prompt

### Files

- README.rdoc

### Class/Module Index

Quicksearch

- Net
- Net::SSH
- Net::SSH::Authentication
- Net::SSH::Authentication::Agent
- Net::SSH::Authentication::Agent::Comment
- Net::SSH::Authentication::AgentError
- Net::SSH::Authentication::AgentNotAvailable
- Net::SSH::Authentication::Constants
- Net::SSH::Authentication::DisallowedMethod
- Net::SSH::Authentication::KeyManager
- Net::SSH::Authentication::KeyManagerError
- Net::SSH::Authentication::Methods
- Net::SSH::Authentication::Methods::Abstract
- Net::SSH::Authentication::Methods::Hostbased
- Net::SSH::Authentication::Methods::KeyboardInteractive
- Net::SSH::Authentication::Methods::None
- Net::SSH::Authentication::Methods::Password
- Net::SSH::Authentication::Methods::Publickey
- Net::SSH::Authentication::Session
- Net::SSH::AuthenticationFailed
- Net::SSH::Buffer
- Net::SSH::ChannelOpenFailed
- Net::SSH::ChannelRequestFailed
- Net::SSH::Compat
- Net::SSH::Config
- Net::SSH::Connection
- Net::SSH::Connection::Channel
- Net::SSH::Connection::Constants
- Net::SSH::Connection::Keepalive
- Net::SSH::Connection::Session
- Net::SSH::Connection::Session::NilChannel
- Net::SSH::Connection::Term
- Net::SSH::ConnectionTimeout
- Net::SSH::Disconnect
- Net::SSH::Exception
- Net::SSH::HostKeyError
- Net::SSH::HostKeyMismatch
- Net::SSH::HostKeyUnknown
- Net::SSH::KeyFactory
- Net::SSH::KnownHosts
- Net::SSH::Loggable
- Net::SSH::Packet
- Net::SSH::PromptMethods
- Net::SSH::PromptMethods::Clear
- Net::SSH::PromptMethods::Highline
- Net::SSH::PromptMethods::Termios
- Net::SSH::Proxy
- Net::SSH::Proxy::Command
- Net::SSH::Proxy::ConnectError
- Net::SSH::Proxy::Error
- Net::SSH::Proxy::HTTP
- Net::SSH::Proxy::SOCKS4
- Net::SSH::Proxy::SOCKS5
- Net::SSH::Proxy::UnauthorizedError
- Net::SSH::Test
- Net::SSH::Test::Channel
- Net::SSH::Test::Extensions
- Net::SSH::Test::Extensions::BufferedIo
- Net::SSH::Test::Extensions::Channel
- Net::SSH::Test::Extensions::IO
- Net::SSH::Test::Extensions::IO::ClassMethods
- Net::SSH::Test::Extensions::PacketStream
- Net::SSH::Test::Kex
- Net::SSH::Test::LocalPacket
- Net::SSH::Test::Packet
- Net::SSH::Test::RemotePacket
- Net::SSH::Test::Script
- Net::SSH::Test::Socket
- Net::SSH::Timeout
- Net::SSH::Transport
- Net::SSH::Transport::Algorithms
- Net::SSH::Transport::CTR
- Net::SSH::Transport::CipherFactory
- Net::SSH::Transport::Constants
- Net::SSH::Transport::HMAC
- Net::SSH::Transport::HMAC::Abstract
- Net::SSH::Transport::HMAC::MD5
- Net::SSH::Transport::HMAC::MD5_96
- Net::SSH::Transport::HMAC::None
- Net::SSH::Transport::HMAC::RIPEMD160
- Net::SSH::Transport::HMAC::SHA1
- Net::SSH::Transport::HMAC::SHA1_96
- Net::SSH::Transport::HMAC::SHA2_256
- Net::SSH::Transport::HMAC::SHA2_256_96
- Net::SSH::Transport::HMAC::SHA2_512
- Net::SSH::Transport::HMAC::SHA2_512_96
- Net::SSH::Transport::IdentityCipher
- Net::SSH::Transport::Kex
- Net::SSH::Transport::Kex::DiffieHellmanGroup14SHA1
- Net::SSH::Transport::Kex::DiffieHellmanGroup1SHA1
- Net::SSH::Transport::Kex::DiffieHellmanGroupExchangeSHA1
- Net::SSH::Transport::Kex::DiffieHellmanGroupExchangeSHA256
- Net::SSH::Transport::Kex::EcdhSHA2NistP256
- Net::SSH::Transport::Kex::EcdhSHA2NistP384
- Net::SSH::Transport::Kex::EcdhSHA2NistP521
- Net::SSH::Transport::KeyExpander
- Net::SSH::Transport::PacketStream
- Net::SSH::Transport::ServerVersion
- Net::SSH::Transport::Session
- Net::SSH::Transport::State
- Net::SSH::Verifiers
- Net::SSH::Verifiers::Lenient
- Net::SSH::Verifiers::Null
- Net::SSH::Verifiers::Secure
- Net::SSH::Verifiers::Strict
- Net::SSH::Version
- OpenSSL
- OpenSSL::BN
- OpenSSL::PKey
- OpenSSL::PKey::DH
- OpenSSL::PKey::DSA
- OpenSSL::PKey::EC
- OpenSSL::PKey::OpenSSL
- OpenSSL::PKey::OpenSSL::PKey
- OpenSSL::PKey::OpenSSL::PKey::OpenSSL
- OpenSSL::PKey::OpenSSL::PKey::OpenSSL::PKey
- OpenSSL::PKey::OpenSSL::PKey::OpenSSL::PKey::ECError
- OpenSSL::PKey::PKey
- OpenSSL::PKey::RSA
- String

No matching classes.

# Net::SSH::KeyFactory

A factory class for returning new Key classes. It is used for obtaining OpenSSL key instances via their SSH names, and for loading both public and private keys. It used used primarily by Net::SSH itself, internally, and will rarely (if ever) be directly used by consumers of the library.

    klass = Net::SSH::KeyFactory.get("rsa")
    assert klass.is_a?(OpenSSL::PKey::RSA)

    key = Net::SSH::KeyFactory.load_public_key("~/.ssh/id_dsa.pub")

### Constants

MAP

  Specifies the mapping of SSH names to OpenSSL key classes.

### Public Class Methods

get(name) click to toggle source

Fetch an OpenSSL key instance by its SSH name. It will be a new, empty key of the given type.

    # File lib/net/ssh/key_factory.rb, line 31
    def get(name)
      MAP.fetch(name).new
    end

load_data_private_key(data, passphrase=nil, ask_passphrase=true, filename="") click to toggle source

Loads a private key. It will correctly determine whether the file describes an RSA or DSA key, and will load it appropriately. The new key is returned. If the key itself is encrypted (requiring a passphrase to use), the user will be prompted to enter their password unless passphrase works.

    # File lib/net/ssh/key_factory.rb, line 50
    def load_data_private_key(data, passphrase=nil, ask_passphrase=true, filename="")
      if OpenSSL::PKey.respond_to?(:read)
        pkey_read = true
        error_class = ArgumentError
      else
        pkey_read = false
        if data.match(/-----BEGIN DSA PRIVATE KEY-----/)
          key_type = OpenSSL::PKey::DSA
          error_class = OpenSSL::PKey::DSAError
        elsif data.match(/-----BEGIN RSA PRIVATE KEY-----/)
          key_type = OpenSSL::PKey::RSA
          error_class = OpenSSL::PKey::RSAError
        elsif data.match(/-----BEGIN EC PRIVATE KEY-----/) && defined?(OpenSSL::PKey::EC)
          key_type = OpenSSL::PKey::EC
          error_class = OpenSSL::PKey::ECError
        elsif data.match(/-----BEGIN (.+) PRIVATE KEY-----/)
          raise OpenSSL::PKey::PKeyError, "not a supported key type '#{$1}'"
        else
          raise OpenSSL::PKey::PKeyError, "not a private key (#{filename})"
        end
      end

      encrypted_key = data.match(/ENCRYPTED/)
      tries = 0

      begin
        if pkey_read
          return OpenSSL::PKey.read(data, passphrase || 'invalid')
        else
          return key_type.new(data, passphrase || 'invalid')
        end
      rescue error_class
        if encrypted_key && ask_passphrase
          tries += 1
          if tries <= 3
            passphrase = prompt("Enter passphrase for #{filename}:", false)
            retry
          else
            raise
          end
        else
          raise
        end
      end
    end

load_data_public_key(data, filename="") click to toggle source

Loads a public key. It will correctly determine whether the file describes an RSA or DSA key, and will load it appropriately. The new public key is returned.

    # File lib/net/ssh/key_factory.rb, line 107
    def load_data_public_key(data, filename="")
      fields = data.split(/ /)

      blob = nil
      begin
        blob = fields.shift
      end while !blob.nil? && !/^(ssh-(rsa|dss)|ecdsa-sha2-nistp\d+)$/.match(blob)
      blob = fields.shift

      raise Net::SSH::Exception, "public key at #{filename} is not valid" if blob.nil?

      blob = blob.unpack("m*").first
      reader = Net::SSH::Buffer.new(blob)
      reader.read_key or raise OpenSSL::PKey::PKeyError, "not a public key #{filename.inspect}"
    end

load_private_key(filename, passphrase=nil, ask_passphrase=true) click to toggle source

Loads a private key from a file. It will correctly determine whether the file describes an RSA or DSA key, and will load it appropriately. The new key is returned. If the key itself is encrypted (requiring a passphrase to use), the user will be prompted to enter their password unless passphrase works.

    # File lib/net/ssh/key_factory.rb, line 40
    def load_private_key(filename, passphrase=nil, ask_passphrase=true)
      data = File.read(File.expand_path(filename))
      load_data_private_key(data, passphrase, ask_passphrase, filename)
    end

load_public_key(filename) click to toggle source

Loads a public key from a file. It will correctly determine whether the file describes an RSA or DSA key, and will load it appropriately. The new public key is returned.

    # File lib/net/ssh/key_factory.rb, line 99
    def load_public_key(filename)
      data = File.read(File.expand_path(filename))
      load_data_public_key(data, filename)
    end

[Validate]

Generated with the Darkfish Rdoc Generator 2.