1319a6d95bd61316641539a7d0b8237c629a557e
bernd Neue Zertifikatsverwaltung

bernd authored 14 years ago

1) <?php
2) 
3) require_once('inc/base.php');
bernd CSR-Erstellung

bernd authored 14 years ago

4) require_once('inc/security.php');
bernd Neue Zertifikatsverwaltung

bernd authored 14 years ago

5) 
6) define("CERT_OK", 0);
7) define("CERT_INVALID", 1);
8) define("CERT_NOCHAIN", 2);
9) 
10) function user_certs()
11) {
12)   $uid = (int) $_SESSION['userinfo']['uid'];
13)   $result = db_query("SELECT id, valid_from, valid_until, subject, cn FROM vhosts.certs WHERE uid=${uid}");
14)   $ret = array();
15)   while ($i = mysql_fetch_assoc($result))
16)     $ret[] = $i;
17)   DEBUG($ret);
18)   return $ret;
19) }
20) 
bernd CSR-Erstellung

bernd authored 14 years ago

21) function user_csr()
22) {
23)   $uid = (int) $_SESSION['userinfo']['uid'];
24)   $result = db_query("SELECT id, created, hostname, bits FROM vhosts.csr WHERE uid=${uid}");
25)   $ret = array();
26)   while ($i = mysql_fetch_assoc($result))
27)     $ret[] = $i;
28)   DEBUG($ret);
29)   return $ret;
30) }
bernd Neue Zertifikatsverwaltung

bernd authored 14 years ago

31) 
32) function cert_details($id)
33) {
34)   $id = (int) $id;
35)   $uid = (int) $_SESSION['userinfo']['uid'];
36)   
bernd CSR-Erstellung

bernd authored 14 years ago

37)   $result = db_query("SELECT id, lastchange, valid_from, valid_until, subject, cn, cert, `key` FROM vhosts.certs WHERE uid={$uid} AND id={$id}");
bernd Neue Zertifikatsverwaltung

bernd authored 14 years ago

38)   if (mysql_num_rows($result) != 1)
39)     system_failure("Ungültiges Zertifikat");
40)   return mysql_fetch_assoc($result);
41) }
42) 
43) 
bernd CSR-Erstellung

bernd authored 14 years ago

44) function csr_details($id)
45) {
46)   $id = (int) $id;
47)   $uid = (int) $_SESSION['userinfo']['uid'];
48)   
49)   $result = db_query("SELECT id, created, hostname, bits, csr, `key` FROM vhosts.csr WHERE uid={$uid} AND id={$id}");
50)   if (mysql_num_rows($result) != 1)
51)     system_failure("Ungültiger CSR");
52)   return mysql_fetch_assoc($result);
53) }
54) 
55) 
bernd Neue Zertifikatsverwaltung

bernd authored 14 years ago

56) function get_available_CAs()
57) {
58)   $path = '/etc/apache2/certs/cabundle/';
59)   $ret = glob($path.'*.pem');
60)   if (! $ret)
61)     system_failure("Konnte die CA-Zertifikate nicht laden");
62)   DEBUG($ret);
63)   return $ret;
64) }
65) 
66) 
67) function validate_certificate($cert, $key)
68) {  
69)   if (openssl_x509_check_private_key($cert, $key) !== true)
70)   {
71)     DEBUG("Zertifikat und Key passen nicht zusammen");
72)     return CERT_INVALID;
73)   }
74) 
75)   $cacerts = get_available_CAs();
76) 
77)   if (openssl_x509_checkpurpose($cert, X509_PURPOSE_SSL_SERVER, $cacerts) !== true)
78)   { 
79)     DEBUG('certificate was not validated as a server certificate with the available chain');
80)     return CERT_NOCHAIN;
81)   }
82) 
83)   return CERT_OK;
84) }
85) 
86) 
87) function parse_cert_details($cert)
88) {
89)   $certdata = openssl_x509_parse($cert, true);
90)   /* 
91) name => /CN=*.bwurst.org
92) validFrom_time_t => 1204118790
93) validTo_time_t => 1267190790
94) 
95) 
96)   */
97)  
98)   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) }
100) 
101) 
102) function save_cert($info, $cert, $key)
103) {
104)   $subject = mysql_real_escape_string(filter_input_general($info['subject']));
105)   $cn = mysql_real_escape_string(filter_input_general($info['cn']));
106)   $valid_from = mysql_real_escape_string($info['valid_from']);
107)   $valid_until = mysql_real_escape_string($info['valid_until']);
108)   $cert = mysql_real_escape_string($cert);
109)   $key = mysql_real_escape_string($key);
110)   $uid = (int) $_SESSION['userinfo']['uid'];
111) 
112)   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}')");
113) }
114) 
115) function delete_cert($id)
116) {
117)   $uid = (int) $_SESSION['userinfo']['uid'];
118)   $id = (int) $id;
119)   
120)   db_query("DELETE FROM vhosts.certs WHERE uid={$uid} AND id={$id} LIMIT 1");
121) }
122)