c208bd906b3991555db11b9229846c4601ca408c
bernd Neue Zertifikatsverwaltung

bernd authored 14 years ago

1) <?php
Bernd Wurst Added license tags for CC0,...

Bernd Wurst authored 12 years ago

2) /*
3) This file belongs to the Webinterface of schokokeks.org Hosting
4) 
5) Written 2008-2012 by schokokeks.org Hosting, namely
6)   Bernd Wurst <bernd@schokokeks.org>
7)   Hanno Böck <hanno@schokokeks.org>
8) 
9) To the extent possible under law, the author(s) have dedicated all copyright and related and neighboring rights to this software to the public domain worldwide. This software is distributed without any warranty.
10) 
11) You should have received a copy of the CC0 Public Domain Dedication along with this software. If not, see 
12) http://creativecommons.org/publicdomain/zero/1.0/
13) 
14) Nevertheless, in case you use a significant part of this code, we ask (but not require, see the license) that you keep the authors' names in place and return your changes to the public. We would be especially happy if you tell us what you're going to do with this code.
15) */
bernd Neue Zertifikatsverwaltung

bernd authored 14 years ago

16) 
17) require_once('inc/base.php');
bernd CSR-Erstellung

bernd authored 14 years ago

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

bernd authored 14 years ago

19) 
20) define("CERT_OK", 0);
21) define("CERT_INVALID", 1);
22) define("CERT_NOCHAIN", 2);
23) 
24) function user_certs()
25) {
26)   $uid = (int) $_SESSION['userinfo']['uid'];
27)   $result = db_query("SELECT id, valid_from, valid_until, subject, cn FROM vhosts.certs WHERE uid=${uid}");
28)   $ret = array();
29)   while ($i = mysql_fetch_assoc($result))
30)     $ret[] = $i;
31)   DEBUG($ret);
32)   return $ret;
33) }
34) 
bernd CSR-Erstellung

bernd authored 14 years ago

35) function user_csr()
36) {
37)   $uid = (int) $_SESSION['userinfo']['uid'];
38)   $result = db_query("SELECT id, created, hostname, bits FROM vhosts.csr WHERE uid=${uid}");
39)   $ret = array();
40)   while ($i = mysql_fetch_assoc($result))
41)     $ret[] = $i;
42)   DEBUG($ret);
43)   return $ret;
44) }
bernd Neue Zertifikatsverwaltung

bernd authored 14 years ago

45) 
46) function cert_details($id)
47) {
48)   $id = (int) $id;
49)   $uid = (int) $_SESSION['userinfo']['uid'];
50)   
bernd CSR-Erstellung

bernd authored 14 years ago

51)   $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

52)   if (mysql_num_rows($result) != 1)
bernd Cert-Refresh

bernd authored 14 years ago

53)     system_failure("Ungültiges Zertifikat #{$id}");
bernd Neue Zertifikatsverwaltung

bernd authored 14 years ago

54)   return mysql_fetch_assoc($result);
55) }
56) 
57) 
bernd CSR-Erstellung

bernd authored 14 years ago

58) function csr_details($id)
59) {
60)   $id = (int) $id;
61)   $uid = (int) $_SESSION['userinfo']['uid'];
62)   
63)   $result = db_query("SELECT id, created, hostname, bits, csr, `key` FROM vhosts.csr WHERE uid={$uid} AND id={$id}");
64)   if (mysql_num_rows($result) != 1)
65)     system_failure("Ungültiger CSR");
66)   return mysql_fetch_assoc($result);
67) }
68) 
69) 
bernd Neue Zertifikatsverwaltung

bernd authored 14 years ago

70) function get_available_CAs()
71) {
72)   $path = '/etc/apache2/certs/cabundle/';
73)   $ret = glob($path.'*.pem');
74)   if (! $ret)
75)     system_failure("Konnte die CA-Zertifikate nicht laden");
76)   DEBUG($ret);
77)   return $ret;
78) }
79) 
80) 
bernd Cert-Chain erkennen und ben...

bernd authored 13 years ago

81) function get_chain($cert)
82) {
83)   $certdata = openssl_x509_parse($cert, true);
bernd Mehr Fehlerbehandlung für S...

bernd authored 13 years ago

84)   if ($certdata === FALSE) {
85)     system_failure("Das Zertifikat konnte nicht gelesen werden");
86)   }
87)   if (! isset($certdata['issuer']['CN'])) {
88)     return NULL;
89)   }
bernd Cert-Chain erkennen und ben...

bernd authored 13 years ago

