Project

General

Profile

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

    
22
##|+PRIV
23
##|*IDENT=page-system-crlmanager
24
##|*NAME=System: CRL Manager
25
##|*DESCR=Allow access to the 'System: CRL Manager' page.
26
##|*MATCH=system_crlmanager.php*
27
##|-PRIV
28

    
29
require_once("guiconfig.inc");
30
require_once("certs.inc");
31
require_once("openvpn.inc");
32
require_once("pfsense-utils.inc");
33
require_once("vpn.inc");
34

    
35
$max_lifetime = crl_get_max_lifetime();
36
$default_lifetime = 3650;
37
if ($max_lifetime < $default_lifetime) {
38
	$default_lifetime = $max_lifetime;
39
}
40

    
41
global $openssl_crl_status;
42

    
43
$crl_methods = array(
44
	"internal" => gettext("Create an internal Certificate Revocation List"),
45
	"existing" => gettext("Import an existing Certificate Revocation List"));
46

    
47
if (isset($_REQUEST['id']) && ctype_alnum($_REQUEST['id'])) {
48
	$id = $_REQUEST['id'];
49
}
50

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

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

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

    
60
foreach ($a_crl as $cid => $acrl) {
61
	if (!isset($acrl['refid'])) {
62
		unset ($a_crl[$cid]);
63
	}
64
}
65

    
66
$act = $_REQUEST['act'];
67

    
68

    
69
if (!empty($id)) {
70
	$thiscrl =& lookup_crl($id);
71
}
72

    
73
// If we were given an invalid crlref in the id, no sense in continuing as it would only cause errors.
74
if (!$thiscrl && (($act != "") && ($act != "new"))) {
75
	pfSenseHeader("system_crlmanager.php");
76
	$act="";
77
	$savemsg = gettext("Invalid CRL reference.");
78
	$class = "danger";
79
}
80

    
81
if ($_POST['act'] == "del") {
82
	$name = htmlspecialchars($thiscrl['descr']);
83
	if (crl_in_use($id)) {
84
		$savemsg = sprintf(gettext("Certificate Revocation List %s is in use and cannot be deleted."), $name);
85
		$class = "danger";
86
	} else {
87
		foreach ($a_crl as $cid => $acrl) {
88
			if ($acrl['refid'] == $thiscrl['refid']) {
89
				unset($a_crl[$cid]);
90
			}
91
		}
92
		write_config("Deleted CRL {$name}.");
93
		$savemsg = sprintf(gettext("Certificate Revocation List %s successfully deleted."), $name);
94
		$class = "success";
95
	}
96
}
97

    
98
if ($act == "new") {
99
	$pconfig['method'] = $_REQUEST['method'];
100
	$pconfig['caref'] = $_REQUEST['caref'];
101
	$pconfig['lifetime'] = $default_lifetime;
102
	$pconfig['serial'] = "0";
103
}
104

    
105
if ($act == "exp") {
106
	crl_update($thiscrl);
107
	$exp_name = urlencode("{$thiscrl['descr']}.crl");
108
	$exp_data = base64_decode($thiscrl['text']);
109
	$exp_size = strlen($exp_data);
110

    
111
	header("Content-Type: application/octet-stream");
112
	header("Content-Disposition: attachment; filename={$exp_name}");
113
	header("Content-Length: $exp_size");
114
	echo $exp_data;
115
	exit;
116
}
117

    
118
if ($act == "addcert") {
119

    
120
	unset($input_errors);
121
	$pconfig = $_REQUEST;
122

    
123
	if (!$pconfig['crlref'] || !$pconfig['certref']) {
124
		pfSenseHeader("system_crlmanager.php");
125
		exit;
126
	}
127

    
128
	// certref, crlref
129
	$crl =& lookup_crl($pconfig['crlref']);
130
	$cert = lookup_cert($pconfig['certref']);
131

    
132
	if (!$crl['caref'] || !$cert['caref']) {
133
		$input_errors[] = gettext("Both the Certificate and CRL must be specified.");
134
	}
135

    
136
	if ($crl['caref'] != $cert['caref']) {
137
		$input_errors[] = gettext("CA mismatch between the Certificate and CRL. Unable to Revoke.");
138
	}
139
	if (!is_crl_internal($crl)) {
140
		$input_errors[] = gettext("Cannot revoke certificates for an imported/external CRL.");
141
	}
142

    
143
	if (!$input_errors) {
144
		$reason = (empty($pconfig['crlreason'])) ? 0 : $pconfig['crlreason'];
145
		cert_revoke($cert, $crl, $reason);
146
		// refresh IPsec and OpenVPN CRLs
147
		openvpn_refresh_crls();
148
		vpn_ipsec_configure();
149
		write_config("Revoked cert {$cert['descr']} in CRL {$crl['descr']}.");
150
		pfSenseHeader("system_crlmanager.php");
151
		exit;
152
	}
153
}
154

    
155
if ($act == "delcert") {
156
	if (!is_array($thiscrl['cert'])) {
157
		pfSenseHeader("system_crlmanager.php");
158
		exit;
159
	}
160
	$found = false;
161
	foreach ($thiscrl['cert'] as $acert) {
162
		if ($acert['refid'] == $_REQUEST['certref']) {
163
			$found = true;
164
			$thiscert = $acert;
165
		}
166
	}
167
	if (!$found) {
168
		pfSenseHeader("system_crlmanager.php");
169
		exit;
170
	}
171
	$certname = htmlspecialchars($thiscert['descr']);
172
	$crlname = htmlspecialchars($thiscrl['descr']);
173
	if (cert_unrevoke($thiscert, $thiscrl)) {
174
		$savemsg = sprintf(gettext('Deleted Certificate %1$s from CRL %2$s.'), $certname, $crlname);
175
		$class = "success";
176
		// refresh IPsec and OpenVPN CRLs
177
		openvpn_refresh_crls();
178
		vpn_ipsec_configure();
179
		write_config($savemsg);
180
	} else {
181
		$savemsg = sprintf(gettext('Failed to delete Certificate %1$s from CRL %2$s.'), $certname, $crlname);
182
		$class = "danger";
183
	}
184
	$act="edit";
185
}
186

    
187
if ($_POST['save']) {
188
	$input_errors = array();
189
	$pconfig = $_POST;
190

    
191
	/* input validation */
192
	if (($pconfig['method'] == "existing") || ($act == "editimported")) {
193
		$reqdfields = explode(" ", "descr crltext");
194
		$reqdfieldsn = array(
195
			gettext("Descriptive name"),
196
			gettext("Certificate Revocation List data"));
197
	}
198
	if ($pconfig['method'] == "internal") {
199
		$reqdfields = explode(" ", "descr caref");
200
		$reqdfieldsn = array(
201
			gettext("Descriptive name"),
202
			gettext("Certificate Authority"));
203
	}
204

    
205
	do_input_validation($_POST, $reqdfields, $reqdfieldsn, $input_errors);
206

    
207
	if (preg_match("/[\?\>\<\&\/\\\"\']/", $pconfig['descr'])) {
208
		array_push($input_errors, "The field 'Descriptive Name' contains invalid characters.");
209
	}
210
	if ($pconfig['lifetime'] > $max_lifetime) {
211
		$input_errors[] = gettext("Lifetime is longer than the maximum allowed value. Use a shorter lifetime.");
212
	}
213

    
214
	/* save modifications */
215
	if (!$input_errors) {
216
		$result = false;
217

    
218
		if ($thiscrl) {
219
			$crl =& $thiscrl;
220
		} else {
221
			$crl = array();
222
			$crl['refid'] = uniqid();
223
		}
224

    
225
		$crl['descr'] = $pconfig['descr'];
226
		if ($act != "editimported") {
227
			$crl['caref'] = $pconfig['caref'];
228
			$crl['method'] = $pconfig['method'];
229
		}
230

    
231
		if (($pconfig['method'] == "existing") || ($act == "editimported")) {
232
			$crl['text'] = base64_encode($pconfig['crltext']);
233
		}
234

    
235
		if ($pconfig['method'] == "internal") {
236
			$crl['serial'] = empty($pconfig['serial']) ? 9999 : $pconfig['serial'];
237
			$crl['lifetime'] = empty($pconfig['lifetime']) ? $default_lifetime : $pconfig['lifetime'];
238
			$crl['cert'] = array();
239
		}
240

    
241
		if (!$thiscrl) {
242
			$a_crl[] = $crl;
243
		}
244

    
245
		write_config("Saved CRL {$crl['descr']}");
246
		// refresh IPsec and OpenVPN CRLs
247
		openvpn_refresh_crls();
248
		vpn_ipsec_configure();
249
		pfSenseHeader("system_crlmanager.php");
250
	}
251
}
252

    
253
$pgtitle = array(gettext("System"), gettext("Certificate Manager"), gettext("Certificate Revocation"));
254
$pglinks = array("", "system_camanager.php", "system_crlmanager.php");
255

    
256
if ($act == "new" || $act == gettext("Save") || $input_errors || $act == "edit") {
257
	$pgtitle[] = gettext('Edit');
258
	$pglinks[] = "@self";
259
}
260
include("head.inc");
261
?>
262

    
263
<script type="text/javascript">
264
//<![CDATA[
265

    
266
function method_change() {
267

    
268
	method = document.iform.method.value;
269

    
270
	switch (method) {
271
		case "internal":
272
			document.getElementById("existing").style.display="none";
273
			document.getElementById("internal").style.display="";
274
			break;
275
		case "existing":
276
			document.getElementById("existing").style.display="";
277
			document.getElementById("internal").style.display="none";
278
			break;
279
	}
280
}
281

    
282
//]]>
283
</script>
284

    
285
<?php
286

    
287
function build_method_list() {
288
	global $_POST, $crl_methods;
289

    
290
	$list = array();
291

    
292
	foreach ($crl_methods as $method => $desc) {
293
		if (($_POST['importonly'] == "yes") && ($method != "existing")) {
294
			continue;
295
		}
296

    
297
		$list[$method] = $desc;
298
	}
299

    
300
	return($list);
301
}
302

    
303
function build_ca_list() {
304
	global $a_ca;
305

    
306
	$list = array();
307

    
308
	foreach ($a_ca as $ca) {
309
		$list[$ca['refid']] = $ca['descr'];
310
	}
311

    
312
	return($list);
313
}
314

    
315
function build_cacert_list() {
316
	global $ca_certs;
317

    
318
	$list = array();
319

    
320
	foreach ($ca_certs as $cert) {
321
		$list[$cert['refid']] = $cert['descr'];
322
	}
323

    
324
	return($list);
325
}
326

    
327
if ($input_errors) {
328
	print_input_errors($input_errors);
329
}
330

    
331
if ($savemsg) {
332
	print_info_box($savemsg, $class);
333
}
334

    
335
$tab_array = array();
336
$tab_array[] = array(gettext("CAs"), false, "system_camanager.php");
337
$tab_array[] = array(gettext("Certificates"), false, "system_certmanager.php");
338
$tab_array[] = array(gettext("Certificate Revocation"), true, "system_crlmanager.php");
339
display_top_tabs($tab_array);
340

    
341
if ($act == "new" || $act == gettext("Save") || $input_errors) {
342
	if (!isset($id)) {
343
		$form = new Form();
344

    
345
		$section = new Form_Section('Create new Revocation List');
346

    
347
		$section->addInput(new Form_Select(
348
			'method',
349
			'*Method',
350
			$pconfig['method'],
351
			build_method_list()
352
		));
353

    
354
	}
355

    
356
	$section->addInput(new Form_Input(
357
		'descr',
358
		'*Descriptive name',
359
		'text',
360
		$pconfig['descr']
361
	));
362

    
363
	$section->addInput(new Form_Select(
364
		'caref',
365
		'*Certificate Authority',
366
		$pconfig['caref'],
367
		build_ca_list()
368
	));
369

    
370
	$form->add($section);
371

    
372
	$section = new Form_Section('Existing Certificate Revocation List');
373
	$section->addClass('existing');
374

    
375
	$section->addInput(new Form_Textarea(
376
		'crltext',
377
		'*CRL data',
378
		$pconfig['crltext']
379
		))->setHelp('Paste a Certificate Revocation List in X.509 CRL format here.');
380

    
381
	$form->add($section);
382

    
383
	$section = new Form_Section('Internal Certificate Revocation List');
384
	$section->addClass('internal');
385

    
386
	$section->addInput(new Form_Input(
387
		'lifetime',
388
		'Lifetime (Days)',
389
		'number',
390
		$pconfig['lifetime'],
391
		['max' => $max_lifetime]
392
	));
393

    
394
	$section->addInput(new Form_Input(
395
		'serial',
396
		'Serial',
397
		'number',
398
		$pconfig['serial'],
399
		['min' => '0', 'max' => '9999']
400
	));
401

    
402
	$form->add($section);
403

    
404
	if (isset($id) && $thiscrl) {
405
		$section->addInput(new Form_Input(
406
			'id',
407
			null,
408
			'hidden',
409
			$id
410
		));
411
	}
412

    
413
	print($form);
414

    
415
} elseif ($act == "editimported") {
416

    
417
	$form = new Form();
418

    
419
	$section = new Form_Section('Edit Imported Certificate Revocation List');
420

    
421
	$section->addInput(new Form_Input(
422
		'descr',
423
		'*Descriptive name',
424
		'text',
425
		$pconfig['descr']
426
	));
427

    
428
	$section->addInput(new Form_Textarea(
429
		'crltext',
430
		'*CRL data',
431
		$pconfig['crltext']
432
	))->setHelp('Paste a Certificate Revocation List in X.509 CRL format here.');
433

    
434
	$section->addInput(new Form_Input(
435
		'id',
436
		null,
437
		'hidden',
438
		$id
439
	));
440

    
441
	$section->addInput(new Form_Input(
442
		'act',
443
		null,
444
		'hidden',
445
		'editimported'
446
	));
447

    
448
	$form->add($section);
449

    
450
	print($form);
451

    
452
} elseif ($act == "edit") {
453
	$crl = $thiscrl;
454

    
455
	$form = new Form(false);
456
?>
457

    
458
	<div class="panel panel-default">
459
		<div class="panel-heading"><h2 class="panel-title"><?=gettext("Currently Revoked Certificates for CRL") . ': ' . $crl['descr']?></h2></div>
460
		<div class="panel-body table-responsive">
461
<?php
462
	if (!is_array($crl['cert']) || (count($crl['cert']) == 0)) {
463
		print_info_box(gettext("No certificates found for this CRL."), 'danger');
464
	} else {
465
?>
466
			<table class="table table-striped table-hover table-condensed">
467
				<thead>
468
					<tr>
469
						<th><?=gettext("Certificate Name")?></th>
470
						<th><?=gettext("Revocation Reason")?></th>
471
						<th><?=gettext("Revoked At")?></th>
472
						<th></th>
473
					</tr>
474
				</thead>
475
				<tbody>
476
<?php
477
		foreach ($crl['cert'] as $i => $cert):
478
			$name = htmlspecialchars($cert['descr']);
479
?>
480
					<tr>
481
						<td class="listlr">
482
							<?=$name; ?>
483
						</td>
484
						<td class="listlr">
485
							<?=$openssl_crl_status[$cert["reason"]]; ?>
486
						</td>
487
						<td class="listlr">
488
							<?=date("D M j G:i:s T Y", $cert["revoke_time"]); ?>
489
						</td>
490
						<td class="list">
491
							<a href="system_crlmanager.php?act=delcert&amp;id=<?=$crl['refid']; ?>&amp;certref=<?=$cert['refid']; ?>" usepost>
492
								<i class="fa fa-trash" title="<?=gettext("Delete this certificate from the CRL")?>" alt="<?=gettext("Delete this certificate from the CRL")?>"></i>
493
							</a>
494
						</td>
495
					</tr>
496
<?php
497
		endforeach;
498
?>
499
				</tbody>
500
			</table>
501
<?php
502
	}
503
?>
504
		</div>
505
	</div>
506
<?php
507

    
508
	$ca_certs = array();
509
	foreach ($a_cert as $cert) {
510
		if ($cert['caref'] == $crl['caref'] && !is_cert_revoked($cert, $id)) {
511
			$ca_certs[] = $cert;
512
		}
513
	}
514

    
515
	if (count($ca_certs) == 0) {
516
		print_info_box(gettext("No certificates found for this CA."), 'danger');
517
	} else {
518
		$section = new Form_Section('Choose a Certificate to Revoke');
519
		$group = new Form_Group(null);
520

    
521
		$group->add(new Form_Select(
522
			'certref',
523
			null,
524
			$pconfig['certref'],
525
			build_cacert_list()
526
			))->setWidth(4)->setHelp('Certificate');
527

    
528
		$group->add(new Form_Select(
529
			'crlreason',
530
			null,
531
			-1,
532
			$openssl_crl_status
533
			))->setHelp('Reason');
534

    
535
		$group->add(new Form_Button(
536
			'submit',
537
			'Add',
538
			null,
539
			'fa-plus'
540
			))->addClass('btn-success btn-sm');
541

    
542
		$section->add($group);
543

    
544
		$section->addInput(new Form_Input(
545
			'id',
546
			null,
547
			'hidden',
548
			$crl['refid']
549
		));
550

    
551
		$section->addInput(new Form_Input(
552
			'act',
553
			null,
554
			'hidden',
555
			'addcert'
556
		));
557

    
558
		$section->addInput(new Form_Input(
559
			'crlref',
560
			null,
561
			'hidden',
562
			$crl['refid']
563
		));
564

    
565
		$form->add($section);
566
	}
567

    
568
	print($form);
569
} else {
570
?>
571

    
572
	<div class="panel panel-default">
573
		<div class="panel-heading"><h2 class="panel-title"><?=gettext("Additional Certificate Revocation Lists")?></h2></div>
574
		<div class="panel-body table-responsive">
575
			<table class="table table-striped table-hover table-condensed table-rowdblclickedit">
576
				<thead>
577
					<tr>
578
						<th><?=gettext("Name")?></th>
579
						<th><?=gettext("Internal")?></th>
580
						<th><?=gettext("Certificates")?></th>
581
						<th><?=gettext("In Use")?></th>
582
						<th><?=gettext("Actions")?></th>
583
					</tr>
584
				</thead>
585
				<tbody>
586
<?php
587
	$pluginparams = array();
588
	$pluginparams['type'] = 'certificates';
589
	$pluginparams['event'] = 'used_crl';
590
	$certificates_used_by_packages = pkg_call_plugins('plugin_certificates', $pluginparams);
591
	// Map CRLs to CAs in one pass
592
	$ca_crl_map = array();
593
	foreach ($a_crl as $crl) {
594
		$ca_crl_map[$crl['caref']][] = $crl['refid'];
595
	}
596

    
597
	$i = 0;
598
	foreach ($a_ca as $ca):
599
		$name = htmlspecialchars($ca['descr']);
600

    
601
		if ($ca['prv']) {
602
			$cainternal = "YES";
603
		} else {
604
			$cainternal = "NO";
605
		}
606
?>
607
					<tr>
608
						<td colspan="4">
609
							<?=$name?>
610
						</td>
611
						<td>
612
<?php
613
		if ($cainternal == "YES"):
614
?>
615
							<a href="system_crlmanager.php?act=new&amp;caref=<?=$ca['refid']; ?>" class="btn btn-xs btn-success">
616
								<i class="fa fa-plus icon-embed-btn"></i>
617
								<?=gettext("Add or Import CRL")?>
618
							</a>
619
<?php
620
		else:
621
?>
622
							<a href="system_crlmanager.php?act=new&amp;caref=<?=$ca['refid']; ?>&amp;importonly=yes" class="btn btn-xs btn-success">
623
								<i class="fa fa-plus icon-embed-btn"></i>
624
								<?=gettext("Add or Import CRL")?>
625
							</a>
626
<?php
627
		endif;
628
?>
629
						</td>
630
					</tr>
631
<?php
632
		if (is_array($ca_crl_map[$ca['refid']])):
633
			foreach ($ca_crl_map[$ca['refid']] as $crl):
634
				$tmpcrl = lookup_crl($crl);
635
				$internal = is_crl_internal($tmpcrl);
636
				if ($internal && (!isset($tmpcrl['cert']) || empty($tmpcrl['cert'])) ) {
637
					$tmpcrl['cert'] = array();
638
				}
639
				$inuse = crl_in_use($tmpcrl['refid']);
640
?>
641
					<tr>
642
						<td><?=$tmpcrl['descr']; ?></td>
643
						<td><i class="fa fa-<?=($internal) ? "check" : "times"; ?>"></i></td>
644
						<td><?=($internal) ? count($tmpcrl['cert']) : "Unknown (imported)"; ?></td>
645
						<td><i class="fa fa-<?=($inuse) ? "check" : "times"; ?>"></i>
646
						<?php echo cert_usedby_description($tmpcrl['refid'], $certificates_used_by_packages); ?>
647
						</td>
648
						<td>
649
							<a href="system_crlmanager.php?act=exp&amp;id=<?=$tmpcrl['refid']?>" class="fa fa-download" title="<?=gettext("Export CRL")?>" ></a>
650
<?php
651
				if ($internal): ?>
652
							<a href="system_crlmanager.php?act=edit&amp;id=<?=$tmpcrl['refid']?>" class="fa fa-pencil" title="<?=gettext("Edit CRL")?>"></a>
653
<?php
654
				else:
655
?>
656
							<a href="system_crlmanager.php?act=editimported&amp;id=<?=$tmpcrl['refid']?>" class="fa fa-pencil" title="<?=gettext("Edit CRL")?>"></a>
657
<?php			endif;
658
				if (!$inuse):
659
?>
660
							<a href="system_crlmanager.php?act=del&amp;id=<?=$tmpcrl['refid']?>" class="fa fa-trash" title="<?=gettext("Delete CRL")?>" usepost></a>
661
<?php
662
				endif;
663
?>
664
						</td>
665
					</tr>
666
<?php
667
				$i++;
668
				endforeach;
669
			endif;
670
			$i++;
671
		endforeach;
672
?>
673
				</tbody>
674
			</table>
675
		</div>
676
	</div>
677

    
678

    
679
<?php
680
}
681
?>
682

    
683
<script type="text/javascript">
684
//<![CDATA[
685
events.push(function() {
686

    
687
	// Hides all elements of the specified class. This will usually be a section or group
688
	function hideClass(s_class, hide) {
689
		if (hide) {
690
			$('.' + s_class).hide();
691
		} else {
692
			$('.' + s_class).show();
693
		}
694
	}
695

    
696
	// When the 'method" selector is changed, we show/hide certain sections
697
	$('#method').on('change', function() {
698
		hideClass('internal', ($('#method').val() == 'existing'));
699
		hideClass('existing', ($('#method').val() == 'internal'));
700
	});
701

    
702
	hideClass('internal', ($('#method').val() == 'existing'));
703
	hideClass('existing', ($('#method').val() == 'internal'));
704
});
705
//]]>
706
</script>
707

    
708
<?php include("foot.inc");
(203-203/235)