summaryrefslogtreecommitdiff
path: root/src/keytrans/pem2openpgp
blob: 73becfe464277b61acfbbcfa96777ce0901c361b (plain)
  1. #!/usr/bin/perl -w -T
  2. # pem2openpgp: take a PEM-encoded RSA private-key on standard input, a
  3. # User ID as the first argument, and generate an OpenPGP secret key
  4. # and certificate from it.
  5. # WARNING: the secret key material *will* appear on stdout (albeit in
  6. # OpenPGP form) -- if you redirect stdout to a file, make sure the
  7. # permissions on that file are appropriately locked down!
  8. # Usage:
  9. # pem2openpgp 'ssh://'$(hostname -f) < /etc/ssh/ssh_host_rsa_key | gpg --import
  10. # Authors:
  11. # Jameson Rollins <jrollins@finestructure.net>
  12. # Daniel Kahn Gillmor <dkg@fifthhorseman.net>
  13. # Started on: 2009-01-07 02:01:19-0500
  14. # License: GPL v3 or later (we may need to adjust this given that this
  15. # connects to OpenSSL via perl)
  16. use strict;
  17. use warnings;
  18. use File::Basename;
  19. use Crypt::OpenSSL::RSA;
  20. use Crypt::OpenSSL::Bignum;
  21. use Crypt::OpenSSL::Bignum::CTX;
  22. use Digest::SHA1;
  23. use MIME::Base64;
  24. ## make sure all length() and substr() calls use bytes only:
  25. use bytes;
  26. my $old_format_packet_lengths = { one => 0,
  27. two => 1,
  28. four => 2,
  29. indeterminate => 3,
  30. };
  31. # see RFC 4880 section 9.1 (ignoring deprecated algorithms for now)
  32. my $asym_algos = { rsa => 1,
  33. elgamal => 16,
  34. dsa => 17,
  35. };
  36. # see RFC 4880 section 9.2
  37. my $ciphers = { plaintext => 0,
  38. idea => 1,
  39. tripledes => 2,
  40. cast5 => 3,
  41. blowfish => 4,
  42. aes128 => 7,
  43. aes192 => 8,
  44. aes256 => 9,
  45. twofish => 10,
  46. };
  47. # see RFC 4880 section 9.3
  48. my $zips = { uncompressed => 0,
  49. zip => 1,
  50. zlib => 2,
  51. bzip2 => 3,
  52. };
  53. # see RFC 4880 section 9.4
  54. my $digests = { md5 => 1,
  55. sha1 => 2,
  56. ripemd160 => 3,
  57. sha256 => 8,
  58. sha384 => 9,
  59. sha512 => 10,
  60. sha224 => 11,
  61. };
  62. # see RFC 4880 section 5.2.3.21
  63. my $usage_flags = { certify => 0x01,
  64. sign => 0x02,
  65. encrypt_comms => 0x04,
  66. encrypt_storage => 0x08,
  67. encrypt => 0x0c, ## both comms and storage
  68. split => 0x10, # the private key is split via secret sharing
  69. authenticate => 0x20,
  70. shared => 0x80, # more than one person holds the entire private key
  71. };
  72. # see RFC 4880 section 4.3
  73. my $packet_types = { pubkey_enc_session => 1,
  74. sig => 2,
  75. symkey_enc_session => 3,
  76. onepass_sig => 4,
  77. seckey => 5,
  78. pubkey => 6,
  79. sec_subkey => 7,
  80. compressed_data => 8,
  81. symenc_data => 9,
  82. marker => 10,
  83. literal => 11,
  84. trust => 12,
  85. uid => 13,
  86. pub_subkey => 14,
  87. uat => 17,
  88. symenc_w_integrity => 18,
  89. mdc => 19,
  90. };
  91. # see RFC 4880 section 5.2.1
  92. my $sig_types = { binary_doc => 0x00,
  93. text_doc => 0x01,
  94. standalone => 0x02,
  95. generic_certification => 0x10,
  96. persona_certification => 0x11,
  97. casual_certification => 0x12,
  98. positive_certification => 0x13,
  99. subkey_binding => 0x18,
  100. primary_key_binding => 0x19,
  101. key_signature => 0x1f,
  102. key_revocation => 0x20,
  103. subkey_revocation => 0x28,
  104. certification_revocation => 0x30,
  105. timestamp => 0x40,
  106. thirdparty => 0x50,
  107. };
  108. # see RFC 4880 section 5.2.3.1
  109. my $subpacket_types = { sig_creation_time => 2,
  110. sig_expiration_time => 3,
  111. exportable => 4,
  112. trust_sig => 5,
  113. regex => 6,
  114. revocable => 7,
  115. key_expiration_time => 9,
  116. preferred_cipher => 11,
  117. revocation_key => 12,
  118. issuer => 16,
  119. notation => 20,
  120. preferred_digest => 21,
  121. preferred_compression => 22,
  122. keyserver_prefs => 23,
  123. preferred_keyserver => 24,
  124. primary_uid => 25,
  125. policy_uri => 26,
  126. usage_flags => 27,
  127. signers_uid => 28,
  128. revocation_reason => 29,
  129. features => 30,
  130. signature_target => 31,
  131. embedded_signature => 32,
  132. };
  133. # bitstring (see RFC 4880 section 5.2.3.24)
  134. my $features = { mdc => 0x01
  135. };
  136. # bitstring (see RFC 4880 5.2.3.17)
  137. my $keyserver_prefs = { nomodify => 0x80
  138. };
  139. ###### end lookup tables ######
  140. # FIXME: if we want to be able to interpret openpgp data as well as
  141. # produce it, we need to produce key/value-swapped lookup tables as well.
  142. ########### Math/Utility Functions ##############
  143. # see the bottom of page 43 of RFC 4880
  144. sub simple_checksum {
  145. my $bytes = shift;
  146. return unpack("%32W*",$bytes) % 65536;
  147. }
  148. # calculate the multiplicative inverse of a mod b this is euclid's
  149. # extended algorithm. For more information see:
  150. # http://en.wikipedia.org/wiki/Extended_Euclidean_algorithm the
  151. # arguments here should be Crypt::OpenSSL::Bignum objects. $a should
  152. # be the larger of the two values, and the two values should be
  153. # coprime.
  154. sub modular_multi_inverse {
  155. my $a = shift;
  156. my $b = shift;
  157. my $origdivisor = $b->copy();
  158. my $ctx = Crypt::OpenSSL::Bignum::CTX->new();
  159. my $x = Crypt::OpenSSL::Bignum->zero();
  160. my $y = Crypt::OpenSSL::Bignum->one();
  161. my $lastx = Crypt::OpenSSL::Bignum->one();
  162. my $lasty = Crypt::OpenSSL::Bignum->zero();
  163. my $finalquotient;
  164. my $finalremainder;
  165. while (! $b->is_zero()) {
  166. my ($quotient, $remainder) = $a->div($b, $ctx);
  167. $a = $b;
  168. $b = $remainder;
  169. my $temp = $x;
  170. $x = $lastx->sub($quotient->mul($x, $ctx));
  171. $lastx = $temp;
  172. $temp = $y;
  173. $y = $lasty->sub($quotient->mul($y, $ctx));
  174. $lasty = $temp;
  175. }
  176. if (!$a->is_one()) {
  177. die "did this math wrong.\n";
  178. }
  179. # let's make sure that we return a positive value because RFC 4880,
  180. # section 3.2 only allows unsigned values:
  181. ($finalquotient, $finalremainder) = $lastx->add($origdivisor)->div($origdivisor, $ctx);
  182. return $finalremainder;
  183. }
  184. ############ OpenPGP formatting functions ############
  185. # make an old-style packet out of the given packet type and body.
  186. # old-style (see RFC 4880 section 4.2)
  187. sub make_packet {
  188. my $type = shift;
  189. my $body = shift;
  190. my $options = shift;
  191. my $len = length($body);
  192. my $pseudolen = $len;
  193. # if the caller wants to use at least N octets of packet length,
  194. # pretend that we're using that many.
  195. if (defined $options && defined $options->{'packet_length'}) {
  196. $pseudolen = 2**($options->{'packet_length'} * 8) - 1;
  197. }
  198. if ($pseudolen < $len) {
  199. $pseudolen = $len;
  200. }
  201. my $lenbytes;
  202. my $lencode;
  203. if ($pseudolen < 2**8) {
  204. $lenbytes = $old_format_packet_lengths->{one};
  205. $lencode = 'C';
  206. } elsif ($pseudolen < 2**16) {
  207. $lenbytes = $old_format_packet_lengths->{two};
  208. $lencode = 'n';
  209. } elsif ($pseudolen < 2**31) {
  210. ## not testing against full 32 bits because i don't want to deal
  211. ## with potential overflow.
  212. $lenbytes = $old_format_packet_lengths->{four};
  213. $lencode = 'N';
  214. } else {
  215. ## what the hell do we do here?
  216. $lenbytes = $old_format_packet_lengths->{indeterminate};
  217. $lencode = '';
  218. }
  219. return pack('C'.$lencode, 0x80 + ($type * 4) + $lenbytes, $len).
  220. $body;
  221. }
  222. # takes a Crypt::OpenSSL::Bignum, returns it formatted as OpenPGP MPI
  223. # (RFC 4880 section 3.2)
  224. sub mpi_pack {
  225. my $num = shift;
  226. my $val = $num->to_bin();
  227. my $mpilen = length($val)*8;
  228. # this is a kludgy way to get the number of significant bits in the
  229. # first byte:
  230. my $bitsinfirstbyte = length(sprintf("%b", ord($val)));
  231. $mpilen -= (8 - $bitsinfirstbyte);
  232. return pack('n', $mpilen).$val;
  233. }
  234. # takes a Crypt::OpenSSL::Bignum, returns an MPI packed in preparation
  235. # for an OpenSSH-style public key format. see:
  236. # http://marc.info/?l=openssh-unix-dev&m=121866301718839&w=2
  237. sub openssh_mpi_pack {
  238. my $num = shift;
  239. my $val = $num->to_bin();
  240. my $mpilen = length($val);
  241. my $ret = pack('N', $mpilen);
  242. # if the first bit of the leading byte is high, we should include a
  243. # 0 byte:
  244. if (ord($val) & 0x80) {
  245. $ret = pack('NC', $mpilen+1, 0);
  246. }
  247. return $ret.$val;
  248. }
  249. sub openssh_pubkey_pack {
  250. my $key = shift;
  251. my ($modulus, $exponent) = $key->get_key_parameters();
  252. return openssh_mpi_pack(Crypt::OpenSSL::Bignum->new_from_bin("ssh-rsa")).
  253. openssh_mpi_pack($exponent).
  254. openssh_mpi_pack($modulus);
  255. }
  256. # pull an OpenPGP-specified MPI off of a given stream, returning it as
  257. # a Crypt::OpenSSL::Bignum.
  258. sub read_mpi {
  259. my $instr = shift;
  260. my $readtally = shift;
  261. my $bitlen;
  262. read($instr, $bitlen, 2) or die "could not read MPI length.\n";
  263. $bitlen = unpack('n', $bitlen);
  264. $$readtally += 2;
  265. my $bytestoread = ($bitlen + 7)/8;
  266. my $ret;
  267. read($instr, $ret, $bytestoread) or die "could not read MPI body.\n";
  268. $$readtally += $bytestoread;
  269. return Crypt::OpenSSL::Bignum->new_from_bin($ret);
  270. }
  271. # FIXME: genericize these to accept either RSA or DSA keys:
  272. sub make_rsa_pub_key_body {
  273. my $key = shift;
  274. my $timestamp = shift;
  275. my ($n, $e) = $key->get_key_parameters();
  276. return
  277. pack('CN', 4, $timestamp).
  278. pack('C', $asym_algos->{rsa}).
  279. mpi_pack($n).
  280. mpi_pack($e);
  281. }
  282. sub make_rsa_sec_key_body {
  283. my $key = shift;
  284. my $timestamp = shift;
  285. # we're not using $a and $b, but we need them to get to $c.
  286. my ($n, $e, $d, $p, $q) = $key->get_key_parameters();
  287. my $c3 = modular_multi_inverse($p, $q);
  288. my $secret_material = mpi_pack($d).
  289. mpi_pack($p).
  290. mpi_pack($q).
  291. mpi_pack($c3);
  292. # according to Crypt::OpenSSL::RSA, the closest value we can get out
  293. # of get_key_parameters is 1/q mod p; but according to sec 5.5.3 of
  294. # RFC 4880, we're actually looking for u, the multiplicative inverse
  295. # of p, mod q. This is why we're calculating the value directly
  296. # with modular_multi_inverse.
  297. return
  298. pack('CN', 4, $timestamp).
  299. pack('C', $asym_algos->{rsa}).
  300. mpi_pack($n).
  301. mpi_pack($e).
  302. pack('C', 0). # seckey material is not encrypted -- see RFC 4880 sec 5.5.3
  303. $secret_material.
  304. pack('n', simple_checksum($secret_material));
  305. }
  306. # expects an RSA key (public or private) and a timestamp
  307. sub fingerprint {
  308. my $key = shift;
  309. my $timestamp = shift;
  310. my $rsabody = make_rsa_pub_key_body($key, $timestamp);
  311. return Digest::SHA1::sha1(pack('Cn', 0x99, length($rsabody)).$rsabody);
  312. }
  313. # FIXME: handle DSA keys as well!
  314. sub pem2openpgp {
  315. my $rsa = shift;
  316. my $uid = shift;
  317. my $args = shift;
  318. $rsa->use_sha1_hash();
  319. # see page 22 of RFC 4880 for why i think this is the right padding
  320. # choice to use:
  321. $rsa->use_pkcs1_padding();
  322. if (! $rsa->check_key()) {
  323. die "key does not check";
  324. }
  325. my $version = pack('C', 4);
  326. # strong assertion of identity:
  327. my $sigtype = pack('C', $sig_types->{positive_certification});
  328. # RSA
  329. my $pubkey_algo = pack('C', $asym_algos->{rsa});
  330. # SHA1
  331. my $hash_algo = pack('C', $digests->{sha1});
  332. # FIXME: i'm worried about generating a bazillion new OpenPGP
  333. # certificates from the same key, which could easily happen if you run
  334. # this script more than once against the same key (because the
  335. # timestamps will differ). How can we prevent this?
  336. # this environment variable (if set) overrides the current time, to
  337. # be able to create a standard key? If we read the key from a file
  338. # instead of stdin, should we use the creation time on the file?
  339. my $timestamp = 0;
  340. if (defined $args->{timestamp}) {
  341. $timestamp = ($args->{timestamp} + 0);
  342. } else {
  343. $timestamp = time();
  344. }
  345. my $creation_time_packet = pack('CCN', 5, $subpacket_types->{sig_creation_time}, $timestamp);
  346. my $flags = 0;
  347. if (! defined $args->{usage_flags}) {
  348. $flags = $usage_flags->{certify};
  349. } else {
  350. my @ff = split(",", $args->{usage_flags});
  351. foreach my $f (@ff) {
  352. if (! defined $usage_flags->{$f}) {
  353. die "No such flag $f";
  354. }
  355. $flags |= $usage_flags->{$f};
  356. }
  357. }
  358. my $usage_packet = pack('CCC', 2, $subpacket_types->{usage_flags}, $flags);
  359. # how should we determine how far off to set the expiration date?
  360. # default is no expiration. Specify the timestamp in seconds from the
  361. # key creation.
  362. my $expiration_packet = '';
  363. if (defined $args->{expiration}) {
  364. my $expires_in = $args->{expiration} + 0;
  365. $expiration_packet = pack('CCN', 5, $subpacket_types->{key_expiration_time}, $expires_in);
  366. }
  367. # prefer AES-256, AES-192, AES-128, CAST5, 3DES:
  368. my $pref_sym_algos = pack('CCCCCCC', 6, $subpacket_types->{preferred_cipher},
  369. $ciphers->{aes256},
  370. $ciphers->{aes192},
  371. $ciphers->{aes128},
  372. $ciphers->{cast5},
  373. $ciphers->{tripledes}
  374. );
  375. # prefer SHA-1, SHA-256, RIPE-MD/160
  376. my $pref_hash_algos = pack('CCCCC', 4, $subpacket_types->{preferred_digest},
  377. $digests->{sha1},
  378. $digests->{sha256},
  379. $digests->{ripemd160}
  380. );
  381. # prefer ZLIB, BZip2, ZIP
  382. my $pref_zip_algos = pack('CCCCC', 4, $subpacket_types->{preferred_compression},
  383. $zips->{zlib},
  384. $zips->{bzip2},
  385. $zips->{zip}
  386. );
  387. # we support the MDC feature:
  388. my $feature_subpacket = pack('CCC', 2, $subpacket_types->{features},
  389. $features->{mdc});
  390. # keyserver preference: only owner modify (???):
  391. my $keyserver_pref = pack('CCC', 2, $subpacket_types->{keyserver_prefs},
  392. $keyserver_prefs->{nomodify});
  393. my $subpackets_to_be_hashed =
  394. $creation_time_packet.
  395. $usage_packet.
  396. $expiration_packet.
  397. $pref_sym_algos.
  398. $pref_hash_algos.
  399. $pref_zip_algos.
  400. $feature_subpacket.
  401. $keyserver_pref;
  402. my $subpacket_octets = pack('n', length($subpackets_to_be_hashed));
  403. my $sig_data_to_be_hashed =
  404. $version.
  405. $sigtype.
  406. $pubkey_algo.
  407. $hash_algo.
  408. $subpacket_octets.
  409. $subpackets_to_be_hashed;
  410. my $pubkey = make_rsa_pub_key_body($rsa, $timestamp);
  411. my $seckey = make_rsa_sec_key_body($rsa, $timestamp);
  412. # this is for signing. it needs to be an old-style header with a
  413. # 2-packet octet count.
  414. my $key_data = make_packet($packet_types->{pubkey}, $pubkey, {'packet_length'=>2});
  415. # take the last 8 bytes of the fingerprint as the keyid:
  416. my $keyid = substr(fingerprint($rsa, $timestamp), 20 - 8, 8);
  417. # the v4 signature trailer is:
  418. # version number, literal 0xff, and then a 4-byte count of the
  419. # signature data itself.
  420. my $trailer = pack('CCN', 4, 0xff, length($sig_data_to_be_hashed));
  421. my $uid_data =
  422. pack('CN', 0xb4, length($uid)).
  423. $uid;
  424. my $datatosign =
  425. $key_data.
  426. $uid_data.
  427. $sig_data_to_be_hashed.
  428. $trailer;
  429. my $data_hash = Digest::SHA1::sha1_hex($datatosign);
  430. my $issuer_packet = pack('CCa8', 9, $subpacket_types->{issuer}, $keyid);
  431. my $sig = Crypt::OpenSSL::Bignum->new_from_bin($rsa->sign($datatosign));
  432. my $sig_body =
  433. $sig_data_to_be_hashed.
  434. pack('n', length($issuer_packet)).
  435. $issuer_packet.
  436. pack('n', hex(substr($data_hash, 0, 4))).
  437. mpi_pack($sig);
  438. return
  439. make_packet($packet_types->{seckey}, $seckey).
  440. make_packet($packet_types->{uid}, $uid).
  441. make_packet($packet_types->{sig}, $sig_body);
  442. }
  443. sub openpgp2ssh {
  444. my $instr = shift;
  445. my $fpr = shift;
  446. if (defined $fpr) {
  447. if (length($fpr) < 8) {
  448. die "We need at least 8 hex digits of fingerprint.\n";
  449. }
  450. $fpr = uc($fpr);
  451. }
  452. my $packettag;
  453. my $dummy;
  454. my $tag;
  455. my $key;
  456. while (! eof($instr)) {
  457. read($instr, $packettag, 1);
  458. $packettag = ord($packettag);
  459. my $packetlen;
  460. if ( ! (0x80 & $packettag)) {
  461. die "This is not an OpenPGP packet\n";
  462. }
  463. if (0x40 & $packettag) {
  464. $tag = (0x3f & $packettag);
  465. my $nextlen = 0;
  466. read($instr, $nextlen, 1);
  467. $nextlen = ord($nextlen);
  468. if ($nextlen < 192) {
  469. $packetlen = $nextlen;
  470. } elsif ($nextlen < 224) {
  471. my $newoct;
  472. read($instr, $newoct, 1);
  473. $newoct = ord($newoct);
  474. $packetlen = (($nextlen - 192) << 8) + ($newoct) + 192;
  475. } elsif ($nextlen == 255) {
  476. read($instr, $nextlen, 4);
  477. $packetlen = unpack('N', $nextlen);
  478. } else {
  479. # packet length is undefined.
  480. }
  481. } else {
  482. my $lentype;
  483. $lentype = 0x03 & $packettag;
  484. $tag = ( 0x3c & $packettag ) >> 2;
  485. if ($lentype == 0) {
  486. read($instr, $packetlen, 1) or die "could not read packet length\n";
  487. $packetlen = unpack('C', $packetlen);
  488. } elsif ($lentype == 1) {
  489. read($instr, $packetlen, 2) or die "could not read packet length\n";
  490. $packetlen = unpack('n', $packetlen);
  491. } elsif ($lentype == 2) {
  492. read($instr, $packetlen, 4) or die "could not read packet length\n";
  493. $packetlen = unpack('N', $packetlen);
  494. } else {
  495. # packet length is undefined.
  496. }
  497. }
  498. if (! defined($packetlen)) {
  499. die "Undefined packet lengths are not supported.\n";
  500. }
  501. if ($tag == $packet_types->{pubkey} ||
  502. $tag == $packet_types->{pub_subkey} ||
  503. $tag == $packet_types->{seckey} ||
  504. $tag == $packet_types->{sec_subkey}) {
  505. my $ver;
  506. my $readbytes = 0;
  507. read($instr, $ver, 1) or die "could not read key version\n";
  508. $readbytes += 1;
  509. $ver = ord($ver);
  510. if ($ver != 4) {
  511. printf(STDERR "We only work with version 4 keys. This key appears to be version %s.\n", $ver);
  512. read($instr, $dummy, $packetlen - $readbytes) or die "Could not skip past this packet.\n";
  513. } else {
  514. my $timestamp;
  515. read($instr, $timestamp, 4) or die "could not read key timestamp.\n";
  516. $readbytes += 4;
  517. $timestamp = unpack('N', $timestamp);
  518. my $algo;
  519. read($instr, $algo, 1) or die "could not read key algorithm.\n";
  520. $readbytes += 1;
  521. $algo = ord($algo);
  522. if ($algo != $asym_algos->{rsa}) {
  523. printf(STDERR "We only support RSA keys (this key used algorithm %d).\n", $algo);
  524. read($instr, $dummy, $packetlen - $readbytes) or die "Could not skip past this packet.\n";
  525. } else {
  526. ## we have an RSA key.
  527. my $modulus = read_mpi($instr, \$readbytes);
  528. my $exponent = read_mpi($instr, \$readbytes);
  529. my $pubkey = Crypt::OpenSSL::RSA->new_key_from_parameters($modulus, $exponent);
  530. my $foundfpr = fingerprint($pubkey, $timestamp);
  531. my $foundfprstr = Crypt::OpenSSL::Bignum->new_from_bin($foundfpr)->to_hex();
  532. # is this a match?
  533. if ((!defined($fpr)) ||
  534. (substr($foundfprstr, -1 * length($fpr)) eq $fpr)) {
  535. if (defined($key)) {
  536. die "Found two matching keys.\n";
  537. }
  538. $key = $pubkey;
  539. }
  540. if ($tag == $packet_types->{seckey} ||
  541. $tag == $packet_types->{sec_subkey}) {
  542. if (!defined($key)) { # we don't think the public part of
  543. # this key matches
  544. read($instr, $dummy, $packetlen - $readbytes) or die "Could not skip past this packet.\n";
  545. } else {
  546. my $s2k;
  547. read($instr, $s2k, 1) or die "Could not read S2K octet.\n";
  548. $readbytes += 1;
  549. $s2k = ord($s2k);
  550. if ($s2k == 0) {
  551. # secret material is unencrypted
  552. # see http://tools.ietf.org/html/rfc4880#section-5.5.3
  553. my $d = read_mpi($instr, \$readbytes);
  554. my $p = read_mpi($instr, \$readbytes);
  555. my $q = read_mpi($instr, \$readbytes);
  556. my $u = read_mpi($instr, \$readbytes);
  557. my $checksum;
  558. read($instr, $checksum, 2) or die "Could not read checksum of secret key material.\n";
  559. $readbytes += 2;
  560. $checksum = unpack('n', $checksum);
  561. # FIXME: compare with the checksum! how? the data is
  562. # gone into the Crypt::OpenSSL::Bignum
  563. $key = Crypt::OpenSSL::RSA->new_key_from_parameters($modulus,
  564. $exponent,
  565. $d,
  566. $p,
  567. $q);
  568. $key->check_key() or die "Secret key is not a valid RSA key.\n";
  569. } else {
  570. print(STDERR "We cannot handle encrypted secret keys. Skipping!\n") ;
  571. read($instr, $dummy, $packetlen - $readbytes) or die "Could not skip past this packet.\n";
  572. }
  573. }
  574. }
  575. }
  576. }
  577. } else {
  578. read($instr, $dummy, $packetlen) or die "Could not skip past this packet!\n";
  579. }
  580. }
  581. return $key;
  582. }
  583. for (basename($0)) {
  584. if (/^pem2openpgp$/) {
  585. my $rsa;
  586. my $stdin;
  587. my $uid = shift;
  588. defined($uid) or die "You must specify a user ID string.\n";
  589. # FIXME: fail if there is no given user ID; or should we default to
  590. # hostname_long() from Sys::Hostname::Long ?
  591. if (defined $ENV{PEM2OPENPGP_NEWKEY}) {
  592. $rsa = Crypt::OpenSSL::RSA->generate_key($ENV{PEM2OPENPGP_NEWKEY});
  593. } else {
  594. $stdin = do {
  595. local $/; # slurp!
  596. <STDIN>;
  597. };
  598. $rsa = Crypt::OpenSSL::RSA->new_private_key($stdin);
  599. }
  600. print pem2openpgp($rsa,
  601. $uid,
  602. { timestamp => $ENV{PEM2OPENPGP_TIMESTAMP},
  603. expiration => $ENV{PEM2OPENPGP_EXPIRATION},
  604. usage_flags => $ENV{PEM2OPENPGP_USAGE_FLAGS},
  605. }
  606. );
  607. }
  608. elsif (/^openpgp2ssh$/) {
  609. my $fpr = shift;
  610. my $instream;
  611. open($instream,'-');
  612. binmode($instream, ":bytes");
  613. my $key = openpgp2ssh($instream, $fpr);
  614. if (defined($key)) {
  615. if ($key->is_private()) {
  616. print $key->get_private_key_string();
  617. } else {
  618. print "ssh-rsa ".encode_base64(openssh_pubkey_pack($key), '')."\n";
  619. }
  620. } else {
  621. die "No matching key found.\n";
  622. }
  623. }
  624. else {
  625. die "Unrecognized keytrans call.\n";
  626. }
  627. }