90)   $issuer = mysql_real_escape_string($certdata['issuer']['CN']);
91)   $result = db_query("SELECT id FROM vhosts.certchain WHERE cn='{$issuer}'");
92)   if (mysql_num_rows($result) > 0)
93)   {
94)     $c = mysql_fetch_assoc($result);
95)     //$chainfile = '/etc/apache2/certs/chains/'.$c['id'].'.pem';
96)     DEBUG("identified fitting certificate chain #".$c['id']);
97)     return $c['id'];
98)   }
99) }
100) 
101) 
bernd Neue Zertifikatsverwaltung

bernd authored 14 years ago

102) function validate_certificate($cert, $key)
bernd Mehr Fehlerbehandlung für S...

bernd authored 13 years ago

103) { 
104)   // Lade private key 
105)   $seckey = openssl_get_privatekey($key);
106)   if ($seckey === FALSE) {
107)     system_failure("Der private Schlüssel konnte (ohne Passwort) nicht gelesen werden.");
108)   }
109)   // Lade public key
110)   $pubkey = openssl_get_publickey($cert);
111)   if ($pubkey === FALSE) {
112)     system_failure("In dem eingetragenen Zertifikat wurde kein öffentlicher Schlüssel gefunden.");
113)   }
114)   // Parse Details über den pubkey
115)   $certinfo = openssl_pkey_get_details($pubkey);
bernd Erlaube nur RSA- und DSA-Ze...

bernd authored 13 years ago

116)   DEBUG($certinfo);
bernd Mehr Fehlerbehandlung für S...

bernd authored 13 years ago

117)   if ($certinfo === FALSE) {
118)     system_failure("Der öffentliche Schlüssel des Zertifikats konnte nicht gelesen werden");
bernd Erlaube nur RSA- und DSA-Ze...

bernd authored 13 years ago

119)   }
bernd Mehr Fehlerbehandlung für S...

bernd authored 13 years ago

120) 
121)   // Apache unterstützt nur Schlüssel vom Typ RSA oder DSA
122)   if (! in_array($certinfo['type'], array(OPENSSL_KEYTYPE_RSA, OPENSSL_KEYTYPE_DSA))) {
bernd Erlaube nur RSA- und DSA-Ze...

bernd authored 13 years ago

123)     system_failure("Dieser Schlüssel nutzt einen nicht unterstützten Algorithmus.");
124)   }
125)     
bernd Mehr Fehlerbehandlung für S...

bernd authored 13 years ago

126)   // Bei ECC-Keys treten kürzere Schlüssellängen auf, die können wir aktuell aber sowieso nicht unterstützen
127)   if ($certinfo['bits'] < 2048) {
128)     warning("Dieser Schlüssel hat eine sehr geringe Bitlänge und ist daher als nicht besonders sicher einzustufen!");
129)   }
130) 
131)   // Prüfe ob Key und Zertifikat zusammen passen
bernd Neue Zertifikatsverwaltung

bernd authored 14 years ago

132)   if (openssl_x509_check_private_key($cert, $key) !== true)
133)   {
134)     DEBUG("Zertifikat und Key passen nicht zusammen");
135)     return CERT_INVALID;
136)   }
137) 
bernd Cert-Chain erkennen und ben...

bernd authored 13 years ago

138)   $cacerts = array('/etc/ssl/certs');
139)   $chain = get_chain($cert);
140)   if ($chain)
141)   {
142)     $cacerts[] = '/etc/apache2/certs/chains/'.$chain.'.pem';
143)   }
bernd Neue Zertifikatsverwaltung

bernd authored 14 years ago

144) 
145)   if (openssl_x509_checkpurpose($cert, X509_PURPOSE_SSL_SERVER, $cacerts) !== true)
146)   { 
147)     DEBUG('certificate was not validated as a server certificate with the available chain');
148)     return CERT_NOCHAIN;
149)   }
150) 
151)   return CERT_OK;
152) }
153) 
154) 
155) function parse_cert_details($cert)
156) {
157)   $certdata = openssl_x509_parse($cert, true);
158)   /* 
159) name => /CN=*.bwurst.org
160) validFrom_time_t => 1204118790
161) validTo_time_t => 1267190790
162) 
163) 
164)   */
bernd Erlaube nur RSA- und DSA-Ze...

bernd authored 13 years ago

165)   DEBUG($certdata);
bernd Cert-Name ist Subject-CN un...

bernd authored 14 years ago

166)   //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']));
bernd Cert-Chain erkennen und ben...

bernd authored 13 years ago

167)   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']);
bernd Neue Zertifikatsverwaltung

