Project

General

Profile

Download (25.5 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-2020 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
			if ($_POST['cert'] && (!strstr($_POST['cert'], "BEGIN CERTIFICATE") || !strstr($_POST['cert'], "END CERTIFICATE"))) {
157
				$input_errors[] = gettext("This certificate does not appear to be valid.");
158
			}
159
			if ($_POST['key'] && strstr($_POST['key'], "ENCRYPTED")) {
160
				$input_errors[] = gettext("Encrypted private keys are not yet supported.");
161
			}
162
			if (!$input_errors && !empty($_POST['key']) && cert_get_publickey($_POST['cert'], false) != cert_get_publickey($_POST['key'], false, 'prv')) {
163
				$input_errors[] = gettext("The submitted private key does not match the submitted certificate data.");
164
			}
165
			/* we must ensure the certificate is capable of acting as a CA
166
			 * https://redmine.pfsense.org/issues/7885
167
			 */
168
			if (!$input_errors) {
169
				$purpose = cert_get_purpose($_POST['cert'], false);
170
				if ($purpose['ca'] != 'Yes') {
171
					$input_errors[] = gettext("The submitted certificate does not appear to be a Certificate Authority, import it on the Certificates tab instead.");
172
				}
173
			}
174
			break;
175
		case 'internal':
176
			$reqdfields = explode(" ",
177
				"descr keylen ecname keytype lifetime dn_commonname");
178
			$reqdfieldsn = array(
179
				gettext("Descriptive name"),
180
				gettext("Key length"),
181
				gettext("Elliptic Curve Name"),
182
				gettext("Key type"),
183
				gettext("Lifetime"),
184
				gettext("Common Name"));
185
			break;
186
		case 'intermediate':
187
			$reqdfields = explode(" ",
188
				"descr caref keylen ecname keytype lifetime dn_commonname");
189
			$reqdfieldsn = array(
190
				gettext("Descriptive name"),
191
				gettext("Signing Certificate Authority"),
192
				gettext("Key length"),
193
				gettext("Elliptic Curve Name"),
194
				gettext("Key type"),
195
				gettext("Lifetime"),
196
				gettext("Common Name"));
197
			break;
198
		default:
199
			break;
200
	}
201

    
202
	do_input_validation($_POST, $reqdfields, $reqdfieldsn, $input_errors);
203
	if ($pconfig['method'] != "existing") {
204
		/* Make sure we do not have invalid characters in the fields for the certificate */
205
		if (preg_match("/[\?\>\<\&\/\\\"\']/", $_POST['descr'])) {
206
			array_push($input_errors, gettext("The field 'Descriptive Name' contains invalid characters."));
207
		}
208
		if (!in_array($_POST["keytype"], $ca_keytypes)) {
209
			array_push($input_errors, gettext("Please select a valid Key Type."));
210
		}
211
		if (!in_array($_POST["keylen"], $ca_keylens)) {
212
			array_push($input_errors, gettext("Please select a valid Key Length."));
213
		}
214
		if (!in_array($_POST["ecname"], array_keys($openssl_ecnames))) {
215
			array_push($input_errors, gettext("Please select a valid Elliptic Curve Name."));
216
		}
217
		if (!in_array($_POST["digest_alg"], $openssl_digest_algs)) {
218
			array_push($input_errors, gettext("Please select a valid Digest Algorithm."));
219
		}
220
		if ($_POST['lifetime'] > $max_lifetime) {
221
			$input_errors[] = gettext("Lifetime is longer than the maximum allowed value. Use a shorter lifetime.");
222
		}
223
	}
224

    
225
	if (!empty($_POST['serial']) && !cert_validate_serial($_POST['serial'])) {
226
		$input_errors[] = gettext("Please enter a valid integer serial number.");
227
	}
228

    
229
	/* save modifications */
