Project

General

Profile

Download (18.8 KB) Statistics
| Branch: | Tag: | Revision:
1
<?php
2
/*
3
    system_camanager.php
4

    
5
    Copyright (C) 2008 Shrew Soft Inc.
6
    All rights reserved.
7

    
8
    Redistribution and use in source and binary forms, with or without
9
    modification, are permitted provided that the following conditions are met:
10

    
11
    1. Redistributions of source code must retain the above copyright notice,
12
       this list of conditions and the following disclaimer.
13

    
14
    2. Redistributions in binary form must reproduce the above copyright
15
       notice, this list of conditions and the following disclaimer in the
16
       documentation and/or other materials provided with the distribution.
17

    
18
    THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
19
    INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
20
    AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
21
    AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
22
    OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23
    SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24
    INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25
    CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26
    ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27
    POSSIBILITY OF SUCH DAMAGE.
28
*/
29
/*
30
	pfSense_MODULE:	certificate_managaer
31
*/
32

    
33
##|+PRIV
34
##|*IDENT=page-system-camanager
35
##|*NAME=System: CA Manager
36
##|*DESCR=Allow access to the 'System: CA Manager' page.
37
##|*MATCH=system_camanager.php*
38
##|-PRIV
39

    
40
require("guiconfig.inc");
41
require_once("certs.inc");
42

    
43
$ca_methods = array(
44
	"existing" => gettext("Import an existing Certificate Authority"),
45
	"internal" => gettext("Create an internal Certificate Authority"));
46

    
47
$ca_keylens = array( "512", "1024", "2048", "4096");
48

    
49
$pgtitle = array(gettext("System"), gettext("Certificate Authority Manager"));
50

    
51
$id = $_GET['id'];
52
if (isset($_POST['id']))
53
	$id = $_POST['id'];
54

    
55
if (!is_array($config['ca']))
56
	$config['ca'] = array();
57

    
58
$a_ca =& $config['ca'];
59

    
60
if (!is_array($config['cert']))
61
	$config['cert'] = array();
62

    
63
$a_cert =& $config['cert'];
64

    
65
$act = $_GET['act'];
66
if ($_POST['act'])
67
	$act = $_POST['act'];
