Project

General

Profile

Download (14.2 KB) Statistics
| Branch: | Tag: | Revision:
1 64cc39d3 Matthew Grooms
<?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
##|+PRIV
31
##|*IDENT=page-system-camanager
32
##|*NAME=System: CA Manager
33
##|*DESCR=Allow access to the 'System: CA Manager' page.
34
##|*MATCH=system_camanager.php*
35
##|-PRIV
36
37
require("guiconfig.inc");
38
39
$ca_methods = array(
40
	"existing" => "Import an existing Certificate Authority",
41
	"internal" => "Create an internal Certificate Authority");
42
43
$ca_keylens = array( "512", "1024", "2048", "4096");
44
45
$pgtitle = array("System", "Certificate Authority Manager");
46
47
$id = $_GET['id'];
48
if (isset($_POST['id']))
49
	$id = $_POST['id'];
50
51
if (!is_array($config['system']['ca']))
52
	$config['system']['ca'] = array();
53
54
$a_ca =& $config['system']['ca'];
55
56
if (!is_array($config['system']['cert']))
57
	$config['system']['cert'] = array();
58
59
$a_cert =& $config['system']['cert'];
60
61
$act = $_GET['act'];
62
if ($_POST['act'])
63
	$act = $_POST['act'];
64
65
if ($act == "del") {
66
67
	if (!$a_ca[$id]) {
68
		pfSenseHeader("system_camanager.php");
69
		exit;
70
	}
71
72
	$index = count($a_cert) - 1;
73
	for (;$index >=0; $index--)
74
		if ($a_cert[$index]['caref'] == $a_ca[$id]['refid'])
75
			unset($a_cert[$index]);
76
77
	$name = $a_ca[$id]['name'];
78
	unset($a_ca[$id]);
79
	write_config();
80
	$savemsg = gettext("Certificate Authority")." {$name} ".
81
				gettext("successfully deleted")."<br/>";
82
}
83
84
if ($act == "new") {
85
	$pconfig['method'] = $_GET['method'];
86
	$pconfig['keylen'] = "2048";
87
	$pconfig['lifetime'] = "365";
88
	$pconfig['dn_commonname'] = "internal-ca";
89
}
90
91 93823b10 Matthew Grooms
if ($act == "exp") {
92
93
	if (!$a_ca[$id]) {
94
		pfSenseHeader("system_camanager.php");
95
		exit;
96
	}
97
98
	$exp_name = urlencode("{$a_ca[$id]['name']}.crt");
99
	$exp_data = base64_decode($a_ca[$id]['crt']);
100
	$exp_size = strlen($exp_data);
101
102
	header("Content-Type: application/octet-stream");
103
	header("Content-Disposition: attachment; filename={$exp_name}");
104
	header("Content-Length: $exp_size");
105
	echo $exp_data;
106
	exit;
107
}
108
109 64cc39d3 Matthew Grooms
if ($_POST) {
110
111
	unset($input_errors);
112
	$pconfig = $_POST;
113
114
	/* input validation */
115
	if ($pconfig['method'] == "existing") {
116
		$reqdfields = explode(" ", "name cert");
117
		$reqdfieldsn = explode(",", "Desriptive name,Certificate data");
118
	}
119
	if ($pconfig['method'] == "internal") {
120
		$reqdfields = explode(" ",
121
				"name keylen lifetime dn_country dn_state dn_city ".
122
				"dn_organization dn_email dn_commonname");
123
		$reqdfieldsn = explode(",",
124
				"Desriptive name,Key length,Lifetime,".
125
				"Distinguished name Country Code,".
126
				"Distinguished name State or Province,".
127
				"Distinguished name City,".
128
				"Distinguished name Organization,".
129
				"Distinguished name Email Address,".
130
				"Distinguished name Common Name");
131
	}
132
133
	do_input_validation($_POST, $reqdfields, $reqdfieldsn, &$input_errors);
134
135
	/* if this is an AJAX caller then handle via JSON */
136
	if (isAjax() && is_array($input_errors)) {
137
		input_errors2Ajax($input_errors);
138
		exit;
139
	}
140
141
	/* save modifications */
142
	if (!$input_errors) {
143
144
		$ca = array();
145
		$ca['refid'] = uniqid();
146
		if (isset($id) && $a_ca[$id])
147
			$ca = $a_ca[$id];
148
149
	    $ca['name'] = $pconfig['name'];
150
151
		if ($pconfig['method'] == "existing")
152
			ca_import($ca, $pconfig['cert']);
153
154
		if ($pconfig['method'] == "internal")
155
		{
156
			$dn = array(
157
				'countryName' => $pconfig['dn_country'],
158
				'stateOrProvinceName' => $pconfig['dn_state'],
159
				'localityName' => $pconfig['dn_city'],
160
				'organizationName' => $pconfig['dn_organization'],
161
				'emailAddress' => $pconfig['dn_email'],
162
				'commonName' => $pconfig['dn_commonname']);
163
164
			ca_create($ca, $pconfig['keylen'], $pconfig['lifetime'], $dn);
165
		}
166
167
		if (isset($id) && $a_ca[$id])
168
			$a_ca[$id] = $ca;
169
		else
170
			$a_ca[] = $ca;
171
172
		write_config();
173
174
//		pfSenseHeader("system_camanager.php");
175
	}
176
}
177
178
include("head.inc");
179
?>
180
181
<body link="#000000" vlink="#000000" alink="#000000" onload="<?= $jsevents["body"]["onload"] ?>">
182
<?php include("fbegin.inc"); ?>
183
<script type="text/javascript">
184
<!--
185
186
function method_change() {
187
188
	method = document.iform.method.selectedIndex;
189
190
	switch (method) {
191
		case 0:
192
			document.getElementById("existing").style.display="";
193
			document.getElementById("internal").style.display="none";
194
			break;
195
		case 1:
196
			document.getElementById("existing").style.display="none";
197
			document.getElementById("internal").style.display="";
198
			break;
199
	}
200
}
201
202
//-->
203
</script>
204
<?php
205
	if ($input_errors)
