NAME

Parse::SSH2::PublicKey - Parse SSH2 public keys in either SECSH or OpenSSH format.

VERSION

Version 0.01

PURPOSE

Different implementations of SSH (OpenSSH, SSH Tectia, PuTTY, etc) use different key formats. For example, for public key authentication, OpenSSH will accept an authorized_keys file that holds all keys, whereas the ssh.com proprietary implementation wants an authorized_keys/ *directory* with a file for each key!

This module was created to assist sysadmins in converting from one SSH implementation to another.

SYNOPSIS

use Parse::SSH2::PublicKey;

my $auth_key = "$ENV{HOME}/.ssh/authorized_keys";
my @keys = Parse::SSH2::PublicKey->parse_file($auth_key);

for my $k ( @keys ) {
    print $k->secsh();
    # or ->openssh()
}

...

my $dir  = "$ENV{HOME}/.ssh2/authorized_keys/";
my @files = glob("$dir/*pub");
my @keys = map { Parse::SSH2::PublicKey->parse_file($_) } @files;

for my $k ( @keys ) {
    print $k->openssh();
}

METHODS

new()

Creates an Parse::SSH2::PublicKey object. Not intended to be used directly. Instead, this is called internally by parse(), which returns an array of objects.

parse()

Accepts a block of text and parses out SSH2 public keys in both OpenSSH and SECSH format. Returns an *array* of Parse::SSH2::PublicKey objects. Class method to be used instead of new().

parse_file()

Convenience method which opens a file and calls parse on the contents.

secsh()

Returns an SSH public key in SECSH format (as specified in RFC4716). Preserves headers and the order of headers.

See http://tools.ietf.org/html/rfc4716.

openssh()

Returns an SSH public key in OpenSSH format. Preserves 'comment' field parsed from either SECSH or OpenSSH.

comment()

Convenience method for $k->headers->{Comment}. Returns the Comment header value or the empty string.

subject()

Convenience method for $k->headers->{Subject}. Returns the Subject header value or the empty string.

ATTRIBUTES

encryption

Either 'ssh-rsa' or 'ssh-dss', for RSA and DSA keys, respectively.

header_order

Order of headers parsed from SECSH-format keys. See also http://tools.ietf.org/html/rfc4716.

headers

Hashref containing headers parsed from SECSH-format keys. See also http://tools.ietf.org/html/rfc4716.

key

The actual base64-encoded key data.

type

Either 'public' or 'private', but private keys aren't currently supported. Obsolete. (Or perhaps ahead of it's time.)

EXAMPLE USAGE

OpenSSH to SSH Tectia

#! /usr/bin/perl -w
# Sample script to prepare for a move from OpenSSH
# to the ssh.com commercial implementation

use strict;
use feature qw/say/;
use File::Slurp qw(read_file write_file);
use File::Temp qw(tempdir);
use Parse::SSH2::PublicKey;

my @keys = Parse::SSH2::PublicKey->parse_file("$ENV{HOME}/.ssh/authorized_keys");

my $dir = tempdir( CLEANUP => 0 );

my $count = 0;
for my $k ( @keys ) {
    my $filename = $dir . '/' . 'key' . ($count+1) . '.pub';
    ++$count if write_file( $filename, $k->secsh );
}

say "Wrote $count SECSH format key files to dir [$dir]";
say "Now move $dir into place at \$HOME/.ssh2/authorized_keys/";

OpenSSH to SSH Tectia

#! /usr/bin/perl -w
# Sample script to convert from ssh.com implementation
# to OpenSSH

use strict;
use feature qw/say/;
use Parse::SSH2::PublicKey;

my $ssh_authkeys_dir = "$ENV{HOME}/.ssh2/authorized_keys/";
my @files = glob("$ssh_authkeys_dir/*pub");
my @keys = map { Parse::SSH2::PublicKey->parse_file($_) } @files;

# output can be redirected to a file, e.g. '$HOME/.ssh/authorized_keys'
for my $k ( @keys ) {
    print $k->openssh();
}

AUTHOR

Nathan Marley, <nathan.marley at gmail.com>

BUGS

Please report any bugs or feature requests to bug-parse-ssh2-publickey at rt.cpan.org, or through the web interface at http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Parse-SSH2-PublicKey. I will be notified, and then you'll automatically be notified of progress on your bug as I make changes.

SUPPORT

You can find documentation for this module with the perldoc command.

perldoc Parse::SSH2::PublicKey

You can also look for information at:

SEE ALSO

The Secure Shell (SSH) Public Key File Format

LICENSE AND COPYRIGHT

Copyright 2012 Nathan Marley.

This program is free software; you can redistribute it and/or modify it under the terms of either: the GNU General Public License as published by the Free Software Foundation; or the Artistic License.

See http://dev.perl.org/licenses/ for more information.