Project

General

Profile

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

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

    
49
$pgtitle = array(gettext("System"), gettext("Certificate Authority Manager"));
50

    
51
$id = $_GET['id'];
52
if (isset($_POST['id']))
53
	$id = $_POST['id'];
54

    
55
if (!is_array($config['ca']))
56
	$config['ca'] = array();
57

    
58
$a_ca =& $config['ca'];
59

    
60
if (!is_array($config['cert']))
61
	$config['cert'] = array();
62

    
63
$a_cert =& $config['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]['descr'];
82
	unset($a_ca[$id]);
83
	write_config();
84
	$savemsg = sprintf(gettext("Certificate Authority %s successfully deleted"), $name) . "<br/>";
85
}
86

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

    
94
if ($act == "exp") {
95

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

    
101
	$exp_name = urlencode("{$a_ca[$id]['descr']}.crt");
102
	$exp_data = base64_decode($a_ca[$id]['crt']);
103
	$exp_size = strlen($exp_data);
104

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

    
112
if ($act == "expkey") {
113

    
114
	if (!$a_ca[$id]) {
115
		pfSenseHeader("system_camanager.php");
116
		exit;
117
	}
118

    
119
	$exp_name = urlencode("{$a_ca[$id]['descr']}.key");
120
	$exp_data = base64_decode($a_ca[$id]['prv']);
121
	$exp_size = strlen($exp_data);
122

    
123
	header("Content-Type: application/octet-stream");
124
	header("Content-Disposition: attachment; filename={$exp_name}");
125
	header("Content-Length: $exp_size");
126
	echo $exp_data;
127
	exit;
128
}
129

    
130
if ($_POST) {
131

    
132
	unset($input_errors);
133
	$pconfig = $_POST;
134

    
135
	/* input validation */
136
	if ($pconfig['method'] == "existing") {
137
		$reqdfields = explode(" ", "descr cert");
138
		$reqdfieldsn = array(
139
				gettext("Descriptive name"),
140
				gettext("Certificate data"));
141
	}
142
	if ($pconfig['method'] == "internal") {
143
		$reqdfields = explode(" ",
144
				"descr keylen lifetime dn_country dn_state dn_city ".
145
				"dn_organization dn_email dn_commonname");
146
		$reqdfieldsn = array(
147
				gettext("Descriptive name"),
148
				gettext("Key length"),
149
				gettext("Lifetime"),
150
				gettext("Distinguished name Country Code"),
151
				gettext("Distinguished name State or Province"),
152
				gettext("Distinguished name City"),
153
				gettext("Distinguished name Organization"),
154
				gettext("Distinguished name Email Address"),
155
				gettext("Distinguished name Common Name"));
156
	}
157

    
158
	do_input_validation($_POST, $reqdfields, $reqdfieldsn, &$input_errors);
159

    
160
	/* if this is an AJAX caller then handle via JSON */
161
	if (isAjax() && is_array($input_errors)) {
162
		input_errors2Ajax($input_errors);
163
		exit;
164
	}
165

    
166
	/* save modifications */
167
	if (!$input_errors) {
168

    
169
		$ca = array();
170
		$ca['refid'] = uniqid();
171
		if (isset($id) && $a_ca[$id])
172
			$ca = $a_ca[$id];
173

    
174
	    $ca['descr'] = $pconfig['descr'];
175

    
176
		if ($pconfig['method'] == "existing")
177
			ca_import($ca, $pconfig['cert'], $pconfig['key']);
178

    
179
		if ($pconfig['method'] == "internal")
180
		{
181
			$dn = array(
182
				'countryName' => $pconfig['dn_country'],
183
				'stateOrProvinceName' => $pconfig['dn_state'],
184
				'localityName' => $pconfig['dn_city'],
185
				'organizationName' => $pconfig['dn_organization'],
186
				'emailAddress' => $pconfig['dn_email'],
187
				'commonName' => $pconfig['dn_commonname']);
188

    
189
			ca_create($ca, $pconfig['keylen'], $pconfig['lifetime'], $dn);
190
		}
191

    
192
		if (isset($id) && $a_ca[$id])
193
			$a_ca[$id] = $ca;
194
		else
195
			$a_ca[] = $ca;
196

    
197
		write_config();
198

    
199
//		pfSenseHeader("system_camanager.php");
200
	}
201
}
202

    
203
include("head.inc");
204
?>
205

    
206
<body link="#000000" vlink="#000000" alink="#000000" onload="<?= $jsevents["body"]["onload"] ?>">
207
<?php include("fbegin.inc"); ?>
208
<script type="text/javascript">
209
<!--
210

    
211
function method_change() {
212

    
213
	method = document.iform.method.selectedIndex;
214

    
215
	switch (method) {
216
		case 0:
217
			document.getElementById("existing").style.display="";
218
			document.getElementById("internal").style.display="none";
219
			break;
220
		case 1:
221
			document.getElementById("existing").style.display="none";
222
			document.getElementById("internal").style.display="";
223
			break;
224
	}
225
}
226

    
227
//-->
228
</script>
229
<?php
230
	if ($input_errors)
231
		print_input_errors($input_errors);
232
	if ($savemsg)
233
		print_info_box($savemsg);
234
?>
235
<table width="100%" border="0" cellpadding="0" cellspacing="0">
236
	<tr>
237
		<td>
238
		<?php
239
			$tab_array = array();
240
			$tab_array[] = array(gettext("CAs"), true, "system_camanager.php");
241
			$tab_array[] = array(gettext("Certificates"), false, "system_certmanager.php");
242
			$tab_array[] = array(gettext("Certificate Revocation"), false, "system_crlmanager.php");
243
			display_top_tabs($tab_array);
244
		?>
245
		</td>
246
	</tr>
247
	<tr>
248
		<td id="mainarea">
249
			<div class="tabcont">
250

    
251
				<?php if ($act == "new" || $act == gettext("Save") || $input_errors): ?>
252

    
253
				<form action="system_camanager.php" method="post" name="iform" id="iform">
254
					<table width="100%" border="0" cellpadding="6" cellspacing="0">
255
						<tr>
256
							<td width="22%" valign="top" class="vncellreq"><?=gettext("Descriptive name");?></td>
257
							<td width="78%" class="vtable">
258
								<input name="descr" type="text" class="formfld unknown" id="descr" size="20" value="<?=htmlspecialchars($pconfig['descr']);?>"/>
259
							</td>
260
						</tr>
261
						<?php if (!isset($id)): ?>
262
						<tr>
263
							<td width="22%" valign="top" class="vncellreq"><?=gettext("Method");?></td>
264
							<td width="78%" class="vtable">
265
								<select name='method' id='method' class="formselect" onchange='method_change()'>
266
								<?php
267
									foreach($ca_methods as $method => $desc):
268
									$selected = "";
269
									if ($pconfig['method'] == $method)
270
										$selected = "selected";
271
								?>
272
									<option value="<?=$method;?>"<?=$selected;?>><?=$desc;?></option>
273
								<?php endforeach; ?>
274
								</select>
275
							</td>
276
						</tr>
277
						<?php endif; ?>
278
					</table>
279

    
280
					<table width="100%" border="0" cellpadding="6" cellspacing="0" id="existing">
281
						<tr>
282
							<td colspan="2" class="list" height="12"></td>
283
						</tr>
284
						<tr>
285
							<td colspan="2" valign="top" class="listtopic"><?=gettext("Existing Certificate Authority");?></td>
286
						</tr>
287

    
288
						<tr>
289
							<td width="22%" valign="top" class="vncellreq"><?=gettext("Certificate data");?></td>
290
							<td width="78%" class="vtable">
291
								<textarea name="cert" id="cert" cols="65" rows="7" class="formfld_cert"><?=htmlspecialchars($pconfig['cert']);?></textarea>
292
								<br>
293
								<?=gettext("Paste a certificate in X.509 PEM format here.");?></td>
294
							</td>
295
						</tr>
296
						<tr>
297
							<td width="22%" valign="top" class="vncellreq"><?=gettext("Certificate Private Key");?><br/><?=gettext("(optional)");?></td>
298
							<td width="78%" class="vtable">
299
								<textarea name="key" id="key" cols="65" rows="7" class="formfld_cert"><?=htmlspecialchars($pconfig['key']);?></textarea>
300
								<br>
301
								<?=gettext("Paste the private key for the above certificate here. This is optional in most cases, but required if you need to generate a Certificate Revocation List (CRL).");?></td>
302
							</td>
303
						</tr>
304
					</table>
305

    
306
					<table width="100%" border="0" cellpadding="6" cellspacing="0" id="internal">
307
						<tr>
308
							<td colspan="2" class="list" height="12"></td>
309
						</tr>
310
						<tr>
311
							<td colspan="2" valign="top" class="listtopic"><?=gettext("Internal Certificate Authority");?></td>
312
						</tr>
313
						<tr>
314
							<td width="22%" valign="top" class="vncellreq"><?=gettext("Key length");?></td>
315
							<td width="78%" class="vtable">
316
								<select name='keylen' id='keylen' class="formselect">
317
								<?php
318
									foreach( $ca_keylens as $len):
319
									$selected = "";
320
									if ($pconfig['keylen'] == $len)
321
										$selected = "selected";
322
								?>
323
									<option value="<?=$len;?>"<?=$selected;?>><?=$len;?></option>
324
								<?php endforeach; ?>
325
								</select>
326
								<?=gettext("bits");?>
327
							</td>
328
						</tr>
329
						<tr>
330
							<td width="22%" valign="top" class="vncellreq"><?=gettext("Lifetime");?></td>
331
							<td width="78%" class="vtable">
332
								<input name="lifetime" type="text" class="formfld unknown" id="lifetime" size="5" value="<?=htmlspecialchars($pconfig['lifetime']);?>"/>
333
								<?=gettext("days");?>
334
							</td>
335
						</tr>
336
						<tr>
337
							<td width="22%" valign="top" class="vncellreq"><?=gettext("Distinguished name");?></td>
338
							<td width="78%" class="vtable">
339
								<table border="0" cellspacing="0" cellpadding="2">
340
									<tr>
341
										<td align="right"><?=gettext("Country Code");?> : &nbsp;</td>
342
										<td align="left">
343
											<input name="dn_country" type="text" class="formfld unknown" maxlength="2" size="2" value="<?=htmlspecialchars($pconfig['dn_country']);?>"/>
344
											&nbsp;
345
											<em><?=gettext("ex:");?></em>
346
											&nbsp;
347
											<?=gettext("US");?>
348
											<em><?=gettext("( two letters )");?></em>
349
										</td>
350
									</tr>
351
									<tr>
352
										<td align="right"><?=gettext("State or Province");?> : &nbsp;</td>
353
										<td align="left">
354
											<input name="dn_state" type="text" class="formfld unknown" size="40" value="<?=htmlspecialchars($pconfig['dn_state']);?>"/>
355
											&nbsp;
356
											<em><?=gettext("ex:");?></em>
357
											&nbsp;
358
											<?=gettext("Texas");?>
359
										</td>
360
									</tr>
361
									<tr>
362
										<td align="right"><?=gettext("City");?> : &nbsp;</td>
363
										<td align="left">
364
											<input name="dn_city" type="text" class="formfld unknown" size="40" value="<?=htmlspecialchars($pconfig['dn_city']);?>"/>
365
											&nbsp;
366
											<em><?=gettext("ex:");?></em>
367
											&nbsp;
368
											<?=gettext("Austin");?>
369
										</td>
370
									</tr>
371
									<tr>
372
										<td align="right"><?=gettext("Organization");?> : &nbsp;</td>
373
										<td align="left">
374
											<input name="dn_organization" type="text" class="formfld unknown" size="40" value="<?=htmlspecialchars($pconfig['dn_organization']);?>"/>
375
											&nbsp;
376
											<em><?=gettext("ex:");?></em>
377
											&nbsp;
378
											<?=gettext("My Company Inc.");?>
379
										</td>
380
									</tr>
381
									<tr>
382
										<td align="right"><?=gettext("Email Address");?> : &nbsp;</td>
383
										<td align="left">
384
											<input name="dn_email" type="text" class="formfld unknown" size="25" value="<?=htmlspecialchars($pconfig['dn_email']);?>"/>
385
											&nbsp;
386
											<em><?=gettext("ex:");?></em>
387
											&nbsp;
388
											<?=gettext("admin@mycompany.com");?>
389
										</td>
390
									</tr>
391
									<tr>
392
										<td align="right"><?=gettext("Common Name");?> : &nbsp;</td>
393
										<td align="left">
394
											<input name="dn_commonname" type="text" class="formfld unknown" size="25" value="<?=htmlspecialchars($pconfig['dn_commonname']);?>"/>
395
											&nbsp;
396
											<em><?=gettext("ex:");?></em>
397
											&nbsp;
398
											<?=gettext("internal-ca");?>
399
										</td>
400
									</tr>
401
								</table>
402
							</td>
403
						</tr>
404
					</table>
405

    
406
					<table width="100%" border="0" cellpadding="6" cellspacing="0">
407
						<tr>
408
							<td width="22%" valign="top">&nbsp;</td>
409
							<td width="78%">
410
								<input id="submit" name="save" type="submit" class="formbtn" value="<?=gettext("Save"); ?>" />
411
								<?php if (isset($id) && $a_ca[$id]): ?>
412
								<input name="id" type="hidden" value="<?=$id;?>" />
413
								<?php endif;?>
414
							</td>
415
						</tr>
416
					</table>
417
				</form>
418

    
419
				<?php else: ?>
420

    
421
				<table width="100%" border="0" cellpadding="0" cellspacing="0">
422
					<tr>
423
						<td width="20%" class="listhdrr"><?=gettext("Name");?></td>
424
						<td width="10%" class="listhdrr"><?=gettext("Internal");?></td>
425
						<td width="10%" class="listhdrr"><?=gettext("Issuer");?></td>
426
						<td width="10%" class="listhdrr"><?=gettext("Certificates");?></td>
427
						<td width="40%" class="listhdrr"><?=gettext("Distinguished Name");?></td>
428
						<td width="10%" class="list"></td>
429
					</tr>
430
					<?php
431
						$i = 0;
432
						foreach($a_ca as $ca):
433
							$name = htmlspecialchars($ca['descr']);
434
							$subj = cert_get_subject($ca['crt']);
435
							$issuer = cert_get_issuer($ca['crt']);
436
							if($subj == $issuer)
437
							  $issuer_name = "<em>" . gettext("self-signed") . "</em>";
438
							else
439
							  $issuer_name = "<em>" . gettext("external") . "</em>";
440
							$subj = htmlspecialchars($subj);
441
							$issuer = htmlspecialchars($issuer);
442
							$certcount = 0;
443

    
444
							$issuer_ca = lookup_ca($ca['caref']);
445
							if ($issuer_ca)
446
								$issuer_name = $issuer_ca['descr'];
447

    
448
							// TODO : Need gray certificate icon
449

    
450
							if($ca['prv']) {
451
								$caimg = "/themes/{$g['theme']}/images/icons/icon_frmfld_cert.png";
452
								$internal = "YES";
453

    
454
							} else {
455
								$caimg = "/themes/{$g['theme']}/images/icons/icon_frmfld_cert.png";
456
								$internal = "NO";
457
							}
458
							foreach ($a_cert as $cert)
459
								if ($cert['caref'] == $ca['refid'])
460
									$certcount++;
461
  						foreach ($a_ca as $cert)
462
  							if ($cert['caref'] == $ca['refid'])
463
  								$certcount++;
464
					?>
465
					<tr>
466
						<td class="listlr">
467
							<table border="0" cellpadding="0" cellspacing="0">
468
								<tr>
469
									<td align="left" valign="center">
470
										<img src="<?=$caimg;?>" alt="CA" title="CA" border="0" height="16" width="16" />
471
									</td>
472
									<td align="left" valign="middle">
473
										<?=$name;?>
474
									</td>
475
								</tr>
476
							</table>
477
						</td>
478
						<td class="listr"><?=$internal;?>&nbsp;</td>
479
						<td class="listr"><?=$issuer_name;?>&nbsp;</td>
480
						<td class="listr"><?=$certcount;?>&nbsp;</td>
481
						<td class="listr"><?=$subj;?>&nbsp;</td>
482
						<td valign="middle" nowrap class="list">
483
							<a href="system_camanager.php?act=exp&id=<?=$i;?>")">
