packages/ssoinabox-webui/root/usr/local/share/ssoinabox/htdocs/includes/functions.php
changeset 4 2212b2ded8bf
parent 3 a044870a9d3d
child 8 f68fdcc18df9
--- a/packages/ssoinabox-webui/root/usr/local/share/ssoinabox/htdocs/includes/functions.php	Fri Jan 11 00:32:54 2013 -0500
+++ b/packages/ssoinabox-webui/root/usr/local/share/ssoinabox/htdocs/includes/functions.php	Fri Jan 11 05:41:41 2013 -0500
@@ -26,6 +26,11 @@
 	return get_next_available_uid();
 }
 
+function smarty_function_json_encode($params)
+{
+	return json_encode($params['value']);
+}
+
 function load_credentials()
 {
 	$config = yaml_parse_file("/usr/local/etc/ssoinabox/webcreds.yml");
@@ -87,3 +92,49 @@
 	
 	return strlen($uniq);
 }
+
+$ssh_key_lengths = array(
+		// pubkey len => key bits
+		'ecdsa-sha2-nistp521' => array('name' => 'ECDSA', 172 => 521)
+		, 'ecdsa-sha2-nistp384' => array('name' => 'ECDSA', 136 => 384)
+		, 'ecdsa-sha2-nistp256' => array('name' => 'ECDSA', 104 => 256)
+		, 'ssh-dss' => array(
+				'name' => 'DSA'
+				, 432 => 1024
+				, 433 => 1024
+				, 434 => 1024
+				, 435 => 1024
+			)
+		, 'ssh-rsa' => array(
+				'name' => 'RSA'
+				, 119 => 768
+				, 151 => 1024
+				, 215 => 1536
+				, 277 => 2048
+				, 279 => 2048
+				, 407 => 3072
+				, 535 => 4096
+			)
+	);
+
+function smarty_function_decode_ssh_key($params, $smarty)
+{
+	global $ssh_key_lengths;
+	
+	if ( !isset($params['key']) )
+		throw new SmartyException("No key provided");
+	
+	if ( !isset($params['out']) )
+		throw new SmartyException("No output var provided");
+	
+	list($type, $key_b64) = preg_split('/\s+/', $params['key']);
+	
+	$key = base64_decode($key_b64);
+	$bits = isset($ssh_key_lengths[$type][strlen($key)]) ? $ssh_key_lengths[$type][strlen($key)] : 0;
+	
+	$smarty->assign($params['out'], array(
+			'fingerprint' => implode(':', str_split(md5($key), 2))
+			, 'type' => $ssh_key_lengths[$type]['name']
+			, 'bits' => $bits
+		));
+}