230
	if (!$input_errors) {
231
		$ca = array();
232
		if (!isset($pconfig['refid']) || empty($pconfig['refid'])) {
233
			$ca['refid'] = uniqid();
234
		} else {
235
			$ca['refid'] = $pconfig['refid'];
236
		}
237

    
238
		if (isset($id) && $thisca) {
239
			$ca = $thisca;
240
		}
241

    
242
		$ca['descr'] = $pconfig['descr'];
243
		$ca['trust'] = ($pconfig['trust'] == 'yes') ? "enabled" : "disabled";
244
		$ca['randomserial'] = ($pconfig['randomserial'] == 'yes') ? "enabled" : "disabled";
245

    
246
		if ($act == "edit") {
247
			$ca['descr']  = $pconfig['descr'];
248
			$ca['refid']  = $pconfig['refid'];
249
			$ca['serial'] = $pconfig['serial'];
250
			$ca['crt'] = base64_encode($pconfig['cert']);
251
			$ca['prv'] = base64_encode($pconfig['key']);
252
			$savemsg = sprintf(gettext("Updated Certificate Authority %s"), $ca['descr']);
253
		} else {
254
			$old_err_level = error_reporting(0); /* otherwise openssl_ functions throw warnings directly to a page screwing menu tab */
255
			if ($pconfig['method'] == "existing") {
256
				ca_import($ca, $pconfig['cert'], $pconfig['key'], $pconfig['serial']);
257
				$savemsg = sprintf(gettext("Imported Certificate Authority %s"), $ca['descr']);
258
			} else if ($pconfig['method'] == "internal") {
259
				$dn = array('commonName' => cert_escape_x509_chars($pconfig['dn_commonname']));
260
				if (!empty($pconfig['dn_country'])) {
261
					$dn['countryName'] = $pconfig['dn_country'];
262
				}
263
				if (!empty($pconfig['dn_state'])) {
264
					$dn['stateOrProvinceName'] = cert_escape_x509_chars($pconfig['dn_state']);
265
				}
266
				if (!empty($pconfig['dn_city'])) {
267
					$dn['localityName'] = cert_escape_x509_chars($pconfig['dn_city']);
268
				}
269
				if (!empty($pconfig['dn_organization'])) {
270
					$dn['organizationName'] = cert_escape_x509_chars($pconfig['dn_organization']);
271
				}
272
				if (!empty($pconfig['dn_organizationalunit'])) {
273
					$dn['organizationalUnitName'] = cert_escape_x509_chars($pconfig['dn_organizationalunit']);
274
				}
275
				if (!ca_create($ca, $pconfig['keylen'], $pconfig['lifetime'], $dn, $pconfig['digest_alg'], $pconfig['keytype'], $pconfig['ecname'])) {
276
					$input_errors = array();
277
					while ($ssl_err = openssl_error_string()) {
278
						if (strpos($ssl_err, 'NCONF_get_string:no value') === false) {
279
							array_push($input_errors, "openssl library returns: " . $ssl_err);
280
						}
281
					}
282
				}
283
				$savemsg = sprintf(gettext("Created internal Certificate Authority %s"), $ca['descr']);
284
			} else if ($pconfig['method'] == "intermediate") {
285
				$dn = array('commonName' => cert_escape_x509_chars($pconfig['dn_commonname']));
286
				if (!empty($pconfig['dn_country'])) {
287
					$dn['countryName'] = $pconfig['dn_country'];
288
				}
289
				if (!empty($pconfig['dn_state'])) {
290
					$dn['stateOrProvinceName'] = cert_escape_x509_chars($pconfig['dn_state']);
291
				}
292
				if (!empty($pconfig['dn_city'])) {
293
					$dn['localityName'] = cert_escape_x509_chars($pconfig['dn_city']);
294
				}
295
				if (!empty($pconfig['dn_organization'])) {
296
					$dn['organizationName'] = cert_escape_x509_chars($pconfig['dn_organization']);
297
				}
298
				if (!empty($pconfig['dn_organizationalunit'])) {
299
					$dn['organizationalUnitName'] = cert_escape_x509_chars($pconfig['dn_organizationalunit']);
300
				}
301
				if (!ca_inter_create($ca, $pconfig['keylen'], $pconfig['lifetime'], $dn, $pconfig['caref'], $pconfig['digest_alg'], $pconfig['keytype'], $pconfig['ecname'])) {
302
					$input_errors = array();
303
					while ($ssl_err = openssl_error_string()) {
304
						if (strpos($ssl_err, 'NCONF_get_string:no value') === false) {
305
							array_push($input_errors, "openssl library returns: " . $ssl_err);
306
						}
307
					}
308
				}
309
				$savemsg = sprintf(gettext("Created internal intermediate Certificate Authority %s"), $ca['descr']);
310
			}
311
			error_reporting($old_err_level);
312
		}
313

    
314
		if (isset($id) && $thisca) {
315
			$thisca = $ca;
316
		} else {
317
			$a_ca[] = $ca;
318
		}
319

    
320
		if (!$input_errors) {
321
			write_config($savemsg);
322
			ca_setup_trust_store();
323
			pfSenseHeader("system_camanager.php");
324
		}
325
	}
