-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgenerate.php
More file actions
186 lines (152 loc) · 6.93 KB
/
Copy pathgenerate.php
File metadata and controls
186 lines (152 loc) · 6.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
<?php
/**
* this File is part of xTLS - (c) 20225 xTLS
*
* NOTICE OF LICENSE
*
* EUROPEAN UNION PUBLIC LICENCE v. 1.2
* that is bundled with this package in the file LICENSE.md.
* It is also available through the world-wide-web at this URL:
* https://eupl.eu/
*
*
* @author Micro
* @copyright 2025 xTLS
* @link
* @see Internal Documentation ~/doc/
* @version 1.0.0
* @todo
*/
define('REAL_BASE_DIR', dirname(__FILE__));
require_once(REAL_BASE_DIR."/config/config.php");
//loadEnv();
require_once(REAL_BASE_DIR . "/create_san_config.php");
require_once(REAL_BASE_DIR . "/create_domain_config.php");
require_once(REAL_BASE_DIR."/functions.php");
HTML_HEAD($bgcolor);
?>
<h1>📜 Zertifikatsverwaltung</h1>
<?php
// Hilfsfunktion zur Domainvalidierung
function validateDomains(array $domains): array {
return array_filter($domains, function($domain) {
return preg_match('/^[a-z0-9.-]+$/i', $domain);
});
}
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
if ($_POST['mode'] === 'san') {
// SAN-Modus: mehrere Domains in EINEM Zertifikat
$domains = array_map('trim', explode(',', $_POST['domains']));
$domains = validateDomains($domains);
if (count($domains) < 1) {
echo "❌ Ungültige Domains für SAN-Zertifikat.";
exit;
}
$primaryDomain = $domains[0];
$domainDir = "$certsDir/$primaryDomain";
if (!is_dir($domainDir)) mkdir($domainDir, 0775, true);
$keyFile = "$domainDir/key.pem";
$csrFile = "$domainDir/csr.pem";
$certFile = "$domainDir/cert.pem";
$bundleFile = "$domainDir/bundle.crt";
$verifyFile = "$domainDir/verify.txt";
$confFile = "$domainDir/$primaryDomain.cnf";
$zipFile = "$domainDir/{$primaryDomain}_SAN.zip";
$fullchainFile = "$domainDir/fullchain.pem";
$postfixPem = "$domainDir/postfix.pem";
if (!file_exists($keyFile)) {
exec("openssl genrsa -out $keyFile 4096");
}
$conf = createOpenSSLConfigForSAN($opensslCnf, $domains);
copy($conf, $confFile);
exec("openssl req -new -key $keyFile -out $csrFile -config $conf -reqexts v3_req", $out, $ret);
if ($ret !== 0) {
echo "Fehler beim Erstellen der CSR für SAN-Zertifikat.";
exit;
}
exec("openssl ca -batch -config $confFile -in $csrFile -out $certFile -extensions v3_req -passin pass:$caPass", $out, $ret);
if ($ret !== 0) {
echo "Fehler beim Signieren des SAN-Zertifikats.";
exit;
}
file_put_contents($bundleFile, file_get_contents($certFile) . file_get_contents("$caDir/rootCA.pem"));
exec("openssl x509 -in $certFile -noout -text", $verifyOut);
file_put_contents($verifyFile, implode(PHP_EOL, $verifyOut));
// Mailserver sollen das lieben
file_put_contents($fullchainFile, file_get_contents($certFile) . file_get_contents("$caDir/rootCA.pem"));
file_put_contents($postfixPem, file_get_contents($keyFile) . file_get_contents($certFile) . file_get_contents("$caDir/rootCA.pem"));
$zip = new ZipArchive();
if ($zip->open($zipFile, ZipArchive::CREATE | ZipArchive::OVERWRITE)) {
foreach (['key.pem', 'csr.pem', 'cert.pem', 'bundle.crt', 'verify.txt', 'fullchain.pem', 'postfix.pem'] as $f) {
$zip->addFile("$domainDir/$f", $f);
}
$zusatzfile = $caDir.'/'.$ROOTCA;
$zip->addFile($zusatzfile, $ROOTCA);
$zip->close();
}
echo "✔ SAN-Zertifikat für Domains erstellt: <strong>" . implode(", ", $domains) . "</strong>.<br>";
echo "<a href='index.php'>Zurück</a>";
HTML_FOOT();
exit;
} elseif ($_POST['mode'] === 'single') {
// Einzelzertifikate-Modus: Für jede Domain eigenes Zertifikat
$domains = array_map('trim', explode(',', $_POST['domains']));
$domains = validateDomains($domains);
if (count($domains) < 1) {
echo "❌ Keine gültigen Domains angegeben.";
HTML_FOOT();
exit;
}
foreach ($domains as $domain) {
$domainDir = "$certsDir/$domain";
if (!is_dir($domainDir)) mkdir($domainDir, 0775, true);
$keyFile = "$domainDir/key.pem";
$csrFile = "$domainDir/csr.pem";
$certFile = "$domainDir/cert.pem";
$bundleFile = "$domainDir/bundle.crt";
$verifyFile = "$domainDir/verify.txt";
$confFile = "$domainDir/$domain.cnf";
$zipFile = "$domainDir/$domain.zip";
$fullchainFile = "$domainDir/fullchain.pem";
$postfixPem = "$domainDir/postfix.pem";
if (!file_exists($keyFile)) {
exec("openssl genrsa -out $keyFile 4096");
}
$conf = createOpenSSLConfigForDomain($opensslCnf, $domain);
copy($conf, $confFile);
exec("openssl req -new -key $keyFile -out $csrFile -config $conf -reqexts v3_req", $out, $ret);
if ($ret !== 0) {
echo "Fehler beim Erstellen der CSR für Domain: $domain<br>";
continue;
}
//exec("openssl ca -batch -config $confFile -in $csrFile -out $certFile -extensions v3_req -passin pass:$caPass", $out, $ret);
exec("openssl ca -batch -config $confFile -in $csrFile -out $certFile -extensions v3_req -passin pass:$caPass 2>&1", $out, $ret);
if ($ret !== 0) {
echo "Fehler beim Signieren des Zertifikats für Domain: $domain<br>";
echo "<pre>Fehler beim Signieren:\n" . htmlspecialchars(implode("\n", $out)) . "</pre>";
continue;
}
file_put_contents($bundleFile, file_get_contents($certFile) . file_get_contents("$caDir/rootCA.pem"));
exec("openssl x509 -in $certFile -noout -text", $verifyOut);
file_put_contents($verifyFile, implode(PHP_EOL, $verifyOut));
// Mailserver sollen das lieben
file_put_contents($fullchainFile, file_get_contents($certFile) . file_get_contents("$caDir/rootCA.pem"));
file_put_contents($postfixPem, file_get_contents($keyFile) . file_get_contents($certFile) . file_get_contents("$caDir/rootCA.pem"));
$zip = new ZipArchive();
if ($zip->open($zipFile, ZipArchive::CREATE | ZipArchive::OVERWRITE)) {
foreach (['key.pem', 'csr.pem', 'cert.pem', 'bundle.crt', 'verify.txt', 'fullchain.pem', 'postfix.pem'] as $f) {
$zip->addFile("$domainDir/$f", $f);
}
$zusatzfile = $caDir.'/'.$ROOTCA;
$zip->addFile($zusatzfile, $ROOTCA);
$zip->close();
}
echo "✔ Zertifikat für <strong>$domain</strong> erstellt.<br>";
}
echo "<br><a href='index.php'>Zurück</a>";
HTML_FOOT();
exit;
}
}
echo "<p>Keine Domains angegeben.</p>";
HTML_FOOT();