d27efa24ff72380c6ce45966ff2ba3ab6a067e54
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) 
Bernd Wurst Interface für Zertifikate i...

Bernd Wurst authored 7 years ago

46) function user_has_manual_certs()
47) {
48)   foreach (user_certs() as $c) {
49)     if (!cert_is_letsencrypt($c['id'])) {
50)       return true;
51)     }
52)   }
53)   foreach (user_csr() as $c) {
54)     return true;
55)   }
56) }
57) 
58) 
bernd Neue Zertifikatsverwaltung

bernd authored 14 years ago

59) function cert_details($id)
60) {
61)   $id = (int) $id;
62)   $uid = (int) $_SESSION['userinfo']['uid'];
63)   
Bernd Wurst Status der letsencrypt-Opti...

Bernd Wurst authored 8 years ago

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

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

bernd authored 14 years ago

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

Bernd Wurst authored 10 years ago

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

bernd authored 14 years ago

68) }
69) 
Bernd Wurst Status der letsencrypt-Opti...

Bernd Wurst authored 8 years ago

70) function cert_is_letsencrypt($id)
71) {
72)   $details = cert_details($id);
Bernd Wurst Erkenne LE-Zertifikat, wenn...

Bernd Wurst authored 8 years ago

73)   DEBUG($details);
Bernd Wurst Speichere und zeige Serienn...

Bernd Wurst authored 8 years ago

74)   if (strpos($details['subject'], "Let's Encrypt autogenerated") > 0) {
Bernd Wurst Status der letsencrypt-Opti...

Bernd Wurst authored 8 years ago

75)     return true;
76)   }
77)   return false;
78) }
79) 
bernd Neue Zertifikatsverwaltung

bernd authored 14 years ago

80) 
bernd CSR-Erstellung

bernd authored 14 years ago

81) function csr_details($id)
82) {
83)   $id = (int) $id;
84)   $uid = (int) $_SESSION['userinfo']['uid'];
85)   
Bernd Wurst Einige Statements auf Prepa...

Bernd Wurst authored 10 years ago

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

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

bernd authored 14 years ago

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

Bernd Wurst authored 10 years ago

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

bernd authored 14 years ago

90) }
91) 
92) 
bernd Neue Zertifikatsverwaltung

bernd authored 14 years ago

93) function get_available_CAs()
94) {
95)   $path = '/etc/apache2/certs/cabundle/';
96)   $ret = glob($path.'*.pem');
97)   if (! $ret)
98)     system_failure("Konnte die CA-Zertifikate nicht laden");
99)   DEBUG($ret);
100)   return $ret;
101) }
102) 
103) 
bernd Cert-Chain erkennen und ben...

bernd authored 13 years ago

104) function get_chain($cert)
105) {
106)   $certdata = openssl_x509_parse($cert, true);
bernd Mehr Fehlerbehandlung für S...

bernd authored 13 years ago

107)   if ($certdata === FALSE) {
108)     system_failure("Das Zertifikat konnte nicht gelesen werden");
109)   }
110)   if (! isset($certdata['issuer']['CN'])) {
111)     return NULL;
112)   }
Bernd Wurst Einige Statements auf Prepa...

Bernd Wurst authored 10 years ago

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

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

bernd authored 13 years ago

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

Bernd Wurst authored 10 years ago

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

bernd authored 13 years ago

117)     //$chainfile = '/etc/apache2/certs/chains/'.$c['id'].'.pem';
118)     DEBUG("identified fitting certificate chain #".$c['id']);
119)     return $c['id'];
120)   }
121) }
122) 
123) 
bernd Neue Zertifikatsverwaltung

bernd authored 14 years ago

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

bernd authored 13 years ago

125) { 
126)   // Lade private key 
127)   $seckey = openssl_get_privatekey($key);
128)   if ($seckey === FALSE) {
129)     system_failure("Der private Schlüssel konnte (ohne Passwort) nicht gelesen werden.");
130)   }
131)   // Lade public key
132)   $pubkey = openssl_get_publickey($cert);
133)   if ($pubkey === FALSE) {
134)     system_failure("In dem eingetragenen Zertifikat wurde kein öffentlicher Schlüssel gefunden.");
135)   }
136)   // Parse Details über den pubkey
137)   $certinfo = openssl_pkey_get_details($pubkey);
bernd Erlaube nur RSA- und DSA-Ze...

