Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
98.08% covered (success)
98.08%
51 / 52
0.00% covered (danger)
0.00%
0 / 1
CRAP
0.00% covered (danger)
0.00%
0 / 1
FormatV2
98.08% covered (success)
98.08%
51 / 52
0.00% covered (danger)
0.00%
0 / 1
33
0.00% covered (danger)
0.00%
0 / 1
 isValid
98.08% covered (success)
98.08%
51 / 52
0.00% covered (danger)
0.00%
0 / 1
33
1<?php declare(strict_types=1);
2/**
3 * PrivateBin
4 *
5 * a zero-knowledge paste bin
6 *
7 * @link      https://github.com/PrivateBin/PrivateBin
8 * @copyright 2012 Sébastien SAUVAGE (sebsauvage.net)
9 * @license   https://www.opensource.org/licenses/zlib-license.php The zlib/libpng License
10 */
11
12namespace PrivateBin;
13
14/**
15 * FormatV2
16 *
17 * Provides validation function for version 2 format of pastes & comments.
18 */
19class FormatV2
20{
21    /**
22     * version 2 format validator
23     *
24     * Checks if the given array is a proper version 2 formatted, encrypted message.
25     *
26     * @access public
27     * @static
28     * @param  array $message
29     * @param  bool  $isComment
30     * @return bool
31     */
32    public static function isValid(&$message, $isComment = false)
33    {
34        $required_keys = ['adata', 'v', 'ct'];
35        if ($isComment) {
36            $required_keys[] = 'pasteid';
37            $required_keys[] = 'parentid';
38        } else {
39            $required_keys[] = 'meta';
40        }
41
42        // Make sure no additionnal keys were added.
43        if (count(array_keys($message)) !== count($required_keys)) {
44            return false;
45        }
46
47        // Make sure required fields are present.
48        foreach ($required_keys as $k) {
49            if (!array_key_exists($k, $message)) {
50                return false;
51            }
52        }
53
54        // Make sure adata is an array.
55        if (!is_array($message['adata'])) {
56            return false;
57        }
58
59        $cipherParams = $isComment ? $message['adata'] : ($message['adata'][0] ?? null);
60
61        // Make sure the cipher parameters are a properly sized array.
62        if (!is_array($cipherParams) || count($cipherParams) < 8) {
63            return false;
64        }
65
66        // Make sure the ciphertext and the cipher parameters used in the
67        // string operations below are actually strings, so that malformed
68        // input yields "Invalid data." instead of a fatal type error.
69        if (!is_string($message['ct']) ||
70            !is_string($cipherParams[0] ?? null) ||
71            !is_string($cipherParams[1] ?? null)) {
72            return false;
73        }
74
75        // Make sure some fields are base64 data:
76        // - initialization vector
77        if (!base64_decode($cipherParams[0], true)) {
78            return false;
79        }
80        // - salt
81        if (!base64_decode($cipherParams[1], true)) {
82            return false;
83        }
84        // - cipher text
85        if (!($ct = base64_decode($message['ct'], true))) {
86            return false;
87        }
88
89        // Make sure some fields have a reasonable size:
90        // - initialization vector
91        if (strlen($cipherParams[0]) > 24) {
92            return false;
93        }
94        // - salt
95        if (strlen($cipherParams[1]) > 14) {
96            return false;
97        }
98
99        // Make sure some fields contain no unsupported values:
100        // - version
101        if (!(is_int($message['v']) || is_float($message['v'])) || (float) $message['v'] < 2) {
102            return false;
103        }
104        // - iterations, refuse less then 10000 iterations (minimum NIST recommendation)
105        if (!is_int($cipherParams[2]) || $cipherParams[2] <= 10000) {
106            return false;
107        }
108        // - key size
109        if (!in_array($cipherParams[3], [128, 192, 256], true)) {
110            return false;
111        }
112        // - tag size
113        if (!in_array($cipherParams[4], [64, 96, 128], true)) {
114            return false;
115        }
116        // - algorithm, must be AES
117        if ($cipherParams[5] !== 'aes') {
118            return false;
119        }
120        // - mode
121        if (!in_array($cipherParams[6], ['ctr', 'cbc', 'gcm'], true)) {
122            return false;
123        }
124        // - compression
125        if (!in_array($cipherParams[7], ['zlib', 'none'], true)) {
126            return false;
127        }
128
129        // Reject data if entropy is too low
130        if (strlen($ct) > strlen(gzdeflate($ct))) {
131            return false;
132        }
133
134        // require only the key 'expire' in the metadata of pastes
135        if (!$isComment && (
136            !is_array($message['meta']) ||
137            count($message['meta']) === 0 ||
138            !array_key_exists('expire', $message['meta']) ||
139            count($message['meta']) > 1
140        )) {
141            return false;
142        }
143
144        return true;
145    }
146}