Project

General

Profile

Download (16.7 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['system']['ca']))
56
	$config['system']['ca'] = array();
57

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

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

    
63
$a_cert =& $config['system']['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]['name'];
82
	unset($a_ca[$id]);
83
	write_config();
84
	$savemsg = gettext("Certificate Authority")." {$name} ".
85
				gettext("successfully deleted")."<br/>";
86
}
87

    
88
if ($act == "new") {
89
	$pconfig['method'] = $_GET['method'];
90
	$pconfig['keylen'] = "2048";
91
	$pconfig['lifetime'] = "3650";
92
	$pconfig['dn_commonname'] = "internal-ca";
93
}
94

    
95
if ($act == "exp") {
96

    
97
	if (!$a_ca[$id]) {
98
		pfSenseHeader("system_camanager.php");
99
		exit;
100
	}
101

    
102
	$exp_name = urlencode("{$a_ca[$id]['name']}.crt");
103
	$exp_data = base64_decode($a_ca[$id]['crt']);
104
	$exp_size = strlen($exp_data);
105

    
106
	header("Content-Type: application/octet-stream");
107
	header("Content-Disposition: attachment; filename={$exp_name}");
108
	header("Content-Length: $exp_size");
109
	echo $exp_data;
110
	exit;
111
}
112

    
113
if ($act == "expkey") {
114

    
115
	if (!$a_ca[$id]) {
116
		pfSenseHeader("system_camanager.php");
117
		exit;
118
	}
119

    
120
	$exp_name = urlencode("{$a_ca[$id]['name']}.key");
121
	$exp_data = base64_decode($a_ca[$id]['prv']);
122
	$exp_size = strlen($exp_data);
123

    
124
	header("Content-Type: application/octet-stream");
125
	header("Content-Disposition: attachment; filename={$exp_name}");
126
	header("Content-Length: $exp_size");
127
	echo $exp_data;
128
	exit;
129
}
130

    
131
if ($_POST) {
132

    
133
	unset($input_errors);
134
	$pconfig = $_POST;
135

    
136
	/* input validation */
137
	if ($pconfig['method'] == "existing") {
138
		$reqdfields = explode(" ", "name cert");
139
		$reqdfieldsn = array(
140
				gettext("Descriptive name"),
141
				gettext("Certificate data"));
142
	}
143
	if ($pconfig['method'] == "internal") {
144
		$reqdfields = explode(" ",
145
				"name keylen lifetime dn_country dn_state dn_city ".
146
				"dn_organization dn_email dn_commonname");
147
		$reqdfieldsn = array(
148
				gettext("Descriptive name"),
149
				gettext("Key length"),
150
				gettext("Lifetime"),
151
				gettext("Distinguished name Country Code"),
152
				gettext("Distinguished name State or Province"),
153
				gettext("Distinguished name City"),
154
				gettext("Distinguished name Organization"),
155
				gettext("Distinguished name Email Address"),
156
				gettext("Distinguished name Common Name"));
157
	}
158

    
159
	do_input_validation($_POST, $reqdfields, $reqdfieldsn, &$input_errors);
160

    
161
	/* if this is an AJAX caller then handle via JSON */
162
	if (isAjax() && is_array($input_errors)) {
163
		input_errors2Ajax($input_errors);
164
		exit;
165
	}
166

    
167
	/* save modifications */
168
	if (!$input_errors) {
169

    
170
		$ca = array();
171
		$ca['refid'] = uniqid();
172
		if (isset($id) && $a_ca[$id])
173
			$ca = $a_ca[$id];
174

    
175
	    $ca['name'] = $pconfig['name'];
176

    
177
		if ($pconfig['method'] == "existing")
178
			ca_import($ca, $pconfig['cert'], $pconfig['key']);
179

    
180
		if ($pconfig['method'] == "internal")
181
		{
182
			$dn = array(
183
				'countryName' => $pconfig['dn_country'],
184
				'stateOrProvinceName' => $pconfig['dn_state'],
185
				'localityName' => $pconfig['dn_city'],
186
				'organizationName' => $pconfig['dn_organization'],
187
				'emailAddress' => $pconfig['dn_email'],
188
				'commonName' => $pconfig['dn_commonname']);
189

    
190
			ca_create($ca, $pconfig['keylen'], $pconfig['lifetime'], $dn);
191
		}
192

    
193
		if (isset($id) && $a_ca[$id])
194
			$a_ca[$id] = $ca;
195
		else
196
			$a_ca[] = $ca;
197

    
198
		write_config();
199

    
200
//		pfSenseHeader("system_camanager.php");
201
	}
202
}
203

    
204
include("head.inc");
205
?>
206

    
207
<body link="#000000" vlink="#000000" alink="#000000" onload="<?= $jsevents["body"]["onload"] ?>">
208
<?php include("fbegin.inc"); ?>
209
<script type="text/javascript">
210
<!--
211

    
212
function method_change() {
213

    
214
	method = document.iform.method.selectedIndex;
215

    
216
	switch (method) {
217
		case 0:
218
			document.getElementById("existing").style.display="";
219
			document.getElementById("internal").style.display="none";
220
			break;
221
		case 1:
222
			document.getElementById("existing").style.display="none";
223
			document.getElementById("internal").style.display="";
224
			break;
225
	}
226
}
227

    
228
//-->
229
</script>
230
<?php
231
	if ($input_errors)