326
}
327

    
328
$pgtitle = array(gettext("System"), gettext("Certificate Manager"), gettext("CAs"));
329
$pglinks = array("", "system_camanager.php", "system_camanager.php");
330

    
331
if ($act == "new" || $act == "edit" || $act == gettext("Save") || $input_errors) {
332
	$pgtitle[] = gettext('Edit');
333
	$pglinks[] = "@self";
334
}
335
include("head.inc");
336

    
337
if ($input_errors) {
338
	print_input_errors($input_errors);
339
}
340

    
341
if ($savemsg) {
342
	print_info_box($savemsg, $class);
343
}
344

    
345
$tab_array = array();
346
$tab_array[] = array(gettext("CAs"), true, "system_camanager.php");
347
$tab_array[] = array(gettext("Certificates"), false, "system_certmanager.php");
348
$tab_array[] = array(gettext("Certificate Revocation"), false, "system_crlmanager.php");
349
display_top_tabs($tab_array);
350

    
351
if (!($act == "new" || $act == "edit" || $act == gettext("Save") || $input_errors)) {
352
?>
353
<div class="panel panel-default" id="search-panel">
354
	<div class="panel-heading">
355
		<h2 class="panel-title">
356
			<?=gettext('Search')?>
357
			<span class="widget-heading-icon pull-right">
358
				<a data-toggle="collapse" href="#search-panel_panel-body">
359
					<i class="fa fa-plus-circle"></i>
360
				</a>
361
			</span>
362
		</h2>
363
	</div>
364
	<div id="search-panel_panel-body" class="panel-body collapse in">
365
		<div class="form-group">
366
			<label class="col-sm-2 control-label">
367
				<?=gettext("Search term")?>
368
			</label>
369
			<div class="col-sm-5"><input class="form-control" name="searchstr" id="searchstr" type="text"/></div>
370
			<div class="col-sm-2">
371
				<select id="where" class="form-control">
372
					<option value="0"><?=gettext("Name")?></option>
373
					<option value="1"><?=gettext("Distinguished Name")?></option>
374
					<option value="2" selected><?=gettext("Both")?></option>
375
				</select>
376
			</div>
377
			<div class="col-sm-3">
378
				<a id="btnsearch" title="<?=gettext("Search")?>" class="btn btn-primary btn-sm"><i class="fa fa-search icon-embed-btn"></i><?=gettext("Search")?></a>
379
				<a id="btnclear" title="<?=gettext("Clear")?>" class="btn btn-info btn-sm"><i class="fa fa-undo icon-embed-btn"></i><?=gettext("Clear")?></a>
380
			</div>
381
			<div class="col-sm-10 col-sm-offset-2">
382
				<span class="help-block"><?=gettext('Enter a search string or *nix regular expression to search certificate names and distinguished names.')?></span>
383
			</div>
384
		</div>
385
	</div>
386
</div>
387

    
388
<div class="panel panel-default">
389
	<div class="panel-heading"><h2 class="panel-title"><?=gettext('Certificate Authorities')?></h2></div>
390
	<div class="panel-body">
391
		<div class="table-responsive">
392
		<table id="catable" class="table table-striped table-hover table-rowdblclickedit sortable-theme-bootstrap" data-sortable>
393
			<thead>
394
				<tr>
395
					<th><?=gettext("Name")?></th>
396
					<th><?=gettext("Internal")?></th>
397
					<th><?=gettext("Issuer")?></th>
398
					<th><?=gettext("Certificates")?></th>
399
					<th><?=gettext("Distinguished Name")?></th>
400
					<th><?=gettext("In Use")?></th>
401
					<th><?=gettext("Actions")?></th>
402
				</tr>
403
			</thead>
404
			<tbody>
405
<?php
406
$pluginparams = array();
407
$pluginparams['type'] = 'certificates';
408
$pluginparams['event'] = 'used_ca';
409
$certificates_used_by_packages = pkg_call_plugins('plugin_certificates', $pluginparams);
410

    
411
foreach ($a_ca as $ca):
412
	$name = htmlspecialchars($ca['descr']);
413
	$subj = cert_get_subject($ca['crt']);
414
	$issuer = cert_get_issuer($ca['crt']);
415
	if ($subj == $issuer) {
416
		$issuer_name = gettext("self-signed");
417
	} else {
418
		$issuer_name = gettext("external");
419
	}
420
	$subj = htmlspecialchars(cert_escape_x509_chars($subj, true));
421
	$issuer = htmlspecialchars($issuer);
422
	$certcount = 0;
423

    
424
	$issuer_ca = lookup_ca($ca['caref']);
425
	if ($issuer_ca) {
426
		$issuer_name = $issuer_ca['descr'];
427
	}
428

    
429
	foreach ($a_cert as $cert) {
430
		if ($cert['caref'] == $ca['refid']) {
431
			$certcount++;
432
		}
433
	}
434

    
435
	foreach ($a_ca as $cert) {
436
		if ($cert['caref'] == $ca['refid']) {
437
			$certcount++;
438
		}
439
	}
440
?>
441
				<tr>
442
					<td><?=$name?></td>
443
					<td><i class="fa fa-<?= (!empty($ca['prv'])) ? "check" : "times" ; ?>"></i></td>
444
					<td><i><?=$issuer_name?></i></td>
445
					<td><?=$certcount?></td>
446
					<td>
447
						<?=$subj?>
448
						<?php cert_print_infoblock($ca); ?>
449
						<?php cert_print_dates($ca);?>
450
					</td>
451
					<td class="text-nowrap">
452
						<?php if (is_openvpn_server_ca($ca['refid'])): ?>
453
							<?=gettext("OpenVPN Server")?><br/>
454
						<?php endif?>
455
						<?php if (is_openvpn_client_ca($ca['refid'])): ?>
456
							<?=gettext("OpenVPN Client")?><br/>
457
						<?php endif?>
458
						<?php if (is_ipsec_peer_ca($ca['refid'])): ?>
459
							<?=gettext("IPsec Tunnel")?><br/>
460
						<?php endif?>
461
						<?php if (is_ldap_peer_ca($ca['refid'])): ?>
462
							<?=gettext("LDAP Server")?>
463
						<?php endif?>
464
						<?php echo cert_usedby_description($ca['refid'], $certificates_used_by_packages); ?>
465
					</td>
466
					<td class="text-nowrap">
467
						<a class="fa fa-pencil"	title="<?=gettext("Edit CA")?>"	href="system_camanager.php?act=edit&amp;id=<?=$ca['refid']?>"></a>
468
						<a class="fa fa-certificate"	title="<?=gettext("Export CA")?>"	href="system_camanager.php?act=exp&amp;id=<?=$ca['refid']?>"></a>
469
					<?php if ($ca['prv']): ?>
470
						<a class="fa fa-key"	title="<?=gettext("Export key")?>"	href="system_camanager.php?act=expkey&amp;id=<?=$ca['refid']?>"></a>
471
					<?php endif?>
472
					<?php if (is_cert_locally_renewable($ca)): ?>
473
						<a href="system_certmanager_renew.php?type=ca&amp;refid=<?=$ca['refid']?>" class="fa fa-repeat" title="<?=gettext("Reissue/Renew")?>"></a>
474
					<?php endif ?>
475
					<?php if (!ca_in_use($ca['refid'])): ?>
476
						<a class="fa fa-trash" 	title="<?=gettext("Delete CA and its CRLs")?>"	href="system_camanager.php?act=del&amp;id=<?=$ca['refid']?>" usepost ></a>
477
					<?php endif?>
478
					</td>
479
				</tr>
480
<?php endforeach; ?>
481
			</tbody>
482
		</table>
483
		</div>
484
	</div>
485
</div>
486

    
487
<nav class="action-buttons">
488
	<a href="?act=new" class="btn btn-success btn-sm">
489
		<i class="fa fa-plus icon-embed-btn"></i>
490
		<?=gettext("Add")?>
491
	</a>
492
</nav>
493
<script type="text/javascript">
494
//<![CDATA[
495

    
496
events.push(function() {
497

    
498
	// Make these controls plain buttons
499
	$("#btnsearch").prop('type', 'button');
500
	$("#btnclear").prop('type', 'button');
501

    
502
	// Search for a term in the entry name and/or dn
503
	$("#btnsearch").click(function() {
504
		var searchstr = $('#searchstr').val().toLowerCase();
505
		var table = $("table tbody");
506
		var where = $('#where').val();
507

    
508
		table.find('tr').each(function (i) {
509
			var $tds = $(this).find('td'),
510
				shortname = $tds.eq(0).text().trim().toLowerCase(),
511
				dn = $tds.eq(4).text().trim().toLowerCase();
512

    
513
			regexp = new RegExp(searchstr);
514
			if (searchstr.length > 0) {
515
				if (!(regexp.test(shortname) && (where != 1)) && !(regexp.test(dn) && (where != 0))) {
516
					$(this).hide();
517
				} else {
518
					$(this).show();
519
				}
520
			} else {
521
				$(this).show();	// A blank search string shows all
522
			}
523
		});
524
	});
525

    
526
	// Clear the search term and unhide all rows (that were hidden during a previous search)
527
	$("#btnclear").click(function() {
528
		var table = $("table tbody");
529

    
530
		$('#searchstr').val("");
531

    
532
		table.find('tr').each(function (i) {
533
			$(this).show();
534
		});
535
	});
536

    
537
	// Hitting the enter key will do the same as clicking the search button
538
	$("#searchstr").on("keyup", function (event) {
539
		if (event.keyCode == 13) {
540
			$("#btnsearch").get(0).click();
541
		}
542
	});
543
});
544
//]]>
545
</script>
546

    
547
<?php
548
	include("foot.inc");
549
	exit;
550
}
551

    
552
$form = new Form;
553
//$form->setAction('system_camanager.php?act=edit');
554
if (isset($id) && $thisca) {
555
	$form->addGlobal(new Form_Input(
556
		'id',
557
		null,
558
		'hidden',
559
		$id
560
	));
561
}
562

    
563
if ($act == "edit") {
564
	$form->addGlobal(new Form_Input(
565
		'refid',
566
		null,
567
		'hidden',
568
		$pconfig['refid']
569
	));
570
}
571

    
572
$section = new Form_Section('Create / Edit CA');
573

    
574
$section->addInput(new Form_Input(
575
	'descr',
576
	'*Descriptive name',
577
	'text',
578
	$pconfig['descr']
579
));
580

    
581
if (!isset($id) || $act == "edit") {
582
	$section->addInput(new Form_Select(
583
		'method',
584
		'*Method',
585
		$pconfig['method'],
586
		$ca_methods
587
	))->toggles();
588
}
589

    
590
$section->addInput(new Form_Checkbox(
591
	'trust',
592
	'Trust Store',
593
	'Add this Certificate Authority to the Operating System Trust Store',
594
	$pconfig['trust']
595
))->setHelp('When enabled, the contents of the CA will be added to the trust ' .
596
	'store so that they will be trusted by the operating system.');
