Project

General

Profile

Download (54.2 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-2022 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
$max_lifetime = cert_get_max_lifetime();
53
$default_lifetime = min(3650, $max_lifetime);
54
$openssl_ecnames = cert_build_curve_list();
55
$class = "success";
56

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

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

    
67
init_config_arr(array('ca'));
68
$a_ca = &$config['ca'];
69

    
70
init_config_arr(array('cert'));
71
$a_cert = &$config['cert'];
72

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

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

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

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

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

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

    
216
if ($_POST['save'] == gettext("Save")) {
217
	/* Creating a new entry */
218
	$input_errors = array();
219
	$pconfig = $_POST;
220

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

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

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

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

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

    
314
	$altnames = array();
315
	do_input_validation($_POST, $reqdfields, $reqdfieldsn, $input_errors);
316

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

    
336
			if (ctype_digit($entry)) {
337
				$entry++;	// Pre-bootstrap code is one-indexed, but the bootstrap code is 0-indexed
338
				$altnames[$entry][$field] = $value;
339
			}
340
		}
341

    
342
		$pconfig['altnames']['item'] = $altnames;
343

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

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

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

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

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

    
447
		$cert['descr'] = $pconfig['descr'];
448

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

    
617
		if (isset($id) && $thiscert) {
618
			$thiscert = $cert;
619
		} elseif ($cert) {
620
			$a_cert[] = $cert;
621
		}
622

    
623
		if (isset($a_user) && isset($userid)) {
624
			$a_user[$userid]['cert'][] = $cert['refid'];
625
		}
626

    
627
		if (!$input_errors) {
628
			write_config($savemsg);
629
		}
630

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

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

    
647
	do_input_validation($_POST, $reqdfields, $reqdfieldsn, $input_errors);
648

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

    
653
	$mod_csr = cert_get_publickey($pconfig['csr'], false, 'csr');
654
	$mod_cert = cert_get_publickey($pconfig['cert'], false);
655

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

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

    
674
$pgtitle = array(gettext("System"), gettext("Certificate Manager"), gettext("Certificates"));
675
$pglinks = array("", "system_camanager.php", "system_certmanager.php");
676

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

    
684
if ($input_errors) {
685
	print_input_errors($input_errors);
686
}
687

    
688
if ($savemsg) {
689
	print_info_box($savemsg, $class);
690
}
691

    
692
$tab_array = array();
693
$tab_array[] = array(gettext("CAs"), false, "system_camanager.php");
694
$tab_array[] = array(gettext("Certificates"), true, "system_certmanager.php");
695
$tab_array[] = array(gettext("Certificate Revocation"), false, "system_crlmanager.php");
696
display_top_tabs($tab_array);
697

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

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

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

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

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

    
739
	$section = new Form_Section($maintitle);
740

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

    
750
	$section->addInput(new Form_Input(
751
		'descr',
752
		'*Descriptive name',
753
		'text',
754
		($a_user && empty($pconfig['descr'])) ? $a_user[$userid]['name'] : $pconfig['descr']
755
	))->addClass('toggle-internal toggle-import toggle-edit toggle-external toggle-sign toggle-existing collapse');
756

    
757
	if (!empty($pconfig['cert'])) {
758
		$section->addInput(new Form_StaticText(
759
			"Subject",
760
			htmlspecialchars(cert_get_subject($pconfig['cert'], false))
761
		))->addClass('toggle-edit collapse');
762
	}
763

    
764
	$form->add($section);
765

    
766
	// Return an array containing the IDs od all CAs
767
	function list_cas() {
768
		global $a_ca;
769
		$allCas = array();
770

    
771
		foreach ($a_ca as $ca) {
772
			if ($ca['prv']) {
773
				$allCas[$ca['refid']] = $ca['descr'];
774
			}
775
		}
776

    
777
		return $allCas;
778
	}
779

    
780
	// Return an array containing the IDs od all CSRs
781
	function list_csrs() {
782
		global $config;
783
		$allCsrs = array();
784

    
785
		foreach ($config['cert'] as $cert) {
786
			if ($cert['csr']) {
787
				$allCsrs[$cert['refid']] = $cert['descr'];
788
			}
789
		}
790

    
791
		return ['new' => gettext('New CSR (Paste below)')] + $allCsrs;
792
	}
793

    
794
	$section = new Form_Section('Sign CSR');
795
	$section->addClass('toggle-sign collapse');
796

    
797
	$section->AddInput(new Form_Select(
798
		'catosignwith',
799
		'*CA to sign with',
800
		$pconfig['catosignwith'],
801
		list_cas()
802
	));
803

    
804
	$section->AddInput(new Form_Select(
805
		'csrtosign',
806
		'*CSR to sign',
807
		isset($pconfig['csrtosign']) ? $pconfig['csrtosign'] : 'new',
808
		list_csrs()
809
	));
810

    
811
	$section->addInput(new Form_Textarea(
812
		'csrpaste',
813
		'CSR data',
814
		$pconfig['csrpaste']
815
	))->setHelp('Paste a Certificate Signing Request in X.509 PEM format here.');
816

    
817
	$section->addInput(new Form_Textarea(
818
		'keypaste',
819
		'Key data',
820
		$pconfig['keypaste']
821
	))->setHelp('Optionally paste a private key here. The key will be associated with the newly signed certificate in %1$s', $g['product_label']);
822

    
823
	$section->addInput(new Form_Input(
824
		'csrsign_lifetime',
825
		'*Certificate Lifetime (days)',
826
		'number',
827
		$pconfig['csrsign_lifetime'] ? $pconfig['csrsign_lifetime']:$default_lifetime,
828
		['max' => $max_lifetime]
829
	))->setHelp('The length of time the signed certificate will be valid, in days. %1$s' .
830
		'Server certificates should not have a lifetime over %2$s days or some platforms ' .
831
		'may consider the certificate invalid.', '<br/>', $cert_strict_values['max_server_cert_lifetime']);
832
	$section->addInput(new Form_Select(
833
		'csrsign_digest_alg',
834
		'*Digest Algorithm',
835
		$pconfig['csrsign_digest_alg'],
836
		array_combine($openssl_digest_algs, $openssl_digest_algs)
837
	))->setHelp('The digest method used when the certificate is signed. %1$s' .
838
		'The best practice is to use an algorithm stronger than SHA1. '.
839
		'Some platforms may consider weaker digest algorithms invalid', '<br/>');
840

    
841
	$form->add($section);
842

    
843
	if ($act == 'edit') {
844
		$editimport = gettext("Edit Certificate");
845
	} else {
846
		$editimport = gettext("Import Certificate");
847
	}
848

    
849
	$section = new Form_Section($editimport);
850
	$section->addClass('toggle-import toggle-edit collapse');
851

    
852
	$group = new Form_Group('Certificate Type');
853

    
854
	$group->add(new Form_Checkbox(
855
		'import_type',
856
		'Certificate Type',
857
		'X.509 (PEM)',
858
		(!isset($pconfig['import_type']) || $pconfig['import_type'] == 'x509'),
859
		'x509'
860
	))->displayAsRadio()->addClass('import_type_toggle');
861

    
862
	$group->add(new Form_Checkbox(
863
		'import_type',
864
		'Certificate Type',
865
		'PKCS #12 (PFX)',
866
		(isset($pconfig['import_type']) && $pconfig['import_type'] == 'pkcs12'),
867
		'pkcs12'
868
	))->displayAsRadio()->addClass('import_type_toggle');
869

    
870
	$section->add($group);
871

    
872
	$section->addInput(new Form_Textarea(
873
		'cert',
874
		'*Certificate data',
875
		$pconfig['cert']
876
	))->setHelp('Paste a certificate in X.509 PEM format here.');
877

    
878
	$section->addInput(new Form_Textarea(
879
		'key',
880
		'Private key data',
881
		$pconfig['key']
882
	))->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.');
883

    
884
	$section->addInput(new Form_Input(
885
		'pkcs12_cert',
886
		'PKCS #12 certificate',
887
		'file',
888
		$pconfig['pkcs12_cert']
889
	))->setHelp('Select a PKCS #12 certificate store.');
890

    
891
	$section->addInput(new Form_Input(
892
		'pkcs12_pass',
893
		'PKCS #12 certificate password',
894
		'password',
895
		$pconfig['pkcs12_pass']
896
	))->setHelp('Enter the password to unlock the PKCS #12 certificate store.');
897

    
898
	$section->addInput(new Form_Checkbox(
899
		'pkcs12_intermediate',
900
		'Intermediates',
901
		'Import intermediate CAs',
902
		isset($pconfig['pkcs12_intermediate'])
903
	))->setHelp('Import any intermediate certificate authorities found in the PKCS #12 certificate store.');
904

    
905
	if ($act == 'edit') {
906
		$section->addInput(new Form_Input(
907
			'exportpass',
908
			'Export Password',
909
			'password',
910
			null,
911
			['placeholder' => gettext('Export Password'), 'autocomplete' => 'new-password']
912
		))->setHelp('Enter the password to use when using the export buttons below (not stored)')->addClass('toggle-edit collapse');
913
	}
914

    
915
	$form->add($section);
916
	$section = new Form_Section('Internal Certificate');
917
	$section->addClass('toggle-internal collapse');
918

    
919
	if (!$internal_ca_count) {
920
		$section->addInput(new Form_StaticText(
921
			'*Certificate authority',
922
			gettext('No internal Certificate Authorities have been defined. ') .
923
			gettext('An internal CA must be defined in order to create an internal certificate. ') .
924
			sprintf(gettext('%1$sCreate%2$s an internal CA.'), '<a href="system_camanager.php?act=new&amp;method=internal"> ', '</a>')
925
		));
926
	} else {
927
		$allCas = array();
928
		foreach ($a_ca as $ca) {
929
			if (!$ca['prv']) {
930
				continue;
931
			}
932

    
933
			$allCas[ $ca['refid'] ] = $ca['descr'];
934
		}
935

    
936
		$section->addInput(new Form_Select(
937
			'caref',
938
			'*Certificate authority',
939
			$pconfig['caref'],
940
			$allCas
941
		));
942
	}
943

    
944
	$section->addInput(new Form_Select(
945
		'keytype',
946
		'*Key type',
947
		$pconfig['keytype'],
948
		array_combine($cert_keytypes, $cert_keytypes)
949
	));
950

    
951
	$group = new Form_Group($i == 0 ? '*Key length':'');
952
	$group->addClass('rsakeys');
953
	$group->add(new Form_Select(
954
		'keylen',
955
		null,
956
		$pconfig['keylen'],
957
		array_combine($cert_keylens, $cert_keylens)
958
	))->setHelp('The length to use when generating a new RSA key, in bits. %1$s' .
959
		'The Key Length should not be lower than 2048 or some platforms ' .
960
		'may consider the certificate invalid.', '<br/>');
961
	$section->add($group);
962

    
963
	$group = new Form_Group($i == 0 ? '*Elliptic Curve Name':'');
964
	$group->addClass('ecnames');
965
	$group->add(new Form_Select(
966
		'ecname',
967
		null,
968
		$pconfig['ecname'],
969
		$openssl_ecnames
970
	))->setHelp('Curves may not be compatible with all uses. Known compatible curve uses are denoted in brackets.');
971
	$section->add($group);
972

    
973
	$section->addInput(new Form_Select(
974
		'digest_alg',
975
		'*Digest Algorithm',
976
		$pconfig['digest_alg'],
977
		array_combine($openssl_digest_algs, $openssl_digest_algs)
978
	))->setHelp('The digest method used when the certificate is signed. %1$s' .
979
		'The best practice is to use an algorithm stronger than SHA1. '.
980
		'Some platforms may consider weaker digest algorithms invalid', '<br/>');
981

    
982
	$section->addInput(new Form_Input(
983
		'lifetime',
984
		'*Lifetime (days)',
985
		'number',
986
		$pconfig['lifetime'],
987
		['max' => $max_lifetime]
988
	))->setHelp('The length of time the signed certificate will be valid, in days. %1$s' .
989
		'Server certificates should not have a lifetime over %2$s days or some platforms ' .
990
		'may consider the certificate invalid.', '<br/>', $cert_strict_values['max_server_cert_lifetime']);
991

    
992
	$section->addInput(new Form_Input(
993
		'dn_commonname',
994
		'*Common Name',
995
		'text',
996
		$pconfig['dn_commonname'],
997
		['placeholder' => 'e.g. www.example.com']
998
	));
999

    
1000
	$section->addInput(new Form_StaticText(
1001
		null,
1002
		gettext('The following certificate subject components are optional and may be left blank.')
1003
	));
1004

    
1005
	$section->addInput(new Form_Select(
1006
		'dn_country',
1007
		'Country Code',
1008
		$pconfig['dn_country'],
1009
		get_cert_country_codes()
1010
	));
1011

    
1012
	$section->addInput(new Form_Input(
1013
		'dn_state',
1014
		'State or Province',
1015
		'text',
1016
		$pconfig['dn_state'],
1017
		['placeholder' => 'e.g. Texas']
1018
	));
1019

    
1020
	$section->addInput(new Form_Input(
1021
		'dn_city',
1022
		'City',
1023
		'text',
1024
		$pconfig['dn_city'],
1025
		['placeholder' => 'e.g. Austin']
1026
	));
1027

    
1028
	$section->addInput(new Form_Input(
1029
		'dn_organization',
1030
		'Organization',
1031
		'text',
1032
		$pconfig['dn_organization'],
1033
		['placeholder' => 'e.g. My Company Inc']
1034
	));
1035

    
1036
	$section->addInput(new Form_Input(
1037
		'dn_organizationalunit',
1038
		'Organizational Unit',
1039
		'text',
1040
		$pconfig['dn_organizationalunit'],
1041
		['placeholder' => 'e.g. My Department Name (optional)']
1042
	));
1043

    
1044
	$form->add($section);
1045
	$section = new Form_Section('External Signing Request');
1046
	$section->addClass('toggle-external collapse');
1047

    
1048
	$section->addInput(new Form_Select(
1049
		'csr_keytype',
1050
		'*Key type',
1051
		$pconfig['csr_keytype'],
1052
		array_combine($cert_keytypes, $cert_keytypes)
1053
	));
1054

    
1055
	$group = new Form_Group($i == 0 ? '*Key length':'');
1056
	$group->addClass('csr_rsakeys');
1057
	$group->add(new Form_Select(
1058
		'csr_keylen',
1059
		null,
1060
		$pconfig['csr_keylen'],
1061
		array_combine($cert_keylens, $cert_keylens)
1062
	))->setHelp('The length to use when generating a new RSA key, in bits. %1$s' .
1063
		'The Key Length should not be lower than 2048 or some platforms ' .
1064
		'may consider the certificate invalid.', '<br/>');
1065
	$section->add($group);
1066

    
1067
	$group = new Form_Group($i == 0 ? '*Elliptic Curve Name':'');
1068
	$group->addClass('csr_ecnames');
1069
	$group->add(new Form_Select(
1070
		'csr_ecname',
1071
		null,
1072
		$pconfig['csr_ecname'],
1073
		$openssl_ecnames
1074
	));
1075
	$section->add($group);
1076

    
1077
	$section->addInput(new Form_Select(
1078
		'csr_digest_alg',
1079
		'*Digest Algorithm',
1080
		$pconfig['csr_digest_alg'],
1081
		array_combine($openssl_digest_algs, $openssl_digest_algs)
1082
	))->setHelp('The digest method used when the certificate is signed. %1$s' .
1083
		'The best practice is to use an algorithm stronger than SHA1. '.
1084
		'Some platforms may consider weaker digest algorithms invalid', '<br/>');
1085

    
1086
	$section->addInput(new Form_Input(
1087
		'csr_dn_commonname',
1088
		'*Common Name',
1089
		'text',
1090
		$pconfig['csr_dn_commonname'],
1091
		['placeholder' => 'e.g. internal-ca']
1092
	));
1093

    
1094
	$section->addInput(new Form_StaticText(
1095
		null,
1096
		gettext('The following certificate subject components are optional and may be left blank.')
1097
	));
1098

    
1099
	$section->addInput(new Form_Select(
1100
		'csr_dn_country',
1101
		'Country Code',
1102
		$pconfig['csr_dn_country'],
1103
		get_cert_country_codes()
1104
	));
1105

    
1106
	$section->addInput(new Form_Input(
1107
		'csr_dn_state',
1108
		'State or Province',
1109
		'text',
1110
		$pconfig['csr_dn_state'],
1111
		['placeholder' => 'e.g. Texas']
1112
	));
1113

    
1114
	$section->addInput(new Form_Input(
1115
		'csr_dn_city',
1116
		'City',
1117
		'text',
1118
		$pconfig['csr_dn_city'],
1119
		['placeholder' => 'e.g. Austin']
1120
	));
1121

    
1122
	$section->addInput(new Form_Input(
1123
		'csr_dn_organization',
1124
		'Organization',
1125
		'text',
1126
		$pconfig['csr_dn_organization'],
1127
		['placeholder' => 'e.g. My Company Inc']
1128
	));
1129

    
1130
	$section->addInput(new Form_Input(
1131
		'csr_dn_organizationalunit',
1132
		'Organizational Unit',
1133
		'text',
1134
		$pconfig['csr_dn_organizationalunit'],
1135
		['placeholder' => 'e.g. My Department Name (optional)']
1136
	));
1137

    
1138
	$form->add($section);
1139
	$section = new Form_Section('Choose an Existing Certificate');
1140
	$section->addClass('toggle-existing collapse');
1141

    
1142
	$existCerts = array();
1143

    
1144
	foreach ($config['cert'] as $cert) {
1145
		if (!is_array($cert) || empty($cert)) {
1146
			continue;
1147
		}
1148
		if (is_array($config['system']['user'][$userid]['cert'])) { // Could be MIA!
1149
			if (isset($userid) && in_array($cert['refid'], $config['system']['user'][$userid]['cert'])) {
1150
				continue;
1151
			}
1152
		}
1153

    
1154
		$ca = lookup_ca($cert['caref']);
1155
		if ($ca) {
1156
			$cert['descr'] .= " (CA: {$ca['descr']})";
1157
		}
1158

    
1159
		if (cert_in_use($cert['refid'])) {
1160
			$cert['descr'] .= " (In Use)";
1161
		}
1162
		if (is_cert_revoked($cert)) {
1163
			$cert['descr'] .= " (Revoked)";
1164
		}
1165

    
1166
		$existCerts[ $cert['refid'] ] = $cert['descr'];
1167
	}
1168

    
1169
	$section->addInput(new Form_Select(
1170
		'certref',
1171
		'*Existing Certificates',
1172
		$pconfig['certref'],
1173
		$existCerts
1174
	));
1175

    
1176
	$form->add($section);
1177

    
1178
	$section = new Form_Section('Certificate Attributes');
1179
	$section->addClass('toggle-external toggle-internal toggle-sign collapse');
1180

    
1181
	$section->addInput(new Form_StaticText(
1182
		gettext('Attribute Notes'),
1183
		'<span class="help-block">'.
1184
		gettext('The following attributes are added to certificates and ' .
1185
		'requests when they are created or signed. These attributes behave ' .
1186
		'differently depending on the selected mode.') .
1187
		'<br/><br/>' .
1188
		'<span class="toggle-internal collapse">' . gettext('For Internal Certificates, these attributes are added directly to the certificate as shown.') . '</span>' .
1189
		'<span class="toggle-external collapse">' .
1190
		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. ') .
1191
		'<br/><br/>' .
1192
		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>' .
1193
		'<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>' .
1194
		'</span>'
1195
	));
1196

    
1197
	$section->addInput(new Form_Select(
1198
		'type',
1199
		'*Certificate Type',
1200
		$pconfig['type'],
1201
		$cert_types
1202
	))->setHelp('Add type-specific usage attributes to the signed certificate.' .
1203
		' Used for placing usage restrictions on, or granting abilities to, ' .
1204
		'the signed certificate.');
1205

    
1206
	if (empty($pconfig['altnames']['item'])) {
1207
		$pconfig['altnames']['item'] = array(
1208
			array('type' => null, 'value' => null)
1209
		);
1210
	}
1211

    
1212
	$counter = 0;
1213
	$numrows = count($pconfig['altnames']['item']) - 1;
1214

    
1215
	foreach ($pconfig['altnames']['item'] as $item) {
1216

    
1217
		$group = new Form_Group($counter == 0 ? 'Alternative Names':'');
1218

    
1219
		$group->add(new Form_Select(
1220
			'altname_type' . $counter,
1221
			'Type',
1222
			$item['type'],
1223
			$cert_altname_types
1224
		))->setHelp(($counter == $numrows) ? 'Type':null);
1225

    
1226
		$group->add(new Form_Input(
1227
			'altname_value' . $counter,
1228
			null,
1229
			'text',
1230
			$item['value']
1231
		))->setHelp(($counter == $numrows) ? 'Value':null);
1232

    
1233
		$group->add(new Form_Button(
1234
			'deleterow' . $counter,
1235
			'Delete',
1236
			null,
1237
			'fa-trash'
1238
		))->addClass('btn-warning');
1239

    
1240
		$group->addClass('repeatable');
1241

    
1242
		$group->setHelp('Enter additional identifiers for the certificate ' .
1243
			'in this list. The Common Name field is automatically ' .
1244
			'added to the certificate as an Alternative Name. ' .
1245
			'The signing CA may ignore or change these values.');
1246

    
1247
		$section->add($group);
1248

    
1249
		$counter++;
1250
	}
1251

    
1252
	$section->addInput(new Form_Button(
1253
		'addrow',
1254
		'Add',
1255
		null,
1256
		'fa-plus'
1257
	))->addClass('btn-success');
1258

    
1259
	$form->add($section);
1260

    
1261
	if (($act == 'edit') && !empty($pconfig['key'])) {
1262
		$form->addGlobal(new Form_Button(
1263
			'exportpkey',
1264
			'Export Private Key',
1265
			null,
1266
			'fa-key'
1267
		))->addClass('btn-primary');
1268
		$form->addGlobal(new Form_Button(
1269
			'exportp12',
1270
			'Export PKCS#12',
1271
			null,
1272
			'fa-archive'
1273
		))->addClass('btn-primary');
1274
	}
1275

    
1276
	print $form;
1277

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

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

    
1284
	$section->addInput(new Form_Input(
1285
		'descr',
1286
		'*Descriptive name',
1287
		'text',
1288
		$pconfig['descr']
1289
	));
1290

    
1291
	$section->addInput(new Form_Textarea(
1292
		'csr',
1293
		'Signing request data',
1294
		$pconfig['csr']
1295
	))->setReadonly()
1296
	  ->setWidth(7)
1297
	  ->setHelp('Copy the certificate signing data from here and forward it to a certificate authority for signing.');
1298

    
1299
	$section->addInput(new Form_Textarea(
1300
		'cert',
1301
		'*Final certificate data',
1302
		$pconfig['cert']
1303
	))->setWidth(7)
1304
	  ->setHelp('Paste the certificate received from the certificate authority here.');
1305

    
1306
	if (isset($id) && $thiscert) {
1307
		$form->addGlobal(new Form_Input(
1308
			'id',
1309
			null,
1310
			'hidden',
1311
			$id
1312
		));
1313

    
1314
		$form->addGlobal(new Form_Input(
1315
			'act',
1316
			null,
1317
			'hidden',
1318
			'csr'
1319
		));
1320
	}
1321

    
1322
	$form->add($section);
1323

    
1324
	$form->addGlobal(new Form_Button(
1325
		'save',
1326
		'Update',
1327
		null,
1328
		'fa-save'
1329
	))->addClass('btn-primary');
1330

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

    
1380
					<th class="col-sm-2"><?=gettext("Actions")?></th>
1381
				</tr>
1382
			</thead>
1383
			<tbody>
1384
<?php
1385

    
1386
$pluginparams = array();
1387
$pluginparams['type'] = 'certificates';
1388
$pluginparams['event'] = 'used_certificates';
1389
$certificates_used_by_packages = pkg_call_plugins('plugin_certificates', $pluginparams);
1390
foreach ($a_cert as $cert):
1391
	if (!is_array($cert) || empty($cert)) {
1392
		continue;
1393
	}
1394
	$name = htmlspecialchars($cert['descr']);
1395
	if ($cert['crt']) {
1396
		$subj = cert_get_subject($cert['crt']);
1397
		$issuer = cert_get_issuer($cert['crt']);
1398
		$purpose = cert_get_purpose($cert['crt']);
1399

    
1400
		if ($subj == $issuer) {
1401
			$caname = '<i>'. gettext("self-signed") .'</i>';
1402
		} else {
1403
			$caname = '<i>'. gettext("external").'</i>';
1404
		}
1405

    
1406
		$subj = htmlspecialchars(cert_escape_x509_chars($subj, true));
1407
	} else {
1408
		$subj = "";
1409
		$issuer = "";
1410
		$purpose = "";
1411
		$startdate = "";
1412
		$enddate = "";
1413
		$caname = "<em>" . gettext("private key only") . "</em>";
1414
	}
1415

    
1416
	if ($cert['csr']) {
1417
		$subj = htmlspecialchars(cert_escape_x509_chars(csr_get_subject($cert['csr']), true));
1418
		$caname = "<em>" . gettext("external - signature pending") . "</em>";
1419
	}
1420

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

    
1499
<nav class="action-buttons">
1500
	<a href="?act=new" class="btn btn-success btn-sm">
1501
		<i class="fa fa-plus icon-embed-btn"></i>
1502
		<?=gettext("Add/Sign")?>
1503
	</a>
1504
</nav>
1505
<script type="text/javascript">
1506
//<![CDATA[
1507

    
1508
events.push(function() {
1509

    
1510
	// Make these controls plain buttons
1511
	$("#btnsearch").prop('type', 'button');
1512
	$("#btnclear").prop('type', 'button');
1513

    
1514
	// Search for a term in the entry name and/or dn
1515
	$("#btnsearch").click(function() {
1516
		var searchstr = $('#searchstr').val().toLowerCase();
1517
		var table = $("table tbody");
1518
		var where = $('#where').val();
1519

    
1520
		table.find('tr').each(function (i) {
1521
			var $tds = $(this).find('td'),
1522
				shortname = $tds.eq(0).text().trim().toLowerCase(),
1523
				dn = $tds.eq(2).text().trim().toLowerCase();
1524

    
1525
			regexp = new RegExp(searchstr);
1526
			if (searchstr.length > 0) {
1527
				if (!(regexp.test(shortname) && (where != 1)) && !(regexp.test(dn) && (where != 0))) {
1528
					$(this).hide();
1529
				} else {
1530
					$(this).show();
1531
				}
1532
			} else {
1533
				$(this).show();	// A blank search string shows all
1534
			}
1535
		});
1536
	});
1537

    
1538
	// Clear the search term and unhide all rows (that were hidden during a previous search)
1539
	$("#btnclear").click(function() {
1540
		var table = $("table tbody");
1541

    
1542
		$('#searchstr').val("");
1543

    
1544
		table.find('tr').each(function (i) {
1545
			$(this).show();
1546
		});
1547
	});
1548

    
1549
	// Hitting the enter key will do the same as clicking the search button
1550
	$("#searchstr").on("keyup", function (event) {
1551
		if (event.keyCode == 13) {
1552
			$("#btnsearch").get(0).click();
1553
		}
1554
	});
1555
});
1556
//]]>
1557
</script>
1558
<?php
1559
	include("foot.inc");
1560
	exit;
1561
}
1562

    
1563

    
1564
?>
1565
<script type="text/javascript">
1566
//<![CDATA[
1567
events.push(function() {
1568

    
1569
	$('.import_type_toggle').click(function() {
1570
		var x509 = (this.value === 'x509');
1571
		hideInput('cert', !x509);
1572
		setRequired('cert', x509);
1573
		hideInput('key', !x509);
1574
		setRequired('key', x509);
1575
		hideInput('pkcs12_cert', x509);
1576
		setRequired('pkcs12_cert', !x509);
1577
		hideInput('pkcs12_pass', x509);
1578
		hideCheckbox('pkcs12_intermediate', x509);
1579
	});
1580
	if ($('input[name=import_type]:checked').val() == 'x509') {
1581
		hideInput('pkcs12_cert', true);
1582
		setRequired('pkcs12_cert', false);
1583
		hideInput('pkcs12_pass', true);
1584
		hideCheckbox('pkcs12_intermediate', true);
1585
		hideInput('cert', false);
1586
		setRequired('cert', true);
1587
		hideInput('key', false);
1588
		setRequired('key', true);
1589
	} else if ($('input[name=import_type]:checked').val() == 'pkcs12') {
1590
		hideInput('cert', true);
1591
		setRequired('cert', false);
1592
		hideInput('key', true);
1593
		setRequired('key', false);
1594
		setRequired('pkcs12_cert', false);
1595
	}
1596

    
1597
<?php if ($internal_ca_count): ?>
1598
	function internalca_change() {
1599

    
1600
		caref = $('#caref').val();
1601

    
1602
		switch (caref) {
1603
<?php
1604
			foreach ($a_ca as $ca):
1605
				if (!$ca['prv']) {
1606
					continue;
1607
				}
1608

    
1609
				$subject = @cert_get_subject_hash($ca['crt']);
1610
				if (!is_array($subject) || empty($subject)) {
1611
					continue;
1612
				}
1613
?>
1614
				case "<?=$ca['refid'];?>":
1615
					$('#dn_country').val(<?=json_encode(cert_escape_x509_chars($subject['C'], true));?>);
1616
					$('#dn_state').val(<?=json_encode(cert_escape_x509_chars($subject['ST'], true));?>);
1617
					$('#dn_city').val(<?=json_encode(cert_escape_x509_chars($subject['L'], true));?>);
1618
					$('#dn_organization').val(<?=json_encode(cert_escape_x509_chars($subject['O'], true));?>);
1619
					$('#dn_organizationalunit').val(<?=json_encode(cert_escape_x509_chars($subject['OU'], true));?>);
1620
					break;
1621
<?php
1622
			endforeach;
1623
?>
1624
		}
1625
	}
1626

    
1627
	function set_csr_ro() {
1628
		var newcsr = ($('#csrtosign').val() == "new");
1629

    
1630
		$('#csrpaste').attr('readonly', !newcsr);
1631
		$('#keypaste').attr('readonly', !newcsr);
1632
		setRequired('csrpaste', newcsr);
1633
	}
1634

    
1635
	function check_lifetime() {
1636
		var maxserverlife = <?= $cert_strict_values['max_server_cert_lifetime'] ?>;
1637
		var ltid = '#lifetime';
1638
		if ($('#method').val() == "sign") {
1639
			ltid = '#csrsign_lifetime';
1640
		}
1641
		if (($('#type').val() == "server") && (parseInt($(ltid).val()) > maxserverlife)) {
1642
			$(ltid).parent().parent().removeClass("text-normal").addClass("text-warning");
1643
			$(ltid).removeClass("text-normal").addClass("text-warning");
1644
		} else {
1645
			$(ltid).parent().parent().removeClass("text-warning").addClass("text-normal");
1646
			$(ltid).removeClass("text-warning").addClass("text-normal");
1647
		}
1648
	}
1649
	function check_keylen() {
1650
		var min_keylen = <?= $cert_strict_values['min_private_key_bits'] ?>;
1651
		var klid = '#keylen';
1652
		if ($('#method').val() == "external") {
1653
			klid = '#csr_keylen';
1654
		}
1655
		/* Color the Parent/Label */
1656
		if (parseInt($(klid).val()) < min_keylen) {
1657
			$(klid).parent().parent().removeClass("text-normal").addClass("text-warning");
1658
		} else {
1659
			$(klid).parent().parent().removeClass("text-warning").addClass("text-normal");
1660
		}
1661
		/* Color individual options */
1662
		$(klid + " option").filter(function() {
1663
			return parseInt($(this).val()) < min_keylen;
1664
		}).removeClass("text-normal").addClass("text-warning").siblings().removeClass("text-warning").addClass("text-normal");
1665
	}
1666

    
1667
	function check_digest() {
1668
		var weak_algs = <?= json_encode($cert_strict_values['digest_blacklist']) ?>;
1669
		var daid = '#digest_alg';
1670
		if ($('#method').val() == "external") {
1671
			daid = '#csr_digest_alg';
1672
		} else if ($('#method').val() == "sign") {
1673
			daid = '#csrsign_digest_alg';
1674
		}
1675
		/* Color the Parent/Label */
1676
		if (jQuery.inArray($(daid).val(), weak_algs) > -1) {
1677
			$(daid).parent().parent().removeClass("text-normal").addClass("text-warning");
1678
		} else {
1679
			$(daid).parent().parent().removeClass("text-warning").addClass("text-normal");
1680
		}
1681
		/* Color individual options */
1682
		$(daid + " option").filter(function() {
1683
			return (jQuery.inArray($(this).val(), weak_algs) > -1);
1684
		}).removeClass("text-normal").addClass("text-warning").siblings().removeClass("text-warning").addClass("text-normal");
1685
	}
1686

    
1687
	// ---------- Click checkbox handlers ---------------------------------------------------------
1688

    
1689
	$('#type').on('change', function() {
1690
		check_lifetime();
1691
	});
1692
	$('#method').on('change', function() {
1693
		check_lifetime();
1694
		check_keylen();
1695
		check_digest();
1696
	});
1697
	$('#lifetime').on('change', function() {
1698
		check_lifetime();
1699
	});
1700
	$('#csrsign_lifetime').on('change', function() {
1701
		check_lifetime();
1702
	});
1703

    
1704
	$('#keylen').on('change', function() {
1705
		check_keylen();
1706
	});
1707
	$('#csr_keylen').on('change', function() {
1708
		check_keylen();
1709
	});
1710

    
1711
	$('#digest_alg').on('change', function() {
1712
		check_digest();
1713
	});
1714
	$('#csr_digest_alg').on('change', function() {
1715
		check_digest();
1716
	});
1717

    
1718
	$('#caref').on('change', function() {
1719
		internalca_change();
1720
	});
1721

    
1722
	$('#csrtosign').change(function () {
1723
		set_csr_ro();
1724
	});
1725

    
1726
	function change_keytype() {
1727
		hideClass('rsakeys', ($('#keytype').val() != 'RSA'));
1728
		hideClass('ecnames', ($('#keytype').val() != 'ECDSA'));
1729
	}
1730

    
1731
	$('#keytype').change(function () {
1732
		change_keytype();
1733
	});
1734

    
1735
	function change_csrkeytype() {
1736
		hideClass('csr_rsakeys', ($('#csr_keytype').val() != 'RSA'));
1737
		hideClass('csr_ecnames', ($('#csr_keytype').val() != 'ECDSA'));
1738
	}
1739

    
1740
	$('#csr_keytype').change(function () {
1741
		change_csrkeytype();
1742
	});
1743

    
1744
	// ---------- On initial page load ------------------------------------------------------------
1745

    
1746
	internalca_change();
1747
	set_csr_ro();
1748
	change_keytype();
1749
	change_csrkeytype();
1750
	check_lifetime();
1751
	check_keylen();
1752
	check_digest();
1753

    
1754
	// Suppress "Delete row" button if there are fewer than two rows
1755
	checkLastRow();
1756

    
1757

    
1758
<?php endif; ?>
1759

    
1760

    
1761
});
1762
//]]>
1763
</script>
1764
<?php
1765
include('foot.inc');
(193-193/228)