summaryrefslogtreecommitdiff
path: root/src/keytrans/pem2openpgp
blob: fa92297218db594ae5d9d82e819cddf6086a5d57 (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. # see RFC 4880 section 9.1 (ignoring deprecated algorithms for now)
  25. my $asym_algos = { rsa => 1,
  26. elgamal => 16,
  27. dsa => 17,
  28. };
  29. # see RFC 4880 section 9.2
  30. my $ciphers = { plaintext => 0,
  31. idea => 1,
  32. tripledes => 2,
  33. cast5 => 3,
  34. blowfish => 4,
  35. aes128 => 7,
  36. aes192 => 8,
  37. aes256 => 9,
  38. twofish => 10,
  39. };
  40. # see RFC 4880 section 9.3
  41. my $zips = { uncompressed => 0,
  42. zip => 1,
  43. zlib => 2,
  44. bzip2 => 3,
  45. };
  46. # see RFC 4880 section 9.4
  47. my $digests = { md5 => 1,
  48. sha1 => 2,
  49. ripemd160 => 3,
  50. sha256 => 8,
  51. sha384 => 9,
  52. sha512 => 10,
  53. sha224 => 11,
  54. };
  55. # see RFC 4880 section 5.2.3.21
  56. my $usage_flags = { certify => 0x01,
  57. sign => 0x02,
  58. encrypt_comms => 0x04,
  59. encrypt_storage => 0x08,
  60. encrypt => 0x0c, ## both comms and storage
  61. split => 0x10, # the private key is split via secret sharing
  62. authenticate => 0x20,
  63. shared => 0x80, # more than one person holds the entire private key
  64. };
  65. # see RFC 4880 section 4.3
  66. my $packet_types = { pubkey_enc_session => 1,
  67. sig => 2,
  68. symkey_enc_session => 3,
  69. onepass_sig => 4,
  70. seckey => 5,
  71. pubkey => 6,
  72. sec_subkey => 7,
  73. compressed_data => 8,
  74. symenc_data => 9,
  75. marker => 10,
  76. literal => 11,
  77. trust => 12,
  78. uid => 13,
  79. pub_subkey => 14,
  80. uat => 17,
  81. symenc_w_integrity => 18,
  82. mdc => 19,
  83. };
  84. # see RFC 4880 section 5.2.1
  85. my $sig_types = { binary_doc => 0x00,
  86. text_doc => 0x01,
  87. standalone => 0x02,
  88. generic_certification => 0x10,
  89. persona_certification => 0x11,
  90. casual_certification => 0x12,
  91. positive_certification => 0x13,
  92. subkey_binding => 0x18,
  93. primary_key_binding => 0x19,
  94. key_signature => 0x1f,
  95. key_revocation => 0x20,
  96. subkey_revocation => 0x28,
  97. certification_revocation => 0x30,
  98. timestamp => 0x40,
  99. thirdparty => 0x50,
  100. };
  101. # see RFC 4880 section 5.2.3.1
  102. my $subpacket_types = { sig_creation_time => 2,
  103. sig_expiration_time => 3,
  104. exportable => 4,
  105. trust_sig => 5,
  106. regex => 6,
  107. revocable => 7,
  108. key_expiration_time => 9,
  109. preferred_cipher => 11,
  110. revocation_key => 12,
  111. issuer => 16,
  112. notation => 20,
  113. preferred_digest => 21,
  114. preferred_compression => 22,
  115. keyserver_prefs => 23,
  116. preferred_keyserver => 24,
  117. primary_uid => 25,
  118. policy_uri => 26,
  119. usage_flags => 27,
  120. signers_uid => 28,
  121. revocation_reason => 29,
  122. features => 30,
  123. signature_target => 31,
  124. embedded_signature => 32,
  125. };
  126. # bitstring (see RFC 4880 section 5.2.3.24)
  127. my $features = { mdc => 0x01
  128. };
  129. # bitstring (see RFC 4880 5.2.3.17)
  130. my $keyserver_prefs = { nomodify => 0x80
  131. };
  132. ###### end lookup tables ######
  133. # FIXME: if we want to be able to interpret openpgp data as well as
  134. # produce it, we need to produce key/value-swapped lookup tables as well.
  135. # make an old-style packet out of the given packet type and body.
  136. # old-style (see RFC 4880 section 4.2)
  137. sub make_packet {
  138. my $type = shift;
  139. my $body = shift;
  140. my $len = length($body);
  141. my $lenbytes;
  142. my $lencode;
  143. if ($len < 2**8) {
  144. $lenbytes = 0;
  145. $lencode = 'C';
  146. } elsif ($len < 2**16) {
  147. $lenbytes = 1;
  148. $lencode = 'n';
  149. } elsif ($len < 2**31) {
  150. ## not testing against full 32 bits because i don't want to deal
  151. ## with potential overflow.
  152. $lenbytes = 2;
  153. $lencode = 'N';
  154. } else {
  155. ## what the hell do we do here?
  156. $lenbytes = 3;
  157. $lencode = '';
  158. }
  159. return pack('C'.$lencode, 0x80 + ($type * 4) + $lenbytes, $len).
  160. $body;
  161. }
  162. # takes a Crypt::OpenSSL::Bignum, returns it formatted as OpenPGP MPI
  163. # (RFC 4880 section 3.2)
  164. sub mpi_pack {
  165. my $num = shift;
  166. my $val = $num->to_bin();
  167. my $mpilen = length($val)*8;
  168. # this is a kludgy way to get the number of significant bits in the
  169. # first byte:
  170. my $bitsinfirstbyte = length(sprintf("%b", ord($val)));
  171. $mpilen -= (8 - $bitsinfirstbyte);
  172. return pack('n', $mpilen).$val;
  173. }
  174. # see the bottom of page 43 of RFC 4880
  175. sub simple_checksum {
  176. my $bytes = shift;
  177. return unpack("%C*",$bytes) % 65536;
  178. }
  179. # FIXME: genericize these to accept either RSA or DSA keys:
  180. sub make_rsa_pub_key_body {
  181. my $key = shift;
  182. my $timestamp = shift;
  183. my ($n, $e) = $key->get_key_parameters();
  184. return
  185. pack('CN', 4, $timestamp).
  186. pack('C', $asym_algos->{rsa}).
  187. mpi_pack($n).
  188. mpi_pack($e);
  189. }
  190. sub make_rsa_sec_key_body {
  191. my $key = shift;
  192. my $timestamp = shift;
  193. # we're not using $a and $b, but we need them to get to $c.
  194. my ($n, $e, $d, $p, $q, $a, $b, $c) = $key->get_key_parameters();
  195. my $secret_material = mpi_pack($d).
  196. mpi_pack($p).
  197. mpi_pack($q).
  198. mpi_pack($c);
  199. # FIXME: according to Crypt::OpenSSL::RSA, $c is 1/q mod p; but
  200. # according to sec 5.5.3 of RFC 4880, this last argument should
  201. # instead be: u, the multiplicative inverse of p, mod q. i don't
  202. # see a simple way to generate this number from the perl module
  203. # directly yet.
  204. return
  205. pack('CN', 4, $timestamp).
  206. pack('C', $asym_algos->{rsa}).
  207. mpi_pack($n).
  208. mpi_pack($e).
  209. pack('C', 0). # seckey material is not encrypted -- see RFC 4880 sec 5.5.3
  210. $secret_material.
  211. simple_checksum($secret_material);
  212. }
  213. # expects an RSA key (public or private) and a timestamp
  214. sub fingerprint {
  215. my $key = shift;
  216. my $timestamp = shift;
  217. my $rsabody = make_rsa_pub_key_body($key, $timestamp);
  218. return Digest::SHA1::sha1(pack('Cn', 0x99, length($rsabody)).$rsabody);
  219. }
  220. # we're just not dealing with newline business right now. slurp in
  221. # the whole file.
  222. undef $/;
  223. my $buf = <STDIN>;
  224. my $rsa = Crypt::OpenSSL::RSA->new_private_key($buf);
  225. $rsa->use_sha1_hash();
  226. # see page 22 of RFC 4880 for why i think this is the right padding
  227. # choice to use:
  228. $rsa->use_pkcs1_padding();
  229. if (! $rsa->check_key()) {
  230. die "key does not check";
  231. }
  232. my $version = pack('C', 4);
  233. # strong assertion of identity:
  234. my $sigtype = pack('C', $sig_types->{positive_certification});
  235. # RSA
  236. my $pubkey_algo = pack('C', $asym_algos->{rsa});
  237. # SHA1
  238. my $hash_algo = pack('C', $digests->{sha1});
  239. # FIXME: i'm worried about generating a bazillion new OpenPGP
  240. # certificates from the same key, which could easily happen if you run
  241. # this script more than once against the same key. How can we prevent
  242. # this?
  243. # could an environment variable (if set) override the current time?
  244. my $timestamp = time();
  245. my $creation_time_packet = pack('CCN', 5, $subpacket_types->{sig_creation_time}, $timestamp);
  246. # FIXME: HARDCODED: what if someone wants to select a different set of
  247. # usage flags? For now, we do only authentication.
  248. my $usage_packet = pack('CCC', 2, $subpacket_types->{usage_flags}, $usage_flags->{authenticate});
  249. # FIXME: HARDCODED: how should we determine how far off to set the
  250. # expiration date? default is to expire in 2 days, which is insanely
  251. # short (but good for testing).
  252. my $expires_in = 86400*2;
  253. my $expiration_packet = pack('CCN', 5, $subpacket_types->{key_expiration_time}, $expires_in);
  254. # prefer AES-256, AES-192, AES-128, CAST5, 3DES:
  255. my $pref_sym_algos = pack('CCCCCCC', 6, $subpacket_types->{preferred_cipher},
  256. $ciphers->{aes256},
  257. $ciphers->{aes192},
  258. $ciphers->{aes128},
  259. $ciphers->{cast5},
  260. $ciphers->{tripledes}
  261. );
  262. # prefer SHA-1, SHA-256, RIPE-MD/160
  263. my $pref_hash_algos = pack('CCCCC', 4, $subpacket_types->{preferred_digest},
  264. $digests->{sha1},
  265. $digests->{sha256},
  266. $digests->{ripemd160}
  267. );
  268. # prefer ZLIB, BZip2, ZIP
  269. my $pref_zip_algos = pack('CCCCC', 4, $subpacket_types->{preferred_compression},
  270. $zips->{zlib},
  271. $zips->{bzip2},
  272. $zips->{zip}
  273. );
  274. # we support the MDC feature:
  275. my $feature_subpacket = pack('CCC', 2, $subpacket_types->{features},
  276. $features->{mdc});
  277. # keyserver preference: only owner modify (???):
  278. my $keyserver_pref = pack('CCC', 2, $subpacket_types->{keyserver_prefs},
  279. $keyserver_prefs->{nomodify});
  280. my $subpackets_to_be_hashed =
  281. $creation_time_packet.
  282. $usage_packet.
  283. $expiration_packet.
  284. $pref_sym_algos.
  285. $pref_hash_algos.
  286. $pref_zip_algos.
  287. $feature_subpacket.
  288. $keyserver_pref;
  289. my $subpacket_octets = pack('n', length($subpackets_to_be_hashed));
  290. my $sig_data_to_be_hashed =
  291. $version.
  292. $sigtype.
  293. $pubkey_algo.
  294. $hash_algo.
  295. $subpacket_octets.
  296. $subpackets_to_be_hashed;
  297. my $pubkey = make_rsa_pub_key_body($rsa, $timestamp);
  298. my $seckey = make_rsa_sec_key_body($rsa, $timestamp);
  299. my $key_data = make_packet($packet_types->{pubkey}, $pubkey);
  300. # take the last 8 bytes of the fingerprint as the keyid:
  301. my $keyid = substr(fingerprint($rsa, $timestamp), 20 - 8, 8);
  302. # the v4 signature trailer is:
  303. # version number, literal 0xff, and then a 4-byte count of the
  304. # signature data itself.
  305. my $trailer = pack('CCN', 4, 0xff, length($sig_data_to_be_hashed));
  306. my $uid_data =
  307. pack('CN', 0xb4, length($uid)).
  308. $uid;
  309. my $datatosign =
  310. $key_data.
  311. $uid_data.
  312. $sig_data_to_be_hashed.
  313. $trailer;
  314. my $data_hash = Digest::SHA1::sha1_hex($datatosign);
  315. my $issuer_packet = pack('CCa8', 9, $subpacket_types->{issuer}, $keyid);
  316. my $sig = Crypt::OpenSSL::Bignum->new_from_bin($rsa->sign($datatosign));
  317. my $sig_body =
  318. $sig_data_to_be_hashed.
  319. pack('n', length($issuer_packet)).
  320. $issuer_packet.
  321. pack('n', hex(substr($data_hash, 0, 4))).
  322. mpi_pack($sig);
  323. print
  324. make_packet($packet_types->{pubkey}, $pubkey).
  325. make_packet($packet_types->{uid}, $uid).
  326. make_packet($packet_types->{sig}, $sig_body);