Project

General

Profile

Download (55.7 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-2025 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
}
66

    
67
$internal_ca_count = 0;
68
foreach (config_get_path('cert', []) as $ca) {
69
	if ($ca['prv']) {
70
		$internal_ca_count++;
71
	}
72
}
73

    
74
if ($_REQUEST['exportp12']) {
75
	$act = 'p12';
76
} elseif ($_REQUEST['exportpkey']) {
77
	$act = 'key';
78
} else {
79
	$act = $_REQUEST['act'];
80
}
81

    
82
if ($act == 'edit') {
83
	$cert_methods = array(
84
		'edit' => gettext("Edit an existing certificate")
85
	);
86
}
87

    
88
if (isset($_REQUEST['id']) && ctype_alnum($_REQUEST['id'])) {
89
	$id = $_REQUEST['id'];
90
}
91
if (!empty($id)) {
92
	$cert_item_config = lookup_cert($id);
93
	$thiscert = &$cert_item_config['item'];
94
}
95

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

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

    
205
if ($_POST['save'] == gettext("Save")) {
206
	/* Creating a new entry */
207
	$input_errors = array();
208
	$pconfig = $_POST;
209

    
210
	switch ($pconfig['method']) {
211
		case 'sign':
212
			$reqdfields = explode(" ",
213
				"descr catosignwith");
214
			$reqdfieldsn = array(
215
				gettext("Descriptive name"),
216
				gettext("CA to sign with"));
217

    
218
			if (($_POST['csrtosign'] === "new") &&
219
			    ((!strstr($_POST['csrpaste'], "BEGIN CERTIFICATE REQUEST") || !strstr($_POST['csrpaste'], "END CERTIFICATE REQUEST")) &&
220
			    (!strstr($_POST['csrpaste'], "BEGIN NEW CERTIFICATE REQUEST") || !strstr($_POST['csrpaste'], "END NEW CERTIFICATE REQUEST")))) {
221
				$input_errors[] = gettext("This signing request does not appear to be valid.");
222
			}
223

    
224
			if ( (($_POST['csrtosign'] === "new") && (strlen($_POST['keypaste']) > 0)) &&
225
			    ((!strstr($_POST['keypaste'], "BEGIN PRIVATE KEY") && !strstr($_POST['keypaste'], "BEGIN EC PRIVATE KEY")) ||
226
			    (strstr($_POST['keypaste'], "BEGIN PRIVATE KEY") && !strstr($_POST['keypaste'], "END PRIVATE KEY")) ||
227
			    (strstr($_POST['keypaste'], "BEGIN EC PRIVATE KEY") && !strstr($_POST['keypaste'], "END EC PRIVATE KEY")))) {
228
				$input_errors[] = gettext("This private does not appear to be valid.");
229
				$input_errors[] = gettext("Key data field should be blank, or a valid x509 private key");
230
			}
231

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

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

    
303
	$altnames = array();
304
	do_input_validation($_POST, $reqdfields, $reqdfieldsn, $input_errors);
305

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

    
325
			if (ctype_digit(strval($entry))) {
326
				$entry++;	// Pre-bootstrap code is one-indexed, but the bootstrap code is 0-indexed
327
				$altnames[$entry][$field] = $value;
328
			}
329
		}
330

    
331
		$pconfig['altnames']['item'] = $altnames;
332

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

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

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

    
432
	/* save modifications */
433
	if (!$input_errors) {
434
		$old_err_level = error_reporting(0); /* otherwise openssl_ functions throw warnings directly to a page breaking menu tabs */
435

    
436
		if (isset($id) && $thiscert) {
437
			$cert = $thiscert;
438
		} else {
439
			$cert = array();
440
			$cert['refid'] = uniqid();
441
		}
442

    
443
		$cert['descr'] = $pconfig['descr'];
444

    
445
		switch($pconfig['method']) {
446
			case 'existing':
447
				/* Add an existing certificate to a user */
448
				$ucert = lookup_cert($pconfig['certref']);
449
				$ucert = $ucert['item'];
450
				if ($ucert && config_get_path("system/user")) {
451
					config_set_path("system/user/{$userid}/cert/", $ucert['refid']);
452
					$savemsg = sprintf(gettext("Added certificate %s to user %s"), htmlspecialchars($ucert['descr']), config_get_path("system/user/{$userid}/name"));
453
				}
454
				unset($cert);
455
				break;
456
			case 'sign':
457
				/* Sign a CSR */
458
				$csrid = lookup_cert($pconfig['csrtosign']);
459
				$csrid = $csrid['item'];
460
				$ca_item_config = lookup_ca($pconfig['catosignwith']);
461
				$ca = &$ca_item_config['item'];
462
				// Read the CSR from config array, or if a new one, from the textarea
463
				if ($pconfig['csrtosign'] === "new") {
464
					$csr = $pconfig['csrpaste'];
465
				} else {
466
					$csr = base64_decode($csrid['csr']);
467
				}
468
				if (count($altnames)) {
469
					foreach ($altnames as $altname) {
470
						$altnames_tmp[] = "{$altname['type']}:" . $altname['value'];
471
					}
472
					$altname_str = implode(",", $altnames_tmp);
473
				}
474
				$n509 = csr_sign($csr, $ca, $pconfig['csrsign_lifetime'], $pconfig['type'], $altname_str, $pconfig['csrsign_digest_alg']);
475
				config_set_path("ca/{$ca_item_config['idx']}", $ca);
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_set_path('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
								config_set_path('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
			config_set_path("cert/{$cert_item_config['idx']}", $thiscert);
620
		} elseif ($cert) {
621
			config_set_path('cert/', $cert);
622
		}
623

    
624
		if (isset($userid) && (config_get_path('system/user') !== null)) {
625
			config_set_path("system/user/{$userid}/cert/", $cert['refid']);
626
		}
627

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
704
	if (isset($userid) && config_get_path("system/user")) {
705
		$form->addGlobal(new Form_Input(
706
			'userid',
707
			null,
708
			'hidden',
709
			$userid
710
		));
711
	}
712

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

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

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

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

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

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

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

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

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

    
775
		foreach (config_get_path('ca', []) as $ca) {
776
			if ($ca['prv']) {
777
				$allCas[$ca['refid']] = $ca['descr'];
778
			}
779
		}
780

    
781
		return $allCas;
782
	}
783

    
784
	// Return an array containing the IDs od all CSRs
785
	function list_csrs() {
786
		$allCsrs = array();
787

    
788
		foreach (config_get_path('cert', []) as $cert) {
789
			if ($cert['csr']) {
790
				$allCsrs[$cert['refid']] = $cert['descr'];
791
			}
792
		}
793

    
794
		return ['new' => gettext('New CSR (Paste below)')] + $allCsrs;
795
	}
796

    
797
	$section = new Form_Section('Sign CSR');
798
	$section->addClass('toggle-sign collapse');
799

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

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

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

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

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

    
844
	$form->add($section);
845

    
846
	if ($act == 'edit') {
847
		$editimport = gettext("Edit Certificate");
848
	} else {
849
		$editimport = gettext("Import Certificate");
850
	}
851

    
852
	$section = new Form_Section($editimport);
853
	$section->addClass('toggle-import toggle-edit collapse');
854

    
855
	$group = new Form_Group('Certificate Type');
856

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

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

    
873
	$section->add($group);
874

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

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

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

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

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

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

    
925
	$form->add($section);
926
	$section = new Form_Section('Internal Certificate');
927
	$section->addClass('toggle-internal collapse');
928

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

    
943
			$allCas[ $ca['refid'] ] = $ca['descr'];
944
		}
945

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

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

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

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

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

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

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

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

    
1015
	$section->addInput(new Form_Select(
1016
		'dn_country',
1017
		'Country Code',
1018
		$pconfig['dn_country'],
1019
		get_cert_country_codes()
1020
	));
1021

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

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

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

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

    
1054
	$form->add($section);
1055
	$section = new Form_Section('External Signing Request');
1056
	$section->addClass('toggle-external collapse');
1057

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

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

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

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

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

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

    
1109
	$section->addInput(new Form_Select(
1110
		'csr_dn_country',
1111
		'Country Code',
1112
		$pconfig['csr_dn_country'],
1113
		get_cert_country_codes()
1114
	));
1115

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

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

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

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

    
1148
	$form->add($section);
1149
	$section = new Form_Section('Choose an Existing Certificate');
1150
	$section->addClass('toggle-existing collapse');
1151

    
1152
	$existCerts = array();
1153

    
1154
	foreach (config_get_path('cert', []) as $cert) {
1155
		if (!is_array($cert) || empty($cert)) {
1156
			continue;
1157
		}
1158

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

    
1164
		$ca = lookup_ca($cert['caref']);
1165
		$ca = $ca['item'];
1166
		if ($ca) {
1167
			$cert['descr'] .= " (CA: {$ca['descr']})";
1168
		}
1169

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

    
1177
		$existCerts[ $cert['refid'] ] = $cert['descr'];
1178
	}
1179

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

    
1187
	$form->add($section);
1188

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

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

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

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

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

    
1226
	foreach ($pconfig['altnames']['item'] as $item) {
1227

    
1228
		$group = new Form_Group($counter == 0 ? 'Alternative Names':'');
1229

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

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

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

    
1251
		$group->addClass('repeatable');
1252

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

    
1258
		$section->add($group);
1259

    
1260
		$counter++;
1261
	}
1262

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

    
1270
	$form->add($section);
1271

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

    
1287
	print $form;
1288

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

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

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

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

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

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

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

    
1335
	$form->add($section);
1336

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

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

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

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

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

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

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

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

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

    
1525
events.push(function() {
1526

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

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

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

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

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

    
1559
		$('#searchstr').val("");
1560

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

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

    
1580

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

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

    
1614
<?php if ($internal_ca_count): ?>
1615
	function internalca_change() {
1616

    
1617
		caref = $('#caref').val();
1618

    
1619
		switch (caref) {
1620
<?php
1621
			foreach (config_get_path('ca', []) as $ca):
1622
				if (!$ca['prv']) {
1623
					continue;
1624
				}
1625

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

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

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

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

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

    
1704
	// ---------- Click checkbox handlers ---------------------------------------------------------
1705

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

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

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

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

    
1739
	$('#csrtosign').change(function () {
1740
		set_csr_ro();
1741
	});
1742

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

    
1748
	$('#keytype').change(function () {
1749
		change_keytype();
1750
	});
1751

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

    
1757
	$('#csr_keytype').change(function () {
1758
		change_csrkeytype();
1759
	});
1760

    
1761
	// ---------- On initial page load ------------------------------------------------------------
1762

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

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

    
1774

    
1775
<?php endif; ?>
1776

    
1777

    
1778
});
1779
//]]>
1780
</script>
1781
<?php
1782
include('foot.inc');
(198-198/233)