Project

General

Profile

Download (41.3 KB) Statistics
| Branch: | Tag: | Revision:
1
<?php
2
/*
3
 * system_certmanager.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-2019 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-certmanager
27
##|*NAME=System: Certificate Manager
28
##|*DESCR=Allow access to the 'System: Certificate Manager' page.
29
##|*MATCH=system_certmanager.php*
30
##|-PRIV
31

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

    
36
$cert_methods = array(
37
	"internal" => gettext("Create an internal Certificate"),
38
	"import" => gettext("Import an existing Certificate"),
39
	"external" => gettext("Create a Certificate Signing Request"),
40
	"sign" => gettext("Sign a Certificate Signing Request")
41
);
42

    
43
$cert_keylens = array("1024", "2048", "3072", "4096", "6144", "7680", "8192", "15360", "16384");
44
$cert_types = array(
45
	"server" => "Server Certificate",
46
	"user" => "User Certificate");
47

    
48
global $cert_altname_types;
49
global $openssl_digest_algs;
50

    
51
if (isset($_REQUEST['userid']) && is_numericint($_REQUEST['userid'])) {
52
	$userid = $_REQUEST['userid'];
53
}
54

    
55
if (isset($userid)) {
56
	$cert_methods["existing"] = gettext("Choose an existing certificate");
57
	init_config_arr(array('system', 'user'));
58
	$a_user =& $config['system']['user'];
59
}
60

    
61
if (isset($_REQUEST['id']) && is_numericint($_REQUEST['id'])) {
62
	$id = $_REQUEST['id'];
63
}
64

    
65
init_config_arr(array('ca'));
66
$a_ca = &$config['ca'];
67

    
68
init_config_arr(array('cert'));
69
$a_cert = &$config['cert'];
70

    
71
$internal_ca_count = 0;
72
foreach ($a_ca as $ca) {
73
	if ($ca['prv']) {
74
		$internal_ca_count++;
75
	}
76
}
77

    
78
$act = $_REQUEST['act'];
79

    
80
if ($_POST['act'] == "del") {
81

    
82
	if (!isset($a_cert[$id])) {
83
		pfSenseHeader("system_certmanager.php");
84
		exit;
85
	}
86

    
87
	unset($a_cert[$id]);
88
	write_config();
89
	$savemsg = sprintf(gettext("Certificate %s successfully deleted."), htmlspecialchars($a_cert[$id]['descr']));
90
	pfSenseHeader("system_certmanager.php");
91
	exit;
92
}
93

    
94
if ($act == "new") {
95
	$pconfig['method'] = $_POST['method'];
96
	$pconfig['keylen'] = "2048";
97
	$pconfig['digest_alg'] = "sha256";
98
	$pconfig['csr_keylen'] = "2048";
99
	$pconfig['csr_digest_alg'] = "sha256";
100
	$pconfig['csrsign_digest_alg'] = "sha256";
101
	$pconfig['type'] = "user";
102
	$pconfig['lifetime'] = "3650";
103
}
104

    
105
if ($act == "exp") {
106

    
107
	if (!$a_cert[$id]) {
108
		pfSenseHeader("system_certmanager.php");
109
		exit;
110
	}
111

    
112
	$exp_name = urlencode("{$a_cert[$id]['descr']}.crt");
113
	$exp_data = base64_decode($a_cert[$id]['crt']);
114
	$exp_size = strlen($exp_data);
115

    
116
	header("Content-Type: application/octet-stream");
117
	header("Content-Disposition: attachment; filename={$exp_name}");
118
	header("Content-Length: $exp_size");
119
	echo $exp_data;
120
	exit;
121
}
122

    
123
if ($act == "req") {
124

    
125
	if (!$a_cert[$id]) {
126
		pfSenseHeader("system_certmanager.php");
127
		exit;
128
	}
129

    
130
	$exp_name = urlencode("{$a_cert[$id]['descr']}.req");
131
	$exp_data = base64_decode($a_cert[$id]['csr']);
132
	$exp_size = strlen($exp_data);
133

    
134
	header("Content-Type: application/octet-stream");
135
	header("Content-Disposition: attachment; filename={$exp_name}");
136
	header("Content-Length: $exp_size");
137
	echo $exp_data;
138
	exit;
139
}
140

    
141
if ($act == "key") {
142

    
143
	if (!$a_cert[$id]) {
144
		pfSenseHeader("system_certmanager.php");
145
		exit;
146
	}
147

    
148
	$exp_name = urlencode("{$a_cert[$id]['descr']}.key");
149
	$exp_data = base64_decode($a_cert[$id]['prv']);
150
	$exp_size = strlen($exp_data);
151

    
152
	header("Content-Type: application/octet-stream");
153
	header("Content-Disposition: attachment; filename={$exp_name}");
154
	header("Content-Length: $exp_size");
155
	echo $exp_data;
156
	exit;
157
}
158

    
159
if ($act == "p12") {
160
	if (!$a_cert[$id]) {
161
		pfSenseHeader("system_certmanager.php");
162
		exit;
163
	}
164

    
165
	$exp_name = urlencode("{$a_cert[$id]['descr']}.p12");
166
	$args = array();
167
	$args['friendly_name'] = $a_cert[$id]['descr'];
168

    
169
	$ca = lookup_ca($a_cert[$id]['caref']);
170

    
171
	if ($ca) {
172
		$args['extracerts'] = openssl_x509_read(base64_decode($ca['crt']));
173
	}
174

    
175
	$res_crt = openssl_x509_read(base64_decode($a_cert[$id]['crt']));
176
	$res_key = openssl_pkey_get_private(array(0 => base64_decode($a_cert[$id]['prv']) , 1 => ""));
177

    
178
	$exp_data = "";
179
	openssl_pkcs12_export($res_crt, $exp_data, $res_key, null, $args);
180
	$exp_size = strlen($exp_data);
181

    
182
	header("Content-Type: application/octet-stream");
183
	header("Content-Disposition: attachment; filename={$exp_name}");
184
	header("Content-Length: $exp_size");
185
	echo $exp_data;
186
	exit;
187
}
188

    
189
if ($act == "csr") {
190
	if (!$a_cert[$id]) {
191
		pfSenseHeader("system_certmanager.php");
192
		exit;
193
	}
194

    
195
	$pconfig['descr'] = $a_cert[$id]['descr'];
196
	$pconfig['csr'] = base64_decode($a_cert[$id]['csr']);
197
}
198

    
199
if ($_POST['save']) {
200

    
201
	if ($_POST['save'] == gettext("Save")) {
202
		$input_errors = array();
203
		$pconfig = $_POST;
204

    
205
		/* input validation */
