6962c43ce62fb257544a45ed4f04845b04993541
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 Lizenzinfos in eigenes Modu...

Bernd Wurst authored 10 years ago

5) Written 2008-2014 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 Einige Statements auf Prepa...

Bernd Wurst authored 10 years ago

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

bernd authored 14 years ago

28)   $ret = array();
Bernd Wurst Umstellung auf PDO-Datenban...

Bernd Wurst authored 10 years ago

29)   while ($i = $result->fetch())
bernd Neue Zertifikatsverwaltung

bernd authored 14 years ago

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 Einige Statements auf Prepa...

Bernd Wurst authored 10 years ago

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

bernd authored 14 years ago

39)   $ret = array();
Bernd Wurst Umstellung auf PDO-Datenban...

Bernd Wurst authored 10 years ago

40)   while ($i = $result->fetch())
bernd CSR-Erstellung

bernd authored 14 years ago

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 Wurst Status der letsencrypt-Opti...

Bernd Wurst authored 8 years ago

51)   $result = db_query("SELECT id, lastchange, valid_from, valid_until, subject, cn, chain, cert, `key` FROM vhosts.certs WHERE uid=:uid AND id=:id", array(":uid" => $uid, ":id" => $id));
Bernd Wurst Umstellung auf PDO-Datenban...

Bernd Wurst authored 10 years ago

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

bernd authored 14 years ago

53)     system_failure("Ungültiges Zertifikat #{$id}");
Bernd Wurst Umstellung auf PDO-Datenban...

Bernd Wurst authored 10 years ago

54)   return $result->fetch();
bernd Neue Zertifikatsverwaltung

bernd authored 14 years ago

55) }
56) 
Bernd Wurst Status der letsencrypt-Opti...

Bernd Wurst authored 8 years ago

57) function cert_is_letsencrypt($id)
58) {
59)   $details = cert_details($id);
Bernd Wurst Erkenne LE-Zertifikate nich...

Bernd Wurst authored 8 years ago

60)   if ($details['chain'] == config('letsencrypt_chain')) {
Bernd Wurst Status der letsencrypt-Opti...

Bernd Wurst authored 8 years ago

61)     return true;
62)   }
63)   return false;
64) }
65) 
bernd Neue Zertifikatsverwaltung

bernd authored 14 years ago

66) 
bernd CSR-Erstellung

bernd authored 14 years ago

67) function csr_details($id)
68) {
69)   $id = (int) $id;
70)   $uid = (int) $_SESSION['userinfo']['uid'];
71)   
Bernd Wurst Einige Statements auf Prepa...

Bernd Wurst authored 10 years ago

72)   $result = db_query("SELECT id, created, hostname, bits, `replace`, csr, `key` FROM vhosts.csr WHERE uid=:uid AND id=:id", array(":uid" => $uid, ":id" => $id));
Bernd Wurst Umstellung auf PDO-Datenban...

Bernd Wurst authored 10 years ago

73)   if ($result->rowCount() != 1)
bernd CSR-Erstellung

bernd authored 14 years ago

74)     system_failure("Ungültiger CSR");
Bernd Wurst Umstellung auf PDO-Datenban...

Bernd Wurst authored 10 years ago

75)   return $result->fetch();
bernd CSR-Erstellung

bernd authored 14 years ago

76) }
77) 
78) 
bernd Neue Zertifikatsverwaltung

bernd authored 14 years ago

79) function get_available_CAs()
80) {
81)   $path = '/etc/apache2/certs/cabundle/';
82)   $ret = glob($path.'*.pem');
83)   if (! $ret)
84)     system_failure("Konnte die CA-Zertifikate nicht laden");
85)   DEBUG($ret);
86)   return $ret;
87) }
88) 
89) 
bernd Cert-Chain erkennen und ben...

bernd authored 13 years ago

90) function get_chain($cert)
91) {
92)   $certdata = openssl_x509_parse($cert, true);
bernd Mehr Fehlerbehandlung für S...

bernd authored 13 years ago

93)   if ($certdata === FALSE) {
94)     system_failure("Das Zertifikat konnte nicht gelesen werden");
95)   }
96)   if (! isset($certdata['issuer']['CN'])) {
97)     return NULL;
98)   }
Bernd Wurst Einige Statements auf Prepa...

Bernd Wurst authored 10 years ago

99)   $result = db_query("SELECT id FROM vhosts.certchain WHERE cn=?", array($certdata['issuer']['CN']));
Bernd Wurst Umstellung auf PDO-Datenban...

Bernd Wurst authored 10 years ago

100)   if ($result->rowCount() > 0)
bernd Cert-Chain erkennen und ben...

bernd authored 13 years ago

101)   {
Bernd Wurst Umstellung auf PDO-Datenban...

Bernd Wurst authored 10 years ago

102)     $c = $result->fetch();
bernd Cert-Chain erkennen und ben...

