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;
23 my $holdTerminator = $/;
28 my $rsa = Crypt::OpenSSL::RSA->new_private_key($buf);
30 $rsa->use_sha1_hash();
31 $rsa->use_no_padding();
33 if (! $rsa->check_key()) {
34 die "key does not check";
37 my $uid = 'fake key (do not use) <test@example.org>';
41 my $version = pack('C', 4);
42 # strong assertion of identity:
43 my $sigtype = pack('C', 0x13);
45 my $pubkey_algo = pack('C', 1);
47 my $hash_algo = pack('C', 2);
51 my $timestamp = 1231003584;
53 my $creation_time_packet = pack('CCN', 5, 2, $timestamp);
56 # usage: signing and certification:
58 my $usage_packet = pack('CCC', 2, 27, $flags);
62 my $expires_in = 86400*2;
63 my $expiration_packet = pack('CCN', 5, 9, $expires_in);
66 # prefer AES-256, AES-192, AES-128, CAST5, 3DES:
67 my $pref_sym_algos = pack('CCCCCCC', 6, 11, 9, 8, 7, 3, 2);
69 # prefer SHA-1, SHA-256, RIPE-MD/160
70 my $pref_hash_algos = pack('CCCCC', 4, 21, 2, 8, 3);
72 # prefer ZLIB, BZip2, ZIP
73 my $pref_zip_algos = pack('CCCCC', 4, 22, 2, 3, 1);
75 # we support the MDC feature:
76 my $features = pack('CCC', 2, 30, 1);
78 # keyserver preference: only owner modify (???):
79 my $keyserver_pref = pack('CCC', 2, 23, 0x80);
81 my $subpackets_to_be_hashed =
82 $creation_time_packet.
91 #FIXME: what's the right way to get length()?
92 my $subpacket_octets = pack('n', length($subpackets_to_be_hashed));
94 my $sig_data_to_be_hashed =
100 $subpackets_to_be_hashed;
103 my ($n, $e, $d, $p, $q) = $rsa->get_key_parameters();
106 open(KEYFILE, "</home/wt215/gpg-test/key-data");
107 my $key_data = <KEYFILE>;
109 # FIXME: $keyid should be generated from the public key instead of
111 my $keyid = '5616d7cb02e69446';
113 # the v4 signature trailer is:
115 # version number, literal 0xff, and then a 4-byte count of the
116 # signature data itself.
117 my $trailer = pack('CCN', 4, 0xff, length($sig_data_to_be_hashed));
119 # FIXME: length() is probably not right here either in the event that
120 # the uid uses unicode.
122 pack('CN', 0xb4, length($uid)).
128 $sig_data_to_be_hashed.
131 my $data_hash = Digest::SHA1::sha1_hex($datatosign);
134 my $issuer_packet = pack('CCH16', 9, 16, $keyid);
136 my $sig = $rsa->sign($datatosign);
138 my $bigsig = Crypt::OpenSSL::Bignum->new_from_bin($sig);
141 my $hex = $bigsig->to_hex();
143 my $mpilen = length($hex)*4;
145 # this is a kludgy way to get the number of bits in the first byte:
146 my $bitsinfirstbyte = length(sprintf("%b", hex(substr $hex, 0, 2)));
148 $mpilen -= (8 - $bitsinfirstbyte);
150 # emit two octets representing $mpilen, followed by the signature itself:
154 $sig_data_to_be_hashed.
155 # FIXME: another dubious length() call.
156 pack('n', length($issuer_packet)).
158 pack('n', hex(substr($data_hash, 0, 4))).
162 # FIXME: yet another length():
163 my $len = length($sig_body);
168 $header = pack('CC', 0x88, $len);
169 } elsif ($len < 2**16) {
170 $header = pack('Cn', 0x89, $len);
171 } elsif ($len < 2**31) {
172 $header = pack('CN', 0x8a, $len);
174 # what the hell do we do here?
175 $header = pack('C', 0x8b);
178 print $header.$sig_body;
180 $/ = $holdTerminator;