Module: SSHData::PrivateKey

Defined in:
lib/ssh_data/private_key.rb,
lib/ssh_data/private_key/dsa.rb,
lib/ssh_data/private_key/rsa.rb,
lib/ssh_data/private_key/base.rb,
lib/ssh_data/private_key/ecdsa.rb,
lib/ssh_data/private_key/ed25519.rb

Defined Under Namespace

Classes: Base, DSA, ECDSA, ED25519, RSA

Constant Summary collapse

OPENSSH_PEM_TYPE =
"OPENSSH PRIVATE KEY"
RSA_PEM_TYPE =
"RSA PRIVATE KEY"
DSA_PEM_TYPE =
"DSA PRIVATE KEY"
ECDSA_PEM_TYPE =
"EC PRIVATE KEY"
ENCRYPTED_PEM_TYPE =
"ENCRYPTED PRIVATE KEY"

Class Method Summary collapse

Class Method Details

.from_data(data) ⇒ Object


50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/ssh_data/private_key.rb', line 50

def self.from_data(data)
  data[:private_keys].map do |priv|
    case priv[:algo]
    when PublicKey::ALGO_RSA
      RSA.new(**priv)
    when PublicKey::ALGO_DSA
      DSA.new(**priv)
    when PublicKey::ALGO_ECDSA256, PublicKey::ALGO_ECDSA384, PublicKey::ALGO_ECDSA521
      ECDSA.new(**priv)
    when PublicKey::ALGO_ED25519
      ED25519.new(**priv)
    else
      raise DecodeError, "unkown algo: #{priv[:algo].inspect}"
    end
  end
end

.parse(key) ⇒ Object

Parse an SSH private key.

key - A PEM or OpenSSH encoded private key.

Returns an Array of PrivateKey::Base subclass instances.


14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/ssh_data/private_key.rb', line 14

def self.parse(key)
  pem_type = Encoding.pem_type(key)
  case pem_type
  when OPENSSH_PEM_TYPE
    parse_openssh(key)
  when RSA_PEM_TYPE
    [RSA.from_openssl(OpenSSL::PKey::RSA.new(key, ""))]
  when DSA_PEM_TYPE
    [DSA.from_openssl(OpenSSL::PKey::DSA.new(key, ""))]
  when ECDSA_PEM_TYPE
    [ECDSA.from_openssl(OpenSSL::PKey::EC.new(key, ""))]
  when ENCRYPTED_PEM_TYPE
    raise DecryptError, "cannot decode encrypted private keys"
  else
    raise AlgorithmError, "unknown PEM type: #{pem_type.inspect}"
  end
rescue OpenSSL::PKey::PKeyError => e
  raise DecodeError, "bad private key. maybe encrypted?"
end

.parse_openssh(key) ⇒ Object

Parse an OpenSSH formatted private key.

key - An OpenSSH encoded private key.

Returns an Array of PrivateKey::Base subclass instances.


39
40
41
42
43
44
45
46
47
48
# File 'lib/ssh_data/private_key.rb', line 39

def self.parse_openssh(key)
  raw = Encoding.decode_pem(key, OPENSSH_PEM_TYPE)

  data, read = Encoding.decode_openssh_private_key(raw)
  unless read == raw.bytesize
    raise DecodeError, "unexpected trailing data"
  end

  from_data(data)
end