Class | Net::SSH::KeyFactory |
In: |
lib/net/ssh/key_factory.rb
lib/net/ssh/key_factory.rb |
Parent: | Object |
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::KeyFacory.load_public_key("~/.ssh/id_dsa.pub")
MAP | = | { "dh" => OpenSSL::PKey::DH, "rsa" => OpenSSL::PKey::RSA, "dsa" => OpenSSL::PKey::DSA | Specifies the mapping of SSH names to OpenSSL key classes. | |
MAP | = | { "dh" => OpenSSL::PKey::DH, "rsa" => OpenSSL::PKey::RSA, "dsa" => OpenSSL::PKey::DSA | Specifies the mapping of SSH names to OpenSSL key classes. |
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 37 37: def load_private_key(filename, passphrase=nil) 38: file = File.read(File.expand_path(filename)) 39: 40: if file.match(/-----BEGIN DSA PRIVATE KEY-----/) 41: key_type = OpenSSL::PKey::DSA 42: elsif file.match(/-----BEGIN RSA PRIVATE KEY-----/) 43: key_type = OpenSSL::PKey::RSA 44: elsif file.match(/-----BEGIN (.*) PRIVATE KEY-----/) 45: raise OpenSSL::PKey::PKeyError, "not a supported key type '#{$1}'" 46: else 47: raise OpenSSL::PKey::PKeyError, "not a private key (#{filename})" 48: end 49: 50: encrypted_key = file.match(/ENCRYPTED/) 51: tries = 0 52: 53: begin 54: return key_type.new(file, passphrase || 'invalid') 55: rescue OpenSSL::PKey::RSAError, OpenSSL::PKey::DSAError => e 56: if encrypted_key 57: tries += 1 58: if tries <= 3 59: passphrase = prompt("Enter passphrase for #{filename}:", false) 60: retry 61: else 62: raise 63: end 64: else 65: raise 66: end 67: end 68: end
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 37 37: def load_private_key(filename, passphrase=nil) 38: file = File.read(File.expand_path(filename)) 39: 40: if file.match(/-----BEGIN DSA PRIVATE KEY-----/) 41: key_type = OpenSSL::PKey::DSA 42: elsif file.match(/-----BEGIN RSA PRIVATE KEY-----/) 43: key_type = OpenSSL::PKey::RSA 44: elsif file.match(/-----BEGIN (.*) PRIVATE KEY-----/) 45: raise OpenSSL::PKey::PKeyError, "not a supported key type '#{$1}'" 46: else 47: raise OpenSSL::PKey::PKeyError, "not a private key (#{filename})" 48: end 49: 50: encrypted_key = file.match(/ENCRYPTED/) 51: tries = 0 52: 53: begin 54: return key_type.new(file, passphrase || 'invalid') 55: rescue OpenSSL::PKey::RSAError, OpenSSL::PKey::DSAError => e 56: if encrypted_key 57: tries += 1 58: if tries <= 3 59: passphrase = prompt("Enter passphrase for #{filename}:", false) 60: retry 61: else 62: raise 63: end 64: else 65: raise 66: end 67: end 68: end
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 73 73: def load_public_key(filename) 74: data = File.read(File.expand_path(filename)) 75: type, blob = data.split(/ /) 76: 77: raise Net::SSH::Exception, "public key at #{filename} is not valid" if blob.nil? 78: 79: blob = blob.unpack("m*").first 80: reader = Net::SSH::Buffer.new(blob) 81: reader.read_key or raise OpenSSL::PKey::PKeyError, "not a public key #{filename.inspect}" 82: end
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 73 73: def load_public_key(filename) 74: data = File.read(File.expand_path(filename)) 75: type, blob = data.split(/ /) 76: 77: raise Net::SSH::Exception, "public key at #{filename} is not valid" if blob.nil? 78: 79: blob = blob.unpack("m*").first 80: reader = Net::SSH::Buffer.new(blob) 81: reader.read_key or raise OpenSSL::PKey::PKeyError, "not a public key #{filename.inspect}" 82: end