206
		if ($pconfig['method'] == "sign") {
207
			$reqdfields = explode(" ",
208
				"descr catosignwith");
209
			$reqdfieldsn = array(
210
				gettext("Descriptive name"),
211
				gettext("CA to sign with"));
212

    
213
			if (($_POST['csrtosign'] === "new") &&
214
			    ((!strstr($_POST['csrpaste'], "BEGIN CERTIFICATE REQUEST") || !strstr($_POST['csrpaste'], "END CERTIFICATE REQUEST")) &&
215
			    (!strstr($_POST['csrpaste'], "BEGIN NEW CERTIFICATE REQUEST") || !strstr($_POST['csrpaste'], "END NEW CERTIFICATE REQUEST")))) {
216
				$input_errors[] = gettext("This signing request does not appear to be valid.");
217
			}
218

    
219
			if ( (($_POST['csrtosign'] === "new") && (strlen($_POST['keypaste']) > 0)) && (!strstr($_POST['keypaste'], "BEGIN PRIVATE KEY") || !strstr($_POST['keypaste'], "END PRIVATE KEY"))) {
220
				$input_errors[] = gettext("This private does not appear to be valid.");
221
				$input_errors[] = gettext("Key data field should be blank, or a valid x509 private key");
222
			}
223

    
224
		}
225

    
226
		if ($pconfig['method'] == "import") {
227
			$reqdfields = explode(" ",
228
				"descr cert key");
229
			$reqdfieldsn = array(
230
				gettext("Descriptive name"),
231
				gettext("Certificate data"),
232
				gettext("Key data"));
233
			if ($_POST['cert'] && (!strstr($_POST['cert'], "BEGIN CERTIFICATE") || !strstr($_POST['cert'], "END CERTIFICATE"))) {
234
				$input_errors[] = gettext("This certificate does not appear to be valid.");
235
			}
236

    
237
			if (cert_get_publickey($_POST['cert'], false) != cert_get_publickey($_POST['key'], false, 'prv')) {
238
				$input_errors[] = gettext("The submitted private key does not match the submitted certificate data.");
239
			}
240
		}
241

    
242
		if ($pconfig['method'] == "internal") {
243
			$reqdfields = explode(" ",
244
				"descr caref keylen type lifetime dn_commonname");
245
			$reqdfieldsn = array(
246
				gettext("Descriptive name"),
247
				gettext("Certificate authority"),
248
				gettext("Key length"),
249
				gettext("Certificate Type"),
250
				gettext("Lifetime"),
251
				gettext("Distinguished name Common Name"));
252
		}
253

    
254
		if ($pconfig['method'] == "external") {
255
			$reqdfields = explode(" ",
256
				"descr csr_keylen csr_dn_commonname");
257
			$reqdfieldsn = array(
258
				gettext("Descriptive name"),
259
				gettext("Key length"),
260
				gettext("Distinguished name Common Name"));
261
		}
262

    
263
		if ($pconfig['method'] == "existing") {
264
			$reqdfields = array("certref");
265
			$reqdfieldsn = array(gettext("Existing Certificate Choice"));
266
		}
267

    
268
		$altnames = array();
269
		do_input_validation($_POST, $reqdfields, $reqdfieldsn, $input_errors);
270

    
271
		if ($pconfig['method'] != "import" && $pconfig['method'] != "existing") {
272
			/* subjectAltNames */
273
			$san_typevar = 'altname_type';
274
			$san_valuevar = 'altname_value';
275
			// This is just the blank alternate name that is added for display purposes. We don't want to validate/save it
276
			if ($_POST["{$san_valuevar}0"] == "") {
277
				unset($_POST["{$san_typevar}0"]);
278
				unset($_POST["{$san_valuevar}0"]);
279
			}
280
			foreach ($_POST as $key => $value) {
281
				$entry = '';
282
				if (!substr_compare($san_typevar, $key, 0, strlen($san_typevar))) {
283
					$entry = substr($key, strlen($san_typevar));
284
					$field = 'type';
285
				} elseif (!substr_compare($san_valuevar, $key, 0, strlen($san_valuevar))) {
286
					$entry = substr($key, strlen($san_valuevar));
287
					$field = 'value';
288
				}
289

    
290
				if (ctype_digit($entry)) {
291
					$entry++;	// Pre-bootstrap code is one-indexed, but the bootstrap code is 0-indexed
292
					$altnames[$entry][$field] = $value;
293
				}
294
			}
295

    
296
			$pconfig['altnames']['item'] = $altnames;
297

    
298
			/* Input validation for subjectAltNames */
299
			foreach ($altnames as $idx => $altname) {
300
				switch ($altname['type']) {
301
					case "DNS":
302
						if (!is_hostname($altname['value'], true) || is_ipaddr($altname['value'])) {
303
							array_push($input_errors, "DNS subjectAltName values must be valid hostnames, FQDNs or wildcard domains.");
304
						}
305
						break;
306
					case "IP":
307
						if (!is_ipaddr($altname['value'])) {
308
							array_push($input_errors, "IP subjectAltName values must be valid IP Addresses");
309
						}
310
						break;
311
					case "email":
312
						if (empty($altname['value'])) {
313
							array_push($input_errors, "An e-mail address must be provided for this type of subjectAltName");
314
						}
315
						if (preg_match("/[\!\#\$\%\^\(\)\~\?\>\<\&\/\\\,\"\']/", $altname['value'])) {
316
							array_push($input_errors, "The e-mail provided in a subjectAltName contains invalid characters.");
317
						}
318
						break;
319
					case "URI":
320
						/* Close enough? */
321
						if (!is_URL($altname['value'])) {
322
							$input_errors[] = "URI subjectAltName types must be a valid URI";
323
						}
324
						break;
325
					default:
326
						$input_errors[] = "Unrecognized subjectAltName type.";
327
				}
328
			}
329

    
330
			/* Make sure we do not have invalid characters in the fields for the certificate */
331

    
332
			if (preg_match("/[\?\>\<\&\/\\\"\']/", $_POST['descr'])) {
333
				array_push($input_errors, "The field 'Descriptive Name' contains invalid characters.");
334
			}
335

    
336
			switch ($pconfig['method']) {
337
				case "internal":
338
					if (isset($_POST["keylen"]) && !in_array($_POST["keylen"], $cert_keylens)) {
339
						array_push($input_errors, gettext("Please select a valid Key Length."));
340
					}
341
					if (!in_array($_POST["digest_alg"], $openssl_digest_algs)) {
342
						array_push($input_errors, gettext("Please select a valid Digest Algorithm."));
343
					}
344
					break;
345
				case "external":
346
					if (isset($_POST["csr_keylen"]) && !in_array($_POST["csr_keylen"], $cert_keylens)) {
347
						array_push($input_errors, gettext("Please select a valid Key Length."));
348
					}
349
					if (!in_array($_POST["csr_digest_alg"], $openssl_digest_algs)) {
350
						array_push($input_errors, gettext("Please select a valid Digest Algorithm."));
351
					}
352
					break;
353
				case "sign":
354
					if (!in_array($_POST["csrsign_digest_alg"], $openssl_digest_algs)) {
355
						array_push($input_errors, gettext("Please select a valid Digest Algorithm."));
356
					}
357
					break;
358
				default:
359
					break;
360
			}
361
		}
362

    
363
		/* save modifications */
