summaryrefslogtreecommitdiff
path: root/src/lib/emile
diff options
context:
space:
mode:
authorMarcel Hollerbach <marcel-hollerbach@t-online.de>2016-12-05 11:13:02 +0100
committerMarcel Hollerbach <marcel-hollerbach@t-online.de>2016-12-05 11:54:53 +0100
commit07bb7ca2824221826d2a7568cea26cab1b5f3250 (patch)
tree9c81f2684a7304d535fd2f97c0ff8f61dcd68b95 /src/lib/emile
parent6076014f5815ee7d129aa38a1eb380d9262de77f (diff)
emile: do not rely on the struct size
Summary: in newer versions of openssl, most of the public structures are made opaque. So we should use the openssl api to allocate this structure. Since ctx is now a pointer we dont need opened anymore, so those vars are replaced with ctx checks. ctx is always NULL'ed after a free fix T4923 Reviewers: cedric, jpeg, stefan_schmidt Reviewed By: jpeg, stefan_schmidt Subscribers: stefan_schmidt, ManMower, jpeg Maniphest Tasks: T4923 Differential Revision: https://phab.enlightenment.org/D4456
Diffstat (limited to 'src/lib/emile')
-rw-r--r--src/lib/emile/emile_cipher_openssl.c37
1 files changed, 17 insertions, 20 deletions
diff --git a/src/lib/emile/emile_cipher_openssl.c b/src/lib/emile/emile_cipher_openssl.c
index 9963c22601..efd9324a5c 100644
--- a/src/lib/emile/emile_cipher_openssl.c
+++ b/src/lib/emile/emile_cipher_openssl.c
@@ -102,9 +102,8 @@ emile_binbuf_cipher(Emile_Cipher_Algorithm algo,
102 unsigned int salt; 102 unsigned int salt;
103 unsigned int tmp = 0; 103 unsigned int tmp = 0;
104 unsigned int crypted_length; 104 unsigned int crypted_length;
105 int opened = 0;
106 /* Openssl declarations*/ 105 /* Openssl declarations*/
107 EVP_CIPHER_CTX ctx; 106 EVP_CIPHER_CTX *ctx = NULL;
108 unsigned int *buffer = NULL; 107 unsigned int *buffer = NULL;
109 int tmp_len; 108 int tmp_len;
110 109
@@ -151,29 +150,28 @@ emile_binbuf_cipher(Emile_Cipher_Algorithm algo,
151 150
152 /* Openssl create the corresponding cipher 151 /* Openssl create the corresponding cipher
153 AES with a 256 bit key, Cipher Block Chaining mode */ 152 AES with a 256 bit key, Cipher Block Chaining mode */
154 EVP_CIPHER_CTX_init(&ctx); 153 ctx = EVP_CIPHER_CTX_new();
155 if (!EVP_EncryptInit_ex(&ctx, EVP_aes_256_cbc(), NULL, ik, iv)) 154 if (!EVP_EncryptInit_ex(ctx, EVP_aes_256_cbc(), NULL, ik, iv))
156 goto on_error; 155 goto on_error;
157 156
158 opened = 1;
159
160 memset(iv, 0, sizeof (iv)); 157 memset(iv, 0, sizeof (iv));
161 memset(ik, 0, sizeof (ik)); 158 memset(ik, 0, sizeof (ik));
162 159
163 pointer = (unsigned char*) eina_binbuf_string_get(result); 160 pointer = (unsigned char*) eina_binbuf_string_get(result);
164 161
165 /* Openssl encrypt */ 162 /* Openssl encrypt */
166 if (!EVP_EncryptUpdate(&ctx, pointer + sizeof (int), &tmp_len, 163 if (!EVP_EncryptUpdate(ctx, pointer + sizeof (int), &tmp_len,
167 (unsigned char *)buffer, 164 (unsigned char *)buffer,
168 eina_binbuf_length_get(data) + sizeof(unsigned int))) 165 eina_binbuf_length_get(data) + sizeof(unsigned int)))
169 goto on_error; 166 goto on_error;
170 167
171 /* Openssl close the cipher */ 168 /* Openssl close the cipher */
172 if (!EVP_EncryptFinal_ex(&ctx, pointer + sizeof (int) + tmp_len, 169 if (!EVP_EncryptFinal_ex(ctx, pointer + sizeof (int) + tmp_len,
173 &tmp_len)) 170 &tmp_len))
174 goto on_error; 171 goto on_error;
175 172
176 EVP_CIPHER_CTX_cleanup(&ctx); 173 EVP_CIPHER_CTX_free(ctx);
174 ctx = NULL;
177 free(buffer); 175 free(buffer);
178 176
179 return result; 177 return result;
@@ -183,8 +181,8 @@ on_error:
183 memset(ik, 0, sizeof (ik)); 181 memset(ik, 0, sizeof (ik));
184 182
185 /* Openssl error */ 183 /* Openssl error */
186 if (opened) 184 if (ctx)
187 EVP_CIPHER_CTX_cleanup(&ctx); 185 EVP_CIPHER_CTX_cleanup(ctx);
188 186
189 free(buffer); 187 free(buffer);
190 188
@@ -203,7 +201,7 @@ emile_binbuf_decipher(Emile_Cipher_Algorithm algo,
203{ 201{
204 Eina_Binbuf *result = NULL; 202 Eina_Binbuf *result = NULL;
205 unsigned int *over; 203 unsigned int *over;
206 EVP_CIPHER_CTX ctx; 204 EVP_CIPHER_CTX *ctx = NULL;
207 unsigned char ik[MAX_KEY_LEN]; 205 unsigned char ik[MAX_KEY_LEN];
208 unsigned char iv[MAX_IV_LEN]; 206 unsigned char iv[MAX_IV_LEN];
209 unsigned char key_material[MAX_KEY_LEN + MAX_IV_LEN]; 207 unsigned char key_material[MAX_KEY_LEN + MAX_IV_LEN];
@@ -211,7 +209,6 @@ emile_binbuf_decipher(Emile_Cipher_Algorithm algo,
211 unsigned int size; 209 unsigned int size;
212 int tmp_len; 210 int tmp_len;
213 int tmp = 0; 211 int tmp = 0;
214 int opened = 0;
215 212
216 if (algo != EMILE_AES256_CBC) return NULL; 213 if (algo != EMILE_AES256_CBC) return NULL;
217 if (!emile_cipher_init()) return NULL; 214 if (!emile_cipher_init()) return NULL;
@@ -247,23 +244,23 @@ emile_binbuf_decipher(Emile_Cipher_Algorithm algo,
247 eina_binbuf_append_length(result, (unsigned char*) (over + 1), tmp_len); 244 eina_binbuf_append_length(result, (unsigned char*) (over + 1), tmp_len);
248 245
249 /* Openssl create the corresponding cipher */ 246 /* Openssl create the corresponding cipher */
250 EVP_CIPHER_CTX_init(&ctx); 247 ctx = EVP_CIPHER_CTX_new();
251 opened = 1;
252 248
253 if (!EVP_DecryptInit_ex(&ctx, EVP_aes_256_cbc(), NULL, ik, iv)) 249 if (!EVP_DecryptInit_ex(ctx, EVP_aes_256_cbc(), NULL, ik, iv))
254 goto on_error; 250 goto on_error;
255 251
256 memset(iv, 0, sizeof (iv)); 252 memset(iv, 0, sizeof (iv));
257 memset(ik, 0, sizeof (ik)); 253 memset(ik, 0, sizeof (ik));
258 254
259 /* Openssl decrypt */ 255 /* Openssl decrypt */
260 if (!EVP_DecryptUpdate(&ctx, 256 if (!EVP_DecryptUpdate(ctx,
261 (void*) eina_binbuf_string_get(result), &tmp, 257 (void*) eina_binbuf_string_get(result), &tmp,
262 (void*) (over + 1), tmp_len)) 258 (void*) (over + 1), tmp_len))
263 goto on_error; 259 goto on_error;
264 260
265 /* Openssl close the cipher*/ 261 /* Openssl close the cipher*/
266 EVP_CIPHER_CTX_cleanup(&ctx); 262 EVP_CIPHER_CTX_free(ctx);
263 ctx = NULL;
267 264
268 /* Get the decrypted data size */ 265 /* Get the decrypted data size */
269 tmp = *(unsigned int*)(eina_binbuf_string_get(result)); 266 tmp = *(unsigned int*)(eina_binbuf_string_get(result));
@@ -281,8 +278,8 @@ on_error:
281 memset(iv, 0, sizeof (iv)); 278 memset(iv, 0, sizeof (iv));
282 memset(ik, 0, sizeof (ik)); 279 memset(ik, 0, sizeof (ik));
283 280
284 if (opened) 281 if (ctx)
285 EVP_CIPHER_CTX_cleanup(&ctx); 282 EVP_CIPHER_CTX_free(ctx);
286 283
287 eina_binbuf_free(result); 284 eina_binbuf_free(result);
288 285