2019-05-03 23:03:57 +02:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* PrivateBin
|
|
|
|
*
|
|
|
|
* a zero-knowledge paste bin
|
|
|
|
*
|
|
|
|
* @link https://github.com/PrivateBin/PrivateBin
|
|
|
|
* @copyright 2012 Sébastien SAUVAGE (sebsauvage.net)
|
|
|
|
* @license https://www.opensource.org/licenses/zlib-license.php The zlib/libpng License
|
2021-04-05 16:44:12 +02:00
|
|
|
* @version 1.3.5
|
2019-05-03 23:03:57 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
namespace PrivateBin;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* FormatV2
|
|
|
|
*
|
|
|
|
* Provides validation function for version 2 format of pastes & comments.
|
|
|
|
*/
|
|
|
|
class FormatV2
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* version 2 format validator
|
|
|
|
*
|
|
|
|
* Checks if the given array is a proper version 2 formatted, encrypted message.
|
|
|
|
*
|
|
|
|
* @access public
|
|
|
|
* @static
|
|
|
|
* @param array $message
|
|
|
|
* @param bool $isComment
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public static function isValid($message, $isComment = false)
|
|
|
|
{
|
2019-05-06 22:15:21 +02:00
|
|
|
$required_keys = array('adata', 'v', 'ct');
|
2019-05-03 23:03:57 +02:00
|
|
|
if ($isComment) {
|
|
|
|
$required_keys[] = 'pasteid';
|
|
|
|
$required_keys[] = 'parentid';
|
2019-05-06 22:15:21 +02:00
|
|
|
} else {
|
|
|
|
$required_keys[] = 'meta';
|
2019-05-03 23:03:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Make sure no additionnal keys were added.
|
|
|
|
if (count(array_keys($message)) != count($required_keys)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Make sure required fields are present.
|
|
|
|
foreach ($required_keys as $k) {
|
|
|
|
if (!array_key_exists($k, $message)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-13 10:44:26 +02:00
|
|
|
// Make sure adata is an array.
|
|
|
|
if (!is_array($message['adata'])) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-05-15 07:44:03 +02:00
|
|
|
$cipherParams = $isComment ? $message['adata'] : $message['adata'][0];
|
|
|
|
|
2019-05-03 23:03:57 +02:00
|
|
|
// Make sure some fields are base64 data:
|
|
|
|
// - initialization vector
|
2019-05-15 07:44:03 +02:00
|
|
|
if (!base64_decode($cipherParams[0], true)) {
|
2019-05-03 23:03:57 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
// - salt
|
2019-05-15 07:44:03 +02:00
|
|
|
if (!base64_decode($cipherParams[1], true)) {
|
2019-05-03 23:03:57 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
// - cipher text
|
|
|
|
if (!($ct = base64_decode($message['ct'], true))) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Make sure some fields have a reasonable size:
|
|
|
|
// - initialization vector
|
2019-05-15 07:44:03 +02:00
|
|
|
if (strlen($cipherParams[0]) > 24) {
|
2019-05-03 23:03:57 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
// - salt
|
2019-05-15 07:44:03 +02:00
|
|
|
if (strlen($cipherParams[1]) > 14) {
|
2019-05-03 23:03:57 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Make sure some fields contain no unsupported values:
|
|
|
|
// - version
|
|
|
|
if (!(is_int($message['v']) || is_float($message['v'])) || (float) $message['v'] < 2) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
// - iterations, refuse less then 10000 iterations (minimum NIST recommendation)
|
2019-05-15 07:44:03 +02:00
|
|
|
if (!is_int($cipherParams[2]) || $cipherParams[2] <= 10000) {
|
2019-05-03 23:03:57 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
// - key size
|
2019-05-15 07:44:03 +02:00
|
|
|
if (!in_array($cipherParams[3], array(128, 192, 256), true)) {
|
2019-05-03 23:03:57 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
// - tag size
|
2019-05-15 07:44:03 +02:00
|
|
|
if (!in_array($cipherParams[4], array(64, 96, 128), true)) {
|
2019-05-03 23:03:57 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
// - algorithm, must be AES
|
2019-05-15 07:44:03 +02:00
|
|
|
if ($cipherParams[5] !== 'aes') {
|
2019-05-03 23:03:57 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
// - mode
|
2019-05-15 07:44:03 +02:00
|
|
|
if (!in_array($cipherParams[6], array('ctr', 'cbc', 'gcm'), true)) {
|
2019-05-03 23:03:57 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
// - compression
|
2019-05-15 07:44:03 +02:00
|
|
|
if (!in_array($cipherParams[7], array('zlib', 'none'), true)) {
|
2019-05-03 23:03:57 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Reject data if entropy is too low
|
|
|
|
if (strlen($ct) > strlen(gzdeflate($ct))) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-05-06 22:15:21 +02:00
|
|
|
// require only the key 'expire' in the metadata of pastes
|
|
|
|
if (!$isComment && (
|
|
|
|
count($message['meta']) === 0 ||
|
|
|
|
!array_key_exists('expire', $message['meta']) ||
|
|
|
|
count($message['meta']) > 1
|
|
|
|
)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-05-03 23:03:57 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|