562716a6e1f6e12634e6ae1dc647d19d6672d87c
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) 
Bernd Wurst Updated copyright notice (2...

Bernd Wurst authored 11 years ago

5) Written 2008-2013 by schokokeks.org Hosting, namely
Bernd Wurst Added license tags for CC0,...

Bernd Wurst authored 12 years ago

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'];
Bernd Wurst order SSL certs and CSR alp...

Bernd Wurst authored 12 years ago

27)   $result = db_query("SELECT id, valid_from, valid_until, subject, cn FROM vhosts.certs WHERE uid=${uid} ORDER BY cn");
bernd Neue Zertifikatsverwaltung

bernd authored 14 years ago

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'];
Bernd Wurst order SSL certs and CSR alp...

Bernd Wurst authored 12 years ago

38)   $result = db_query("SELECT id, created, hostname, bits FROM vhosts.csr WHERE uid=${uid} ORDER BY hostname");
bernd CSR-Erstellung

bernd authored 14 years ago

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)   
Bernd Wurst Neuer Workflow für Cert-Rep...

Bernd Wurst authored 11 years ago

63)   $result = db_query("SELECT id, created, hostname, bits, `replace`, csr, `key` FROM vhosts.csr WHERE uid={$uid} AND id={$id}");
bernd CSR-Erstellung

bernd authored 14 years ago

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');
Bernd Wurst use temporary file for vali...

Bernd Wurst authored 12 years ago

139)   $chain = (int) get_chain($cert);
bernd Cert-Chain erkennen und ben...

bernd authored 13 years ago

140)   if ($chain)
141)   {
Bernd Wurst use temporary file for vali...

Bernd Wurst authored 12 years ago

142)     $result = db_query("SELECT content FROM vhosts.certchain WHERE id={$chain}");
143)     $tmp = mysql_fetch_assoc($result);
144)     $chaincert = $tmp['content'];
145)     $chainfile = tempnam(sys_get_temp_dir(), 'webinterface');
146)     $f = fopen($chainfile, "w");
147)     fwrite($f, $chaincert);
148)     fclose($f);
149)     $cacerts[] = $chainfile;
bernd Cert-Chain erkennen und ben...

bernd authored 13 years ago

150)   }
bernd Neue Zertifikatsverwaltung

bernd authored 14 years ago

151) 
Bernd Wurst use temporary file for vali...

Bernd Wurst authored 12 years ago

152)   $valid = openssl_x509_checkpurpose($cert, X509_PURPOSE_SSL_SERVER, $cacerts);
153)   if ($chain) {
154)     unlink($chainfile);
155)   }
156)   if ($valid !== true)
bernd Neue Zertifikatsverwaltung

bernd authored 14 years ago

157)   { 
158)     DEBUG('certificate was not validated as a server certificate with the available chain');
159)     return CERT_NOCHAIN;
160)   }
161) 
162)   return CERT_OK;
163) }
164) 
165) 
166) function parse_cert_details($cert)
167) {
168)   $certdata = openssl_x509_parse($cert, true);
169)   /* 
170) name => /CN=*.bwurst.org
171) validFrom_time_t => 1204118790
172) validTo_time_t => 1267190790
173) 
174) 
175)   */
bernd Erlaube nur RSA- und DSA-Ze...

bernd authored 13 years ago

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

bernd authored 14 years ago

177)   //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

178)   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

179) }
180) 
181) 
182) function save_cert($info, $cert, $key)
183) {
hanno zertifikate und keys export...

hanno authored 14 years ago

184)   openssl_pkey_export($key, $key);
185)   openssl_x509_export($cert, $cert);
bernd Neue Zertifikatsverwaltung

bernd authored 14 years ago

186)   $subject = mysql_real_escape_string(filter_input_general($info['subject']));
187)   $cn = mysql_real_escape_string(filter_input_general($info['cn']));
188)   $valid_from = mysql_real_escape_string($info['valid_from']);
189)   $valid_until = mysql_real_escape_string($info['valid_until']);
bernd Cert-Chain erkennen und ben...

bernd authored 13 years ago

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

bernd authored 14 years ago

191)   $cert = mysql_real_escape_string($cert);
192)   $key = mysql_real_escape_string($key);
193)   $uid = (int) $_SESSION['userinfo']['uid'];
194) 
bernd Cert-Chain erkennen und ben...

bernd authored 13 years ago

195)   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

196) }
197) 
bernd Cert-Refresh

bernd authored 14 years ago

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

