Project

General

Profile

Download (13.3 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 ($_POST) {
92

    
93
	unset($input_errors);
94
	$pconfig = $_POST;
95

    
96
	/* input validation */
97
	if ($pconfig['method'] == "existing") {
98
		$reqdfields = explode(" ", "name cert");
99
		$reqdfieldsn = explode(",", "Desriptive name,Certificate data");
100
	}
101
	if ($pconfig['method'] == "internal") {
102
		$reqdfields = explode(" ",
103
				"name keylen lifetime dn_country dn_state dn_city ".
104
				"dn_organization dn_email dn_commonname");
105
		$reqdfieldsn = explode(",",
106
				"Desriptive name,Key length,Lifetime,".
107
				"Distinguished name Country Code,".
108
				"Distinguished name State or Province,".
109
				"Distinguished name City,".
110
				"Distinguished name Organization,".
111
				"Distinguished name Email Address,".
112
				"Distinguished name Common Name");
113
	}
114

    
115
	do_input_validation($_POST, $reqdfields, $reqdfieldsn, &$input_errors);
116

    
117
	/* if this is an AJAX caller then handle via JSON */
118
	if (isAjax() && is_array($input_errors)) {
119
		input_errors2Ajax($input_errors);
120
		exit;
121
	}
122

    
123
	/* save modifications */
124
	if (!$input_errors) {
125

    
126
		$ca = array();
127
		$ca['refid'] = uniqid();
128
		if (isset($id) && $a_ca[$id])
129
			$ca = $a_ca[$id];
130

    
131
	    $ca['name'] = $pconfig['name'];
132

    
133
		if ($pconfig['method'] == "existing")
134
			ca_import($ca, $pconfig['cert']);
135

    
136
		if ($pconfig['method'] == "internal")
137
		{
138
			$dn = array(
139
				'countryName' => $pconfig['dn_country'],
140
				'stateOrProvinceName' => $pconfig['dn_state'],
141
				'localityName' => $pconfig['dn_city'],
142
				'organizationName' => $pconfig['dn_organization'],
143
				'emailAddress' => $pconfig['dn_email'],
144
				'commonName' => $pconfig['dn_commonname']);
145

    
146
			ca_create($ca, $pconfig['keylen'], $pconfig['lifetime'], $dn);
147
		}
148

    
149
		if (isset($id) && $a_ca[$id])
150
			$a_ca[$id] = $ca;
151
		else
152
			$a_ca[] = $ca;
153

    
154
		write_config();
155

    
156
//		pfSenseHeader("system_camanager.php");
157
	}
158
}
159

    
160
include("head.inc");
161
?>
162

    
163
<body link="#000000" vlink="#000000" alink="#000000" onload="<?= $jsevents["body"]["onload"] ?>">
164
<?php include("fbegin.inc"); ?>
165
<script type="text/javascript">
166
<!--
167

    
168
function method_change() {
169

    
170
	method = document.iform.method.selectedIndex;
171

    
172
	switch (method) {
173
		case 0:
174
			document.getElementById("existing").style.display="";
175
			document.getElementById("internal").style.display="none";
176
			break;
177
		case 1:
178
			document.getElementById("existing").style.display="none";
179
			document.getElementById("internal").style.display="";
180
			break;
181
	}
182
}
183

    
184
//-->
185
</script>
186
<?php
187
	if ($input_errors)
188
		print_input_errors($input_errors);
189
	if ($savemsg)
190
		print_info_box($savemsg);
191
?>
192
<table width="100%" border="0" cellpadding="0" cellspacing="0">
193
	<tr>
194
		<td class="tabnavtbl">
195
		<?php
196
			$tab_array = array();
197
			$tab_array[] = array(gettext("CAs"), true, "system_camanager.php");
198
			$tab_array[] = array(gettext("Certificates"), false, "system_certmanager.php");
199
			display_top_tabs($tab_array);
200
		?>
201
		</td>
202
	</tr>
203
	<tr>
204
		<td class="tabcont">
205

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

    
208
			<form action="system_camanager.php" method="post" name="iform" id="iform">
209
				<table width="100%" border="0" cellpadding="6" cellspacing="0">
210
					<tr>
211
						<td width="22%" valign="top" class="vncellreq"><?=gettext("Descriptive name");?></td>
212
						<td width="78%" class="vtable">
213
							<input name="name" type="text" class="formfld unknown" id="name" size="20" value="<?=htmlspecialchars($pconfig['name']);?>"/>
214
						</td>
215
					</tr>
216
					<?php if (!isset($id)): ?>
217
					<tr>
218
						<td width="22%" valign="top" class="vncellreq"><?=gettext("Method");?></td>
219
						<td width="78%" class="vtable">
220
							<select name='method' id='method' class="formselect" onchange='method_change()'>
221
							<?php
222
								foreach($ca_methods as $method => $desc):
223
								$selected = "";
224
								if ($pconfig['method'] == $method)
225
									$selected = "selected";
226
							?>
227
								<option value="<?=$method;?>"<?=$selected;?>><?=$desc;?></option>
228
							<?php endforeach; ?>
229
							</select>
230
						</td>
231
					</tr>
232
					<?php endif; ?>
233
				</table>
234

    
235
				<table width="100%" border="0" cellpadding="6" cellspacing="0" id="existing">
236
					<tr>
237
						<td colspan="2" class="list" height="12"></td>
238
					</tr>
239
					<tr>
240
						<td colspan="2" valign="top" class="listtopic">Existing Certificate Authority</td>
241
					</tr>
242

    
243
					<tr>
244
						<td width="22%" valign="top" class="vncellreq">Certificate data</td>
245
						<td width="78%" class="vtable">
246
							<textarea name="cert" id="cert" cols="65" rows="7" class="formfld_cert"><?=$pconfig['cert'];?></textarea>
247
							<br>
248
							Paste a certificate in X.509 PEM format here.</td>
249
						</td>
250
					</tr>
251
				</table>
252

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

    
353
				<table width="100%" border="0" cellpadding="6" cellspacing="0">
354
					<tr>
355
						<td width="22%" valign="top">&nbsp;</td>
356
						<td width="78%">
357
							<input id="submit" name="save" type="submit" class="formbtn" value="Save" />
358
							<?php if (isset($id) && $a_ca[$id]): ?>
359
							<input name="id" type="hidden" value="<?=$id;?>" />
360
							<?php endif;?>
361
						</td>
362
					</tr>
363
				</table>
364
			</form>
365

    
366
			<?php else: ?>
367

    
368
			<table width="100%" border="0" cellpadding="0" cellspacing="0">
369
				<tr>
370
					<td width="20%" class="listhdrr">Name</td>
371
					<td width="10%" class="listhdrr">Internal</td>
372
					<td width="10%" class="listhdrr">Certificates</td>
373
					<td width="40%" class="listhdrr">Distinguished Name</td>
374
					<td width="10%" class="list"></td>
375
				</tr>
376
				<?php
377
					$i = 0;
378
					foreach($a_ca as $ca):
379
						$name = htmlspecialchars($ca['name']);
380
						$subj = cert_get_subject($ca['crt']);
381
						$subj = htmlspecialchars($subj);
382
						$certcount = 0;
383

    
384
						// TODO : Need gray certificate icon
385

    
386
						if($ca['prv']) {
387
							$caimg = "/themes/{$g['theme']}/images/icons/icon_frmfld_cert.png";
388
							$internal = "YES";
389

    
390
							foreach ($a_cert as $cert)
391
								if ($cert['caref'] == $ca['refid'])
392
									$certcount++;
393
						} else {
394
							$caimg = "/themes/{$g['theme']}/images/icons/icon_frmfld_cert.png";
395
							$internal = "NO";
396
						}
397
				?>
398
				<tr>
399
					<td class="listlr">
400
						<table border="0" cellpadding="0" cellspacing="0">
401
							<tr>
402
								<td align="left" valign="center">
403
									<img src="<?=$caimg;?>" alt="CA" title="CA" border="0" height="16" width="16" />
404
								</td>
405
								<td align="left" valign="middle">
406
									<?=$name;?>
407
								</td>
408
							</tr>
409
						</table>
410
					</td>
411
					<td class="listr"><?=$internal;?>&nbsp;</td>
412
					<td class="listr"><?=$certcount;?>&nbsp;</td>
413
					<td class="listr"><?=$subj;?>&nbsp;</td>
414
					<td valign="middle" nowrap class="list">
415
						<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?");?>')">
416
							<img src="/themes/<?= $g['theme'];?>/images/icons/icon_x.gif" title="delete ca" alt="delete ca" width="17" height="17" border="0" />
417
						</a>
418
					</td>
419
				</tr>
420
				<?php
421
						$i++;
422
					endforeach;
423
				?>
424
				<tr>
425
					<td class="list" colspan="4"></td>
426
					<td class="list">
427
						<a href="system_camanager.php?act=new">
428
							<img src="/themes/<?= $g['theme'];?>/images/icons/icon_plus.gif" title="add or import ca" alt="add ca" width="17" height="17" border="0" />
429
						</a>
430
					</td>
431
				</tr>
432
				<tr>
433
					<td colspan="4">
434
						<p>
435
							<?=gettext("Additional trusted certificate authorities can be added here.");?>
436
						</p>
437
					</td>
438
				</tr>
439
			</table>
440

    
441
			<?php endif; ?>
442

    
443
		</td>
444
	</tr>
445
</table>
446
<?php include("fend.inc");?>
447
<script type="text/javascript">
448
<!--
449

    
450
method_change();
451

    
452
//-->
453
</script>
454

    
455
</body>
(170-170/210)