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 ?
35 # make an old-style packet out of the given packet type and body.
36 # old-style (see RFC 4880 section 4.2)
41 my $len = length($body);
49 } elsif ($len < 2**16) {
52 } elsif ($len < 2**31) {
53 ## not testing against full 32 bits because i don't want to deal
54 ## with potential overflow.
58 ## what the hell do we do here?
63 return pack('C'.$lencode, 0x80 + ($type * 4) + $lenbytes, $len).
68 # takes a Crypt::OpenSSL::Bignum, returns it formatted as OpenPGP MPI
69 # (RFC 4880 section 3.2)
73 my $val = $num->to_bin();
74 my $mpilen = length($val)*8;
76 # this is a kludgy way to get the number of significant bits in the
78 my $bitsinfirstbyte = length(sprintf("%b", ord($val)));
80 $mpilen -= (8 - $bitsinfirstbyte);
82 return pack('n', $mpilen).$val;
85 # FIXME: genericize this to accept either RSA or DSA keys:
86 sub make_rsa_pub_key_body {
88 my $timestamp = shift;
90 my ($n, $e) = $key->get_key_parameters();
93 pack('CN', 4, $timestamp).
100 # expects an RSA key (public or private) and a timestamp
103 my $timestamp = shift;
105 my $rsabody = make_rsa_pub_key_body($key, $timestamp);
107 return Digest::SHA1::sha1(pack('Cn', 0x99, length($rsabody)).$rsabody);
110 # FIXME: replace the opaque numbers below with
111 # semantically-meaningful references based on these tables.
113 # see RFC 4880 section 9.1 (ignoring deprecated algorithms for now)
114 my $asym_algos = { rsa => 1,
119 # see RFC 4880 section 9.2
120 my $ciphers = { plaintext => 0,
131 # see RFC 4880 section 9.3
132 my $zips = { uncompressed => 0,
138 # see RFC 4880 section 9.4
139 my $digests = { md5 => 1,
148 # see RFC 4880 section 5.2.3.21
149 my $usage_flags = { certify => 0x01,
151 encrypt_comms => 0x04,
152 encrypt_storage => 0x08,
153 encrypt => 0x0c, ## both comms and storage
154 split => 0x10, # the private key is split via secret sharing
155 authenticate => 0x20,
156 shared => 0x80, # more than one person holds the entire private key
159 # see RFC 4880 section 4.3
160 my $packet_types = { pubkey_enc_session => 1,
162 symkey_enc_session => 3,
167 compressed_data => 8,
175 symenc_w_integrity => 18,
179 # see RFC 4880 section 5.2.1
180 my $sig_types = { binary_doc => 0x00,
183 generic_certification => 0x10,
184 persona_certification => 0x11,
185 casual_certification => 0x12,
186 positive_certification => 0x13,
187 subkey_binding => 0x18,
188 primary_key_binding => 0x19,
189 key_signature => 0x1f,
190 key_revocation => 0x20,
191 subkey_revocation => 0x28,
192 certification_revocation => 0x30,
198 # see RFC 4880 section 5.2.3.1
199 my $subpacket_types = { sig_creation_time => 2,
200 sig_expiration_time => 3,
205 key_expiration_time => 9,
206 preferred_cipher => 11,
207 revocation_key => 12,
210 preferred_digest => 21,
211 preferred_compression => 22,
212 keyserver_prefs => 23,
213 preferred_keyserver => 24,
218 revocation_reason => 29,
220 signature_target => 31,
221 embedded_signature => 32,
224 # bitstring (see RFC 4880 section 5.2.3.24)
225 my $features = { mdc => 0x01
228 # bitstring (see RFC 4880 5.2.3.17)
229 my $keyserver_prefs = { nomodify => 0x80
232 # we're just not dealing with newline business right now. slurp in
238 my $rsa = Crypt::OpenSSL::RSA->new_private_key($buf);
240 $rsa->use_sha1_hash();
242 # see page 22 of RFC 4880 for why i think this is the right padding
244 $rsa->use_pkcs1_padding();
246 if (! $rsa->check_key()) {
247 die "key does not check";
250 my $version = pack('C', 4);
251 # strong assertion of identity:
252 my $sigtype = pack('C', $sig_types->{positive_certification});
254 my $pubkey_algo = pack('C', $asym_algos->{rsa});
256 my $hash_algo = pack('C', $digests->{sha1});
258 # FIXME: i'm worried about generating a bazillion new OpenPGP
259 # certificates from the same key, which could easily happen if you run
260 # this script more than once against the same key. How can we prevent
263 # could an environment variable (if set) override the current time?
264 my $timestamp = time();
266 my $creation_time_packet = pack('CCN', 5, $subpacket_types->{sig_creation_time}, $timestamp);
269 # FIXME: HARDCODED: what if someone wants to select a different set of
270 # usage flags? For now, we do only authentication.
271 my $usage_packet = pack('CCC', 2, $subpacket_types->{usage_flags}, $usage_flags->{authenticate});
274 # FIXME: HARDCODED: how should we determine how far off to set the
275 # expiration date? default is to expire in 2 days, which is insanely
276 # short (but good for testing).
277 my $expires_in = 86400*2;
278 my $expiration_packet = pack('CCN', 5, $subpacket_types->{key_expiration_time}, $expires_in);
281 # prefer AES-256, AES-192, AES-128, CAST5, 3DES:
282 my $pref_sym_algos = pack('CCCCCCC', 6, $subpacket_types->{preferred_cipher},
287 $ciphers->{tripledes}
290 # prefer SHA-1, SHA-256, RIPE-MD/160
291 my $pref_hash_algos = pack('CCCCC', 4, $subpacket_types->{preferred_digest},
294 $digests->{ripemd160}
297 # prefer ZLIB, BZip2, ZIP
298 my $pref_zip_algos = pack('CCCCC', 4, $subpacket_types->{preferred_compression},
304 # we support the MDC feature:
305 my $feature_subpacket = pack('CCC', 2, $subpacket_types->{features},
308 # keyserver preference: only owner modify (???):
309 my $keyserver_pref = pack('CCC', 2, $subpacket_types->{keyserver_prefs},
310 $keyserver_prefs->{nomodify});
312 my $subpackets_to_be_hashed =
313 $creation_time_packet.
322 my $subpacket_octets = pack('n', length($subpackets_to_be_hashed));
324 my $sig_data_to_be_hashed =
330 $subpackets_to_be_hashed;
332 my $pubkey = make_rsa_pub_key_body($rsa, $timestamp);
334 my $key_data = make_packet($packet_types->{pubkey}, $pubkey);
336 # take the last 8 bytes of the fingerprint as the keyid:
337 my $keyid = substr(fingerprint($rsa, $timestamp), 20 - 8, 8);
339 # the v4 signature trailer is:
341 # version number, literal 0xff, and then a 4-byte count of the
342 # signature data itself.
343 my $trailer = pack('CCN', 4, 0xff, length($sig_data_to_be_hashed));
346 pack('CN', 0xb4, length($uid)).
352 $sig_data_to_be_hashed.
355 my $data_hash = Digest::SHA1::sha1_hex($datatosign);
358 my $issuer_packet = pack('CCa8', 9, $subpacket_types->{issuer}, $keyid);
360 my $sig = Crypt::OpenSSL::Bignum->new_from_bin($rsa->sign($datatosign));
363 $sig_data_to_be_hashed.
364 pack('n', length($issuer_packet)).
366 pack('n', hex(substr($data_hash, 0, 4))).
370 make_packet($packet_types->{pubkey}, $pubkey).
371 make_packet($packet_types->{uid}, $uid).
372 make_packet($packet_types->{sig}, $sig_body);