Project

General

Profile

Download (26.9 KB) Statistics
| Branch: | Tag: | Revision:
1
<?php
2
/*
3
 * system_camanager.php
4
 *
5
 * part of pfSense (https://www.pfsense.org)
6
 * Copyright (c) 2004-2013 BSD Perimeter
7
 * Copyright (c) 2013-2016 Electric Sheep Fencing
8
 * Copyright (c) 2014-2023 Rubicon Communications, LLC (Netgate)
9
 * Copyright (c) 2008 Shrew Soft Inc
10
 * All rights reserved.
11
 *
12
 * Licensed under the Apache License, Version 2.0 (the "License");
13
 * you may not use this file except in compliance with the License.
14
 * You may obtain a copy of the License at
15
 *
16
 * http://www.apache.org/licenses/LICENSE-2.0
17
 *
18
 * Unless required by applicable law or agreed to in writing, software
19
 * distributed under the License is distributed on an "AS IS" BASIS,
20
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
21
 * See the License for the specific language governing permissions and
22
 * limitations under the License.
23
 */
24

    
25
##|+PRIV
26
##|*IDENT=page-system-camanager
27
##|*NAME=System: CA Manager
28
##|*DESCR=Allow access to the 'System: CA Manager' page.
29
##|*MATCH=system_camanager.php*
30
##|-PRIV
31

    
32
require_once("guiconfig.inc");
33
require_once("certs.inc");
34
require_once("pfsense-utils.inc");
35

    
36
$ca_methods = array(
37
	"internal" => gettext("Create an internal Certificate Authority"),
38
	"existing" => gettext("Import an existing Certificate Authority"),
39
	"intermediate" => gettext("Create an intermediate Certificate Authority"));
40

    
41
$ca_keylens = array("1024", "2048", "3072", "4096", "6144", "7680", "8192", "15360", "16384");
42
$ca_keytypes = array("RSA", "ECDSA");
43
global $openssl_digest_algs;
44
global $cert_strict_values;
45
$max_lifetime = cert_get_max_lifetime();
46
$default_lifetime = min(3650, $max_lifetime);
47
$openssl_ecnames = cert_build_curve_list();
48
$class = "success";
49

    
50
init_config_arr(array('ca'));
51
$a_ca = &$config['ca'];
52

    
53
init_config_arr(array('cert'));
54
$a_cert = &$config['cert'];
55

    
56
init_config_arr(array('crl'));
57
$a_crl = &$config['crl'];
58

    
59
$act = $_REQUEST['act'];
60

    
61
if (isset($_REQUEST['id']) && ctype_alnum($_REQUEST['id'])) {
62
	$id = $_REQUEST['id'];
63
}
64
if (!empty($id)) {
65
	$thisca =& lookup_ca($id);
66
}
67

    
68
/* Actions other than 'new' require an ID.
69
 * 'del' action must be submitted via POST. */