206
		print_input_errors($input_errors);
207
	if ($savemsg)
208
		print_info_box($savemsg);
209
?>
210
<table width="100%" border="0" cellpadding="0" cellspacing="0">
211
	<tr>
212 96c7a492 Matthew Grooms
		<td>
213 64cc39d3 Matthew Grooms
		<?php
214
			$tab_array = array();
215
			$tab_array[] = array(gettext("CAs"), true, "system_camanager.php");
216
			$tab_array[] = array(gettext("Certificates"), false, "system_certmanager.php");
217
			display_top_tabs($tab_array);
218
		?>
219
		</td>
220
	</tr>
221
	<tr>
222 96c7a492 Matthew Grooms
		<td id="mainarea">
223
			<div class="tabcont">
224
225
				<?php if ($act == "new" || $act == "save" || $input_errors): ?>
226
227
				<form action="system_camanager.php" method="post" name="iform" id="iform">
228
					<table width="100%" border="0" cellpadding="6" cellspacing="0">
229
						<tr>
230
							<td width="22%" valign="top" class="vncellreq"><?=gettext("Descriptive name");?></td>
231
							<td width="78%" class="vtable">
232
								<input name="name" type="text" class="formfld unknown" id="name" size="20" value="<?=htmlspecialchars($pconfig['name']);?>"/>
233
							</td>
234
						</tr>
235
						<?php if (!isset($id)): ?>
236
						<tr>
237
							<td width="22%" valign="top" class="vncellreq"><?=gettext("Method");?></td>
238
							<td width="78%" class="vtable">
239
								<select name='method' id='method' class="formselect" onchange='method_change()'>
240
								<?php
241
									foreach($ca_methods as $method => $desc):
242
									$selected = "";
243
									if ($pconfig['method'] == $method)
244
										$selected = "selected";
245
								?>
246
									<option value="<?=$method;?>"<?=$selected;?>><?=$desc;?></option>
247
								<?php endforeach; ?>
248
								</select>
249
							</td>
250
						</tr>
251
						<?php endif; ?>
252
					</table>
253
254
					<table width="100%" border="0" cellpadding="6" cellspacing="0" id="existing">
255
						<tr>
256
							<td colspan="2" class="list" height="12"></td>
257
						</tr>
258
						<tr>
259
							<td colspan="2" valign="top" class="listtopic">Existing Certificate Authority</td>
260
						</tr>
261
262
						<tr>
263
							<td width="22%" valign="top" class="vncellreq">Certificate data</td>
264
							<td width="78%" class="vtable">
265
								<textarea name="cert" id="cert" cols="65" rows="7" class="formfld_cert"><?=$pconfig['cert'];?></textarea>
266
								<br>