bernd authored 14 years ago

168) }
169) 
170) 
171) function save_cert($info, $cert, $key)
172) {
hanno zertifikate und keys export...

hanno authored 14 years ago

173)   openssl_pkey_export($key, $key);
174)   openssl_x509_export($cert, $cert);
bernd Neue Zertifikatsverwaltung

bernd authored 14 years ago

175)   $subject = mysql_real_escape_string(filter_input_general($info['subject']));
176)   $cn = mysql_real_escape_string(filter_input_general($info['cn']));
177)   $valid_from = mysql_real_escape_string($info['valid_from']);
178)   $valid_until = mysql_real_escape_string($info['valid_until']);
bernd Cert-Chain erkennen und ben...

bernd authored 13 years ago

179)   $chain = maybe_null( get_chain($cert) );
bernd Neue Zertifikatsverwaltung

bernd authored 14 years ago

180)   $cert = mysql_real_escape_string($cert);
181)   $key = mysql_real_escape_string($key);
182)   $uid = (int) $_SESSION['userinfo']['uid'];
183) 
bernd Cert-Chain erkennen und ben...

bernd authored 13 years ago

184)   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}')");
bernd Neue Zertifikatsverwaltung

bernd authored 14 years ago

185) }
186) 
bernd Cert-Refresh

bernd authored 14 years ago

187) 
bernd neue Bilder, mehr SSL-Zerti...

bernd authored 14 years ago

188) function refresh_cert($id, $info, $cert, $key = NULL)
bernd Cert-Refresh

bernd authored 14 years ago

189) {
hanno zertifikate und keys export...

hanno authored 14 years ago

190)   openssl_x509_export($cert, $cert);
bernd Bugfix: Beim Eintragen eine...

bernd authored 13 years ago

191)   $chain = maybe_null( get_chain($cert) );
192) 
bernd Cert-Refresh

bernd authored 14 years ago

193)   $id = (int) $id;
194)   $oldcert = cert_details($id);
195)   $cert = mysql_real_escape_string($cert);
196)   
197)   $valid_from = mysql_real_escape_string($info['valid_from']);
198)   $valid_until = mysql_real_escape_string($info['valid_until']);
199) 
bernd neue Bilder, mehr SSL-Zerti...

bernd authored 14 years ago

200)   $keyop = '';
hanno zertifikate und keys export...

hanno authored 14 years ago

201)   if ($key) {
202)     openssl_pkey_export($key, $key);
bernd neue Bilder, mehr SSL-Zerti...

bernd authored 14 years ago

203)     $keyop = ", `key`='".mysql_real_escape_string($key)."'";
hanno zertifikate und keys export...

hanno authored 14 years ago

204)   }
bernd chain auch beim refresh erm...

bernd authored 13 years ago

205)   db_query("UPDATE vhosts.certs SET cert='{$cert}'{$keyop}, valid_from='{$valid_from}', valid_until='{$valid_until}', chain={$chain} WHERE id={$id} LIMIT 1");
bernd Cert-Refresh

bernd authored 14 years ago

206) }
207) 
208) 
bernd Neue Zertifikatsverwaltung

bernd authored 14 years ago

209) function delete_cert($id)
210) {
211)   $uid = (int) $_SESSION['userinfo']['uid'];
212)   $id = (int) $id;
213)   
214)   db_query("DELETE FROM vhosts.certs WHERE uid={$uid} AND id={$id} LIMIT 1");
215) }
216) 
bernd CSR-Erstellung

bernd authored 14 years ago

217) function delete_csr($id)
218) {
219)   $uid = (int) $_SESSION['userinfo']['uid'];
220)   $id = (int) $id;
221)   
222)   db_query("DELETE FROM vhosts.csr WHERE uid={$uid} AND id={$id} LIMIT 1");
223) }
224) 
225) function create_wildcard_csr($cn, $bits)
226) {
227)   $cn = filter_input_hostname($cn);
228)   $bits = (int) $bits;
229)   if ($bits == 0)
230)     $bits = 4096;
231) 
232)   $keyfile = tempnam(ini_get('upload_tmp_dir'), 'key');
233)   $csrfile = tempnam(ini_get('upload_tmp_dir'), 'csr');
234)   $config = tempnam(ini_get('upload_tmp_dir'), 'config');
235) 
236)   DEBUG("key: ".$keyfile." / csr: ".$csrfile." / config: ".$config);
237) 
238)   $c = fopen($config, "w");
239)   fwrite($c, "[req]
240) default_bits = {$bits}
241) default_keyfile = {$keyfile}
242) encrypt_key = no
243) distinguished_name      = req_distinguished_name
244) req_extensions = v3_req
245) 
246) [v3_req]
247) subjectAltName = DNS:{$cn}, DNS:*.{$cn}
248) 
249) [ req_distinguished_name ]
250) countryName                     = Country Name (2 letter code)
251) countryName_default             = DE
252) stateOrProvinceName             = State or Province Name (full name)
253) stateOrProvinceName_default     = Baden-Wuerttemberg
254) localityName                    = Locality Name (eg, city)
255) localityName_default            = Murrhardt
256) 0.organizationName              = Organization Name (eg, company)
257) 0.organizationName_default      = schokokeks.org
258) 
259) commonName = Common Name
260) commonName_default = *.{$cn}
261) ");
262)   fclose($c);
263) 
264)   $output = '';
hanno sha2 csrs