70
if ((!empty($act) &&
71
    ($act != 'new') &&
72
    !$thisca) ||
73
    (($act == 'del') && empty($_POST))) {
74
	pfSenseHeader("system_camanager.php");
75
	exit;
76
}
77

    
78
switch ($act) {
79
	case 'del':
80
		$name = htmlspecialchars($thisca['descr']);
81
		if (cert_in_use($id)) {
82
			$savemsg = sprintf(gettext("Certificate %s is in use and cannot be deleted"), $name);
83
			$class = "danger";
84
		} else {
85
			/* Only remove CA reference when deleting. It can be reconnected if a new matching CA is imported */
86
			foreach ($a_cert as $cid => $acrt) {
87
				if ($acrt['caref'] == $thisca['refid']) {
88
					unset($a_cert[$cid]['caref']);
89
				}
90
			}
91
			/* Remove any CRLs for this CA, there is no way to recover the connection once the CA has been removed. */
92
			foreach ($a_crl as $cid => $acrl) {
93
				if ($acrl['caref'] == $thisca['refid']) {
94
					unset($a_crl[$cid]);
95
				}
96
			}
97
			/* Delete the CA */
98
			foreach ($a_ca as $cid => $aca) {
99
				if ($aca['refid'] == $thisca['refid']) {
100
					unset($a_ca[$cid]);
101
				}
102
			}
103
			$savemsg = sprintf(gettext("Deleted Certificate Authority %s and associated CRLs"), htmlspecialchars($name));
104
			write_config($savemsg);
105
			ca_setup_trust_store();
106
		}
107
		unset($act);
108
		break;
109
	case 'edit':
110
		/* Editing an existing CA, so populate values. */
111
		$pconfig['method'] = 'existing';
112
		$pconfig['descr']  = $thisca['descr'];
113
		$pconfig['refid']  = $thisca['refid'];
114
		$pconfig['cert']   = base64_decode($thisca['crt']);
115
		$pconfig['serial'] = $thisca['serial'];
116
		$pconfig['trust']  = ($thisca['trust'] == 'enabled');
117
		$pconfig['randomserial']  = ($thisca['randomserial'] == 'enabled');
118
		if (!empty($thisca['prv'])) {
119
			$pconfig['key'] = base64_decode($thisca['prv']);
120
		}
121
		break;
122
	case 'new':
123
		/* New CA, so set default values */
124
		$pconfig['method'] = $_POST['method'];
125
		$pconfig['keytype'] = "RSA";
126
		$pconfig['keylen'] = "2048";
127
		$pconfig['ecname'] = "prime256v1";
128
		$pconfig['digest_alg'] = "sha256";
129
		$pconfig['lifetime'] = $default_lifetime;
130
		$pconfig['dn_commonname'] = "internal-ca";
131
		break;
132
	case 'exp':
133
		/* Exporting a ca */
134
		send_user_download('data', base64_decode($thisca['crt']), "{$thisca['descr']}.crt");
135
		break;
136
	case 'expkey':
137
		/* Exporting a private key */
138
		send_user_download('data', base64_decode($thisca['prv']), "{$thisca['descr']}.key");
139
		break;
140
	default:
141
		break;
142
}
143

    
144
if ($_POST['save']) {
145
	unset($input_errors);
146
	$input_errors = array();
147
	$pconfig = $_POST;
148

    
149
	/* input validation */
150
	switch ($pconfig['method']) {
151
		case 'existing':
152
			$reqdfields = explode(" ", "descr cert");
153
			$reqdfieldsn = array(
154
				gettext("Descriptive name"),
155
				gettext("Certificate data"));
156
			/* Make sure we do not have invalid characters in the fields for the certificate */
157
			if (preg_match("/[\?\>\<\&\/\\\"\']/", $_POST['descr'])) {
158
				array_push($input_errors, gettext("The field 'Descriptive Name' contains invalid characters."));
159
			}
160
			if ($_POST['cert'] && (!strstr($_POST['cert'], "BEGIN CERTIFICATE") || !strstr($_POST['cert'], "END CERTIFICATE"))) {
161
				$input_errors[] = gettext("This certificate does not appear to be valid.");
162
			}
163
			if ($_POST['key'] && strstr($_POST['key'], "ENCRYPTED")) {
164
				$input_errors[] = gettext("Encrypted private keys are not yet supported.");
165
			}
166
			if (!$input_errors && !empty($_POST['key']) && cert_get_publickey($_POST['cert'], false) != cert_get_publickey($_POST['key'], false, 'prv')) {
167
				$input_errors[] = gettext("The submitted private key does not match the submitted certificate data.");
168
			}
169
			/* we must ensure the certificate is capable of acting as a CA
170
			 * https://redmine.pfsense.org/issues/7885
171
			 */
172
			if (!$input_errors) {
173
				$purpose = cert_get_purpose($_POST['cert'], false);
174
				if ($purpose['ca'] != 'Yes') {
175
					$input_errors[] = gettext("The submitted certificate does not appear to be a Certificate Authority, import it on the Certificates tab instead.");
176
				}
177
			}
178
			break;
179
		case 'internal':
180
			$reqdfields = explode(" ",
181
				"descr keylen ecname keytype lifetime dn_commonname");
182
			$reqdfieldsn = array(
183
				gettext("Descriptive name"),
184
				gettext("Key length"),
185
				gettext("Elliptic Curve Name"),
186
				gettext("Key type"),
187
				gettext("Lifetime"),
188
				gettext("Common Name"));
189
			break;
190
		case 'intermediate':
191
			$reqdfields = explode(" ",
192
				"descr caref keylen ecname keytype lifetime dn_commonname");
193
			$reqdfieldsn = array(
194
				gettext("Descriptive name"),
195
				gettext("Signing Certificate Authority"),
196
				gettext("Key length"),
197
				gettext("Elliptic Curve Name"),
198
				gettext("Key type"),
199
				gettext("Lifetime"),
200
				gettext("Common Name"));
201
			break;
202
		default:
203
			break;
204
	}
205

    
206
	do_input_validation($_POST, $reqdfields, $reqdfieldsn, $input_errors);
207
	if ($pconfig['method'] != "existing") {
208
		/* Make sure we do not have invalid characters in the fields for the certificate */
209
		if (preg_match("/[\?\>\<\&\/\\\"\']/", $_POST['descr'])) {
210
			array_push($input_errors, gettext("The field 'Descriptive Name' contains invalid characters."));
211
		}
212
		$pattern = '/[^a-zA-Z0-9\ \'\/~`\!@#\$%\^&\*\(\)_\-\+=\{\}\[\]\|;:"\<\>,\.\?\\\]/';
213
		if (!empty($_POST['dn_commonname']) && preg_match($pattern, $_POST['dn_commonname'])) {
214
			$input_errors[] = gettext("The field 'Common Name' contains invalid characters.");
215
		}
216
		if (!empty($_POST['dn_state']) && preg_match($pattern, $_POST['dn_state'])) {
217
			$input_errors[] = gettext("The field 'State or Province' contains invalid characters.");
218
		}
219
		if (!empty($_POST['dn_city']) && preg_match($pattern, $_POST['dn_city'])) {
220
			$input_errors[] = gettext("The field 'City' contains invalid characters.");
221
		}
222
		if (!empty($_POST['dn_organization']) && preg_match($pattern, $_POST['dn_organization'])) {
223
			$input_errors[] = gettext("The field 'Organization' contains invalid characters.");
224
		}
225
		if (!empty($_POST['dn_organizationalunit']) && preg_match($pattern, $_POST['dn_organizationalunit'])) {
226
			$input_errors[] = gettext("The field 'Organizational Unit' contains invalid characters.");
227
		}
228
		if (!in_array($_POST["keytype"], $ca_keytypes)) {
229
			array_push($input_errors, gettext("Please select a valid Key Type."));
230
		}
231
		if (!in_array($_POST["keylen"], $ca_keylens)) {
232
			array_push($input_errors, gettext("Please select a valid Key Length."));
233
		}
234
		if (!in_array($_POST["ecname"], array_keys($openssl_ecnames))) {
235
			array_push($input_errors, gettext("Please select a valid Elliptic Curve Name."));
236
		}
237
		if (!in_array($_POST["digest_alg"], $openssl_digest_algs)) {
238
			array_push($input_errors, gettext("Please select a valid Digest Algorithm."));
239
		}
240
		if ($_POST['lifetime'] > $max_lifetime) {
241
			$input_errors[] = gettext("Lifetime is longer than the maximum allowed value. Use a shorter lifetime.");
242
		}
243
	}
244

    
245
	if (!empty($_POST['serial']) && !cert_validate_serial($_POST['serial'])) {
246
		$input_errors[] = gettext("Please enter a valid integer serial number.");
247
	}
248

    
249
	/* save modifications */
250
	if (!$input_errors) {
251
		$ca = array();
252
		if (!isset($pconfig['refid']) || empty($pconfig['refid'])) {
253
			$ca['refid'] = uniqid();
254
		} else {
255
			$ca['refid'] = $pconfig['refid'];
256
		}
257

    
258
		if (isset($id) && $thisca) {
259
			$ca = $thisca;
260
		}
261

    
262
		$ca['descr'] = $pconfig['descr'];
263
		$ca['trust'] = ($pconfig['trust'] == 'yes') ? "enabled" : "disabled";
264
		$ca['randomserial'] = ($pconfig['randomserial'] == 'yes') ? "enabled" : "disabled";
265

    
266
		if ($act == "edit") {
267
			$ca['descr']  = $pconfig['descr'];
268
			$ca['refid']  = $pconfig['refid'];
269
			$ca['serial'] = $pconfig['serial'];
270
			$ca['crt'] = base64_encode($pconfig['cert']);
271
			$ca['prv'] = base64_encode($pconfig['key']);
272
			$savemsg = sprintf(gettext("Updated Certificate Authority %s"), $ca['descr']);
273
		} else {
274
			$old_err_level = error_reporting(0); /* otherwise openssl_ functions throw warnings directly to a page screwing menu tab */
275
			if ($pconfig['method'] == "existing") {
276
				ca_import($ca, $pconfig['cert'], $pconfig['key'], $pconfig['serial']);
277
				$savemsg = sprintf(gettext("Imported Certificate Authority %s"), $ca['descr']);
278
			} else if ($pconfig['method'] == "internal") {
279
				$dn = array('commonName' => $pconfig['dn_commonname']);
280
				if (!empty($pconfig['dn_country'])) {
281
					$dn['countryName'] = $pconfig['dn_country'];
282
				}
283
				if (!empty($pconfig['dn_state'])) {
284
					$dn['stateOrProvinceName'] = $pconfig['dn_state'];
285
				}
286
				if (!empty($pconfig['dn_city'])) {
287
					$dn['localityName'] = $pconfig['dn_city'];
288
				}
289
				if (!empty($pconfig['dn_organization'])) {
290
					$dn['organizationName'] = $pconfig['dn_organization'];
291
				}
292
				if (!empty($pconfig['dn_organizationalunit'])) {
293
					$dn['organizationalUnitName'] = $pconfig['dn_organizationalunit'];
294
				}
295
				if (!ca_create($ca, $pconfig['keylen'], $pconfig['lifetime'], $dn, $pconfig['digest_alg'], $pconfig['keytype'], $pconfig['ecname'])) {
296
					$input_errors = array();
297
					while ($ssl_err = openssl_error_string()) {
298
						if (strpos($ssl_err, 'NCONF_get_string:no value') === false) {
299
							array_push($input_errors, "openssl library returns: " . $ssl_err);
300
						}
301
					}
302
				}
303
				$savemsg = sprintf(gettext("Created internal Certificate Authority %s"), $ca['descr']);
304
			} else if ($pconfig['method'] == "intermediate") {
305
				$dn = array('commonName' => $pconfig['dn_commonname']);
306
				if (!empty($pconfig['dn_country'])) {
307
					$dn['countryName'] = $pconfig['dn_country'];
308
				}
309
				if (!empty($pconfig['dn_state'])) {
310
					$dn['stateOrProvinceName'] = $pconfig['dn_state'];
311
				}
312
				if (!empty($pconfig['dn_city'])) {
313
					$dn['localityName'] = $pconfig['dn_city'];
314
				}
315
				if (!empty($pconfig['dn_organization'])) {
316
					$dn['organizationName'] = $pconfig['dn_organization'];
317
				}
318
				if (!empty($pconfig['dn_organizationalunit'])) {
319
					$dn['organizationalUnitName'] = $pconfig['dn_organizationalunit'];
320
				}
321
				if (!ca_inter_create($ca, $pconfig['keylen'], $pconfig['lifetime'], $dn, $pconfig['caref'], $pconfig['digest_alg'], $pconfig['keytype'], $pconfig['ecname'])) {
322
					$input_errors = array();
323
					while ($ssl_err = openssl_error_string()) {
324
						if (strpos($ssl_err, 'NCONF_get_string:no value') === false) {
325
							array_push($input_errors, "openssl library returns: " . $ssl_err);
326
						}
327
					}
328
				}
329
				$savemsg = sprintf(gettext("Created internal intermediate Certificate Authority %s"), $ca['descr']);
330
			}
331
			error_reporting($old_err_level);
332
		}
333

    
334
		if (isset($id) && $thisca) {
335
			$thisca = $ca;
336
		} else {
337
			$a_ca[] = $ca;
338
		}
339

    
340
		if (!$input_errors) {
341
			write_config($savemsg);
342
			ca_setup_trust_store();
343
			pfSenseHeader("system_camanager.php");
344
		}
345
	}
346
}
347

    
348
$pgtitle = array(gettext('System'), gettext('Certificate'), gettext('Authorities'));
349
$pglinks = array("", "system_camanager.php", "system_camanager.php");
350

    
351
if ($act == "new" || $act == "edit" || $act == gettext("Save") || $input_errors) {
352
	$pgtitle[] = gettext('Edit');
353
	$pglinks[] = "@self";
354
}
355
include("head.inc");
356

    
357
if ($input_errors) {
358
	print_input_errors($input_errors);
359
}
360

    
361
if ($savemsg) {
362
	print_info_box($savemsg, $class);
363
}
364

    
365
$tab_array = array();
366
$tab_array[] = array(gettext('Authorities'), true, 'system_camanager.php');
367
$tab_array[] = array(gettext('Certificates'), false, 'system_certmanager.php');
368
$tab_array[] = array(gettext('Revocation'), false, 'system_crlmanager.php');
369
display_top_tabs($tab_array);
370

    
371
if (!($act == "new" || $act == "edit" || $act == gettext("Save") || $input_errors)) {
372
?>
373
<div class="panel panel-default" id="search-panel">
374
	<div class="panel-heading">
375
		<h2 class="panel-title">
376
			<?=gettext('Search')?>
377
			<span class="widget-heading-icon pull-right">
378
				<a data-toggle="collapse" href="#search-panel_panel-body">
379
					<i class="fa-solid fa-plus-circle"></i>
380
				</a>
381
			</span>
382
		</h2>
383
	</div>
384
	<div id="search-panel_panel-body" class="panel-body collapse in">
385
		<div class="form-group">
386
			<label class="col-sm-2 control-label">
387
				<?=gettext("Search term")?>
388
			</label>
389
			<div class="col-sm-5"><input class="form-control" name="searchstr" id="searchstr" type="text"/></div>
390
			<div class="col-sm-2">
391
				<select id="where" class="form-control">
392
					<option value="0"><?=gettext("Name")?></option>
393
					<option value="1"><?=gettext("Distinguished Name")?></option>
394
					<option value="2" selected><?=gettext("Both")?></option>
395
				</select>
396
			</div>
397
			<div class="col-sm-3">
398
				<a id="btnsearch" title="<?=gettext("Search")?>" class="btn btn-primary btn-sm"><i class="fa-solid fa-search icon-embed-btn"></i><?=gettext("Search")?></a>
399
				<a id="btnclear" title="<?=gettext("Clear")?>" class="btn btn-info btn-sm"><i class="fa-solid fa-undo icon-embed-btn"></i><?=gettext("Clear")?></a>
400
			</div>
401
			<div class="col-sm-10 col-sm-offset-2">
402
				<span class="help-block"><?=gettext('Enter a search string or *nix regular expression to search certificate names and distinguished names.')?></span>
403
			</div>
404
		</div>
405
	</div>
406
</div>
407

    
408
<div class="panel panel-default">
409
	<div class="panel-heading"><h2 class="panel-title"><?=gettext('Certificate Authorities')?></h2></div>
410
	<div class="panel-body">
411
		<div class="table-responsive">
412
		<table id="catable" class="table table-striped table-hover table-rowdblclickedit sortable-theme-bootstrap" data-sortable>
413
			<thead>
414
				<tr>
415
					<th><?=gettext("Name")?></th>
416
					<th><?=gettext("Internal")?></th>
417
					<th><?=gettext("Issuer")?></th>
418
					<th><?=gettext("Certificates")?></th>
419
					<th><?=gettext("Distinguished Name")?></th>
420
					<th><?=gettext("In Use")?></th>
421
					<th><?=gettext("Actions")?></th>
422
				</tr>
423
			</thead>
424
			<tbody>
425
<?php
426
$pluginparams = array();
427
$pluginparams['type'] = 'certificates';
428
$pluginparams['event'] = 'used_ca';
429
$certificates_used_by_packages = pkg_call_plugins('plugin_certificates', $pluginparams);
430

    
431
foreach ($a_ca as $ca):
432
	$name = htmlspecialchars($ca['descr']);
433
	$subj = cert_get_subject($ca['crt']);
434
	$issuer = cert_get_issuer($ca['crt']);
435
	if ($subj == $issuer) {
436
		$issuer_name = gettext("self-signed");
437
	} else {
438
		$issuer_name = gettext("external");
439
	}
440
	$subj = htmlspecialchars(cert_escape_x509_chars($subj, true));
441
	$issuer = htmlspecialchars($issuer);
442
	$certcount = 0;
443

    
444
	$issuer_ca = lookup_ca($ca['caref']);
445
	if ($issuer_ca) {
446
		$issuer_name = htmlspecialchars($issuer_ca['descr']);
447
	}
448

    
449
	foreach ($a_cert as $cert) {
450
		if ($cert['caref'] == $ca['refid']) {
451
			$certcount++;
452
		}
453
	}
454

    
455
	foreach ($a_ca as $cert) {
456
		if ($cert['caref'] == $ca['refid']) {
457
			$certcount++;
458
		}
459
	}
460
?>
461
				<tr>
462
					<td><?=$name?></td>
463
					<td><i class="<?= (!empty($ca['prv'])) ? "fa-solid fa-check" : "fa-solid fa-times" ; ?>"></i></td>
464
					<td><i><?=$issuer_name?></i></td>
465
					<td><?=$certcount?></td>
466
					<td>
467
						<?=$subj?>
468
						<?php cert_print_infoblock($ca); ?>
469
						<?php cert_print_dates($ca);?>
470
					</td>
471
					<td class="text-nowrap">
472
						<?php if (is_openvpn_server_ca($ca['refid'])): ?>
473
							<?=gettext("OpenVPN Server")?><br/>
474
						<?php endif?>
475
						<?php if (is_openvpn_client_ca($ca['refid'])): ?>
476
							<?=gettext("OpenVPN Client")?><br/>
477
						<?php endif?>
478
						<?php if (is_ipsec_peer_ca($ca['refid'])): ?>
479
							<?=gettext("IPsec Tunnel")?><br/>
480
						<?php endif?>
481
						<?php if (is_ldap_peer_ca($ca['refid'])): ?>
482
							<?=gettext("LDAP Server")?>
483
						<?php endif?>
484
						<?php echo cert_usedby_description($ca['refid'], $certificates_used_by_packages); ?>
485
					</td>
486
					<td class="text-nowrap">
487
						<a class="fa-solid fa-pencil"	title="<?=gettext("Edit CA")?>"	href="system_camanager.php?act=edit&amp;id=<?=$ca['refid']?>"></a>
488
						<a class="fa-solid fa-certificate"	title="<?=gettext("Export CA")?>"	href="system_camanager.php?act=exp&amp;id=<?=$ca['refid']?>"></a>
489
					<?php if ($ca['prv']): ?>
490
						<a class="fa-solid fa-key"	title="<?=gettext("Export key")?>"	href="system_camanager.php?act=expkey&amp;id=<?=$ca['refid']?>"></a>
491
					<?php endif?>
492
					<?php if (is_cert_locally_renewable($ca)): ?>
493
						<a href="system_certmanager_renew.php?type=ca&amp;refid=<?=$ca['refid']?>" class="fa-solid fa-arrow-rotate-right" title="<?=gettext("Reissue/Renew")?>"></a>
494
					<?php endif ?>
495
					<?php if (!ca_in_use($ca['refid'])): ?>
496
						<a class="fa-solid fa-trash-can" 	title="<?=gettext("Delete CA and its CRLs")?>"	href="system_camanager.php?act=del&amp;id=<?=$ca['refid']?>" usepost ></a>
497
					<?php endif?>
498
					</td>
499
				</tr>
500
<?php endforeach; ?>
501
			</tbody>
502
		</table>
503
		</div>
504
	</div>
505
</div>
506

    
507
<nav class="action-buttons">
508
	<a href="?act=new" class="btn btn-success btn-sm">
509
		<i class="fa-solid fa-plus icon-embed-btn"></i>
510
		<?=gettext("Add")?>
511
	</a>
512
</nav>
513
<script type="text/javascript">
514
//<![CDATA[
515

    
516
events.push(function() {
517

    
518
	// Make these controls plain buttons
519
	$("#btnsearch").prop('type', 'button');
520
	$("#btnclear").prop('type', 'button');
521

    
522
	// Search for a term in the entry name and/or dn
523
	$("#btnsearch").click(function() {
524
		var searchstr = $('#searchstr').val().toLowerCase();
525
		var table = $("table tbody");
526
		var where = $('#where').val();
527

    
528
		table.find('tr').each(function (i) {
529
			var $tds = $(this).find('td'),
530
				shortname = $tds.eq(0).text().trim().toLowerCase(),
531
				dn = $tds.eq(4).text().trim().toLowerCase();
532

    
533
			regexp = new RegExp(searchstr);
534
			if (searchstr.length > 0) {
535
				if (!(regexp.test(shortname) && (where != 1)) && !(regexp.test(dn) && (where != 0))) {
536
					$(this).hide();
537
				} else {
538
					$(this).show();
539
				}
540
			} else {
541
				$(this).show();	// A blank search string shows all
542
			}
543
		});
544
	});
545

    
546
	// Clear the search term and unhide all rows (that were hidden during a previous search)
547
	$("#btnclear").click(function() {
548
		var table = $("table tbody");
549

    
550
		$('#searchstr').val("");
551

    
552
		table.find('tr').each(function (i) {
553
			$(this).show();
554
		});
555
	});
556

    
557
	// Hitting the enter key will do the same as clicking the search button
558
	$("#searchstr").on("keyup", function (event) {
559
		if (event.keyCode == 13) {
560
			$("#btnsearch").get(0).click();
561
		}
562
	});
563
});
564
//]]>
565
</script>
566

    
567
<?php
568
	include("foot.inc");
569
	exit;
570
}
571

    
572
$form = new Form;
573
//$form->setAction('system_camanager.php?act=edit');
574
if (isset($id) && $thisca) {
575
	$form->addGlobal(new Form_Input(
576
		'id',
577
		null,
578
		'hidden',
579
		$id
580
	));
581
}
582

    
583
if ($act == "edit") {
584
	$form->addGlobal(new Form_Input(
585
		'refid',
586
		null,
587
		'hidden',
588
		$pconfig['refid']
589
	));
590
}
591

    
592
$section = new Form_Section('Create / Edit CA');
593

    
594
$section->addInput(new Form_Input(
595
	'descr',
596
	'*Descriptive name',
597
	'text',
598
	$pconfig['descr']
599
))->setHelp('The name of this entry as displayed in the GUI for reference.%s' .
600
		'This name can contain spaces but it cannot contain any of the ' .
601
		'following characters: %s', '<br/>', "?, >, <, &, /, \, \", '");
