From ada8bee70e89c34afa53eff9ad70315cfcd34c6d Mon Sep 17 00:00:00 2001 From: Friendika Date: Sat, 27 Aug 2011 18:09:43 -0700 Subject: [PATCH] remove public disclosure risk --- boot.php | 2 +- include/crypto.php | 65 ++++++++++++++++++++++++++++++++++++++++++++++ mod/network.php | 2 +- 3 files changed, 67 insertions(+), 2 deletions(-) diff --git a/boot.php b/boot.php index cb18b05604..58d6cc8394 100644 --- a/boot.php +++ b/boot.php @@ -7,7 +7,7 @@ require_once('include/text.php'); require_once("include/pgettext.php"); -define ( 'FRIENDIKA_VERSION', '2.2.1084' ); +define ( 'FRIENDIKA_VERSION', '2.2.1085' ); define ( 'DFRN_PROTOCOL_VERSION', '2.21' ); define ( 'DB_UPDATE_VERSION', 1083 ); diff --git a/include/crypto.php b/include/crypto.php index a20606db54..88e05b9eb0 100644 --- a/include/crypto.php +++ b/include/crypto.php @@ -225,3 +225,68 @@ function pkcs5_unpad($text) if (strspn($text, chr($pad), strlen($text) - $pad) != $pad) return false; return substr($text, 0, -1 * $pad); } + +function AES256CBC_encrypt($data,$key,$iv) { + return mcrypt_encrypt( + MCRYPT_RIJNDAEL_128, + str_pad($key,32,"\0"), + pkcs5_pad($data,16), + MCRYPT_MODE_CBC, + str_pad($iv,16,"\0")); +} + +function AES256CBC_decrypt($data,$key,$iv) { + return pkcs5_unpad(mcrypt_decrypt( + MCRYPT_RIJNDAEL_128, + str_pad($key,32,"\0"), + $data, + MCRYPT_MODE_CBC, + str_pad($iv,16,"\0"))); +} + +function aes_encapsulate($data,$pubkey) { + $key = random_string(32,RANDOM_STRING_TEXT); + $iv = random_string(16,RANDOM_STRING_TEXT); + $result['data'] = base64url_encode(AES256CBC_encrypt($data,$key,$iv),true); + openssl_public_encrypt($key,$k,$pubkey); + $result['key'] = base64url_encode($k,true); + openssl_public_encrypt($iv,$i,$pubkey); + $result['iv'] = base64url_encode($i,true); + return $result; +} + +function aes_unencapsulate($data,$prvkey) { + openssl_private_decrypt(base64url_decode($data['key']),$k,$prvkey); + openssl_private_decrypt(base64url_decode($data['iv']),$i,$prvkey); + return AES256CBC_decrypt(base64url_decode($data['data']),$k,$i); +} + + +function zot_encapsulate($data,$sender,$pubkey) { +$res = aes_encapsulate($data,$pubkey); +openssl_public_encrypt($sender,$s,$pubkey); +$s1 = base64url_encode($s,true); + +return <<< EOT + + + {$res['key']} + {$res['iv']} + $s1 + AES-256-CBC + {$res['data']} + +EOT; + +} + +function zot_unencapsulate($data,$prvkey) { + $ret = array(); + $c = array(); + $x = parse_xml_string($data); + $c = array('key' => $x->key,'iv' => $x->iv,'data' => $x->data); + openssl_private_decrypt(base64url_decode($x->sender),$s,$prvkey); + $ret['sender'] = $s; + $ret['data'] = aes_unencapsulate($x,$prvkey); + return $ret; +} \ No newline at end of file diff --git a/mod/network.php b/mod/network.php index 05b74b50a4..54fb2a0a4c 100644 --- a/mod/network.php +++ b/mod/network.php @@ -188,7 +188,7 @@ function network_content(&$a, $update = 0) { if(count($r)) { $sql_extra = " AND `item`.`parent` IN ( SELECT `parent` FROM `item` WHERE `id` = `parent` $star_sql AND `contact-id` IN ( " . intval($cid) . " )) "; $o = '

' . t('Contact: ') . $r[0]['name'] . '

' . $o; - if($r[0]['network'] !== NETWORK_MAIL && $r[0]['network'] !== NETWORK_DFRN && $r[0]['network'] !== NETWORK_FACEBOOK && $r[0]['writable'] && (! get_pconfig(local_user(),'system','nowarn_insecure'))) { + if($r[0]['network'] !== NETWORK_MAIL && $r[0]['network'] !== NETWORK_DFRN && $r[0]['network'] !== NETWORK_FACEBOOK && $r[0]['network'] !== NETWORK_DIASPORA && $r[0]['writable'] && (! get_pconfig(local_user(),'system','nowarn_insecure'))) { notice( t('Private messages to this person are at risk of public disclosure.') . EOL); }