FormatV2.php 3.5 KB

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