de0490248bb7e5c608e4bdfe6a1d785495f979f9
[openwrt/openwrt.git] / tools / cmake / patches / 140-upstream-libarchive-openssl-1.1.x-support.patch
1 From 6f23daea4391c2db8bc27d2e4cb42eac02368822 Mon Sep 17 00:00:00 2001
2 From: Brad King <brad.king@kitware.com>
3 Date: Thu, 17 Nov 2016 15:44:44 -0500
4 Subject: [PATCH] libarchive: Add support for building with OpenSSL 1.1
5
6 OpenSSL 1.1 made some CTX structures opaque. Port our code to use the
7 structures only through pointers via OpenSSL 1.1 APIs. Use our adaption
8 layer to make this work with OpenSSL 1.0 and below.
9
10 Patch-by: Tomas Mraz <tmraz@redhat.com>
11 Patch-from: https://bugzilla.redhat.com/1383744
12 ---
13 Utilities/cmlibarchive/libarchive/archive_cryptor.c | 9 +++++----
14 Utilities/cmlibarchive/libarchive/archive_cryptor_private.h | 2 +-
15 Utilities/cmlibarchive/libarchive/archive_digest.c | 74 ++++++++++++++++++++++++++++++++++++++++++++++++++++++--------------------
16 Utilities/cmlibarchive/libarchive/archive_digest_private.h | 12 ++++++------
17 Utilities/cmlibarchive/libarchive/archive_hmac.c | 14 ++++++++------
18 Utilities/cmlibarchive/libarchive/archive_hmac_private.h | 2 +-
19 6 files changed, 75 insertions(+), 38 deletions(-)
20
21 --- a/Utilities/cmlibarchive/libarchive/archive_cryptor.c
22 +++ b/Utilities/cmlibarchive/libarchive/archive_cryptor.c
23 @@ -302,6 +302,7 @@ aes_ctr_release(archive_crypto_ctx *ctx)
24 static int
25 aes_ctr_init(archive_crypto_ctx *ctx, const uint8_t *key, size_t key_len)
26 {
27 + ctx->ctx = EVP_CIPHER_CTX_new();
28
29 switch (key_len) {
30 case 16: ctx->type = EVP_aes_128_ecb(); break;
31 @@ -314,7 +315,7 @@ aes_ctr_init(archive_crypto_ctx *ctx, co
32 memcpy(ctx->key, key, key_len);
33 memset(ctx->nonce, 0, sizeof(ctx->nonce));
34 ctx->encr_pos = AES_BLOCK_SIZE;
35 - EVP_CIPHER_CTX_init(&ctx->ctx);
36 + EVP_CIPHER_CTX_init(ctx->ctx);
37 return 0;
38 }
39
40 @@ -324,10 +325,10 @@ aes_ctr_encrypt_counter(archive_crypto_c
41 int outl = 0;
42 int r;
43
44 - r = EVP_EncryptInit_ex(&ctx->ctx, ctx->type, NULL, ctx->key, NULL);
45 + r = EVP_EncryptInit_ex(ctx->ctx, ctx->type, NULL, ctx->key, NULL);
46 if (r == 0)
47 return -1;
48 - r = EVP_EncryptUpdate(&ctx->ctx, ctx->encr_buf, &outl, ctx->nonce,
49 + r = EVP_EncryptUpdate(ctx->ctx, ctx->encr_buf, &outl, ctx->nonce,
50 AES_BLOCK_SIZE);
51 if (r == 0 || outl != AES_BLOCK_SIZE)
52 return -1;
53 @@ -337,7 +338,7 @@ aes_ctr_encrypt_counter(archive_crypto_c
54 static int
55 aes_ctr_release(archive_crypto_ctx *ctx)
56 {
57 - EVP_CIPHER_CTX_cleanup(&ctx->ctx);
58 + EVP_CIPHER_CTX_free(ctx->ctx);
59 memset(ctx->key, 0, ctx->key_len);
60 memset(ctx->nonce, 0, sizeof(ctx->nonce));
61 return 0;
62 --- a/Utilities/cmlibarchive/libarchive/archive_cryptor_private.h
63 +++ b/Utilities/cmlibarchive/libarchive/archive_cryptor_private.h
64 @@ -104,7 +104,7 @@ typedef struct {
65 #define AES_MAX_KEY_SIZE 32
66
67 typedef struct {
68 - EVP_CIPHER_CTX ctx;
69 + EVP_CIPHER_CTX *ctx;
70 const EVP_CIPHER *type;
71 uint8_t key[AES_MAX_KEY_SIZE];
72 unsigned key_len;
73 --- a/Utilities/cmlibarchive/libarchive/archive_digest.c
74 +++ b/Utilities/cmlibarchive/libarchive/archive_digest.c
75 @@ -207,7 +207,9 @@ __archive_nettle_md5final(archive_md5_ct
76 static int
77 __archive_openssl_md5init(archive_md5_ctx *ctx)
78 {
79 - EVP_DigestInit(ctx, EVP_md5());
80 + if ((*ctx = EVP_MD_CTX_new()) == NULL)
81 + return (ARCHIVE_FAILED);
82 + EVP_DigestInit(*ctx, EVP_md5());
83 return (ARCHIVE_OK);
84 }
85
86 @@ -215,7 +217,7 @@ static int
87 __archive_openssl_md5update(archive_md5_ctx *ctx, const void *indata,
88 size_t insize)
89 {
90 - EVP_DigestUpdate(ctx, indata, insize);
91 + EVP_DigestUpdate(*ctx, indata, insize);
92 return (ARCHIVE_OK);
93 }
94
95 @@ -226,8 +228,11 @@ __archive_openssl_md5final(archive_md5_c
96 * this is meant to cope with that. Real fix is probably to fix
97 * archive_write_set_format_xar.c
98 */
99 - if (ctx->digest)
100 - EVP_DigestFinal(ctx, md, NULL);
101 + if (*ctx) {
102 + EVP_DigestFinal(*ctx, md, NULL);
103 + EVP_MD_CTX_free(*ctx);
104 + *ctx = NULL;
105 + }
106 return (ARCHIVE_OK);
107 }
108
109 @@ -359,7 +364,9 @@ __archive_nettle_ripemd160final(archive_
110 static int
111 __archive_openssl_ripemd160init(archive_rmd160_ctx *ctx)
112 {
113 - EVP_DigestInit(ctx, EVP_ripemd160());
114 + if ((*ctx = EVP_MD_CTX_new()) == NULL)
115 + return (ARCHIVE_FAILED);
116 + EVP_DigestInit(*ctx, EVP_ripemd160());
117 return (ARCHIVE_OK);
118 }
119
120 @@ -367,14 +374,18 @@ static int
121 __archive_openssl_ripemd160update(archive_rmd160_ctx *ctx, const void *indata,
122 size_t insize)
123 {
124 - EVP_DigestUpdate(ctx, indata, insize);
125 + EVP_DigestUpdate(*ctx, indata, insize);
126 return (ARCHIVE_OK);
127 }
128
129 static int
130 __archive_openssl_ripemd160final(archive_rmd160_ctx *ctx, void *md)
131 {
132 - EVP_DigestFinal(ctx, md, NULL);
133 + if (*ctx) {
134 + EVP_DigestFinal(*ctx, md, NULL);
135 + EVP_MD_CTX_free(*ctx);
136 + *ctx = NULL;
137 + }
138 return (ARCHIVE_OK);
139 }
140
141 @@ -509,7 +520,9 @@ __archive_nettle_sha1final(archive_sha1_
142 static int
143 __archive_openssl_sha1init(archive_sha1_ctx *ctx)
144 {
145 - EVP_DigestInit(ctx, EVP_sha1());
146 + if ((*ctx = EVP_MD_CTX_new()) == NULL)
147 + return (ARCHIVE_FAILED);
148 + EVP_DigestInit(*ctx, EVP_sha1());
149 return (ARCHIVE_OK);
150 }
151
152 @@ -517,7 +530,7 @@ static int
153 __archive_openssl_sha1update(archive_sha1_ctx *ctx, const void *indata,
154 size_t insize)
155 {
156 - EVP_DigestUpdate(ctx, indata, insize);
157 + EVP_DigestUpdate(*ctx, indata, insize);
158 return (ARCHIVE_OK);
159 }
160
161 @@ -528,8 +541,11 @@ __archive_openssl_sha1final(archive_sha1
162 * this is meant to cope with that. Real fix is probably to fix
163 * archive_write_set_format_xar.c
164 */
165 - if (ctx->digest)
166 - EVP_DigestFinal(ctx, md, NULL);
167 + if (*ctx) {
168 + EVP_DigestFinal(*ctx, md, NULL);
169 + EVP_MD_CTX_free(*ctx);
170 + *ctx = NULL;
171 + }
172 return (ARCHIVE_OK);
173 }
174
175 @@ -733,7 +749,9 @@ __archive_nettle_sha256final(archive_sha
176 static int
177 __archive_openssl_sha256init(archive_sha256_ctx *ctx)
178 {
179 - EVP_DigestInit(ctx, EVP_sha256());
180 + if ((*ctx = EVP_MD_CTX_new()) == NULL)
181 + return (ARCHIVE_FAILED);
182 + EVP_DigestInit(*ctx, EVP_sha256());
183 return (ARCHIVE_OK);
184 }
185
186 @@ -741,14 +759,18 @@ static int
187 __archive_openssl_sha256update(archive_sha256_ctx *ctx, const void *indata,
188 size_t insize)
189 {
190 - EVP_DigestUpdate(ctx, indata, insize);
191 + EVP_DigestUpdate(*ctx, indata, insize);
192 return (ARCHIVE_OK);
193 }
194
195 static int
196 __archive_openssl_sha256final(archive_sha256_ctx *ctx, void *md)
197 {
198 - EVP_DigestFinal(ctx, md, NULL);
199 + if (*ctx) {
200 + EVP_DigestFinal(*ctx, md, NULL);
201 + EVP_MD_CTX_free(*ctx);
202 + *ctx = NULL;
203 + }
204 return (ARCHIVE_OK);
205 }
206
207 @@ -928,7 +950,9 @@ __archive_nettle_sha384final(archive_sha
208 static int
209 __archive_openssl_sha384init(archive_sha384_ctx *ctx)
210 {
211 - EVP_DigestInit(ctx, EVP_sha384());
212 + if ((*ctx = EVP_MD_CTX_new()) == NULL)
213 + return (ARCHIVE_FAILED);
214 + EVP_DigestInit(*ctx, EVP_sha384());
215 return (ARCHIVE_OK);
216 }
217
218 @@ -936,14 +960,18 @@ static int
219 __archive_openssl_sha384update(archive_sha384_ctx *ctx, const void *indata,
220 size_t insize)
221 {
222 - EVP_DigestUpdate(ctx, indata, insize);
223 + EVP_DigestUpdate(*ctx, indata, insize);
224 return (ARCHIVE_OK);
225 }
226
227 static int
228 __archive_openssl_sha384final(archive_sha384_ctx *ctx, void *md)
229 {
230 - EVP_DigestFinal(ctx, md, NULL);
231 + if (*ctx) {
232 + EVP_DigestFinal(*ctx, md, NULL);
233 + EVP_MD_CTX_free(*ctx);
234 + *ctx = NULL;
235 + }
236 return (ARCHIVE_OK);
237 }
238
239 @@ -1147,7 +1175,9 @@ __archive_nettle_sha512final(archive_sha
240 static int
241 __archive_openssl_sha512init(archive_sha512_ctx *ctx)
242 {
243 - EVP_DigestInit(ctx, EVP_sha512());
244 + if ((*ctx = EVP_MD_CTX_new()) == NULL)
245 + return (ARCHIVE_FAILED);
246 + EVP_DigestInit(*ctx, EVP_sha512());
247 return (ARCHIVE_OK);
248 }
249
250 @@ -1155,14 +1185,18 @@ static int
251 __archive_openssl_sha512update(archive_sha512_ctx *ctx, const void *indata,
252 size_t insize)
253 {
254 - EVP_DigestUpdate(ctx, indata, insize);
255 + EVP_DigestUpdate(*ctx, indata, insize);
256 return (ARCHIVE_OK);
257 }
258
259 static int
260 __archive_openssl_sha512final(archive_sha512_ctx *ctx, void *md)
261 {
262 - EVP_DigestFinal(ctx, md, NULL);
263 + if (*ctx) {
264 + EVP_DigestFinal(*ctx, md, NULL);
265 + EVP_MD_CTX_free(*ctx);
266 + *ctx = NULL;
267 + }
268 return (ARCHIVE_OK);
269 }
270
271 --- a/Utilities/cmlibarchive/libarchive/archive_digest_private.h
272 +++ b/Utilities/cmlibarchive/libarchive/archive_digest_private.h
273 @@ -161,7 +161,7 @@ typedef CC_MD5_CTX archive_md5_ctx;
274 #elif defined(ARCHIVE_CRYPTO_MD5_NETTLE)
275 typedef struct md5_ctx archive_md5_ctx;
276 #elif defined(ARCHIVE_CRYPTO_MD5_OPENSSL)
277 -typedef EVP_MD_CTX archive_md5_ctx;
278 +typedef EVP_MD_CTX *archive_md5_ctx;
279 #elif defined(ARCHIVE_CRYPTO_MD5_WIN)
280 typedef Digest_CTX archive_md5_ctx;
281 #else
282 @@ -175,7 +175,7 @@ typedef RIPEMD160_CTX archive_rmd160_ctx
283 #elif defined(ARCHIVE_CRYPTO_RMD160_NETTLE)
284 typedef struct ripemd160_ctx archive_rmd160_ctx;
285 #elif defined(ARCHIVE_CRYPTO_RMD160_OPENSSL)
286 -typedef EVP_MD_CTX archive_rmd160_ctx;
287 +typedef EVP_MD_CTX *archive_rmd160_ctx;
288 #else
289 typedef unsigned char archive_rmd160_ctx;
290 #endif
291 @@ -189,7 +189,7 @@ typedef CC_SHA1_CTX archive_sha1_ctx;
292 #elif defined(ARCHIVE_CRYPTO_SHA1_NETTLE)
293 typedef struct sha1_ctx archive_sha1_ctx;
294 #elif defined(ARCHIVE_CRYPTO_SHA1_OPENSSL)
295 -typedef EVP_MD_CTX archive_sha1_ctx;
296 +typedef EVP_MD_CTX *archive_sha1_ctx;
297 #elif defined(ARCHIVE_CRYPTO_SHA1_WIN)
298 typedef Digest_CTX archive_sha1_ctx;
299 #else
300 @@ -209,7 +209,7 @@ typedef CC_SHA256_CTX archive_sha256_ctx
301 #elif defined(ARCHIVE_CRYPTO_SHA256_NETTLE)
302 typedef struct sha256_ctx archive_sha256_ctx;
303 #elif defined(ARCHIVE_CRYPTO_SHA256_OPENSSL)
304 -typedef EVP_MD_CTX archive_sha256_ctx;
305 +typedef EVP_MD_CTX *archive_sha256_ctx;
306 #elif defined(ARCHIVE_CRYPTO_SHA256_WIN)
307 typedef Digest_CTX archive_sha256_ctx;
308 #else
309 @@ -227,7 +227,7 @@ typedef CC_SHA512_CTX archive_sha384_ctx
310 #elif defined(ARCHIVE_CRYPTO_SHA384_NETTLE)
311 typedef struct sha384_ctx archive_sha384_ctx;
312 #elif defined(ARCHIVE_CRYPTO_SHA384_OPENSSL)
313 -typedef EVP_MD_CTX archive_sha384_ctx;
314 +typedef EVP_MD_CTX *archive_sha384_ctx;
315 #elif defined(ARCHIVE_CRYPTO_SHA384_WIN)
316 typedef Digest_CTX archive_sha384_ctx;
317 #else
318 @@ -247,7 +247,7 @@ typedef CC_SHA512_CTX archive_sha512_ctx
319 #elif defined(ARCHIVE_CRYPTO_SHA512_NETTLE)
320 typedef struct sha512_ctx archive_sha512_ctx;
321 #elif defined(ARCHIVE_CRYPTO_SHA512_OPENSSL)
322 -typedef EVP_MD_CTX archive_sha512_ctx;
323 +typedef EVP_MD_CTX *archive_sha512_ctx;
324 #elif defined(ARCHIVE_CRYPTO_SHA512_WIN)
325 typedef Digest_CTX archive_sha512_ctx;
326 #else
327 --- a/Utilities/cmlibarchive/libarchive/archive_hmac.c
328 +++ b/Utilities/cmlibarchive/libarchive/archive_hmac.c
329 @@ -176,8 +176,10 @@ __hmac_sha1_cleanup(archive_hmac_sha1_ct
330 static int
331 __hmac_sha1_init(archive_hmac_sha1_ctx *ctx, const uint8_t *key, size_t key_len)
332 {
333 - HMAC_CTX_init(ctx);
334 - HMAC_Init(ctx, key, key_len, EVP_sha1());
335 + *ctx = HMAC_CTX_new();
336 + if (*ctx == NULL)
337 + return -1;
338 + HMAC_Init_ex(*ctx, key, key_len, EVP_sha1(), NULL);
339 return 0;
340 }
341
342 @@ -185,22 +187,22 @@ static void
343 __hmac_sha1_update(archive_hmac_sha1_ctx *ctx, const uint8_t *data,
344 size_t data_len)
345 {
346 - HMAC_Update(ctx, data, data_len);
347 + HMAC_Update(*ctx, data, data_len);
348 }
349
350 static void
351 __hmac_sha1_final(archive_hmac_sha1_ctx *ctx, uint8_t *out, size_t *out_len)
352 {
353 unsigned int len = (unsigned int)*out_len;
354 - HMAC_Final(ctx, out, &len);
355 + HMAC_Final(*ctx, out, &len);
356 *out_len = len;
357 }
358
359 static void
360 __hmac_sha1_cleanup(archive_hmac_sha1_ctx *ctx)
361 {
362 - HMAC_CTX_cleanup(ctx);
363 - memset(ctx, 0, sizeof(*ctx));
364 + HMAC_CTX_free(*ctx);
365 + *ctx = NULL;
366 }
367
368 #else
369 --- a/Utilities/cmlibarchive/libarchive/archive_hmac_private.h
370 +++ b/Utilities/cmlibarchive/libarchive/archive_hmac_private.h
371 @@ -72,7 +72,7 @@ typedef struct hmac_sha1_ctx archive_hma
372 #elif defined(HAVE_LIBCRYPTO)
373 #include "archive_openssl_hmac_private.h"
374
375 -typedef HMAC_CTX archive_hmac_sha1_ctx;
376 +typedef HMAC_CTX* archive_hmac_sha1_ctx;
377
378 #else
379