FormatV2.php 3.7 KB

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