Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added PKCS#11 support. #198

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion security/pfSense-pkg-openvpn-client-export/Makefile
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# $FreeBSD$

PORTNAME= pfSense-pkg-openvpn-client-export
PORTVERSION= 1.3.11
PORTVERSION= 1.3.12
CATEGORIES= security
MASTER_SITES= # empty
DISTFILES= # empty
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ function openvpn_client_export_validate_config($srvid, $usrid, $crtid) {
return array($settings, $server_cert, $server_ca, $servercn, $user, $cert, $nokeys);
}

function openvpn_client_export_config($srvid, $usrid, $crtid, $useaddr, $verifyservercn, $blockoutsidedns, $randomlocalport, $usetoken, $nokeys = false, $proxy, $expformat = "baseconf", $outpass = "", $skiptls = false, $doslines = false, $openvpnmanager, $advancedoptions = "") {
function openvpn_client_export_config($srvid, $usrid, $crtid, $useaddr, $verifyservercn, $blockoutsidedns, $randomlocalport, $usetoken, $nokeys = false, $proxy, $expformat = "baseconf", $outpass = "", $skiptls = false, $doslines = false, $openvpnmanager, $advancedoptions = "", $usepkcs11, $pkcs11providers, $pkcs11id) {
global $config, $input_errors, $g;

$nl = ($doslines) ? "\r\n" : "\n";
Expand Down Expand Up @@ -279,6 +279,10 @@ EOF;
$conf .= "ca /openvpn/ca.crt{$nl}";
$conf .= "cert /openvpn/phone1.crt{$nl}";
$conf .= "key /openvpn/phone1.key{$nl}";
} elseif ($usepkcs11) {
$conf .= "ca {$cafile}{$nl}";
$conf .= "pkcs11-providers '{$pkcs11providers}'{$nl}";
$conf .= "pkcs11-id '{$pkcs11id}'{$nl}";
} elseif ($usetoken) {
$conf .= "ca {$cafile}{$nl}";
$conf .= "cryptoapicert \"SUBJ:{$user['name']}\"{$nl}";
Expand Down Expand Up @@ -373,7 +377,7 @@ EOF;
}

// write key files
if ($settings['mode'] != "server_user") {
if ($settings['mode'] != "server_user" && !$usepkcs11) {
$crtfile = "{$tempdir}/{$prefix}-cert.crt";
file_put_contents($crtfile, base64_decode($cert['crt']));
$keyfile = "{$tempdir}/{$prefix}.key";
Expand Down Expand Up @@ -478,7 +482,7 @@ EOF;
}
}

function openvpn_client_export_installer($srvid, $usrid, $crtid, $useaddr, $verifyservercn, $blockoutsidedns, $randomlocalport, $usetoken, $outpass, $proxy, $openvpnmanager, $advancedoptions, $openvpn_version = "x86-xp") {
function openvpn_client_export_installer($srvid, $usrid, $crtid, $useaddr, $verifyservercn, $blockoutsidedns, $randomlocalport, $usetoken, $outpass, $proxy, $openvpnmanager, $advancedoptions, $openvpn_version = "x86-xp", $usepkcs11, $pkcs11providers, $pkcs11id) {
global $config, $g, $input_errors, $current_openvpn_version, $current_openvpn_version_rev;
$uname_p = trim(exec("uname -p"));

Expand Down Expand Up @@ -536,7 +540,7 @@ function openvpn_client_export_installer($srvid, $usrid, $crtid, $useaddr, $veri
$pwdfle .= "{$proxy['password']}\r\n";
file_put_contents("{$confdir}/{$proxy['passwdfile']}", $pwdfle);
}
$conf = openvpn_client_export_config($srvid, $usrid, $crtid, $useaddr, $verifyservercn, $blockoutsidedns, $randomlocalport, $usetoken, $nokeys, $proxy, "", "baseconf", false, true, $openvpnmanager, $advancedoptions);
$conf = openvpn_client_export_config($srvid, $usrid, $crtid, $useaddr, $verifyservercn, $blockoutsidedns, $randomlocalport, $usetoken, $nokeys, $proxy, "", "baseconf", false, true, $openvpnmanager, $advancedoptions, $usepkcs11, $pkcs11providers, $pkcs11id);
if (!$conf) {
$input_errors[] = "Could not create a config to export.";
return false;
Expand All @@ -552,7 +556,7 @@ function openvpn_client_export_installer($srvid, $usrid, $crtid, $useaddr, $veri
}

// write key files
if ($settings['mode'] != "server_user") {
if ($settings['mode'] != "server_user" && !$usepkcs11) {
$crtfile = "{$tempdir}/config/{$prefix}-{$user['name']}.crt";
file_put_contents($crtfile, base64_decode($cert['crt']));
$keyfile = "{$tempdir}/config/{$prefix}-{$user['name']}.key";
Expand Down Expand Up @@ -604,7 +608,7 @@ RunProgram="openvpn-postinstall.exe"
return $outfile;
}

function viscosity_openvpn_client_config_exporter($srvid, $usrid, $crtid, $useaddr, $verifyservercn, $blockoutsidedns, $randomlocalport, $usetoken, $outpass, $proxy, $openvpnmanager, $advancedoptions) {
function viscosity_openvpn_client_config_exporter($srvid, $usrid, $crtid, $useaddr, $verifyservercn, $blockoutsidedns, $randomlocalport, $usetoken, $outpass, $proxy, $openvpnmanager, $advancedoptions, $usepkcs11, $pkcs11providers, $pkcs11id) {
global $config, $g;
$uname_p = trim(exec("uname -p"));

Expand Down Expand Up @@ -636,7 +640,7 @@ function viscosity_openvpn_client_config_exporter($srvid, $usrid, $crtid, $usead
file_put_contents("{$tempdir}/{$proxy['passwdfile']}", $pwdfle);
}

$conf = openvpn_client_export_config($srvid, $usrid, $crtid, $useaddr, $verifyservercn, $blockoutsidedns, $randomlocalport, $usetoken, true, $proxy, "baseconf", $outpass, true, true, $openvpnmanager, $advancedoptions);
$conf = openvpn_client_export_config($srvid, $usrid, $crtid, $useaddr, $verifyservercn, $blockoutsidedns, $randomlocalport, $usetoken, true, $proxy, "baseconf", $outpass, true, true, $openvpnmanager, $advancedoptions, $usepkcs11, $pkcs11providers, $pkcs11id);
if (!$conf) {
return false;
}
Expand Down Expand Up @@ -679,7 +683,7 @@ EOF;
$cafile = "{$tempdir}/ca.crt";
file_put_contents($cafile, $server_ca);

if ($settings['mode'] != "server_user") {
if ($settings['mode'] != "server_user" && !$usepkcs11) {

// write user .crt
$crtfile = "{$tempdir}/cert.crt";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,51 @@
$ras_server[] = $ras_serverent;
}

$a_config =& $config['installedpackages']['openvpnclientexport'];
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This code is looking for stored settings, the page to store the settings was removed but not this code.

$hashes = array_map(function($s){return('server_'.md5($s['name']));},$ras_server);
foreach ($a_config as $shash => $cfg) {
$any_change = false;
if (!in_array($shash, $hashes)) {
unset($a_config[$shash]);
$any_change = true;
}
if ($any_change) {
write_config("Settings cleaned up");
}
}
$persistent_config = array(
'useaddr' => array('type' => 'select', 'default' => null),
'useaddr_hostname' => array('type' => 'input', 'default' => null),
'verifyservercn' => array('type' => 'select', 'default' => null),
'blockoutsidedns' => array('type' => 'checkbox', 'default' => true),
'randomlocalport' => array('type' => 'checkbox', 'default' => true),
'usepkcs11' => array('type' => 'checkbox', 'default' => false),
'pkcs11providers' => array('type' => 'input', 'default' => null),
'usetoken' => array('type' => 'checkbox', 'default' => false),
'usepass' => array('type' => 'checkbox', 'default' => false),
'useproxy' => array('type' => 'checkbox', 'default' => false),
'useproxytype' => array('type' => 'select', 'default' => null),
'proxyaddr' => array('type' => 'input', 'default' => null),
'proxyport' => array('type' => 'input', 'default' => null),
'useproxypass' => array('type' => 'input', 'default' => null),
'proxyuser' => array('type' => 'input', 'default' => null),
'proxypass' => array('type' => 'input', 'default' => null),
'proxyconf' => array('type' => 'input', 'default' => null),
'openvpnmanager' => array('type' => 'checkbox', 'default' => false),
'advancedoptions' => array('type' => 'input', 'default' => null),
);
if (isset($_POST['save_as_default'])) {
$vpnidx = $_POST['vpnidx'];
$sname = $ras_server[$vpnidx]['name'];
$scfg = 'server_'.md5($sname);
unset($a_config[$scfg]);
foreach (array_keys($persistent_config) as $ncfg) {
$a_config[$scfg][$ncfg] = str_replace("\r\n","\\n",$_POST[$ncfg]);
}
$savemsg = sprintf(gettext("Settings saved as default for %s."), $sname);
write_config($savemsg);
}

$id = $_GET['id'];
if (isset($_POST['id'])) {
$id = $_POST['id'];
Expand Down Expand Up @@ -161,6 +206,15 @@
if ($usetoken && (($act == "conf_yealink_t28") || ($act == "conf_yealink_t38g") || ($act == "conf_yealink_t38g2") || ($act == "conf_snom"))) {
$input_errors[] = "Microsoft Certificate Storage cannot be used with a Yealink or SNOM configuration.";
}
$usepkcs11 = $_GET['usepkcs11'];
$pkcs11providers = $_GET['pkcs11providers'];
if ($usepkcs11 && !$pkcs11providers) {
$input_errors[] = "You must provide the PKCS#11 providers.";
}
$pkcs11id = $_GET['pkcs11id'];
if ($usepkcs11 && !$pkcs11id) {
$input_errors[] = "You must provide the PKCS#11 ID.";
}
$password = "";
if ($_GET['password']) {
$password = $_GET['password'];
Expand Down Expand Up @@ -239,17 +293,17 @@
$exp_name = urlencode($exp_name . "-config.ovpn");
$expformat = "baseconf";
}
$exp_path = openvpn_client_export_config($srvid, $usrid, $crtid, $useaddr, $verifyservercn, $blockoutsidedns, $randomlocalport, $usetoken, $nokeys, $proxy, $expformat, $password, false, false, $openvpnmanager, $advancedoptions);
$exp_path = openvpn_client_export_config($srvid, $usrid, $crtid, $useaddr, $verifyservercn, $blockoutsidedns, $randomlocalport, $usetoken, $nokeys, $proxy, $expformat, $password, false, false, $openvpnmanager, $advancedoptions, $usepkcs11, $pkcs11providers, $pkcs11id);
}

if ($act == "visc") {
$exp_name = urlencode($exp_name . "-Viscosity.visc.zip");
$exp_path = viscosity_openvpn_client_config_exporter($srvid, $usrid, $crtid, $useaddr, $verifyservercn, $blockoutsidedns, $randomlocalport, $usetoken, $password, $proxy, $openvpnmanager, $advancedoptions);
$exp_path = viscosity_openvpn_client_config_exporter($srvid, $usrid, $crtid, $useaddr, $verifyservercn, $blockoutsidedns, $randomlocalport, $usetoken, $password, $proxy, $openvpnmanager, $advancedoptions, $usepkcs11, $pkcs11providers, $pkcs11id);
}

if (substr($act, 0, 4) == "inst") {
$exp_name = urlencode($exp_name."-install.exe");
$exp_path = openvpn_client_export_installer($srvid, $usrid, $crtid, $useaddr, $verifyservercn, $blockoutsidedns, $randomlocalport, $usetoken, $password, $proxy, $openvpnmanager, $advancedoptions, substr($act, 5));
$exp_path = openvpn_client_export_installer($srvid, $usrid, $crtid, $useaddr, $verifyservercn, $blockoutsidedns, $randomlocalport, $usetoken, $password, $proxy, $openvpnmanager, $advancedoptions, substr($act, 5), $usepkcs11, $pkcs11providers, $pkcs11id);
}

if (!$exp_path) {
Expand Down Expand Up @@ -381,6 +435,23 @@

$section = new Form_Section('Certificate Export Options');

$section->addInput(new Form_Checkbox(
'usepkcs11',
'PKCS#11 Certificate Storage',
'Use PKCS#11 storage instead of local files.'
));

$section->addInput(new Form_Input(
'pkcs11providers',
'PKCS#11 Providers',
'Enter the path to the PKCS#11 providers.'
));

$section->addInput(new Form_Input(
'pkcs11id',
'PKCS#11 ID'
));

$section->addInput(new Form_Checkbox(
'usetoken',
'Microsoft Certificate Storage',
Expand Down Expand Up @@ -490,6 +561,20 @@

$form->add($section);

$form->AddGlobal(new Form_Input(
'vpnidx',
null,
'hidden',
0
));

$form->addGlobal(new Form_Button(
'save_as_default',
gettext("Save as Default"),
null,
'fa-save'
))->addClass('btn-primary');

print($form);
?>

Expand Down Expand Up @@ -531,12 +616,17 @@
var viscosityAvailable = false;

var servers = new Array();
var config = new Array();
<?php foreach ($ras_server as $sindex => $server): ?>
servers[<?=$sindex?>] = new Array();
servers[<?=$sindex?>][0] = '<?=$server['index']?>';
servers[<?=$sindex?>][1] = new Array();
servers[<?=$sindex?>][2] = '<?=$server['mode']?>';
servers[<?=$sindex?>][3] = new Array();
config[<?=$sindex?>] = new Array();
<?php foreach ($a_config['server_'.md5($server['name'])] as $cname => $cvalue): ?>
config[<?=$sindex?>]['<?=$cname?>'] = '<?=$cvalue?>';
<? endforeach; ?>
<?php foreach ($server['users'] as $uindex => $user): ?>
servers[<?=$sindex?>][1][<?=$uindex?>] = new Array();
servers[<?=$sindex?>][1][<?=$uindex?>][0] = '<?=$user['uindex']?>';
Expand Down Expand Up @@ -589,6 +679,12 @@ function download_begin(act, i, j) {
if (document.getElementById("usetoken").checked) {
usetoken = 1;
}
var usepkcs11 = 0;
if (document.getElementById("usepkcs11").checked) {
usepkcs11 = 1;
}
var pkcs11providers = document.getElementById("pkcs11providers").value;
var pkcs11id = document.getElementById("pkcs11id").value;
var usepass = 0;
if (document.getElementById("usepass").checked) {
usepass = 1;
Expand Down Expand Up @@ -668,6 +764,9 @@ function download_begin(act, i, j) {
dlurl += "&randomlocalport=" + encodeURIComponent(randomlocalport);
dlurl += "&openvpnmanager=" + encodeURIComponent(openvpnmanager);
dlurl += "&usetoken=" + encodeURIComponent(usetoken);
dlurl += "&usepkcs11=" + escape(usepkcs11);
dlurl += "&pkcs11providers=" + escape(pkcs11providers);
dlurl += "&pkcs11id=" + escape(pkcs11id);
if (usepass) {
dlurl += "&password=" + encodeURIComponent(pass);
}
Expand Down Expand Up @@ -695,6 +794,18 @@ function server_changed() {
}

var index = document.getElementById("server").selectedIndex;
$('#vpnidx').val(index);
<?php foreach ($persistent_config as $ncfg => $cfg): ?>
<?php if ($cfg['type'] == 'checkbox'): ?>
$('#<?=$ncfg?>').prop('checked', (typeof config[index]['<?=$ncfg?>'] !== 'undefined' ? config[index]['<?=$ncfg?>'] == 'yes' : '<?=$cfg['default']?>'));
<?php else: ?>
$('#<?=$ncfg?>').val(typeof config[index]['<?=$ncfg?>'] !== 'undefined' ? config[index]['<?=$ncfg?>'] : '<?=$cfg['default']?>');
<?php endif; ?>
<?php endforeach; ?>
useaddr_changed();
usepkcs11_changed();
usepass_changed();
useproxy_changed();
var users = servers[index][1];
var certs = servers[index][3];
for (i = 0; i < users.length; i++) {
Expand Down Expand Up @@ -833,6 +944,16 @@ function useaddr_changed() {
}
}

function usepkcs11_changed() {
if ($('#usepkcs11').prop('checked')) {
hideInput('pkcs11id', false);
hideInput('pkcs11providers', false);
} else {
hideInput('pkcs11id', true);
hideInput('pkcs11providers', true);
}
}

function usepass_changed() {
if ($('#usepass').prop('checked')) {
hideInput('pass', false);
Expand Down Expand Up @@ -878,6 +999,9 @@ function useproxy_changed() {
$('#useaddr').on('change', function() {
useaddr_changed();
});
$('#usepkcs11').on('change', function() {
usepkcs11_changed();
});
$('#usepass').on('change', function() {
usepass_changed();
});
Expand All @@ -890,10 +1014,10 @@ function useproxy_changed() {

// ---------- On initial page load ------------------------------------------------------------

if ('<?=$vpnidx?>'!='') {
$('#server').val('<?=$vpnidx?>');
}
server_changed();
useaddr_changed();
usepass_changed();
useproxy_changed();
});
//]]>
</script>
Expand Down