pem2openpgp: clean up comments, treat fingerprint as raw data instead of ascii
[monkeysphere.git] / src / keytrans / pem2openpgp
1 #!/usr/bin/perl -w -T
2
3 # pem2openpgp: take a PEM-encoded RSA private-key on standard input, a
4 # User ID as the first argument, and generate an OpenPGP certificate
5 # from it.
6
7 # Usage:
8
9 # pem2openpgp 'ssh://'$(hostname -f) < /etc/ssh/ssh_host_rsa_key | gpg --import
10
11 # Authors:
12 #  Jameson Rollins <jrollins@finestructure.net>
13 #  Daniel Kahn Gillmor <dkg@fifthhorseman.net>
14
15 # Started on: 2009-01-07 02:01:19-0500
16
17 # License: GPL v3 or later (we may need to adjust this given that this
18 # connects to OpenSSL via perl)
19
20 use strict;
21 use warnings;
22 use Crypt::OpenSSL::RSA;
23 use Crypt::OpenSSL::Bignum;
24 use Digest::SHA1;
25 use MIME::Base64;
26
27 ## make sure all length() and substr() calls use bytes only:
28 use bytes;
29
30 my $uid = shift;
31
32 # FIXME: fail if there is no given user ID; or should we default to
33 # hostname_long() from Sys::Hostname::Long ?
34
35 # make an old-style packet out of the given packet type and body.
36 # old-style  (see RFC 4880 section 4.2)
37 sub make_packet {
38   my $type = shift;
39   my $body = shift;
40
41   my $len = length($body);
42
43   my $lenbytes;
44   my $lencode;
45
46   if ($len < 2**8) {
47     $lenbytes = 0;
48     $lencode = 'C';
49   } elsif ($len < 2**16) {
50     $lenbytes = 1;
51     $lencode = 'n';
52   } elsif ($len < 2**31) {
53     ## not testing against full 32 bits because i don't want to deal
54     ## with potential overflow.
55     $lenbytes = 2;
56     $lencode = 'N';
57   } else {
58     ## what the hell do we do here?
59     $lenbytes = 3;
60     $lencode = '';
61   }
62
63   return pack('C'.$lencode, 0x80 + ($type * 4) + $lenbytes, $len).
64     $body;
65 }
66
67
68 # takes a Crypt::OpenSSL::Bignum, returns it formatted as OpenPGP MPI
69 # (RFC 4880 section 3.2)
70 sub mpi_pack {
71   my $num = shift;
72
73   my $val = $num->to_bin();
74   my $mpilen = length($val)*8;
75
76 # this is a kludgy way to get the number of significant bits in the
77 # first byte:
78   my $bitsinfirstbyte = length(sprintf("%b", ord($val)));
79
80   $mpilen -= (8 - $bitsinfirstbyte);
81
82   return pack('n', $mpilen).$val;
83 }
84
85 # FIXME: genericize this to accept either RSA or DSA keys:
86 sub make_rsa_pub_key_body {
87   my $key = shift;
88   my $timestamp = shift;
89
90   my ($n, $e) = $key->get_key_parameters();
91
92   return
93     pack('CN', 4, $timestamp).
94       pack('C', 1). # RSA
95         mpi_pack($n).
96           mpi_pack($e);
97
98 }
99
100 # expects an RSA key (public or private) and a timestamp
101 sub fingerprint {
102   my $key = shift;
103   my $timestamp = shift;
104
105   my $rsabody = make_rsa_pub_key_body($key, $timestamp);
106
107   return Digest::SHA1::sha1(pack('Cn', 0x99, length($rsabody)).$rsabody);
108 }
109
110 # we're just not dealing with newline business right now.  slurp in
111 # the whole file.
112 undef $/;
113 my $buf = <STDIN>;
114
115
116 my $rsa = Crypt::OpenSSL::RSA->new_private_key($buf);
117
118 $rsa->use_sha1_hash();
119 $rsa->use_no_padding();
120
121 if (! $rsa->check_key()) {
122   die "key does not check";
123 }
124
125 my $version = pack('C', 4);
126 # strong assertion of identity:
127 my $sigtype = pack('C', 0x13);
128 # RSA
129 my $pubkey_algo = pack('C', 1);
130 # SHA1
131 my $hash_algo = pack('C', 2);
132
133 # FIXME: i'm worried about generating a bazillion new OpenPGP
134 # certificates from the same key, which could easily happen if you run
135 # this script more than once against the same key.  How can we prevent
136 # this?
137
138 # could an environment variable (if set) override the current time?
139 my $timestamp = time();
140
141 my $creation_time_packet = pack('CCN', 5, 2, $timestamp);
142
143
144 # usage: signing and certification:
145 my $flags = 0x03;
146 my $usage_packet = pack('CCC', 2, 27, $flags);
147
148
149 # FIXME: HARDCODED: how should we determine how far off to set the
150 # expiration date?  default is to expire in 2 days, which is insanely
151 # short (but good for testing).
152 my $expires_in = 86400*2;
153 my $expiration_packet = pack('CCN', 5, 9, $expires_in);
154
155
156 # prefer AES-256, AES-192, AES-128, CAST5, 3DES:
157 my $pref_sym_algos = pack('CCCCCCC', 6, 11, 9, 8, 7, 3, 2);
158
159 # prefer SHA-1, SHA-256, RIPE-MD/160
160 my $pref_hash_algos = pack('CCCCC', 4, 21, 2, 8, 3);
161
162 # prefer ZLIB, BZip2, ZIP
163 my $pref_zip_algos = pack('CCCCC', 4, 22, 2, 3, 1);
164
165 # we support the MDC feature:
166 my $features = pack('CCC', 2, 30, 1);
167
168 # keyserver preference: only owner modify (???):
169 my $keyserver_pref = pack('CCC', 2, 23, 0x80);
170
171 my $subpackets_to_be_hashed =
172   $creation_time_packet.
173   $usage_packet.
174   $expiration_packet.
175   $pref_sym_algos.
176   $pref_hash_algos.
177   $pref_zip_algos.
178   $features.
179   $keyserver_pref;
180
181 my $subpacket_octets = pack('n', length($subpackets_to_be_hashed));
182
183 my $sig_data_to_be_hashed =
184   $version.
185   $sigtype.
186   $pubkey_algo.
187   $hash_algo.
188   $subpacket_octets.
189   $subpackets_to_be_hashed;
190
191 my $pubkey = make_rsa_pub_key_body($rsa, $timestamp);
192
193 #open(KEYFILE, "</home/wt215/gpg-test/key-data");
194 my $key_data = make_packet(6, $pubkey);
195
196 # take the last 8 bytes of the fingerprint as the keyid:
197 my $keyid = substr(fingerprint($rsa, $timestamp), 20 - 8, 8);
198
199 # the v4 signature trailer is:
200
201 # version number, literal 0xff, and then a 4-byte count of the
202 # signature data itself.
203 my $trailer = pack('CCN', 4, 0xff, length($sig_data_to_be_hashed));
204
205 my $uid_data =
206   pack('CN', 0xb4, length($uid)).
207   $uid;
208
209 my $datatosign =
210   $key_data.
211   $uid_data.
212   $sig_data_to_be_hashed.
213   $trailer;
214
215 my $data_hash = Digest::SHA1::sha1_hex($datatosign);
216
217
218 my $issuer_packet = pack('CCa8', 9, 16, $keyid);
219
220 my $sig = Crypt::OpenSSL::Bignum->new_from_bin($rsa->sign($datatosign));
221
222 my $sig_body =
223   $sig_data_to_be_hashed.
224   pack('n', length($issuer_packet)).
225   $issuer_packet.
226   pack('n', hex(substr($data_hash, 0, 4))).
227   mpi_pack($sig);
228
229 print
230   make_packet(6, $pubkey).
231   make_packet(13, $uid).
232   make_packet(2, $sig_body);
233
234