ConvertPubKey: include secstrength in verbose output
[ezcert.git] / ConvertPubKey
blobda5ef5dfa2b12e5e95c52b055da5191b47e268c9
1 #!/usr/bin/perl
3 # ConvertPubKey - Convert public keys to/from OpenSSH/X509 format
4 # Copyright (c) 2011,2012,2013,2014 Kyle J. McKay. All rights reserved.
6 # *** See detailed help starting around line 80 ***
8 # This program is free software: you can redistribute it and/or modify
9 # it under the terms of the GNU Affero General Public License as published by
10 # the Free Software Foundation, either version 3 of the License, or
11 # (at your option) any later version.
13 # This program is distributed in the hope that it will be useful,
14 # but WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 # GNU Affero General Public License for more details.
18 # You should have received a copy of the GNU Affero General Public License
19 # along with this program. If not, see <http://www.gnu.org/licenses/>.
21 exit(&main());
23 use strict;
24 use warnings;
25 use bytes;
27 use MIME::Base64;
28 use IPC::Open2;
29 use Digest::MD5 qw(md5 md5_hex md5_base64);
30 use Getopt::Long qw(:config gnu_getopt);
32 our $VERSION;
33 my $VERSIONMSG;
34 my $HELP;
35 my $USAGE;
37 BEGIN {
38 *VERSION = \'1.0.3';
39 $VERSIONMSG = "ConvertPubKey version $VERSION\n" .
40 "Copyright (c) 2011-2014 Kyle J. McKay. All rights reserved.\n" .
41 "License AGPLv3+: GNU Affero GPL version 3 or later.\n" .
42 "http://gnu.org/licenses/agpl.html\n" .
43 "This is free software: you are free to change and redistribute it.\n" .
44 "There is NO WARRANTY, to the extent permitted by law.\n";
47 BEGIN {
48 eval {
49 require Digest::SHA1;
50 Digest::SHA1->import(
51 qw(
52 sha1 sha1_hex sha1_base64
54 ); 1} ||
55 eval {
56 require Digest::SHA;
57 Digest::SHA->import(
58 qw(
59 sha1 sha1_hex sha1_base64
61 ); 1} ||
62 eval {
63 require Digest::SHA::PurePerl;
64 Digest::SHA::PurePerl->import(
65 qw(
66 sha1 sha1_hex sha1_base64
68 ); 1} ||
69 die "One of Digest::SHA1 or Digest::SHA or Digest::SHA::PurePerl "
70 . "must be available\n";
73 BEGIN {
74 $USAGE = <<'USAGE';
75 Usage: ConvertPubKey [-h] [--version] [--verbose] [--debug] [--pubx509] [-t]
76 [--format ssh|pem|der] [--in pub_key_file] [--out out_file.pub]
77 [--quiet] [--check] [optional comment here]
78 USAGE
79 $HELP = <<'HELP';
80 NAME
81 ConvertPubKey -- convert public key formats
83 SYNOPSIS
84 ConvertPubKey [-h] [--version] [--verbose] [--debug] [--pubx509] [-t]
85 [--format ssh|pem|der] [--in pub_key_file] [--out out_file.pub]
86 [--quiet] [--check] [optional comment here]
88 DESCRIPTION
89 ConvertPubKey converts public keys to/from OpenSSH/X509 format.
91 Input may be an X.509 public key in either PEM or DER format or an
92 OpenSSH public key.
94 Output defaults to the other kind of key so that if input is an OpenSSH
95 public key, output will be an X.509 PEM public key and if input is an
96 X.509 PEM or DER public key, output will be an OpenSSH public key.
98 When outputing an OpenSSH public key, the comment, if any will be
99 placed at the end of the line separated from the Base64 of the OpenSSH
100 public key by a space (this is the normal location for an OpenSSH public
101 key comment). Web sites that accept OpenSSH public keys often require
102 the comment to be of the form user@host or they will reject the public
103 key.
105 No validation is performed on the "optional comment here" value.
107 OPTIONS
108 -h/--help
109 Show this help
111 -V/--version
112 Show the ConvertPubKey version
114 -v/--verbose
115 Produce extra informational messages to standard error.
117 --debug
118 Show debugging information. Automatically enables --verbose.
119 Suppresses --quiet.
121 --quiet
122 Suppress all messages except errors. Ignored if --debug or
123 --verbose given.
125 --check
126 Perform all normal validation checks but do not actually output a
127 public key. Automatically enables --verbose.
129 --pubx509/--pubX509
130 Force the public key read from standard input to be interpreted
131 as an X.509 format public key. Normally this should be
132 automatically detected and this option should not be needed.
135 Allow reading the public key from standard input when standard
136 input is a tty. In most cases attempting to read the public key
137 from standard input that is a tty indicates that the public key
138 was accidentally omitted. If that is not the case, the -t option
139 must be given to allow reading the public key from standard input
140 when standard input is a tty. This option is always implied if
141 the --in option is used with a value other than "-".
143 --format ssh|pem|der
144 Normally the output format is automatically chosen based on the
145 input format. If the input format is an X.509 DER or PEM format
146 public key, the output format will be an OpenSSH public key. If
147 the input format is an OpenSSH public key, the output format will
148 be an X.509 PEM format public key. This option can be used to
149 override the default output format. Using "ssh" requests the
150 output be an OpenSSH format public key. Giving "pem" requests the
151 output be an X.509 PEM format public key and "der" requests output
152 be an X.509 DER format public key.
154 --in pub_key_file
155 The public key to convert. May be an OpenSSH protocol 2 format
156 public key or an X.509 format public key (in either PEM or DER
157 format). See also the --pubx509 option. If pub_key_file is "-"
158 or this option is omitted then standard input is read.
160 --out out_file.pub
161 The converted public key will be written to out_file.pub. If
162 this option is omitted or out_file.pub is "-" then the converted
163 public key is written to standard output.
165 NOTES
166 Only minimal checking is done on the input public key.
168 OpenSSH ssh-rsa and ssh-dss key types (and their corresponding X.509
169 equivalents) are supported, other types will result in errors.
171 Neither OpenSSH nor OpenSSL are needed to perform the key conversion.
173 TIPS
174 Create a new 2048-bit RSA private key in new_rsa_2048 with OpenSSL:
176 openssl genrsa -f4 -out new_rsa_2048 2048
178 Store the OpenSSH format public key for new_rsa_2048 in the file
179 new_rsa_2048.pub with comment "test@example.com":
181 openssl rsa -in new_rsa_2048 -pubout | \
182 ConvertPubKey --out new_rsa_2048.pub test@example.com
184 HELP
187 sub formatbold($;$)
189 my $str = shift;
190 my $fancy = shift || 0;
191 if ($fancy) {
192 $str = join('',map($_."\b".$_, split(//,$str)));
194 return $str;
197 sub formatul($;$)
199 my $str = shift;
200 my $fancy = shift || 0;
201 if ($fancy) {
202 $str = join('',map("_\b".$_, split(//,$str)));
204 return $str;
207 sub formatman($;$)
209 my $man = shift;
210 my $fancy = shift || 0;
211 my @inlines = split(/\n/, $man, -1);
212 my @outlines = ();
213 foreach my $line (@inlines) {
214 if ($line =~ /^[A-Z]+$/) {
215 $line = formatbold($line, $fancy);
217 else {
218 $line =~ s/'''(.+?)'''/formatbold($1,$fancy)/gse;
219 $line =~ s/''(.+?)''/formatul($1,$fancy)/gse;
221 push (@outlines, $line);
223 my $result = join("\n", @outlines);
224 $result =~ s/\\\n//gso;
225 return $result;
228 sub DERLength($)
230 # return a DER encoded length
231 my $len = shift;
232 return pack('C',$len) if $len <= 127;
233 return pack('C2',0x81, $len) if $len <= 255;
234 return pack('Cn',0x82, $len) if $len <= 65535;
235 return pack('CCn',0x83, ($len >> 16), $len & 0xFFFF) if $len <= 16777215;
236 # Silently returns invalid result if $len > 2^32-1
237 return pack('CN',0x84, $len);
240 sub SingleOID($)
242 # return a single DER encoded OID component
243 no warnings;
244 my $num = shift;
245 $num += 0;
246 my $result = pack('C', $num & 0x7F);
247 $num >>= 7;
248 while ($num) {
249 $result = pack('C', 0x80 | ($num & 0x7F)) . $result;
250 $num >>= 7;
252 return $result;
255 sub DEROID($)
257 # return a DER encoded OID complete with leading 0x06 and DER length
258 # Input is a string of decimal numbers separated by '.' with at least
259 # two numbers required.
260 no warnings;
261 my @ids = split(/[.]/,$_[0]);
262 push(@ids, 0) while @ids < 2; # return something that's kind of valid
263 unshift(@ids, shift(@ids) * 40 + shift(@ids)); # combine first two
264 my $ans = '';
265 foreach my $num (@ids) {
266 $ans .= SingleOID($num);
268 return pack('C',0x6).DERLength(length($ans)).$ans;
271 sub ReadDERLength($)
273 # Input is a DER encoded length with possibly extra trailing bytes
274 # Output is an array of length and bytes-used-for-encoded-length
275 my $der = shift;
276 return undef unless length($der);
277 my $byte = unpack('C',substr($der,0,1));
278 return ($byte, 1) if $byte <= 127;
279 return undef if $byte == 128 || $byte > 128+8; # Fail if greater than 2^64
280 my $cnt = $byte & 0x7F;
281 return undef unless length($der) >= $cnt+1; # Fail if not enough bytes
282 my $val = 0;
283 for (my $i = 0; $i < $cnt; ++$i) {
284 $val <<= 8;
285 $val |= unpack('C',substr($der,$i+1,1));
287 return ($val, $cnt+1);
290 sub CountKeyBits($)
292 my $data = shift;
293 return undef if length($data) < 1;
294 my $bits = 8 * length($data);
295 # but leading zero bits must be subtracted
296 my $byte = unpack('C',substr($data,0,1));
297 if (!$byte) {
298 $bits -= 8;
299 } else {
300 return undef if $byte & 0x80; # negative is not valid
301 while (!($byte & 0x80)) {
302 --$bits;
303 $byte <<= 1;
306 return $bits;
309 sub PackUIntBytes($)
311 my $intbytes = shift || '';
312 return pack('C2',0x02,0x00) unless length($intbytes);
313 my $byte = unpack('C',substr($intbytes,0,1));
314 $intbytes = pack('C',0x00).$intbytes if $byte & 0x80; # would've been negative
315 return pack('C',0x02).DERLength(length($intbytes)).$intbytes;
318 sub GetOpenSSHKeyInfo($)
320 # Input is an OpenSSH public key in .pub format
321 # Output is an array of:
322 # key type (either 'rsa' or 'dsa')
323 # how many bits in the modulus
324 # the public exponent (if RSA) or how many bits in prime divisor (if DSA)
325 # the key id
326 # the OpenSSH md5 fingerprint
327 # the OpenSSH sha1 fingerprint
328 # the OpenSSH comment (may be '')
329 # the OpenSSH public key in OpenSSL PUBLIC KEY DER format
330 # or undef if the key is unparseable
331 # or just the key type if it's not ssh-rsa
333 # Expected format is:
334 # ssh-rsa BASE64PUBLICKEYDATA optional comment here
335 # or
336 # ssh-dss BASE64PUBLICKEYDATA optional comment here
337 # where the BASE64PUBLICKEYDATA when decoded produces:
338 # RSA:
339 # 4 Byte Big-Endian length of Key type (must be 7 for RSA)
340 # Key type WITHOUT terminating NUL (must be ssh-rsa for RSA)
341 # 4 Byte Big-Endian length of public exponent e
342 # Public exponent e integer bytes
343 # 4 Byte Big-Endian length of modulus n
344 # Modulus n integer bytes
345 # DSS:
346 # 4 Byte Big-Endian length of Key type (must be 7 for DSS)
347 # Key type WITHOUT terminating NUL (must be ssh-dss for DSS)
348 # 4 Byte Big-Endian length of prime modulus p
349 # Prime modulus p integer bytes
350 # 4 Byte Big-Endian length of prime divisor q
351 # Prime divisor q integer bytes
352 # 4 Byte Big-Endian length of parameter g
353 # Parameter g integer bytes
354 # 4 Byte Big-Endian length of public key y
355 # Public key y integer bytes
356 # no extra trailing bytes are permitted
357 my $input = shift;
358 $input =~ s/((?:\r\n|\n|\r).*)$//os;
359 my @fields = split(' ', $input, 3);
360 return undef unless @fields >= 2;
361 my $data = decode_base64($fields[1]);
362 my $origData = $data;
363 my @parts = ();
364 while (length($data) >= 4) {
365 my $len = unpack('N',substr($data,0,4));
366 my $value = '';
367 if ($len > 0) {
368 return undef if $len + 4 > length($data);
369 $value = substr($data,4,$len);
371 push(@parts, $value);
372 substr($data, 0, 4+$len) = '';
374 return undef unless length($data) == 0;
375 return $parts[0] if @parts >= 1 && $parts[0] &&
376 $parts[0] ne 'ssh-rsa' && $parts[0] ne 'ssh-dss';
377 if ($parts[0] eq 'ssh-rsa') {
378 return undef unless @parts == 3;
379 my $rsaEncryption = DEROID('1.2.840.113549.1.1.1'); # :rsaEncryption
380 $rsaEncryption = pack('C',0x30).DERLength(length($rsaEncryption)+2)
381 .$rsaEncryption.pack('C2',0x05,0x00);
382 my $pubrsa = PackUIntBytes($parts[2]); # modulus
383 $pubrsa .= PackUIntBytes($parts[1]); # exponent
384 $pubrsa = pack('C',0x30).DERLength(length($pubrsa)).$pubrsa;
385 my $id = sha1($pubrsa); # The id is the sha1 hash of the BIT STRING part
386 $pubrsa = pack('C',0x3).DERLength(length($pubrsa)+1).pack('C',0x0).$pubrsa;
387 $pubrsa = $rsaEncryption.$pubrsa;
388 $pubrsa = pack('C',0x30).DERLength(length($pubrsa)).$pubrsa;
390 my $bits = CountKeyBits($parts[2]);
391 return undef unless $bits;
393 my $rawexp = $parts[1];
394 my $exp;
395 if (length($rawexp) > 8) {
396 # Fudge the result because it's bigger than a 64-bit number
397 my $lastbyte = unpack('C',substr($rawexp,-1,1));
398 $exp = $lastbyte & 0x01 ? 65537 : 65536;
400 else {
401 $exp = 0;
402 while (length($rawexp)) {
403 $exp <<= 8;
404 $exp |= unpack('C',substr($rawexp,0,1));
405 substr($rawexp,0,1) = '';
408 my $cmnt = $fields[2]||'';
409 return
410 ('rsa',$bits,$exp,$id,md5($origData),sha1($origData),$cmnt,$pubrsa);
411 } else {
412 return undef unless @parts == 5;
413 my $dsaEncryption = DEROID('1.2.840.10040.4.1'); # :dsaEncryption
414 my $algparms = PackUIntBytes($parts[1]). # prime modulus p
415 PackUIntBytes($parts[2]). # prime divisor q
416 PackUIntBytes($parts[3]); # parameter g
417 $algparms = pack('C',0x30).DERLength(length($algparms)).$algparms;
418 $dsaEncryption = pack('C',0x30).
419 DERLength(length($dsaEncryption) + length($algparms)).
420 $dsaEncryption.$algparms;
421 my $pubdsa = PackUIntBytes($parts[4]); # public key y
422 my $id = sha1($pubdsa); # The id is the sha1 hash of the BIT STRING part
423 $pubdsa = pack('C',0x3).DERLength(length($pubdsa)+1).pack('C',0x0).$pubdsa;
424 $pubdsa = $dsaEncryption.$pubdsa;
425 $pubdsa = pack('C',0x30).DERLength(length($pubdsa)).$pubdsa;
427 my $bits = CountKeyBits($parts[1]);
428 return undef unless $bits;
429 my $divsiz = CountKeyBits($parts[2]);
430 return undef unless $divsiz;
432 my $cmnt = $fields[2]||'';
433 return
434 ('dsa',$bits,$divsiz,$id,md5($origData),sha1($origData),$cmnt,$pubdsa);
438 sub GetKeyInfo($)
440 # Input is a X.509 PUBLIC KEY (RSA or DSS) in DER format
441 # Output is an array of:
442 # key type (either 'rsa' or 'dsa')
443 # how many bits in the modulus
444 # the public exponent (if RSA) or how many bits in prime divisor (if DSA)
445 # the key id
446 # the OpenSSH md5 fingerprint
447 # the OpenSSH sha1 fingerprint
448 # the OpenSSH public key in .pub format
449 # or undef if the key is unparseable
451 # Expected format for an RSA public key is:
452 # SEQUENCE {
453 # SEQUENCE {
454 # OBJECT IDENTIFIER :rsaEncryption = 1.2.840.113549.1.1.1
455 # NULL
457 # BIT STRING (primitive) {
458 # 0 unused bits
459 # SEQUENCE { # this part is the contents of an "RSA PUBLIC KEY" file
460 # INTEGER modulus n
461 # INTEGER publicExponent e
466 # Expected format for a DSA public key is:
467 # SEQUENCE {
468 # SEQUENCE {
469 # OBJECT IDENTIFIER :dsaEncryption = 1.2.840.10040.4.1
470 # SEQUENCE {
471 # INTEGER prime modulus p
472 # INTEGER prime divisor q
473 # INTEGER parameter g
476 # BIT STRING (primitive) {
477 # 0 unused bits
478 # INTEGER public key y
482 no warnings;
483 my $der = shift;
484 my $rawmod;
485 my $rawexp;
487 return undef if unpack('C',substr($der,0,1)) != 0x30;
488 my ($len, $lenbytes) = ReadDERLength(substr($der,1));
489 return undef unless length($der) == 1 + $lenbytes + $len;
490 substr($der, 0, 1 + $lenbytes) = '';
492 return undef if unpack('C',substr($der,0,1)) != 0x30;
493 ($len, $lenbytes) = ReadDERLength(substr($der,1));
494 return undef unless length($der) >= 1 + $lenbytes + $len;
495 my $ident = substr($der, 1 + $lenbytes, $len);
496 substr($der, 0, 1 + $lenbytes + $len) = '';
498 return undef unless unpack('C',substr($ident,0,1)) == 0x06; # OID
499 ($len, $lenbytes) = ReadDERLength(substr($ident,1));
500 return undef unless length($ident) > 1 + $lenbytes + $len;
501 my $keytypeoid = substr($ident, 0, 1 + $lenbytes + $len);
502 substr($ident, 0, 1 + $lenbytes + $len) = '';
504 # $keytypeoid may be either rsaEncryption or dsaEncryption
505 my $rsaEncryption = DEROID('1.2.840.113549.1.1.1'); # :rsaEncryption
506 my $dsaEncryption = DEROID('1.2.840.10040.4.1'); # :dsaEncryption
508 return undef
509 unless $keytypeoid eq $rsaEncryption || $keytypeoid eq $dsaEncryption;
511 return undef if unpack('C',substr($der,0,1)) != 0x03;
512 ($len, $lenbytes) = ReadDERLength(substr($der,1));
513 return undef unless length($der) == 1 + $lenbytes + $len && $len >= 1;
514 return undef unless unpack('C',substr($der, 1 + $lenbytes, 1)) == 0x00;
515 substr($der, 0, 1 + $lenbytes + 1) = '';
516 my $id = sha1($der); # The id is the sha1 hash of the BIT STRING contents
518 if ($keytypeoid eq $rsaEncryption) {
519 return undef unless $ident eq pack('C2', 0x05, 0x00);
521 return undef if unpack('C',substr($der,0,1)) != 0x30;
522 ($len, $lenbytes) = ReadDERLength(substr($der,1));
523 return undef unless length($der) == 1 + $lenbytes + $len;
524 substr($der, 0, 1 + $lenbytes) = '';
526 return undef if unpack('C',substr($der,0,1)) != 0x02;
527 ($len, $lenbytes) = ReadDERLength(substr($der,1));
528 substr($der, 0, 1 + $lenbytes) = '';
529 my $derexp = substr($der, $len);
530 substr($der, $len) = '';
531 return undef unless $len >= 1;
532 $rawmod = $der;
533 my $bits = CountKeyBits($der);
534 return undef unless $bits;
536 $der = $derexp;
537 return undef if unpack('C',substr($der,0,1)) != 0x02;
538 ($len, $lenbytes) = ReadDERLength(substr($der,1));
539 substr($der, 0, 1 + $lenbytes) = '';
540 return undef unless length($der) == $len && $len >= 1;
541 return undef if unpack('C',substr($der,0,1)) & 0x80; # negative pub exp bad
542 $rawexp = $der;
543 my $exp;
544 if ($len > 8) {
545 # Fudge the result because it's bigger than a 64-bit number
546 my $lastbyte = unpack('C',substr($der,-1,1));
547 $exp = $lastbyte & 0x01 ? 65537 : 65536;
549 else {
550 $exp = 0;
551 while (length($der)) {
552 $exp <<= 8;
553 $exp |= unpack('C',substr($der,0,1));
554 substr($der,0,1) = '';
558 my $sshbin = pack('N',7)."ssh-rsa".pack('N',length($rawexp)).$rawexp
559 .pack('N',length($rawmod)).$rawmod;
560 my $sshpub = "ssh-rsa " . encode_base64($sshbin,'');
562 return ('rsa',$bits,$exp,$id,md5($sshbin),sha1($sshbin),$sshpub);
564 } else {
565 return undef unless unpack('C',substr($ident,0,1)) == 0x30;
566 ($len, $lenbytes) = ReadDERLength(substr($ident,1));
567 return undef unless length($ident) == 1 + $lenbytes + $len;
568 substr($ident, 0, 1 + $lenbytes) = '';
570 return undef unless unpack('C',substr($ident,0,1)) == 0x02;
571 ($len, $lenbytes) = ReadDERLength(substr($ident,1));
572 return undef unless length($ident) > 1 + $lenbytes + $len;
573 substr($ident, 0, 1 + $lenbytes) = '';
574 my $dsa_p = substr($ident, 0, $len);
575 substr($ident, 0, $len) = '';
577 return undef unless unpack('C',substr($ident,0,1)) == 0x02;
578 ($len, $lenbytes) = ReadDERLength(substr($ident,1));
579 return undef unless length($ident) > 1 + $lenbytes + $len;
580 substr($ident, 0, 1 + $lenbytes) = '';
581 my $dsa_q = substr($ident, 0, $len);
582 substr($ident, 0, $len) = '';
584 return undef unless unpack('C',substr($ident,0,1)) == 0x02;
585 ($len, $lenbytes) = ReadDERLength(substr($ident,1));
586 return undef unless length($ident) == 1 + $lenbytes + $len;
587 substr($ident, 0, 1 + $lenbytes) = '';
588 my $dsa_g = substr($ident, 0, $len);
590 return undef unless unpack('C',substr($der,0,1)) == 0x02;
591 ($len, $lenbytes) = ReadDERLength(substr($der,1));
592 return undef unless length($der) == 1 + $lenbytes + $len;
593 my $dsa_y = substr($der, 1 + $lenbytes, $len);
595 my $bits = CountKeyBits($dsa_p);
596 return undef unless $bits;
597 my $divsiz = CountKeyBits($dsa_q);
598 return undef unless $divsiz;
600 my $sshbin = pack('N',7)."ssh-dss".pack('N',length($dsa_p)).$dsa_p
601 .pack('N',length($dsa_q)).$dsa_q.pack('N',length($dsa_g)).$dsa_g
602 .pack('N',length($dsa_y)).$dsa_y;
603 my $sshpub = "ssh-dss " . encode_base64($sshbin,'');
605 return ('dsa',$bits,$divsiz,$id,md5($sshbin),sha1($sshbin),$sshpub);
609 my %rsadsa_known_strengths;
610 BEGIN {
611 %rsadsa_known_strengths = (
612 1024 => 80,
613 2048 => 112,
614 3072 => 128,
615 7680 => 192,
616 15360 => 256,
620 sub compute_rsadsa_strength($)
622 my $rsadsabits = shift;
623 return 0 unless $rsadsabits && $rsadsabits > 0;
624 return $rsadsa_known_strengths{$rsadsabits} if $rsadsa_known_strengths{$rsadsabits};
625 my $guess;
626 if ($rsadsabits < 1024) {
627 $guess = 80 * sqrt($rsadsabits/1024);
628 } elsif ($rsadsabits > 15360) {
629 $guess = 256 * sqrt($rsadsabits/15360);
630 } else {
631 $guess = 34.141 + sqrt(34.141*34.141 - 4*0.344*(1554.7-$rsadsabits));
632 $guess = $guess / (2 * 0.344);
634 $guess = 79 if $rsadsabits < 1024 && $guess >= 80;
635 $guess = 111 if $rsadsabits > 1024 && $rsadsabits < 2048 && $guess >= 112;
636 $guess = 127 if $rsadsabits > 2048 && $rsadsabits < 3072 && $guess >= 128;
637 $guess = 191 if $rsadsabits > 3072 && $rsadsabits < 7680 && $guess >= 192;
638 $guess = 255 if $rsadsabits > 7680 && $rsadsabits < 15360 && $guess >= 256;
639 return int($guess) . ' (approximately)';
642 sub BreakLine($$)
644 my ($line,$width) = @_;
645 my @ans = ();
646 return $line if $width < 1;
647 while (length($line) > $width) {
648 push(@ans, substr($line, 0, $width));
649 substr($line, 0, $width) = '';
651 push(@ans, $line) if length($line);
652 return @ans;
655 sub main
657 my $help = '';
658 my $verbose = '';
659 my $quiet = '';
660 my $termOK = '';
661 my $debug = 0;
662 my $pubx509 = '';
663 my $check = '';
664 my $format = undef;
665 my $infile = '-';
666 my $outfile = '-';
668 #tests;
669 eval {GetOptions(
670 "help|h" => sub{$help=1;die"!FINISH"},
671 "verbose|v" => \$verbose,
672 "version|V" => sub{print STDERR $VERSIONMSG;exit(0)},
673 "debug" => \$debug,
674 "quiet" => \$quiet,
675 "pubx509" => \$pubx509,
676 "pubX509" => \$pubx509,
677 "check" => \$check,
678 "t" => \$termOK,
679 "format=s" => \$format,
680 "in=s" => \$infile,
681 "out=s" => \$outfile
682 )} || $help
683 or die $USAGE;
684 if ($help) {
685 local *MAN;
686 my $pager = $ENV{'PAGER'} || 'less';
687 if (-t STDOUT && open(MAN, "|-", $pager)) {
688 print MAN formatman($HELP,1);
689 close(MAN);
691 else {
692 print formatman($HELP);
694 exit(0);
696 die "--in requires a filename\n" if !$infile;
697 die "--out requires a filename\n" if !$outfile;
698 if (defined($format)) {
699 $format = lc($format);
700 die "--format argument must be 'ssh', 'pem' or 'der'\n"
701 if $format ne 'ssh' && $format ne 'pem' && $format ne 'der';
703 $verbose = 1 if $debug || $check;
704 print STDERR $VERSIONMSG if $verbose;
705 my $keytype = 'OpenSSH';
706 $keytype = 'X.509' if $pubx509;
707 die "Standard input is a tty (which is an unlikely source of a "
708 . "public key)\n"
709 . "If that's what you truly meant, add the -t option to allow it.\n"
710 if $infile eq '-' && -t STDIN && !$termOK;
711 my $opensshdotpub;
712 my $infilename;
714 local $/ if $pubx509;
715 my $input;
716 if ($infile ne '-') {
717 $infilename = "\"$infile\"";
718 open($input, '<', $infile)
719 or die "Cannot open $infilename for input: $!\n";
720 } else {
721 $input = *STDIN;
722 $infilename = 'standard input';
724 !!($opensshdotpub = <$input>)
725 or die "Cannot read $keytype public key from $infilename\n";
726 if (!$pubx509) {
727 my $auto509 = 0;
728 if ($opensshdotpub =~ /^----[- ]BEGIN PUBLIC KEY[- ]----/) {
729 $auto509 = 1;
731 else {
732 my $input = $opensshdotpub;
733 $input =~ s/((?:\r\n|\n|\r).*)$//os;
734 my @fields = split(' ', $input, 3);
735 if (@fields < 2 ||
736 length($fields[1]) < 16 ||
737 $fields[1] !~ m|^[0-9A-Za-z+/=]+$|) {
738 $auto509 = 1;
741 if ($auto509) {
742 $pubx509 = 1;
743 $keytype = 'X.509';
744 print STDERR "auto detected --pubx509 option\n" if $debug;
745 local $/;
746 my $extra = <$input>;
747 $opensshdotpub .= $extra if $extra;
750 close($input) if $infile ne '-';
753 my ($kk, $sshkeybits, $sshkeyexp, $sshkeyid, $sfmd5, $sfsha1, $sshcmnt);
754 my ($opensshpub, $dotpub);
755 if ($pubx509) {
756 my $inform =
757 ($opensshdotpub =~ m|^[\t\n\r\x20-\x7E\x80-\xFF]*$|os) ? 'PEM' : 'DER';
758 print STDERR "pubx509 -inform $inform\n" if $debug;
759 my $bin = 1;
760 if ($inform eq 'PEM') {
761 $opensshpub = $opensshdotpub;
762 if ($opensshpub =~ /^----[- ]BEGIN/m) {
763 $opensshpub = "\n".$opensshpub."\n";
764 $opensshpub =~ s/\A.*?\n----[- ]BEGIN[^\n]*\n//s;
765 $opensshpub =~ s/\n----[- ]END[^\n]*\n.*\z//s;
766 $opensshpub =~ tr/[ \t\r\n]//d;
767 $bin = 0;
768 } elsif ($opensshpub =~ m,\A[\t\r\n A-Za-z0-9+/=]*\z,s) {
769 $opensshpub =~ tr/[ \t\r\n]//d;
770 $bin = 0;
772 } else {
773 $opensshpub = $opensshdotpub;
775 print STDERR "BASE64 X509: $opensshpub\n" if !$bin && $debug;
776 $opensshpub = decode_base64($opensshpub) if !$bin;
777 print STDERR "HEX X509: ", unpack('H*', $opensshpub), "\n" if $debug;
778 $sshcmnt = undef;
779 ($kk,$sshkeybits,$sshkeyexp,$sshkeyid,$sfmd5,$sfsha1,$dotpub) =
780 GetKeyInfo($opensshpub);
781 die "Unparseable X.509 public key format read from $infilename\n"
782 unless $sshkeybits;
783 ($kk,$sshkeybits,$sshkeyexp,$sshkeyid,$sfmd5,$sfsha1,$sshcmnt,$opensshpub) =
784 GetOpenSSHKeyInfo($dotpub);
785 } else {
786 ($kk,$sshkeybits,$sshkeyexp,$sshkeyid,$sfmd5,$sfsha1,$sshcmnt,$opensshpub) =
787 GetOpenSSHKeyInfo($opensshdotpub);
788 die "Unparseable OpenSSH public key read from $infilename\n"
789 unless $sshkeybits;
790 ($kk,$sshkeybits,$sshkeyexp,$sshkeyid,$sfmd5,$sfsha1,$dotpub) =
791 GetKeyInfo($opensshpub);
793 my $keykind = uc($kk);
794 if ($verbose) {
795 my $strength = compute_rsadsa_strength($sshkeybits);
796 printf STDERR "$keytype $keykind Public Key Info:\n".
797 " bits=$sshkeybits %s=$sshkeyexp %s\n", $kk eq 'rsa'?"pubexp":"divsiz",
798 "secstrength=$strength";
800 print STDERR " keyid=",
801 join(":", uc(unpack("H*",$sshkeyid))=~/../g), "\n" if $verbose;
802 print STDERR " fingerprint(md5)=",
803 join(":", lc(unpack("H*",$sfmd5))=~/../g), "\n" if $verbose;
804 print STDERR " fingerprint(sha1)=",
805 join(":", lc(unpack("H*",$sfsha1))=~/../g), "\n" if $verbose;
806 print STDERR " comment=",$sshcmnt||'<none present>',"\n"
807 if $verbose && !$pubx509;
808 die "*** Error: $keytype key has less than 512 bits ($sshkeybits)\n"
809 . "*** You might as well just donate your system to hackers now.\n"
810 if $sshkeybits < 512;
811 die "*** Error: The $keytype key's public exponent is even ($sshkeyexp)!\n"
812 if $kk eq 'rsa' && $sshkeyexp && !($sshkeyexp & 0x01);
813 warn "*** Warning: The $keytype key has less than 2048 bits ($sshkeybits), "
814 . "continuing anyway\n" if !$quiet && $sshkeybits < 2048;
815 die "*** Error: The $keytype public key's exponent of $sshkeyexp is "
816 . "unacceptably weak!\n"
817 if $kk eq 'rsa' && $sshkeyexp && $sshkeyexp < 35; # OpenSSH used 35 until v5.4
818 warn "*** Warning: The $keytype public key's exponent ($sshkeyexp) is weak "
819 . "(< 65537), continuing anyway\n"
820 if !$quiet && $kk eq 'rsa' && $sshkeyexp && $sshkeyexp < 65537;
821 warn "*** Warning: The $keytype public key's prime divisor bit size "
822 . "($sshkeyexp) is weak (< 160), continuing anyway\n"
823 if !$quiet && $kk eq 'dsa' &&
824 $sshkeybits >= 1024 && $sshkeybits < 2048 && $sshkeyexp < 160;
825 warn "*** Warning: The $keytype public key's prime divisor bit size "
826 . "($sshkeyexp) is weak (< 224), continuing anyway\n"
827 if !$quiet && $kk eq 'dsa' &&
828 $sshkeybits >= 2048 && $sshkeybits < 3072 && $sshkeyexp < 224;
829 warn "*** Warning: The $keytype public key's prime divisor bit size "
830 . "($sshkeyexp) is weak (< 256), continuing anyway\n"
831 if !$quiet && $kk eq 'dsa' && $sshkeybits >= 3072 && $sshkeyexp < 256;
833 return 0 if $check;
835 if (!defined($format)) {
836 $format = $pubx509 ? 'ssh' : 'pem';
839 my $output;
840 if ($outfile ne '-') {
841 open($output, ">", $outfile)
842 or die "Cannot open \"$outfile\" for output: $!\n";
843 } else {
844 $output = *STDOUT;
847 if ($format eq 'ssh') {
848 my $cmnt = '';
849 $cmnt = ' ' . join(' ', @ARGV) if @ARGV;
850 print $output $dotpub, $cmnt, "\n";
851 } elsif ($format eq 'der') {
852 print $output $opensshpub;
853 } else {
854 my $base64 = join("\n", BreakLine(encode_base64($opensshpub, ''), 64))."\n";
855 print $output "-----BEGIN PUBLIC KEY-----\n",
856 $base64,
857 "-----END PUBLIC KEY-----\n";
860 close($output) if $outfile ne '-';
861 return 0;