bernd authored 13 years ago

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

bernd authored 13 years ago

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

bernd authored 13 years ago

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

bernd authored 13 years ago

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

bernd authored 13 years ago

145)     system_failure("Dieser Schlüssel nutzt einen nicht unterstützten Algorithmus.");
146)   }
147)     
bernd Mehr Fehlerbehandlung für S...

bernd authored 13 years ago

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

bernd authored 14 years ago

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

Bernd Wurst authored 10 years ago

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

bernd authored 14 years ago

157)     return CERT_INVALID;
158)   }
159) 
bernd Cert-Chain erkennen und ben...

bernd authored 13 years ago

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

Bernd Wurst authored 12 years ago

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

bernd authored 13 years ago

162)   if ($chain)
163)   {
Bernd Wurst Einige Statements auf Prepa...

Bernd Wurst authored 10 years ago

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

Bernd Wurst authored 10 years ago

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

Bernd Wurst authored 12 years ago

166)     $chaincert = $tmp['content'];
167)     $chainfile = tempnam(sys_get_temp_dir(), 'webinterface');
168)     $f = fopen($chainfile, "w");
169)     fwrite($f, $chaincert);
170)     fclose($f);
171)     $cacerts[] = $chainfile;
bernd Cert-Chain erkennen und ben...

bernd authored 13 years ago

172)   }
bernd Neue Zertifikatsverwaltung

bernd authored 14 years ago

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

Bernd Wurst authored 12 years ago

174)   $valid = openssl_x509_checkpurpose($cert, X509_PURPOSE_SSL_SERVER, $cacerts);
175)   if ($chain) {
176)     unlink($chainfile);
177)   }
178)   if ($valid !== true)
bernd Neue Zertifikatsverwaltung

bernd authored 14 years ago

179)   { 
180)     DEBUG('certificate was not validated as a server certificate with the available chain');
181)     return CERT_NOCHAIN;
182)   }
183) 
184)   return CERT_OK;
185) }
186) 
187) 
188) function parse_cert_details($cert)
189) {
190)   $certdata = openssl_x509_parse($cert, true);
191)   /* 
192) name => /CN=*.bwurst.org
193) validFrom_time_t => 1204118790
194) validTo_time_t => 1267190790
195) 
196) 
197)   */
bernd Erlaube nur RSA- und DSA-Ze...

bernd authored 13 years ago

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

bernd authored 14 years ago

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

200)   $issuer = $certdata['issuer']['CN'];
201)   if (isset($certdata['issuer']['O'])) {
202)     $issuer = $certdata['issuer']['O'];
203)   }
204)   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

205) }
206) 
207) 
208) function save_cert($info, $cert, $key)
209) {
hanno zertifikate und keys export...

hanno authored 14 years ago

210)   openssl_pkey_export($key, $key);
211)   openssl_x509_export($cert, $cert);
bernd Neue Zertifikatsverwaltung

bernd authored 14 years ago

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

Bernd Wurst authored 10 years ago

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

bernd authored 14 years ago

217) }
218) 
bernd Cert-Refresh

bernd authored 14 years ago

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

bernd authored 14 years ago

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

bernd authored 14 years ago

221) {
hanno zertifikate und keys export...

hanno authored 14 years ago

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

Bernd Wurst authored 10 years ago

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

bernd authored 13 years ago

224) 
bernd Cert-Refresh

bernd authored 14 years ago

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

Bernd Wurst authored 10 years ago

227)   $args = array(":subject" => filter_input_general($info['subject']),
228)                 ":cn" => filter_input_general($info['cn']),
229)                 ":cert" => $cert,
230)                 ":valid_from" => $info['valid_from'],
231)                 ":valid_until" => $info['valid_until'],
232)                 ":chain" => get_chain($cert),
233)                 ":id" => $id);
234) 
bernd neue Bilder, mehr SSL-Zerti...

bernd authored 14 years ago

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

hanno authored 14 years ago

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

Bernd Wurst authored 10 years ago

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

hanno authored 14 years ago

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

Bernd Wurst authored 10 years ago

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

242) }
243) 
244) 
bernd Neue Zertifikatsverwaltung

bernd authored 14 years ago

245) function delete_cert($id)
246) {
247)   $uid = (int) $_SESSION['userinfo']['uid'];
248)   $id = (int) $id;
249)   
Bernd Wurst Einige Statements auf Prepa...

