pem2openpgp now accepts a choice of User ID on stdin.
[monkeysphere.git] / src / keytrans / pem2openpgp
index 1575671eab27be5b2bf6075679dbe5e39c430ba4..3fdc469ac95df1c07fcd694fc4a5d3a0877e743f 100755 (executable)
@@ -23,13 +23,16 @@ use MIME::Base64;
 ## make sure all length() and substr() calls use bytes only:
 use bytes;
 
+my $uid = shift;
+
+# FIXME: fail if there is no given user ID.
+
 # make an old-style packet out of the given packet type and body.
 # old-style  (see RFC 4880 section 4.2)
 sub make_packet {
   my $type = shift;
   my $body = shift;
 
-# FIXME: yet another length():
   my $len = length($body);
 
   my $lenbytes;
@@ -62,17 +65,18 @@ sub make_packet {
 sub mpi_pack {
   my $num = shift;
 
-  my $hex = $num->to_hex();
+  my $val = $num->to_bin();
+  my $mpilen = length($val)*8;
 
-  my $mpilen = length($hex)*4;
-
-# this is a kludgy way to get the number of bits in the first byte:
-  my $bitsinfirstbyte = length(sprintf("%b", hex(substr $hex, 0, 2)));
+# this is a kludgy way to get the number of significant bits in the
+# first byte:
+  my $bitsinfirstbyte = length(sprintf("%b", ord($val)));
 
   $mpilen -= (8 - $bitsinfirstbyte);
 
-  return pack('n', $mpilen).$num->to_bin();
+  return pack('n', $mpilen).$val;
 }
+
 # FIXME: genericize this to accept either RSA or DSA keys:
 sub make_rsa_key_body {
   my $key = shift;
@@ -112,10 +116,6 @@ if (! $rsa->check_key()) {
   die "key does not check";
 }
 
-my $uid = 'fake key (do not use) <test@example.org>';
-
-
-
 my $version = pack('C', 4);
 # strong assertion of identity:
 my $sigtype = pack('C', 0x13);
@@ -166,7 +166,6 @@ my $subpackets_to_be_hashed =
   $features.
   $keyserver_pref;
 
-#FIXME: what's the right way to get length()?
 my $subpacket_octets = pack('n', length($subpackets_to_be_hashed));
 
 my $sig_data_to_be_hashed =
@@ -191,8 +190,6 @@ my $keyid = substr(fingerprint($rsa, $timestamp), 40 - 16, 16);
 # signature data itself.
 my $trailer = pack('CCN', 4, 0xff, length($sig_data_to_be_hashed));
 
-# FIXME: length() is probably not right here either in the event that
-# the uid uses unicode.
 my $uid_data =
   pack('CN', 0xb4, length($uid)).
   $uid;
@@ -212,7 +209,6 @@ my $sig = Crypt::OpenSSL::Bignum->new_from_bin($rsa->sign($datatosign));
 
 my $sig_body =
   $sig_data_to_be_hashed.
-# FIXME: another dubious length() call.
   pack('n', length($issuer_packet)).
   $issuer_packet.
   pack('n', hex(substr($data_hash, 0, 4))).