bernd authored 14 years ago

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

bernd authored 14 years ago

200) {
hanno zertifikate und keys export...

hanno authored 14 years ago

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

bernd authored 13 years ago

202)   $chain = maybe_null( get_chain($cert) );
203) 
bernd Cert-Refresh

bernd authored 14 years ago

204)   $id = (int) $id;
205)   $oldcert = cert_details($id);
206)   $cert = mysql_real_escape_string($cert);
Bernd Wurst Ersetze die Zertifikats-inf...

Bernd Wurst authored 11 years ago

207)   $subject = mysql_real_escape_string(filter_input_general($info['subject']));
208)   $cn = mysql_real_escape_string(filter_input_general($info['cn']));
bernd Cert-Refresh

bernd authored 14 years ago

209)   
210)   $valid_from = mysql_real_escape_string($info['valid_from']);
211)   $valid_until = mysql_real_escape_string($info['valid_until']);
212) 
bernd neue Bilder, mehr SSL-Zerti...

bernd authored 14 years ago

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

hanno authored 14 years ago

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

bernd authored 14 years ago

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

hanno authored 14 years ago

217)   }
Bernd Wurst Ersetze die Zertifikats-inf...

Bernd Wurst authored 11 years ago

218)   db_query("UPDATE vhosts.certs SET subject='{$subject}', cn='{$cn}', 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

219) }
220) 
221) 
bernd Neue Zertifikatsverwaltung

bernd authored 14 years ago

222) function delete_cert($id)
223) {
224)   $uid = (int) $_SESSION['userinfo']['uid'];
225)   $id = (int) $id;
226)   
227)   db_query("DELETE FROM vhosts.certs WHERE uid={$uid} AND id={$id} LIMIT 1");
228) }
229) 
bernd CSR-Erstellung

bernd authored 14 years ago

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

hanno authored 14 years ago

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

bernd authored 14 years ago

279)   $retval = 0;
280)   exec($cmdline, $output, $retval);
281)   DEBUG($output);
282)   DEBUG($retval);
283)   if ($retval != 0)
284)   {
bernd Bugfix: Beim Eintragen eine...

bernd authored 13 years ago

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

bernd authored 14 years ago

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

hanno authored 14 years ago

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

bernd authored 14 years ago

337)   $retval = 0;
338)   exec($cmdline, $output, $retval);
339)   DEBUG($output);
340)   DEBUG($retval);
341)   if ($retval != 0)
342)   {
bernd Bugfix: Beim Eintragen eine...

bernd authored 13 years ago

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

bernd authored 14 years ago

344)   }
345)   
346)   $csr = file_get_contents($csrfile);
347)   $key = file_get_contents($keyfile);
348) 
349)   unlink($csrfile);
350)   unlink($keyfile);
351)   unlink($config);
352) 
353)   return array($csr, $key);
354) }
355) 
356) 
357) 
Bernd Wurst Neuer Workflow für Cert-Rep...

Bernd Wurst authored 11 years ago

358) function save_csr($cn, $bits, $wildcard=true, $replace=NULL)
bernd CSR-Erstellung

bernd authored 14 years ago

359) {
bernd (Verständliche) Fehlermeldu...

bernd authored 13 years ago

360)   if (! $cn) {
361)     system_failure("Sie müssen einen Domainname eingeben!");
362)   }
bernd CSR-Erstellung

bernd authored 14 years ago

363)   $csr = NULL;
364)   $key = NULL;
365)   if ($wildcard)
366)     list($csr, $key) = create_wildcard_csr($cn, $bits);
367)   else
368)     list($csr, $key) = create_csr($cn, $bits);
369)   
370)   $uid = (int) $_SESSION['userinfo']['uid'];
371)   $cn = mysql_real_escape_string(filter_input_hostname($cn));
372)   $bits = (int) $bits;
Bernd Wurst Bugfix: Man kann keine neue...

Bernd Wurst authored 11 years ago

373)   $replace = ($replace ? (int) $replace : 'NULL');
bernd CSR-Erstellung

bernd authored 14 years ago

374)   $csr = mysql_real_escape_string($csr);
375)   $key = mysql_real_escape_string($key);
Bernd Wurst Neuer Workflow für Cert-Rep...

Bernd Wurst authored 11 years ago

376)   db_query("INSERT INTO vhosts.csr (uid, hostname, bits, `replace`, csr, `key`) VALUES ({$uid}, '{$cn}', {$bits}, {$replace}, '{$csr}', '{$key}')");