2 # $OpenBSD: format-pem.pl,v 1.1 2016/12/15 10:23:21 sthen Exp $
4 # Copyright (c) 2016 Stuart Henderson <sthen@openbsd.org>
6 # Permission to use, copy, modify, and distribute this software for any
7 # purpose with or without fee is hereby granted, provided that the above
8 # copyright notice and this permission notice appear in all copies.
10 # THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 # WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 # MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 # ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 # WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 # ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 # OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
21 use File
::Temp qw
/ :seekable /;
22 if (! eval {require Date
::Parse
;1;}) {
23 print STDERR
"Date::Parse not available - install p5-Time-TimeDate to check cert dates.\n";
28 my $tmp = File
::Temp
->new(TEMPLATE
=> '/tmp/splitcert.XXXXXXXX');
29 my $t = $tmp->filename;
34 my $rcsid = '# $'.'OpenBSD$';
37 $rcsid = $_ if ($_ =~ m/^# \$[O]penBSD/);
38 $incert++ if ($_ =~ m/^-----BEGIN CERTIFICATE-----/);
39 print $tmp $_ if ($incert);
41 if ($_ =~ m/^-----END CERTIFICATE-----/) {
44 my $issuer = `openssl x509 -in $t -noout -issuer`;
45 $issuer =~ s/^issuer= (.*)\n/$1/;
46 my $subj = `openssl x509 -in $t -noout -subject`;
47 $subj =~ s/^subject= (.*)\n/$1/;
49 print STDERR
"'$subj' not self-signed"
50 if ($issuer ne $subj);
52 my $o = `openssl x509 -in $t -noout -nameopt sep_multiline,use_quote,esc_msb -subject`;
53 $o =~ s/.*O=([^\n]*).*/$1/sm;
55 if (eval {require Date
::Parse
;1;}) {
56 my $startdate = `openssl x509 -in $t -startdate -noout`;
57 my $enddate = `openssl x509 -in $t -enddate -noout`;
58 $startdate =~ s/notBefore=(.*)\n/$1/;
59 $enddate =~ s/notAfter=(.*)\n/$1/;
60 my $starttime = str2time
($startdate);
61 my $endtime = str2time
($enddate);
63 if ($starttime > time) {
64 print STDERR
"'$subj' not valid yet\n"
66 if ($endtime < time) {
67 print STDERR
"'$subj' expired on $startdate\n"
68 } elsif ($endtime < time + 86400 * 365 * 2) {
69 print STDERR
"'$subj' expires on $enddate\n"
73 my $info = qx/openssl x509 -in $t -text -fingerprint -sha1 -certopt no_pubkey,no_sigdump,no_issuer -noout/;
74 $info .= qx/openssl x509 -in $t -fingerprint -sha256 -noout/;
75 my $cert = qx/openssl x509 -in $t/;
77 if (defined $ca{$o}{$subj}) {
78 print STDERR
"'$subj': duplicate\n";
81 $ca{$o}{$subj}{'subj'} = $subj;
82 $ca{$o}{$subj}{'info'} = $info;
83 $ca{$o}{$subj}{'cert'} = $cert;
85 $tmp->seek(0, SEEK_SET
);
92 foreach my $o (sort{lc($a) cmp lc($b)} keys %ca) {
94 foreach my $subj (sort{lc($a) cmp lc($b)} keys %{ $ca{$o} }) {
96 print $ca{$o}{$subj}{'info'};
97 print $ca{$o}{$subj}{'cert'};
101 # print a visual summary at the end
102 foreach my $o (sort{lc($a) cmp lc($b)} keys %ca) {
103 print STDERR
"\n$o\n";
104 foreach my $subj (sort{lc($a) cmp lc($b)} keys %{ $ca{$o} }) {
105 print STDERR
" $subj\n";