Project

General

Profile

Download (13.9 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

    
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
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
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
		<td class="tabnavtbl">
213
		<?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
		<td class="tabcont">
223

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

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

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

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

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

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

    
384
			<?php else: ?>
385

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

    
402
						// TODO : Need gray certificate icon
403

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

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

    
462
			<?php endif; ?>
463

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

    
471
method_change();
472

    
473
//-->
474
</script>
475

    
476
</body>
(171-171/211)