232
		print_input_errors($input_errors);
233
	if ($savemsg)
234
		print_info_box($savemsg);
235
?>
236
<table width="100%" border="0" cellpadding="0" cellspacing="0">
237
	<tr>
238
		<td>
239
		<?php
240
			$tab_array = array();
241
			$tab_array[] = array(gettext("CAs"), true, "system_camanager.php");
242
			$tab_array[] = array(gettext("Certificates"), false, "system_certmanager.php");
243
			display_top_tabs($tab_array);
244
		?>
245
		</td>
246
	</tr>
247
	<tr>
248
		<td id="mainarea">
249
			<div class="tabcont">
250

    
251
				<?php if ($act == "new" || $act == gettext("save") || $input_errors): ?>
252

    
253
				<form action="system_camanager.php" method="post" name="iform" id="iform">
254
					<table width="100%" border="0" cellpadding="6" cellspacing="0">
255
						<tr>
256
							<td width="22%" valign="top" class="vncellreq"><?=gettext("Descriptive name");?></td>
257
							<td width="78%" class="vtable">
258
								<input name="name" type="text" class="formfld unknown" id="name" size="20" value="<?=htmlspecialchars($pconfig['name']);?>"/>
259
							</td>
260
						</tr>
261
						<?php if (!isset($id)): ?>
262
						<tr>
263
							<td width="22%" valign="top" class="vncellreq"><?=gettext("Method");?></td>
264
							<td width="78%" class="vtable">
265
								<select name='method' id='method' class="formselect" onchange='method_change()'>
266
								<?php
267
									foreach($ca_methods as $method => $desc):
268
									$selected = "";
269
									if ($pconfig['method'] == $method)
270
										$selected = "selected";
271
								?>
272
									<option value="<?=$method;?>"<?=$selected;?>><?=$desc;?></option>
273
								<?php endforeach; ?>
274
								</select>
275
							</td>
276
						</tr>
277
						<?php endif; ?>
278
					</table>
279

    
280
					<table width="100%" border="0" cellpadding="6" cellspacing="0" id="existing">
281
						<tr>
282
							<td colspan="2" class="list" height="12"></td>
283
						</tr>
284
						<tr>
285
							<td colspan="2" valign="top" class="listtopic"><?=gettext("Existing Certificate Authority");?></td>
286
						</tr>
287

    
288
						<tr>
289
							<td width="22%" valign="top" class="vncellreq"><?=gettext("Certificate data");?></td>
290
							<td width="78%" class="vtable">
291
								<textarea name="cert" id="cert" cols="65" rows="7" class="formfld_cert"><?=$pconfig['cert'];?></textarea>
292
								<br>
293
								<?=gettext("Paste a certificate in X.509 PEM format here.");?></td>
294
							</td>
295
						</tr>
296
						<tr>
297
							<td width="22%" valign="top" class="vncellreq"><?=gettext("Certificate Private Key");?><br/><?=gettext("(optional)");?></td>
298
							<td width="78%" class="vtable">
299
								<textarea name="key" id="key" cols="65" rows="7" class="formfld_cert"><?=$pconfig['key'];?></textarea>
300
								<br>
301
								<?=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>
302
							</td>
303
						</tr>
304
					</table>
305

    
306
					<table width="100%" border="0" cellpadding="6" cellspacing="0" id="internal">
307
						<tr>
308
							<td colspan="2" class="list" height="12"></td>
309
						</tr>
310
						<tr>
311
							<td colspan="2" valign="top" class="listtopic"><?=gettext("Internal Certificate Authority");?></td>
312
						</tr>
313
						<tr>
314
							<td width="22%" valign="top" class="vncellreq"><?=gettext("Key length");?></td>
315
							<td width="78%" class="vtable">
