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
9 # pem2openpgp 'ssh://'$(hostname -f) < /etc/ssh/ssh_host_rsa_key | gpg --import
12 # Jameson Rollins <jrollins@finestructure.net>
13 # Daniel Kahn Gillmor <dkg@fifthhorseman.net>
15 # Started on: 2009-01-07 02:01:19-0500
17 # License: GPL v3 or later (we may need to adjust this given that this
18 # connects to OpenSSL via perl)
22 use Crypt::OpenSSL::RSA;
23 use Crypt::OpenSSL::Bignum;
27 ## make sure all length() and substr() calls use bytes only:
32 # FIXME: fail if there is no given user ID; or should we default to
33 # hostname_long() from Sys::Hostname::Long ?
37 # see RFC 4880 section 9.1 (ignoring deprecated algorithms for now)
38 my $asym_algos = { rsa => 1,
43 # see RFC 4880 section 9.2
44 my $ciphers = { plaintext => 0,
55 # see RFC 4880 section 9.3
56 my $zips = { uncompressed => 0,
62 # see RFC 4880 section 9.4
63 my $digests = { md5 => 1,
72 # see RFC 4880 section 5.2.3.21
73 my $usage_flags = { certify => 0x01,
75 encrypt_comms => 0x04,
76 encrypt_storage => 0x08,
77 encrypt => 0x0c, ## both comms and storage
78 split => 0x10, # the private key is split via secret sharing
80 shared => 0x80, # more than one person holds the entire private key
83 # see RFC 4880 section 4.3
84 my $packet_types = { pubkey_enc_session => 1,
86 symkey_enc_session => 3,
99 symenc_w_integrity => 18,
103 # see RFC 4880 section 5.2.1
104 my $sig_types = { binary_doc => 0x00,
107 generic_certification => 0x10,
108 persona_certification => 0x11,
109 casual_certification => 0x12,
110 positive_certification => 0x13,
111 subkey_binding => 0x18,
112 primary_key_binding => 0x19,
113 key_signature => 0x1f,
114 key_revocation => 0x20,
115 subkey_revocation => 0x28,
116 certification_revocation => 0x30,
122 # see RFC 4880 section 5.2.3.1
123 my $subpacket_types = { sig_creation_time => 2,
124 sig_expiration_time => 3,
129 key_expiration_time => 9,
130 preferred_cipher => 11,
131 revocation_key => 12,
134 preferred_digest => 21,
135 preferred_compression => 22,
136 keyserver_prefs => 23,
137 preferred_keyserver => 24,
142 revocation_reason => 29,
144 signature_target => 31,
145 embedded_signature => 32,
148 # bitstring (see RFC 4880 section 5.2.3.24)
149 my $features = { mdc => 0x01
152 # bitstring (see RFC 4880 5.2.3.17)
153 my $keyserver_prefs = { nomodify => 0x80
156 ###### end lookup tables ######
158 # FIXME: if we want to be able to interpret openpgp data as well as
159 # produce it, we need to produce key/value-swapped lookup tables as well.
162 # make an old-style packet out of the given packet type and body.
163 # old-style (see RFC 4880 section 4.2)
168 my $len = length($body);
176 } elsif ($len < 2**16) {
179 } elsif ($len < 2**31) {
180 ## not testing against full 32 bits because i don't want to deal
181 ## with potential overflow.
185 ## what the hell do we do here?
190 return pack('C'.$lencode, 0x80 + ($type * 4) + $lenbytes, $len).
195 # takes a Crypt::OpenSSL::Bignum, returns it formatted as OpenPGP MPI
196 # (RFC 4880 section 3.2)
200 my $val = $num->to_bin();
201 my $mpilen = length($val)*8;
203 # this is a kludgy way to get the number of significant bits in the
205 my $bitsinfirstbyte = length(sprintf("%b", ord($val)));
207 $mpilen -= (8 - $bitsinfirstbyte);
209 return pack('n', $mpilen).$val;
212 # see the bottom of page 43 of RFC 4880
213 sub simple_checksum {
216 return unpack("%C*",$bytes) % 65536;
219 # FIXME: genericize these to accept either RSA or DSA keys:
220 sub make_rsa_pub_key_body {
222 my $timestamp = shift;
224 my ($n, $e) = $key->get_key_parameters();
227 pack('CN', 4, $timestamp).
228 pack('C', $asym_algos->{rsa}).
232 sub make_rsa_sec_key_body {
234 my $timestamp = shift;
236 # we're not using $a and $b, but we need them to get to $c.
237 my ($n, $e, $d, $p, $q, $a, $b, $c) = $key->get_key_parameters();
239 my $secret_material = mpi_pack($d).
244 # FIXME: according to Crypt::OpenSSL::RSA, $c is 1/q mod p; but
245 # according to sec 5.5.3 of RFC 4880, this last argument should
246 # instead be: u, the multiplicative inverse of p, mod q. i don't
247 # see a simple way to generate this number from the perl module
251 pack('CN', 4, $timestamp).
252 pack('C', $asym_algos->{rsa}).
255 pack('C', 0). # seckey material is not encrypted -- see RFC 4880 sec 5.5.3
257 simple_checksum($secret_material);
260 # expects an RSA key (public or private) and a timestamp
263 my $timestamp = shift;
265 my $rsabody = make_rsa_pub_key_body($key, $timestamp);
267 return Digest::SHA1::sha1(pack('Cn', 0x99, length($rsabody)).$rsabody);
270 # we're just not dealing with newline business right now. slurp in
276 my $rsa = Crypt::OpenSSL::RSA->new_private_key($buf);
278 $rsa->use_sha1_hash();
280 # see page 22 of RFC 4880 for why i think this is the right padding
282 $rsa->use_pkcs1_padding();
284 if (! $rsa->check_key()) {
285 die "key does not check";
288 my $version = pack('C', 4);
289 # strong assertion of identity:
290 my $sigtype = pack('C', $sig_types->{positive_certification});
292 my $pubkey_algo = pack('C', $asym_algos->{rsa});
294 my $hash_algo = pack('C', $digests->{sha1});
296 # FIXME: i'm worried about generating a bazillion new OpenPGP
297 # certificates from the same key, which could easily happen if you run
298 # this script more than once against the same key. How can we prevent
301 # could an environment variable (if set) override the current time?
302 my $timestamp = time();
304 my $creation_time_packet = pack('CCN', 5, $subpacket_types->{sig_creation_time}, $timestamp);
307 # FIXME: HARDCODED: what if someone wants to select a different set of
308 # usage flags? For now, we do only authentication.
309 my $usage_packet = pack('CCC', 2, $subpacket_types->{usage_flags}, $usage_flags->{authenticate});
312 # FIXME: HARDCODED: how should we determine how far off to set the
313 # expiration date? default is to expire in 2 days, which is insanely
314 # short (but good for testing).
315 my $expires_in = 86400*2;
316 my $expiration_packet = pack('CCN', 5, $subpacket_types->{key_expiration_time}, $expires_in);
319 # prefer AES-256, AES-192, AES-128, CAST5, 3DES:
320 my $pref_sym_algos = pack('CCCCCCC', 6, $subpacket_types->{preferred_cipher},
325 $ciphers->{tripledes}
328 # prefer SHA-1, SHA-256, RIPE-MD/160
329 my $pref_hash_algos = pack('CCCCC', 4, $subpacket_types->{preferred_digest},
332 $digests->{ripemd160}
335 # prefer ZLIB, BZip2, ZIP
336 my $pref_zip_algos = pack('CCCCC', 4, $subpacket_types->{preferred_compression},
342 # we support the MDC feature:
343 my $feature_subpacket = pack('CCC', 2, $subpacket_types->{features},
346 # keyserver preference: only owner modify (???):
347 my $keyserver_pref = pack('CCC', 2, $subpacket_types->{keyserver_prefs},
348 $keyserver_prefs->{nomodify});
350 my $subpackets_to_be_hashed =
351 $creation_time_packet.
360 my $subpacket_octets = pack('n', length($subpackets_to_be_hashed));
362 my $sig_data_to_be_hashed =
368 $subpackets_to_be_hashed;
370 my $pubkey = make_rsa_pub_key_body($rsa, $timestamp);
371 my $seckey = make_rsa_sec_key_body($rsa, $timestamp);
373 my $key_data = make_packet($packet_types->{pubkey}, $pubkey);
375 # take the last 8 bytes of the fingerprint as the keyid:
376 my $keyid = substr(fingerprint($rsa, $timestamp), 20 - 8, 8);
378 # the v4 signature trailer is:
380 # version number, literal 0xff, and then a 4-byte count of the
381 # signature data itself.
382 my $trailer = pack('CCN', 4, 0xff, length($sig_data_to_be_hashed));
385 pack('CN', 0xb4, length($uid)).
391 $sig_data_to_be_hashed.
394 my $data_hash = Digest::SHA1::sha1_hex($datatosign);
397 my $issuer_packet = pack('CCa8', 9, $subpacket_types->{issuer}, $keyid);
399 my $sig = Crypt::OpenSSL::Bignum->new_from_bin($rsa->sign($datatosign));
402 $sig_data_to_be_hashed.
403 pack('n', length($issuer_packet)).
405 pack('n', hex(substr($data_hash, 0, 4))).
409 make_packet($packet_types->{pubkey}, $pubkey).
410 make_packet($packet_types->{uid}, $uid).
411 make_packet($packet_types->{sig}, $sig_body);