597

    
598
$section->addInput(new Form_Checkbox(
599
	'randomserial',
600
	'Randomize Serial',
601
	'Use random serial numbers when signing certifices',
602
	$pconfig['randomserial']
603
))->setHelp('When enabled, if this CA is capable of signing certificates then ' .
604
		'serial numbers for certificates signed by this CA will be ' .
605
		'automatically randomized and checked for uniqueness instead of ' .
606
		'using the sequential value from Next Certificate Serial.');
607

    
608
$form->add($section);
609

    
610
$section = new Form_Section('Existing Certificate Authority');
611
$section->addClass('toggle-existing collapse');
612

    
613
$section->addInput(new Form_Textarea(
614
	'cert',
615
	'*Certificate data',
616
	$pconfig['cert']
617
))->setHelp('Paste a certificate in X.509 PEM format here.');
618

    
619
$section->addInput(new Form_Textarea(
620
	'key',
621
	'Certificate Private Key (optional)',
622
	$pconfig['key']
623
))->setHelp('Paste the private key for the above certificate here. This is '.
624
	'optional in most cases, but is required when generating a '.
625
	'Certificate Revocation List (CRL).');
626

    
627
$section->addInput(new Form_Input(
628
	'serial',
629
	'Next Certificate Serial',
630
	'number',
631
	$pconfig['serial']
632
))->setHelp('Enter a decimal number to be used as a sequential serial number for ' .
633
	'the next certificate to be signed by this CA.');