316
								<select name='keylen' id='keylen' class="formselect">
317
								<?php
318
									foreach( $ca_keylens as $len):
319
									$selected = "";
320
									if ($pconfig['keylen'] == $len)
321
										$selected = "selected";
322
								?>
323
									<option value="<?=$len;?>"<?=$selected;?>><?=$len;?></option>
324
								<?php endforeach; ?>
325
								</select>
326
								<?=gettext(bits);?>
327
							</td>
328
						</tr>
329
						<tr>
330
							<td width="22%" valign="top" class="vncellreq"><?=gettext("Lifetime");?></td>
331
							<td width="78%" class="vtable">
332
								<input name="lifetime" type="text" class="formfld unknown" id="lifetime" size="5" value="<?=htmlspecialchars($pconfig['lifetime']);?>"/>
333
								<?=gettext(days);?>
334
							</td>
335
						</tr>
336
						<tr>
337
							<td width="22%" valign="top" class="vncellreq"><?=gettext("Distinguished name");?></td>
338
							<td width="78%" class="vtable">
339
								<table border="0" cellspacing="0" cellpadding="2">
340
									<tr>
341
										<td align="right"><?=gettext("Country Code");?> : &nbsp;</td>
342
										<td align="left">
343
											<input name="dn_country" type="text" class="formfld unknown" maxlength="2" size="2" value="<?=htmlspecialchars($pconfig['dn_country']);?>"/>
344
											&nbsp;
345
											<em>ex:</em>
346
											&nbsp;
347
											<?=gettext("US");?>
348
											<em><?=gettext("( two letters )");?></em>
349
										</td>
350
									</tr>
351
									<tr>
352
										<td align="right"><?=gettext("State or Province");?> : &nbsp;</td>
353
										<td align="left">
354
											<input name="dn_state" type="text" class="formfld unknown" size="40" value="<?=htmlspecialchars($pconfig['dn_state']);?>"/>
355
											&nbsp;
356
											<em>ex:</em>
357
											&nbsp;
358
											<?=gettext("Texas");?>
359
										</td>
360
									</tr>
361
									<tr>
362
										<td align="right"><?=gettext("City");?> : &nbsp;</td>
363
										<td align="left">
364
											<input name="dn_city" type="text" class="formfld unknown" size="40" value="<?=htmlspecialchars($pconfig['dn_city']);?>"/>
365
											&nbsp;
366
											<em>ex:</em>
367
											&nbsp;
368
											<?=gettext("Austin");?>
369
										</td>
370
									</tr>
371
									<tr>
372
										<td align="right"><?=gettext("Organization");?> : &nbsp;</td>
373
										<td align="left">
374
											<input name="dn_organization" type="text" class="formfld unknown" size="40" value="<?=htmlspecialchars($pconfig['dn_organization']);?>"/>
375
											&nbsp;
376
											<em>ex:</em>
377
											&nbsp;
378
											<?=gettext("My Company Inc.");?>
379
										</td>
380
									</tr>
381
									<tr>
382
										<td align="right"><?=gettext("Email Address");?> : &nbsp;</td>
383
										<td align="left">
384
											<input name="dn_email" type="text" class="formfld unknown" size="25" value="<?=htmlspecialchars($pconfig['dn_email']);?>"/>
385
											&nbsp;
386
											<em>ex:</em>
387
											&nbsp;
388
											<?=gettext("admin@mycompany.com");?>
389
										</td>
390
									</tr>
391
									<tr>
392
										<td align="right"><?=gettext("Common Name");?> : &nbsp;</td>
393
										<td align="left">
394
											<input name="dn_commonname" type="text" class="formfld unknown" size="25" value="<?=htmlspecialchars($pconfig['dn_commonname']);?>"/>
395
											&nbsp;
396
											<em>ex:</em>
397
											&nbsp;
398
											<?=gettext("internal-ca");?>
399
										</td>
400
									</tr>
401
								</table>
402
							</td>
403
						</tr>
404
					</table>
405

    
406
					<table width="100%" border="0" cellpadding="6" cellspacing="0">
407
						<tr>
408
							<td width="22%" valign="top">&nbsp;</td>
409
							<td width="78%">
410
								<input id="submit" name="save" type="submit" class="formbtn" value="<?=gettext("Save"); ?>" />
411
								<?php if (isset($id) && $a_ca[$id]): ?>
412
								<input name="id" type="hidden" value="<?=$id;?>" />
413
								<?php endif;?>
414
							</td>
415
						</tr>
416
					</table>
417
				</form>
