From 46cb45d94b6d7892a10b043e036da09cc72cbe98 Mon Sep 17 00:00:00 2001 From: zotlabs Date: Fri, 16 Feb 2018 18:45:15 -0800 Subject: crypto improvements (use pkcs1_oaep_padding instead of the older pkcs1_padding) --- include/crypto.php | 27 ++++++++++++++++++++++----- 1 file changed, 22 insertions(+), 5 deletions(-) (limited to 'include/crypto.php') diff --git a/include/crypto.php b/include/crypto.php index 105c1c54f..b732b17ad 100644 --- a/include/crypto.php +++ b/include/crypto.php @@ -122,6 +122,14 @@ function other_encapsulate($data,$pubkey,$alg) { if(! $pubkey) logger('no key. data: ' . $data); + $oaep = false; + + if(strpos($alg,'.oaep')) { + $oaep = true; + $alg = substr($alg,0,-5); + } + + $fn = strtoupper($alg) . '_encrypt'; if(function_exists($fn)) { @@ -140,14 +148,14 @@ function other_encapsulate($data,$pubkey,$alg) { $iv = openssl_random_pseudo_bytes(256); $result['data'] = base64url_encode($fn($data,$key,$iv),true); // log the offending call so we can track it down - if(! openssl_public_encrypt($key,$k,$pubkey)) { + if(! openssl_public_encrypt($key,$k,$pubkey,(($oaep) ? OPENSSL_PKCS1_OAEP_PADDING : OPENSSL_PKCS1_PADDING))) { $x = debug_backtrace(); logger('RSA failed. ' . print_r($x[0],true)); } $result['alg'] = $alg; $result['key'] = base64url_encode($k,true); - openssl_public_encrypt($iv,$i,$pubkey); + openssl_public_encrypt($iv,$i,$pubkey,(($oaep) ? OPENSSL_PKCS1_OAEP_PADDING : OPENSSL_PKCS1_PADDING)); $result['iv'] = base64url_encode($i,true); return $result; } @@ -166,7 +174,7 @@ function crypto_methods() { // The actual methods are responsible for deriving the actual key/iv from the provided parameters; // possibly by truncation or segmentation - though many other methods could be used. - $r = [ 'aes256ctr', 'camellia256cfb', 'cast5cfb', 'aes256cbc', 'aes128cbc', 'cast5cbc' ]; + $r = [ 'aes256ctr.oaep', 'camellia256cfb.oaep', 'cast5cfb.oaep', 'aes256ctr', 'camellia256cfb', 'cast5cfb', 'aes256cbc', 'aes128cbc', 'cast5cbc' ]; call_hooks('crypto_methods',$r); return $r; @@ -216,10 +224,19 @@ function crypto_unencapsulate($data,$prvkey) { } function other_unencapsulate($data,$prvkey,$alg) { + + $oaep = false; + + if(strpos($alg,'.oaep')) { + $oaep = true; + $alg = substr($alg,0,-5); + } + + $fn = strtoupper($alg) . '_decrypt'; if(function_exists($fn)) { - openssl_private_decrypt(base64url_decode($data['key']),$k,$prvkey); - openssl_private_decrypt(base64url_decode($data['iv']),$i,$prvkey); + openssl_private_decrypt(base64url_decode($data['key']),$k,$prvkey,(($oaep) ? OPENSSL_PKCS1_OAEP_PADDING : OPENSSL_PKCS1_PADDING)); + openssl_private_decrypt(base64url_decode($data['iv']),$i,$prvkey,(($oaep) ? OPENSSL_PKCS1_OAEP_PADDING : OPENSSL_PKCS1_PADDING)); return $fn(base64url_decode($data['data']),$k,$i); } else { -- cgit v1.2.3 From b6b4827680d14bcb0062bba4a272f661bbb33d8c Mon Sep 17 00:00:00 2001 From: zotlabs Date: Mon, 19 Feb 2018 15:44:18 -0800 Subject: OAEP padding mismatch on some newer encryption methods --- include/crypto.php | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) (limited to 'include/crypto.php') diff --git a/include/crypto.php b/include/crypto.php index b732b17ad..f9cf20deb 100644 --- a/include/crypto.php +++ b/include/crypto.php @@ -126,11 +126,11 @@ function other_encapsulate($data,$pubkey,$alg) { if(strpos($alg,'.oaep')) { $oaep = true; - $alg = substr($alg,0,-5); + $subalg = substr($alg,0,-5); } - $fn = strtoupper($alg) . '_encrypt'; + $fn = strtoupper($subalg) . '_encrypt'; if(function_exists($fn)) { // A bit hesitant to use openssl_random_pseudo_bytes() as we know @@ -160,7 +160,7 @@ function other_encapsulate($data,$pubkey,$alg) { return $result; } else { - $x = [ 'data' => $data, 'pubkey' => $pubkey, 'alg' => $alg, 'result' => $data ]; + $x = [ 'data' => $data, 'pubkey' => $pubkey, 'alg' => $subalg, 'result' => $data ]; call_hooks('other_encapsulate', $x); return $x['result']; } @@ -215,6 +215,7 @@ function aes_encapsulate($data,$pubkey) { function crypto_unencapsulate($data,$prvkey) { if(! $data) return; + $alg = ((array_key_exists('alg',$data)) ? $data['alg'] : 'aes256cbc'); if($alg === 'aes256cbc') return aes_unencapsulate($data,$prvkey); @@ -229,18 +230,18 @@ function other_unencapsulate($data,$prvkey,$alg) { if(strpos($alg,'.oaep')) { $oaep = true; - $alg = substr($alg,0,-5); + $subalg = substr($alg,0,-5); } - $fn = strtoupper($alg) . '_decrypt'; + $fn = strtoupper($subalg) . '_decrypt'; if(function_exists($fn)) { openssl_private_decrypt(base64url_decode($data['key']),$k,$prvkey,(($oaep) ? OPENSSL_PKCS1_OAEP_PADDING : OPENSSL_PKCS1_PADDING)); openssl_private_decrypt(base64url_decode($data['iv']),$i,$prvkey,(($oaep) ? OPENSSL_PKCS1_OAEP_PADDING : OPENSSL_PKCS1_PADDING)); return $fn(base64url_decode($data['data']),$k,$i); } else { - $x = [ 'data' => $data, 'prvkey' => $prvkey, 'alg' => $alg, 'result' => $data ]; + $x = [ 'data' => $data, 'prvkey' => $prvkey, 'alg' => $subalg, 'result' => $data ]; call_hooks('other_unencapsulate',$x); return $x['result']; } -- cgit v1.2.3 From ae8623e3afb5fe6f6693f0b5b520735a7afce12f Mon Sep 17 00:00:00 2001 From: zotlabs Date: Tue, 20 Feb 2018 11:51:59 -0800 Subject: encrypt/decrypt function not found --- include/crypto.php | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) (limited to 'include/crypto.php') diff --git a/include/crypto.php b/include/crypto.php index f9cf20deb..11654564e 100644 --- a/include/crypto.php +++ b/include/crypto.php @@ -128,6 +128,9 @@ function other_encapsulate($data,$pubkey,$alg) { $oaep = true; $subalg = substr($alg,0,-5); } + else { + $subalg = $alg; + } $fn = strtoupper($subalg) . '_encrypt'; @@ -232,7 +235,9 @@ function other_unencapsulate($data,$prvkey,$alg) { $oaep = true; $subalg = substr($alg,0,-5); } - + else { + $subalg = $alg; + } $fn = strtoupper($subalg) . '_decrypt'; if(function_exists($fn)) { -- cgit v1.2.3 From dbeee4707b73c87684146fefa15c2caaf323f921 Mon Sep 17 00:00:00 2001 From: zotlabs Date: Tue, 20 Feb 2018 11:56:51 -0800 Subject: don't try to handle OAEP for plugin crypto methods; let them do it if desired --- include/crypto.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'include/crypto.php') diff --git a/include/crypto.php b/include/crypto.php index 11654564e..ab33ba096 100644 --- a/include/crypto.php +++ b/include/crypto.php @@ -163,7 +163,7 @@ function other_encapsulate($data,$pubkey,$alg) { return $result; } else { - $x = [ 'data' => $data, 'pubkey' => $pubkey, 'alg' => $subalg, 'result' => $data ]; + $x = [ 'data' => $data, 'pubkey' => $pubkey, 'alg' => $alg, 'result' => $data ]; call_hooks('other_encapsulate', $x); return $x['result']; } @@ -246,7 +246,7 @@ function other_unencapsulate($data,$prvkey,$alg) { return $fn(base64url_decode($data['data']),$k,$i); } else { - $x = [ 'data' => $data, 'prvkey' => $prvkey, 'alg' => $subalg, 'result' => $data ]; + $x = [ 'data' => $data, 'prvkey' => $prvkey, 'alg' => $alg, 'result' => $data ]; call_hooks('other_unencapsulate',$x); return $x['result']; } -- cgit v1.2.3