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