364
		if (!$input_errors) {
365

    
366
			if ($pconfig['method'] == "existing") {
367
				$cert = lookup_cert($pconfig['certref']);
368
				if ($cert && $a_user) {
369
					$a_user[$userid]['cert'][] = $cert['refid'];
370
				}
371
			} else if ($pconfig['method'] == "sign") { // Sign a CSR
372
				$csrid = lookup_cert($pconfig['csrtosign']);
373
				$ca = & lookup_ca($pconfig['catosignwith']);
374

    
375
				// Read the CSR from $config, or if a new one, from the textarea
376
				if ($pconfig['csrtosign'] === "new") {
377
					$csr = $pconfig['csrpaste'];
378
				} else {
379
					$csr = base64_decode($csrid['csr']);
380
				}
381
				if (count($altnames)) {
382
					foreach ($altnames as $altname) {
383
						$altnames_tmp[] = "{$altname['type']}:" . cert_escape_x509_chars($altname['value']);
384
					}
385
					$altname_str = implode(",", $altnames_tmp);
386
				}
387

    
388
				$n509 = csr_sign($csr, $ca, $pconfig['csrsign_lifetime'], $pconfig['type'], $altname_str, $pconfig['csrsign_digest_alg']);
389

    
390
				if ($n509) {
391
					// Gather the details required to save the new cert
392
					$newcert = array();
393
					$newcert['refid'] = uniqid();
394
					$newcert['caref'] = $pconfig['catosignwith'];
395
					$newcert['descr'] = $pconfig['descr'];
396
					$newcert['type'] = $pconfig['type'];
397
					$newcert['crt'] = base64_encode($n509);
398

    
399
					if ($pconfig['csrtosign'] === "new") {
400
						$newcert['prv'] = base64_encode($pconfig['keypaste']);
401
					} else {
402
						$newcert['prv'] = $csrid['prv'];
403
					}
404

    
405
					// Add it to the config file
406
					$config['cert'][] = $newcert;
407
				}
408

    
409
			} else {
410
				$cert = array();
411
				$cert['refid'] = uniqid();
412
				if (isset($id) && $a_cert[$id]) {
413
					$cert = $a_cert[$id];
414
				}
415

    
416
				$cert['descr'] = $pconfig['descr'];
417

    
418
				$old_err_level = error_reporting(0); /* otherwise openssl_ functions throw warnings directly to a page screwing menu tab */
419

    
420
				if ($pconfig['method'] == "import") {
421
					cert_import($cert, $pconfig['cert'], $pconfig['key']);
422
				}
423

    
424
				if ($pconfig['method'] == "internal") {
425
					$dn = array('commonName' => cert_escape_x509_chars($pconfig['dn_commonname']));
426
					if (!empty($pconfig['dn_country'])) {
427
						$dn['countryName'] = $pconfig['dn_country'];
428
					}
429
					if (!empty($pconfig['dn_state'])) {
430
						$dn['stateOrProvinceName'] = cert_escape_x509_chars($pconfig['dn_state']);
431
					}
432
					if (!empty($pconfig['dn_city'])) {
433
						$dn['localityName'] = cert_escape_x509_chars($pconfig['dn_city']);
434
					}
435
					if (!empty($pconfig['dn_organization'])) {
436
						$dn['organizationName'] = cert_escape_x509_chars($pconfig['dn_organization']);
437
					}
438
					if (!empty($pconfig['dn_organizationalunit'])) {
439
						$dn['organizationalUnitName'] = cert_escape_x509_chars($pconfig['dn_organizationalunit']);
440
					}
441

    
442
					$altnames_tmp = array();
443
					$cn_altname = cert_add_altname_type($pconfig['dn_commonname']);
444
					if (!empty($cn_altname)) {
445
						$altnames_tmp[] = $cn_altname;
446
					}
447
					if (count($altnames)) {
448
						foreach ($altnames as $altname) {
449
							// The CN is added as a SAN automatically, do not add it again.
450
							if ($altname['value'] != $pconfig['dn_commonname']) {
451
								$altnames_tmp[] = "{$altname['type']}:" . cert_escape_x509_chars($altname['value']);
452
							}
453
						}
454
					}
455
					if (!empty($altnames_tmp)) {
456
						$dn['subjectAltName'] = implode(",", $altnames_tmp);
457
					}
458

    
459
					if (!cert_create($cert, $pconfig['caref'], $pconfig['keylen'], $pconfig['lifetime'], $dn, $pconfig['type'], $pconfig['digest_alg'])) {
460
						$input_errors = array();
461
						while ($ssl_err = openssl_error_string()) {
462
							if (strpos($ssl_err, 'NCONF_get_string:no value') === false) {
463
								array_push($input_errors, "openssl library returns: " . $ssl_err);
464
							}
465
						}
466
					}
467
				}
468

    
469
				if ($pconfig['method'] == "external") {
470
					$dn = array('commonName' => cert_escape_x509_chars($pconfig['csr_dn_commonname']));
471
					if (!empty($pconfig['csr_dn_country'])) {
472
						$dn['countryName'] = $pconfig['csr_dn_country'];
473
					}
474
					if (!empty($pconfig['csr_dn_state'])) {
475
						$dn['stateOrProvinceName'] = cert_escape_x509_chars($pconfig['csr_dn_state']);
476
					}
477
					if (!empty($pconfig['csr_dn_city'])) {
478
						$dn['localityName'] = cert_escape_x509_chars($pconfig['csr_dn_city']);
479
					}
480
					if (!empty($pconfig['csr_dn_organization'])) {
481
						$dn['organizationName'] = cert_escape_x509_chars($pconfig['csr_dn_organization']);
482
					}
483
					if (!empty($pconfig['csr_dn_organizationalunit'])) {
484
						$dn['organizationalUnitName'] = cert_escape_x509_chars($pconfig['csr_dn_organizationalunit']);
485
					}
486

    
487
					$altnames_tmp = array();
488
					$cn_altname = cert_add_altname_type($pconfig['csr_dn_commonname']);
489
					if (!empty($cn_altname)) {
490
						$altnames_tmp[] = $cn_altname;
491
					}
492
					if (count($altnames)) {
493
						foreach ($altnames as $altname) {
494
							// The CN is added as a SAN automatically, do not add it again.
495
							if ($altname['value'] != $pconfig['csr_dn_commonname']) {
496
								$altnames_tmp[] = "{$altname['type']}:" . cert_escape_x509_chars($altname['value']);
497
							}
498
						}
499
					}
500
					if (!empty($altnames_tmp)) {
501
						$dn['subjectAltName'] = implode(",", $altnames_tmp);
502
					}
503

    
504
					if (!csr_generate($cert, $pconfig['csr_keylen'], $dn, $pconfig['type'], $pconfig['csr_digest_alg'])) {
505
						$input_errors = array();
506
						while ($ssl_err = openssl_error_string()) {
507
							if (strpos($ssl_err, 'NCONF_get_string:no value') === false) {
508
								array_push($input_errors, "openssl library returns: " . $ssl_err);
509
							}
510
						}
511
					}
512
				}
513

    
514
				error_reporting($old_err_level);
515

    
516
				if (isset($id) && $a_cert[$id]) {
517
					$a_cert[$id] = $cert;
518
				} else {
519
					$a_cert[] = $cert;
520
				}
521

    
522
				if (isset($a_user) && isset($userid)) {
523
					$a_user[$userid]['cert'][] = $cert['refid'];
524
				}
525
			}
526

    
527
			if (!$input_errors) {
528
				write_config();
529
			}
530

    
531
			if ((isset($userid) && is_numeric($userid)) && !$input_errors) {
532
				post_redirect("system_usermanager.php", array('act' => 'edit', 'userid' => $userid));
533
				exit;
534
			}
535
		}
