FormatV2.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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', 'meta', 'v', 'ct');
  34. if ($isComment) {
  35. $required_keys[] = 'pasteid';
  36. $required_keys[] = 'parentid';
  37. }
  38. // Make sure no additionnal keys were added.
  39. if (count(array_keys($message)) != count($required_keys)) {
  40. return false;
  41. }
  42. // Make sure required fields are present.
  43. foreach ($required_keys as $k) {
  44. if (!array_key_exists($k, $message)) {
  45. return false;
  46. }
  47. }
  48. // Make sure some fields are base64 data:
  49. // - initialization vector
  50. if (!base64_decode($message['adata'][0][0], true)) {
  51. return false;
  52. }
  53. // - salt
  54. if (!base64_decode($message['adata'][0][1], true)) {
  55. return false;
  56. }
  57. // - cipher text
  58. if (!($ct = base64_decode($message['ct'], true))) {
  59. return false;
  60. }
  61. // Make sure some fields have a reasonable size:
  62. // - initialization vector
  63. if (strlen($message['adata'][0][0]) > 24) {
  64. return false;
  65. }
  66. // - salt
  67. if (strlen($message['adata'][0][1]) > 14) {
  68. return false;
  69. }
  70. // Make sure some fields contain no unsupported values:
  71. // - version
  72. if (!(is_int($message['v']) || is_float($message['v'])) || (float) $message['v'] < 2) {
  73. return false;
  74. }
  75. // - iterations, refuse less then 10000 iterations (minimum NIST recommendation)
  76. if (!is_int($message['adata'][0][2]) || $message['adata'][0][2] <= 10000) {
  77. return false;
  78. }
  79. // - key size
  80. if (!in_array($message['adata'][0][3], array(128, 192, 256), true)) {
  81. return false;
  82. }
  83. // - tag size
  84. if (!in_array($message['adata'][0][4], array(64, 96, 128), true)) {
  85. return false;
  86. }
  87. // - algorithm, must be AES
  88. if ($message['adata'][0][5] !== 'aes') {
  89. return false;
  90. }
  91. // - mode
  92. if (!in_array($message['adata'][0][6], array('ctr', 'cbc', 'gcm'), true)) {
  93. return false;
  94. }
  95. // - compression
  96. if (!in_array($message['adata'][0][7], array('zlib', 'none'), true)) {
  97. return false;
  98. }
  99. // Reject data if entropy is too low
  100. if (strlen($ct) > strlen(gzdeflate($ct))) {
  101. return false;
  102. }
  103. return true;
  104. }
  105. }