You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

139 lines
3.5 KiB

  1. From 1664902525a1c3771b4d8b3ccab7ea1ba6b2bdd1 Mon Sep 17 00:00:00 2001
  2. From: Holger Liebig <holger.liebig@ts.fujitsu.com>
  3. Date: Tue, 4 Apr 2017 20:43:05 +0200
  4. Subject: [PATCH 4/4] ID:480 - Call EVP_CIPHER_CTX_free() instead of
  5. EVP_CIPHER_CTX_cleanup()
  6. Call EVP_CIPHER_CTX_free() instead of EVP_CIPHER_CTX_cleanup() to fix memory
  7. leak.
  8. ---
  9. src/plugins/lanplus/lanplus_crypt_impl.c | 44 +++++++++++++++++---------------
  10. 1 file changed, 23 insertions(+), 21 deletions(-)
  11. --- a/src/plugins/lanplus/lanplus_crypt_impl.c
  12. +++ b/src/plugins/lanplus/lanplus_crypt_impl.c
  13. @@ -165,13 +165,6 @@ lanplus_encrypt_aes_cbc_128(const uint8_
  14. uint32_t * bytes_written)
  15. {
  16. EVP_CIPHER_CTX *ctx = NULL;
  17. - ctx = EVP_CIPHER_CTX_new();
  18. - if (ctx == NULL) {
  19. - *bytes_written = 0;
  20. - return;
  21. - }
  22. - EVP_EncryptInit_ex(ctx, EVP_aes_128_cbc(), NULL, key, iv);
  23. - EVP_CIPHER_CTX_set_padding(ctx, 0);
  24. *bytes_written = 0;
  25. @@ -185,6 +178,14 @@ lanplus_encrypt_aes_cbc_128(const uint8_
  26. printbuf(input, input_length, "encrypting this data");
  27. }
  28. + ctx = EVP_CIPHER_CTX_new();
  29. + if (ctx == NULL) {
  30. + lprintf(LOG_DEBUG, "ERROR: EVP_CIPHER_CTX_new() failed");
  31. + return;
  32. + }
  33. + EVP_CIPHER_CTX_init(ctx);
  34. + EVP_EncryptInit_ex(ctx, EVP_aes_128_cbc(), NULL, key, iv);
  35. + EVP_CIPHER_CTX_set_padding(ctx, 0);
  36. /*
  37. * The default implementation adds a whole block of padding if the input
  38. @@ -198,7 +199,6 @@ lanplus_encrypt_aes_cbc_128(const uint8_
  39. {
  40. /* Error */
  41. *bytes_written = 0;
  42. - return;
  43. }
  44. else
  45. {
  46. @@ -206,16 +206,17 @@ lanplus_encrypt_aes_cbc_128(const uint8_
  47. if(!EVP_EncryptFinal_ex(ctx, output + *bytes_written, (int *)&tmplen))
  48. {
  49. + /* Error */
  50. *bytes_written = 0;
  51. - return; /* Error */
  52. }
  53. else
  54. {
  55. /* Success */
  56. *bytes_written += tmplen;
  57. - EVP_CIPHER_CTX_cleanup(ctx);
  58. }
  59. }
  60. + /* performs cleanup and free */
  61. + EVP_CIPHER_CTX_free(ctx);
  62. }
  63. @@ -243,13 +244,6 @@ lanplus_decrypt_aes_cbc_128(const uint8_
  64. uint32_t * bytes_written)
  65. {
  66. EVP_CIPHER_CTX *ctx = NULL;
  67. - ctx = EVP_CIPHER_CTX_new();
  68. - if (ctx == NULL) {
  69. - *bytes_written = 0;
  70. - return;
  71. - }
  72. - EVP_DecryptInit_ex(ctx, EVP_aes_128_cbc(), NULL, key, iv);
  73. - EVP_CIPHER_CTX_set_padding(ctx, 0);
  74. if (verbose >= 5)
  75. {
  76. @@ -258,12 +252,20 @@ lanplus_decrypt_aes_cbc_128(const uint8_
  77. printbuf(input, input_length, "decrypting this data");
  78. }
  79. -
  80. *bytes_written = 0;
  81. if (input_length == 0)
  82. return;
  83. + ctx = EVP_CIPHER_CTX_new();
  84. + if (ctx == NULL) {
  85. + lprintf(LOG_DEBUG, "ERROR: EVP_CIPHER_CTX_new() failed");
  86. + return;
  87. + }
  88. + EVP_CIPHER_CTX_init(ctx);
  89. + EVP_DecryptInit_ex(ctx, EVP_aes_128_cbc(), NULL, key, iv);
  90. + EVP_CIPHER_CTX_set_padding(ctx, 0);
  91. +
  92. /*
  93. * The default implementation adds a whole block of padding if the input
  94. * data is perfectly aligned. We would like to keep that from happening.
  95. @@ -277,7 +279,6 @@ lanplus_decrypt_aes_cbc_128(const uint8_
  96. /* Error */
  97. lprintf(LOG_DEBUG, "ERROR: decrypt update failed");
  98. *bytes_written = 0;
  99. - return;
  100. }
  101. else
  102. {
  103. @@ -285,20 +286,21 @@ lanplus_decrypt_aes_cbc_128(const uint8_
  104. if (!EVP_DecryptFinal_ex(ctx, output + *bytes_written, (int *)&tmplen))
  105. {
  106. + /* Error */
  107. char buffer[1000];
  108. ERR_error_string(ERR_get_error(), buffer);
  109. lprintf(LOG_DEBUG, "the ERR error %s", buffer);
  110. lprintf(LOG_DEBUG, "ERROR: decrypt final failed");
  111. *bytes_written = 0;
  112. - return; /* Error */
  113. }
  114. else
  115. {
  116. /* Success */
  117. *bytes_written += tmplen;
  118. - EVP_CIPHER_CTX_cleanup(ctx);
  119. }
  120. }
  121. + /* performs cleanup and free */
  122. + EVP_CIPHER_CTX_free(ctx);
  123. if (verbose >= 5)
  124. {