536
	}
537

    
538
	if ($_POST['save'] == gettext("Update")) {
539
		unset($input_errors);
540
		$pconfig = $_POST;
541

    
542
		/* input validation */
543
		$reqdfields = explode(" ", "descr cert");
544
		$reqdfieldsn = array(
545
			gettext("Descriptive name"),
546
			gettext("Final Certificate data"));
547

    
548
		do_input_validation($_POST, $reqdfields, $reqdfieldsn, $input_errors);
549

    
550
		if (preg_match("/[\?\>\<\&\/\\\"\']/", $_POST['descr'])) {
551
			array_push($input_errors, "The field 'Descriptive Name' contains invalid characters.");
552
		}
553

    
554
//		old way
555
		/* make sure this csr and certificate subjects match */
556
//		$subj_csr = csr_get_subject($pconfig['csr'], false);
557
//		$subj_cert = cert_get_subject($pconfig['cert'], false);
558
//
559
//		if (!isset($_POST['ignoresubjectmismatch']) && !($_POST['ignoresubjectmismatch'] == "yes")) {
560
//			if (strcmp($subj_csr, $subj_cert)) {
561
//				$input_errors[] = sprintf(gettext("The certificate subject '%s' does not match the signing request subject."), $subj_cert);
562
//				$subject_mismatch = true;
563
//			}
564
//		}
565
		$mod_csr = cert_get_publickey($pconfig['csr'], false, 'csr');
566
		$mod_cert = cert_get_publickey($pconfig['cert'], false);
567

    
568
		if (strcmp($mod_csr, $mod_cert)) {
569
			// simply: if the moduli don't match, then the private key and public key won't match
570
			$input_errors[] = sprintf(gettext("The certificate public key does not match the signing request public key."), $subj_cert);
571
			$subject_mismatch = true;
572
		}
573

    
574
		/* save modifications */
575
		if (!$input_errors) {
576

    
577
			$cert = $a_cert[$id];
578

    
579
			$cert['descr'] = $pconfig['descr'];
580

    
581
			csr_complete($cert, $pconfig['cert']);
582

    
583
			$a_cert[$id] = $cert;
584

    
585
			write_config();
586

    
587
			pfSenseHeader("system_certmanager.php");
588
		}
589
	}
