Project

General

Profile

Download (14.8 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" => "Import an existing Certificate Authority",
45
	"internal" => "Create an internal Certificate Authority");
46

    
47
$ca_keylens = array( "512", "1024", "2048", "4096");
48

    
49
$pgtitle = array("System", "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 ($_POST) {
114

    
115
	unset($input_errors);
116
	$pconfig = $_POST;
117

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

    
137
	do_input_validation($_POST, $reqdfields, $reqdfieldsn, &$input_errors);
138

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

    
145
	/* save modifications */
146
	if (!$input_errors) {
147

    
148
		$ca = array();
149
		$ca['refid'] = uniqid();
150
		if (isset($id) && $a_ca[$id])
151
			$ca = $a_ca[$id];
152

    
153
	    $ca['name'] = $pconfig['name'];
154

    
155
		if ($pconfig['method'] == "existing")
156
			ca_import($ca, $pconfig['cert']);
157

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

    
168
			ca_create($ca, $pconfig['keylen'], $pconfig['lifetime'], $dn);
169
		}
170

    
171
		if (isset($id) && $a_ca[$id])
172
			$a_ca[$id] = $ca;
173
		else
174
			$a_ca[] = $ca;
175

    
176
		write_config();
177

    
178
//		pfSenseHeader("system_camanager.php");
179
	}
180
}
181

    
182
include("head.inc");
183
?>
184

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

    
190
function method_change() {
191

    
192
	method = document.iform.method.selectedIndex;
193

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

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

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

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

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

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

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

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

    
389
				<?php else: ?>
390

    
391
				<table width="100%" border="0" cellpadding="0" cellspacing="0">
392
					<tr>
393
						<td width="20%" class="listhdrr">Name</td>
394
						<td width="10%" class="listhdrr">Internal</td>
395
						<td width="10%" class="listhdrr">Issuer</td>
396
						<td width="10%" class="listhdrr">Certificates</td>
397
						<td width="40%" class="listhdrr">Distinguished Name</td>
398
						<td width="10%" class="list"></td>
399
					</tr>
400
					<?php
401
						$i = 0;
402
						foreach($a_ca as $ca):
403
							$name = htmlspecialchars($ca['name']);
404
							$subj = cert_get_subject($ca['crt']);
405
							$issuer = cert_get_issuer($ca['crt']);
406
							if($subj == $issuer)
407
							  $issuer_name = "<em>self-signed</em>";
408
							else
409
							  $issuer_name = "<em>external</em>";
410
							$subj = htmlspecialchars($subj);
411
							$issuer = htmlspecialchars($issuer);
412
							$certcount = 0;
413

    
414
							$issuer_ca = lookup_ca($ca['caref']);
415
							if ($issuer_ca)
416
								$issuer_name = $issuer_ca['name'];
417

    
418
							// TODO : Need gray certificate icon
419

    
420
							if($ca['prv']) {
421
								$caimg = "/themes/{$g['theme']}/images/icons/icon_frmfld_cert.png";
422
								$internal = "YES";
423

    
424
							} else {
425
								$caimg = "/themes/{$g['theme']}/images/icons/icon_frmfld_cert.png";
426
								$internal = "NO";
427
							}
428
							foreach ($a_cert as $cert)
429
								if ($cert['caref'] == $ca['refid'])
430
									$certcount++;
431
  						foreach ($a_ca as $cert)
432
  							if ($cert['caref'] == $ca['refid'])
433
  								$certcount++;
434
					?>
435
					<tr>
436
						<td class="listlr">
437
							<table border="0" cellpadding="0" cellspacing="0">
438
								<tr>
439
									<td align="left" valign="center">
440
										<img src="<?=$caimg;?>" alt="CA" title="CA" border="0" height="16" width="16" />
441
									</td>
442
									<td align="left" valign="middle">
443
										<?=$name;?>
444
									</td>
445
								</tr>
446
							</table>
447
						</td>
448
						<td class="listr"><?=$internal;?>&nbsp;</td>
449
						<td class="listr"><?=$issuer_name;?>&nbsp;</td>
450
						<td class="listr"><?=$certcount;?>&nbsp;</td>
451
						<td class="listr"><?=$subj;?>&nbsp;</td>
452
						<td valign="middle" nowrap class="list">
453
							<a href="system_camanager.php?act=exp&id=<?=$i;?>")">
454
								<img src="/themes/<?= $g['theme'];?>/images/icons/icon_down.gif" title="export ca" alt="export ca" width="17" height="17" border="0" />
455
							</a>
456
							<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?");?>')">
457
								<img src="/themes/<?= $g['theme'];?>/images/icons/icon_x.gif" title="delete ca" alt="delete ca" width="17" height="17" border="0" />
458
							</a>
459
						</td>
460
					</tr>
461
					<?php
462
							$i++;
463
						endforeach;
464
					?>
465
					<tr>
466
						<td class="list" colspan="5"></td>
467
						<td class="list">
468
							<a href="system_camanager.php?act=new">
469
								<img src="/themes/<?= $g['theme'];?>/images/icons/icon_plus.gif" title="add or import ca" alt="add ca" width="17" height="17" border="0" />
470
							</a>
471
						</td>
472
					</tr>
473
					<tr>
474
						<td colspan="5">
475
							<p>
476
								<?=gettext("Additional trusted certificate authorities can be added here.");?>
477
							</p>
478
						</td>
479
					</tr>
480
				</table>
481

    
482
				<?php endif; ?>
483

    
484
			</div>
485
		</td>
486
	</tr>
487
</table>
488
<?php include("fend.inc");?>
489
<script type="text/javascript">
490
<!--
491

    
492
method_change();
493

    
494
//-->
495
</script>
496

    
497
</body>
(177-177/218)