summaryrefslogtreecommitdiff
path: root/src/keytrans/pem2openpgp
blob: 1575671eab27be5b2bf6075679dbe5e39c430ba4 (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. # Authors:
  6. # Jameson Rollins <jrollins@finestructure.net>
  7. # Daniel Kahn Gillmor <dkg@fifthhorseman.net>
  8. # Started on: 2009-01-07 02:01:19-0500
  9. # License: GPL v3 or later (we may need to adjust this given that this
  10. # connects to OpenSSL via perl)
  11. use strict;
  12. use warnings;
  13. use Crypt::OpenSSL::RSA;
  14. use Crypt::OpenSSL::Bignum;
  15. use Digest::SHA1;
  16. use MIME::Base64;
  17. ## make sure all length() and substr() calls use bytes only:
  18. use bytes;
  19. # make an old-style packet out of the given packet type and body.
  20. # old-style (see RFC 4880 section 4.2)
  21. sub make_packet {
  22. my $type = shift;
  23. my $body = shift;
  24. # FIXME: yet another length():
  25. my $len = length($body);
  26. my $lenbytes;
  27. my $lencode;
  28. if ($len < 2**8) {
  29. $lenbytes = 0;
  30. $lencode = 'C';
  31. } elsif ($len < 2**16) {
  32. $lenbytes = 1;
  33. $lencode = 'n';
  34. } elsif ($len < 2**31) {
  35. ## not testing against full 32 bits because i don't want to deal
  36. ## with potential overflow.
  37. $lenbytes = 2;
  38. $lencode = 'N';
  39. } else {
  40. ## what the hell do we do here?
  41. $lenbytes = 3;
  42. $lencode = '';
  43. }
  44. return pack('C'.$lencode, 0x80 + ($type * 4) + $lenbytes, $len).
  45. $body;
  46. }
  47. # takes a Crypt::OpenSSL::Bignum, returns it formatted as OpenPGP MPI
  48. # (RFC 4880 section 3.2)
  49. sub mpi_pack {
  50. my $num = shift;
  51. my $hex = $num->to_hex();
  52. my $mpilen = length($hex)*4;
  53. # this is a kludgy way to get the number of bits in the first byte:
  54. my $bitsinfirstbyte = length(sprintf("%b", hex(substr $hex, 0, 2)));
  55. $mpilen -= (8 - $bitsinfirstbyte);
  56. return pack('n', $mpilen).$num->to_bin();
  57. }
  58. # FIXME: genericize this to accept either RSA or DSA keys:
  59. sub make_rsa_key_body {
  60. my $key = shift;
  61. my $timestamp = shift;
  62. my ($n, $e) = $key->get_key_parameters();
  63. return
  64. pack('CN', 4, $timestamp).
  65. pack('C', 1). # RSA
  66. mpi_pack($n).
  67. mpi_pack($e);
  68. }
  69. # expects an RSA key (public or private) and a timestamp
  70. sub fingerprint {
  71. my $key = shift;
  72. my $timestamp = shift;
  73. my $rsabody = make_rsa_key_body($key, $timestamp);
  74. return Digest::SHA1::sha1_hex(pack('Cn', 0x99, length($rsabody)).$rsabody);
  75. }
  76. my $holdTerminator = $/;
  77. undef $/;
  78. my $buf = <STDIN>;
  79. my $rsa = Crypt::OpenSSL::RSA->new_private_key($buf);
  80. $rsa->use_sha1_hash();
  81. $rsa->use_no_padding();
  82. if (! $rsa->check_key()) {
  83. die "key does not check";
  84. }
  85. my $uid = 'fake key (do not use) <test@example.org>';
  86. my $version = pack('C', 4);
  87. # strong assertion of identity:
  88. my $sigtype = pack('C', 0x13);
  89. # RSA
  90. my $pubkey_algo = pack('C', 1);
  91. # SHA1
  92. my $hash_algo = pack('C', 2);
  93. my $timestamp = 1231003584;
  94. my $creation_time_packet = pack('CCN', 5, 2, $timestamp);
  95. # usage: signing and certification:
  96. my $flags = 0x03;
  97. my $usage_packet = pack('CCC', 2, 27, $flags);
  98. # expire in 2 days:
  99. my $expires_in = 86400*2;
  100. my $expiration_packet = pack('CCN', 5, 9, $expires_in);
  101. # prefer AES-256, AES-192, AES-128, CAST5, 3DES:
  102. my $pref_sym_algos = pack('CCCCCCC', 6, 11, 9, 8, 7, 3, 2);
  103. # prefer SHA-1, SHA-256, RIPE-MD/160
  104. my $pref_hash_algos = pack('CCCCC', 4, 21, 2, 8, 3);
  105. # prefer ZLIB, BZip2, ZIP
  106. my $pref_zip_algos = pack('CCCCC', 4, 22, 2, 3, 1);
  107. # we support the MDC feature:
  108. my $features = pack('CCC', 2, 30, 1);
  109. # keyserver preference: only owner modify (???):
  110. my $keyserver_pref = pack('CCC', 2, 23, 0x80);
  111. my $subpackets_to_be_hashed =
  112. $creation_time_packet.
  113. $usage_packet.
  114. $expiration_packet.
  115. $pref_sym_algos.
  116. $pref_hash_algos.
  117. $pref_zip_algos.
  118. $features.
  119. $keyserver_pref;
  120. #FIXME: what's the right way to get length()?
  121. my $subpacket_octets = pack('n', length($subpackets_to_be_hashed));
  122. my $sig_data_to_be_hashed =
  123. $version.
  124. $sigtype.
  125. $pubkey_algo.
  126. $hash_algo.
  127. $subpacket_octets.
  128. $subpackets_to_be_hashed;
  129. my $pubkey = make_rsa_key_body($rsa, $timestamp);
  130. #open(KEYFILE, "</home/wt215/gpg-test/key-data");
  131. my $key_data = make_packet(6, $pubkey);
  132. # take the last 16 characters of the fingerprint as the keyid:
  133. my $keyid = substr(fingerprint($rsa, $timestamp), 40 - 16, 16);
  134. # the v4 signature trailer is:
  135. # version number, literal 0xff, and then a 4-byte count of the
  136. # signature data itself.
  137. my $trailer = pack('CCN', 4, 0xff, length($sig_data_to_be_hashed));
  138. # FIXME: length() is probably not right here either in the event that
  139. # the uid uses unicode.
  140. my $uid_data =
  141. pack('CN', 0xb4, length($uid)).
  142. $uid;
  143. my $datatosign =
  144. $key_data.
  145. $uid_data.
  146. $sig_data_to_be_hashed.
  147. $trailer;
  148. my $data_hash = Digest::SHA1::sha1_hex($datatosign);
  149. my $issuer_packet = pack('CCH16', 9, 16, $keyid);
  150. my $sig = Crypt::OpenSSL::Bignum->new_from_bin($rsa->sign($datatosign));
  151. my $sig_body =
  152. $sig_data_to_be_hashed.
  153. # FIXME: another dubious length() call.
  154. pack('n', length($issuer_packet)).
  155. $issuer_packet.
  156. pack('n', hex(substr($data_hash, 0, 4))).
  157. mpi_pack($sig);
  158. print make_packet(6, $pubkey);
  159. print make_packet(13, $uid);
  160. print make_packet(2, $sig_body);
  161. $/ = $holdTerminator;