+ my $data = { target => { fpr => $fpr,
+ },
+ };
+ my $subs = { $packet_types->{pubkey} => \&findkey,
+ $packet_types->{pub_subkey} => \&findkey,
+ $packet_types->{seckey} => \&findkey,
+ $packet_types->{sec_subkey} => \&findkey };
+
+ packetwalk($instr, $subs, $data);
+
+ return $data->{key}->{rsa};
+}
+
+sub adduserid {
+ my $instr = shift;
+ my $fpr = shift;
+ my $uid = shift;
+ my $args = shift;
+
+ if ((! defined $fpr) ||
+ (length($fpr) < 8)) {
+ die "We need at least 8 hex digits of fingerprint.\n";
+ }
+
+ $fpr = uc($fpr);
+
+ if (! defined $uid) {
+ die "No User ID defined.\n";
+ }
+
+ my $data = { target => { fpr => $fpr,
+ uid => $uid,
+ },
+ };
+ my $subs = { $packet_types->{seckey} => \&findkey,
+ $packet_types->{uid} => \&finduid,
+ $packet_types->{sig} => \&findsig,
+ };
+
+ packetwalk($instr, $subs, $data);
+
+ if ((! defined $data->{key}) ||
+ (! defined $data->{key}->{rsa}) ||
+ (! defined $data->{key}->{timestamp})) {
+ die "The key requested was not found.\n"
+ }
+
+ if (defined $data->{uid}->{$uid}) {
+ die "The requested User ID '$uid' is already associated with this key.\n";
+ }
+ $args->{key_timestamp} = $data->{key}->{timestamp};
+
+ return
+ make_packet($packet_types->{pubkey}, make_rsa_pub_key_body($data->{key}->{rsa}, $data->{key}->{timestamp})).
+ make_packet($packet_types->{uid}, $uid).
+ makeselfsig($data->{key}->{rsa},
+ $uid,
+ $args);
+
+}
+
+
+sub revokeuserid {
+ my $instr = shift;
+ my $fpr = shift;
+ my $uid = shift;
+ my $sigtime = shift;
+
+ if ((! defined $fpr) ||
+ (length($fpr) < 8)) {
+ die "We need at least 8 hex digits of fingerprint.\n";
+ }
+
+ $fpr = uc($fpr);
+
+ if (! defined $uid) {
+ die "No User ID defined.\n";
+ }
+
+ my $data = { target => { fpr => $fpr,
+ uid => $uid,
+ },
+ };
+ my $subs = { $packet_types->{seckey} => \&findkey,
+ $packet_types->{uid} => \&finduid,
+ $packet_types->{sig} => \&findsig,
+ };
+
+ packetwalk($instr, $subs, $data);
+
+ if ((! defined $data->{uid}) ||
+ (! defined $data->{uid}->{$uid})) {
+ die "The User ID \"$uid\" is not associated with this key";
+ }
+
+ if ((! defined $data->{key}) ||
+ (! defined $data->{key}->{rsa}) ||
+ (! defined $data->{key}->{timestamp})) {
+ die "The key requested was not found."
+ }
+
+ my $revocation_reason = 'No longer using this hostname';
+ if (defined $data->{revocation_reason}) {
+ $revocation_reason = $data->{revocation_reason};
+ }
+
+ my $rev_reason_subpkt = prefixsubpacket(pack('CC',
+ $subpacket_types->{revocation_reason},
+ $revocation_reasons->{user_id_no_longer_valid}).
+ $revocation_reason);
+
+ if (! defined $sigtime) {
+ $sigtime = time();
+ }
+ # what does a signature like this look like?
+ my $args = { key_timestamp => $data->{key}->{timestamp},
+ sig_timestamp => $sigtime,
+ certification_type => $sig_types->{certification_revocation},
+ hashed_subpackets => $rev_reason_subpkt,
+ };
+
+ return
+ make_packet($packet_types->{pubkey}, make_rsa_pub_key_body($data->{key}->{rsa}, $data->{key}->{timestamp})).
+ make_packet($packet_types->{uid}, $uid).
+ join('', @{$data->{sigs}}).
+ gensig($data->{key}->{rsa}, $uid, $args);
+}
+
+
+# see 5.2.3.1 for tips on how to calculate the length of a subpacket:
+sub prefixsubpacket {
+ my $subpacket = shift;
+
+ my $len = length($subpacket);
+ my $prefix;
+ use bytes;
+ if ($len < 192) {
+ # one byte:
+ $prefix = pack('C', $len);
+ } elsif ($len < 16576) {
+ my $in = $len - 192;
+ my $second = $in%256;
+ my $first = ($in - $second)>>8;
+ $prefix = pack('CC', $first + 192, $second)
+ } else {
+ $prefix = pack('CN', 255, $len);
+ }
+ return $prefix.$subpacket;
+}
+
+
+
+sub packetwalk {
+ my $instr = shift;
+ my $subs = shift;
+ my $data = shift;
+