forked from Cacti/cacti
-
Notifications
You must be signed in to change notification settings - Fork 0
/
genkey.php
344 lines (295 loc) · 9.64 KB
/
genkey.php
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
#!/usr/bin/env php
<?php
/*
+-------------------------------------------------------------------------+
| Copyright (C) 2004-2024 The Cacti Group |
| |
| This program is free software; you can redistribute it and/or |
| modify it under the terms of the GNU General Public License |
| as published by the Free Software Foundation; either version 2 |
| of the License, or (at your option) any later version. |
| |
| This program is distributed in the hope that it will be useful, |
| but WITHOUT ANY WARRANTY; without even the implied warranty of |
| MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| GNU General Public License for more details. |
+-------------------------------------------------------------------------+
| Cacti: The Complete RRDTool-based Graphing Solution |
+-------------------------------------------------------------------------+
| This code is designed, written, and maintained by the Cacti Group. See |
| about.php and/or the AUTHORS file for specific developer information. |
+-------------------------------------------------------------------------+
| http://www.cacti.net/ |
+-------------------------------------------------------------------------+
*/
require(__DIR__ . '/../include/cli_check.php');
/* process calling arguments */
$parms = $_SERVER['argv'];
array_shift($parms);
if (cacti_sizeof($parms)) {
// Defaults
$keypair_path = CACTI_PATH_PKI;
$generate = false;
$replace = false;
$author = -1;
$homepage = -1;
$email = -1;
$privkey = '';
$pubkey = '';
// For certificate
$country = 'US';
$state = 'Zion';
$org = '.';
$unit = '.';
$days = 2048;
$shortopts = 'VvHh';
$longopts = array(
'privkey::',
'pubkey::',
'author::',
'homepage::',
'email::',
'country:',
'state:',
'org:',
'unit:',
'days:',
'replace',
'generate',
'version',
'help'
);
$options = getopt($shortopts, $longopts);
foreach($options as $arg => $value) {
switch($arg) {
case 'replace':
$replace = true;
break;
case 'generate':
$generate = true;
break;
case 'author':
$author = $value;
break;
case 'homepage':
$homepage = $value;
break;
case 'email':
$email = $value;
break;
case 'country':
$country = $value;
break;
case 'state':
$state = $value;
break;
case 'org':
$org = $value;
break;
case 'unit':
$unit = $value;
break;
case 'privkey':
$privkey = $value;
break;
case 'pubkey':
$pubkey = $value;
break;
case 'days':
$days = $value;
break;
case 'version':
case 'V':
case 'v':
display_version();
exit;
case 'help':
case 'H':
case 'h':
display_help();
exit(0);
default:
print "FATAL: Invalid Argument: ($arg)\n\n";
display_help();
exit(1);
}
}
}
if (!$replace && !$generate) {
display_help();
exit(0);
}
if ($author == -1) {
print "FATAL: The parameter --author is required." . PHP_EOL;
exit(1);
}
if ($homepage == -1) {
print "FATAL: The parameter --homepage is required." . PHP_EOL;
exit(1);
}
if ($email == -1) {
print "FATAL: The parameter --email is required." . PHP_EOL;
exit(1);
}
if (is_array($privkey)) {
print "FATAL: The private key can only be specified once." . PHP_EOL;
exit(1);
} elseif ($privkey != '' && !file_exists($privkey)) {
print "FATAL: Private key '$privkey' does not exist" . PHP_EOL;
exit(1);
}
if (is_array($pubkey)) {
print "FATAL: The public key can only be specified once" . PHP_EOL;
exit(1);
} elseif ($pubkey != '' && !file_exists($pubkey)) {
print "FATAL: Public key '$pubkey' does not exist" . PHP_EOL;
exit(1);
}
if (($pubkey != '' && $privkey == '') || ($privkey != '' && $pubkey == '')) {
print "FATAL: You must specify both public and private keys if you wish to use them" . PHP_EOL;
exit(1);
}
if ((file_exists($keypair_path . '/package.pem') || file_exists($keypair_path . '/package.info')) && !$replace) {
print "FATAL: Package private key or info file exists. You must use the --replace option to replace it" . PHP_EOL;
} else {
$package_info = "[info]" . PHP_EOL;
$package_info .= "author = $author" . PHP_EOL;
$package_info .= "homepage = $homepage" . PHP_EOL;
$package_info .= "email = $email" . PHP_EOL;
file_put_contents("$keypair_path/package.info", $package_info);
$config = array(
'digest_alg' => 'sha256',
'private_key_type' => OPENSSL_KEYTYPE_RSA
);
$subject = array(
'countryName' => $country,
'stateOrProvinceName' => $state,
'localityName' => $homepage,
'organizationName' => $org,
'organizationalUnitName' => $unit,
'commonName' => $author,
'emailAddress' => $email,
);
if ($privkey != '') {
print 'NOTE: Using user provided public/private key pair.' . PHP_EOL;
$provided = true;
$privKey = openssl_pkey_get_private("file://$privkey");
$pubKey = openssl_pkey_get_public("file://$pubkey");
if ($privKey === false) {
print "FATAL: Unable to open your private key $privkey" . PHP_EOL;
exit(0);
}
if ($pubKey === false) {
print "FATAL: Unable to open your public key $pubkey" . PHP_EOL;
exit(0);
}
} else {
print 'NOTE: Generating custom public/private key pair.' . PHP_EOL;
$provided = false;
$res = openssl_pkey_new($config);
if ($res !== false) {
print 'NOTE: Generated private key' . PHP_EOL;
// Generate a private key and certificate
if (openssl_pkey_export($res, $privKey)) {
$details = openssl_pkey_get_details($res);
if ($details !== false) {
print 'NOTE: Gathered key details from private key' . PHP_EOL;
$pubKey = $details['key'];
} else {
print 'FATAL: Unable to get key details.' . PHP_EOL;
exit(1);
}
} else {
print 'FATAL: Unable to export private key.' . PHP_EOL;
exit(1);
}
} else {
print 'FATAL: Unable to generate private key.' . PHP_EOL;
exit(1);
}
}
// Generate a certificate
//$csr = openssl_csr_new($subject, $privKey, array('digest_alg' => 'sha256'));
$csr = openssl_csr_new($subject, $privKey, $config);
if ($csr !== false) {
print 'NOTE: Gathered CSR records for certificate.' . PHP_EOL;
$x509 = openssl_csr_sign($csr, null, $privKey, $days, array('digest_alg' => 'sha256'));
if ($x509 !== false) {
print 'NOTE: Signed CSR record.' . PHP_EOL;
if (openssl_x509_export_to_file($x509, "$keypair_path/package.pem")) {
print 'NOTE: Generated certificate file package.pem.' . PHP_EOL;
} else {
print 'FATAL: Unable to generate certificate file package.pem.' . PHP_EOL;
exit(1);
}
} else {
print 'FATAL: Unable to Sign CSR record.' . PHP_EOL;
exit(1);
}
} else {
print 'FATAL: Unable to create CSR record.' . PHP_EOL;
exit(1);
}
// Output the private key to the package directory
if (!$provided) {
if (openssl_pkey_export_to_file($privKey, "$keypair_path/package.key")) {
print 'NOTE: Generated private key file package.key.' . PHP_EOL;
} else {
print 'FATAL: Unable to generate private key file package.key.' . PHP_EOL;
exit(1);
}
} else {
if ($privkey != "$keypair_path/package.key") {
if (file_put_contents("$keypair_path/package.key", $privKey)) {
print 'NOTE: Generated private key file package.key.' . PHP_EOL;
} else {
print 'FATAL: Unable to generate private key file package.key.' . PHP_EOL;
exit(1);
}
} else {
print 'NOTE: Provided private key is the existing packaging private key package.key.'. PHP_EOL;
}
}
// Output the public key to the package directory
if (!$provided) {
if (file_put_contents("$keypair_path/package.pub", $pubKey)) {
print 'NOTE: Generated public key file package.pub.' . PHP_EOL;
} else {
print 'FATAL: Unable to generate public key file package.pub.' . PHP_EOL;
exit(1);
}
}
print 'SUCCESS!!' . PHP_EOL;
}
/* display_version - displays version information */
function display_version() {
if (defined('CACTI_VERSION')) {
$version = CACTI_VERSION;
} else {
$version = get_cacti_version();
}
print "Cacti Package Genkey Tool, Version $version, " . COPYRIGHT_YEARS . "\n";
}
function display_help() {
display_version();
print "\nusage: genkey.php [options]\n\n";
print "This script generates a Package Authors Certificate information based upon a valid ssh key pair.\n";
print "If you do not have an existing ssh key pair, use the ssh-keygen command to create one before\n";
print "running this script. Otherwise, the script will create one for you.\n\n";
print "Options:\n";
print " --generate Generate a new key pair.\n";
print " --replace Replace the existing key pair.\n\n";
print "Required:\n";
print " --author Registered Author Name.\n";
print " --homepage Registered Authors Homepage.\n";
print " --email Registered Authors Email.\n\n";
print "Required For Certificate Generation:\n";
print " --country Registered Authors Country of the Package.\n";
print " --state Registered Authors State or Province.\n";
print " --org Registered Authors Organization.\n";
print " --unit Registered Authors Organizational Unit.\n";
print " --days The number of days for the certificate to remain valid.\n\n";
print "Optional:\n";
print " --privkey=path Path to an existing private key.\n";
print " --pubkey=path Path to an existing public key.\n";
}