module PuTTY::Key::OpenSSL::ClassMethods

The {ClassMethods} module is used to extend OpenSSL::PKey when using the PuTTY::Key refinement or calling {PuTTY::Key.global_install}. This adds a from_ppk class method to OpenSSL::PKey.

Public Instance Methods

from_ppk(ppk) click to toggle source

Creates a new OpenSSL::PKey from a PuTTY private key (instance of {PPK}).

This method is called using OpenSSL::PKey.from_ppk(ppk).

PuTTY keys using the algorithms ssh-dss, ssh-rsa, ecdsa-sha2-nistp256, ecdsa-sha2-nistp384 and ecdsa-sha2-nistp521 are supported.

@return [Object] An instance of either OpenSSL::PKey::DSA, OpenSSL::PKey::RSA or OpenSSL::PKey::EC depending on the algorithm of ppk.

@raise [ArgumentError] If ppk is nil. @raise [ArgumentError] If the algorithm of ppk is not supported.

# File lib/putty/key/openssl.rb, line 304
def from_ppk(ppk)
  raise ArgumentError, 'ppk must not be nil' unless ppk

  case ppk.algorithm
  when 'ssh-dss'
    PKeyBuilding.ppk_to_dsa(ppk)
  when 'ssh-rsa'
    PKeyBuilding.ppk_to_rsa(ppk)
  when /\Aecdsa-sha2-(nistp(?:256|384|521))\z/
    PKeyBuilding.ppk_to_ec(ppk, $1)
  else
    raise ArgumentError, "Unsupported algorithm: #{ppk.algorithm}"
  end
end