634

    
635
$form->add($section);
636

    
637
$section = new Form_Section('Internal Certificate Authority');
638
$section->addClass('toggle-internal', 'toggle-intermediate', 'collapse');
639

    
640
$allCas = array();
641
foreach ($a_ca as $ca) {
642
	if (!$ca['prv']) {
643
			continue;
644
	}
645

    
646
	$allCas[ $ca['refid'] ] = $ca['descr'];
647
}
648

    
649
$group = new Form_Group('*Signing Certificate Authority');
650
$group->addClass('toggle-intermediate', 'collapse');
651
$group->add(new Form_Select(
652
	'caref',
653
	null,
654
	$pconfig['caref'],
655
	$allCas
656
));
657
$section->add($group);
658

    
659
$section->addInput(new Form_Select(
660
	'keytype',
661
	'*Key type',
662
	$pconfig['keytype'],
663
	array_combine($ca_keytypes, $ca_keytypes)
664
));
665

    
666
$group = new Form_Group($i == 0 ? '*Key length':'');
667
$group->addClass('rsakeys');
668
$group->add(new Form_Select(
669
	'keylen',
670
	null,
671
	$pconfig['keylen'],
672
	array_combine($ca_keylens, $ca_keylens)
673
))->setHelp('The length to use when generating a new RSA key, in bits. %1$s' .
674
	'The Key Length should not be lower than 2048 or some platforms ' .
675
	'may consider the certificate invalid.', '<br/>');
