3 # keytrans: this is an RSA key translation utility; it is capable of
4 # transforming RSA keys (both public keys and secret keys) between
5 # several popular representations, including OpenPGP, PEM-encoded
6 # PKCS#1 DER, and OpenSSH-style public key lines.
8 # How it behaves depends on the name under which it is invoked. The
9 # two implementations currently are: pem2openpgp and openpgp2ssh.
13 # pem2openpgp: take a PEM-encoded RSA private-key on standard input, a
14 # User ID as the first argument, and generate an OpenPGP secret key
15 # and certificate from it.
17 # WARNING: the secret key material *will* appear on stdout (albeit in
18 # OpenPGP form) -- if you redirect stdout to a file, make sure the
19 # permissions on that file are appropriately locked down!
23 # pem2openpgp 'ssh://'$(hostname -f) < /etc/ssh/ssh_host_rsa_key | gpg --import
28 # openpgp2ssh: take a stream of OpenPGP packets containing public or
29 # secret key material on standard input, and a Key ID (or fingerprint)
30 # as the first argument. Find the matching key in the input stream,
31 # and emit it on stdout in an OpenSSH-compatible format. If the input
32 # key is an OpenPGP public key (either primary or subkey), the output
33 # will be an OpenSSH single-line public key. If the input key is an
34 # OpenPGP secret key, the output will be a PEM-encoded RSA key.
38 # gpg --export-secret-subkeys --export-options export-reset-subkey-passwd $KEYID | \
39 # openpgp2ssh $KEYID | ssh-add /dev/stdin
43 # Jameson Rollins <jrollins@finestructure.net>
44 # Daniel Kahn Gillmor <dkg@fifthhorseman.net>
46 # Started on: 2009-01-07 02:01:19-0500
48 # License: GPL v3 or later (we may need to adjust this given that this
49 # connects to OpenSSL via perl)
54 use Crypt::OpenSSL::RSA;
55 use Crypt::OpenSSL::Bignum;
56 use Crypt::OpenSSL::Bignum::CTX;
61 ## make sure all length() and substr() calls use bytes only:
64 my $old_format_packet_lengths = { one => 0,
70 # see RFC 4880 section 9.1 (ignoring deprecated algorithms for now)
71 my $asym_algos = { rsa => 1,
76 # see RFC 4880 section 9.2
77 my $ciphers = { plaintext => 0,
88 # see RFC 4880 section 9.3
89 my $zips = { uncompressed => 0,
95 # see RFC 4880 section 9.4
96 my $digests = { md5 => 1,
105 # see RFC 4880 section 5.2.3.21
106 my $usage_flags = { certify => 0x01,
108 encrypt_comms => 0x04,
109 encrypt_storage => 0x08,
110 encrypt => 0x0c, ## both comms and storage
111 split => 0x10, # the private key is split via secret sharing
112 authenticate => 0x20,
113 shared => 0x80, # more than one person holds the entire private key
116 # see RFC 4880 section 4.3
117 my $packet_types = { pubkey_enc_session => 1,
119 symkey_enc_session => 3,
124 compressed_data => 8,
132 symenc_w_integrity => 18,
136 # see RFC 4880 section 5.2.1
137 my $sig_types = { binary_doc => 0x00,
140 generic_certification => 0x10,
141 persona_certification => 0x11,
142 casual_certification => 0x12,
143 positive_certification => 0x13,
144 subkey_binding => 0x18,
145 primary_key_binding => 0x19,
146 key_signature => 0x1f,
147 key_revocation => 0x20,
148 subkey_revocation => 0x28,
149 certification_revocation => 0x30,
155 # see RFC 4880 section 5.2.3.23
156 my $revocation_reasons = { no_reason_specified => 0,
158 key_compromised => 2,
160 user_id_no_longer_valid => 32,
163 # see RFC 4880 section 5.2.3.1
164 my $subpacket_types = { sig_creation_time => 2,
165 sig_expiration_time => 3,
170 key_expiration_time => 9,
171 preferred_cipher => 11,
172 revocation_key => 12,
175 preferred_digest => 21,
176 preferred_compression => 22,
177 keyserver_prefs => 23,
178 preferred_keyserver => 24,
183 revocation_reason => 29,
185 signature_target => 31,
186 embedded_signature => 32,
189 # bitstring (see RFC 4880 section 5.2.3.24)
190 my $features = { mdc => 0x01
193 # bitstring (see RFC 4880 5.2.3.17)
194 my $keyserver_prefs = { nomodify => 0x80
197 ###### end lookup tables ######
199 # FIXME: if we want to be able to interpret openpgp data as well as
200 # produce it, we need to produce key/value-swapped lookup tables as well.
203 ########### Math/Utility Functions ##############
206 # see the bottom of page 44 of RFC 4880 (http://tools.ietf.org/html/rfc4880#page-44)
207 sub simple_checksum {
210 return unpack("%16C*",$bytes);
213 # calculate the multiplicative inverse of a mod b this is euclid's
214 # extended algorithm. For more information see:
215 # http://en.wikipedia.org/wiki/Extended_Euclidean_algorithm the
216 # arguments here should be Crypt::OpenSSL::Bignum objects. $a should
217 # be the larger of the two values, and the two values should be
220 sub modular_multi_inverse {
225 my $origdivisor = $b->copy();
227 my $ctx = Crypt::OpenSSL::Bignum::CTX->new();
228 my $x = Crypt::OpenSSL::Bignum->zero();
229 my $y = Crypt::OpenSSL::Bignum->one();
230 my $lastx = Crypt::OpenSSL::Bignum->one();
231 my $lasty = Crypt::OpenSSL::Bignum->zero();
236 while (! $b->is_zero()) {
237 my ($quotient, $remainder) = $a->div($b, $ctx);
243 $x = $lastx->sub($quotient->mul($x, $ctx));
247 $y = $lasty->sub($quotient->mul($y, $ctx));
252 die "did this math wrong.\n";
255 # let's make sure that we return a positive value because RFC 4880,
256 # section 3.2 only allows unsigned values:
258 ($finalquotient, $finalremainder) = $lastx->add($origdivisor)->div($origdivisor, $ctx);
260 return $finalremainder;
264 ############ OpenPGP formatting functions ############
266 # make an old-style packet out of the given packet type and body.
267 # old-style (see RFC 4880 section 4.2)
273 my $len = length($body);
274 my $pseudolen = $len;
276 # if the caller wants to use at least N octets of packet length,
277 # pretend that we're using that many.
278 if (defined $options && defined $options->{'packet_length'}) {
279 $pseudolen = 2**($options->{'packet_length'} * 8) - 1;
281 if ($pseudolen < $len) {
288 if ($pseudolen < 2**8) {
289 $lenbytes = $old_format_packet_lengths->{one};
291 } elsif ($pseudolen < 2**16) {
292 $lenbytes = $old_format_packet_lengths->{two};
294 } elsif ($pseudolen < 2**31) {
295 ## not testing against full 32 bits because i don't want to deal
296 ## with potential overflow.
297 $lenbytes = $old_format_packet_lengths->{four};
300 ## what the hell do we do here?
301 $lenbytes = $old_format_packet_lengths->{indeterminate};
305 return pack('C'.$lencode, 0x80 + ($type * 4) + $lenbytes, $len).
310 # takes a Crypt::OpenSSL::Bignum, returns it formatted as OpenPGP MPI
311 # (RFC 4880 section 3.2)
315 my $val = $num->to_bin();
316 my $mpilen = length($val)*8;
318 # this is a kludgy way to get the number of significant bits in the
320 my $bitsinfirstbyte = length(sprintf("%b", ord($val)));
322 $mpilen -= (8 - $bitsinfirstbyte);
324 return pack('n', $mpilen).$val;
327 # takes a Crypt::OpenSSL::Bignum, returns an MPI packed in preparation
328 # for an OpenSSH-style public key format. see:
329 # http://marc.info/?l=openssh-unix-dev&m=121866301718839&w=2
330 sub openssh_mpi_pack {
333 my $val = $num->to_bin();
334 my $mpilen = length($val);
336 my $ret = pack('N', $mpilen);
338 # if the first bit of the leading byte is high, we should include a
340 if (ord($val) & 0x80) {
341 $ret = pack('NC', $mpilen+1, 0);
347 sub openssh_pubkey_pack {
350 my ($modulus, $exponent) = $key->get_key_parameters();
352 return openssh_mpi_pack(Crypt::OpenSSL::Bignum->new_from_bin("ssh-rsa")).
353 openssh_mpi_pack($exponent).
354 openssh_mpi_pack($modulus);
357 # pull an OpenPGP-specified MPI off of a given stream, returning it as
358 # a Crypt::OpenSSL::Bignum.
361 my $readtally = shift;
364 read($instr, $bitlen, 2) or die "could not read MPI length.\n";
365 $bitlen = unpack('n', $bitlen);
368 my $bytestoread = POSIX::floor(($bitlen + 7)/8);
370 read($instr, $ret, $bytestoread) or die "could not read MPI body.\n";
371 $$readtally += $bytestoread;
372 return Crypt::OpenSSL::Bignum->new_from_bin($ret);
376 # FIXME: genericize these to accept either RSA or DSA keys:
377 sub make_rsa_pub_key_body {
379 my $key_timestamp = shift;
381 my ($n, $e) = $key->get_key_parameters();
384 pack('CN', 4, $key_timestamp).
385 pack('C', $asym_algos->{rsa}).
390 sub make_rsa_sec_key_body {
392 my $key_timestamp = shift;
394 # we're not using $a and $b, but we need them to get to $c.
395 my ($n, $e, $d, $p, $q) = $key->get_key_parameters();
397 my $c3 = modular_multi_inverse($p, $q);
399 my $secret_material = mpi_pack($d).
404 # according to Crypt::OpenSSL::RSA, the closest value we can get out
405 # of get_key_parameters is 1/q mod p; but according to sec 5.5.3 of
406 # RFC 4880, we're actually looking for u, the multiplicative inverse
407 # of p, mod q. This is why we're calculating the value directly
408 # with modular_multi_inverse.
411 pack('CN', 4, $key_timestamp).
412 pack('C', $asym_algos->{rsa}).
415 pack('C', 0). # seckey material is not encrypted -- see RFC 4880 sec 5.5.3
417 pack('n', simple_checksum($secret_material));
420 # expects an RSA key (public or private) and a timestamp
423 my $key_timestamp = shift;
425 my $rsabody = make_rsa_pub_key_body($key, $key_timestamp);
427 return Digest::SHA::sha1(pack('Cn', 0x99, length($rsabody)).$rsabody);
431 # FIXME: handle DSA keys as well!
437 # strong assertion of identity is the default (for a self-sig):
438 if (! defined $args->{certification_type}) {
439 $args->{certification_type} = $sig_types->{positive_certification};
442 if (! defined $args->{sig_timestamp}) {
443 $args->{sig_timestamp} = time();
445 if (! defined $args->{key_timestamp}) {
446 $args->{key_timestamp} = $args->{sig_timestamp} + 0;
448 my $key_timestamp = $args->{key_timestamp};
450 # generate and aggregate subpackets:
454 if (! defined $args->{usage_flags}) {
455 $flags = $usage_flags->{certify};
457 my @ff = split(",", $args->{usage_flags});
458 foreach my $f (@ff) {
459 if (! defined $usage_flags->{$f}) {
460 die "No such flag $f";
462 $flags |= $usage_flags->{$f};
465 my $usage_subpacket = pack('CCC', 2, $subpacket_types->{usage_flags}, $flags);
467 # how should we determine how far off to set the expiration date?
468 # default is no expiration. Specify the timestamp in seconds from the
470 my $expiration_subpacket = '';
471 if (defined $args->{expiration}) {
472 my $expires_in = $args->{expiration} + 0;
473 $expiration_subpacket = pack('CCN', 5, $subpacket_types->{key_expiration_time}, $expires_in);
477 # prefer AES-256, AES-192, AES-128, CAST5, 3DES:
478 my $pref_sym_algos = pack('CCCCCCC', 6, $subpacket_types->{preferred_cipher},
483 $ciphers->{tripledes}
486 # prefer SHA-512, SHA-384, SHA-256, SHA-224, RIPE-MD/160, SHA-1
487 my $pref_hash_algos = pack('CCCCCCCC', 7, $subpacket_types->{preferred_digest},
492 $digests->{ripemd160},
496 # prefer ZLIB, BZip2, ZIP
497 my $pref_zip_algos = pack('CCCCC', 4, $subpacket_types->{preferred_compression},
503 # we support the MDC feature:
504 my $feature_subpacket = pack('CCC', 2, $subpacket_types->{features},
507 # keyserver preference: only owner modify (???):
508 my $keyserver_pref = pack('CCC', 2, $subpacket_types->{keyserver_prefs},
509 $keyserver_prefs->{nomodify});
512 $args->{hashed_subpackets} =
514 $expiration_subpacket.
522 make_packet($packet_types->{seckey}, make_rsa_sec_key_body($rsa, $key_timestamp)).
523 make_packet($packet_types->{uid}, $uid).
524 gensig($rsa, $uid, $args);
527 # FIXME: handle non-RSA keys
529 # FIXME: this currently only makes self-sigs -- we should parameterize
530 # it to make certifications over keys other than the issuer.
536 # FIXME: allow signature creation using digests other than SHA256
537 $rsa->use_sha256_hash();
539 # see page 22 of RFC 4880 for why i think this is the right padding
541 $rsa->use_pkcs1_padding();
543 if (! $rsa->check_key()) {
544 die "key does not check\n";
547 my $certtype = $args->{certification_type} + 0;
549 my $version = pack('C', 4);
550 my $sigtype = pack('C', $certtype);
552 my $pubkey_algo = pack('C', $asym_algos->{rsa});
553 # SHA256 FIXME: allow signature creation using digests other than SHA256
554 my $hash_algo = pack('C', $digests->{sha256});
556 # FIXME: i'm worried about generating a bazillion new OpenPGP
557 # certificates from the same key, which could easily happen if you run
558 # this script more than once against the same key (because the
559 # timestamps will differ). How can we prevent this?
561 # this argument (if set) overrides the current time, to
562 # be able to create a standard key. If we read the key from a file
563 # instead of stdin, should we use the creation time on the file?
564 my $sig_timestamp = ($args->{sig_timestamp} + 0);
565 my $key_timestamp = ($args->{key_timestamp} + 0);
567 if ($key_timestamp > $sig_timestamp) {
568 die "key timestamp must not be later than signature timestamp\n";
571 my $creation_time_packet = pack('CCN', 5, $subpacket_types->{sig_creation_time}, $sig_timestamp);
573 my $hashed_subs = $creation_time_packet.$args->{hashed_subpackets};
575 my $subpacket_octets = pack('n', length($hashed_subs));
577 my $sig_data_to_be_hashed =
585 my $pubkey = make_rsa_pub_key_body($rsa, $key_timestamp);
587 # this is for signing. it needs to be an old-style header with a
588 # 2-packet octet count.
590 my $key_data = make_packet($packet_types->{pubkey}, $pubkey, {'packet_length'=>2});
592 # take the last 8 bytes of the fingerprint as the keyid:
593 my $keyid = substr(fingerprint($rsa, $key_timestamp), 20 - 8, 8);
595 # the v4 signature trailer is:
597 # version number, literal 0xff, and then a 4-byte count of the
598 # signature data itself.
599 my $trailer = pack('CCN', 4, 0xff, length($sig_data_to_be_hashed));
602 pack('CN', 0xb4, length($uid)).
608 $sig_data_to_be_hashed.
612 # FIXME: handle signatures over digests other than SHA256:
613 my $data_hash = Digest::SHA::sha256_hex($datatosign);
615 my $issuer_packet = pack('CCa8', 9, $subpacket_types->{issuer}, $keyid);
617 my $sig = Crypt::OpenSSL::Bignum->new_from_bin($rsa->sign($datatosign));
620 $sig_data_to_be_hashed.
621 pack('n', length($issuer_packet)).
623 pack('n', hex(substr($data_hash, 0, 4))).
626 return make_packet($packet_types->{sig}, $sig_body);
629 # FIXME: switch to passing the whole packet as the arg, instead of the
632 # FIXME: think about native perl representation of the packets instead.
634 # Put a user ID into the $data
639 my $packetlen = shift;
642 ($tag == $packet_types->{uid}) or die "This should not be called on anything but a User ID packet\n";
644 read($instr, $dummy, $packetlen);
645 $data->{uid} = {} unless defined $data->{uid};
646 $data->{uid}->{$dummy} = {};
650 # find signatures associated with the given fingerprint and user ID.
655 my $packetlen = shift;
657 ($tag == $packet_types->{sig}) or die "No calling findsig on anything other than a signature packet.\n";
662 if ((undef $data->{key}) ||
663 (undef $data->{uid}) ||
664 (undef $data->{uid}->{$data->{target}->{uid}})) {
665 # this is not the user ID we are looking for.
666 read($instr, $dummy, $packetlen - $readbytes) or die "Could not skip past this packet.\n";
669 read($instr, $data, 6) or die "could not read signature header\n";
670 my ($ver, $sigtype, $pubkeyalgo, $digestalgo, $subpacketsize) = unpack('CCCCn', $data);
672 printf(STDERR "We only work with version 4 signatures.");
673 read($instr, $dummy, $packetlen - $readbytes) or die "Could not skip past this packet.\n";
676 if ($pubkeyalgo != $asym_algos->{rsa}) {
677 printf(STDERR "We can only work with RSA at the moment");
678 read($instr, $dummy, $packetlen - $readbytes) or die "Could not skip past this packet.\n";
681 if ($sigtype != $sig_types->{positive_certification}) {
682 # FIXME: some weird implementations might have made generic,
683 # persona, or casual certifications instead of positive
684 # certifications for self-sigs. Probably should handle them too.
685 read($instr, $dummy, $packetlen - $readbytes) or die "Could not skip past this packet.\n";
690 read($instr, $subpackets, $subpacketsize) or die "could not read hashed signature subpackets.\n";
692 read($instr, $subpacketsize, 2) or die "could not read unhashed signature subpacket size.\n";
693 $subpacketsize = unpack('n', $subpacketsize);
695 my $unhashedsubpackets;
696 read($instr, $unhashedsubpackets, $subpacketsize) or die "could not read unhashed signature subpackets.\n";
699 read($instr, $hashtail, 2) or die "could not read left 16 bits of digest.\n";
701 # FIXME: RSA signatures should read in how many MPIs?
705 # given an input stream and data, store the found key in data and
706 # consume the rest of the stream corresponding to the packet.
707 # data contains: (fpr: fingerprint to find, key: current best guess at key)
712 my $packetlen = shift;
718 read($instr, $ver, 1) or die "could not read key version\n";
723 printf(STDERR "We only work with version 4 keys. This key appears to be version %s.\n", $ver);
724 read($instr, $dummy, $packetlen - $readbytes) or die "Could not skip past this packet.\n";
729 read($instr, $key_timestamp, 4) or die "could not read key timestamp.\n";
731 $key_timestamp = unpack('N', $key_timestamp);
734 read($instr, $algo, 1) or die "could not read key algorithm.\n";
737 if ($algo != $asym_algos->{rsa}) {
738 printf(STDERR "We only support RSA keys (this key used algorithm %d).\n", $algo);
739 read($instr, $dummy, $packetlen - $readbytes) or die "Could not skip past this packet.\n";
743 ## we have an RSA key.
744 my $modulus = read_mpi($instr, \$readbytes);
745 my $exponent = read_mpi($instr, \$readbytes);
747 my $pubkey = Crypt::OpenSSL::RSA->new_key_from_parameters($modulus, $exponent);
748 my $foundfpr = fingerprint($pubkey, $key_timestamp);
750 my $foundfprstr = Crypt::OpenSSL::Bignum->new_from_bin($foundfpr)->to_hex();
751 # left-pad with 0's to bring up to full 40-char (160-bit) fingerprint:
752 $foundfprstr = sprintf("%040s", $foundfprstr);
755 if ((!defined($data->{target}->{fpr})) ||
756 (substr($foundfprstr, -1 * length($data->{target}->{fpr})) eq $data->{target}->{fpr})) {
757 if (defined($data->{key})) {
758 die "Found two matching keys.\n";
760 $data->{key} = { 'rsa' => $pubkey,
761 'timestamp' => $key_timestamp };
764 if ($tag != $packet_types->{seckey} &&
765 $tag != $packet_types->{sec_subkey}) {
766 if ($readbytes < $packetlen) {
767 read($instr, $dummy, $packetlen - $readbytes) or die "Could not skip past this packet.\n";
771 if (!defined($data->{key})) {
772 # we don't think the public part of this key matches
773 if ($readbytes < $packetlen) {
774 read($instr, $dummy, $packetlen - $readbytes) or die "Could not skip past this packet.\n";
780 read($instr, $s2k, 1) or die "Could not read S2K octet.\n";
784 printf(STDERR "We cannot handle encrypted secret keys. Skipping!\n") ;
785 read($instr, $dummy, $packetlen - $readbytes) or die "Could not skip past this packet.\n";
789 # secret material is unencrypted
790 # see http://tools.ietf.org/html/rfc4880#section-5.5.3
791 my $d = read_mpi($instr, \$readbytes);
792 my $p = read_mpi($instr, \$readbytes);
793 my $q = read_mpi($instr, \$readbytes);
794 my $u = read_mpi($instr, \$readbytes);
797 read($instr, $checksum, 2) or die "Could not read checksum of secret key material.\n";
799 $checksum = unpack('n', $checksum);
801 # FIXME: compare with the checksum! how? the data is
802 # gone into the Crypt::OpenSSL::Bignum
804 $data->{key}->{rsa} = Crypt::OpenSSL::RSA->new_key_from_parameters($modulus,
810 $data->{key}->{rsa}->check_key() or die "Secret key is not a valid RSA key.\n";
812 if ($readbytes < $packetlen) {
813 read($instr, $dummy, $packetlen - $readbytes) or die "Could not skip past this packet.\n";
822 if (length($fpr) < 8) {
823 die "We need at least 8 hex digits of fingerprint.\n";
828 my $data = { 'fpr' => $fpr};
829 my $subs = { $packet_types->{pubkey} => \&findkey,
830 $packet_types->{pub_subkey} => \&findkey,
831 $packet_types->{seckey} => \&findkey,
832 $packet_types->{sec_subkey} => \&findkey };
834 packetwalk($instr, $subs, $data);
836 return $data->{key}->{rsa};
844 if ((! defined $fpr) ||
845 (length($fpr) < 8)) {
846 die "We need at least 8 hex digits of fingerprint.\n";
851 if (! defined $uid) {
852 die "No User ID defined.\n";
855 my $data = { target => { fpr => $fpr,
858 my $subs = { $packet_types->{seckey} => \&findkey,
859 $packet_types->{uid} => \&finduid
862 packetwalk($instr, $subs, $data);
864 if ((! defined $data->{uid}) ||
865 (! defined $data->{uid}->{$uid})) {
866 die "The User ID \"$uid\" is not associated with this key";
869 if ((! defined $data->{key}) ||
870 (! defined $data->{key}->{rsa}) ||
871 (! defined $data->{key}->{timestamp})) {
872 die "The key requested was not found."
875 my $revocation_reason = 'No longer using this hostname';
876 if (defined $data->{revocation_reason}) {
877 $revocation_reason = $data->{revocation_reason};
880 my $rev_reason_subpkt = prefixsubpacket(pack('CC',
881 $subpacket_types->{revocation_reason},
882 $revocation_reasons->{user_id_no_longer_valid}).
885 # what does a signature like this look like?
886 my $args = { 'key_timestamp' => $data->{key}->{timestamp},
887 'sig_timestamp' => time(),
888 'certification_type' => $sig_types->{certification_revocation},
889 'hashed_subpackets' => $rev_reason_subpkt,
893 return gensig($data->{key}->{rsa}, $data->{uid}, $args);
897 # see 5.2.3.1 for tips on how to calculate the length of a subpacket:
898 sub prefixsubpacket {
899 my $subpacket = shift;
901 my $len = length($subpacket);
906 $prefix = pack('C', $len);
907 } elsif ($len < 16576) {
909 my $second = $in%256;
910 my $first = ($in - $second)>>8;
911 $prefix = pack('CC', $first + 192, $second)
913 $prefix = pack('CN', 255, $len);
915 return $prefix.$subpacket;
929 while (! eof($instr)) {
930 read($instr, $packettag, 1);
931 $packettag = ord($packettag);
934 if ( ! (0x80 & $packettag)) {
935 die "This is not an OpenPGP packet\n";
937 if (0x40 & $packettag) {
938 # this is a new-format packet.
939 $tag = (0x3f & $packettag);
941 read($instr, $nextlen, 1);
942 $nextlen = ord($nextlen);
943 if ($nextlen < 192) {
944 $packetlen = $nextlen;
945 } elsif ($nextlen < 224) {
947 read($instr, $newoct, 1);
948 $newoct = ord($newoct);
949 $packetlen = (($nextlen - 192) << 8) + ($newoct) + 192;
950 } elsif ($nextlen == 255) {
951 read($instr, $nextlen, 4);
952 $packetlen = unpack('N', $nextlen);
954 # packet length is undefined.
957 # this is an old-format packet.
959 $lentype = 0x03 & $packettag;
960 $tag = ( 0x3c & $packettag ) >> 2;
962 read($instr, $packetlen, 1) or die "could not read packet length\n";
963 $packetlen = unpack('C', $packetlen);
964 } elsif ($lentype == 1) {
965 read($instr, $packetlen, 2) or die "could not read packet length\n";
966 $packetlen = unpack('n', $packetlen);
967 } elsif ($lentype == 2) {
968 read($instr, $packetlen, 4) or die "could not read packet length\n";
969 $packetlen = unpack('N', $packetlen);
971 # packet length is undefined.
975 if (! defined($packetlen)) {
976 die "Undefined packet lengths are not supported.\n";
979 if (defined $subs->{$tag}) {
980 $subs->{$tag}($data, $instr, $tag, $packetlen);
982 read($instr, $dummy, $packetlen) or die "Could not skip past this packet!\n";
991 if (/^pem2openpgp$/) {
996 defined($uid) or die "You must specify a user ID string.\n";
998 # FIXME: fail if there is no given user ID; or should we default to
999 # hostname_long() from Sys::Hostname::Long ?
1001 if (defined $ENV{PEM2OPENPGP_NEWKEY}) {
1002 $rsa = Crypt::OpenSSL::RSA->generate_key($ENV{PEM2OPENPGP_NEWKEY});
1009 $rsa = Crypt::OpenSSL::RSA->new_private_key($stdin);
1012 print pem2openpgp($rsa,
1014 { sig_timestamp => $ENV{PEM2OPENPGP_TIMESTAMP},
1015 key_timestamp => $ENV{PEM2OPENPGP_KEY_TIMESTAMP},
1016 expiration => $ENV{PEM2OPENPGP_EXPIRATION},
1017 usage_flags => $ENV{PEM2OPENPGP_USAGE_FLAGS},
1021 elsif (/^openpgp2ssh$/) {
1024 open($instream,'-');
1025 binmode($instream, ":bytes");
1026 my $key = openpgp2rsa($instream, $fpr);
1027 if (defined($key)) {
1028 if ($key->is_private()) {
1029 print $key->get_private_key_string();
1031 print "ssh-rsa ".encode_base64(openssh_pubkey_pack($key), '')."\n";
1034 die "No matching key found.\n";
1037 elsif (/^keytrans$/) {
1038 # subcommands when keytrans is invoked directly are UNSUPPORTED,
1039 # UNDOCUMENTED, and WILL NOT BE MAINTAINED.
1040 my $subcommand = shift;
1042 if (/^revokeuserid$/) {
1046 open($instream,'-');
1047 binmode($instream, ":bytes");
1049 my $revcert = revokeuserid($instream, $fpr, $uid);
1053 die "Unrecognized subcomand. keytrans subcommands are not a stable interface!\n";
1058 die "Unrecognized keytrans call.\n";