602

    
603
if (!isset($id) || $act == "edit") {
604
	$section->addInput(new Form_Select(
605
		'method',
606
		'*Method',
607
		$pconfig['method'],
608
		$ca_methods
609
	))->toggles();
610
}
611

    
612
$section->addInput(new Form_Checkbox(
613
	'trust',
614
	'Trust Store',
615
	'Add this Certificate Authority to the Operating System Trust Store',
616
	$pconfig['trust']
617
))->setHelp('When enabled, the contents of the CA will be added to the trust ' .
618
	'store so that they will be trusted by the operating system.');
619

    
620
$section->addInput(new Form_Checkbox(
621
	'randomserial',
622
	'Randomize Serial',
623
	'Use random serial numbers when signing certificates',
624
	$pconfig['randomserial']
625
))->setHelp('When enabled, if this CA is capable of signing certificates then ' .
626
		'serial numbers for certificates signed by this CA will be ' .
627
		'automatically randomized and checked for uniqueness instead of ' .
628
		'using the sequential value from Next Certificate Serial.');
629

    
630
$form->add($section);
631

    
632
$section = new Form_Section('Existing Certificate Authority');
633
$section->addClass('toggle-existing collapse');
634

    
635
$section->addInput(new Form_Textarea(
636
	'cert',
637
	'*Certificate data',
638
	$pconfig['cert']
639
))->setHelp('Paste a certificate in X.509 PEM format here.');
640

    
641
$section->addInput(new Form_Textarea(
642
	'key',
643
	'Certificate Private Key (optional)',
644
	$pconfig['key']
645
))->setHelp('Paste the private key for the above certificate here. This is '.
646
	'optional in most cases, but is required when generating a '.
647
	'Certificate Revocation List (CRL).');
