summaryrefslogtreecommitdiff
path: root/src/keytrans/pem2openpgp
blob: f6e2d4f33d3f79f2cbc7d59fa61dba06936a8dbb (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. my $len = length($body);
  25. my $lenbytes;
  26. my $lencode;
  27. if ($len < 2**8) {
  28. $lenbytes = 0;
  29. $lencode = 'C';
  30. } elsif ($len < 2**16) {
  31. $lenbytes = 1;
  32. $lencode = 'n';
  33. } elsif ($len < 2**31) {
  34. ## not testing against full 32 bits because i don't want to deal
  35. ## with potential overflow.
  36. $lenbytes = 2;
  37. $lencode = 'N';
  38. } else {
  39. ## what the hell do we do here?
  40. $lenbytes = 3;
  41. $lencode = '';
  42. }
  43. return pack('C'.$lencode, 0x80 + ($type * 4) + $lenbytes, $len).
  44. $body;
  45. }
  46. # takes a Crypt::OpenSSL::Bignum, returns it formatted as OpenPGP MPI
  47. # (RFC 4880 section 3.2)
  48. sub mpi_pack {
  49. my $num = shift;
  50. my $val = $num->to_bin();
  51. my $mpilen = length($val)*8;
  52. # this is a kludgy way to get the number of significant bits in the
  53. # first byte:
  54. my $bitsinfirstbyte = length(sprintf("%b", ord($val)));
  55. $mpilen -= (8 - $bitsinfirstbyte);
  56. return pack('n', $mpilen).$val;
  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. my $subpacket_octets = pack('n', length($subpackets_to_be_hashed));
  121. my $sig_data_to_be_hashed =
  122. $version.
  123. $sigtype.
  124. $pubkey_algo.
  125. $hash_algo.
  126. $subpacket_octets.
  127. $subpackets_to_be_hashed;
  128. my $pubkey = make_rsa_key_body($rsa, $timestamp);
  129. #open(KEYFILE, "</home/wt215/gpg-test/key-data");
  130. my $key_data = make_packet(6, $pubkey);
  131. # take the last 16 characters of the fingerprint as the keyid:
  132. my $keyid = substr(fingerprint($rsa, $timestamp), 40 - 16, 16);
  133. # the v4 signature trailer is:
  134. # version number, literal 0xff, and then a 4-byte count of the
  135. # signature data itself.
  136. my $trailer = pack('CCN', 4, 0xff, length($sig_data_to_be_hashed));
  137. my $uid_data =
  138. pack('CN', 0xb4, length($uid)).
  139. $uid;
  140. my $datatosign =
  141. $key_data.
  142. $uid_data.
  143. $sig_data_to_be_hashed.
  144. $trailer;
  145. my $data_hash = Digest::SHA1::sha1_hex($datatosign);
  146. my $issuer_packet = pack('CCH16', 9, 16, $keyid);
  147. my $sig = Crypt::OpenSSL::Bignum->new_from_bin($rsa->sign($datatosign));
  148. my $sig_body =
  149. $sig_data_to_be_hashed.
  150. pack('n', length($issuer_packet)).
  151. $issuer_packet.
  152. pack('n', hex(substr($data_hash, 0, 4))).
  153. mpi_pack($sig);
  154. print make_packet(6, $pubkey);
  155. print make_packet(13, $uid);
  156. print make_packet(2, $sig_body);
  157. $/ = $holdTerminator;