bernd authored 13 years ago

103)     //$chainfile = '/etc/apache2/certs/chains/'.$c['id'].'.pem';
104)     DEBUG("identified fitting certificate chain #".$c['id']);
105)     return $c['id'];
106)   }
107) }
108) 
109) 
bernd Neue Zertifikatsverwaltung

bernd authored 14 years ago

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

bernd authored 13 years ago

111) { 
112)   // Lade private key 
113)   $seckey = openssl_get_privatekey($key);
114)   if ($seckey === FALSE) {
115)     system_failure("Der private Schlüssel konnte (ohne Passwort) nicht gelesen werden.");
116)   }
117)   // Lade public key
118)   $pubkey = openssl_get_publickey($cert);
119)   if ($pubkey === FALSE) {
120)     system_failure("In dem eingetragenen Zertifikat wurde kein öffentlicher Schlüssel gefunden.");
121)   }
122)   // Parse Details über den pubkey
123)   $certinfo = openssl_pkey_get_details($pubkey);
bernd Erlaube nur RSA- und DSA-Ze...

bernd authored 13 years ago

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

bernd authored 13 years ago

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

bernd authored 13 years ago

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

bernd authored 13 years ago

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

bernd authored 13 years ago

131)     system_failure("Dieser Schlüssel nutzt einen nicht unterstützten Algorithmus.");
132)   }
133)     
bernd Mehr Fehlerbehandlung für S...

bernd authored 13 years ago

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

bernd authored 14 years ago

140)   if (openssl_x509_check_private_key($cert, $key) !== true)
141)   {
Bernd Wurst Ermögliche einen Zertifikat...

Bernd Wurst authored 10 years ago

142)     DEBUG("Zertifikat und Key passen nicht zusammen: ".openssl_x509_check_private_key($cert, $key));
bernd Neue Zertifikatsverwaltung

bernd authored 14 years ago

143)     return CERT_INVALID;
144)   }
145) 
bernd Cert-Chain erkennen und ben...

bernd authored 13 years ago

146)   $cacerts = array('/etc/ssl/certs');
Bernd Wurst use temporary file for vali...

Bernd Wurst authored 12 years ago

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

bernd authored 13 years ago

148)   if ($chain)
149)   {
Bernd Wurst Einige Statements auf Prepa...

Bernd Wurst authored 10 years ago

150)     $result = db_query("SELECT content FROM vhosts.certchain WHERE id=?", array($chain));
Bernd Wurst Umstellung auf PDO-Datenban...

Bernd Wurst authored 10 years ago

151)     $tmp = $result->fetch();
Bernd Wurst use temporary file for vali...

Bernd Wurst authored 12 years ago

152)     $chaincert = $tmp['content'];
153)     $chainfile = tempnam(sys_get_temp_dir(), 'webinterface');
154)     $f = fopen($chainfile, "w");
155)     fwrite($f, $chaincert);
156)     fclose($f);
157)     $cacerts[] = $chainfile;
bernd Cert-Chain erkennen und ben...

bernd authored 13 years ago

158)   }
bernd Neue Zertifikatsverwaltung

bernd authored 14 years ago

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

Bernd Wurst authored 12 years ago

160)   $valid = openssl_x509_checkpurpose($cert, X509_PURPOSE_SSL_SERVER, $cacerts);
161)   if ($chain) {
162)     unlink($chainfile);
163)   }
164)   if ($valid !== true)
bernd Neue Zertifikatsverwaltung

bernd authored 14 years ago

165)   { 
166)     DEBUG('certificate was not validated as a server certificate with the available chain');
167)     return CERT_NOCHAIN;
168)   }
169) 
170)   return CERT_OK;
171) }
172) 
173) 
174) function parse_cert_details($cert)
175) {
176)   $certdata = openssl_x509_parse($cert, true);
177)   /* 
178) name => /CN=*.bwurst.org
179) validFrom_time_t => 1204118790
180) validTo_time_t => 1267190790
181) 
182) 
183)   */
bernd Erlaube nur RSA- und DSA-Ze...

bernd authored 13 years ago

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

bernd authored 14 years ago

185)   //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 Wurst Letsencrypt vorbereitet und...

Bernd Wurst authored 8 years ago

186)   $issuer = $certdata['issuer']['CN'];
187)   if (isset($certdata['issuer']['O'])) {
188)     $issuer = $certdata['issuer']['O'];
189)   }
190)   return array('subject' => $certdata['subject']['CN'].' / '.$issuer, '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

191) }
192) 
193) 
194) function save_cert($info, $cert, $key)
195) {
hanno zertifikate und keys export...

hanno authored 14 years ago

196)   openssl_pkey_export($key, $key);
197)   openssl_x509_export($cert, $cert);
bernd Neue Zertifikatsverwaltung

