Cert-Chain erkennen und benutzen
bernd

bernd commited on 2010-07-27 17:29:24
Zeige 1 geänderte Dateien mit 24 Einfügungen und 3 Löschungen.


git-svn-id: https://svn.schokokeks.org/repos/tools/webinterface/trunk@1765 87cf0b9e-d624-0410-a070-f6ee81989793
... ...
@@ -64,6 +64,21 @@ function get_available_CAs()
64 64
 }
65 65
 
66 66
 
67
+function get_chain($cert)
68
+{
69
+  $certdata = openssl_x509_parse($cert, true);
70
+  $issuer = mysql_real_escape_string($certdata['issuer']['CN']);
71
+  $result = db_query("SELECT id FROM vhosts.certchain WHERE cn='{$issuer}'");
72
+  if (mysql_num_rows($result) > 0)
73
+  {
74
+    $c = mysql_fetch_assoc($result);
75
+    //$chainfile = '/etc/apache2/certs/chains/'.$c['id'].'.pem';
76
+    DEBUG("identified fitting certificate chain #".$c['id']);
77
+    return $c['id'];
78
+  }
79
+}
80
+
81
+
67 82
 function validate_certificate($cert, $key)
68 83
 {  
69 84
   if (openssl_x509_check_private_key($cert, $key) !== true)
... ...
@@ -72,7 +87,12 @@ function validate_certificate($cert, $key)
72 87
     return CERT_INVALID;
73 88
   }
74 89
 
75
-  $cacerts = get_available_CAs();
90
+  $cacerts = array('/etc/ssl/certs');
91
+  $chain = get_chain($cert);
92
+  if ($chain)
93
+  {
94
+    $cacerts[] = '/etc/apache2/certs/chains/'.$chain.'.pem';
95
+  }
76 96
 
77 97
   if (openssl_x509_checkpurpose($cert, X509_PURPOSE_SSL_SERVER, $cacerts) !== true)
78 98
   { 
... ...
@@ -96,7 +116,7 @@ validTo_time_t => 1267190790
96 116
   */
97 117
  
98 118
   //return array('subject' => $certdata['name'], 'cn' => $certdata['subject']['CN'], 'valid_from' => date('Y-m-d', $certdata['validFrom_time_t']), 'valid_until' => date('Y-m-d', $certdata['validTo_time_t']));
99
-  return array('subject' => $certdata['subject']['CN'].' / '.$certdata['issuer']['O'], 'cn' => $certdata['subject']['CN'], 'valid_from' => date('Y-m-d', $certdata['validFrom_time_t']), 'valid_until' => date('Y-m-d', $certdata['validTo_time_t']));
119
+  return array('subject' => $certdata['subject']['CN'].' / '.$certdata['issuer']['O'], 'cn' => $certdata['subject']['CN'], 'valid_from' => date('Y-m-d', $certdata['validFrom_time_t']), 'valid_until' => date('Y-m-d', $certdata['validTo_time_t']), 'issuer' => $certdata['issuer']['CN']);
100 120
 }
101 121
 
102 122
 
... ...
@@ -108,11 +128,12 @@ function save_cert($info, $cert, $key)
108 128
   $cn = mysql_real_escape_string(filter_input_general($info['cn']));
109 129
   $valid_from = mysql_real_escape_string($info['valid_from']);
110 130
   $valid_until = mysql_real_escape_string($info['valid_until']);
131
+  $chain = maybe_null( get_chain($cert) );
111 132
   $cert = mysql_real_escape_string($cert);
112 133
   $key = mysql_real_escape_string($key);
113 134
   $uid = (int) $_SESSION['userinfo']['uid'];
114 135
 
115
-  db_query("INSERT INTO vhosts.certs (uid, subject, cn, valid_from, valid_until, cert, `key`) VALUES ({$uid}, '{$subject}', '{$cn}', '{$valid_from}', '{$valid_until}', '{$cert}', '{$key}')");
136
+  db_query("INSERT INTO vhosts.certs (uid, subject, cn, valid_from, valid_until, chain, cert, `key`) VALUES ({$uid}, '{$subject}', '{$cn}', '{$valid_from}', '{$valid_until}', {$chain}, '{$cert}', '{$key}')");
116 137
 }
117 138
 
118 139
 
119 140