Project

General

Profile

Download (14.2 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
##|+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
require_once("certs.inc");
39

    
40
$ca_methods = array(
41
	"existing" => "Import an existing Certificate Authority",
42
	"internal" => "Create an internal Certificate Authority");
43

    
44
$ca_keylens = array( "512", "1024", "2048", "4096");
45

    
46
$pgtitle = array("System", "Certificate Authority Manager");
47

    
48
$id = $_GET['id'];
49
if (isset($_POST['id']))
50
	$id = $_POST['id'];
51

    
52
if (!is_array($config['system']['ca']))
53
	$config['system']['ca'] = array();
54

    
55
$a_ca =& $config['system']['ca'];
56

    
57
if (!is_array($config['system']['cert']))
58
	$config['system']['cert'] = array();
59

    
60
$a_cert =& $config['system']['cert'];
61

    
62
$act = $_GET['act'];
63
if ($_POST['act'])
64
	$act = $_POST['act'];
65

    
66
if ($act == "del") {
67

    
68
	if (!$a_ca[$id]) {
69
		pfSenseHeader("system_camanager.php");
70
		exit;
71
	}
72

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

    
78
	$name = $a_ca[$id]['name'];
79
	unset($a_ca[$id]);
80
	write_config();
81
	$savemsg = gettext("Certificate Authority")." {$name} ".
82
				gettext("successfully deleted")."<br/>";
83
}
84

    
85
if ($act == "new") {
86
	$pconfig['method'] = $_GET['method'];
87
	$pconfig['keylen'] = "2048";
88
	$pconfig['lifetime'] = "365";
89
	$pconfig['dn_commonname'] = "internal-ca";
90
}
91

    
92
if ($act == "exp") {
93

    
94
	if (!$a_ca[$id]) {
95
		pfSenseHeader("system_camanager.php");
96
		exit;
97
	}
98

    
99
	$exp_name = urlencode("{$a_ca[$id]['name']}.crt");
100
	$exp_data = base64_decode($a_ca[$id]['crt']);
101
	$exp_size = strlen($exp_data);
102

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

    
110
if ($_POST) {
111

    
112
	unset($input_errors);
113
	$pconfig = $_POST;
114

    
115
	/* input validation */
116
	if ($pconfig['method'] == "existing") {
117
		$reqdfields = explode(" ", "name cert");
118
		$reqdfieldsn = explode(",", "Desriptive name,Certificate data");
119
	}
120
	if ($pconfig['method'] == "internal") {
121
		$reqdfields = explode(" ",
122
				"name keylen lifetime dn_country dn_state dn_city ".
123
				"dn_organization dn_email dn_commonname");
124
		$reqdfieldsn = explode(",",
125
				"Desriptive name,Key length,Lifetime,".
126
				"Distinguished name Country Code,".
127
				"Distinguished name State or Province,".
128
				"Distinguished name City,".
129
				"Distinguished name Organization,".
130
				"Distinguished name Email Address,".
131
				"Distinguished name Common Name");
132
	}
133

    
134
	do_input_validation($_POST, $reqdfields, $reqdfieldsn, &$input_errors);
135

    
136
	/* if this is an AJAX caller then handle via JSON */
137
	if (isAjax() && is_array($input_errors)) {
138
		input_errors2Ajax($input_errors);
139
		exit;
140
	}
141

    
142
	/* save modifications */
143
	if (!$input_errors) {
144

    
145
		$ca = array();
146
		$ca['refid'] = uniqid();
147
		if (isset($id) && $a_ca[$id])
148
			$ca = $a_ca[$id];
149

    
150
	    $ca['name'] = $pconfig['name'];
151

    
152
		if ($pconfig['method'] == "existing")
153
			ca_import($ca, $pconfig['cert']);
154

    
155
		if ($pconfig['method'] == "internal")
156
		{
157
			$dn = array(
158
				'countryName' => $pconfig['dn_country'],
159
				'stateOrProvinceName' => $pconfig['dn_state'],
160
				'localityName' => $pconfig['dn_city'],
161
				'organizationName' => $pconfig['dn_organization'],
162
				'emailAddress' => $pconfig['dn_email'],
163
				'commonName' => $pconfig['dn_commonname']);
164

    
165
			ca_create($ca, $pconfig['keylen'], $pconfig['lifetime'], $dn);
166
		}
167

    
168
		if (isset($id) && $a_ca[$id])
169
			$a_ca[$id] = $ca;
170
		else
171
			$a_ca[] = $ca;
172

    
173
		write_config();
174

    
175
//		pfSenseHeader("system_camanager.php");
176
	}
177
}
178

    
179
include("head.inc");
180
?>
181

    
182
<body link="#000000" vlink="#000000" alink="#000000" onload="<?= $jsevents["body"]["onload"] ?>">
183
<?php include("fbegin.inc"); ?>
184
<script type="text/javascript">
185
<!--
186

    
187
function method_change() {
188

    
189
	method = document.iform.method.selectedIndex;
190

    
191
	switch (method) {
192
		case 0:
193
			document.getElementById("existing").style.display="";
194
			document.getElementById("internal").style.display="none";
195
			break;
196
		case 1:
197
			document.getElementById("existing").style.display="none";
198
			document.getElementById("internal").style.display="";
199
			break;
200
	}
201
}
202

    
203
//-->
204
</script>
205
<?php
206
	if ($input_errors)
207
		print_input_errors($input_errors);
208
	if ($savemsg)
209
		print_info_box($savemsg);
210
?>
211
<table width="100%" border="0" cellpadding="0" cellspacing="0">
212
	<tr>
213
		<td>
214
		<?php
215
			$tab_array = array();
216
			$tab_array[] = array(gettext("CAs"), true, "system_camanager.php");
217
			$tab_array[] = array(gettext("Certificates"), false, "system_certmanager.php");
218
			display_top_tabs($tab_array);
219
		?>
220
		</td>
221
	</tr>
222
	<tr>
223
		<td id="mainarea">
224
			<div class="tabcont">
225

    
226
				<?php if ($act == "new" || $act == "save" || $input_errors): ?>
227

    
228
				<form action="system_camanager.php" method="post" name="iform" id="iform">
229
					<table width="100%" border="0" cellpadding="6" cellspacing="0">
230
						<tr>
231
							<td width="22%" valign="top" class="vncellreq"><?=gettext("Descriptive name");?></td>
232
							<td width="78%" class="vtable">
233
								<input name="name" type="text" class="formfld unknown" id="name" size="20" value="<?=htmlspecialchars($pconfig['name']);?>"/>
234
							</td>
235
						</tr>
236
						<?php if (!isset($id)): ?>
237
						<tr>
238
							<td width="22%" valign="top" class="vncellreq"><?=gettext("Method");?></td>
239
							<td width="78%" class="vtable">
240
								<select name='method' id='method' class="formselect" onchange='method_change()'>
241
								<?php
242
									foreach($ca_methods as $method => $desc):
243
									$selected = "";
244
									if ($pconfig['method'] == $method)
245
										$selected = "selected";
246
								?>
247
									<option value="<?=$method;?>"<?=$selected;?>><?=$desc;?></option>
248
								<?php endforeach; ?>
249
								</select>
250
							</td>
251
						</tr>
252
						<?php endif; ?>
253
					</table>
254

    
255
					<table width="100%" border="0" cellpadding="6" cellspacing="0" id="existing">
256
						<tr>
257
							<td colspan="2" class="list" height="12"></td>
258
						</tr>
259
						<tr>
260
							<td colspan="2" valign="top" class="listtopic">Existing Certificate Authority</td>
261
						</tr>
262

    
263
						<tr>
264
							<td width="22%" valign="top" class="vncellreq">Certificate data</td>
265
							<td width="78%" class="vtable">
266
								<textarea name="cert" id="cert" cols="65" rows="7" class="formfld_cert"><?=$pconfig['cert'];?></textarea>
267
								<br>
268
								Paste a certificate in X.509 PEM format here.</td>
269
							</td>
270
						</tr>
271
					</table>
272

    
273
					<table width="100%" border="0" cellpadding="6" cellspacing="0" id="internal">
274
						<tr>
275
							<td colspan="2" class="list" height="12"></td>
276
						</tr>
277
						<tr>
278
							<td colspan="2" valign="top" class="listtopic">Internal Certificate Authority</td>
279
						</tr>
280
						<tr>
281
							<td width="22%" valign="top" class="vncellreq"><?=gettext("Key length");?></td>
282
							<td width="78%" class="vtable">
283
								<select name='keylen' id='keylen' class="formselect">
284
								<?php
285
									foreach( $ca_keylens as $len):
286
									$selected = "";
287
									if ($pconfig['keylen'] == $len)
288
										$selected = "selected";
289
								?>
290
									<option value="<?=$len;?>"<?=$selected;?>><?=$len;?></option>
291
								<?php endforeach; ?>
292
								</select>
293
								bits
294
							</td>
295
						</tr>
296
						<tr>
297
							<td width="22%" valign="top" class="vncellreq"><?=gettext("Lifetime");?></td>
298
							<td width="78%" class="vtable">
299
								<input name="lifetime" type="text" class="formfld unknown" id="lifetime" size="5" value="<?=htmlspecialchars($pconfig['lifetime']);?>"/>
300
								days
301
							</td>
302
						</tr>
303
						<tr>
304
							<td width="22%" valign="top" class="vncellreq"><?=gettext("Distinguished name");?></td>
305
							<td width="78%" class="vtable">
306
								<table border="0" cellspacing="0" cellpadding="2">
307
									<tr>
308
										<td align="right">Country Code : &nbsp;</td>
309
										<td align="left">
310
											<input name="dn_country" type="text" class="formfld unknown" maxlength="2" size="2" value="<?=htmlspecialchars($pconfig['dn_country']);?>"/>
311
											&nbsp;
312
											<em>ex:</em>
313
											&nbsp;
314
											US
315
											<em>( two letters )</em>
316
										</td>
317
									</tr>
318
									<tr>
319
										<td align="right">State or Province : &nbsp;</td>
320
										<td align="left">
321
											<input name="dn_state" type="text" class="formfld unknown" size="40" value="<?=htmlspecialchars($pconfig['dn_state']);?>"/>
322
											&nbsp;
323
											<em>ex:</em>
324
											&nbsp;
325
											Texas
326
										</td>
327
									</tr>
328
									<tr>
329
										<td align="right">City : &nbsp;</td>
330
										<td align="left">
331
											<input name="dn_city" type="text" class="formfld unknown" size="40" value="<?=htmlspecialchars($pconfig['dn_city']);?>"/>
332
											&nbsp;
333
											<em>ex:</em>
334
											&nbsp;
335
											Austin
336
										</td>
337
									</tr>
338
									<tr>
339
										<td align="right">Organization : &nbsp;</td>
340
										<td align="left">
341
											<input name="dn_organization" type="text" class="formfld unknown" size="40" value="<?=htmlspecialchars($pconfig['dn_organization']);?>"/>
342
											&nbsp;
343
											<em>ex:</em>
344
											&nbsp;
345
											My Company Inc.
346
										</td>
347
									</tr>
348
									<tr>
349
										<td align="right">Email Address : &nbsp;</td>
350
										<td align="left">
351
											<input name="dn_email" type="text" class="formfld unknown" size="25" value="<?=htmlspecialchars($pconfig['dn_email']);?>"/>
352
											&nbsp;
353
											<em>ex:</em>
354
											&nbsp;
355
											admin@mycompany.com
356
										</td>
357
									</tr>
358
									<tr>
359
										<td align="right">Common Name : &nbsp;</td>
360
										<td align="left">
361
											<input name="dn_commonname" type="text" class="formfld unknown" size="25" value="<?=htmlspecialchars($pconfig['dn_commonname']);?>"/>
362
											&nbsp;
363
											<em>ex:</em>
364
											&nbsp;
365
											internal-ca
366
										</td>
367
									</tr>
368
								</table>
369
							</td>
370
						</tr>
371
					</table>
372

    
373
					<table width="100%" border="0" cellpadding="6" cellspacing="0">
374
						<tr>
375
							<td width="22%" valign="top">&nbsp;</td>
376
							<td width="78%">
377
								<input id="submit" name="save" type="submit" class="formbtn" value="Save" />
378
								<?php if (isset($id) && $a_ca[$id]): ?>
379
								<input name="id" type="hidden" value="<?=$id;?>" />
380
								<?php endif;?>
381
							</td>
382
						</tr>
383
					</table>
384
				</form>
385

    
386
				<?php else: ?>
387

    
388
				<table width="100%" border="0" cellpadding="0" cellspacing="0">
389
					<tr>
390
						<td width="20%" class="listhdrr">Name</td>
391
						<td width="10%" class="listhdrr">Internal</td>
392
						<td width="10%" class="listhdrr">Certificates</td>
393
						<td width="40%" class="listhdrr">Distinguished Name</td>
394
						<td width="10%" class="list"></td>
395
					</tr>
396
					<?php
397
						$i = 0;
398
						foreach($a_ca as $ca):
399
							$name = htmlspecialchars($ca['name']);
400
							$subj = cert_get_subject($ca['crt']);
401
							$subj = htmlspecialchars($subj);
402
							$certcount = 0;
403

    
404
							// TODO : Need gray certificate icon
405

    
406
							if($ca['prv']) {
407
								$caimg = "/themes/{$g['theme']}/images/icons/icon_frmfld_cert.png";
408
								$internal = "YES";
409

    
410
								foreach ($a_cert as $cert)
411
									if ($cert['caref'] == $ca['refid'])
412
										$certcount++;
413
							} else {
414
								$caimg = "/themes/{$g['theme']}/images/icons/icon_frmfld_cert.png";
415
								$internal = "NO";
416
							}
417
					?>
418
					<tr>
419
						<td class="listlr">
420
							<table border="0" cellpadding="0" cellspacing="0">
421
								<tr>
422
									<td align="left" valign="center">
423
										<img src="<?=$caimg;?>" alt="CA" title="CA" border="0" height="16" width="16" />
424
									</td>
425
									<td align="left" valign="middle">
426
										<?=$name;?>
427
									</td>
428
								</tr>
429
							</table>
430
						</td>
431
						<td class="listr"><?=$internal;?>&nbsp;</td>
432
						<td class="listr"><?=$certcount;?>&nbsp;</td>
433
						<td class="listr"><?=$subj;?>&nbsp;</td>
434
						<td valign="middle" nowrap class="list">
435
							<a href="system_camanager.php?act=exp&id=<?=$i;?>")">
436
								<img src="/themes/<?= $g['theme'];?>/images/icons/icon_down.gif" title="export ca" alt="export ca" width="17" height="17" border="0" />
437
							</a>
438
							<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?");?>')">
439
								<img src="/themes/<?= $g['theme'];?>/images/icons/icon_x.gif" title="delete ca" alt="delete ca" width="17" height="17" border="0" />
440
							</a>
441
						</td>
442
					</tr>
443
					<?php
444
							$i++;
445
						endforeach;
446
					?>
447
					<tr>
448
						<td class="list" colspan="4"></td>
449
						<td class="list">
450
							<a href="system_camanager.php?act=new">
451
								<img src="/themes/<?= $g['theme'];?>/images/icons/icon_plus.gif" title="add or import ca" alt="add ca" width="17" height="17" border="0" />
452
							</a>
453
						</td>
454
					</tr>
455
					<tr>
456
						<td colspan="4">
457
							<p>
458
								<?=gettext("Additional trusted certificate authorities can be added here.");?>
459
							</p>
460
						</td>
461
					</tr>
462
				</table>
463

    
464
				<?php endif; ?>
465

    
466
			</div>
467
		</td>
468
	</tr>
469
</table>
470
<?php include("fend.inc");?>
471
<script type="text/javascript">
472
<!--
473

    
474
method_change();
475

    
476
//-->
477
</script>
478

    
479
</body>
(175-175/216)