| 1 | package Crypt::DSA::Key::SSH2; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | use MIME::Base64 qw( decode_base64 ); | ||
| 6 | use Crypt::DSA::Key; | ||
| 7 | |||
| 8 | our $VERSION = '1.24'; #VERSION | ||
| 9 | |||
| 10 | use vars qw{$VERSION @ISA}; | ||
| 11 | BEGIN { | ||
| 12 | @ISA = 'Crypt::DSA::Key'; | ||
| 13 | } | ||
| 14 | |||
| 15 | use constant PRIVKEY_MAGIC => 0x3f6ff9eb; | ||
| 16 | |||
| 17 | sub deserialize { | ||
| 18 | my $key = shift; | ||
| 19 | my %param = @_; | ||
| 20 | |||
| 21 | chomp($param{Content}); | ||
| 22 | my($head, $object, $content, $tail) = $param{Content} =~ | ||
| 23 | m:(---- BEGIN ([^\n\-]+) ----)\n(.+)(---- END .*? ----)$:s; | ||
| 24 | my @lines = split /\n/, $content; | ||
| 25 | my $escaped = 0; | ||
| 26 | my @real; | ||
| 27 | for my $l (@lines) { | ||
| 28 | if (substr($l, -1) eq '\\') { | ||
| 29 | $escaped++; | ||
| 30 | next; | ||
| 31 | } | ||
| 32 | next if index($l, ':') != -1; | ||
| 33 | if ($escaped) { | ||
| 34 | $escaped--; | ||
| 35 | next; | ||
| 36 | } | ||
| 37 | push @real, $l; | ||
| 38 | } | ||
| 39 | $content = join "\n", @real; | ||
| 40 | $content = decode_base64($content); | ||
| 41 | |||
| 42 | my $b = BufferWithInt->new; | ||
| 43 | $b->append($content); | ||
| 44 | my $magic = $b->get_int32; | ||
| 45 | return unless $magic == PRIVKEY_MAGIC; | ||
| 46 | |||
| 47 | my($ignore); | ||
| 48 | $ignore = $b->get_int32; | ||
| 49 | my $type = $b->get_str; | ||
| 50 | my $cipher = $b->get_str; | ||
| 51 | $ignore = $b->get_int32 for 1..3; | ||
| 52 | |||
| 53 | return unless $cipher eq 'none'; | ||
| 54 | |||
| 55 | $key->p( $b->get_mp_ssh2 ); | ||
| 56 | $key->g( $b->get_mp_ssh2 ); | ||
| 57 | $key->q( $b->get_mp_ssh2 ); | ||
| 58 | $key->pub_key( $b->get_mp_ssh2 ); | ||
| 59 | $key->priv_key( $b->get_mp_ssh2 ); | ||
| 60 | |||
| 61 | #return unless $b->length == $b->offset; | ||
| 62 | |||
| 63 | $key; | ||
| 64 | } | ||
| 65 | |||
| 66 | sub serialize { | ||
| 67 | my $key = shift; | ||
| 68 | my %param = @_; | ||
| 69 | die "serialize is unimplemented"; | ||
| 70 | } | ||
| 71 | |||
| 72 | package BufferWithInt; | ||
| 73 | use strict; | ||
| 74 | |||
| 75 | use Data::Buffer; | ||
| 76 | use Crypt::DSA::Util qw( bin2mp ); | ||
| 77 | use base qw( Data::Buffer ); | ||
| 78 | |||
| 79 | sub get_mp_ssh2 { | ||
| 80 | my $buf = shift; | ||
| 81 | my $bits = $buf->get_int32; | ||
| 82 | my $off = $buf->{offset}; | ||
| 83 | my $bytes = int(($bits+7) / 8); | ||
| 84 | my $int = bin2mp( $buf->bytes($off, $bytes) ); | ||
| 85 | $buf->{offset} += $bytes; | ||
| 86 | $int; | ||
| 87 | } | ||
| 88 | |||
| 89 | 1; | ||
| 90 | __END__ |