0001-openssl-1.1-compatibility.patch 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. From f16f247d30f868e84f31e24792b4464488f1c009 Mon Sep 17 00:00:00 2001
  2. From: Peter Wu <peter@lekensteyn.nl>
  3. Date: Tue, 2 May 2017 15:53:38 +0200
  4. Subject: [PATCH] vfdecrypt: OpenSSL 1.1 compatibility
  5. Allocate contexts from the heap on all OpenSSL versions, this is needed
  6. since OpenSSL 1.1.0. No attempt is done at addressing issues like global
  7. variables and fixing potential memleaks on error paths.
  8. Compile-tested only with OpenSSL 1.1.0e (Arch Linux) and OpenSSL 1.0.2g
  9. (Ubuntu 16.04), I have no test file.
  10. Fixes https://github.com/Lekensteyn/dmg2img/issues/4
  11. ---
  12. vfdecrypt.c | 103 ++++++++++++++++++++++++++++++++++++++++++++++--------------
  13. 1 file changed, 80 insertions(+), 23 deletions(-)
  14. diff --git a/vfdecrypt.c b/vfdecrypt.c
  15. index 56d3530..b1a36d3 100644
  16. --- a/vfdecrypt.c
  17. +++ b/vfdecrypt.c
  18. @@ -183,7 +183,7 @@ void adjust_v2_header_byteorder(cencrypted_v2_pwheader *pwhdr) {
  19. pwhdr->encrypted_keyblob_size = htonl(pwhdr->encrypted_keyblob_size);
  20. }
  21. -HMAC_CTX hmacsha1_ctx;
  22. +HMAC_CTX *hmacsha1_ctx;
  23. AES_KEY aes_decrypt_key;
  24. int CHUNK_SIZE=4096; // default
  25. @@ -196,9 +196,9 @@ void compute_iv(uint32_t chunk_no, uint8_t *iv) {
  26. unsigned int mdLen;
  27. chunk_no = OSSwapHostToBigInt32(chunk_no);
  28. - HMAC_Init_ex(&hmacsha1_ctx, NULL, 0, NULL, NULL);
  29. - HMAC_Update(&hmacsha1_ctx, (void *) &chunk_no, sizeof(uint32_t));
  30. - HMAC_Final(&hmacsha1_ctx, mdResult, &mdLen);
  31. + HMAC_Init_ex(hmacsha1_ctx, NULL, 0, NULL, NULL);
  32. + HMAC_Update(hmacsha1_ctx, (void *) &chunk_no, sizeof(uint32_t));
  33. + HMAC_Final(hmacsha1_ctx, mdResult, &mdLen);
  34. memcpy(iv, mdResult, CIPHER_BLOCKSIZE);
  35. }
  36. @@ -212,52 +212,75 @@ void decrypt_chunk(uint8_t *ctext, uint8_t *ptext, uint32_t chunk_no) {
  37. /* DES3-EDE unwrap operation loosely based on to RFC 2630, section 12.6
  38. * wrapped_key has to be 40 bytes in length. */
  39. int apple_des3_ede_unwrap_key(uint8_t *wrapped_key, int wrapped_key_len, uint8_t *decryptKey, uint8_t *unwrapped_key) {
  40. - EVP_CIPHER_CTX ctx;
  41. + EVP_CIPHER_CTX *ctx;
  42. uint8_t *TEMP1, *TEMP2, *CEKICV;
  43. uint8_t IV[8] = { 0x4a, 0xdd, 0xa2, 0x2c, 0x79, 0xe8, 0x21, 0x05 };
  44. int outlen, tmplen, i;
  45. - EVP_CIPHER_CTX_init(&ctx);
  46. +#if OPENSSL_VERSION_NUMBER >= 0x10100000L
  47. + ctx = EVP_CIPHER_CTX_new();
  48. +#else
  49. + ctx = malloc(sizeof(*ctx));
  50. +#endif
  51. + if (!ctx) {
  52. + fprintf(stderr, "Out of memory: EVP_CIPHER_CTX!\n");
  53. + return(-1);
  54. + }
  55. +
  56. + EVP_CIPHER_CTX_init(ctx);
  57. /* result of the decryption operation shouldn't be bigger than ciphertext */
  58. TEMP1 = malloc(wrapped_key_len);
  59. TEMP2 = malloc(wrapped_key_len);
  60. CEKICV = malloc(wrapped_key_len);
  61. /* uses PKCS#7 padding for symmetric key operations by default */
  62. - EVP_DecryptInit_ex(&ctx, EVP_des_ede3_cbc(), NULL, decryptKey, IV);
  63. + EVP_DecryptInit_ex(ctx, EVP_des_ede3_cbc(), NULL, decryptKey, IV);
  64. - if(!EVP_DecryptUpdate(&ctx, TEMP1, &outlen, wrapped_key, wrapped_key_len)) {
  65. + if(!EVP_DecryptUpdate(ctx, TEMP1, &outlen, wrapped_key, wrapped_key_len)) {
  66. fprintf(stderr, "internal error (1) during key unwrap operation!\n");
  67. return(-1);
  68. }
  69. - if(!EVP_DecryptFinal_ex(&ctx, TEMP1 + outlen, &tmplen)) {
  70. + if(!EVP_DecryptFinal_ex(ctx, TEMP1 + outlen, &tmplen)) {
  71. fprintf(stderr, "internal error (2) during key unwrap operation!\n");
  72. return(-1);
  73. }
  74. outlen += tmplen;
  75. - EVP_CIPHER_CTX_cleanup(&ctx);
  76. +#if OPENSSL_VERSION_NUMBER >= 0x10100000L
  77. + EVP_CIPHER_CTX_reset(ctx);
  78. +#else
  79. + EVP_CIPHER_CTX_cleanup(ctx);
  80. +#endif
  81. /* reverse order of TEMP3 */
  82. for(i = 0; i < outlen; i++) TEMP2[i] = TEMP1[outlen - i - 1];
  83. - EVP_CIPHER_CTX_init(&ctx);
  84. + EVP_CIPHER_CTX_init(ctx);
  85. /* uses PKCS#7 padding for symmetric key operations by default */
  86. - EVP_DecryptInit_ex(&ctx, EVP_des_ede3_cbc(), NULL, decryptKey, TEMP2);
  87. - if(!EVP_DecryptUpdate(&ctx, CEKICV, &outlen, TEMP2+8, outlen-8)) {
  88. + EVP_DecryptInit_ex(ctx, EVP_des_ede3_cbc(), NULL, decryptKey, TEMP2);
  89. + if(!EVP_DecryptUpdate(ctx, CEKICV, &outlen, TEMP2+8, outlen-8)) {
  90. fprintf(stderr, "internal error (3) during key unwrap operation!\n");
  91. return(-1);
  92. }
  93. - if(!EVP_DecryptFinal_ex(&ctx, CEKICV + outlen, &tmplen)) {
  94. + if(!EVP_DecryptFinal_ex(ctx, CEKICV + outlen, &tmplen)) {
  95. fprintf(stderr, "internal error (4) during key unwrap operation!\n");
  96. return(-1);
  97. }
  98. outlen += tmplen;
  99. - EVP_CIPHER_CTX_cleanup(&ctx);
  100. +#if OPENSSL_VERSION_NUMBER >= 0x10100000L
  101. + EVP_CIPHER_CTX_reset(ctx);
  102. +#else
  103. + EVP_CIPHER_CTX_cleanup(ctx);
  104. +#endif
  105. memcpy(unwrapped_key, CEKICV+4, outlen-4);
  106. free(TEMP1);
  107. free(TEMP2);
  108. free(CEKICV);
  109. +#if OPENSSL_VERSION_NUMBER >= 0x10100000L
  110. + EVP_CIPHER_CTX_free(ctx);
  111. +#else
  112. + free(ctx);
  113. +#endif
  114. return(0);
  115. }
  116. @@ -279,31 +302,46 @@ int unwrap_v1_header(char *passphrase, cencrypted_v1_header *header, uint8_t *ae
  117. int unwrap_v2_header(char *passphrase, cencrypted_v2_pwheader *header, uint8_t *aes_key, uint8_t *hmacsha1_key) {
  118. /* derived key is a 3DES-EDE key */
  119. uint8_t derived_key[192/8];
  120. - EVP_CIPHER_CTX ctx;
  121. + EVP_CIPHER_CTX *ctx;
  122. uint8_t *TEMP1;
  123. int outlen, tmplen;
  124. +#if OPENSSL_VERSION_NUMBER >= 0x10100000L
  125. + ctx = EVP_CIPHER_CTX_new();
  126. +#else
  127. + ctx = malloc(sizeof(*ctx));
  128. +#endif
  129. + if (!ctx) {
  130. + fprintf(stderr, "Out of memory: EVP_CIPHER_CTX!\n");
  131. + return(-1);
  132. + }
  133. +
  134. PKCS5_PBKDF2_HMAC_SHA1(passphrase, strlen(passphrase), (unsigned char*)header->kdf_salt, 20,
  135. PBKDF2_ITERATION_COUNT, sizeof(derived_key), derived_key);
  136. print_hex(derived_key, 192/8);
  137. - EVP_CIPHER_CTX_init(&ctx);
  138. + EVP_CIPHER_CTX_init(ctx);
  139. /* result of the decryption operation shouldn't be bigger than ciphertext */
  140. TEMP1 = malloc(header->encrypted_keyblob_size);
  141. /* uses PKCS#7 padding for symmetric key operations by default */
  142. - EVP_DecryptInit_ex(&ctx, EVP_des_ede3_cbc(), NULL, derived_key, header->blob_enc_iv);
  143. + EVP_DecryptInit_ex(ctx, EVP_des_ede3_cbc(), NULL, derived_key, header->blob_enc_iv);
  144. - if(!EVP_DecryptUpdate(&ctx, TEMP1, &outlen, header->encrypted_keyblob, header->encrypted_keyblob_size)) {
  145. + if(!EVP_DecryptUpdate(ctx, TEMP1, &outlen, header->encrypted_keyblob, header->encrypted_keyblob_size)) {
  146. fprintf(stderr, "internal error (1) during key unwrap operation!\n");
  147. return(-1);
  148. }
  149. - if(!EVP_DecryptFinal_ex(&ctx, TEMP1 + outlen, &tmplen)) {
  150. + if(!EVP_DecryptFinal_ex(ctx, TEMP1 + outlen, &tmplen)) {
  151. fprintf(stderr, "internal error (2) during key unwrap operation!\n");
  152. return(-1);
  153. }
  154. outlen += tmplen;
  155. - EVP_CIPHER_CTX_cleanup(&ctx);
  156. +#if OPENSSL_VERSION_NUMBER >= 0x10100000L
  157. + EVP_CIPHER_CTX_free(ctx);
  158. +#else
  159. + EVP_CIPHER_CTX_cleanup(ctx);
  160. + free(ctx);
  161. +#endif
  162. memcpy(aes_key, TEMP1, 16);
  163. memcpy(hmacsha1_key, TEMP1, 20);
  164. @@ -446,8 +484,21 @@ int main(int argc, char *argv[]) {
  165. CHUNK_SIZE = v2header.blocksize;
  166. }
  167. - HMAC_CTX_init(&hmacsha1_ctx);
  168. - HMAC_Init_ex(&hmacsha1_ctx, hmacsha1_key, sizeof(hmacsha1_key), EVP_sha1(), NULL);
  169. +#if OPENSSL_VERSION_NUMBER >= 0x10100000L
  170. + hmacsha1_ctx = HMAC_CTX_new();
  171. +#else
  172. + hmacsha1_ctx = malloc(sizeof(*hmacsha1_ctx));
  173. +#endif
  174. + if (!hmacsha1_ctx) {
  175. + fprintf(stderr, "Out of memory: HMAC CTX!\n");
  176. + exit(1);
  177. + }
  178. +#if OPENSSL_VERSION_NUMBER >= 0x10100000L
  179. + HMAC_CTX_reset(hmacsha1_ctx);
  180. +#else
  181. + HMAC_CTX_init(hmacsha1_ctx);
  182. +#endif
  183. + HMAC_Init_ex(hmacsha1_ctx, hmacsha1_key, sizeof(hmacsha1_key), EVP_sha1(), NULL);
  184. AES_set_decrypt_key(aes_key, CIPHER_KEY_LENGTH * 8, &aes_decrypt_key);
  185. if (verbose >= 1) {
  186. @@ -472,5 +523,11 @@ int main(int argc, char *argv[]) {
  187. }
  188. if (verbose) fprintf(stderr, "%"PRIX32" chunks written\n", chunk_no);
  189. +#if OPENSSL_VERSION_NUMBER >= 0x10100000L
  190. + HMAC_CTX_free(hmacsha1_ctx);
  191. +#else
  192. + HMAC_CTX_cleanup(hmacsha1_ctx);
  193. + free(hmacsha1_ctx);
  194. +#endif
  195. return(0);
  196. }