bernd authored 14 years ago

198)   $uid = (int) $_SESSION['userinfo']['uid'];
199) 
Bernd Wurst Einige Statements auf Prepa...

Bernd Wurst authored 10 years ago

200)   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)", 
201)         array(":uid" => $uid, ":subject" => filter_input_general($info['subject']), ":cn" => filter_input_general($info['cn']), ":valid_from" => $info['valid_from'], 
202)               ":valid_until" => $info['valid_until'], ":chain" => get_chain($cert), ":cert" => $cert, ":key" => $key));
bernd Neue Zertifikatsverwaltung

bernd authored 14 years ago

203) }
204) 
bernd Cert-Refresh

bernd authored 14 years ago

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

bernd authored 14 years ago

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

bernd authored 14 years ago

207) {
hanno zertifikate und keys export...

hanno authored 14 years ago

208)   openssl_x509_export($cert, $cert);
Bernd Wurst Ein maybe_null() entfernt

Bernd Wurst authored 10 years ago

209)   $chain = get_chain($cert);
bernd Bugfix: Beim Eintragen eine...

bernd authored 13 years ago

210) 
bernd Cert-Refresh

bernd authored 14 years ago

211)   $id = (int) $id;
212)   $oldcert = cert_details($id);
Bernd Wurst Einige Statements auf Prepa...

Bernd Wurst authored 10 years ago

213)   $args = array(":subject" => filter_input_general($info['subject']),
214)                 ":cn" => filter_input_general($info['cn']),
215)                 ":cert" => $cert,
216)                 ":valid_from" => $info['valid_from'],
217)                 ":valid_until" => $info['valid_until'],
218)                 ":chain" => get_chain($cert),
219)                 ":id" => $id);
220) 
bernd neue Bilder, mehr SSL-Zerti...

bernd authored 14 years ago

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

hanno authored 14 years ago

222)   if ($key) {
223)     openssl_pkey_export($key, $key);
Bernd Wurst Einige Statements auf Prepa...

Bernd Wurst authored 10 years ago

224)     $keyop = ", `key`=:key";
225)     $args[":key"] = $key;
hanno zertifikate und keys export...

hanno authored 14 years ago

226)   }
Bernd Wurst Einige Statements auf Prepa...

Bernd Wurst authored 10 years ago

227)   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", $args);
bernd Cert-Refresh

bernd authored 14 years ago

228) }
229) 
230) 
bernd Neue Zertifikatsverwaltung

bernd authored 14 years ago

231) function delete_cert($id)
232) {
233)   $uid = (int) $_SESSION['userinfo']['uid'];
234)   $id = (int) $id;
235)   
Bernd Wurst Einige Statements auf Prepa...

Bernd Wurst authored 10 years ago

236)   db_query("DELETE FROM vhosts.certs WHERE uid=? AND id=?", array($uid, $id));
bernd Neue Zertifikatsverwaltung

bernd authored 14 years ago

237) }
238) 
bernd CSR-Erstellung

bernd authored 14 years ago

239) function delete_csr($id)
240) {
241)   $uid = (int) $_SESSION['userinfo']['uid'];
242)   $id = (int) $id;
243)   
Bernd Wurst Einige Statements auf Prepa...

Bernd Wurst authored 10 years ago

244)   db_query("DELETE FROM vhosts.csr WHERE uid=? AND id=?", array($uid, $id));
bernd CSR-Erstellung

bernd authored 14 years ago

245) }
246) 
247) 
Bernd Wurst * Ermögliche SAN bei CSRs *...

Bernd Wurst authored 8 years ago

248) function split_cn($cn)
249) {
250)   $domains = array();
251)   if (strstr($cn, ',') or strstr($cn, "\n")) {
252)     $domains = preg_split("/[, \n]+/", $cn);
253)     DEBUG("Domains:");
254)     DEBUG($domains);
255)   } else {
256)     $domains[] = $cn;
257)   }
258)   for ($i=0;$i!=count($domains);$i++) {
259)     $domains[$i] = filter_input_hostname($domains[$i], true);
260)   }
261)   return $domains;
262) }
263) 
bernd CSR-Erstellung

bernd authored 14 years ago

264) function create_csr($cn, $bits)
265) {
Bernd Wurst * Ermögliche SAN bei CSRs *...

Bernd Wurst authored 8 years ago

266)   $domains = split_cn($cn);
267)   $tmp = array();
268)   foreach ($domains as $dom) {
269)     $tmp[] = 'DNS:'.$dom;
270)   }
271)   $SAN = "[ v3_req ]\nsubjectAltName = ".implode(', ', $tmp);
272)   DEBUG($SAN);
273)   $cn = $domains[0];
bernd CSR-Erstellung