648

    
649
$section->addInput(new Form_Input(
650
	'serial',
651
	'Next Certificate Serial',
652
	'number',
653
	$pconfig['serial']
654
))->setHelp('Enter a decimal number to be used as a sequential serial number for ' .
655
	'the next certificate to be signed by this CA. This value is ignored ' .
656
	'when Randomize Serial is checked.');
657

    
658
$form->add($section);
659

    
660
$section = new Form_Section('Internal Certificate Authority');
661
$section->addClass('toggle-internal', 'toggle-intermediate', 'collapse');
662

    
663
$allCas = array();
664
foreach ($a_ca as $ca) {
665
	if (!$ca['prv']) {
666
			continue;
667
	}
668

    
669
	$allCas[ $ca['refid'] ] = $ca['descr'];
670
}
671

    
672
$group = new Form_Group('*Signing Certificate Authority');
673
$group->addClass('toggle-intermediate', 'collapse');
674
$group->add(new Form_Select(
675
	'caref',
676
	null,
677
	$pconfig['caref'],
678
	$allCas
679
));
680
$section->add($group);
681

    
682
$section->addInput(new Form_Select(
683
	'keytype',
684
	'*Key type',
685
	$pconfig['keytype'],
686
	array_combine($ca_keytypes, $ca_keytypes)
687
));
688

    
689
$group = new Form_Group($i == 0 ? '*Key length':'');
690
$group->addClass('rsakeys');
691
$group->add(new Form_Select(
692
	'keylen',
693
	null,
694
	$pconfig['keylen'],
695
	array_combine($ca_keylens, $ca_keylens)
696
))->setHelp('The length to use when generating a new RSA key, in bits. %1$s' .
697
	'The Key Length should not be lower than 2048 or some platforms ' .
698
	'may consider the certificate invalid.', '<br/>');
