Skip to content
This repository was archived by the owner on Nov 25, 2020. It is now read-only.

Commit 89053cb

Browse files
committed
Add phpseclib for pure-php implementation of legacy mcrypt-ed data. We have to switch to another block size for future.
1 parent 9aa6534 commit 89053cb

File tree

3 files changed

+104
-4
lines changed

3 files changed

+104
-4
lines changed

core/src/core/composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@
1919
"sabre/dav":"1.8.10",
2020
"aws/aws-sdk-php": "^3.19.4",
2121
"meenie/javascript-packer":"1.1",
22-
"dapphp/securimage":"3.6.4"
22+
"dapphp/securimage":"3.6.4",
23+
"phpseclib/phpseclib":"2.0.3"
2324
}
2425

2526
}

core/src/core/src/pydio/Core/Utils/Crypto.php

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,11 @@
2020
*/
2121
namespace Pydio\Core\Utils;
2222

23+
use phpseclib\Crypt\Rijndael;
2324
use Pydio\Core\Services\ConfService;
25+
use Pydio\Core\Utils\Crypto\ZeroPaddingRijndael;
26+
use Pydio\Core\Utils\Vars\StringHelper;
27+
2428

2529
defined('AJXP_EXEC') or die('Access not allowed');
2630

@@ -59,7 +63,13 @@ public static function getCliSecret(){
5963
* @return string
6064
*/
6165
public static function getRandomSalt($base64encode = true){
62-
$salt = mcrypt_create_iv(PBKDF2_SALT_BYTE_SIZE, MCRYPT_DEV_URANDOM);
66+
if(function_exists('openssl_random_pseudo_bytes')){
67+
$salt = openssl_random_pseudo_bytes(32);
68+
}else if (function_exists('mcrypt_create_iv')){
69+
$salt = mcrypt_create_iv(PBKDF2_SALT_BYTE_SIZE, MCRYPT_DEV_URANDOM);
70+
}else{
71+
$salt = StringHelper::generateRandomString(32, true);
72+
}
6373
return ($base64encode ? base64_encode($salt) : $salt);
6474
}
6575

@@ -70,7 +80,10 @@ public static function getRandomSalt($base64encode = true){
7080
* @return mixed
7181
*/
7282
public static function encrypt($data, $key, $base64encode = true){
73-
$encoded = mcrypt_encrypt(MCRYPT_RIJNDAEL_256, $key, $data, MCRYPT_MODE_ECB);
83+
$r = new ZeroPaddingRijndael(Rijndael::MODE_ECB);
84+
$r->setKey($key);
85+
$r->setBlockLength(256);
86+
$encoded = $r->encrypt($data);
7487
if($base64encode) {
7588
return base64_encode($encoded);
7689
} else {
@@ -88,7 +101,10 @@ public static function decrypt($data, $key, $base64encoded = true){
88101
if($base64encoded){
89102
$data = base64_decode($data);
90103
}
91-
return trim(mcrypt_decrypt(MCRYPT_RIJNDAEL_256, $key, $data, MCRYPT_MODE_ECB), "\0");
104+
$r = new ZeroPaddingRijndael(Rijndael::MODE_ECB);
105+
$r->setKey($key);
106+
$r->setBlockLength(256);
107+
return $r->decrypt($data);
92108
}
93109

94110
}
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
<?php
2+
/*
3+
* Copyright 2007-2016 Abstrium <contact (at) pydio.com>
4+
* This file is part of Pydio.
5+
*
6+
* Pydio is free software: you can redistribute it and/or modify
7+
* it under the terms of the GNU Affero General Public License as published by
8+
* the Free Software Foundation, either version 3 of the License, or
9+
* (at your option) any later version.
10+
*
11+
* Pydio is distributed in the hope that it will be useful,
12+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14+
* GNU Affero General Public License for more details.
15+
*
16+
* You should have received a copy of the GNU Affero General Public License
17+
* along with Pydio. If not, see <http://www.gnu.org/licenses/>.
18+
*
19+
* The latest code can be found at <https://pydio.com/>.
20+
*/
21+
22+
namespace Pydio\Core\Utils\Crypto;
23+
24+
defined('AJXP_EXEC') or die('Access not allowed');
25+
26+
use \phpseclib\Crypt\Rijndael;
27+
28+
/**
29+
* Class ZeroPaddingRijndael
30+
* @package Pydio\Core\Utils\Crypto
31+
*/
32+
class ZeroPaddingRijndael extends Rijndael {
33+
/**
34+
* Pads a string
35+
*
36+
* Pads a string using the RSA PKCS padding standards so that its length is a multiple of the blocksize.
37+
* $this->block_size - (strlen($text) % $this->block_size) bytes are added, each of which is equal to
38+
* chr($this->block_size - (strlen($text) % $this->block_size)
39+
*
40+
* If padding is disabled and $text is not a multiple of the blocksize, the string will be padded regardless
41+
* and padding will, hence forth, be enabled.
42+
*
43+
* @see self::_unpad()
44+
* @param string $text
45+
* @throws \LengthException if padding is disabled and the plaintext's length is not a multiple of the block size
46+
* @access private
47+
* @return string
48+
*/
49+
function _pad($text)
50+
{
51+
$length = strlen($text);
52+
53+
if (!$this->padding) {
54+
if ($length % $this->block_size == 0) {
55+
return $text;
56+
} else {
57+
throw new \LengthException("The plaintext's length ($length) is not a multiple of the block size ({$this->block_size}). Try enabling padding.");
58+
}
59+
}
60+
61+
$pad = $this->block_size - ($length % $this->block_size);
62+
return str_pad($text, $length + $pad, "\0");
63+
}
64+
/**
65+
* Unpads a string.
66+
*
67+
* If padding is enabled and the reported padding length is invalid the encryption key will be assumed to be wrong
68+
* and false will be returned.
69+
*
70+
* @see self::_pad()
71+
* @param string $text
72+
* @throws \LengthException if the ciphertext's length is not a multiple of the block size
73+
* @access private
74+
* @return string
75+
*/
76+
function _unpad($text) {
77+
$trimed = trim($text, "\0");
78+
while($text === $trimed){
79+
$trimed = trim($text, "\0");
80+
}
81+
return $trimed;
82+
}
83+
}

0 commit comments

Comments
 (0)