diff --git a/lib/Crypt/OpenPGP/KeyRing.pm b/lib/Crypt/OpenPGP/KeyRing.pm index 6709ed4..1e8287a 100644 --- a/lib/Crypt/OpenPGP/KeyRing.pm +++ b/lib/Crypt/OpenPGP/KeyRing.pm @@ -64,14 +64,20 @@ sub restore { my($buf) = @_; $ring->{blocks} = []; my($kb); - while (my $packet = Crypt::OpenPGP::PacketFactory->parse($buf)) { - if (ref($packet) eq "Crypt::OpenPGP::Certificate" && - !$packet->is_subkey) { - $kb = Crypt::OpenPGP::KeyBlock->new; - $ring->add($kb); + do { + my $packet = Crypt::OpenPGP::PacketFactory->parse($buf); + if (defined $packet) { + if (ref($packet) eq "Crypt::OpenPGP::Certificate" && + !$packet->is_subkey) { + $kb = Crypt::OpenPGP::KeyBlock->new; + $ring->add($kb); + } + $kb->add($packet) if $kb; + } else { + warn("Failed to parse packet: " . + Crypt::OpenPGP::PacketFactory->errstr); } - $kb->add($packet) if $kb; - } + } while($buf->offset < $buf->length); } sub add { diff --git a/lib/Crypt/OpenPGP/PacketFactory.pm b/lib/Crypt/OpenPGP/PacketFactory.pm index eba1ad8..a21c899 100644 --- a/lib/Crypt/OpenPGP/PacketFactory.pm +++ b/lib/Crypt/OpenPGP/PacketFactory.pm @@ -73,6 +73,10 @@ sub parse { eval "use $pkt_class;"; return $class->error("Loading $pkt_class failed: $@") if $@; $obj = $pkt_class->parse($b, @args); + if (!defined $obj) { + return $class->error("Parsing packet of type $pkt_class failed: " . + $pkt_class->errstr); + } } else { $obj = { type => $type, length => $len,