CVE-2020-7069.patch 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. Subject: Fix bug #79601 (Wrong ciphertext/tag in AES-CCM encryption
  2. for a 12 bytes IV)
  3. ---
  4. ext/openssl/openssl.c | 10 ++++-----
  5. ext/openssl/tests/cipher_tests.inc | 21 +++++++++++++++++
  6. ext/openssl/tests/openssl_decrypt_ccm.phpt | 22 +++++++++++-------
  7. ext/openssl/tests/openssl_encrypt_ccm.phpt | 26 ++++++++++++++--------
  8. 4 files changed, 57 insertions(+), 22 deletions(-)
  9. diff --git a/ext/openssl/openssl.c b/ext/openssl/openssl.c
  10. index 04cb9b0f..fdad2c3b 100644
  11. --- a/ext/openssl/openssl.c
  12. +++ b/ext/openssl/openssl.c
  13. @@ -6521,11 +6521,6 @@ static int php_openssl_validate_iv(char **piv, size_t *piv_len, size_t iv_requir
  14. {
  15. char *iv_new;
  16. - /* Best case scenario, user behaved */
  17. - if (*piv_len == iv_required_len) {
  18. - return SUCCESS;
  19. - }
  20. -
  21. if (mode->is_aead) {
  22. if (EVP_CIPHER_CTX_ctrl(cipher_ctx, mode->aead_ivlen_flag, *piv_len, NULL) != 1) {
  23. php_error_docref(NULL, E_WARNING, "Setting of IV length for AEAD mode failed");
  24. @@ -6534,6 +6529,11 @@ static int php_openssl_validate_iv(char **piv, size_t *piv_len, size_t iv_requir
  25. return SUCCESS;
  26. }
  27. + /* Best case scenario, user behaved */
  28. + if (*piv_len == iv_required_len) {
  29. + return SUCCESS;
  30. + }
  31. +
  32. iv_new = ecalloc(1, iv_required_len + 1);
  33. if (*piv_len == 0) {
  34. diff --git a/ext/openssl/tests/cipher_tests.inc b/ext/openssl/tests/cipher_tests.inc
  35. index b1e46b41..779bfa85 100644
  36. --- a/ext/openssl/tests/cipher_tests.inc
  37. +++ b/ext/openssl/tests/cipher_tests.inc
  38. @@ -1,5 +1,26 @@
  39. <?php
  40. $php_openssl_cipher_tests = array(
  41. + 'aes-128-ccm' => array(
  42. + array(
  43. + 'key' => '404142434445464748494a4b4c4d4e4f',
  44. + 'iv' => '1011121314151617',
  45. + 'aad' => '000102030405060708090a0b0c0d0e0f',
  46. + 'tag' => '1fc64fbfaccd',
  47. + 'pt' => '202122232425262728292a2b2c2d2e2f',
  48. + 'ct' => 'd2a1f0e051ea5f62081a7792073d593d',
  49. + ),
  50. + array(
  51. + 'key' => '404142434445464748494a4b4c4d4e4f',
  52. + 'iv' => '101112131415161718191a1b',
  53. + 'aad' => '000102030405060708090a0b0c0d0e0f' .
  54. + '10111213',
  55. + 'tag' => '484392fbc1b09951',
  56. + 'pt' => '202122232425262728292a2b2c2d2e2f' .
  57. + '3031323334353637',
  58. + 'ct' => 'e3b201a9f5b71a7a9b1ceaeccd97e70b' .
  59. + '6176aad9a4428aa5',
  60. + ),
  61. + ),
  62. 'aes-256-ccm' => array(
  63. array(
  64. 'key' => '1bde3251d41a8b5ea013c195ae128b21' .
  65. diff --git a/ext/openssl/tests/openssl_decrypt_ccm.phpt b/ext/openssl/tests/openssl_decrypt_ccm.phpt
  66. index a5f01b87..08ef5bb7 100644
  67. --- a/ext/openssl/tests/openssl_decrypt_ccm.phpt
  68. +++ b/ext/openssl/tests/openssl_decrypt_ccm.phpt
  69. @@ -10,14 +10,16 @@ if (!in_array('aes-256-ccm', openssl_get_cipher_methods()))
  70. --FILE--
  71. <?php
  72. require_once __DIR__ . "/cipher_tests.inc";
  73. -$method = 'aes-256-ccm';
  74. -$tests = openssl_get_cipher_tests($method);
  75. +$methods = ['aes-128-ccm', 'aes-256-ccm'];
  76. -foreach ($tests as $idx => $test) {
  77. - echo "TEST $idx\n";
  78. - $pt = openssl_decrypt($test['ct'], $method, $test['key'], OPENSSL_RAW_DATA,
  79. - $test['iv'], $test['tag'], $test['aad']);
  80. - var_dump($test['pt'] === $pt);
  81. +foreach ($methods as $method) {
  82. + $tests = openssl_get_cipher_tests($method);
  83. + foreach ($tests as $idx => $test) {
  84. + echo "$method - TEST $idx\n";
  85. + $pt = openssl_decrypt($test['ct'], $method, $test['key'], OPENSSL_RAW_DATA,
  86. + $test['iv'], $test['tag'], $test['aad']);
  87. + var_dump($test['pt'] === $pt);
  88. + }
  89. }
  90. // no IV
  91. @@ -32,7 +34,11 @@ var_dump(openssl_decrypt($test['ct'], $method, $test['key'], OPENSSL_RAW_DATA,
  92. ?>
  93. --EXPECTF--
  94. -TEST 0
  95. +aes-128-ccm - TEST 0
  96. +bool(true)
  97. +aes-128-ccm - TEST 1
  98. +bool(true)
  99. +aes-256-ccm - TEST 0
  100. bool(true)
  101. Warning: openssl_decrypt(): Setting of IV length for AEAD mode failed in %s on line %d
  102. diff --git a/ext/openssl/tests/openssl_encrypt_ccm.phpt b/ext/openssl/tests/openssl_encrypt_ccm.phpt
  103. index fb5dbbc8..8c4c41f8 100644
  104. --- a/ext/openssl/tests/openssl_encrypt_ccm.phpt
  105. +++ b/ext/openssl/tests/openssl_encrypt_ccm.phpt
  106. @@ -10,15 +10,17 @@ if (!in_array('aes-256-ccm', openssl_get_cipher_methods()))
  107. --FILE--
  108. <?php
  109. require_once __DIR__ . "/cipher_tests.inc";
  110. -$method = 'aes-256-ccm';
  111. -$tests = openssl_get_cipher_tests($method);
  112. +$methods = ['aes-128-ccm', 'aes-256-ccm'];
  113. -foreach ($tests as $idx => $test) {
  114. - echo "TEST $idx\n";
  115. - $ct = openssl_encrypt($test['pt'], $method, $test['key'], OPENSSL_RAW_DATA,
  116. - $test['iv'], $tag, $test['aad'], strlen($test['tag']));
  117. - var_dump($test['ct'] === $ct);
  118. - var_dump($test['tag'] === $tag);
  119. +foreach ($methods as $method) {
  120. + $tests = openssl_get_cipher_tests($method);
  121. + foreach ($tests as $idx => $test) {
  122. + echo "$method - TEST $idx\n";
  123. + $ct = openssl_encrypt($test['pt'], $method, $test['key'], OPENSSL_RAW_DATA,
  124. + $test['iv'], $tag, $test['aad'], strlen($test['tag']));
  125. + var_dump($test['ct'] === $ct);
  126. + var_dump($test['tag'] === $tag);
  127. + }
  128. }
  129. // Empty IV error
  130. @@ -32,7 +34,13 @@ var_dump(strlen($tag));
  131. var_dump(openssl_encrypt('data', $method, 'password', 0, str_repeat('x', 16), $tag, '', 1024));
  132. ?>
  133. --EXPECTF--
  134. -TEST 0
  135. +aes-128-ccm - TEST 0
  136. +bool(true)
  137. +bool(true)
  138. +aes-128-ccm - TEST 1
  139. +bool(true)
  140. +bool(true)
  141. +aes-256-ccm - TEST 0
  142. bool(true)
  143. bool(true)
  144. --
  145. 2.25.1