From 94b3b28cc10096b7b137be79952fb63da6721ca3 Mon Sep 17 00:00:00 2001 From: Glenn Willen Date: Wed, 30 Mar 2011 02:12:20 -0400 Subject: [PATCH 1/2] When parsing a keyring, if a packet fails to parse, warn and continue instead of silently aborting. --- lib/Crypt/OpenPGP/KeyRing.pm | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) 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 { From 0ff5e270098fcf6e4e3f6910603dfb086ef13e05 Mon Sep 17 00:00:00 2001 From: Glenn Willen Date: Wed, 30 Mar 2011 02:14:50 -0400 Subject: [PATCH 2/2] When parsing a packet fails, properly return an error from Crypt::OpenPGP::PacketFactory::parse. --- lib/Crypt/OpenPGP/PacketFactory.pm | 4 ++++ 1 file changed, 4 insertions(+) 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,