68

    
69
if ($act == "del") {
70

    
71
	if (!$a_ca[$id]) {
72
		pfSenseHeader("system_camanager.php");
73
		exit;
74
	}
75

    
76
	$index = count($a_cert) - 1;
77
	for (;$index >=0; $index--)
78
		if ($a_cert[$index]['caref'] == $a_ca[$id]['refid'])
79
			unset($a_cert[$index]);
80

    
81
	$name = $a_ca[$id]['descr'];
82
	unset($a_ca[$id]);
83
	write_config();
84
	$savemsg = sprintf(gettext("Certificate Authority %s successfully deleted"), $name) . "<br/>";
85
}
86

    
87
if ($act == "edit") {
88
	if (!$a_ca[$id]) {
89
		pfSenseHeader("system_camanager.php");
90
		exit;
91
	}
92
	$pconfig['descr']  = $a_ca[$id]['descr'];
93
	$pconfig['refid']  = $a_ca[$id]['refid'];
94
	$pconfig['cert']   = base64_decode($a_ca[$id]['crt']);
95
	$pconfig['serial'] = $a_ca[$id]['serial'];
96
	if (!empty($a_ca[$id]['prv']))
97
		$pconfig['key'] = base64_decode($a_ca[$id]['prv']);
98
}
99

    
100
if ($act == "new") {
101
	$pconfig['method'] = $_GET['method'];
102
	$pconfig['keylen'] = "2048";
103
	$pconfig['lifetime'] = "3650";
104
	$pconfig['dn_commonname'] = "internal-ca";
105
}
106

    
107
if ($act == "exp") {
108

    
109
	if (!$a_ca[$id]) {
110
		pfSenseHeader("system_camanager.php");
111
		exit;
112
	}
113

    
114
	$exp_name = urlencode("{$a_ca[$id]['descr']}.crt");
115
	$exp_data = base64_decode($a_ca[$id]['crt']);
116
	$exp_size = strlen($exp_data);
117

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

    
125
if ($act == "expkey") {
126

    
127
	if (!$a_ca[$id]) {
128
		pfSenseHeader("system_camanager.php");
129
		exit;
130
	}
131

    
132
	$exp_name = urlencode("{$a_ca[$id]['descr']}.key");
133
	$exp_data = base64_decode($a_ca[$id]['prv']);
134
	$exp_size = strlen($exp_data);
135

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

    
143
if ($_POST) {
144

    
145
	unset($input_errors);
146
	$pconfig = $_POST;
147

    
148
	/* input validation */
149
	if ($pconfig['method'] == "existing") {
150
		$reqdfields = explode(" ", "descr cert");
151
		$reqdfieldsn = array(
152
				gettext("Descriptive name"),
153
				gettext("Certificate data"));
154
		if ($_POST['cert'] && (!strstr($_POST['cert'], "BEGIN CERTIFICATE") || !strstr($_POST['cert'], "END CERTIFICATE")))
155
			$input_errors[] = gettext("This certificate does not appear to be valid.");
156
	}
157
	if ($pconfig['method'] == "internal") {
158
		$reqdfields = explode(" ",
159
				"descr keylen lifetime dn_country dn_state dn_city ".
160
				"dn_organization dn_email dn_commonname");
161
		$reqdfieldsn = array(
162
				gettext("Descriptive name"),
163
				gettext("Key length"),
164
				gettext("Lifetime"),
165
				gettext("Distinguished name Country Code"),
166
				gettext("Distinguished name State or Province"),
167
				gettext("Distinguished name City"),
168
				gettext("Distinguished name Organization"),
169
				gettext("Distinguished name Email Address"),
170
				gettext("Distinguished name Common Name"));
171
	}
172

    
173
	do_input_validation($_POST, $reqdfields, $reqdfieldsn, &$input_errors);
174

    
175
	/* if this is an AJAX caller then handle via JSON */
176
	if (isAjax() && is_array($input_errors)) {
177
		input_errors2Ajax($input_errors);
178
		exit;
179
	}
180

    
181
	/* save modifications */
182
	if (!$input_errors) {
183

    
184
		$ca = array();
185
		if (!isset($pconfig['refid']) || empty($pconfig['refid']))
186
			$ca['refid'] = uniqid();
187
		else
188
			$ca['refid'] = $pconfig['refid'];
189

    
190
		if (isset($id) && $a_ca[$id])
191
			$ca = $a_ca[$id];
192

    
193
		$ca['descr'] = $pconfig['descr'];
194

    
195
		if ($_POST['edit'] == "edit") {
196
			$ca['descr']  = $pconfig['descr'];
197
			$ca['refid']  = $pconfig['refid'];
198
			$ca['serial'] = $pconfig['serial'];
199
			$ca['crt']    = base64_encode($pconfig['cert']);
200
			if (!empty($pconfig['key']))
201
				$ca['prv']    = base64_encode($pconfig['key']);
202
		} else {
203
			if ($pconfig['method'] == "existing")
204
				ca_import($ca, $pconfig['cert'], $pconfig['key'], $pconfig['serial']);
205

    
206
			if ($pconfig['method'] == "internal") {
207
				$dn = array(
208
					'countryName' => $pconfig['dn_country'],
209
					'stateOrProvinceName' => $pconfig['dn_state'],
210
					'localityName' => $pconfig['dn_city'],
211
					'organizationName' => $pconfig['dn_organization'],
212
					'emailAddress' => $pconfig['dn_email'],
213
					'commonName' => $pconfig['dn_commonname']);
214
				ca_create($ca, $pconfig['keylen'], $pconfig['lifetime'], $dn);
215
			}
216
		}
217

    
218
		if (isset($id) && $a_ca[$id])
219
			$a_ca[$id] = $ca;
220
		else
221
			$a_ca[] = $ca;
222

    
223
		write_config();
224

    
225
//		pfSenseHeader("system_camanager.php");
226
	}
227
}
228

    
229
include("head.inc");
230
?>
231

    
232
<body link="#000000" vlink="#000000" alink="#000000" onload="<?= $jsevents["body"]["onload"] ?>">
233
<?php include("fbegin.inc"); ?>
234
<script type="text/javascript">
235
<!--
236

    
237
function method_change() {
238

    
239
	method = document.iform.method.selectedIndex;
240

    
241
	switch (method) {
242
		case 0:
243
			document.getElementById("existing").style.display="";
244
			document.getElementById("internal").style.display="none";
245
			break;
246
		case 1:
247
			document.getElementById("existing").style.display="none";
248
			document.getElementById("internal").style.display="";
249
			break;
250
	}
251
}
252

    
253
//-->
254
</script>
255
<?php
256
	if ($input_errors)
257
		print_input_errors($input_errors);
258
	if ($savemsg)
259
		print_info_box($savemsg);
260
?>
261
<table width="100%" border="0" cellpadding="0" cellspacing="0">
262
	<tr>
263
		<td>
264
		<?php
265
			$tab_array = array();
266
			$tab_array[] = array(gettext("CAs"), true, "system_camanager.php");
267
			$tab_array[] = array(gettext("Certificates"), false, "system_certmanager.php");
268
			$tab_array[] = array(gettext("Certificate Revocation"), false, "system_crlmanager.php");
269
			display_top_tabs($tab_array);
270
		?>
271
		</td>
272
	</tr>
273
	<tr>
274
		<td id="mainarea">
275
			<div class="tabcont">
276

    
277
				<?php if ($act == "new" || $act == "edit" || $act == gettext("Save") || $input_errors): ?>
278

    
279
				<form action="system_camanager.php" method="post" name="iform" id="iform">
280
					<?php if ($act == "edit"): ?>
281
					<input type="hidden" name="edit" value="edit" id="edit">
282
					<input type="hidden" name="id" value="<?php echo $id; ?>" id="id">
283
					<input type="hidden" name="refid" value="<?php echo $pconfig['refid']; ?>" id="refid">
284
					<?php endif; ?>
285
					<table width="100%" border="0" cellpadding="6" cellspacing="0">
286
						<tr>
287
							<td width="22%" valign="top" class="vncellreq"><?=gettext("Descriptive name");?></td>
288
							<td width="78%" class="vtable">
289
								<input name="descr" type="text" class="formfld unknown" id="descr" size="20" value="<?=htmlspecialchars($pconfig['descr']);?>"/>
290
							</td>
291
						</tr>
292
						<?php if (!isset($id) || $act == "edit"): ?>
293
						<tr>
294
							<td width="22%" valign="top" class="vncellreq"><?=gettext("Method");?></td>
295
							<td width="78%" class="vtable">
296
								<select name='method' id='method' class="formselect" onchange='method_change()'>
297
								<?php
298
									foreach($ca_methods as $method => $desc):
299
									$selected = "";
300
									if ($pconfig['method'] == $method)
301
										$selected = "selected";
302
								?>
303
									<option value="<?=$method;?>"<?=$selected;?>><?=$desc;?></option>
304
								<?php endforeach; ?>
305
								</select>
306
							</td>
307
						</tr>
308
						<?php endif; ?>
309
					</table>
310

    
311
					<table width="100%" border="0" cellpadding="6" cellspacing="0" id="existing">
312
						<tr>
313
							<td colspan="2" class="list" height="12"></td>
314
						</tr>
315
						<tr>
316
							<td colspan="2" valign="top" class="listtopic"><?=gettext("Existing Certificate Authority");?></td>
317
						</tr>
318

    
319
						<tr>
320
							<td width="22%" valign="top" class="vncellreq"><?=gettext("Certificate data");?></td>
321
							<td width="78%" class="vtable">
322
								<textarea name="cert" id="cert" cols="65" rows="7" class="formfld_cert"><?=htmlspecialchars($pconfig['cert']);?></textarea>
323
								<br>
324
								<?=gettext("Paste a certificate in X.509 PEM format here.");?></td>
325
							</td>
326
						</tr>
327
						<tr>
328
							<td width="22%" valign="top" class="vncellreq"><?=gettext("Certificate Private Key");?><br/><?=gettext("(optional)");?></td>
329
							<td width="78%" class="vtable">
330
								<textarea name="key" id="key" cols="65" rows="7" class="formfld_cert"><?=htmlspecialchars($pconfig['key']);?></textarea>
331
								<br>
332
								<?=gettext("Paste the private key for the above certificate here. This is optional in most cases, but required if you need to generate a Certificate Revocation List (CRL).");?></td>
333
							</td>
334
						</tr>
335

    
336
					<?php if (!isset($id) || $act == "edit"): ?>
337
						<tr>
338
							<td width="22%" valign="top" class="vncellreq"><?=gettext("Serial");?></td>
339
							<td width="78%" class="vtable">
340
								<input name="serial" type="text" class="formfld unknown" id="serial" size="20" value="<?=htmlspecialchars($pconfig['serial']);?>"/>
341
							</td>
342
						</tr>
343
					<?php endif; ?>
344
					</table>
345

    
346
					<table width="100%" border="0" cellpadding="6" cellspacing="0" id="internal">
347
						<tr>
348
							<td colspan="2" class="list" height="12"></td>
349
						</tr>
350
						<tr>
351
							<td colspan="2" valign="top" class="listtopic"><?=gettext("Internal Certificate Authority");?></td>
352
						</tr>
353
						<tr>
354
							<td width="22%" valign="top" class="vncellreq"><?=gettext("Key length");?></td>
355
							<td width="78%" class="vtable">
356
								<select name='keylen' id='keylen' class="formselect">
357
								<?php
358
									foreach( $ca_keylens as $len):
359
									$selected = "";
360
									if ($pconfig['keylen'] == $len)
361
										$selected = "selected";
362
								?>
363
									<option value="<?=$len;?>"<?=$selected;?>><?=$len;?></option>
364
								<?php endforeach; ?>
365
								</select>
366
								<?=gettext("bits");?>
367
							</td>
368
						</tr>
369
						<tr>
370
							<td width="22%" valign="top" class="vncellreq"><?=gettext("Lifetime");?></td>
371
							<td width="78%" class="vtable">
372
								<input name="lifetime" type="text" class="formfld unknown" id="lifetime" size="5" value="<?=htmlspecialchars($pconfig['lifetime']);?>"/>
373
								<?=gettext("days");?>
374
							</td>
375
						</tr>
376
						<tr>
377
							<td width="22%" valign="top" class="vncellreq"><?=gettext("Distinguished name");?></td>
378
							<td width="78%" class="vtable">
379
								<table border="0" cellspacing="0" cellpadding="2">
380
									<tr>
381
										<td align="right"><?=gettext("Country Code");?> : &nbsp;</td>
382
										<td align="left">
383
											<input name="dn_country" type="text" class="formfld unknown" maxlength="2" size="2" value="<?=htmlspecialchars($pconfig['dn_country']);?>"/>
384
											&nbsp;
385
											<em><?=gettext("ex:");?></em>
386
											&nbsp;
387
											<?=gettext("US");?>
388
											<em><?=gettext("( two letters )");?></em>
389
										</td>
390
									</tr>
391
									<tr>
392
										<td align="right"><?=gettext("State or Province");?> : &nbsp;</td>
393
										<td align="left">
394
											<input name="dn_state" type="text" class="formfld unknown" size="40" value="<?=htmlspecialchars($pconfig['dn_state']);?>"/>
395
											&nbsp;
396
											<em><?=gettext("ex:");?></em>
397
											&nbsp;
398
											<?=gettext("Texas");?>
399
										</td>
400
									</tr>
401
									<tr>
402
										<td align="right"><?=gettext("City");?> : &nbsp;</td>
403
										<td align="left">
404
											<input name="dn_city" type="text" class="formfld unknown" size="40" value="<?=htmlspecialchars($pconfig['dn_city']);?>"/>
405
											&nbsp;
406
											<em><?=gettext("ex:");?></em>
407
											&nbsp;
408
											<?=gettext("Austin");?>
409
										</td>
410
									</tr>
411
									<tr>
412
										<td align="right"><?=gettext("Organization");?> : &nbsp;</td>
413
										<td align="left">
414
											<input name="dn_organization" type="text" class="formfld unknown" size="40" value="<?=htmlspecialchars($pconfig['dn_organization']);?>"/>
415
											&nbsp;
416
											<em><?=gettext("ex:");?></em>
417
											&nbsp;
418
											<?=gettext("My Company Inc.");?>
419
										</td>
420
									</tr>
421
									<tr>
422
										<td align="right"><?=gettext("Email Address");?> : &nbsp;</td>
423
										<td align="left">
424
											<input name="dn_email" type="text" class="formfld unknown" size="25" value="<?=htmlspecialchars($pconfig['dn_email']);?>"/>
425
											&nbsp;
426
											<em><?=gettext("ex:");?></em>
427
											&nbsp;
428
											<?=gettext("admin@mycompany.com");?>
429
										</td>
430
									</tr>
431
									<tr>
432
										<td align="right"><?=gettext("Common Name");?> : &nbsp;</td>
433
										<td align="left">
434
											<input name="dn_commonname" type="text" class="formfld unknown" size="25" value="<?=htmlspecialchars($pconfig['dn_commonname']);?>"/>
435
											&nbsp;
436
											<em><?=gettext("ex:");?></em>
437
											&nbsp;
438
											<?=gettext("internal-ca");?>
439
										</td>
440
									</tr>
441
								</table>
442
							</td>
443
						</tr>
444
					</table>
445

    
446
					<table width="100%" border="0" cellpadding="6" cellspacing="0">
447
						<tr>
448
							<td width="22%" valign="top">&nbsp;</td>
449
							<td width="78%">
450
								<input id="submit" name="save" type="submit" class="formbtn" value="<?=gettext("Save"); ?>" />
451
								<?php if (isset($id) && $a_ca[$id]): ?>
452
								<input name="id" type="hidden" value="<?=$id;?>" />
453
								<?php endif;?>
454
							</td>
455
						</tr>
456
					</table>
457
				</form>
458

    
459
				<?php else: ?>
460

    
461
				<table width="100%" border="0" cellpadding="0" cellspacing="0">
462
					<tr>
463
						<td width="20%" class="listhdrr"><?=gettext("Name");?></td>
464
						<td width="10%" class="listhdrr"><?=gettext("Internal");?></td>
465
						<td width="10%" class="listhdrr"><?=gettext("Issuer");?></td>
466
						<td width="10%" class="listhdrr"><?=gettext("Certificates");?></td>
467
						<td width="40%" class="listhdrr"><?=gettext("Distinguished Name");?></td>
468
						<td width="10%" class="list"></td>
469
					</tr>
470
					<?php
471
						$i = 0;
472
						foreach($a_ca as $ca):
473
							$name = htmlspecialchars($ca['descr']);
474
							$subj = cert_get_subject($ca['crt']);
475
							$issuer = cert_get_issuer($ca['crt']);
476
							if($subj == $issuer)
477
							  $issuer_name = "<em>" . gettext("self-signed") . "</em>";
478
							else
479
							  $issuer_name = "<em>" . gettext("external") . "</em>";
480
							$subj = htmlspecialchars($subj);
481
							$issuer = htmlspecialchars($issuer);
482
							$certcount = 0;
483

    
484
							$issuer_ca = lookup_ca($ca['caref']);
485
							if ($issuer_ca)
486
								$issuer_name = $issuer_ca['descr'];
487

    
488
							// TODO : Need gray certificate icon
489

    
490
							if($ca['prv']) {
491
								$caimg = "/themes/{$g['theme']}/images/icons/icon_frmfld_cert.png";
492
								$internal = "YES";
493

    
494
							} else {
495
								$caimg = "/themes/{$g['theme']}/images/icons/icon_frmfld_cert.png";
496
								$internal = "NO";
497
							}
498
							foreach ($a_cert as $cert)
499
								if ($cert['caref'] == $ca['refid'])
500
									$certcount++;
501
  						foreach ($a_ca as $cert)
502
  							if ($cert['caref'] == $ca['refid'])
503
  								$certcount++;
504
					?>
505
					<tr>
506
						<td class="listlr">
507
							<table border="0" cellpadding="0" cellspacing="0">
508
								<tr>
509
									<td align="left" valign="center">
510
										<img src="<?=$caimg;?>" alt="CA" title="CA" border="0" height="16" width="16" />
511
									</td>
512
									<td align="left" valign="middle">
513
										<?=$name;?>
514
									</td>
515
								</tr>
516
							</table>
517
						</td>
518
						<td class="listr"><?=$internal;?>&nbsp;</td>
519
						<td class="listr"><?=$issuer_name;?>&nbsp;</td>
520
						<td class="listr"><?=$certcount;?>&nbsp;</td>
521
						<td class="listr"><?=$subj;?>&nbsp;</td>
522
						<td valign="middle" nowrap class="list">
523
							<a href="system_camanager.php?act=edit&id=<?=$i;?>")">
524
								<img src="/themes/<?= $g['theme'];?>/images/icons/icon_e.gif" title="<?=gettext("export ca");?>" alt="<?=gettext("edit ca");?>" width="17" height="17" border="0" />
525
							</a>
526
							<a href="system_camanager.php?act=exp&id=<?=$i;?>")">
