summaryrefslogtreecommitdiff
path: root/src/keytrans/pem2openpgp
blob: 637eba2a225fecbc399a64e3ad6433461b63a36c (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. tripledes => 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. preferred_compression => 22,
  174. keyserver_prefs => 23,
  175. preferred_keyserver => 24,
  176. primary_uid => 25,
  177. policy_uri => 26,
  178. usage_flags => 27,
  179. signers_uid => 28,
  180. revocation_reason => 29,
  181. features => 30,
  182. signature_target => 31,
  183. embedded_signature => 32,
  184. };
  185. # bitstring (see RFC 4880 section 5.2.3.24)
  186. my $features = { mdc => 0x01
  187. };
  188. # bitstring (see RFC 4880 5.2.3.17)
  189. my $keyserver_prefs = { nomodify => 0x80
  190. };
  191. # we're just not dealing with newline business right now. slurp in
  192. # the whole file.
  193. undef $/;
  194. my $buf = <STDIN>;
  195. my $rsa = Crypt::OpenSSL::RSA->new_private_key($buf);
  196. $rsa->use_sha1_hash();
  197. # see page 22 of RFC 4880 for why i think this is the right padding
  198. # choice to use:
  199. $rsa->use_pkcs1_padding();
  200. if (! $rsa->check_key()) {
  201. die "key does not check";
  202. }
  203. my $version = pack('C', 4);
  204. # strong assertion of identity:
  205. my $sigtype = pack('C', $sig_types->{positive_certification});
  206. # RSA
  207. my $pubkey_algo = pack('C', $asym_algos->{rsa});
  208. # SHA1
  209. my $hash_algo = pack('C', $digests->{sha1});
  210. # FIXME: i'm worried about generating a bazillion new OpenPGP
  211. # certificates from the same key, which could easily happen if you run
  212. # this script more than once against the same key. How can we prevent
  213. # this?
  214. # could an environment variable (if set) override the current time?
  215. my $timestamp = time();
  216. my $creation_time_packet = pack('CCN', 5, $subpacket_types->{sig_creation_time}, $timestamp);
  217. # FIXME: HARDCODED: what if someone wants to select a different set of
  218. # usage flags? For now, we do only authentication.
  219. my $usage_packet = pack('CCC', 2, $subpacket_types->{usage_flags}, $usage_flags->{authenticate});
  220. # FIXME: HARDCODED: how should we determine how far off to set the
  221. # expiration date? default is to expire in 2 days, which is insanely
  222. # short (but good for testing).
  223. my $expires_in = 86400*2;
  224. my $expiration_packet = pack('CCN', 5, $subpacket_types->{key_expiration_time}, $expires_in);
  225. # prefer AES-256, AES-192, AES-128, CAST5, 3DES:
  226. my $pref_sym_algos = pack('CCCCCCC', 6, $subpacket_types->{preferred_cipher},
  227. $ciphers->{aes256},
  228. $ciphers->{aes192},
  229. $ciphers->{aes128},
  230. $ciphers->{cast5},
  231. $ciphers->{tripledes}
  232. );
  233. # prefer SHA-1, SHA-256, RIPE-MD/160
  234. my $pref_hash_algos = pack('CCCCC', 4, $subpacket_types->{preferred_digest},
  235. $digests->{sha1},
  236. $digests->{sha256},
  237. $digests->{ripemd160}
  238. );
  239. # prefer ZLIB, BZip2, ZIP
  240. my $pref_zip_algos = pack('CCCCC', 4, $subpacket_types->{preferred_compression},
  241. $zips->{zlib},
  242. $zips->{bzip2},
  243. $zips->{zip}
  244. );
  245. # we support the MDC feature:
  246. my $feature_subpacket = pack('CCC', 2, $subpacket_types->{features},
  247. $features->{mdc});
  248. # keyserver preference: only owner modify (???):
  249. my $keyserver_pref = pack('CCC', 2, $subpacket_types->{keyserver_prefs},
  250. $keyserver_prefs->{nomodify});
  251. my $subpackets_to_be_hashed =
  252. $creation_time_packet.
  253. $usage_packet.
  254. $expiration_packet.
  255. $pref_sym_algos.
  256. $pref_hash_algos.
  257. $pref_zip_algos.
  258. $feature_subpacket.
  259. $keyserver_pref;
  260. my $subpacket_octets = pack('n', length($subpackets_to_be_hashed));
  261. my $sig_data_to_be_hashed =
  262. $version.
  263. $sigtype.
  264. $pubkey_algo.
  265. $hash_algo.
  266. $subpacket_octets.
  267. $subpackets_to_be_hashed;
  268. my $pubkey = make_rsa_pub_key_body($rsa, $timestamp);
  269. my $key_data = make_packet($packet_types->{pubkey}, $pubkey);
  270. # take the last 8 bytes of the fingerprint as the keyid:
  271. my $keyid = substr(fingerprint($rsa, $timestamp), 20 - 8, 8);
  272. # the v4 signature trailer is:
  273. # version number, literal 0xff, and then a 4-byte count of the
  274. # signature data itself.
  275. my $trailer = pack('CCN', 4, 0xff, length($sig_data_to_be_hashed));
  276. my $uid_data =
  277. pack('CN', 0xb4, length($uid)).
  278. $uid;
  279. my $datatosign =
  280. $key_data.
  281. $uid_data.
  282. $sig_data_to_be_hashed.
  283. $trailer;
  284. my $data_hash = Digest::SHA1::sha1_hex($datatosign);
  285. my $issuer_packet = pack('CCa8', 9, $subpacket_types->{issuer}, $keyid);
  286. my $sig = Crypt::OpenSSL::Bignum->new_from_bin($rsa->sign($datatosign));
  287. my $sig_body =
  288. $sig_data_to_be_hashed.
  289. pack('n', length($issuer_packet)).
  290. $issuer_packet.
  291. pack('n', hex(substr($data_hash, 0, 4))).
  292. mpi_pack($sig);
  293. print
  294. make_packet($packet_types->{pubkey}, $pubkey).
  295. make_packet($packet_types->{uid}, $uid).
  296. make_packet($packet_types->{sig}, $sig_body);