summarylogtreecommitdiffstats
path: root/openssl1.1.patch
blob: 41db4e064c3a130e7fc1ffb52ad575ce66a71cbd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
From 0dbc7bdbe1ad3b42fed52d2a326db6fa40204a06 Mon Sep 17 00:00:00 2001
From: Derek Konigsberg <dkonigsberg@whatsapp.com>
Date: Thu, 4 May 2017 09:22:35 -0700
Subject: [PATCH] Added support for building against OpenSSL 1.1

OpenSSL 1.1 introduced some minor API changes in how certain context
objects could be created. This update uses preprocessor macros to adapt
to those changes, while preserving backwards compatibility.

Referencing #66
---
 tests/test_common_openssl.c | 79 +++++++++++++++++++++++++++++++++++++--------
 1 file changed, 65 insertions(+), 14 deletions(-)

diff --git a/tests/test_common_openssl.c b/tests/test_common_openssl.c
index da3fb87..90d4b0c 100644
--- a/tests/test_common_openssl.c
+++ b/tests/test_common_openssl.c
@@ -1,5 +1,6 @@
 #include "test_common.h"
 
+#include <openssl/opensslv.h>
 #include <openssl/evp.h>
 #include <openssl/hmac.h>
 #include <openssl/rand.h>
@@ -17,11 +18,19 @@ int test_random_generator(uint8_t *data, size_t len, void *user_data)
 
 int test_hmac_sha256_init(void **hmac_context, const uint8_t *key, size_t key_len, void *user_data)
 {
+#if OPENSSL_VERSION_NUMBER >= 0x1010000fL
+    HMAC_CTX *ctx = HMAC_CTX_new();
+    if(!ctx) {
+        return SG_ERR_NOMEM;
+    }
+#else
     HMAC_CTX *ctx = malloc(sizeof(HMAC_CTX));
     if(!ctx) {
         return SG_ERR_NOMEM;
     }
     HMAC_CTX_init(ctx);
+#endif
+
     *hmac_context = ctx;
 
     if(HMAC_Init_ex(ctx, key, key_len, EVP_sha256(), 0) != 1) {
@@ -65,8 +74,12 @@ void test_hmac_sha256_cleanup(void *hmac_context, void *user_data)
 {
     if(hmac_context) {
         HMAC_CTX *ctx = hmac_context;
+#if OPENSSL_VERSION_NUMBER >= 0x1010000fL
+        HMAC_CTX_free(ctx);
+#else
         HMAC_CTX_cleanup(ctx);
         free(ctx);
+#endif
     }
 }
 
@@ -188,6 +201,7 @@ int test_encrypt(signal_buffer **output,
         void *user_data)
 {
     int result = 0;
+    EVP_CIPHER_CTX *ctx = 0;
     uint8_t *out_buf = 0;
 
     const EVP_CIPHER *evp_cipher = aes_cipher(cipher, key_len);
@@ -206,10 +220,22 @@ int test_encrypt(signal_buffer **output,
         return SG_ERR_UNKNOWN;
     }
 
-    EVP_CIPHER_CTX ctx;
-    EVP_CIPHER_CTX_init(&ctx);
+#if OPENSSL_VERSION_NUMBER >= 0x1010000fL
+    ctx = EVP_CIPHER_CTX_new();
+    if(!ctx) {
+        result = SG_ERR_NOMEM;
+        goto complete;
+    }
+#else
+    ctx = malloc(sizeof(EVP_CIPHER_CTX));
+    if(!ctx) {
+        result = SG_ERR_NOMEM;
+        goto complete;
+    }
+    EVP_CIPHER_CTX_init(ctx);
+#endif
 
-    result = EVP_EncryptInit_ex(&ctx, evp_cipher, 0, key, iv);
+    result = EVP_EncryptInit_ex(ctx, evp_cipher, 0, key, iv);
     if(!result) {
         fprintf(stderr, "cannot initialize cipher\n");
         result = SG_ERR_UNKNOWN;
@@ -217,7 +243,7 @@ int test_encrypt(signal_buffer **output,
     }
 
     if(cipher == SG_CIPHER_AES_CTR_NOPADDING) {
-        result = EVP_CIPHER_CTX_set_padding(&ctx, 0);
+        result = EVP_CIPHER_CTX_set_padding(ctx, 0);
         if(!result) {
             fprintf(stderr, "cannot set padding\n");
             result = SG_ERR_UNKNOWN;
@@ -233,7 +259,7 @@ int test_encrypt(signal_buffer **output,
     }
 
     int out_len = 0;
-    result = EVP_EncryptUpdate(&ctx,
+    result = EVP_EncryptUpdate(ctx,
         out_buf, &out_len, plaintext, plaintext_len);
     if(!result) {
         fprintf(stderr, "cannot encrypt plaintext\n");
@@ -242,7 +268,7 @@ int test_encrypt(signal_buffer **output,
     }
 
     int final_len = 0;
-    result = EVP_EncryptFinal_ex(&ctx, out_buf + out_len, &final_len);
+    result = EVP_EncryptFinal_ex(ctx, out_buf + out_len, &final_len);
     if(!result) {
         fprintf(stderr, "cannot finish encrypting plaintext\n");
         result = SG_ERR_UNKNOWN;
@@ -252,7 +278,13 @@ int test_encrypt(signal_buffer **output,
     *output = signal_buffer_create(out_buf, out_len + final_len);
 
 complete:
-    EVP_CIPHER_CTX_cleanup(&ctx);
+    if(ctx) {
+#if OPENSSL_VERSION_NUMBER >= 0x1010000fL
+        EVP_CIPHER_CTX_free(ctx);
+#else
+        free(ctx);
+#endif
+    }
     if(out_buf) {
         free(out_buf);
     }
@@ -267,6 +299,7 @@ int test_decrypt(signal_buffer **output,
         void *user_data)
 {
     int result = 0;
+    EVP_CIPHER_CTX *ctx = 0;
     uint8_t *out_buf = 0;
 
     const EVP_CIPHER *evp_cipher = aes_cipher(cipher, key_len);
@@ -285,10 +318,22 @@ int test_decrypt(signal_buffer **output,
         return SG_ERR_UNKNOWN;
     }
 
-    EVP_CIPHER_CTX ctx;
-    EVP_CIPHER_CTX_init(&ctx);
+#if OPENSSL_VERSION_NUMBER >= 0x1010000fL
+    ctx = EVP_CIPHER_CTX_new();
+    if(!ctx) {
+        result = SG_ERR_NOMEM;
+        goto complete;
+    }
+#else
+    ctx = malloc(sizeof(EVP_CIPHER_CTX));
+    if(!ctx) {
+        result = SG_ERR_NOMEM;
+        goto complete;
+    }
+    EVP_CIPHER_CTX_init(ctx);
+#endif
 
-    result = EVP_DecryptInit_ex(&ctx, evp_cipher, 0, key, iv);
+    result = EVP_DecryptInit_ex(ctx, evp_cipher, 0, key, iv);
     if(!result) {
         fprintf(stderr, "cannot initialize cipher\n");
         result = SG_ERR_UNKNOWN;
@@ -296,7 +341,7 @@ int test_decrypt(signal_buffer **output,
     }
 
     if(cipher == SG_CIPHER_AES_CTR_NOPADDING) {
-        result = EVP_CIPHER_CTX_set_padding(&ctx, 0);
+        result = EVP_CIPHER_CTX_set_padding(ctx, 0);
         if(!result) {
             fprintf(stderr, "cannot set padding\n");
             result = SG_ERR_UNKNOWN;
@@ -312,7 +357,7 @@ int test_decrypt(signal_buffer **output,
     }
 
     int out_len = 0;
-    result = EVP_DecryptUpdate(&ctx,
+    result = EVP_DecryptUpdate(ctx,
         out_buf, &out_len, ciphertext, ciphertext_len);
     if(!result) {
         fprintf(stderr, "cannot decrypt ciphertext\n");
@@ -321,7 +366,7 @@ int test_decrypt(signal_buffer **output,
     }
 
     int final_len = 0;
-    result = EVP_DecryptFinal_ex(&ctx, out_buf + out_len, &final_len);
+    result = EVP_DecryptFinal_ex(ctx, out_buf + out_len, &final_len);
     if(!result) {
         fprintf(stderr, "cannot finish decrypting ciphertext\n");
         result = SG_ERR_UNKNOWN;
@@ -331,7 +376,13 @@ int test_decrypt(signal_buffer **output,
     *output = signal_buffer_create(out_buf, out_len + final_len);
 
 complete:
-    EVP_CIPHER_CTX_cleanup(&ctx);
+    if(ctx) {
+#if OPENSSL_VERSION_NUMBER >= 0x1010000fL
+        EVP_CIPHER_CTX_free(ctx);
+#else
+        free(ctx);
+#endif
+    }
     if(out_buf) {
         free(out_buf);
     }