676
$section->add($group);
677

    
678
$group = new Form_Group($i == 0 ? '*Elliptic Curve Name':'');
679
$group->addClass('ecnames');
680
$group->add(new Form_Select(
681
	'ecname',
682
	null,
683
	$pconfig['ecname'],
684
	$openssl_ecnames
685
))->setHelp('Curves may not be compatible with all uses. Known compatible curve uses are denoted in brackets.');
686
$section->add($group);
687

    
688
$section->addInput(new Form_Select(
689
	'digest_alg',
690
	'*Digest Algorithm',
691
	$pconfig['digest_alg'],
692
	array_combine($openssl_digest_algs, $openssl_digest_algs)
693
))->setHelp('The digest method used when the CA is signed. %1$s' .
694
	'The best practice is to use an algorithm stronger than SHA1. '.
695
	'Some platforms may consider weaker digest algorithms invalid', '<br/>');
696

    
697
$section->addInput(new Form_Input(
698
	'lifetime',
699
	'*Lifetime (days)',
700
	'number',
701
	$pconfig['lifetime'],
702
	['max' => $max_lifetime]
703
));
704

    
705
$section->addInput(new Form_Input(
706
	'dn_commonname',
707
	'*Common Name',
708
	'text',
709
	$pconfig['dn_commonname'],
710
	['placeholder' => 'e.g. internal-ca']
711
));
712

    
713
$section->addInput(new Form_StaticText(
714
	null,
715
	gettext('The following certificate authority subject components are optional and may be left blank.')
716
));
717

    
718
$section->addInput(new Form_Select(
719
	'dn_country',
720
	'Country Code',
721
	$pconfig['dn_country'],
722
	get_cert_country_codes()
723
));
724

    
725
$section->addInput(new Form_Input(
726
	'dn_state',
727
	'State or Province',
728
	'text',
729
	$pconfig['dn_state'],
730
	['placeholder' => 'e.g. Texas']
731
));
732

    
733
$section->addInput(new Form_Input(
734
	'dn_city',
735
	'City',
736
	'text',
737
	$pconfig['dn_city'],
738
	['placeholder' => 'e.g. Austin']
739
));
740

    
741
$section->addInput(new Form_Input(
742
	'dn_organization',
743
	'Organization',
744
	'text',
745
	$pconfig['dn_organization'],
746
	['placeholder' => 'e.g. My Company Inc']
747
));
748

    
749
$section->addInput(new Form_Input(
750
	'dn_organizationalunit',
751
	'Organizational Unit',
752
	'text',
753
	$pconfig['dn_organizationalunit'],
754
	['placeholder' => 'e.g. My Department Name (optional)']
755
));
756

    
757
$form->add($section);
758

    
759
print $form;
760

    
761
$internal_ca_count = 0;
762
foreach ($a_ca as $ca) {
763
	if ($ca['prv']) {
764
		$internal_ca_count++;
765
	}
766
}
767

    
768
?>
769
<script type="text/javascript">
770
//<![CDATA[
771
events.push(function() {
772
	function change_keytype() {
773
	hideClass('rsakeys', ($('#keytype').val() != 'RSA'));
774
		hideClass('ecnames', ($('#keytype').val() != 'ECDSA'));
775
	}
776

    
777
	$('#keytype').change(function () {
778
		change_keytype();
779
	});
780

    
781
	function check_keylen() {
782
		var min_keylen = <?= $cert_strict_values['min_private_key_bits'] ?>;
783
		var klid = '#keylen';
784
		/* Color the Parent/Label */
785
		if (parseInt($(klid).val()) < min_keylen) {
786
			$(klid).parent().parent().removeClass("text-normal").addClass("text-warning");
787
		} else {
788
			$(klid).parent().parent().removeClass("text-warning").addClass("text-normal");
789
		}
790
		/* Color individual options */
791
		$(klid + " option").filter(function() {
792
			return parseInt($(this).val()) < min_keylen;
793
		}).removeClass("text-normal").addClass("text-warning").siblings().removeClass("text-warning").addClass("text-normal");
794
	}
795

    
796
	function check_digest() {
797
		var weak_algs = <?= json_encode($cert_strict_values['digest_blacklist']) ?>;
798
		var daid = '#digest_alg';
799
		/* Color the Parent/Label */
800
		if (jQuery.inArray($(daid).val(), weak_algs) > -1) {
801
			$(daid).parent().parent().removeClass("text-normal").addClass("text-warning");
802
		} else {
803
			$(daid).parent().parent().removeClass("text-warning").addClass("text-normal");
804
		}
805
		/* Color individual options */
806
		$(daid + " option").filter(function() {
807
			return (jQuery.inArray($(this).val(), weak_algs) > -1);
808
		}).removeClass("text-normal").addClass("text-warning").siblings().removeClass("text-warning").addClass("text-normal");
809
	}
810

    
811
	// ---------- Control change handlers ---------------------------------------------------------
812

    
813
	$('#method').on('change', function() {
814
		check_keylen();
815
		check_digest();
816
	});
817

    
818
	$('#keylen').on('change', function() {
819
		check_keylen();
820
	});
821

    
822
	$('#digest_alg').on('change', function() {
823
		check_digest();
824
	});
825

    
826
	// ---------- On initial page load ------------------------------------------------------------
827
	change_keytype();
828
	check_keylen();
829
	check_digest();
830
});
831
//]]>
832
</script>
833
<?php
834
include('foot.inc');
(194-194/229)