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")
Methods
public class
Included modules
- Prompt
Constants
Public class methods
# File lib/net/ssh/key_factory.rb, line 28 28: def get(name) 29: MAP.fetch(name).new 30: end
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 47 47: def load_data_private_key(data, passphrase=nil, filename="") 48: if data.match(/-----BEGIN DSA PRIVATE KEY-----/) 49: key_type = OpenSSL::PKey::DSA 50: elsif data.match(/-----BEGIN RSA PRIVATE KEY-----/) 51: key_type = OpenSSL::PKey::RSA 52: elsif data.match(/-----BEGIN (.*) PRIVATE KEY-----/) 53: raise OpenSSL::PKey::PKeyError, "not a supported key type '#{$1}'" 54: else 55: raise OpenSSL::PKey::PKeyError, "not a private key (#{filename})" 56: end 57: 58: encrypted_key = data.match(/ENCRYPTED/) 59: tries = 0 60: 61: begin 62: return key_type.new(data, passphrase || 'invalid') 63: rescue OpenSSL::PKey::RSAError, OpenSSL::PKey::DSAError => e 64: if encrypted_key 65: tries += 1 66: if tries <= 3 67: passphrase = prompt("Enter passphrase for #{filename}:", false) 68: retry 69: else 70: raise 71: end 72: else 73: raise 74: end 75: end 76: end
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 89 89: def load_data_public_key(data, filename="") 90: type, blob = data.split(/ /) 91: 92: raise Net::SSH::Exception, "public key at #{filename} is not valid" if blob.nil? 93: 94: blob = blob.unpack("m*").first 95: reader = Net::SSH::Buffer.new(blob) 96: reader.read_key or raise OpenSSL::PKey::PKeyError, "not a public key #{filename.inspect}" 97: 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: data = File.read(File.expand_path(filename)) 39: load_data_private_key(data, passphrase, filename) 40: 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 81 81: def load_public_key(filename) 82: data = File.read(File.expand_path(filename)) 83: load_data_public_key(data, filename) 84: end