summaryrefslogtreecommitdiff
path: root/src/keytrans/pem2openpgp
blob: 2fa221debaed889812e8d45c88537917af6c7652 (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 certificate
  4. # from it.
  5. # Usage:
  6. # pem2openpgp 'ssh://'$(hostname -f) < /etc/ssh/ssh_host_rsa_key | gpg --import
  7. # Authors:
  8. # Jameson Rollins <jrollins@finestructure.net>
  9. # Daniel Kahn Gillmor <dkg@fifthhorseman.net>
  10. # Started on: 2009-01-07 02:01:19-0500
  11. # License: GPL v3 or later (we may need to adjust this given that this
  12. # connects to OpenSSL via perl)
  13. use strict;
  14. use warnings;
  15. use Crypt::OpenSSL::RSA;
  16. use Crypt::OpenSSL::Bignum;
  17. use Digest::SHA1;
  18. use MIME::Base64;
  19. ## make sure all length() and substr() calls use bytes only:
  20. use bytes;
  21. my $uid = shift;
  22. # FIXME: fail if there is no given user ID; or should we default to
  23. # hostname_long() from Sys::Hostname::Long ?
  24. # make an old-style packet out of the given packet type and body.
  25. # old-style (see RFC 4880 section 4.2)
  26. sub make_packet {
  27. my $type = shift;
  28. my $body = shift;
  29. my $len = length($body);
  30. my $lenbytes;
  31. my $lencode;
  32. if ($len < 2**8) {
  33. $lenbytes = 0;
  34. $lencode = 'C';
  35. } elsif ($len < 2**16) {
  36. $lenbytes = 1;
  37. $lencode = 'n';
  38. } elsif ($len < 2**31) {
  39. ## not testing against full 32 bits because i don't want to deal
  40. ## with potential overflow.
  41. $lenbytes = 2;
  42. $lencode = 'N';
  43. } else {
  44. ## what the hell do we do here?
  45. $lenbytes = 3;
  46. $lencode = '';
  47. }
  48. return pack('C'.$lencode, 0x80 + ($type * 4) + $lenbytes, $len).
  49. $body;
  50. }
  51. # takes a Crypt::OpenSSL::Bignum, returns it formatted as OpenPGP MPI
  52. # (RFC 4880 section 3.2)
  53. sub mpi_pack {
  54. my $num = shift;
  55. my $val = $num->to_bin();
  56. my $mpilen = length($val)*8;
  57. # this is a kludgy way to get the number of significant bits in the
  58. # first byte:
  59. my $bitsinfirstbyte = length(sprintf("%b", ord($val)));
  60. $mpilen -= (8 - $bitsinfirstbyte);
  61. return pack('n', $mpilen).$val;
  62. }
  63. # FIXME: genericize this to accept either RSA or DSA keys:
  64. sub make_rsa_pub_key_body {
  65. my $key = shift;
  66. my $timestamp = shift;
  67. my ($n, $e) = $key->get_key_parameters();
  68. return
  69. pack('CN', 4, $timestamp).
  70. pack('C', 1). # RSA
  71. mpi_pack($n).
  72. mpi_pack($e);
  73. }
  74. # expects an RSA key (public or private) and a timestamp
  75. sub fingerprint {
  76. my $key = shift;
  77. my $timestamp = shift;
  78. my $rsabody = make_rsa_pub_key_body($key, $timestamp);
  79. return Digest::SHA1::sha1(pack('Cn', 0x99, length($rsabody)).$rsabody);
  80. }
  81. # FIXME: make tables of relevant identifiers: digest algorithms,
  82. # ciphers, asymmetric crypto, packet types, subpacket types, signature
  83. # types. As these are created, replace the opaque numbers below with
  84. # semantically-meaningful code.
  85. # see RFC 4880 section 5.2.3.21
  86. my $usage_flags = { certify => 0x01,
  87. sign => 0x02,
  88. encrypt_comms => 0x04,
  89. encrypt_storage => 0x08,
  90. encrypt => 0x0c, ## both comms and storage
  91. split => 0x10, # the private key is split via secret sharing
  92. authenticate => 0x20,
  93. shared => 0x80, # more than one person holds the entire private key
  94. };
  95. # we're just not dealing with newline business right now. slurp in
  96. # the whole file.
  97. undef $/;
  98. my $buf = <STDIN>;
  99. my $rsa = Crypt::OpenSSL::RSA->new_private_key($buf);
  100. $rsa->use_sha1_hash();
  101. $rsa->use_no_padding();
  102. if (! $rsa->check_key()) {
  103. die "key does not check";
  104. }
  105. my $version = pack('C', 4);
  106. # strong assertion of identity:
  107. my $sigtype = pack('C', 0x13);
  108. # RSA
  109. my $pubkey_algo = pack('C', 1);
  110. # SHA1
  111. my $hash_algo = pack('C', 2);
  112. # FIXME: i'm worried about generating a bazillion new OpenPGP
  113. # certificates from the same key, which could easily happen if you run
  114. # this script more than once against the same key. How can we prevent
  115. # this?
  116. # could an environment variable (if set) override the current time?
  117. my $timestamp = time();
  118. my $creation_time_packet = pack('CCN', 5, 2, $timestamp);
  119. # FIXME: HARDCODED: what if someone wants to select a different set of
  120. # usage flags? For now, we do only authentication.
  121. my $flags = $usage_flags->{authenticate};
  122. my $usage_packet = pack('CCC', 2, 27, $flags);
  123. # FIXME: HARDCODED: how should we determine how far off to set the
  124. # expiration date? default is to expire in 2 days, which is insanely
  125. # short (but good for testing).
  126. my $expires_in = 86400*2;
  127. my $expiration_packet = pack('CCN', 5, 9, $expires_in);
  128. # prefer AES-256, AES-192, AES-128, CAST5, 3DES:
  129. my $pref_sym_algos = pack('CCCCCCC', 6, 11, 9, 8, 7, 3, 2);
  130. # prefer SHA-1, SHA-256, RIPE-MD/160
  131. my $pref_hash_algos = pack('CCCCC', 4, 21, 2, 8, 3);
  132. # prefer ZLIB, BZip2, ZIP
  133. my $pref_zip_algos = pack('CCCCC', 4, 22, 2, 3, 1);
  134. # we support the MDC feature:
  135. my $features = pack('CCC', 2, 30, 1);
  136. # keyserver preference: only owner modify (???):
  137. my $keyserver_pref = pack('CCC', 2, 23, 0x80);
  138. my $subpackets_to_be_hashed =
  139. $creation_time_packet.
  140. $usage_packet.
  141. $expiration_packet.
  142. $pref_sym_algos.
  143. $pref_hash_algos.
  144. $pref_zip_algos.
  145. $features.
  146. $keyserver_pref;
  147. my $subpacket_octets = pack('n', length($subpackets_to_be_hashed));
  148. my $sig_data_to_be_hashed =
  149. $version.
  150. $sigtype.
  151. $pubkey_algo.
  152. $hash_algo.
  153. $subpacket_octets.
  154. $subpackets_to_be_hashed;
  155. my $pubkey = make_rsa_pub_key_body($rsa, $timestamp);
  156. #open(KEYFILE, "</home/wt215/gpg-test/key-data");
  157. my $key_data = make_packet(6, $pubkey);
  158. # take the last 8 bytes of the fingerprint as the keyid:
  159. my $keyid = substr(fingerprint($rsa, $timestamp), 20 - 8, 8);
  160. # the v4 signature trailer is:
  161. # version number, literal 0xff, and then a 4-byte count of the
  162. # signature data itself.
  163. my $trailer = pack('CCN', 4, 0xff, length($sig_data_to_be_hashed));
  164. my $uid_data =
  165. pack('CN', 0xb4, length($uid)).
  166. $uid;
  167. my $datatosign =
  168. $key_data.
  169. $uid_data.
  170. $sig_data_to_be_hashed.
  171. $trailer;
  172. my $data_hash = Digest::SHA1::sha1_hex($datatosign);
  173. my $issuer_packet = pack('CCa8', 9, 16, $keyid);
  174. my $sig = Crypt::OpenSSL::Bignum->new_from_bin($rsa->sign($datatosign));
  175. my $sig_body =
  176. $sig_data_to_be_hashed.
  177. pack('n', length($issuer_packet)).
  178. $issuer_packet.
  179. pack('n', hex(substr($data_hash, 0, 4))).
  180. mpi_pack($sig);
  181. print
  182. make_packet(6, $pubkey).
  183. make_packet(13, $uid).
  184. make_packet(2, $sig_body);