Project

General

Profile

Download (54.8 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-2023 Rubicon Communications, LLC (Netgate)
9
 * Copyright (c) 2008 Shrew Soft Inc
10
 * All rights reserved.
11
 *
12
 * Licensed under the Apache License, Version 2.0 (the "License");
13
 * you may not use this file except in compliance with the License.
14
 * You may obtain a copy of the License at
15
 *
16
 * http://www.apache.org/licenses/LICENSE-2.0
17
 *
18
 * Unless required by applicable law or agreed to in writing, software
19
 * distributed under the License is distributed on an "AS IS" BASIS,
20
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
21
 * See the License for the specific language governing permissions and
22
 * limitations under the License.
23
 */
24

    
25
##|+PRIV
26
##|*IDENT=page-system-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_keytypes = array("RSA", "ECDSA");
45
$cert_types = array(
46
	"server" => "Server Certificate",
47
	"user" => "User Certificate");
48

    
49
global $cert_altname_types;
50
global $openssl_digest_algs;
51
global $cert_strict_values;
52
global $p12_encryption_levels;
53

    
54
$max_lifetime = cert_get_max_lifetime();
55
$default_lifetime = min(3650, $max_lifetime);
56
$openssl_ecnames = cert_build_curve_list();
57
$class = "success";
58

    
59
if (isset($_REQUEST['userid']) && is_numericint($_REQUEST['userid'])) {
60
	$userid = $_REQUEST['userid'];
61
}
62

    
63
if (isset($userid)) {
64
	$cert_methods["existing"] = gettext("Choose an existing certificate");
65
	init_config_arr(array('system', 'user'));
66
	$a_user =& $config['system']['user'];
67
}
68

    
69
init_config_arr(array('ca'));
70
$a_ca = &$config['ca'];
71

    
72
init_config_arr(array('cert'));
73
$a_cert = &$config['cert'];
74

    
75
$internal_ca_count = 0;
76
foreach ($a_ca as $ca) {
77
	if ($ca['prv']) {
78
		$internal_ca_count++;
79
	}
80
}
81

    
82
if ($_REQUEST['exportp12']) {
83
	$act = 'p12';
84
} elseif ($_REQUEST['exportpkey']) {
85
	$act = 'key';
86
} else {
87
	$act = $_REQUEST['act'];
88
}
89

    
90
if ($act == 'edit') {
91
	$cert_methods = array(
92
		'edit' => gettext("Edit an existing certificate")
93
	);
94
}
95

    
96
if (isset($_REQUEST['id']) && ctype_alnum($_REQUEST['id'])) {
97
	$id = $_REQUEST['id'];
98
}
99
if (!empty($id)) {
100
	$thiscert =& lookup_cert($id);
101
}
102

    
103
/* Actions other than 'new' require an ID.
104
 * 'del' action must be submitted via POST. */
105
if ((!empty($act) &&
106
    ($act != 'new') &&
107
    !$thiscert) ||
108
    (($act == 'del') && empty($_POST))) {
109
	pfSenseHeader("system_certmanager.php");
110
	exit;
111
}
112

    
113
switch ($act) {
114
	case 'del':
115
		$name = htmlspecialchars($thiscert['descr']);
116
		if (cert_in_use($id)) {
117
			$savemsg = sprintf(gettext("Certificate %s is in use and cannot be deleted"), $name);
118
			$class = "danger";
119
		} else {
120
			foreach ($a_cert as $cid => $acrt) {
121
				if ($acrt['refid'] == $thiscert['refid']) {
122
					unset($a_cert[$cid]);
123
				}
124
			}
125
			$savemsg = sprintf(gettext("Deleted certificate %s"), $name);
126
			write_config($savemsg);
127
		}
128
		unset($act);
129
		break;
130
	case 'new':
131
		/* New certificate, so set default values */
132
		$pconfig['method'] = $_POST['method'];
133
		$pconfig['keytype'] = "RSA";
134
		$pconfig['keylen'] = "2048";
135
		$pconfig['ecname'] = "prime256v1";
136
		$pconfig['digest_alg'] = "sha256";
137
		$pconfig['csr_keytype'] = "RSA";
138
		$pconfig['csr_keylen'] = "2048";
139
		$pconfig['csr_ecname'] = "prime256v1";
140
		$pconfig['csr_digest_alg'] = "sha256";
141
		$pconfig['csrsign_digest_alg'] = "sha256";
142
		$pconfig['type'] = "user";
143
		$pconfig['lifetime'] = $default_lifetime;
144
		break;
145
	case 'edit':
146
		/* Editing a certificate, so populate values */
147
		$pconfig['descr'] = $thiscert['descr'];
148
		$pconfig['cert'] = base64_decode($thiscert['crt']);
149
		$pconfig['key'] = base64_decode($thiscert['prv']);
150
		break;
151
	case 'csr':
152
		/* Editing a CSR, so populate values */
153
		$pconfig['descr'] = $thiscert['descr'];
154
		$pconfig['csr'] = base64_decode($thiscert['csr']);
155
		break;
156
	case 'exp':
157
		/* Exporting a certificate */
158
		send_user_download('data', base64_decode($thiscert['crt']), "{$thiscert['descr']}.crt");
159
		break;
160
	case 'req':
161
		/* Exporting a certificate signing request */
162
		send_user_download('data', base64_decode($thiscert['csr']), "{$thiscert['descr']}.req");
163
		break;
164
	case 'key':
165
		/* Exporting a private key */
166
		$keyout = base64_decode($thiscert['prv']);
167
		if (isset($_POST['exportpass']) && !empty($_POST['exportpass'])) {
168
			if ((strlen($_POST['exportpass']) < 4) or (strlen($_POST['exportpass']) > 1023)) {
169
				$savemsg = gettext("Export password must be in 4 to 1023 characters.");
170
				$class = 'danger';
171
				break;
172
			} else {
173
				$res_key = openssl_pkey_get_private($keyout);
174
				if ($res_key) {
175
					$args = array('encrypt_key_cipher' => OPENSSL_CIPHER_AES_256_CBC);
176
					openssl_pkey_export($res_key, $keyout, $_POST['exportpass'], $args);
177
				} else {
178
					$savemsg = gettext("Unable to export password-protected private key.");
179
					$class = 'danger';
180
				}
181
			}
182
		}
183
		if (!empty($keyout)) {
184
			send_user_download('data', $keyout, "{$thiscert['descr']}.key");
185
		}
186
		break;
187
	case 'p12':
188
		/* Exporting a PKCS#12 file containing the certificate, key, and (if present) CA */
189
		if (isset($_POST['exportpass']) && !empty($_POST['exportpass'])) {
190
			if ((strlen($_POST['exportpass']) < 4) or (strlen($_POST['exportpass']) > 1023)) {
191
				$savemsg = gettext("Export password must be in 4 to 1023 characters.");
192
				$class = 'danger';
193
				break;
194
			} else {
195
				$password = $_POST['exportpass'];
196
			}
197
		} else {
198
			$password = null;
199
		}
200
		if (isset($_POST['p12encryption']) &&
201
		    array_key_exists($_POST['p12encryption'], $p12_encryption_levels)) {
202
			$encryption = $_POST['p12encryption'];
203
		} else {
204
			$encryption = 'high';
205
		}
206
		cert_pkcs12_export($thiscert, $encryption, $password, true, 'download');
207
		break;
208
	default:
209
		break;
210
}
211

    
212
if ($_POST['save'] == gettext("Save")) {
213
	/* Creating a new entry */
214
	$input_errors = array();
215
	$pconfig = $_POST;
216

    
217
	switch ($pconfig['method']) {
218
		case 'sign':
219
			$reqdfields = explode(" ",
220
				"descr catosignwith");
221
			$reqdfieldsn = array(
222
				gettext("Descriptive name"),
223
				gettext("CA to sign with"));
224

    
225
			if (($_POST['csrtosign'] === "new") &&
226
			    ((!strstr($_POST['csrpaste'], "BEGIN CERTIFICATE REQUEST") || !strstr($_POST['csrpaste'], "END CERTIFICATE REQUEST")) &&
227
			    (!strstr($_POST['csrpaste'], "BEGIN NEW CERTIFICATE REQUEST") || !strstr($_POST['csrpaste'], "END NEW CERTIFICATE REQUEST")))) {
228
				$input_errors[] = gettext("This signing request does not appear to be valid.");
229
			}
230

    
231
			if ( (($_POST['csrtosign'] === "new") && (strlen($_POST['keypaste']) > 0)) &&
232
			    ((!strstr($_POST['keypaste'], "BEGIN PRIVATE KEY") && !strstr($_POST['keypaste'], "BEGIN EC PRIVATE KEY")) ||
233
			    (strstr($_POST['keypaste'], "BEGIN PRIVATE KEY") && !strstr($_POST['keypaste'], "END PRIVATE KEY")) ||
234
			    (strstr($_POST['keypaste'], "BEGIN EC PRIVATE KEY") && !strstr($_POST['keypaste'], "END EC PRIVATE KEY")))) {
235
				$input_errors[] = gettext("This private does not appear to be valid.");
236
				$input_errors[] = gettext("Key data field should be blank, or a valid x509 private key");
237
			}
238

    
239
			if ($_POST['lifetime'] > $max_lifetime) {
240
				$input_errors[] = gettext("Lifetime is longer than the maximum allowed value. Use a shorter lifetime.");
241
			}
242
			break;
243
		case 'edit':
244
		case 'import':
245
			/* Make sure we do not have invalid characters in the fields for the certificate */
246
			if (preg_match("/[\?\>\<\&\/\\\"\']/", $_POST['descr'])) {
247
				$input_errors[] = gettext("The field 'Descriptive Name' contains invalid characters.");
248
			}
249
			$pkcs12_data = '';
250
			if ($_POST['import_type'] == 'x509') {
251
				$reqdfields = explode(" ",
252
					"descr cert");
253
				$reqdfieldsn = array(
254
					gettext("Descriptive name"),
255
					gettext("Certificate data"));
256
				if ($_POST['cert'] && (!strstr($_POST['cert'], "BEGIN CERTIFICATE") || !strstr($_POST['cert'], "END CERTIFICATE"))) {
257
					$input_errors[] = gettext("This certificate does not appear to be valid.");
258
				}
259

    
260
				if ($_POST['key'] && (cert_get_publickey($_POST['cert'], false) != cert_get_publickey($_POST['key'], false, 'prv'))) {
261
					$input_errors[] = gettext("The submitted private key does not match the submitted certificate data.");
262
				}
263
			} else {
264
				$reqdfields = array('descr');
265
				$reqdfieldsn = array(gettext("Descriptive name"));
266
				if (!empty($_FILES['pkcs12_cert']) && is_uploaded_file($_FILES['pkcs12_cert']['tmp_name'])) {
267
					$pkcs12_file = file_get_contents($_FILES['pkcs12_cert']['tmp_name']);
268
					if (!openssl_pkcs12_read($pkcs12_file, $pkcs12_data, $_POST['pkcs12_pass'])) {
269
						$input_errors[] = gettext("The submitted password does not unlock the submitted PKCS #12 certificate.");
270
					}
271
				} else {
272
					$input_errors[] = gettext("A PKCS #12 certificate store was not uploaded.");
273
				}
274
			}
275
			break;
276
		case 'internal':
277
			$reqdfields = explode(" ",
278
				"descr caref keylen ecname keytype type lifetime dn_commonname");
279
			$reqdfieldsn = array(
280
				gettext("Descriptive name"),
281
				gettext("Certificate authority"),
282
				gettext("Key length"),
283
				gettext("Elliptic Curve Name"),
284
				gettext("Key type"),
285
				gettext("Certificate Type"),
286
				gettext("Lifetime"),
287
				gettext("Common Name"));
288
			if ($_POST['lifetime'] > $max_lifetime) {
289
				$input_errors[] = gettext("Lifetime is longer than the maximum allowed value. Use a shorter lifetime.");
290
			}
291
			break;
292
		case 'external':
293
			$reqdfields = explode(" ",
294
				"descr csr_keylen csr_ecname csr_keytype csr_dn_commonname");
295
			$reqdfieldsn = array(
296
				gettext("Descriptive name"),
297
				gettext("Key length"),
298
				gettext("Elliptic Curve Name"),
299
				gettext("Key type"),
300
				gettext("Common Name"));
301
			break;
302
		case 'existing':
303
			$reqdfields = array("certref");
304
			$reqdfieldsn = array(gettext("Existing Certificate Choice"));
305
			break;
306
		default:
307
			break;
308
	}
309

    
310
	$altnames = array();
311
	do_input_validation($_POST, $reqdfields, $reqdfieldsn, $input_errors);
312

    
313
	if (!in_array($pconfig['method'], array('edit', 'import', 'existing'))) {
314
		/* subjectAltNames */
315
		$san_typevar = 'altname_type';
316
		$san_valuevar = 'altname_value';
317
		// This is just the blank alternate name that is added for display purposes. We don't want to validate/save it
318
		if ($_POST["{$san_valuevar}0"] == "") {
319
			unset($_POST["{$san_typevar}0"]);
320
			unset($_POST["{$san_valuevar}0"]);
321
		}
322
		foreach ($_POST as $key => $value) {
323
			$entry = '';
324
			if (!substr_compare($san_typevar, $key, 0, strlen($san_typevar))) {
325
				$entry = substr($key, strlen($san_typevar));
326
				$field = 'type';
327
			} elseif (!substr_compare($san_valuevar, $key, 0, strlen($san_valuevar))) {
328
				$entry = substr($key, strlen($san_valuevar));
329
				$field = 'value';
330
			}
331

    
332
			if (ctype_digit(strval($entry))) {
333
				$entry++;	// Pre-bootstrap code is one-indexed, but the bootstrap code is 0-indexed
334
				$altnames[$entry][$field] = $value;
335
			}
336
		}
337

    
338
		$pconfig['altnames']['item'] = $altnames;
339

    
340
		/* Input validation for subjectAltNames */
341
		foreach ($altnames as $idx => $altname) {
342
			/* Skip SAN entries with empty values
343
			 * https://redmine.pfsense.org/issues/14183
344
			 */
345
			if (empty($altname['value'])) {
346
				unset($altnames[$idx]);
347
				continue;
348
			}
349
			switch ($altname['type']) {
350
				case "DNS":
351
					if (!is_hostname($altname['value'], true) || is_ipaddr($altname['value'])) {
352
						$input_errors[] = gettext("DNS subjectAltName values must be valid hostnames, FQDNs or wildcard domains.");
353
					}
354
					break;
355
				case "IP":
356
					if (!is_ipaddr($altname['value'])) {
357
						$input_errors[] = gettext("IP subjectAltName values must be valid IP Addresses");
358
					}
359
					break;
360
				case "email":
361
					if (empty($altname['value'])) {
362
						$input_errors[] = gettext("An e-mail address must be provided for this type of subjectAltName");
363
					}
364
					if (preg_match("/[\!\#\$\%\^\(\)\~\?\>\<\&\/\\\,\"\']/", $altname['value'])) {
365
						$input_errors[] = gettext("The e-mail provided in a subjectAltName contains invalid characters.");
366
					}
367
					break;
368
				case "URI":
369
					/* Close enough? */
370
					if (!is_URL($altname['value'])) {
371
						$input_errors[] = gettext("URI subjectAltName types must be a valid URI");
372
					}
373
					break;
374
				default:
375
					$input_errors[] = gettext("Unrecognized subjectAltName type.");
376
			}
377
		}
378

    
379
		/* Make sure we do not have invalid characters in the fields for the certificate */
380
		if (preg_match("/[\?\>\<\&\/\\\"\']/", $_POST['descr'])) {
381
			$input_errors[] = gettext("The field 'Descriptive Name' contains invalid characters.");
382
		}
383
		$pattern = '/[^a-zA-Z0-9\ \'\/~`\!@#\$%\^&\*\(\)_\-\+=\{\}\[\]\|;:"\<\>,\.\?\\\]/';
384
		if (!empty($_POST['dn_commonname']) && preg_match($pattern, $_POST['dn_commonname'])) {
385
			$input_errors[] = gettext("The field 'Common Name' contains invalid characters.");
386
		}
387
		if (!empty($_POST['dn_state']) && preg_match($pattern, $_POST['dn_state'])) {
388
			$input_errors[] = gettext("The field 'State or Province' contains invalid characters.");
389
		}
390
		if (!empty($_POST['dn_city']) && preg_match($pattern, $_POST['dn_city'])) {
391
			$input_errors[] = gettext("The field 'City' contains invalid characters.");
392
		}
393
		if (!empty($_POST['dn_organization']) && preg_match($pattern, $_POST['dn_organization'])) {
394
			$input_errors[] = gettext("The field 'Organization' contains invalid characters.");
395
		}
396
		if (!empty($_POST['dn_organizationalunit']) && preg_match($pattern, $_POST['dn_organizationalunit'])) {
397
			$input_errors[] = gettext("The field 'Organizational Unit' contains invalid characters.");
398
		}
399

    
400
		switch ($pconfig['method']) {
401
			case "internal":
402
				if (isset($_POST["keytype"]) && !in_array($_POST["keytype"], $cert_keytypes)) {
403
					$input_errors[] = gettext("Please select a valid Key Type.");
404
				}
405
				if (isset($_POST["keylen"]) && !in_array($_POST["keylen"], $cert_keylens)) {
406
					$input_errors[] = gettext("Please select a valid Key Length.");
407
				}
408
				if (isset($_POST["ecname"]) && !in_array($_POST["ecname"], array_keys($openssl_ecnames))) {
409
					$input_errors[] = gettext("Please select a valid Elliptic Curve Name.");
410
				}
411
				if (!in_array($_POST["digest_alg"], $openssl_digest_algs)) {
412
					$input_errors[] = gettext("Please select a valid Digest Algorithm.");
413
				}
414
				break;
415
			case "external":
416
				if (isset($_POST["csr_keytype"]) && !in_array($_POST["csr_keytype"], $cert_keytypes)) {
417
					$input_errors[] = gettext("Please select a valid Key Type.");
418
				}
419
				if (isset($_POST["csr_keylen"]) && !in_array($_POST["csr_keylen"], $cert_keylens)) {
420
					$input_errors[] = gettext("Please select a valid Key Length.");
421
				}
422
				if (isset($_POST["csr_ecname"]) && !in_array($_POST["csr_ecname"], array_keys($openssl_ecnames))) {
423
					$input_errors[] = gettext("Please select a valid Elliptic Curve Name.");
424
				}
425
				if (!in_array($_POST["csr_digest_alg"], $openssl_digest_algs)) {
426
					$input_errors[] = gettext("Please select a valid Digest Algorithm.");
427
				}
428
				break;
429
			case "sign":
430
				if (!in_array($_POST["csrsign_digest_alg"], $openssl_digest_algs)) {
431
					$input_errors[] = gettext("Please select a valid Digest Algorithm.");
432
				}
433
				break;
434
			default:
435
				break;
436
		}
437
	}
438

    
439
	/* save modifications */
440
	if (!$input_errors) {
441
		$old_err_level = error_reporting(0); /* otherwise openssl_ functions throw warnings directly to a page breaking menu tabs */
442

    
443
		if (isset($id) && $thiscert) {
444
			$cert = $thiscert;
445
		} else {
446
			$cert = array();
447
			$cert['refid'] = uniqid();
448
		}
449

    
450
		$cert['descr'] = $pconfig['descr'];
451

    
452
		switch($pconfig['method']) {
453
			case 'existing':
454
				/* Add an existing certificate to a user */
455
				$ucert = lookup_cert($pconfig['certref']);
456
				if ($ucert && $a_user) {
457
					$a_user[$userid]['cert'][] = $ucert['refid'];
458
					$savemsg = sprintf(gettext("Added certificate %s to user %s"), htmlspecialchars($ucert['descr']), $a_user[$userid]['name']);
459
				}
460
				unset($cert);
461
				break;
462
			case 'sign':
463
				/* Sign a CSR */
464
				$csrid = lookup_cert($pconfig['csrtosign']);
465
				$ca = & lookup_ca($pconfig['catosignwith']);
466
				// Read the CSR from $config, or if a new one, from the textarea
467
				if ($pconfig['csrtosign'] === "new") {
468
					$csr = $pconfig['csrpaste'];
469
				} else {
470
					$csr = base64_decode($csrid['csr']);
471
				}
472
				if (count($altnames)) {
473
					foreach ($altnames as $altname) {
474
						$altnames_tmp[] = "{$altname['type']}:" . $altname['value'];
475
					}
476
					$altname_str = implode(",", $altnames_tmp);
477
				}
478
				$n509 = csr_sign($csr, $ca, $pconfig['csrsign_lifetime'], $pconfig['type'], $altname_str, $pconfig['csrsign_digest_alg']);
479
				if ($n509) {
480
					// Gather the details required to save the new cert
481
					$newcert = array();
482
					$newcert['refid'] = uniqid();
483
					$newcert['caref'] = $pconfig['catosignwith'];
484
					$newcert['descr'] = $pconfig['descr'];
485
					$newcert['type'] = $pconfig['type'];
486
					$newcert['crt'] = base64_encode($n509);
487
					if ($pconfig['csrtosign'] === "new") {
488
						$newcert['prv'] = base64_encode($pconfig['keypaste']);
489
					} else {
490
						$newcert['prv'] = $csrid['prv'];
491
					}
492
					// Add it to the config file
493
					$config['cert'][] = $newcert;
494
					$savemsg = sprintf(gettext("Signed certificate %s"), htmlspecialchars($newcert['descr']));
495
					unset($act);
496
				}
497
				unset($cert);
498
				break;
499
			case 'edit':
500
				cert_import($cert, $pconfig['cert'], $pconfig['key']);
501
				$savemsg = sprintf(gettext("Edited certificate %s"), htmlspecialchars($cert['descr']));
502
				unset($act);
503
				break;
504
			case 'import':
505
				/* Import an external certificate+key */
506
				if ($pkcs12_data) {
507
					$pconfig['cert'] = $pkcs12_data['cert'];
508
					$pconfig['key'] = $pkcs12_data['pkey'];
509
					if ($_POST['pkcs12_intermediate'] && is_array($pkcs12_data['extracerts'])) {
510
						foreach ($pkcs12_data['extracerts'] as $intermediate) {
511
							$int_data = openssl_x509_parse($intermediate);
512
							if (!$int_data) continue;
513
							$cn = $int_data['subject']['CN'];
514
							$int_ca = array('descr' => $cn, 'refid' => uniqid());
515
							if (ca_import($int_ca, $intermediate)) {
516
								$a_ca[] = $int_ca;
517
							}
518
						}
519
					}
520
				}
521
				cert_import($cert, $pconfig['cert'], $pconfig['key']);
522
				$savemsg = sprintf(gettext("Imported certificate %s"), htmlspecialchars($cert['descr']));
523
				unset($act);
524
				break;
525
			case 'internal':
526
				/* Create an internal certificate */
527
				$dn = array('commonName' => $pconfig['dn_commonname']);
528
				if (!empty($pconfig['dn_country'])) {
529
					$dn['countryName'] = $pconfig['dn_country'];
530
				}
531
				if (!empty($pconfig['dn_state'])) {
532
					$dn['stateOrProvinceName'] = $pconfig['dn_state'];
533
				}
534
				if (!empty($pconfig['dn_city'])) {
535
					$dn['localityName'] = $pconfig['dn_city'];
536
				}
537
				if (!empty($pconfig['dn_organization'])) {
538
					$dn['organizationName'] = $pconfig['dn_organization'];
539
				}
540
				if (!empty($pconfig['dn_organizationalunit'])) {
541
					$dn['organizationalUnitName'] = $pconfig['dn_organizationalunit'];
542
				}
543
				$altnames_tmp = array();
544
				$cn_altname = cert_add_altname_type($pconfig['dn_commonname']);
545
				if (!empty($cn_altname)) {
546
					$altnames_tmp[] = $cn_altname;
547
				}
548
				if (count($altnames)) {
549
					foreach ($altnames as $altname) {
550
						// The CN is added as a SAN automatically, do not add it again.
551
						if ($altname['value'] != $pconfig['dn_commonname']) {
552
							$altnames_tmp[] = "{$altname['type']}:" . $altname['value'];
553
						}
554
					}
555
				}
556
				if (!empty($altnames_tmp)) {
557
					$dn['subjectAltName'] = implode(",", $altnames_tmp);
558
				}
559
				if (!cert_create($cert, $pconfig['caref'], $pconfig['keylen'], $pconfig['lifetime'], $dn, $pconfig['type'], $pconfig['digest_alg'], $pconfig['keytype'], $pconfig['ecname'])) {
560
					$input_errors = array();
561
					while ($ssl_err = openssl_error_string()) {
562
						if (strpos($ssl_err, 'NCONF_get_string:no value') === false) {
563
							$input_errors[] = sprintf(gettext("OpenSSL Library Error: %s"), $ssl_err);
564
						}
565
					}
566
				}
567
				$savemsg = sprintf(gettext("Created internal certificate %s"), htmlspecialchars($cert['descr']));
568
				unset($act);
569
				break;
570
			case 'external':
571
				/* Create a certificate signing request */
572
				$dn = array('commonName' => $pconfig['csr_dn_commonname']);
573
				if (!empty($pconfig['csr_dn_country'])) {
574
					$dn['countryName'] = $pconfig['csr_dn_country'];
575
				}
576
				if (!empty($pconfig['csr_dn_state'])) {
577
					$dn['stateOrProvinceName'] = $pconfig['csr_dn_state'];
578
				}
579
				if (!empty($pconfig['csr_dn_city'])) {
580
					$dn['localityName'] = $pconfig['csr_dn_city'];
581
				}
582
				if (!empty($pconfig['csr_dn_organization'])) {
583
					$dn['organizationName'] = $pconfig['csr_dn_organization'];
584
				}
585
				if (!empty($pconfig['csr_dn_organizationalunit'])) {
586
					$dn['organizationalUnitName'] = $pconfig['csr_dn_organizationalunit'];
587
				}
588
				$altnames_tmp = array();
589
				$cn_altname = cert_add_altname_type($pconfig['csr_dn_commonname']);
590
				if (!empty($cn_altname)) {
591
					$altnames_tmp[] = $cn_altname;
592
				}
593
				if (count($altnames)) {
594
					foreach ($altnames as $altname) {
595
						// The CN is added as a SAN automatically, do not add it again.
596
						if ($altname['value'] != $pconfig['csr_dn_commonname']) {
597
							$altnames_tmp[] = "{$altname['type']}:" . $altname['value'];
598
						}
599
					}
600
				}
601
				if (!empty($altnames_tmp)) {
602
					$dn['subjectAltName'] = implode(",", $altnames_tmp);
603
				}
604
				if (!csr_generate($cert, $pconfig['csr_keylen'], $dn, $pconfig['type'], $pconfig['csr_digest_alg'], $pconfig['csr_keytype'], $pconfig['csr_ecname'])) {
605
					$input_errors = array();
606
					while ($ssl_err = openssl_error_string()) {
607
						if (strpos($ssl_err, 'NCONF_get_string:no value') === false) {
608
							$input_errors[] = sprintf(gettext("OpenSSL Library Error: %s"), $ssl_err);
609
						}
610
					}
611
				}
612
				$savemsg = sprintf(gettext("Created certificate signing request %s"), htmlspecialchars($cert['descr']));
613
				unset($act);
614
				break;
615
			default:
616
				break;
617
		}
618
		error_reporting($old_err_level);
619

    
620
		if (isset($id) && $thiscert) {
621
			$thiscert = $cert;
622
		} elseif ($cert) {
623
			$a_cert[] = $cert;
624
		}
625

    
626
		if (isset($a_user) && isset($userid)) {
627
			$a_user[$userid]['cert'][] = $cert['refid'];
628
		}
629

    
630
		if (!$input_errors) {
631
			write_config($savemsg);
632
		}
633

    
634
		if ((isset($userid) && is_numeric($userid)) && !$input_errors) {
635
			post_redirect("system_usermanager.php", array('act' => 'edit', 'userid' => $userid));
636
			exit;
637
		}
638
	}
639
} elseif ($_POST['save'] == gettext("Update")) {
640
	/* Updating a certificate signing request */
641
	unset($input_errors);
642
	$pconfig = $_POST;
643

    
644
	/* input validation */
645
	$reqdfields = explode(" ", "descr cert");
646
	$reqdfieldsn = array(
647
		gettext("Descriptive name"),
648
		gettext("Final Certificate data"));
649

    
650
	do_input_validation($_POST, $reqdfields, $reqdfieldsn, $input_errors);
651

    
652
	if (preg_match("/[\?\>\<\&\/\\\"\']/", $_POST['descr'])) {
653
		$input_errors[] = gettext("The field 'Descriptive Name' contains invalid characters.");
654
	}
655

    
656
	$mod_csr = cert_get_publickey($pconfig['csr'], false, 'csr');
657
	$mod_cert = cert_get_publickey($pconfig['cert'], false);
658

    
659
	if (strcmp($mod_csr, $mod_cert)) {
660
		// simply: if the moduli don't match, then the private key and public key won't match
661
		$input_errors[] = gettext("The certificate public key does not match the signing request public key.");
662
		$subject_mismatch = true;
663
	}
664

    
665
	/* save modifications */
666
	if (!$input_errors) {
667
		$cert = $thiscert;
668
		$cert['descr'] = $pconfig['descr'];
669
		csr_complete($cert, $pconfig['cert']);
670
		$thiscert = $cert;
671
		$savemsg = sprintf(gettext("Updated certificate signing request %s"), htmlspecialchars($pconfig['descr']));
672
		write_config($savemsg);
673
		pfSenseHeader("system_certmanager.php");
674
	}
675
}
676

    
677
$pgtitle = array(gettext('System'), gettext('Certificates'), gettext('Certificates'));
678
$pglinks = array("", "system_camanager.php", "system_certmanager.php");
679

    
680
if (($act == "new" || ($_POST['save'] == gettext("Save") && $input_errors)) ||
681
    ($act == "csr" || ($_POST['save'] == gettext("Update") && $input_errors))) {
682
	$pgtitle[] = gettext('Edit');
683
	$pglinks[] = "@self";
684
}
685
include("head.inc");
686

    
687
if ($input_errors) {
688
	print_input_errors($input_errors);
689
}
690

    
691
if ($savemsg) {
692
	print_info_box($savemsg, $class);
693
}
694

    
695
$tab_array = array();
696
$tab_array[] = array(gettext('Authorities'), false, 'system_camanager.php');
697
$tab_array[] = array(gettext('Certificates'), true, 'system_certmanager.php');
698
$tab_array[] = array(gettext('Certificate Revocation'), false, 'system_crlmanager.php');
699
display_top_tabs($tab_array);
700

    
701
if (in_array($act, array('new', 'edit')) || (($_POST['save'] == gettext("Save")) && $input_errors)) {
702
	$form = new Form();
703
	$form->setAction('system_certmanager.php')->setMultipartEncoding();
704

    
705
	if (isset($userid) && $a_user) {
706
		$form->addGlobal(new Form_Input(
707
			'userid',
708
			null,
709
			'hidden',
710
			$userid
711
		));
712
	}
713

    
714
	if (isset($id) && $thiscert) {
715
		$form->addGlobal(new Form_Input(
716
			'id',
717
			null,
718
			'hidden',
719
			$id
720
		));
721
	}
722

    
723
	if ($act) {
724
		$form->addGlobal(new Form_Input(
725
			'act',
726
			null,
727
			'hidden',
728
			$act
729
		));
730
	}
731

    
732
	switch ($act) {
733
		case 'edit':
734
			$maintitle = gettext('Edit an Existing Certificate');
735
			break;
736
		case 'new':
737
		default:
738
			$maintitle = gettext('Add/Sign a New Certificate');
739
			break;
740
	}
741

    
742
	$section = new Form_Section($maintitle);
743

    
744
	if (!isset($id) || ($act == 'edit')) {
745
		$section->addInput(new Form_Select(
746
			'method',
747
			'*Method',
748
			$pconfig['method'],
749
			$cert_methods
750
		))->toggles();
751
	}
752

    
753
	$section->addInput(new Form_Input(
754
		'descr',
755
		'*Descriptive name',
756
		'text',
757
		($a_user && empty($pconfig['descr'])) ? $a_user[$userid]['name'] : $pconfig['descr']
758
	))->addClass('toggle-internal toggle-import toggle-edit toggle-external toggle-sign toggle-existing collapse')
759
	->setHelp('The name of this entry as displayed in the GUI for reference.%s' .
760
		'This name can contain spaces but it cannot contain any of the ' .
761
		'following characters: %s', '<br/>', "?, >, <, &, /, \, \", '");
762

    
763
	if (!empty($pconfig['cert'])) {
764
		$section->addInput(new Form_StaticText(
765
			"Subject",
766
			htmlspecialchars(cert_get_subject($pconfig['cert'], false))
767
		))->addClass('toggle-edit collapse');
768
	}
769

    
770
	$form->add($section);
771

    
772
	// Return an array containing the IDs od all CAs
773
	function list_cas() {
774
		global $a_ca;
775
		$allCas = array();
776

    
777
		foreach ($a_ca as $ca) {
778
			if ($ca['prv']) {
779
				$allCas[$ca['refid']] = $ca['descr'];
780
			}
781
		}
782

    
783
		return $allCas;
784
	}
785

    
786
	// Return an array containing the IDs od all CSRs
787
	function list_csrs() {
788
		global $config;
789
		$allCsrs = array();
790

    
791
		foreach ($config['cert'] as $cert) {
792
			if ($cert['csr']) {
793
				$allCsrs[$cert['refid']] = $cert['descr'];
794
			}
795
		}
796

    
797
		return ['new' => gettext('New CSR (Paste below)')] + $allCsrs;
798
	}
799

    
800
	$section = new Form_Section('Sign CSR');
801
	$section->addClass('toggle-sign collapse');
802

    
803
	$section->AddInput(new Form_Select(
804
		'catosignwith',
805
		'*CA to sign with',
806
		$pconfig['catosignwith'],
807
		list_cas()
808
	));
809

    
810
	$section->AddInput(new Form_Select(
811
		'csrtosign',
812
		'*CSR to sign',
813
		isset($pconfig['csrtosign']) ? $pconfig['csrtosign'] : 'new',
814
		list_csrs()
815
	));
816

    
817
	$section->addInput(new Form_Textarea(
818
		'csrpaste',
819
		'CSR data',
820
		$pconfig['csrpaste']
821
	))->setHelp('Paste a Certificate Signing Request in X.509 PEM format here.');
822

    
823
	$section->addInput(new Form_Textarea(
824
		'keypaste',
825
		'Key data',
826
		$pconfig['keypaste']
827
	))->setHelp('Optionally paste a private key here. The key will be associated with the newly signed certificate in %1$s', g_get('product_label'));
828

    
829
	$section->addInput(new Form_Input(
830
		'csrsign_lifetime',
831
		'*Certificate Lifetime (days)',
832
		'number',
833
		$pconfig['csrsign_lifetime'] ? $pconfig['csrsign_lifetime']:$default_lifetime,
834
		['max' => $max_lifetime]
835
	))->setHelp('The length of time the signed certificate will be valid, in days. %1$s' .
836
		'Server certificates should not have a lifetime over %2$s days or some platforms ' .
837
		'may consider the certificate invalid.', '<br/>', $cert_strict_values['max_server_cert_lifetime']);
838
	$section->addInput(new Form_Select(
839
		'csrsign_digest_alg',
840
		'*Digest Algorithm',
841
		$pconfig['csrsign_digest_alg'],
842
		array_combine($openssl_digest_algs, $openssl_digest_algs)
843
	))->setHelp('The digest method used when the certificate is signed. %1$s' .
844
		'The best practice is to use SHA256 or higher. '.
845
		'Some services and platforms, such as the GUI web server and OpenVPN, consider weaker digest algorithms invalid.', '<br/>');
846

    
847
	$form->add($section);
848

    
849
	if ($act == 'edit') {
850
		$editimport = gettext("Edit Certificate");
851
	} else {
852
		$editimport = gettext("Import Certificate");
853
	}
854

    
855
	$section = new Form_Section($editimport);
856
	$section->addClass('toggle-import toggle-edit collapse');
857

    
858
	$group = new Form_Group('Certificate Type');
859

    
860
	$group->add(new Form_Checkbox(
861
		'import_type',
862
		'Certificate Type',
863
		'X.509 (PEM)',
864
		(!isset($pconfig['import_type']) || $pconfig['import_type'] == 'x509'),
865
		'x509'
866
	))->displayAsRadio()->addClass('import_type_toggle');
867

    
868
	$group->add(new Form_Checkbox(
869
		'import_type',
870
		'Certificate Type',
871
		'PKCS #12 (PFX)',
872
		(isset($pconfig['import_type']) && $pconfig['import_type'] == 'pkcs12'),
873
		'pkcs12'
874
	))->displayAsRadio()->addClass('import_type_toggle');
875

    
876
	$section->add($group);
877

    
878
	$section->addInput(new Form_Textarea(
879
		'cert',
880
		'*Certificate data',
881
		$pconfig['cert']
882
	))->setHelp('Paste a certificate in X.509 PEM format here.');
883

    
884
	$section->addInput(new Form_Textarea(
885
		'key',
886
		'Private key data',
887
		$pconfig['key']
888
	))->setHelp('Paste a private key in X.509 PEM format here. This field may remain empty in certain cases, such as when the private key is stored on a PKCS#11 token.');
889

    
890
	$section->addInput(new Form_Input(
891
		'pkcs12_cert',
892
		'PKCS #12 certificate',
893
		'file',
894
		$pconfig['pkcs12_cert']
895
	))->setHelp('Select a PKCS #12 certificate store.');
896

    
897
	$section->addInput(new Form_Input(
898
		'pkcs12_pass',
899
		'PKCS #12 certificate password',
900
		'password',
901
		$pconfig['pkcs12_pass']
902
	))->setHelp('Enter the password to unlock the PKCS #12 certificate store.');
903

    
904
	$section->addInput(new Form_Checkbox(
905
		'pkcs12_intermediate',
906
		'Intermediates',
907
		'Import intermediate CAs',
908
		isset($pconfig['pkcs12_intermediate'])
909
	))->setHelp('Import any intermediate certificate authorities found in the PKCS #12 certificate store.');
910

    
911
	if ($act == 'edit') {
912
		$section->addInput(new Form_Input(
913
			'exportpass',
914
			'Export Password',
915
			'password',
916
			null,
917
			['placeholder' => gettext('Export Password'), 'autocomplete' => 'new-password']
918
		))->setHelp('Enter the password to use when using the export buttons below (not stored)')->addClass('toggle-edit collapse');
919
		$section->addInput(new Form_Select(
920
		'p12encryption',
921
		'PKCS#12 Encryption',
922
		'high',
923
		$p12_encryption_levels
924
		))->setHelp('Select the level of encryption to use when exporting a PKCS#12 archive. ' .
925
				'Encryption support varies by Operating System and program');
926
	}
927

    
928
	$form->add($section);
929
	$section = new Form_Section('Internal Certificate');
930
	$section->addClass('toggle-internal collapse');
931

    
932
	if (!$internal_ca_count) {
933
		$section->addInput(new Form_StaticText(
934
			'*Certificate authority',
935
			gettext('No internal Certificate Authorities have been defined. ') .
936
			gettext('An internal CA must be defined in order to create an internal certificate. ') .
937
			sprintf(gettext('%1$sCreate%2$s an internal CA.'), '<a href="system_camanager.php?act=new&amp;method=internal"> ', '</a>')
938
		));
939
	} else {
940
		$allCas = array();
941
		foreach ($a_ca as $ca) {
942
			if (!$ca['prv']) {
943
				continue;
944
			}
945

    
946
			$allCas[ $ca['refid'] ] = $ca['descr'];
947
		}
948

    
949
		$section->addInput(new Form_Select(
950
			'caref',
951
			'*Certificate authority',
952
			$pconfig['caref'],
953
			$allCas
954
		));
955
	}
956

    
957
	$section->addInput(new Form_Select(
958
		'keytype',
959
		'*Key type',
960
		$pconfig['keytype'],
961
		array_combine($cert_keytypes, $cert_keytypes)
962
	));
963

    
964
	$group = new Form_Group($i == 0 ? '*Key length':'');
965
	$group->addClass('rsakeys');
966
	$group->add(new Form_Select(
967
		'keylen',
968
		null,
969
		$pconfig['keylen'],
970
		array_combine($cert_keylens, $cert_keylens)
971
	))->setHelp('The length to use when generating a new RSA key, in bits. %1$s' .
972
		'The Key Length should not be lower than 2048 or some platforms ' .
973
		'may consider the certificate invalid.', '<br/>');
974
	$section->add($group);
975

    
976
	$group = new Form_Group($i == 0 ? '*Elliptic Curve Name':'');
977
	$group->addClass('ecnames');
978
	$group->add(new Form_Select(
979
		'ecname',
980
		null,
981
		$pconfig['ecname'],
982
		$openssl_ecnames
983
	))->setHelp('Curves may not be compatible with all uses. Known compatible curve uses are denoted in brackets.');
984
	$section->add($group);
985

    
986
	$section->addInput(new Form_Select(
987
		'digest_alg',
988
		'*Digest Algorithm',
989
		$pconfig['digest_alg'],
990
		array_combine($openssl_digest_algs, $openssl_digest_algs)
991
	))->setHelp('The digest method used when the certificate is signed. %1$s' .
992
		'The best practice is to use SHA256 or higher. '.
993
		'Some services and platforms, such as the GUI web server and OpenVPN, consider weaker digest algorithms invalid.', '<br/>');
994

    
995
	$section->addInput(new Form_Input(
996
		'lifetime',
997
		'*Lifetime (days)',
998
		'number',
999
		$pconfig['lifetime'],
1000
		['max' => $max_lifetime]
1001
	))->setHelp('The length of time the signed certificate will be valid, in days. %1$s' .
1002
		'Server certificates should not have a lifetime over %2$s days or some platforms ' .
1003
		'may consider the certificate invalid.', '<br/>', $cert_strict_values['max_server_cert_lifetime']);
1004

    
1005
	$section->addInput(new Form_Input(
1006
		'dn_commonname',
1007
		'*Common Name',
1008
		'text',
1009
		$pconfig['dn_commonname'],
1010
		['placeholder' => 'e.g. www.example.com']
1011
	));
1012

    
1013
	$section->addInput(new Form_StaticText(
1014
		null,
1015
		gettext('The following certificate subject components are optional and may be left blank.')
1016
	));
1017

    
1018
	$section->addInput(new Form_Select(
1019
		'dn_country',
1020
		'Country Code',
1021
		$pconfig['dn_country'],
1022
		get_cert_country_codes()
1023
	));
1024

    
1025
	$section->addInput(new Form_Input(
1026
		'dn_state',
1027
		'State or Province',
1028
		'text',
1029
		$pconfig['dn_state'],
1030
		['placeholder' => 'e.g. Texas']
1031
	));
1032

    
1033
	$section->addInput(new Form_Input(
1034
		'dn_city',
1035
		'City',
1036
		'text',
1037
		$pconfig['dn_city'],
1038
		['placeholder' => 'e.g. Austin']
1039
	));
1040

    
1041
	$section->addInput(new Form_Input(
1042
		'dn_organization',
1043
		'Organization',
1044
		'text',
1045
		$pconfig['dn_organization'],
1046
		['placeholder' => 'e.g. My Company Inc']
1047
	));
1048

    
1049
	$section->addInput(new Form_Input(
1050
		'dn_organizationalunit',
1051
		'Organizational Unit',
1052
		'text',
1053
		$pconfig['dn_organizationalunit'],
1054
		['placeholder' => 'e.g. My Department Name (optional)']
1055
	));
1056

    
1057
	$form->add($section);
1058
	$section = new Form_Section('External Signing Request');
1059
	$section->addClass('toggle-external collapse');
1060

    
1061
	$section->addInput(new Form_Select(
1062
		'csr_keytype',
1063
		'*Key type',
1064
		$pconfig['csr_keytype'],
1065
		array_combine($cert_keytypes, $cert_keytypes)
1066
	));
1067

    
1068
	$group = new Form_Group($i == 0 ? '*Key length':'');
1069
	$group->addClass('csr_rsakeys');
1070
	$group->add(new Form_Select(
1071
		'csr_keylen',
1072
		null,
1073
		$pconfig['csr_keylen'],
1074
		array_combine($cert_keylens, $cert_keylens)
1075
	))->setHelp('The length to use when generating a new RSA key, in bits. %1$s' .
1076
		'The Key Length should not be lower than 2048 or some platforms ' .
1077
		'may consider the certificate invalid.', '<br/>');
1078
	$section->add($group);
1079

    
1080
	$group = new Form_Group($i == 0 ? '*Elliptic Curve Name':'');
1081
	$group->addClass('csr_ecnames');
1082
	$group->add(new Form_Select(
1083
		'csr_ecname',
1084
		null,
1085
		$pconfig['csr_ecname'],
1086
		$openssl_ecnames
1087
	));
1088
	$section->add($group);
1089

    
1090
	$section->addInput(new Form_Select(
1091
		'csr_digest_alg',
1092
		'*Digest Algorithm',
1093
		$pconfig['csr_digest_alg'],
1094
		array_combine($openssl_digest_algs, $openssl_digest_algs)
1095
	))->setHelp('The digest method used when the certificate is signed. %1$s' .
1096
		'The best practice is to use SHA256 or higher. '.
1097
		'Some services and platforms, such as the GUI web server and OpenVPN, consider weaker digest algorithms invalid.', '<br/>');
1098

    
1099
	$section->addInput(new Form_Input(
1100
		'csr_dn_commonname',
1101
		'*Common Name',
1102
		'text',
1103
		$pconfig['csr_dn_commonname'],
1104
		['placeholder' => 'e.g. internal-ca']
1105
	));
1106

    
1107
	$section->addInput(new Form_StaticText(
1108
		null,
1109
		gettext('The following certificate subject components are optional and may be left blank.')
1110
	));
1111

    
1112
	$section->addInput(new Form_Select(
1113
		'csr_dn_country',
1114
		'Country Code',
1115
		$pconfig['csr_dn_country'],
1116
		get_cert_country_codes()
1117
	));
1118

    
1119
	$section->addInput(new Form_Input(
1120
		'csr_dn_state',
1121
		'State or Province',
1122
		'text',
1123
		$pconfig['csr_dn_state'],
1124
		['placeholder' => 'e.g. Texas']
1125
	));
1126

    
1127
	$section->addInput(new Form_Input(
1128
		'csr_dn_city',
1129
		'City',
1130
		'text',
1131
		$pconfig['csr_dn_city'],
1132
		['placeholder' => 'e.g. Austin']
1133
	));
1134

    
1135
	$section->addInput(new Form_Input(
1136
		'csr_dn_organization',
1137
		'Organization',
1138
		'text',
1139
		$pconfig['csr_dn_organization'],
1140
		['placeholder' => 'e.g. My Company Inc']
1141
	));
1142

    
1143
	$section->addInput(new Form_Input(
1144
		'csr_dn_organizationalunit',
1145
		'Organizational Unit',
1146
		'text',
1147
		$pconfig['csr_dn_organizationalunit'],
1148
		['placeholder' => 'e.g. My Department Name (optional)']
1149
	));
1150

    
1151
	$form->add($section);
1152
	$section = new Form_Section('Choose an Existing Certificate');
1153
	$section->addClass('toggle-existing collapse');
1154

    
1155
	$existCerts = array();
1156

    
1157
	foreach ($config['cert'] as $cert) {
1158
		if (!is_array($cert) || empty($cert)) {
1159
			continue;
1160
		}
1161

    
1162
		if (isset($userid) &&
1163
		    in_array($cert['refid'], config_get_path("system/user/{$userid}/cert", []))) {
1164
			continue;
1165
		}
1166

    
1167
		$ca = lookup_ca($cert['caref']);
1168
		if ($ca) {
1169
			$cert['descr'] .= " (CA: {$ca['descr']})";
1170
		}
1171

    
1172
		if (cert_in_use($cert['refid'])) {
1173
			$cert['descr'] .= " (In Use)";
1174
		}
1175
		if (is_cert_revoked($cert)) {
1176
			$cert['descr'] .= " (Revoked)";
1177
		}
1178

    
1179
		$existCerts[ $cert['refid'] ] = $cert['descr'];
1180
	}
1181

    
1182
	$section->addInput(new Form_Select(
1183
		'certref',
1184
		'*Existing Certificates',
1185
		$pconfig['certref'],
1186
		$existCerts
1187
	));
1188

    
1189
	$form->add($section);
1190

    
1191
	$section = new Form_Section('Certificate Attributes');
1192
	$section->addClass('toggle-external toggle-internal toggle-sign collapse');
1193

    
1194
	$section->addInput(new Form_StaticText(
1195
		gettext('Attribute Notes'),
1196
		'<span class="help-block">'.
1197
		gettext('The following attributes are added to certificates and ' .
1198
		'requests when they are created or signed. These attributes behave ' .
1199
		'differently depending on the selected mode.') .
1200
		'<br/><br/>' .
1201
		'<span class="toggle-internal collapse">' . gettext('For Internal Certificates, these attributes are added directly to the certificate as shown.') . '</span>' .
1202
		'<span class="toggle-external collapse">' .
1203
		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. ') .
1204
		'<br/><br/>' .
1205
		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>' .
1206
		'<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>' .
1207
		'</span>'
1208
	));
1209

    
1210
	$section->addInput(new Form_Select(
1211
		'type',
1212
		'*Certificate Type',
1213
		$pconfig['type'],
1214
		$cert_types
1215
	))->setHelp('Add type-specific usage attributes to the signed certificate.' .
1216
		' Used for placing usage restrictions on, or granting abilities to, ' .
1217
		'the signed certificate.');
1218

    
1219
	if (empty($pconfig['altnames']['item'])) {
1220
		$pconfig['altnames']['item'] = array(
1221
			array('type' => null, 'value' => null)
1222
		);
1223
	}
1224

    
1225
	$counter = 0;
1226
	$numrows = count($pconfig['altnames']['item']) - 1;
1227

    
1228
	foreach ($pconfig['altnames']['item'] as $item) {
1229

    
1230
		$group = new Form_Group($counter == 0 ? 'Alternative Names':'');
1231

    
1232
		$group->add(new Form_Select(
1233
			'altname_type' . $counter,
1234
			'Type',
1235
			$item['type'],
1236
			$cert_altname_types
1237
		))->setHelp(($counter == $numrows) ? 'Type':null);
1238

    
1239
		$group->add(new Form_Input(
1240
			'altname_value' . $counter,
1241
			null,
1242
			'text',
1243
			$item['value']
1244
		))->setHelp(($counter == $numrows) ? 'Value':null);
1245

    
1246
		$group->add(new Form_Button(
1247
			'deleterow' . $counter,
1248
			'Delete',
1249
			null,
1250
			'fa-trash'
1251
		))->addClass('btn-warning');
1252

    
1253
		$group->addClass('repeatable');
1254

    
1255
		$group->setHelp('Enter additional identifiers for the certificate ' .
1256
			'in this list. The Common Name field is automatically ' .
1257
			'added to the certificate as an Alternative Name. ' .
1258
			'The signing CA may ignore or change these values.');
1259

    
1260
		$section->add($group);
1261

    
1262
		$counter++;
1263
	}
1264

    
1265
	$section->addInput(new Form_Button(
1266
		'addrow',
1267
		'Add SAN Row',
1268
		null,
1269
		'fa-plus'
1270
	))->addClass('btn-success');
1271

    
1272
	$form->add($section);
1273

    
1274
	if (($act == 'edit') && !empty($pconfig['key'])) {
1275
		$form->addGlobal(new Form_Button(
1276
			'exportpkey',
1277
			'Export Private Key',
1278
			null,
1279
			'fa-key'
1280
		))->addClass('btn-primary');
1281
		$form->addGlobal(new Form_Button(
1282
			'exportp12',
1283
			'Export PKCS#12',
1284
			null,
1285
			'fa-archive'
1286
		))->addClass('btn-primary');
1287
	}
1288

    
1289
	print $form;
1290

    
1291
} elseif ($act == "csr" || (($_POST['save'] == gettext("Update")) && $input_errors)) {
1292
	$form = new Form(false);
1293
	$form->setAction('system_certmanager.php?act=csr');
1294

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

    
1297
	$section->addInput(new Form_Input(
1298
		'descr',
1299
		'*Descriptive name',
1300
		'text',
1301
		$pconfig['descr']
1302
	))->setHelp('The name of this entry as displayed in the GUI for reference.%s' .
1303
		'This name can contain spaces but it cannot contain any of the ' .
1304
		'following characters: %s', '<br/>', "?, >, <, &, /, \, \", '");
1305

    
1306
	$section->addInput(new Form_Textarea(
1307
		'csr',
1308
		'Signing request data',
1309
		$pconfig['csr']
1310
	))->setReadonly()
1311
	  ->setWidth(7)
1312
	  ->setHelp('Copy the certificate signing data from here and forward it to a certificate authority for signing.');
1313

    
1314
	$section->addInput(new Form_Textarea(
1315
		'cert',
1316
		'*Final certificate data',
1317
		$pconfig['cert']
1318
	))->setWidth(7)
1319
	  ->setHelp('Paste the certificate received from the certificate authority here.');
1320

    
1321
	if (isset($id) && $thiscert) {
1322
		$form->addGlobal(new Form_Input(
1323
			'id',
1324
			null,
1325
			'hidden',
1326
			$id
1327
		));
1328

    
1329
		$form->addGlobal(new Form_Input(
1330
			'act',
1331
			null,
1332
			'hidden',
1333
			'csr'
1334
		));
1335
	}
1336

    
1337
	$form->add($section);
1338

    
1339
	$form->addGlobal(new Form_Button(
1340
		'save',
1341
		'Update',
1342
		null,
1343
		'fa-save'
1344
	))->addClass('btn-primary');
1345

    
1346
	print($form);
1347
} else {
1348
?>
1349
<div class="panel panel-default" id="search-panel">
1350
	<div class="panel-heading">
1351
		<h2 class="panel-title">
1352
			<?=gettext('Search')?>
1353
			<span class="widget-heading-icon pull-right">
1354
				<a data-toggle="collapse" href="#search-panel_panel-body">
1355
					<i class="fa fa-plus-circle"></i>
1356
				</a>
1357
			</span>
1358
		</h2>
1359
	</div>
1360
	<div id="search-panel_panel-body" class="panel-body collapse in">
1361
		<div class="form-group">
1362
			<label class="col-sm-2 control-label">
1363
				<?=gettext("Search term")?>
1364
			</label>
1365
			<div class="col-sm-5"><input class="form-control" name="searchstr" id="searchstr" type="text"/></div>
1366
			<div class="col-sm-2">
1367
				<select id="where" class="form-control">
1368
					<option value="0"><?=gettext("Name")?></option>
1369
					<option value="1"><?=gettext("Distinguished Name")?></option>
1370
					<option value="2" selected><?=gettext("Both")?></option>
1371
				</select>
1372
			</div>
1373
			<div class="col-sm-3">
1374
				<a id="btnsearch" title="<?=gettext("Search")?>" class="btn btn-primary btn-sm"><i class="fa fa-search icon-embed-btn"></i><?=gettext("Search")?></a>
1375
				<a id="btnclear" title="<?=gettext("Clear")?>" class="btn btn-info btn-sm"><i class="fa fa-undo icon-embed-btn"></i><?=gettext("Clear")?></a>
1376
			</div>
1377
			<div class="col-sm-10 col-sm-offset-2">
1378
				<span class="help-block"><?=gettext('Enter a search string or *nix regular expression to search certificate names and distinguished names.')?></span>
1379
			</div>
1380
		</div>
1381
	</div>
1382
</div>
1383
<div class="panel panel-default">
1384
	<div class="panel-heading"><h2 class="panel-title"><?=gettext('Certificates')?></h2></div>
1385
	<div class="panel-body">
1386
		<div class="table-responsive">
1387
		<table class="table table-striped table-hover sortable-theme-bootstrap" data-sortable>
1388
			<thead>
1389
				<tr>
1390
					<th><?=gettext("Name")?></th>
1391
					<th><?=gettext("Issuer")?></th>
1392
					<th><?=gettext("Distinguished Name")?></th>
1393
					<th><?=gettext("In Use")?></th>
1394

    
1395
					<th class="col-sm-2"><?=gettext("Actions")?></th>
1396
				</tr>
1397
			</thead>
1398
			<tbody>
1399
<?php
1400

    
1401
$pluginparams = array();
1402
$pluginparams['type'] = 'certificates';
1403
$pluginparams['event'] = 'used_certificates';
1404
$certificates_used_by_packages = pkg_call_plugins('plugin_certificates', $pluginparams);
1405
foreach ($a_cert as $cert):
1406
	if (!is_array($cert) || empty($cert)) {
1407
		continue;
1408
	}
1409
	$name = htmlspecialchars($cert['descr']);
1410
	if ($cert['crt']) {
1411
		$subj = cert_get_subject($cert['crt']);
1412
		$issuer = cert_get_issuer($cert['crt']);
1413
		$purpose = cert_get_purpose($cert['crt']);
1414

    
1415
		if ($subj == $issuer) {
1416
			$caname = '<i>'. gettext("self-signed") .'</i>';
1417
		} else {
1418
			$caname = '<i>'. gettext("external").'</i>';
1419
		}
1420

    
1421
		$subj = htmlspecialchars(cert_escape_x509_chars($subj, true));
1422
	} else {
1423
		$subj = "";
1424
		$issuer = "";
1425
		$purpose = "";
1426
		$startdate = "";
1427
		$enddate = "";
1428
		$caname = "<em>" . gettext("private key only") . "</em>";
1429
	}
1430

    
1431
	if ($cert['csr']) {
1432
		$subj = htmlspecialchars(cert_escape_x509_chars(csr_get_subject($cert['csr']), true));
1433
		$caname = "<em>" . gettext("external - signature pending") . "</em>";
1434
	}
1435

    
1436
	$ca = lookup_ca($cert['caref']);
1437
	if ($ca) {
1438
		$caname = htmlspecialchars($ca['descr']);
1439
	}
1440
?>
1441
				<tr>
1442
					<td>
1443
						<?=$name?><br />
1444
						<?php if ($cert['type']): ?>
1445
							<i><?=$cert_types[$cert['type']]?></i><br />
1446
						<?php endif?>
1447
						<?php if (is_array($purpose)): ?>
1448
							CA: <b><?=$purpose['ca']?></b><br/>
1449
							<?=gettext("Server")?>: <b><?=$purpose['server']?></b><br/>
1450
						<?php endif?>
1451
					</td>
1452
					<td><?=$caname?></td>
1453
					<td>
1454
						<?=$subj?>
1455
						<?= cert_print_infoblock($cert); ?>
1456
						<?php cert_print_dates($cert);?>
1457
					</td>
1458
					<td>
1459
						<?php if (is_cert_revoked($cert)): ?>
1460
							<i><?=gettext("Revoked")?></i>
1461
						<?php endif?>
1462
						<?php if (is_webgui_cert($cert['refid'])): ?>
1463
							<?=gettext("webConfigurator")?>
1464
						<?php endif?>
1465
						<?php if (is_user_cert($cert['refid'])): ?>
1466
							<?=gettext("User Cert")?>
1467
						<?php endif?>
1468
						<?php if (is_openvpn_server_cert($cert['refid'])): ?>
1469
							<?=gettext("OpenVPN Server")?>
1470
						<?php endif?>
1471
						<?php if (is_openvpn_client_cert($cert['refid'])): ?>
1472
							<?=gettext("OpenVPN Client")?>
1473
						<?php endif?>
1474
						<?php if (is_ipsec_cert($cert['refid'])): ?>
1475
							<?=gettext("IPsec Tunnel")?>
1476
						<?php endif?>
1477
						<?php if (is_captiveportal_cert($cert['refid'])): ?>
1478
							<?=gettext("Captive Portal")?>
1479
						<?php endif?>
1480
						<?php if (is_unbound_cert($cert['refid'])): ?>
1481
							<?=gettext("DNS Resolver")?>
1482
						<?php endif?>
1483
						<?php echo cert_usedby_description($cert['refid'], $certificates_used_by_packages); ?>
1484
					</td>
1485
					<td>
1486
						<?php if (!$cert['csr']): ?>
1487
							<a href="system_certmanager.php?act=edit&amp;id=<?=$cert['refid']?>" class="fa fa-pencil" title="<?=gettext("Edit Certificate")?>"></a>
1488
							<a href="system_certmanager.php?act=exp&amp;id=<?=$cert['refid']?>" class="fa fa-certificate" title="<?=gettext("Export Certificate")?>"></a>
1489
							<?php if ($cert['prv']): ?>
1490
								<a href="system_certmanager.php?act=key&amp;id=<?=$cert['refid']?>" class="fa fa-key" title="<?=gettext("Export Key")?>"></a>
1491
								<a href="system_certmanager.php?act=p12&amp;id=<?=$cert['refid']?>" class="fa fa-archive" title="<?=gettext("Export PCKS#12 Archive without Encryption")?>"></a>
1492
							<?php endif?>
1493
							<?php if (is_cert_locally_renewable($cert)): ?>
1494
								<a href="system_certmanager_renew.php?type=cert&amp;refid=<?=$cert['refid']?>" class="fa fa-repeat" title="<?=gettext("Reissue/Renew")?>"></a>
1495
							<?php endif ?>
1496
						<?php else: ?>
1497
							<a href="system_certmanager.php?act=csr&amp;id=<?=$cert['refid']?>" class="fa fa-pencil" title="<?=gettext("Update CSR")?>"></a>
1498
							<a href="system_certmanager.php?act=req&amp;id=<?=$cert['refid']?>" class="fa fa-sign-in" title="<?=gettext("Export Request")?>"></a>
1499
							<a href="system_certmanager.php?act=key&amp;id=<?=$cert['refid']?>" class="fa fa-key" title="<?=gettext("Export Key")?>"></a>
1500
						<?php endif?>
1501
						<?php if (!cert_in_use($cert['refid'])): ?>
1502
							<a href="system_certmanager.php?act=del&amp;id=<?=$cert['refid']?>" class="fa fa-trash" title="<?=gettext("Delete Certificate")?>" usepost></a>
1503
						<?php endif?>
1504
					</td>
1505
				</tr>
1506
<?php
1507
	endforeach; ?>
1508
			</tbody>
1509
		</table>
1510
		</div>
1511
	</div>
1512
</div>
1513

    
1514
<nav class="action-buttons">
1515
	<a href="?act=new" class="btn btn-success btn-sm">
1516
		<i class="fa fa-plus icon-embed-btn"></i>
1517
		<?=gettext("Add/Sign")?>
1518
	</a>
1519
</nav>
1520
<script type="text/javascript">
1521
//<![CDATA[
1522

    
1523
events.push(function() {
1524

    
1525
	// Make these controls plain buttons
1526
	$("#btnsearch").prop('type', 'button');
1527
	$("#btnclear").prop('type', 'button');
1528

    
1529
	// Search for a term in the entry name and/or dn
1530
	$("#btnsearch").click(function() {
1531
		var searchstr = $('#searchstr').val().toLowerCase();
1532
		var table = $("table tbody");
1533
		var where = $('#where').val();
1534

    
1535
		table.find('tr').each(function (i) {
1536
			var $tds = $(this).find('td'),
1537
				shortname = $tds.eq(0).text().trim().toLowerCase(),
1538
				dn = $tds.eq(2).text().trim().toLowerCase();
1539

    
1540
			regexp = new RegExp(searchstr);
1541
			if (searchstr.length > 0) {
1542
				if (!(regexp.test(shortname) && (where != 1)) && !(regexp.test(dn) && (where != 0))) {
1543
					$(this).hide();
1544
				} else {
1545
					$(this).show();
1546
				}
1547
			} else {
1548
				$(this).show();	// A blank search string shows all
1549
			}
1550
		});
1551
	});
1552

    
1553
	// Clear the search term and unhide all rows (that were hidden during a previous search)
1554
	$("#btnclear").click(function() {
1555
		var table = $("table tbody");
1556

    
1557
		$('#searchstr').val("");
1558

    
1559
		table.find('tr').each(function (i) {
1560
			$(this).show();
1561
		});
1562
	});
1563

    
1564
	// Hitting the enter key will do the same as clicking the search button
1565
	$("#searchstr").on("keyup", function (event) {
1566
		if (event.keyCode == 13) {
1567
			$("#btnsearch").get(0).click();
1568
		}
1569
	});
1570
});
1571
//]]>
1572
</script>
1573
<?php
1574
	include("foot.inc");
1575
	exit;
1576
}
1577

    
1578

    
1579
?>
1580
<script type="text/javascript">
1581
//<![CDATA[
1582
events.push(function() {
1583

    
1584
	$('.import_type_toggle').click(function() {
1585
		var x509 = (this.value === 'x509');
1586
		hideInput('cert', !x509);
1587
		setRequired('cert', x509);
1588
		hideInput('key', !x509);
1589
		setRequired('key', x509);
1590
		hideInput('pkcs12_cert', x509);
1591
		setRequired('pkcs12_cert', !x509);
1592
		hideInput('pkcs12_pass', x509);
1593
		hideCheckbox('pkcs12_intermediate', x509);
1594
	});
1595
	if ($('input[name=import_type]:checked').val() == 'x509') {
1596
		hideInput('pkcs12_cert', true);
1597
		setRequired('pkcs12_cert', false);
1598
		hideInput('pkcs12_pass', true);
1599
		hideCheckbox('pkcs12_intermediate', true);
1600
		hideInput('cert', false);
1601
		setRequired('cert', true);
1602
		hideInput('key', false);
1603
		setRequired('key', true);
1604
	} else if ($('input[name=import_type]:checked').val() == 'pkcs12') {
1605
		hideInput('cert', true);
1606
		setRequired('cert', false);
1607
		hideInput('key', true);
1608
		setRequired('key', false);
1609
		setRequired('pkcs12_cert', false);
1610
	}
1611

    
1612
<?php if ($internal_ca_count): ?>
1613
	function internalca_change() {
1614

    
1615
		caref = $('#caref').val();
1616

    
1617
		switch (caref) {
1618
<?php
1619
			foreach ($a_ca as $ca):
1620
				if (!$ca['prv']) {
1621
					continue;
1622
				}
1623

    
1624
				$subject = @cert_get_subject_hash($ca['crt']);
1625
				if (!is_array($subject) || empty($subject)) {
1626
					continue;
1627
				}
1628
?>
1629
				case "<?=$ca['refid'];?>":
1630
					$('#dn_country').val(<?=json_encode(cert_escape_x509_chars($subject['C'], true));?>);
1631
					$('#dn_state').val(<?=json_encode(cert_escape_x509_chars($subject['ST'], true));?>);
1632
					$('#dn_city').val(<?=json_encode(cert_escape_x509_chars($subject['L'], true));?>);
1633
					$('#dn_organization').val(<?=json_encode(cert_escape_x509_chars($subject['O'], true));?>);
1634
					$('#dn_organizationalunit').val(<?=json_encode(cert_escape_x509_chars($subject['OU'], true));?>);
1635
					break;
1636
<?php
1637
			endforeach;
1638
?>
1639
		}
1640
	}
1641

    
1642
	function set_csr_ro() {
1643
		var newcsr = ($('#csrtosign').val() == "new");
1644

    
1645
		$('#csrpaste').attr('readonly', !newcsr);
1646
		$('#keypaste').attr('readonly', !newcsr);
1647
		setRequired('csrpaste', newcsr);
1648
	}
1649

    
1650
	function check_lifetime() {
1651
		var maxserverlife = <?= $cert_strict_values['max_server_cert_lifetime'] ?>;
1652
		var ltid = '#lifetime';
1653
		if ($('#method').val() == "sign") {
1654
			ltid = '#csrsign_lifetime';
1655
		}
1656
		if (($('#type').val() == "server") && (parseInt($(ltid).val()) > maxserverlife)) {
1657
			$(ltid).parent().parent().removeClass("text-normal").addClass("text-warning");
1658
			$(ltid).removeClass("text-normal").addClass("text-warning");
1659
		} else {
1660
			$(ltid).parent().parent().removeClass("text-warning").addClass("text-normal");
1661
			$(ltid).removeClass("text-warning").addClass("text-normal");
1662
		}
1663
	}
1664
	function check_keylen() {
1665
		var min_keylen = <?= $cert_strict_values['min_private_key_bits'] ?>;
1666
		var klid = '#keylen';
1667
		if ($('#method').val() == "external") {
1668
			klid = '#csr_keylen';
1669
		}
1670
		/* Color the Parent/Label */
1671
		if (parseInt($(klid).val()) < min_keylen) {
1672
			$(klid).parent().parent().removeClass("text-normal").addClass("text-warning");
1673
		} else {
1674
			$(klid).parent().parent().removeClass("text-warning").addClass("text-normal");
1675
		}
1676
		/* Color individual options */
1677
		$(klid + " option").filter(function() {
1678
			return parseInt($(this).val()) < min_keylen;
1679
		}).removeClass("text-normal").addClass("text-warning").siblings().removeClass("text-warning").addClass("text-normal");
1680
	}
1681

    
1682
	function check_digest() {
1683
		var weak_algs = <?= json_encode($cert_strict_values['digest_blacklist']) ?>;
1684
		var daid = '#digest_alg';
1685
		if ($('#method').val() == "external") {
1686
			daid = '#csr_digest_alg';
1687
		} else if ($('#method').val() == "sign") {
1688
			daid = '#csrsign_digest_alg';
1689
		}
1690
		/* Color the Parent/Label */
1691
		if (jQuery.inArray($(daid).val(), weak_algs) > -1) {
1692
			$(daid).parent().parent().removeClass("text-normal").addClass("text-warning");
1693
		} else {
1694
			$(daid).parent().parent().removeClass("text-warning").addClass("text-normal");
1695
		}
1696
		/* Color individual options */
1697
		$(daid + " option").filter(function() {
1698
			return (jQuery.inArray($(this).val(), weak_algs) > -1);
1699
		}).removeClass("text-normal").addClass("text-warning").siblings().removeClass("text-warning").addClass("text-normal");
1700
	}
1701

    
1702
	// ---------- Click checkbox handlers ---------------------------------------------------------
1703

    
1704
	$('#type').on('change', function() {
1705
		check_lifetime();
1706
	});
1707
	$('#method').on('change', function() {
1708
		check_lifetime();
1709
		check_keylen();
1710
		check_digest();
1711
	});
1712
	$('#lifetime').on('change', function() {
1713
		check_lifetime();
1714
	});
1715
	$('#csrsign_lifetime').on('change', function() {
1716
		check_lifetime();
1717
	});
1718

    
1719
	$('#keylen').on('change', function() {
1720
		check_keylen();
1721
	});
1722
	$('#csr_keylen').on('change', function() {
1723
		check_keylen();
1724
	});
1725

    
1726
	$('#digest_alg').on('change', function() {
1727
		check_digest();
1728
	});
1729
	$('#csr_digest_alg').on('change', function() {
1730
		check_digest();
1731
	});
1732

    
1733
	$('#caref').on('change', function() {
1734
		internalca_change();
1735
	});
1736

    
1737
	$('#csrtosign').change(function () {
1738
		set_csr_ro();
1739
	});
1740

    
1741
	function change_keytype() {
1742
		hideClass('rsakeys', ($('#keytype').val() != 'RSA'));
1743
		hideClass('ecnames', ($('#keytype').val() != 'ECDSA'));
1744
	}
1745

    
1746
	$('#keytype').change(function () {
1747
		change_keytype();
1748
	});
1749

    
1750
	function change_csrkeytype() {
1751
		hideClass('csr_rsakeys', ($('#csr_keytype').val() != 'RSA'));
1752
		hideClass('csr_ecnames', ($('#csr_keytype').val() != 'ECDSA'));
1753
	}
1754

    
1755
	$('#csr_keytype').change(function () {
1756
		change_csrkeytype();
1757
	});
1758

    
1759
	// ---------- On initial page load ------------------------------------------------------------
1760

    
1761
	internalca_change();
1762
	set_csr_ro();
1763
	change_keytype();
1764
	change_csrkeytype();
1765
	check_lifetime();
1766
	check_keylen();
1767
	check_digest();
1768

    
1769
	// Suppress "Delete row" button if there are fewer than two rows
1770
	checkLastRow();
1771

    
1772

    
1773
<?php endif; ?>
1774

    
1775

    
1776
});
1777
//]]>
1778
</script>
1779
<?php
1780
include('foot.inc');
(193-193/228)