418

    
419
				<?php else: ?>
420

    
421
				<table width="100%" border="0" cellpadding="0" cellspacing="0">
422
					<tr>
423
						<td width="20%" class="listhdrr"><?=gettext("Name");?></td>
424
						<td width="10%" class="listhdrr"><?=gettext("Internal");?></td>
425
						<td width="10%" class="listhdrr"><?=gettext("Issuer");?></td>
426
						<td width="10%" class="listhdrr"><?=gettext("Certificates");?></td>
427
						<td width="40%" class="listhdrr"><?=gettext("Distinguished Name");?></td>
428
						<td width="10%" class="list"></td>
429
					</tr>
430
					<?php
431
						$i = 0;
432
						foreach($a_ca as $ca):
433
							$name = htmlspecialchars($ca['name']);
434
							$subj = cert_get_subject($ca['crt']);
435
							$issuer = cert_get_issuer($ca['crt']);
436
							if($subj == $issuer)
437
							  $issuer_name = "<em>" . gettext("self-signed") . "</em>";
438
							else
439
							  $issuer_name = "<em>" . gettext("external") . "</em>";
440
							$subj = htmlspecialchars($subj);
441
							$issuer = htmlspecialchars($issuer);
442
							$certcount = 0;
443

    
444
							$issuer_ca = lookup_ca($ca['caref']);
445
							if ($issuer_ca)
446
								$issuer_name = $issuer_ca['name'];
447

    
448
							// TODO : Need gray certificate icon
449

    
450
							if($ca['prv']) {
451
								$caimg = "/themes/{$g['theme']}/images/icons/icon_frmfld_cert.png";
452
								$internal = "YES";
453

    
454
							} else {
455
								$caimg = "/themes/{$g['theme']}/images/icons/icon_frmfld_cert.png";
456
								$internal = "NO";
457
							}
458
							foreach ($a_cert as $cert)
459
								if ($cert['caref'] == $ca['refid'])
460
									$certcount++;
461
  						foreach ($a_ca as $cert)
462
  							if ($cert['caref'] == $ca['refid'])
463
  								$certcount++;
464
					?>
465
					<tr>
466
						<td class="listlr">
467
							<table border="0" cellpadding="0" cellspacing="0">
468
								<tr>
469
									<td align="left" valign="center">
470
										<img src="<?=$caimg;?>" alt="CA" title="CA" border="0" height="16" width="16" />
471
									</td>
472
									<td align="left" valign="middle">
473
										<?=$name;?>
474
									</td>
475
								</tr>
476
							</table>
477
						</td>
478
						<td class="listr"><?=$internal;?>&nbsp;</td>
479
						<td class="listr"><?=$issuer_name;?>&nbsp;</td>
480
						<td class="listr"><?=$certcount;?>&nbsp;</td>
481
						<td class="listr"><?=$subj;?>&nbsp;</td>
482
						<td valign="middle" nowrap class="list">
483
							<a href="system_camanager.php?act=exp&id=<?=$i;?>")">
484
								<img src="/themes/<?= $g['theme'];?>/images/icons/icon_down.gif" title="<?=gettext("export ca");?>" alt="<?=gettext("export ca");?>" width="17" height="17" border="0" />
485
							</a>
486
							<?php if ($ca['prv']): ?>
487
							<a href="system_camanager.php?act=expkey&id=<?=$i;?>")">
488
								<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" />
489
							</a>
490
							<?php endif; ?>
491
							<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?");?>')">
492
								<img src="/themes/<?= $g['theme'];?>/images/icons/icon_x.gif" title="<?=gettext("delete ca");?>" alt="<?=gettext("delete ca"); ?>" width="17" height="17" border="0" />
493
							</a>
494
						</td>
495
					</tr>
496
					<?php
497
							$i++;
498
						endforeach;
499
					?>
500
					<tr>
501
						<td class="list" colspan="5"></td>
502
						<td class="list">
503
							<a href="system_camanager.php?act=new">
504
								<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" />
505
							</a>
506
						</td>
507
					</tr>
508
					<tr>
509
						<td colspan="5">
510
							<p>
511
								<?=gettext("Additional trusted Certificate Authorities can be added here.");?>
512
							</p>
513
						</td>
514
					</tr>
515
				</table>
516

    
517
				<?php endif; ?>
518

    
519
			</div>
520
		</td>
521
	</tr>
522
</table>
523
<?php include("fend.inc");?>
524
<script type="text/javascript">
525
<!--
526

    
527
method_change();
528

    
529
//-->
530
</script>
531

    
532
</body>
(179-179/221)