summaryrefslogtreecommitdiff
path: root/scripts/DumpWeakCerts.pl
blob: cecdad526ad25b2a249edf9d8c821a815ed5c8a7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
#!/usr/bin/perl
# Script to dump weak RSA certs (Exponent 3 or Modulus size < 1024) according to https://bugs.cacert.org/view.php?id=918
# and https://wiki.cacert.org/Arbitrations/a20110312.1

use strict;
use warnings;

use DBI;

my $cacert_db_config;
my $cacert_db_user;
my $cacert_db_password;

# Read database access data from the config file
eval `cat perl_mysql`;

my $dbh = DBI->connect($cacert_db_config, $cacert_db_user, $cacert_db_password, { RaiseError => 1, AutoCommit => 0 } ) || die "Cannot connect database: $DBI::errstr";

my $sth_certs;
my $sth_userdata;

my $cert_domid;
my $cert_userid;
my $cert_CN;
my $cert_expire;
my $cert_filename;

my $user_email;
my $user_firstname;

my @row;

sub IsWeak($) {
  my ($CertFileName) = @_;

  my $ModulusSize = 0;
  my $Exponent = 0;
  my $result = 0;
    
  open(CERTTEXT, '-|', "openssl x509 -in $CertFileName -noout -text") || die "Cannot start openssl";
  while (<CERTTEXT>) {
    if (/^ +([^ ]+) Public Key:/) {
      last if ($1 ne "RSA");
    }
    if (/^ +Modulus \((\d+) bit\)/) {
      $ModulusSize = $1;
    }
    if (/^ +Exponent: (\d+)/) {
      $Exponent = $1;
      last;
    }
  }
  close(CERTTEXT);
  if ($ModulusSize > 0 && $Exponent > 0) {
    if ($ModulusSize < 1024 || $Exponent==3) {
      $result = 1;
    }
  }
}

# Select only certificates expiring in more than two weeks, since two weeks will probably be needed as turnaround time
# Get all domain certificates
$sth_certs = $dbh->prepare(
  "SELECT `dc`.`domid`, `dc`.`CN`, `dc`.`expire`, `dc`.`crt_name` ".
  "  FROM `domaincerts` AS `dc` ".
  "  WHERE `dc`.`expire` > DATE_ADD(NOW(), INTERVAL 14 DAY)");
$sth_certs->execute();

$sth_userdata = $dbh->prepare(
  "SELECT `u`.`email`, `u`.`fname` ".
  "  FROM `domains` AS `d`, `users` AS `u` ".
  "  WHERE `d`.`memid`=`u`.`id` AND `d`.`id`=?");
  
while(($cert_domid, $cert_CN, $cert_expire, $cert_filename) = $sth_certs->fetchrow_array) {
  if (-f $cert_filename) {
    if (IsWeak($cert_filename)) {
      $sth_userdata->execute($cert_domid);
      ($user_email, $user_firstname) = $sth_userdata->fetchrow_array();
      print join("\t", ('DomainCert', $user_email, $user_firstname, $cert_expire, $cert_CN)). "\n";
      $sth_userdata->finish();
    }
  }
}
$sth_certs->finish();

# Get all email certificates
$sth_certs = $dbh->prepare(
  "SELECT `ec`.`memid`, `ec`.`CN`, `ec`.`expire`, `ec`.`crt_name` ".
  "  FROM `emailcerts` AS `ec` ".
  "  WHERE `ec`.`expire` > DATE_ADD(NOW(), INTERVAL 14 DAY)");
$sth_certs->execute();

$sth_userdata = $dbh->prepare(
  "SELECT `u`.`email`, `u`.`fname` ".
  "  FROM `users` AS `u` ".
  "  WHERE `u`.`id`=?");
  
while(($cert_userid, $cert_CN, $cert_expire, $cert_filename) = $sth_certs->fetchrow_array) {
  if (-f $cert_filename) {
    if (IsWeak($cert_filename)) {
      $sth_userdata->execute($cert_userid);
      ($user_email, $user_firstname) = $sth_userdata->fetchrow_array();
      print join("\t", ('EmailCert', $user_email, $user_firstname, $cert_expire, $cert_CN)). "\n";
      $sth_userdata->finish();
    }
  }
}
$sth_certs->finish();

# Get all Org Server certificates, notify all admins of the Org!
$sth_certs = $dbh->prepare(
  "SELECT `dc`.`orgid`, `dc`.`CN`, `dc`.`expire`, `dc`.`crt_name` ".
  "  FROM `orgdomaincerts` AS `dc` ".
  "  WHERE `dc`.`expire` > DATE_ADD(NOW(), INTERVAL 14 DAY)");
$sth_certs->execute();

$sth_userdata = $dbh->prepare(
  "SELECT `u`.`email`, `u`.`fname` ".
  "  FROM `users` AS `u`, `org` ".
  "  WHERE `u`.`id`=`org`.`memid` and `org`.`orgid`=?");
  
while(($cert_userid, $cert_CN, $cert_expire, $cert_filename) = $sth_certs->fetchrow_array) {
  if (-f $cert_filename) {
    if (IsWeak($cert_filename)) {
      $sth_userdata->execute($cert_userid);
      while(($user_email, $user_firstname) = $sth_userdata->fetchrow_array()) {
        print join("\t", ('OrgServerCert', $user_email, $user_firstname, $cert_expire, $cert_CN)). "\n";
      }
      $sth_userdata->finish();
    }
  }
}
$sth_certs->finish();

# Get all Org Email certificates, notify all admins of the Org!
$sth_certs = $dbh->prepare(
  "SELECT `ec`.`orgid`, `ec`.`CN`, `ec`.`expire`, `ec`.`crt_name` ".
  "  FROM `orgemailcerts` AS `ec` ".
  "  WHERE `ec`.`expire` > DATE_ADD(NOW(), INTERVAL 14 DAY)");
$sth_certs->execute();

$sth_userdata = $dbh->prepare(
  "SELECT `u`.`email`, `u`.`fname` ".
  "  FROM `users` AS `u`, `org` ".
  "  WHERE `u`.`id`=`org`.`memid` and `org`.`orgid`=?");
  
while(($cert_userid, $cert_CN, $cert_expire, $cert_filename) = $sth_certs->fetchrow_array) {
  if (-f $cert_filename) {
    if (IsWeak($cert_filename)) {
      $sth_userdata->execute($cert_userid);
      while(($user_email, $user_firstname) = $sth_userdata->fetchrow_array()) {
        print join("\t", ('OrgEmailCert', $user_email, $user_firstname, $cert_expire, $cert_CN)). "\n";
      }
      $sth_userdata->finish();
    }
  }
}
$sth_certs->finish();

$dbh->disconnect();