267
								Paste a certificate in X.509 PEM format here.</td>
268
							</td>
269
						</tr>
270
					</table>
271
272
					<table width="100%" border="0" cellpadding="6" cellspacing="0" id="internal">
273
						<tr>
274
							<td colspan="2" class="list" height="12"></td>
275
						</tr>
276
						<tr>
277
							<td colspan="2" valign="top" class="listtopic">Internal Certificate Authority</td>
278
						</tr>
279
						<tr>
280
							<td width="22%" valign="top" class="vncellreq"><?=gettext("Key length");?></td>
281
							<td width="78%" class="vtable">
282
								<select name='keylen' id='keylen' class="formselect">
283
								<?php
284
									foreach( $ca_keylens as $len):
285
									$selected = "";
286
									if ($pconfig['keylen'] == $len)
287
										$selected = "selected";
288
								?>
289
									<option value="<?=$len;?>"<?=$selected;?>><?=$len;?></option>
290
								<?php endforeach; ?>
291
								</select>
292
								bits
293
							</td>
294
						</tr>
295
						<tr>
296
							<td width="22%" valign="top" class="vncellreq"><?=gettext("Lifetime");?></td>
297
							<td width="78%" class="vtable">
298
								<input name="lifetime" type="text" class="formfld unknown" id="lifetime" size="5" value="<?=htmlspecialchars($pconfig['lifetime']);?>"/>
299
								days
300
							</td>
301
						</tr>
302
						<tr>
303
							<td width="22%" valign="top" class="vncellreq"><?=gettext("Distinguished name");?></td>
304
							<td width="78%" class="vtable">
305
								<table border="0" cellspacing="0" cellpadding="2">
306
									<tr>
307
										<td align="right">Country Code : &nbsp;</td>
308
										<td align="left">
309
											<input name="dn_country" type="text" class="formfld unknown" size="2" value="<?=htmlspecialchars($pconfig['dn_country']);?>"/>
310
											&nbsp;
311
											<em>ex:</em>
312
											&nbsp;
313
											US
314
											<em>( two letters )</em>
315
										</td>
316
									</tr>
317
									<tr>
318
										<td align="right">State or Province : &nbsp;</td>
319
										<td align="left">
320
											<input name="dn_state" type="text" class="formfld unknown" size="40" value="<?=htmlspecialchars($pconfig['dn_state']);?>"/>
321
											&nbsp;
322
											<em>ex:</em>
323
											&nbsp;
324
											Texas
325
										</td>
326
									</tr>
327
									<tr>
328
										<td align="right">City : &nbsp;</td>
329
										<td align="left">
330
											<input name="dn_city" type="text" class="formfld unknown" size="40" value="<?=htmlspecialchars($pconfig['dn_city']);?>"/>
331
											&nbsp;
332
											<em>ex:</em>
333
											&nbsp;
334
											Austin
335
										</td>
336
									</tr>
337
									<tr>
338
										<td align="right">Organization : &nbsp;</td>
339
										<td align="left">
340
											<input name="dn_organization" type="text" class="formfld unknown" size="40" value="<?=htmlspecialchars($pconfig['dn_organization']);?>"/>
341
											&nbsp;
342
											<em>ex:</em>
343
											&nbsp;
344
											My Company Inc.
345
										</td>
346
									</tr>
347
									<tr>
348
										<td align="right">Email Address : &nbsp;</td>
349
										<td align="left">
350
											<input name="dn_email" type="text" class="formfld unknown" size="25" value="<?=htmlspecialchars($pconfig['dn_email']);?>"/>
351
											&nbsp;
352
											<em>ex:</em>
353
											&nbsp;
354
											admin@mycompany.com
355
										</td>
356
									</tr>
357
									<tr>
358
										<td align="right">Common Name : &nbsp;</td>
359
										<td align="left">
360
											<input name="dn_commonname" type="text" class="formfld unknown" size="25" value="<?=htmlspecialchars($pconfig['dn_commonname']);?>"/>
361
											&nbsp;
362
											<em>ex:</em>
363
											&nbsp;
364
											internal-ca
365
										</td>
366
									</tr>
367
								</table>
368
							</td>
369
						</tr>
370
					</table>
371
372
					<table width="100%" border="0" cellpadding="6" cellspacing="0">
373
						<tr>
374
							<td width="22%" valign="top">&nbsp;</td>
375
							<td width="78%">
