Class Net::SSH::Transport::OSSL::KeyFactory
In: lib/net/ssh/transport/ossl/key-factory.rb
Parent: Object

A factory class for returning new Key algorithm factories (actually classes).

Methods

Attributes

buffers  [W]  The setter for the buffer factory to use.
prompter  [W]  The setter for describing which prompter service to use when prompting the user for a key passphrase.

Public Class methods

Create a new instance of the KeyFactory that uses the given Hash-like to map SSH2 key algorithm names to names of factories (classes) that can instantiate those algorithms.

[Source]

    # File lib/net/ssh/transport/ossl/key-factory.rb, line 41
41:           def initialize( algorithms )
42:             @factories = algorithms
43:           end

Public Instance methods

Return a new instance of the key factory for the given name. If no such algorithm exists, a KeyTypeNotFound error will be raised.

[Source]

    # File lib/net/ssh/transport/ossl/key-factory.rb, line 47
47:           def get( name )
48:             klass_name = @factories.fetch( name ) do
49:               raise KeyTypeNotFound, name
50:             end
51: 
52:             return klass_name.new
53:           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.

[Source]

    # File lib/net/ssh/transport/ossl/key-factory.rb, line 60
60:           def load_private_key( filename )
61:             file = File.read( filename )
62: 
63:             if file.match( /-----BEGIN DSA PRIVATE KEY-----/ )
64:               key_type = OpenSSL::PKey::DSA
65:             elsif file.match( /-----BEGIN RSA PRIVATE KEY-----/ )
66:               key_type = OpenSSL::PKey::RSA
67:             elsif file.match( /-----BEGIN (.*) PRIVATE KEY-----/ )
68:               raise OpenSSL::PKey::PKeyError, "not a supported key type '#{$1}'"
69:             else
70:               raise OpenSSL::PKey::PKeyError, "not a private key (#{filename})"
71:             end
72: 
73:             encrypted_key = file.match( /ENCRYPTED/ )
74:             password = encrypted_key ? 'nil' : nil
75:             tries = 0
76: 
77:             begin
78:               return key_type.new( file, password )
79:             rescue OpenSSL::PKey::RSAError, OpenSSL::PKey::DSAError => e
80:               if encrypted_key && @prompter
81:                 tries += 1
82:                 if tries <= 3
83:                   password = @prompter.password(
84:                     "Enter password for #{filename}: " )
85:                   retry
86:                 else
87:                   raise
88:                 end
89:               else
90:                 raise
91:               end
92:             end
93:           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.

[Source]

     # File lib/net/ssh/transport/ossl/key-factory.rb, line 98
 98:           def load_public_key( filename )
 99:             data = File.open( filename ) { |file| file.read }
100:             type, blob = data.split( / / )
101: 
102:             blob = Base64.decode64( blob )
103:             reader = @buffers.reader( blob )
104:             key = reader.read_key or
105:               raise OpenSSL::PKey::PKeyError,
106:                 "not a public key #{filename.inspect}"
107:             return key
108:           end

[Validate]