FormatV2.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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 = array('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];
  54. // Make sure some fields are base64 data:
  55. // - initialization vector
  56. if (!base64_decode($cipherParams[0], true)) {
  57. return false;
  58. }
  59. // - salt
  60. if (!base64_decode($cipherParams[1], true)) {
  61. return false;
  62. }
  63. // - cipher text
  64. if (!($ct = base64_decode($message['ct'], true))) {
  65. return false;
  66. }
  67. // Make sure some fields have a reasonable size:
  68. // - initialization vector
  69. if (strlen($cipherParams[0]) > 24) {
  70. return false;
  71. }
  72. // - salt
  73. if (strlen($cipherParams[1]) > 14) {
  74. return false;
  75. }
  76. // Make sure some fields contain no unsupported values:
  77. // - version
  78. if (!(is_int($message['v']) || is_float($message['v'])) || (float) $message['v'] < 2) {
  79. return false;
  80. }
  81. // - iterations, refuse less then 10000 iterations (minimum NIST recommendation)
  82. if (!is_int($cipherParams[2]) || $cipherParams[2] <= 10000) {
  83. return false;
  84. }
  85. // - key size
  86. if (!in_array($cipherParams[3], array(128, 192, 256), true)) {
  87. return false;
  88. }
  89. // - tag size
  90. if (!in_array($cipherParams[4], array(64, 96, 128), true)) {
  91. return false;
  92. }
  93. // - algorithm, must be AES
  94. if ($cipherParams[5] !== 'aes') {
  95. return false;
  96. }
  97. // - mode
  98. if (!in_array($cipherParams[6], array('ctr', 'cbc', 'gcm'), true)) {
  99. return false;
  100. }
  101. // - compression
  102. if (!in_array($cipherParams[7], array('zlib', 'none'), true)) {
  103. return false;
  104. }
  105. // Reject data if entropy is too low
  106. if (strlen($ct) > strlen(gzdeflate($ct))) {
  107. return false;
  108. }
  109. // require only the key 'expire' in the metadata of pastes
  110. if (!$isComment && (
  111. count($message['meta']) === 0 ||
  112. !array_key_exists('expire', $message['meta']) ||
  113. count($message['meta']) > 1
  114. )) {
  115. return false;
  116. }
  117. return true;
  118. }
  119. }