376
								<input id="submit" name="save" type="submit" class="formbtn" value="Save" />
377
								<?php if (isset($id) && $a_ca[$id]): ?>
378
								<input name="id" type="hidden" value="<?=$id;?>" />
379
								<?php endif;?>
380
							</td>
381
						</tr>
382
					</table>
383
				</form>
384
385
				<?php else: ?>
386
387
				<table width="100%" border="0" cellpadding="0" cellspacing="0">
388 64cc39d3 Matthew Grooms
					<tr>
389 96c7a492 Matthew Grooms
						<td width="20%" class="listhdrr">Name</td>
390
						<td width="10%" class="listhdrr">Internal</td>
391
						<td width="10%" class="listhdrr">Certificates</td>
392
						<td width="40%" class="listhdrr">Distinguished Name</td>
393
						<td width="10%" class="list"></td>
394 64cc39d3 Matthew Grooms
					</tr>
395 96c7a492 Matthew Grooms
					<?php
396
						$i = 0;
397
						foreach($a_ca as $ca):
398
							$name = htmlspecialchars($ca['name']);
399
							$subj = cert_get_subject($ca['crt']);
400
							$subj = htmlspecialchars($subj);
401
							$certcount = 0;
402
403
							// TODO : Need gray certificate icon
404
405
							if($ca['prv']) {
406
								$caimg = "/themes/{$g['theme']}/images/icons/icon_frmfld_cert.png";
407
								$internal = "YES";
408
409
								foreach ($a_cert as $cert)
410
									if ($cert['caref'] == $ca['refid'])
411
										$certcount++;
412
							} else {
413
								$caimg = "/themes/{$g['theme']}/images/icons/icon_frmfld_cert.png";
414
								$internal = "NO";
415
							}
416
					?>
417 64cc39d3 Matthew Grooms
					<tr>
418 96c7a492 Matthew Grooms
						<td class="listlr">
419
							<table border="0" cellpadding="0" cellspacing="0">
420 64cc39d3 Matthew Grooms
								<tr>
421 96c7a492 Matthew Grooms
									<td align="left" valign="center">
422
										<img src="<?=$caimg;?>" alt="CA" title="CA" border="0" height="16" width="16" />
423 64cc39d3 Matthew Grooms
									</td>
424 96c7a492 Matthew Grooms
									<td align="left" valign="middle">
425
										<?=$name;?>
426 64cc39d3 Matthew Grooms
									</td>
427
								</tr>
428
							</table>
429
						</td>
430 96c7a492 Matthew Grooms
						<td class="listr"><?=$internal;?>&nbsp;</td>
431
						<td class="listr"><?=$certcount;?>&nbsp;</td>
432
						<td class="listr"><?=$subj;?>&nbsp;</td>
433
						<td valign="middle" nowrap class="list">
434
							<a href="system_camanager.php?act=exp&id=<?=$i;?>")">
435
								<img src="/themes/<?= $g['theme'];?>/images/icons/icon_down.gif" title="export ca" alt="export ca" width="17" height="17" border="0" />
436
							</a>
437
							<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?");?>')">
438
								<img src="/themes/<?= $g['theme'];?>/images/icons/icon_x.gif" title="delete ca" alt="delete ca" width="17" height="17" border="0" />
439
							</a>
440
						</td>
441
					</tr>
442
					<?php
443
							$i++;
444
						endforeach;
445
					?>
446
					<tr>
447
						<td class="list" colspan="4"></td>
448
						<td class="list">
449
							<a href="system_camanager.php?act=new">
450
								<img src="/themes/<?= $g['theme'];?>/images/icons/icon_plus.gif" title="add or import ca" alt="add ca" width="17" height="17" border="0" />
451
							</a>
452
						</td>
453 64cc39d3 Matthew Grooms
					</tr>
454
					<tr>
455 96c7a492 Matthew Grooms
						<td colspan="4">
456
							<p>
457
								<?=gettext("Additional trusted certificate authorities can be added here.");?>
458
							</p>
459 64cc39d3 Matthew Grooms
						</td>
460
					</tr>
461
				</table>
462
463 96c7a492 Matthew Grooms
				<?php endif; ?>
464
465
			</div>
466 64cc39d3 Matthew Grooms
		</td>
467
	</tr>
468
</table>
469
<?php include("fend.inc");?>
470
<script type="text/javascript">
471
<!--
472
473
method_change();
474
475
//-->
476
</script>
477
478
</body>