590
}
591

    
592
$pgtitle = array(gettext("System"), gettext("Certificate Manager"), gettext("Certificates"));
593
$pglinks = array("", "system_camanager.php", "system_certmanager.php");
594

    
595
if (($act == "new" || ($_POST['save'] == gettext("Save") && $input_errors)) || ($act == "csr" || ($_POST['save'] == gettext("Update") && $input_errors))) {
596
	$pgtitle[] = gettext('Edit');
597
	$pglinks[] = "@self";
598
}
599
include("head.inc");
600

    
601
if ($input_errors) {
602
	print_input_errors($input_errors);
603
}
604

    
605
if ($savemsg) {
606
	print_info_box($savemsg, 'success');
607
}
608

    
609
$tab_array = array();
610
$tab_array[] = array(gettext("CAs"), false, "system_camanager.php");
611
$tab_array[] = array(gettext("Certificates"), true, "system_certmanager.php");
612
$tab_array[] = array(gettext("Certificate Revocation"), false, "system_crlmanager.php");
613
display_top_tabs($tab_array);
614

    
615
if ($act == "new" || (($_POST['save'] == gettext("Save")) && $input_errors)) {
616
	$form = new Form();
617
	$form->setAction('system_certmanager.php?act=edit');
618

    
619
	if (isset($userid) && $a_user) {
620
		$form->addGlobal(new Form_Input(
621
			'userid',
622
			null,
623
			'hidden',
624
			$userid
625
		));
626
	}
627

    
628
	if (isset($id) && $a_cert[$id]) {
629
		$form->addGlobal(new Form_Input(
630
			'id',
631
			null,
632
			'hidden',
633
			$id
634
		));
635
	}
636

    
637
	$section = new Form_Section('Add/Sign a New Certificate');
638

    
639
	if (!isset($id)) {
640
		$section->addInput(new Form_Select(
641
			'method',
642
			'*Method',
643
			$pconfig['method'],
644
			$cert_methods
645
		))->toggles();
646
	}
647

    
648
	$section->addInput(new Form_Input(
649
		'descr',
650
		'*Descriptive name',
651
		'text',
652
		($a_user && empty($pconfig['descr'])) ? $a_user[$userid]['name'] : $pconfig['descr']
653
	))->addClass('toggle-existing');
654

    
655
	$form->add($section);
656

    
657
	// Return an array containing the IDs od all CAs
658
	function list_cas() {
659
		global $a_ca;
660
		$allCas = array();
661

    
662
		foreach ($a_ca as $ca) {
663
			if ($ca['prv']) {
664
				$allCas[$ca['refid']] = $ca['descr'];
665
			}
666
		}
667

    
668
		return $allCas;
669
	}
670

    
671
	// Return an array containing the IDs od all CSRs
672
	function list_csrs() {
673
		global $config;
674
		$allCsrs = array();
675

    
676
		foreach ($config['cert'] as $cert) {
677
			if ($cert['csr']) {
678
				$allCsrs[$cert['refid']] = $cert['descr'];
679
			}
680
		}
681

    
682
		return ['new' => gettext('New CSR (Paste below)')] + $allCsrs;
683
	}
684

    
685
	$section = new Form_Section('Sign CSR');
686
	$section->addClass('toggle-sign collapse');
687

    
688
	$section->AddInput(new Form_Select(
689
		'catosignwith',
690
		'*CA to sign with',
691
		$pconfig['catosignwith'],
692
		list_cas()
693
	));
694

    
695
	$section->AddInput(new Form_Select(
696
		'csrtosign',
697
		'*CSR to sign',
698
		isset($pconfig['csrtosign']) ? $pconfig['csrtosign'] : 'new',
699
		list_csrs()
700
	));
701

    
702
	$section->addInput(new Form_Textarea(
703
		'csrpaste',
704
		'CSR data',
705
		$pconfig['csrpaste']
706
	))->setHelp('Paste a Certificate Signing Request in X.509 PEM format here.');
707

    
708
	$section->addInput(new Form_Textarea(
709
		'keypaste',
710
		'Key data',
711
		$pconfig['keypaste']
712
	))->setHelp('Optionally paste a private key here. The key will be associated with the newly signed certificate in pfSense');
713

    
714
	$section->addInput(new Form_Input(
715
		'csrsign_lifetime',
716
		'*Certificate Lifetime (days)',
717
		'number',
718
		$pconfig['csrsign_lifetime'] ? $pconfig['csrsign_lifetime']:'3650'
719
	));
720
	$section->addInput(new Form_Select(
721
		'csrsign_digest_alg',
722
		'*Digest Algorithm',
723
		$pconfig['csrsign_digest_alg'],
724
		array_combine($openssl_digest_algs, $openssl_digest_algs)
725
	))->setHelp('NOTE: It is recommended to use an algorithm stronger than '.
726
		'SHA1 when possible');
727

    
728
	$form->add($section);
729

    
730
	$section = new Form_Section('Import Certificate');
731
	$section->addClass('toggle-import collapse');
732

    
733
	$section->addInput(new Form_Textarea(
734
		'cert',
735
		'*Certificate data',
736
		$pconfig['cert']
737
	))->setHelp('Paste a certificate in X.509 PEM format here.');
738

    
739
	$section->addInput(new Form_Textarea(
740
		'key',
741
		'*Private key data',
742
		$pconfig['key']
743
	))->setHelp('Paste a private key in X.509 PEM format here.');
744

    
745
	$form->add($section);
746
	$section = new Form_Section('Internal Certificate');
747
	$section->addClass('toggle-internal collapse');
748

    
749
	if (!$internal_ca_count) {
750
		$section->addInput(new Form_StaticText(
751
			'*Certificate authority',
752
			gettext('No internal Certificate Authorities have been defined. ') .
753
			gettext('An internal CA must be defined in order to create an internal certificate. ') .
754
			sprintf(gettext('%1$sCreate%2$s an internal CA.'), '<a href="system_camanager.php?act=new&amp;method=internal"> ', '</a>')
755
		));
756
	} else {
757
		$allCas = array();
758
		foreach ($a_ca as $ca) {
759
			if (!$ca['prv']) {
760
				continue;
761
			}
762

    
763
			$allCas[ $ca['refid'] ] = $ca['descr'];
764
		}
765

    
766
		$section->addInput(new Form_Select(
767
			'caref',
768
			'*Certificate authority',
769
			$pconfig['caref'],
770
			$allCas
771
		));
772
	}
773

    
774
	$section->addInput(new Form_Select(
775
		'keylen',
776
		'*Key length',
777
		$pconfig['keylen'],
778
		array_combine($cert_keylens, $cert_keylens)
779
	));
780

    
781
	$section->addInput(new Form_Select(
782
		'digest_alg',
783
		'*Digest Algorithm',
784
		$pconfig['digest_alg'],
785
		array_combine($openssl_digest_algs, $openssl_digest_algs)
786
	))->setHelp('NOTE: It is recommended to use an algorithm stronger than '.
787
		'SHA1 when possible.');
788

    
789
	$section->addInput(new Form_Input(
790
		'lifetime',
791
		'*Lifetime (days)',
792
		'number',
793
		$pconfig['lifetime']
794
	));
795

    
796
	$section->addInput(new Form_Input(
797
		'dn_commonname',
798
		'*Common Name',
799
		'text',
800
		$pconfig['dn_commonname'],
801
		['placeholder' => 'e.g. www.example.com']
802
	));
803

    
804
	$section->addInput(new Form_StaticText(
805
		null,
806
		gettext('The following certificate subject components are optional and may be left blank.')
807
	));
808

    
809
	$section->addInput(new Form_Select(
810
		'dn_country',
811
		'Country Code',
812
		$pconfig['dn_country'],
813
		get_cert_country_codes()
814
	));
815

    
816
	$section->addInput(new Form_Input(
817
		'dn_state',
818
		'State or Province',
819
		'text',
820
		$pconfig['dn_state'],
821
		['placeholder' => 'e.g. Texas']
822
	));
823

    
824
	$section->addInput(new Form_Input(
825
		'dn_city',
826
		'City',
827
		'text',
828
		$pconfig['dn_city'],
829
		['placeholder' => 'e.g. Austin']
830
	));
831

    
832
	$section->addInput(new Form_Input(
833
		'dn_organization',
834
		'Organization',
835
		'text',
836
		$pconfig['dn_organization'],
837
		['placeholder' => 'e.g. My Company Inc']
838
	));
839

    
840
	$section->addInput(new Form_Input(
841
		'dn_organizationalunit',
842
		'Organizational Unit',
843
		'text',
844
		$pconfig['dn_organizationalunit'],
845
		['placeholder' => 'e.g. My Department Name (optional)']
846
	));
847

    
848
	$form->add($section);
849
	$section = new Form_Section('External Signing Request');
850
	$section->addClass('toggle-external collapse');
851

    
852
	$section->addInput(new Form_Select(
853
		'csr_keylen',
854
		'*Key length',
855
		$pconfig['csr_keylen'],
856
		array_combine($cert_keylens, $cert_keylens)
857
	));
858

    
859
	$section->addInput(new Form_Select(
860
		'csr_digest_alg',
861
		'*Digest Algorithm',
862
		$pconfig['csr_digest_alg'],
863
		array_combine($openssl_digest_algs, $openssl_digest_algs)
864
	))->setHelp('NOTE: It is recommended to use an algorithm stronger than '.
865
		'SHA1 when possible');
866

    
867
	$section->addInput(new Form_Input(
868
		'csr_dn_commonname',
869
		'*Common Name',
870
		'text',
871
		$pconfig['csr_dn_commonname'],
872
		['placeholder' => 'e.g. internal-ca']
873
	));
874

    
875
	$section->addInput(new Form_StaticText(
876
		null,
877
		gettext('The following certificate subject components are optional and may be left blank.')
878
	));
879

    
880
	$section->addInput(new Form_Select(
881
		'csr_dn_country',
882
		'Country Code',
883
		$pconfig['csr_dn_country'],
884
		get_cert_country_codes()
885
	));
886

    
887
	$section->addInput(new Form_Input(
888
		'csr_dn_state',
889
		'State or Province',
890
		'text',
891
		$pconfig['csr_dn_state'],
892
		['placeholder' => 'e.g. Texas']
893
	));
894

    
895
	$section->addInput(new Form_Input(
896
		'csr_dn_city',
897
		'City',
898
		'text',
899
		$pconfig['csr_dn_city'],
900
		['placeholder' => 'e.g. Austin']
901
	));
902

    
903
	$section->addInput(new Form_Input(
904
		'csr_dn_organization',
905
		'Organization',
906
		'text',
907
		$pconfig['csr_dn_organization'],
908
		['placeholder' => 'e.g. My Company Inc']
909
	));
910

    
911
	$section->addInput(new Form_Input(
912
		'csr_dn_organizationalunit',
913
		'Organizational Unit',
914
		'text',
915
		$pconfig['csr_dn_organizationalunit'],
916
		['placeholder' => 'e.g. My Department Name (optional)']
917
	));
918

    
919
	$form->add($section);
920
	$section = new Form_Section('Choose an Existing Certificate');
921
	$section->addClass('toggle-existing collapse');
922

    
923
	$existCerts = array();
924

    
925
	foreach ($config['cert'] as $cert) {
926
		if (!is_array($cert) || empty($cert)) {
927
			continue;
928
		}
929
		if (is_array($config['system']['user'][$userid]['cert'])) { // Could be MIA!
930
			if (isset($userid) && in_array($cert['refid'], $config['system']['user'][$userid]['cert'])) {
931
				continue;
932
			}
933
		}
934

    
935
		$ca = lookup_ca($cert['caref']);
936
		if ($ca) {
937
			$cert['descr'] .= " (CA: {$ca['descr']})";
938
		}
939

    
940
		if (cert_in_use($cert['refid'])) {
941
			$cert['descr'] .= " (In Use)";
942
		}
943
		if (is_cert_revoked($cert)) {
944
			$cert['descr'] .= " (Revoked)";
945
		}
946

    
947
		$existCerts[ $cert['refid'] ] = $cert['descr'];
948
	}
949

    
950
	$section->addInput(new Form_Select(
951
		'certref',
952
		'*Existing Certificates',
953
		$pconfig['certref'],
954
		$existCerts
955
	));
956

    
957
	$form->add($section);
958

    
959
	$section = new Form_Section('Certificate Attributes');
960
	$section->addClass('toggle-external toggle-internal toggle-sign collapse');
961

    
962
	$section->addInput(new Form_StaticText(
963
		gettext('Attribute Notes'),
964
		'<span class="help-block">'.
965
		gettext('The following attributes are added to certificates and ' .
966
		'requests when they are created or signed. These attributes behave ' .
967
		'differently depending on the selected mode.') .
968
		'<br/><br/>' .
969
		'<span class="toggle-internal collapse">' . gettext('For Internal Certificates, these attributes are added directly to the certificate as shown.') . '</span>' .
970
		'<span class="toggle-external collapse">' .
971
		gettext('For Certificate Signing Requests, These attributes are added to the request but they may be ignored or changed by the CA that signs the request. ') .
972
		'<br/><br/>' .
973
		gettext('If this CSR will be signed using the Certificate Manager on this firewall, set the attributes when signing instead as they cannot be carried over.') . '</span>' .
974
		'<span class="toggle-sign collapse">' . gettext('When Signing a Certificate Request, existing attributes in the request cannot be copied. The attributes below will be applied to the resulting certificate.') . '</span>' .
975
		'</span>'
976
	));
977

    
978
	$section->addInput(new Form_Select(
979
		'type',
980
		'*Certificate Type',
981
		$pconfig['type'],
982
		$cert_types
983
	))->setHelp('Add type-specific usage attributes to the signed certificate.' .
984
		' Used for placing usage restrictions on, or granting abilities to, ' .
985
		'the signed certificate.');
986

    
987
	if (empty($pconfig['altnames']['item'])) {
988
		$pconfig['altnames']['item'] = array(
989
			array('type' => null, 'value' => null)
990
		);
991
	}
992

    
993
	$counter = 0;
994
	$numrows = count($pconfig['altnames']['item']) - 1;
995

    
996
	foreach ($pconfig['altnames']['item'] as $item) {
997

    
998
		$group = new Form_Group($counter == 0 ? 'Alternative Names':'');
999

    
1000
		$group->add(new Form_Select(
1001
			'altname_type' . $counter,
1002
			'Type',
1003
			$item['type'],
1004
			$cert_altname_types
1005
		))->setHelp(($counter == $numrows) ? 'Type':null);
1006

    
1007
		$group->add(new Form_Input(
1008
			'altname_value' . $counter,
1009
			null,
1010
			'text',
1011
			$item['value']
1012
		))->setHelp(($counter == $numrows) ? 'Value':null);
1013

    
1014
		$group->add(new Form_Button(
1015
			'deleterow' . $counter,
1016
			'Delete',
1017
			null,
1018
			'fa-trash'
1019
		))->addClass('btn-warning');
1020

    
1021
		$group->addClass('repeatable');
1022

    
1023
		$group->setHelp('Enter additional identifiers for the certificate ' .
1024
			'in this list. The Common Name field is automatically ' .
1025
			'added to the certificate as an Alternative Name. ' .
1026
			'The signing CA may ignore or change these values.');
1027

    
1028
		$section->add($group);
1029

    
1030
		$counter++;
1031
	}
1032

    
1033
	$section->addInput(new Form_Button(
1034
		'addrow',
1035
		'Add',
1036
		null,
1037
		'fa-plus'
1038
	))->addClass('btn-success');
1039

    
1040
	$form->add($section);
1041

    
1042

    
1043
	print $form;
1044

    
1045
} else if ($act == "csr" || (($_POST['save'] == gettext("Update")) && $input_errors)) {
1046
	$form = new Form(false);
1047
	$form->setAction('system_certmanager.php?act=csr');
1048

    
1049
	$section = new Form_Section("Complete Signing Request for " . $pconfig['descr']);
1050

    
1051
	$section->addInput(new Form_Input(
1052
		'descr',
1053
		'*Descriptive name',
1054
		'text',
1055
		$pconfig['descr']
1056
	));
1057

    
1058
	$section->addInput(new Form_Textarea(
1059
		'csr',
1060
		'Signing request data',
1061
		$pconfig['csr']
1062
	))->setReadonly()
1063
	  ->setWidth(7)
1064
	  ->setHelp('Copy the certificate signing data from here and forward it to a certificate authority for signing.');
1065

    
1066
	$section->addInput(new Form_Textarea(
1067
		'cert',
1068
		'*Final certificate data',
1069
		$pconfig['cert']
1070
	))->setWidth(7)
1071
	  ->setHelp('Paste the certificate received from the certificate authority here.');
1072

    
1073
	if (isset($id) && $a_cert[$id]) {
1074
		$form->addGlobal(new Form_Input(
1075
			'id',
1076
			null,
1077
			'hidden',
1078
			$id
1079
		));
1080

    
1081
		$form->addGlobal(new Form_Input(
1082
			'act',
1083
			null,
1084
			'hidden',
1085
			'csr'
1086
		));
1087
	}
1088

    
1089
	$form->add($section);
1090

    
1091
	$form->addGlobal(new Form_Button(
1092
		'save',
1093
		'Update',
1094
		null,
1095
		'fa-save'
1096
	))->addClass('btn-primary');
1097

    
1098
	print($form);
1099
} else {
1100
?>
1101
<div class="panel panel-default" id="search-panel">
1102
	<div class="panel-heading">
1103
		<h2 class="panel-title">
1104
			<?=gettext('Search')?>
1105
			<span class="widget-heading-icon pull-right">
1106
				<a data-toggle="collapse" href="#search-panel_panel-body">
1107
					<i class="fa fa-plus-circle"></i>
1108
				</a>
1109
			</span>
1110
		</h2>
1111
	</div>
1112
	<div id="search-panel_panel-body" class="panel-body collapse in">
1113
		<div class="form-group">
1114
			<label class="col-sm-2 control-label">
1115
				<?=gettext("Search term")?>
1116
			</label>
1117
			<div class="col-sm-5"><input class="form-control" name="searchstr" id="searchstr" type="text"/></div>
1118
			<div class="col-sm-2">
1119
				<select id="where" class="form-control">
1120
					<option value="0"><?=gettext("Name")?></option>
1121
					<option value="1"><?=gettext("Distinguished Name")?></option>
1122
					<option value="2" selected><?=gettext("Both")?></option>
1123
				</select>
1124
			</div>
1125
			<div class="col-sm-3">
1126
				<a id="btnsearch" title="<?=gettext("Search")?>" class="btn btn-primary btn-sm"><i class="fa fa-search icon-embed-btn"></i><?=gettext("Search")?></a>
1127
				<a id="btnclear" title="<?=gettext("Clear")?>" class="btn btn-info btn-sm"><i class="fa fa-undo icon-embed-btn"></i><?=gettext("Clear")?></a>
1128
			</div>
1129
			<div class="col-sm-10 col-sm-offset-2">
1130
				<span class="help-block"><?=gettext('Enter a search string or *nix regular expression to search certificate names and distinguished names.')?></span>
1131
			</div>
1132
		</div>
1133
	</div>
1134
</div>
1135
<div class="panel panel-default">
1136
	<div class="panel-heading"><h2 class="panel-title"><?=gettext('Certificates')?></h2></div>
1137
	<div class="panel-body">
1138
		<div class="table-responsive">
1139
		<table class="table table-striped table-hover sortable-theme-bootstrap" data-sortable>
1140
			<thead>
1141
				<tr>
1142
					<th><?=gettext("Name")?></th>
1143
					<th><?=gettext("Issuer")?></th>
1144
					<th><?=gettext("Distinguished Name")?></th>
1145
					<th><?=gettext("In Use")?></th>
1146

    
1147
					<th class="col-sm-2"><?=gettext("Actions")?></th>
1148
				</tr>
1149
			</thead>
1150
			<tbody>
1151
<?php
1152

    
1153
$pluginparams = array();
1154
$pluginparams['type'] = 'certificates';
1155
$pluginparams['event'] = 'used_certificates';
1156
$certificates_used_by_packages = pkg_call_plugins('plugin_certificates', $pluginparams);
1157
$i = 0;
1158
foreach ($a_cert as $i => $cert):
1159
	if (!is_array($cert) || empty($cert)) {
1160
		continue;
1161
	}
1162
	$name = htmlspecialchars($cert['descr']);
1163
	$sans = array();
1164
	if ($cert['crt']) {
1165
		$subj = cert_get_subject($cert['crt']);
1166
		$issuer = cert_get_issuer($cert['crt']);
1167
		$purpose = cert_get_purpose($cert['crt']);
1168
		$sans = cert_get_sans($cert['crt']);
1169
		list($startdate, $enddate) = cert_get_dates($cert['crt']);
1170

    
1171
		if ($subj == $issuer) {
1172
			$caname = '<i>'. gettext("self-signed") .'</i>';
1173
		} else {
1174
			$caname = '<i>'. gettext("external").'</i>';
1175
		}
1176

    
1177
		$subj = htmlspecialchars(cert_escape_x509_chars($subj, true));
1178
	} else {
1179
		$subj = "";
1180
		$issuer = "";
1181
		$purpose = "";
1182
		$startdate = "";
1183
		$enddate = "";
1184
		$caname = "<em>" . gettext("private key only") . "</em>";
1185
	}
1186

    
1187
	if ($cert['csr']) {
1188
		$subj = htmlspecialchars(cert_escape_x509_chars(csr_get_subject($cert['csr']), true));
1189
		$sans = cert_get_sans($cert['crt']);
1190
		$caname = "<em>" . gettext("external - signature pending") . "</em>";
1191
	}
1192

    
1193
	$ca = lookup_ca($cert['caref']);
1194
	if ($ca) {
1195
		$caname = $ca['descr'];
1196
	}
1197
?>
1198
				<tr>
1199
					<td>
1200
						<?=$name?><br />
1201
						<?php if ($cert['type']): ?>
1202
							<i><?=$cert_types[$cert['type']]?></i><br />
1203
						<?php endif?>
1204
						<?php if (is_array($purpose)): ?>
1205
							CA: <b><?=$purpose['ca']?></b><br/>
1206
							<?=gettext("Server")?>: <b><?=$purpose['server']?></b><br/>
1207
						<?php endif?>
1208
					</td>
1209
					<td><?=$caname?></td>
1210
					<td>
1211
						<?=$subj?>
1212
						<?php
1213
						$certextinfo = "";
1214
						$certserial = cert_get_serial($cert['crt']);
1215
						if (!empty($certserial)) {
1216
							$certextinfo .= '<b>' . gettext("Serial: ") . '</b> ';
1217
							$certextinfo .= htmlspecialchars(cert_escape_x509_chars($certserial, true));
1218
							$certextinfo .= '<br/>';
1219
						}
1220
						$certsig = cert_get_sigtype($cert['crt']);
1221
						if (is_array($certsig) && !empty($certsig) && !empty($certsig['shortname'])) {
1222
							$certextinfo .= '<b>' . gettext("Signature Digest: ") . '</b> ';
1223
							$certextinfo .= htmlspecialchars(cert_escape_x509_chars($certsig['shortname'], true));
1224
							$certextinfo .= '<br/>';
1225
						}
1226
						if (is_array($sans) && !empty($sans)) {
1227
							$certextinfo .= '<b>' . gettext("SAN: ") . '</b> ';
1228
							$certextinfo .= htmlspecialchars(implode(', ', cert_escape_x509_chars($sans, true)));
1229
							$certextinfo .= '<br/>';
1230
						}
1231
						if (is_array($purpose) && !empty($purpose['ku'])) {
1232
							$certextinfo .= '<b>' . gettext("KU: ") . '</b> ';
1233
							$certextinfo .= htmlspecialchars(implode(', ', $purpose['ku']));
1234
							$certextinfo .= '<br/>';
1235
						}
1236
						if (is_array($purpose) && !empty($purpose['eku'])) {
1237
							$certextinfo .= '<b>' . gettext("EKU: ") . '</b> ';
1238
							$certextinfo .= htmlspecialchars(implode(', ', $purpose['eku']));
1239
							$certextinfo .= '<br/>';
1240
						}
1241
						if (cert_get_ocspstaple($cert['crt'])) {
1242
							$certextinfo .= '<b>' . gettext("OCSP: ") . '</b> ';
1243
							$certextinfo .= gettext("Must Staple");
1244
						}
1245
						?>
1246
						<?php if (!empty($certextinfo)): ?>
1247
							<div class="infoblock">
1248
							<? print_info_box($certextinfo, 'info', false); ?>
1249
							</div>
1250
						<?php endif?>
1251

    
1252
						<?php if (!empty($startdate) || !empty($enddate)): ?>
1253
						<br />
1254
						<small>
1255
							<?=gettext("Valid From")?>: <b><?=$startdate ?></b><br /><?=gettext("Valid Until")?>: <b><?=$enddate ?></b>
1256
						</small>
1257
						<?php endif?>
1258
					</td>
1259
					<td>
1260
						<?php if (is_cert_revoked($cert)): ?>
1261
							<i><?=gettext("Revoked")?></i>
1262
						<?php endif?>
1263
						<?php if (is_webgui_cert($cert['refid'])): ?>
1264
							<?=gettext("webConfigurator")?>
1265
						<?php endif?>
1266
						<?php if (is_user_cert($cert['refid'])): ?>
1267
							<?=gettext("User Cert")?>
1268
						<?php endif?>
1269
						<?php if (is_openvpn_server_cert($cert['refid'])): ?>
1270
							<?=gettext("OpenVPN Server")?>
1271
						<?php endif?>
1272
						<?php if (is_openvpn_client_cert($cert['refid'])): ?>
1273
							<?=gettext("OpenVPN Client")?>
1274
						<?php endif?>
1275
						<?php if (is_ipsec_cert($cert['refid'])): ?>
1276
							<?=gettext("IPsec Tunnel")?>
1277
						<?php endif?>
1278
						<?php if (is_captiveportal_cert($cert['refid'])): ?>
1279
							<?=gettext("Captive Portal")?>
1280
						<?php endif?>
1281
						<?php echo cert_usedby_description($cert['refid'], $certificates_used_by_packages); ?>
1282
					</td>
1283
					<td>
1284
						<?php if (!$cert['csr']): ?>
1285
							<a href="system_certmanager.php?act=exp&amp;id=<?=$i?>" class="fa fa-certificate" title="<?=gettext("Export Certificate")?>"></a>
1286
							<?php if ($cert['prv']): ?>
1287
								<a href="system_certmanager.php?act=key&amp;id=<?=$i?>" class="fa fa-key" title="<?=gettext("Export Key")?>"></a>
1288
							<?php endif?>
1289
							<a href="system_certmanager.php?act=p12&amp;id=<?=$i?>" class="fa fa-archive" title="<?=gettext("Export P12")?>"></a>
1290
						<?php else: ?>
1291
							<a href="system_certmanager.php?act=csr&amp;id=<?=$i?>" class="fa fa-pencil" title="<?=gettext("Update CSR")?>"></a>
1292
							<a href="system_certmanager.php?act=req&amp;id=<?=$i?>" class="fa fa-sign-in" title="<?=gettext("Export Request")?>"></a>
1293
							<a href="system_certmanager.php?act=key&amp;id=<?=$i?>" class="fa fa-key" title="<?=gettext("Export Key")?>"></a>
1294
						<?php endif?>
1295
						<?php if (!cert_in_use($cert['refid'])): ?>
1296
							<a href="system_certmanager.php?act=del&amp;id=<?=$i?>" class="fa fa-trash" title="<?=gettext("Delete Certificate")?>" usepost></a>
1297
						<?php endif?>
1298
					</td>
1299
				</tr>
1300
<?php
1301
	$i++;
1302
	endforeach; ?>
1303
			</tbody>
1304
		</table>
1305
		</div>
1306
	</div>
1307
</div>
1308

    
1309
<nav class="action-buttons">
1310
	<a href="?act=new" class="btn btn-success btn-sm">
1311
		<i class="fa fa-plus icon-embed-btn"></i>
1312
		<?=gettext("Add/Sign")?>
1313
	</a>
1314
</nav>
1315
<script type="text/javascript">
1316
//<![CDATA[
1317

    
1318
events.push(function() {
1319

    
1320
	// Make these controls plain buttons
1321
	$("#btnsearch").prop('type', 'button');
1322
	$("#btnclear").prop('type', 'button');
1323

    
1324
	// Search for a term in the entry name and/or dn
1325
	$("#btnsearch").click(function() {
1326
		var searchstr = $('#searchstr').val().toLowerCase();
1327
		var table = $("table tbody");
1328
		var where = $('#where').val();
1329

    
1330
		table.find('tr').each(function (i) {
1331
			var $tds = $(this).find('td'),
1332
				shortname = $tds.eq(0).text().trim().toLowerCase(),
1333
				dn = $tds.eq(2).text().trim().toLowerCase();
1334

    
1335
			regexp = new RegExp(searchstr);
1336
			if (searchstr.length > 0) {
1337
				if (!(regexp.test(shortname) && (where != 1)) && !(regexp.test(dn) && (where != 0))) {
1338
					$(this).hide();
1339
				} else {
1340
					$(this).show();
1341
				}
1342
			} else {
1343
				$(this).show();	// A blank search string shows all
1344
			}
1345
		});
1346
	});
1347

    
1348
	// Clear the search term and unhide all rows (that were hidden during a previous search)
1349
	$("#btnclear").click(function() {
1350
		var table = $("table tbody");
1351

    
1352
		$('#searchstr').val("");
1353

    
1354
		table.find('tr').each(function (i) {
1355
			$(this).show();
1356
		});
1357
	});
1358

    
1359
	// Hitting the enter key will do the same as clicking the search button
1360
	$("#searchstr").on("keyup", function (event) {
1361
		if (event.keyCode == 13) {
1362
			$("#btnsearch").get(0).click();
1363
		}
1364
	});
1365
});
1366
//]]>
1367
</script>
1368
<?php
1369
	include("foot.inc");
1370
	exit;
1371
}
1372

    
1373

    
1374
?>
1375
<script type="text/javascript">
1376
//<![CDATA[
1377
events.push(function() {
1378

    
1379
<?php if ($internal_ca_count): ?>
1380
	function internalca_change() {
1381

    
1382
		caref = $('#caref').val();
1383

    
1384
		switch (caref) {
1385
<?php
1386
			foreach ($a_ca as $ca):
1387
				if (!$ca['prv']) {
1388
					continue;
1389
				}
1390

    
1391
				$subject = cert_get_subject_hash($ca['crt']);
1392
?>
1393
				case "<?=$ca['refid'];?>":
1394
					$('#dn_country').val(<?=json_encode(cert_escape_x509_chars($subject['C'], true));?>);
1395
					$('#dn_state').val(<?=json_encode(cert_escape_x509_chars($subject['ST'], true));?>);
1396
					$('#dn_city').val(<?=json_encode(cert_escape_x509_chars($subject['L'], true));?>);
1397
					$('#dn_organization').val(<?=json_encode(cert_escape_x509_chars($subject['O'], true));?>);
1398
					$('#dn_organizationalunit').val(<?=json_encode(cert_escape_x509_chars($subject['OU'], true));?>);
1399
					break;
1400
<?php
1401
			endforeach;
1402
?>
1403
		}
1404
	}
1405

    
1406
	function set_csr_ro() {
1407
		var newcsr = ($('#csrtosign').val() == "new");
1408

    
1409
		$('#csrpaste').attr('readonly', !newcsr);
1410
		$('#keypaste').attr('readonly', !newcsr);
1411
		setRequired('csrpaste', newcsr);
1412
	}
1413

    
1414
	// ---------- Click checkbox handlers ---------------------------------------------------------
1415

    
1416
	$('#caref').on('change', function() {
1417
		internalca_change();
1418
	});
1419

    
1420
	$('#csrtosign').change(function () {
1421
		set_csr_ro();
1422
	});
1423

    
1424
	// ---------- On initial page load ------------------------------------------------------------
1425

    
1426
	internalca_change();
1427
	set_csr_ro();
1428

    
1429
	// Suppress "Delete row" button if there are fewer than two rows
1430
	checkLastRow();
1431

    
1432
<?php endif; ?>
1433

    
1434

    
1435
});
1436
//]]>
1437
</script>
1438
<?php
1439
include('foot.inc');
(192-192/225)