1
0

FormatV2.php 3.6 KB

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