Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
98.33% covered (success)
98.33%
59 / 60
0.00% covered (danger)
0.00%
0 / 1
CRAP
0.00% covered (danger)
0.00%
0 / 1
FormatV2
98.33% covered (success)
98.33%
59 / 60
0.00% covered (danger)
0.00%
0 / 1
39
0.00% covered (danger)
0.00%
0 / 1
 isValid
98.33% covered (success)
98.33%
59 / 60
0.00% covered (danger)
0.00%
0 / 1
39
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 additional 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 adata parameters of pastes are a properly sized array
67        $adataLength = count($message['adata']);
68
69        // Comments may authenticate their document and parent IDs, by
70        // appending them to the cipher parameters. These must match the
71        // unencrypted IDs, which are validated by the comment model.
72        if ($isComment) {
73            if ($adataLength !== 8 && (
74                count($cipherParams) !== 10 ||
75                !is_string($cipherParams[8]) ||
76                !is_string($cipherParams[9]) ||
77                $cipherParams[8] !== $message['pasteid'] ||
78                $cipherParams[9] !== $message['parentid']
79            )) {
80                return false;
81            }
82            // For pastes: adata must have exactly 4 entries,
83            // and meta must contain only the 'expire' key.
84        } elseif ($adataLength !== 4 ||
85            !is_array($message['meta']) ||
86            count($message['meta']) !== 1 ||
87            !array_key_exists('expire', $message['meta'])
88        ) {
89            return false;
90        }
91
92        // Make sure the ciphertext and the cipher parameters used in the
93        // string operations below are actually strings, so that malformed
94        // input yields "Invalid data." instead of a fatal type error.
95        if (!is_string($message['ct']) ||
96            !is_string($cipherParams[0] ?? null) ||
97            !is_string($cipherParams[1] ?? null)) {
98            return false;
99        }
100
101        // Make sure some fields are base64 data:
102        // - initialization vector
103        if (!base64_decode($cipherParams[0], true)) {
104            return false;
105        }
106        // - salt
107        if (!base64_decode($cipherParams[1], true)) {
108            return false;
109        }
110        // - cipher text
111        if (!($ct = base64_decode($message['ct'], true))) {
112            return false;
113        }
114
115        // Make sure some fields have a reasonable size:
116        // - initialization vector
117        if (strlen($cipherParams[0]) > 24) {
118            return false;
119        }
120        // - salt
121        if (strlen($cipherParams[1]) > 14) {
122            return false;
123        }
124
125        // Make sure some fields contain no unsupported values:
126        // - version
127        if (!(is_int($message['v']) || is_float($message['v'])) || (float) $message['v'] < 2) {
128            return false;
129        }
130        // - iterations, refuse less then 10000 iterations (minimum NIST recommendation)
131        if (!is_int($cipherParams[2]) || $cipherParams[2] <= 10000) {
132            return false;
133        }
134        // - key size
135        if (!in_array($cipherParams[3], [128, 192, 256], true)) {
136            return false;
137        }
138        // - tag size
139        if (!in_array($cipherParams[4], [64, 96, 128], true)) {
140            return false;
141        }
142        // - algorithm, must be AES
143        if ($cipherParams[5] !== 'aes') {
144            return false;
145        }
146        // - mode
147        if (!in_array($cipherParams[6], ['ctr', 'cbc', 'gcm'], true)) {
148            return false;
149        }
150        // - compression
151        if (!in_array($cipherParams[7], ['zlib', 'none'], true)) {
152            return false;
153        }
154
155        // Reject data if entropy is too low
156        if (strlen($ct) > strlen(gzdeflate($ct))) {
157            return false;
158        }
159
160        return true;
161    }
162}