bernd authored 14 years ago

274)   $bits = (int) $bits;
275)   if ($bits == 0)
276)     $bits = 4096;
277) 
278)   $keyfile = tempnam(ini_get('upload_tmp_dir'), 'key');
279)   $csrfile = tempnam(ini_get('upload_tmp_dir'), 'csr');
280)   $config = tempnam(ini_get('upload_tmp_dir'), 'config');
281) 
282)   DEBUG("key: ".$keyfile." / csr: ".$csrfile." / config: ".$config);
283) 
284)   $c = fopen($config, "w");
285)   fwrite($c, "[req]
286) default_bits = {$bits}
287) default_keyfile = {$keyfile}
288) encrypt_key = no
289) distinguished_name      = req_distinguished_name
Bernd Wurst * Ermögliche SAN bei CSRs *...

Bernd Wurst authored 8 years ago

290) req_extensions = v3_req
bernd CSR-Erstellung

bernd authored 14 years ago

291) 
292) [ req_distinguished_name ]
293) countryName                     = Country Name (2 letter code)
Bernd Wurst Leere Vorgaben für CSR-Details

Bernd Wurst authored 10 years ago

294) countryName_default             = 
bernd CSR-Erstellung

bernd authored 14 years ago

295) stateOrProvinceName             = State or Province Name (full name)
Bernd Wurst Leere Vorgaben für CSR-Details

Bernd Wurst authored 10 years ago

296) stateOrProvinceName_default     = 
bernd CSR-Erstellung

bernd authored 14 years ago

297) localityName                    = Locality Name (eg, city)
Bernd Wurst Leere Vorgaben für CSR-Details

Bernd Wurst authored 10 years ago

298) localityName_default            = 
bernd CSR-Erstellung

bernd authored 14 years ago

299) 0.organizationName              = Organization Name (eg, company)
Bernd Wurst Leere Vorgaben für CSR-Details

Bernd Wurst authored 10 years ago

300) 0.organizationName_default      = 
bernd CSR-Erstellung

bernd authored 14 years ago

301) 
302) commonName = Common Name
303) commonName_default = {$cn}
Bernd Wurst * Ermögliche SAN bei CSRs *...

Bernd Wurst authored 8 years ago

304) {$SAN}
bernd CSR-Erstellung

bernd authored 14 years ago

305) ");
306)   fclose($c);
307) 
308)   $output = '';
hanno sha2 csrs

hanno authored 14 years ago

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

bernd authored 14 years ago

310)   $retval = 0;
311)   exec($cmdline, $output, $retval);
312)   DEBUG($output);
313)   DEBUG($retval);
314)   if ($retval != 0)
315)   {
bernd Bugfix: Beim Eintragen eine...

bernd authored 13 years ago

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

bernd authored 14 years ago

317)   }
318)   
319)   $csr = file_get_contents($csrfile);
320)   $key = file_get_contents($keyfile);
321) 
322)   unlink($csrfile);
323)   unlink($keyfile);
324)   unlink($config);
325) 
326)   return array($csr, $key);
327) }
328) 
329) 
330) 
Bernd Wurst Wildcard-CSRs können jetzt...

Bernd Wurst authored 11 years ago

331) function save_csr($cn, $bits, $replace=NULL)
bernd CSR-Erstellung

bernd authored 14 years ago

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

bernd authored 13 years ago

333)   if (! $cn) {
334)     system_failure("Sie müssen einen Domainname eingeben!");
335)   }
Bernd Wurst * Ermögliche SAN bei CSRs *...

Bernd Wurst authored 8 years ago

336)   $domains = split_cn($cn);
337)   $cn = $domains[0];
bernd CSR-Erstellung

bernd authored 14 years ago

338)   $csr = NULL;
339)   $key = NULL;
Bernd Wurst * Ermögliche SAN bei CSRs *...

Bernd Wurst authored 8 years ago

340)   list($csr, $key) = create_csr(implode(',',$domains), $bits);
bernd CSR-Erstellung

bernd authored 14 years ago

341)   
342)   $uid = (int) $_SESSION['userinfo']['uid'];
Bernd Wurst Einige Statements auf Prepa...

Bernd Wurst authored 10 years ago

343)   db_query("INSERT INTO vhosts.csr (uid, hostname, bits, `replace`, csr, `key`) VALUES (:uid, :cn, :bits, :replace, :csr, :key)",
Bernd Wurst * Ermögliche SAN bei CSRs *...

Bernd Wurst authored 8 years ago

344)            array(":uid" => $uid, ":cn" => $cn, ":bits" => $bits, 
Bernd Wurst Einige Statements auf Prepa...

Bernd Wurst authored 10 years ago

345)                  ":replace" => $replace, ":csr" => $csr, ":key" => $key));
346)   $id = db_insert_id();