484
								<img src="/themes/<?= $g['theme'];?>/images/icons/icon_down.gif" title="<?=gettext("export ca");?>" alt="<?=gettext("export ca");?>" width="17" height="17" border="0" />
485
							</a>
486
							<?php if ($ca['prv']): ?>
487
							<a href="system_camanager.php?act=expkey&id=<?=$i;?>")">
488
								<img src="/themes/<?= $g['theme'];?>/images/icons/icon_down.gif" title="<?=gettext("export ca private key");?>" alt="<?=gettext("export ca private key");?>" width="17" height="17" border="0" />
489
							</a>
490
							<?php endif; ?>
491
							<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?");?>')">
492
								<img src="/themes/<?= $g['theme'];?>/images/icons/icon_x.gif" title="<?=gettext("delete ca");?>" alt="<?=gettext("delete ca"); ?>" width="17" height="17" border="0" />
493
							</a>
494
						</td>
495
					</tr>
496
					<?php
497
							$i++;
498
						endforeach;
499
					?>
500
					<tr>
501
						<td class="list" colspan="5"></td>
502
						<td class="list">
503
							<a href="system_camanager.php?act=new">
504
								<img src="/themes/<?= $g['theme'];?>/images/icons/icon_plus.gif" title="<?=gettext("add or import ca");?>" alt="<?=gettext("add ca");?>" width="17" height="17" border="0" />
505
							</a>
506
						</td>
507
					</tr>
508
					<tr>
509
						<td colspan="5">
510
							<p>
511
								<?=gettext("Additional trusted Certificate Authorities can be added here.");?>
512
							</p>
513
						</td>
514
					</tr>
515
				</table>
516

    
517
				<?php endif; ?>
518

    
519
			</div>
520
		</td>
521
	</tr>
522
</table>
523
<?php include("fend.inc");?>
524
<script type="text/javascript">
525
<!--
526

    
527
method_change();
528

    
529
//-->
530
</script>
531

    
532
</body>
(181-181/222)