699
$section->add($group);
700

    
701
$group = new Form_Group($i == 0 ? '*Elliptic Curve Name':'');
702
$group->addClass('ecnames');
703
$group->add(new Form_Select(
704
	'ecname',
705
	null,
706
	$pconfig['ecname'],
707
	$openssl_ecnames
708
))->setHelp('Curves may not be compatible with all uses. Known compatible curve uses are denoted in brackets.');
709
$section->add($group);
710

    
711
$section->addInput(new Form_Select(
712
	'digest_alg',
713
	'*Digest Algorithm',
714
	$pconfig['digest_alg'],
715
	array_combine($openssl_digest_algs, $openssl_digest_algs)
716
))->setHelp('The digest method used when the CA is signed. %1$s' .
717
	'The best practice is to use SHA256 or higher. '.
718
	'Some services and platforms, such as the GUI web server and OpenVPN, consider weaker digest algorithms invalid.', '<br/>');
719

    
720
$section->addInput(new Form_Input(
721
	'lifetime',
722
	'*Lifetime (days)',
723
	'number',
724
	$pconfig['lifetime'],
725
	['max' => $max_lifetime]
726
));
727

    
728
$section->addInput(new Form_Input(
729
	'dn_commonname',
730
	'*Common Name',
731
	'text',
732
	$pconfig['dn_commonname'],
733
	['placeholder' => 'e.g. internal-ca']
734
));
735

    
736
$section->addInput(new Form_StaticText(
737
	null,
738
	gettext('The following certificate authority subject components are optional and may be left blank.')
739
));
740

    
741
$section->addInput(new Form_Select(
742
	'dn_country',
743
	'Country Code',
744
	$pconfig['dn_country'],
745
	get_cert_country_codes()
746
));
747

    
748
$section->addInput(new Form_Input(
749
	'dn_state',
750
	'State or Province',
751
	'text',
752
	$pconfig['dn_state'],
753
	['placeholder' => 'e.g. Texas']
754
));
755

    
756
$section->addInput(new Form_Input(
757
	'dn_city',
758
	'City',
759
	'text',
760
	$pconfig['dn_city'],
761
	['placeholder' => 'e.g. Austin']
762
));
763

    
764
$section->addInput(new Form_Input(
765
	'dn_organization',
766
	'Organization',
767
	'text',
768
	$pconfig['dn_organization'],
769
	['placeholder' => 'e.g. My Company Inc']
770
));
771

    
772
$section->addInput(new Form_Input(
773
	'dn_organizationalunit',
774
	'Organizational Unit',
775
	'text',
776
	$pconfig['dn_organizationalunit'],
777
	['placeholder' => 'e.g. My Department Name (optional)']
778
));
779

    
780
$form->add($section);
781

    
782
print $form;
783

    
784
$internal_ca_count = 0;
785
foreach ($a_ca as $ca) {
786
	if ($ca['prv']) {
787
		$internal_ca_count++;
788
	}
789
}
790

    
791
?>
792
<script type="text/javascript">
793
//<![CDATA[
794
events.push(function() {
795
	function change_keytype() {
796
	hideClass('rsakeys', ($('#keytype').val() != 'RSA'));
797
		hideClass('ecnames', ($('#keytype').val() != 'ECDSA'));
798
	}
799

    
800
	$('#keytype').change(function () {
801
		change_keytype();
802
	});
803

    
804
	function check_keylen() {
805
		var min_keylen = <?= $cert_strict_values['min_private_key_bits'] ?>;
806
		var klid = '#keylen';
807
		/* Color the Parent/Label */
808
		if (parseInt($(klid).val()) < min_keylen) {
809
			$(klid).parent().parent().removeClass("text-normal").addClass("text-warning");
810
		} else {
811
			$(klid).parent().parent().removeClass("text-warning").addClass("text-normal");
812
		}
813
		/* Color individual options */
814
		$(klid + " option").filter(function() {
815
			return parseInt($(this).val()) < min_keylen;
816
		}).removeClass("text-normal").addClass("text-warning").siblings().removeClass("text-warning").addClass("text-normal");
817
	}
818

    
819
	function check_digest() {
820
		var weak_algs = <?= json_encode($cert_strict_values['digest_blacklist']) ?>;
821
		var daid = '#digest_alg';
822
		/* Color the Parent/Label */
823
		if (jQuery.inArray($(daid).val(), weak_algs) > -1) {
824
			$(daid).parent().parent().removeClass("text-normal").addClass("text-warning");
825
		} else {
826
			$(daid).parent().parent().removeClass("text-warning").addClass("text-normal");
827
		}
828
		/* Color individual options */
829
		$(daid + " option").filter(function() {
830
			return (jQuery.inArray($(this).val(), weak_algs) > -1);
831
		}).removeClass("text-normal").addClass("text-warning").siblings().removeClass("text-warning").addClass("text-normal");
832
	}
833

    
834
	// ---------- Control change handlers ---------------------------------------------------------
835

    
836
	$('#method').on('change', function() {
837
		check_keylen();
838
		check_digest();
839
	});
840

    
841
	$('#keylen').on('change', function() {
842
		check_keylen();
843
	});
844

    
845
	$('#digest_alg').on('change', function() {
846
		check_digest();
847
	});
848

    
849
	// ---------- On initial page load ------------------------------------------------------------
850
	change_keytype();
851
	check_keylen();
852
	check_digest();
853
});
854
//]]>
855
</script>
856
<?php
857
include('foot.inc');
(192-192/228)