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
8 # Jameson Rollins <jrollins@finestructure.net>
9 # Daniel Kahn Gillmor <dkg@fifthhorseman.net>
11 # Started on: 2009-01-07 02:01:19-0500
13 # License: GPL v3 or later (we may need to adjust this given that this
14 # connects to OpenSSL via perl)
18 use Crypt::OpenSSL::RSA;
19 use Crypt::OpenSSL::Bignum;
24 # make an old-style packet out of the given packet type and body.
25 # old-style (see RFC 4880 section 4.2)
30 # FIXME: yet another length():
31 my $len = length($body);
39 } elsif ($len < 2**16) {
42 } elsif ($len < 2**31) {
43 ## not testing against full 32 bits because i don't want to deal
44 ## with potential overflow.
48 ## what the hell do we do here?
53 return pack('C'.$lencode, 0x80 + ($type * 4) + $lenbytes, $len).
58 # takes a Crypt::OpenSSL::Bignum
62 my $hex = $num->to_hex();
64 my $mpilen = length($hex)*4;
66 # this is a kludgy way to get the number of bits in the first byte:
67 my $bitsinfirstbyte = length(sprintf("%b", hex(substr $hex, 0, 2)));
69 $mpilen -= (8 - $bitsinfirstbyte);
71 return pack('n', $mpilen).$num->to_bin();
75 my $holdTerminator = $/;
80 my $rsa = Crypt::OpenSSL::RSA->new_private_key($buf);
82 $rsa->use_sha1_hash();
83 $rsa->use_no_padding();
85 if (! $rsa->check_key()) {
86 die "key does not check";
89 my $uid = 'fake key (do not use) <test@example.org>';
93 my $version = pack('C', 4);
94 # strong assertion of identity:
95 my $sigtype = pack('C', 0x13);
97 my $pubkey_algo = pack('C', 1);
99 my $hash_algo = pack('C', 2);
103 my $timestamp = 1231003584;
105 my $creation_time_packet = pack('CCN', 5, 2, $timestamp);
108 # usage: signing and certification:
110 my $usage_packet = pack('CCC', 2, 27, $flags);
114 my $expires_in = 86400*2;
115 my $expiration_packet = pack('CCN', 5, 9, $expires_in);
118 # prefer AES-256, AES-192, AES-128, CAST5, 3DES:
119 my $pref_sym_algos = pack('CCCCCCC', 6, 11, 9, 8, 7, 3, 2);
121 # prefer SHA-1, SHA-256, RIPE-MD/160
122 my $pref_hash_algos = pack('CCCCC', 4, 21, 2, 8, 3);
124 # prefer ZLIB, BZip2, ZIP
125 my $pref_zip_algos = pack('CCCCC', 4, 22, 2, 3, 1);
127 # we support the MDC feature:
128 my $features = pack('CCC', 2, 30, 1);
130 # keyserver preference: only owner modify (???):
131 my $keyserver_pref = pack('CCC', 2, 23, 0x80);
133 my $subpackets_to_be_hashed =
134 $creation_time_packet.
143 #FIXME: what's the right way to get length()?
144 my $subpacket_octets = pack('n', length($subpackets_to_be_hashed));
146 my $sig_data_to_be_hashed =
152 $subpackets_to_be_hashed;
155 my ($n, $e, $d, $p, $q) = $rsa->get_key_parameters();
159 pack('CN', 4, $timestamp).
164 #open(KEYFILE, "</home/wt215/gpg-test/key-data");
165 my $key_data = make_packet(6, $pubkey);
167 # FIXME: $keyid should be generated from the public key instead of
169 my $keyid = '5616d7cb02e69446';
171 # the v4 signature trailer is:
173 # version number, literal 0xff, and then a 4-byte count of the
174 # signature data itself.
175 my $trailer = pack('CCN', 4, 0xff, length($sig_data_to_be_hashed));
177 # FIXME: length() is probably not right here either in the event that
178 # the uid uses unicode.
180 pack('CN', 0xb4, length($uid)).
186 $sig_data_to_be_hashed.
189 my $data_hash = Digest::SHA1::sha1_hex($datatosign);
192 my $issuer_packet = pack('CCH16', 9, 16, $keyid);
194 my $sig = Crypt::OpenSSL::Bignum->new_from_bin($rsa->sign($datatosign));
197 $sig_data_to_be_hashed.
198 # FIXME: another dubious length() call.
199 pack('n', length($issuer_packet)).
201 pack('n', hex(substr($data_hash, 0, 4))).
204 print make_packet(6, $pubkey);
205 print make_packet(13, $uid);
206 print make_packet(2, $sig_body);
208 $/ = $holdTerminator;