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-2024 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
	config_init_path('system/user');
66
}
67

    
68
config_init_path('ca');
69
config_init_path('cert');
70

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

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

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

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

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

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

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

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

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

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

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

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

    
307
	$altnames = array();
308
	do_input_validation($_POST, $reqdfields, $reqdfieldsn, $input_errors);
309

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

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

    
335
		$pconfig['altnames']['item'] = $altnames;
336

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

    
621
		if (isset($id) && $thiscert) {
622
			$thiscert = $cert;
623
			config_set_path("cert/{$cert_item_config['idx']}", $thiscert);
624
		} elseif ($cert) {
625
			config_set_path('cert/', $cert);
626
		}
627

    
628
		if (isset($userid) && (config_get_path('system/user') !== null)) {
629
			config_set_path("system/user/{$userid}/cert/", $cert['refid']);
630
		}
631

    
632
		if (!$input_errors) {
633
			write_config($savemsg);
634
		}
635

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

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

    
652
	do_input_validation($_POST, $reqdfields, $reqdfieldsn, $input_errors);
653

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

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

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

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

    
680
$pgtitle = array(gettext('System'), gettext('Certificates'), gettext('Certificates'));
681
$pglinks = array("", "system_camanager.php", "system_certmanager.php");
682

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

    
690
if ($input_errors) {
691
	print_input_errors($input_errors);
692
}
693

    
694
if ($savemsg) {
695
	print_info_box($savemsg, $class);
696
}
697

    
698
$tab_array = array();
699
$tab_array[] = array(gettext('Authorities'), false, 'system_camanager.php');
700
$tab_array[] = array(gettext('Certificates'), true, 'system_certmanager.php');
701
$tab_array[] = array(gettext('Revocation'), false, 'system_crlmanager.php');
702
display_top_tabs($tab_array);
703

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

    
708
	if (isset($userid) && config_get_path("system/user")) {
709
		$form->addGlobal(new Form_Input(
710
			'userid',
711
			null,
712
			'hidden',
713
			$userid
714
		));
715
	}
716

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

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

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

    
745
	$section = new Form_Section($maintitle);
746

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

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

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

    
773
	$form->add($section);
774

    
775
	// Return an array containing the IDs od all CAs
776
	function list_cas() {
777
		$allCas = array();
778

    
779
		foreach (config_get_path('ca', []) as $ca) {
780
			if ($ca['prv']) {
781
				$allCas[$ca['refid']] = $ca['descr'];
782
			}
783
		}
784

    
785
		return $allCas;
786
	}
787

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
885
	$section->addInput(new Form_Textarea(
886
		'key',
887
		'Private key data',
888
		$pconfig['key']
889
	))->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.');
890

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
1156
	$existCerts = array();
1157

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

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

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

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

    
1181
		$existCerts[ $cert['refid'] ] = $cert['descr'];
1182
	}
1183

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

    
1191
	$form->add($section);
1192

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

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

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

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

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

    
1230
	foreach ($pconfig['altnames']['item'] as $item) {
1231

    
1232
		$group = new Form_Group($counter == 0 ? 'Alternative Names':'');
1233

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

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

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

    
1255
		$group->addClass('repeatable');
1256

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

    
1262
		$section->add($group);
1263

    
1264
		$counter++;
1265
	}
1266

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

    
1274
	$form->add($section);
1275

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

    
1291
	print $form;
1292

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

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

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

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

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

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

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

    
1339
	$form->add($section);
1340

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

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

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

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

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

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

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

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

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

    
1529
events.push(function() {
1530

    
1531
	// Make these controls plain buttons
1532
	$("#btnsearch").prop('type', 'button');
1533
	$("#btnclear").prop('type', 'button');
1534

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

    
1541
		table.find('tr').each(function (i) {
1542
			var $tds = $(this).find('td'),
1543
				shortname = $tds.eq(0).text().trim().toLowerCase(),
1544
				dn = $tds.eq(2).text().trim().toLowerCase();
1545

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

    
1559
	// Clear the search term and unhide all rows (that were hidden during a previous search)
1560
	$("#btnclear").click(function() {
1561
		var table = $("table tbody");
1562

    
1563
		$('#searchstr').val("");
1564

    
1565
		table.find('tr').each(function (i) {
1566
			$(this).show();
1567
		});
1568
	});
1569

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

    
1584

    
1585
?>
1586
<script type="text/javascript">
1587
//<![CDATA[
1588
events.push(function() {
1589

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

    
1618
<?php if ($internal_ca_count): ?>
1619
	function internalca_change() {
1620

    
1621
		caref = $('#caref').val();
1622

    
1623
		switch (caref) {
1624
<?php
1625
			foreach (config_get_path('ca', []) as $ca):
1626
				if (!$ca['prv']) {
1627
					continue;
1628
				}
1629

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

    
1648
	function set_csr_ro() {
1649
		var newcsr = ($('#csrtosign').val() == "new");
1650

    
1651
		$('#csrpaste').attr('readonly', !newcsr);
1652
		$('#keypaste').attr('readonly', !newcsr);
1653
		setRequired('csrpaste', newcsr);
1654
	}
1655

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

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

    
1708
	// ---------- Click checkbox handlers ---------------------------------------------------------
1709

    
1710
	$('#type').on('change', function() {
1711
		check_lifetime();
1712
	});
1713
	$('#method').on('change', function() {
1714
		check_lifetime();
1715
		check_keylen();
1716
		check_digest();
1717
	});
1718
	$('#lifetime').on('change', function() {
1719
		check_lifetime();
1720
	});
1721
	$('#csrsign_lifetime').on('change', function() {
1722
		check_lifetime();
1723
	});
1724

    
1725
	$('#keylen').on('change', function() {
1726
		check_keylen();
1727
	});
1728
	$('#csr_keylen').on('change', function() {
1729
		check_keylen();
1730
	});
1731

    
1732
	$('#digest_alg').on('change', function() {
1733
		check_digest();
1734
	});
1735
	$('#csr_digest_alg').on('change', function() {
1736
		check_digest();
1737
	});
1738

    
1739
	$('#caref').on('change', function() {
1740
		internalca_change();
1741
	});
1742

    
1743
	$('#csrtosign').change(function () {
1744
		set_csr_ro();
1745
	});
1746

    
1747
	function change_keytype() {
1748
		hideClass('rsakeys', ($('#keytype').val() != 'RSA'));
1749
		hideClass('ecnames', ($('#keytype').val() != 'ECDSA'));
1750
	}
1751

    
1752
	$('#keytype').change(function () {
1753
		change_keytype();
1754
	});
1755

    
1756
	function change_csrkeytype() {
1757
		hideClass('csr_rsakeys', ($('#csr_keytype').val() != 'RSA'));
1758
		hideClass('csr_ecnames', ($('#csr_keytype').val() != 'ECDSA'));
1759
	}
1760

    
1761
	$('#csr_keytype').change(function () {
1762
		change_csrkeytype();
1763
	});
1764

    
1765
	// ---------- On initial page load ------------------------------------------------------------
1766

    
1767
	internalca_change();
1768
	set_csr_ro();
1769
	change_keytype();
1770
	change_csrkeytype();
1771
	check_lifetime();
1772
	check_keylen();
1773
	check_digest();
1774

    
1775
	// Suppress "Delete row" button if there are fewer than two rows
1776
	checkLastRow();
1777

    
1778

    
1779
<?php endif; ?>
1780

    
1781

    
1782
});
1783
//]]>
1784
</script>
1785
<?php
1786
include('foot.inc');
(197-197/232)