hanno authored 14 years ago

265)   $cmdline = "openssl req -sha256 -new -batch -config {$config} -out {$csrfile}";
bernd CSR-Erstellung

bernd authored 14 years ago

266)   $retval = 0;
267)   exec($cmdline, $output, $retval);
268)   DEBUG($output);
269)   DEBUG($retval);
270)   if ($retval != 0)
271)   {
bernd Bugfix: Beim Eintragen eine...

bernd authored 13 years ago

272)     system_failure("Die Erzeugung des CSR ist fehlgeschlagen. Ausgabe des OpenSSL-Befehls: ".print_r($output, true));
bernd CSR-Erstellung

bernd authored 14 years ago

273)   }
274)   
275)   $csr = file_get_contents($csrfile);
276)   $key = file_get_contents($keyfile);
277) 
278)   unlink($csrfile);
279)   unlink($keyfile);
280)   unlink($config);
281) 
282)   return array($csr, $key);
283) }
284) 
285) 
286) 
287) function create_csr($cn, $bits)
288) {
289)   $cn = filter_input_hostname($cn);
290)   $bits = (int) $bits;
291)   if ($bits == 0)
292)     $bits = 4096;
293) 
294)   $keyfile = tempnam(ini_get('upload_tmp_dir'), 'key');
295)   $csrfile = tempnam(ini_get('upload_tmp_dir'), 'csr');
296)   $config = tempnam(ini_get('upload_tmp_dir'), 'config');
297) 
298)   DEBUG("key: ".$keyfile." / csr: ".$csrfile." / config: ".$config);
299) 
300)   $c = fopen($config, "w");
301)   fwrite($c, "[req]
302) default_bits = {$bits}
303) default_keyfile = {$keyfile}
304) encrypt_key = no
305) distinguished_name      = req_distinguished_name
306) 
307) [ req_distinguished_name ]
308) countryName                     = Country Name (2 letter code)
309) countryName_default             = DE
310) stateOrProvinceName             = State or Province Name (full name)
311) stateOrProvinceName_default     = Baden-Wuerttemberg
312) localityName                    = Locality Name (eg, city)
313) localityName_default            = Murrhardt
314) 0.organizationName              = Organization Name (eg, company)
315) 0.organizationName_default      = schokokeks.org
316) 
317) commonName = Common Name
318) commonName_default = {$cn}
319) ");
320)   fclose($c);
321) 
322)   $output = '';
hanno sha2 csrs

hanno authored 14 years ago

323)   $cmdline = "openssl req -sha256 -new -batch -config {$config} -out {$csrfile}";
bernd CSR-Erstellung

bernd authored 14 years ago

324)   $retval = 0;
325)   exec($cmdline, $output, $retval);
326)   DEBUG($output);
327)   DEBUG($retval);
328)   if ($retval != 0)
329)   {
bernd Bugfix: Beim Eintragen eine...

bernd authored 13 years ago

330)     system_failure("Die Erzeugung des CSR ist fehlgeschlagen. Ausgabe des OpenSSL-Befehls: ".print_r($output, true));
bernd CSR-Erstellung

bernd authored 14 years ago

331)   }
332)   
333)   $csr = file_get_contents($csrfile);
334)   $key = file_get_contents($keyfile);
335) 
336)   unlink($csrfile);
337)   unlink($keyfile);
338)   unlink($config);
339) 
340)   return array($csr, $key);
341) }
342) 
343) 
344) 
345) function save_csr($cn, $bits, $wildcard=true)
346) {
bernd (Verständliche) Fehlermeldu...

bernd authored 13 years ago

347)   if (! $cn) {
348)     system_failure("Sie müssen einen Domainname eingeben!");
349)   }