Bernd Wurst authored 10 years ago

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

bernd authored 14 years ago

251) }
252) 
bernd CSR-Erstellung

bernd authored 14 years ago

253) function delete_csr($id)
254) {
255)   $uid = (int) $_SESSION['userinfo']['uid'];
256)   $id = (int) $id;
257)   
Bernd Wurst Einige Statements auf Prepa...

Bernd Wurst authored 10 years ago

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

bernd authored 14 years ago

259) }
260) 
261) 
Bernd Wurst * Ermögliche SAN bei CSRs *...

Bernd Wurst authored 8 years ago

262) function split_cn($cn)
263) {
264)   $domains = array();
265)   if (strstr($cn, ',') or strstr($cn, "\n")) {
266)     $domains = preg_split("/[, \n]+/", $cn);
267)     DEBUG("Domains:");
268)     DEBUG($domains);
269)   } else {
270)     $domains[] = $cn;
271)   }
272)   for ($i=0;$i!=count($domains);$i++) {
273)     $domains[$i] = filter_input_hostname($domains[$i], true);
274)   }
275)   return $domains;
276) }
277) 
bernd CSR-Erstellung

bernd authored 14 years ago

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

Bernd Wurst authored 8 years ago

280)   $domains = split_cn($cn);
281)   $tmp = array();
282)   foreach ($domains as $dom) {
283)     $tmp[] = 'DNS:'.$dom;
284)   }
285)   $SAN = "[ v3_req ]\nsubjectAltName = ".implode(', ', $tmp);
286)   DEBUG($SAN);
287)   $cn = $domains[0];
bernd CSR-Erstellung

bernd authored 14 years ago

288)   $bits = (int) $bits;
289)   if ($bits == 0)
290)     $bits = 4096;
291) 
292)   $keyfile = tempnam(ini_get('upload_tmp_dir'), 'key');
293)   $csrfile = tempnam(ini_get('upload_tmp_dir'), 'csr');
294)   $config = tempnam(ini_get('upload_tmp_dir'), 'config');
295) 
296)   DEBUG("key: ".$keyfile." / csr: ".$csrfile." / config: ".$config);
297) 
298)   $c = fopen($config, "w");
299)   fwrite($c, "[req]
300) default_bits = {$bits}
301) default_keyfile = {$keyfile}
302) encrypt_key = no
303) distinguished_name      = req_distinguished_name
Bernd Wurst * Ermögliche SAN bei CSRs *...

Bernd Wurst authored 8 years ago

304) req_extensions = v3_req
bernd CSR-Erstellung

bernd authored 14 years ago

305) 
306) [ req_distinguished_name ]
307) countryName                     = Country Name (2 letter code)
Bernd Wurst Leere Vorgaben für CSR-Details

Bernd Wurst authored 10 years ago

308) countryName_default             = 
bernd CSR-Erstellung

bernd authored 14 years ago

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

Bernd Wurst authored 10 years ago

310) stateOrProvinceName_default     = 
bernd CSR-Erstellung

bernd authored 14 years ago

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

Bernd Wurst authored 10 years ago

312) localityName_default            = 
bernd CSR-Erstellung

bernd authored 14 years ago

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

Bernd Wurst authored 10 years ago

314) 0.organizationName_default      = 
bernd CSR-Erstellung

bernd authored 14 years ago

315) 
316) commonName = Common Name
317) commonName_default = {$cn}
Bernd Wurst * Ermögliche SAN bei CSRs *...

Bernd Wurst authored 8 years ago

318) {$SAN}
bernd CSR-Erstellung

bernd authored 14 years ago

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) 
Bernd Wurst Wildcard-CSRs können jetzt...

Bernd Wurst authored 11 years ago

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

bernd authored 14 years ago

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

bernd authored 13 years ago

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

Bernd Wurst authored 8 years ago

350)   $domains = split_cn($cn);
351)   $cn = $domains[0];
bernd CSR-Erstellung

bernd authored 14 years ago

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

Bernd Wurst authored 8 years ago

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

bernd authored 14 years ago

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

Bernd Wurst authored 10 years ago

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

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

Bernd Wurst authored 10 years ago

359)                  ":replace" => $replace, ":csr" => $csr, ":key" => $key));
360)   $id = db_insert_id();