summaryrefslogtreecommitdiff
path: root/src/keytrans/pem2openpgp
blob: e76ba6f81e36c02db87426262d1450fda5b9d867 (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: replace the opaque numbers below with
  82. # semantically-meaningful references based on these tables.
  83. # see RFC 4880 section 9.1 (ignoring deprecated algorithms for now)
  84. my $asym_algos = { rsa => 1,
  85. elgamal => 16,
  86. dsa => 17,
  87. }
  88. # see RFC 4880 section 9.2
  89. my $ciphers = { plaintext => 0,
  90. idea => 1,
  91. 3des => 2,
  92. cast5 => 3,
  93. blowfish => 4,
  94. aes128 => 7,
  95. aes192 => 8,
  96. aes256 => 9
  97. twofish => 10,
  98. };
  99. # see RFC 4880 section 9.3
  100. my $zips = { uncompressed => 0,
  101. zip => 1,
  102. zlib => 2,
  103. bzip2 => 3,
  104. };
  105. # see RFC 4880 section 9.4
  106. my $digests = { md5 => 1,
  107. sha1 => 2,
  108. ripemd160 => 3,
  109. sha256 => 8,
  110. sha384 => 9,
  111. sha512 => 10,
  112. sha224 => 11,
  113. };
  114. # see RFC 4880 section 5.2.3.21
  115. my $usage_flags = { certify => 0x01,
  116. sign => 0x02,
  117. encrypt_comms => 0x04,
  118. encrypt_storage => 0x08,
  119. encrypt => 0x0c, ## both comms and storage
  120. split => 0x10, # the private key is split via secret sharing
  121. authenticate => 0x20,
  122. shared => 0x80, # more than one person holds the entire private key
  123. };
  124. # see RFC 4880 section 4.3
  125. my $packet_types = { pubkey_enc_session => 1,
  126. sig => 2,
  127. symkey_enc_session => 3,
  128. onepass_sig => 4,
  129. seckey => 5,
  130. pubkey => 6,
  131. sec_subkey => 7,
  132. compressed_data => 8,
  133. symenc_data => 9,
  134. marker => 10,
  135. literal => 11,
  136. trust => 12,
  137. uid => 13,
  138. pub_subkey => 14,
  139. uat => 17,
  140. symenc_w_integrity => 18,
  141. mdc => 19,
  142. };
  143. # see RFC 4880 section 5.2.1
  144. my $sig_types = { binary_doc => 0x00,
  145. text_doc => 0x01,
  146. standalone => 0x02,
  147. generic_certification => 0x10,
  148. persona_certification => 0x11,
  149. casual_certification => 0x12,
  150. positive_certification => 0x13,
  151. subkey_binding => 0x18,
  152. primary_key_binding => 0x19,
  153. key_signature => 0x1f,
  154. key_revocation => 0x20,
  155. subkey_revocation => 0x28,
  156. certification_revocation => 0x30,
  157. timestamp => 0x40,
  158. thirdparty => 0x50,
  159. };
  160. # see RFC 4880 section 5.2.3.1
  161. my $subpacket_types => { sig_creation_time => 2,
  162. sig_expiration_time => 3,
  163. exportable => 4,
  164. trust_sig => 5,
  165. regex => 6,
  166. revocable => 7,
  167. key_expiration_time => 9,
  168. preferred_cipher => 11,
  169. revocation_key => 12,
  170. issuer => 16,
  171. notation => 20,
  172. preferred_digest => 21,
  173. keyserver_prefs => 23,
  174. preferred_keyserver => 24,
  175. primary_uid => 25,
  176. policy_uri => 26,
  177. usage_flags => 27,
  178. signers_uid => 28,
  179. revocation_reason => 29,
  180. features => 30,
  181. signature_target => 31,
  182. embedded_signature = 32,
  183. };
  184. # we're just not dealing with newline business right now. slurp in
  185. # the whole file.
  186. undef $/;
  187. my $buf = <STDIN>;
  188. my $rsa = Crypt::OpenSSL::RSA->new_private_key($buf);
  189. $rsa->use_sha1_hash();
  190. $rsa->use_no_padding();
  191. if (! $rsa->check_key()) {
  192. die "key does not check";
  193. }
  194. my $version = pack('C', 4);
  195. # strong assertion of identity:
  196. my $sigtype = pack('C', 0x13);
  197. # RSA
  198. my $pubkey_algo = pack('C', 1);
  199. # SHA1
  200. my $hash_algo = pack('C', 2);
  201. # FIXME: i'm worried about generating a bazillion new OpenPGP
  202. # certificates from the same key, which could easily happen if you run
  203. # this script more than once against the same key. How can we prevent
  204. # this?
  205. # could an environment variable (if set) override the current time?
  206. my $timestamp = time();
  207. my $creation_time_packet = pack('CCN', 5, 2, $timestamp);
  208. # FIXME: HARDCODED: what if someone wants to select a different set of
  209. # usage flags? For now, we do only authentication.
  210. my $flags = $usage_flags->{authenticate};
  211. my $usage_packet = pack('CCC', 2, 27, $flags);
  212. # FIXME: HARDCODED: how should we determine how far off to set the
  213. # expiration date? default is to expire in 2 days, which is insanely
  214. # short (but good for testing).
  215. my $expires_in = 86400*2;
  216. my $expiration_packet = pack('CCN', 5, 9, $expires_in);
  217. # prefer AES-256, AES-192, AES-128, CAST5, 3DES:
  218. my $pref_sym_algos = pack('CCCCCCC', 6, 11, 9, 8, 7, 3, 2);
  219. # prefer SHA-1, SHA-256, RIPE-MD/160
  220. my $pref_hash_algos = pack('CCCCC', 4, 21, 2, 8, 3);
  221. # prefer ZLIB, BZip2, ZIP
  222. my $pref_zip_algos = pack('CCCCC', 4, 22, 2, 3, 1);
  223. # we support the MDC feature:
  224. my $features = pack('CCC', 2, 30, 1);
  225. # keyserver preference: only owner modify (???):
  226. my $keyserver_pref = pack('CCC', 2, 23, 0x80);
  227. my $subpackets_to_be_hashed =
  228. $creation_time_packet.
  229. $usage_packet.
  230. $expiration_packet.
  231. $pref_sym_algos.
  232. $pref_hash_algos.
  233. $pref_zip_algos.
  234. $features.
  235. $keyserver_pref;
  236. my $subpacket_octets = pack('n', length($subpackets_to_be_hashed));
  237. my $sig_data_to_be_hashed =
  238. $version.
  239. $sigtype.
  240. $pubkey_algo.
  241. $hash_algo.
  242. $subpacket_octets.
  243. $subpackets_to_be_hashed;
  244. my $pubkey = make_rsa_pub_key_body($rsa, $timestamp);
  245. #open(KEYFILE, "</home/wt215/gpg-test/key-data");
  246. my $key_data = make_packet(6, $pubkey);
  247. # take the last 8 bytes of the fingerprint as the keyid:
  248. my $keyid = substr(fingerprint($rsa, $timestamp), 20 - 8, 8);
  249. # the v4 signature trailer is:
  250. # version number, literal 0xff, and then a 4-byte count of the
  251. # signature data itself.
  252. my $trailer = pack('CCN', 4, 0xff, length($sig_data_to_be_hashed));
  253. my $uid_data =
  254. pack('CN', 0xb4, length($uid)).
  255. $uid;
  256. my $datatosign =
  257. $key_data.
  258. $uid_data.
  259. $sig_data_to_be_hashed.
  260. $trailer;
  261. my $data_hash = Digest::SHA1::sha1_hex($datatosign);
  262. my $issuer_packet = pack('CCa8', 9, 16, $keyid);
  263. my $sig = Crypt::OpenSSL::Bignum->new_from_bin($rsa->sign($datatosign));
  264. my $sig_body =
  265. $sig_data_to_be_hashed.
  266. pack('n', length($issuer_packet)).
  267. $issuer_packet.
  268. pack('n', hex(substr($data_hash, 0, 4))).
  269. mpi_pack($sig);
  270. print
  271. make_packet(6, $pubkey).
  272. make_packet(13, $uid).
  273. make_packet(2, $sig_body);