FormatV2.php 4.2 KB

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