527
								<img src="/themes/<?= $g['theme'];?>/images/icons/icon_down.gif" title="<?=gettext("export ca");?>" alt="<?=gettext("export ca");?>" width="17" height="17" border="0" />
528
							</a>
529
							<?php if ($ca['prv']): ?>
530
							<a href="system_camanager.php?act=expkey&id=<?=$i;?>")">
531
								<img src="/themes/<?= $g['theme'];?>/images/icons/icon_down.gif" title="<?=gettext("export ca private key");?>" alt="<?=gettext("export ca private key");?>" width="17" height="17" border="0" />
532
							</a>
533
							<?php endif; ?>
534
							<a href="system_camanager.php?act=del&id=<?=$i;?>" onclick="return confirm('<?=gettext("Do you really want to delete this Certificate Authority and all associated certificates?");?>')">
535
								<img src="/themes/<?= $g['theme'];?>/images/icons/icon_x.gif" title="<?=gettext("delete ca");?>" alt="<?=gettext("delete ca"); ?>" width="17" height="17" border="0" />
536
							</a>
537
						</td>
538
					</tr>
539
					<?php
540
							$i++;
541
						endforeach;
542
					?>
543
					<tr>
544
						<td class="list" colspan="5"></td>
545
						<td class="list">
546
							<a href="system_camanager.php?act=new">
547
								<img src="/themes/<?= $g['theme'];?>/images/icons/icon_plus.gif" title="<?=gettext("add or import ca");?>" alt="<?=gettext("add ca");?>" width="17" height="17" border="0" />
548
							</a>
549
						</td>
550
					</tr>
551
					<tr>
552
						<td colspan="5">
553
							<p>
554
								<?=gettext("Additional trusted Certificate Authorities can be added here.");?>
555
							</p>
556
						</td>
557
					</tr>
558
				</table>
559

    
560
				<?php endif; ?>
561

    
562
			</div>
563
		</td>
564
	</tr>
565
</table>
566
<?php include("fend.inc");?>
567
<script type="text/javascript">
568
<!--
569

    
570
method_change();
571

    
572
//-->
573
</script>
574

    
575
</body>
(184-184/225)