summaryrefslogtreecommitdiff
path: root/src/lib/emile/emile_cipher_gnutls.c
blob: 966859bdfd0b753ae4a7af6a58d9002438ef96fe (plain) (blame)
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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif

#ifdef HAVE_NETINET_IN_H
# include <netinet/in.h>
#endif

#include <gnutls/abstract.h>
#include <gnutls/x509.h>
#include <gcrypt.h>

#include <Eina.h>

#include "Emile.h"

#include "emile_private.h"

#define MAX_KEY_LEN   32
#define MAX_IV_LEN    16

struct _Emile_SSL
{
   const char *last_error;
   const char *cert_file;
   const char *name;

   gnutls_certificate_credentials_t cert;
   gnutls_session_t session;

   union {
      struct {
         gnutls_datum_t session_ticket;
      } client;
      struct {
         gnutls_anon_client_credentials_t anoncred_c;
         gnutls_anon_server_credentials_t anoncred_s;
         gnutls_psk_client_credentials_t pskcred_c;
         gnutls_psk_server_credentials_t pskcred_s;
         char *cert_file;
         gnutls_dh_params_t dh_params;
      } server;
   } u;

   Emile_Cipher_Type t;
   Emile_SSL_State ssl_state;

   Eina_Bool server : 1;
   Eina_Bool verify : 1;
   Eina_Bool verify_basic : 1;
};

GCRY_THREAD_OPTION_PTHREAD_IMPL;

Eina_Bool
_emile_cipher_init(void)
{
   if (gcry_control(GCRYCTL_SET_THREAD_CBS, &gcry_threads_pthread))
     WRN("YOU ARE USING PTHREADS, "
         "BUT I CANNOT INITIALIZE THREADSAFE GCRYPT OPERATIONS!");

   /* Before the library can be used, it must initialize itself if needed. */
   if (gcry_control(GCRYCTL_ANY_INITIALIZATION_P) == 0)
     {
        gcry_check_version(NULL);
        /* Disable warning messages about problems with the secure memory subsystem.
           This command should be run right after gcry_check_version. */
        if (gcry_control(GCRYCTL_DISABLE_SECMEM_WARN))
          return EINA_FALSE;

        /* This command is used to allocate a pool of secure memory and thus
           enabling the use of secure memory. It also drops all extra privileges the
           process has (i.e. if it is run as setuid (root)). If the argument nbytes
           is 0, secure memory will be disabled. The minimum amount of secure memory
           allocated is currently 16384 bytes; you may thus use a value of 1 to
           request that default size. */
        if (gcry_control(GCRYCTL_INIT_SECMEM, 16384, 0))
          WRN("BIG FAT WARNING: I AM UNABLE TO REQUEST SECMEM, "
              "Cryptographic operation are at risk !");
     }

   if (gnutls_global_init())
     return EINA_FALSE;

   return EINA_TRUE;
}

static inline Eina_Bool
emile_hmac_sha1(const void    *key,
                size_t         key_len,
                const void    *data,
                size_t         data_len,
                unsigned char *res)
{
   size_t hlen = gcry_md_get_algo_dlen(GCRY_MD_SHA1);
   gcry_md_hd_t mdh;
   unsigned char *hash;
   gpg_error_t err;

   err = gcry_md_open(&mdh, GCRY_MD_SHA1, GCRY_MD_FLAG_HMAC);
   if (err != GPG_ERR_NO_ERROR)
     return EINA_FALSE;

   err = gcry_md_setkey(mdh, key, key_len);
   if (err != GPG_ERR_NO_ERROR)
     {
        gcry_md_close(mdh);
        return EINA_FALSE;
     }

   gcry_md_write(mdh, data, data_len);

   hash = gcry_md_read(mdh, GCRY_MD_SHA1);
   if (!hash)
     {
        gcry_md_close(mdh);
        return EINA_FALSE;
     }

   memcpy(res, hash, hlen);

   gcry_md_close(mdh);

   return EINA_TRUE;
}

EAPI Eina_Bool
emile_binbuf_hmac_sha1(const char *key,
                       unsigned int key_len,
                       const Eina_Binbuf *data,
                       unsigned char digest[20])
{
   return emile_hmac_sha1(key, key_len,
                          eina_binbuf_string_get(data), eina_binbuf_length_get(data),
                          digest);
}

static inline Eina_Bool
emile_sha1(const void    *data,
           size_t         data_len,
           unsigned char *res)
{
   size_t hlen = gcry_md_get_algo_dlen(GCRY_MD_SHA1);
   gcry_md_hd_t mdh;
   unsigned char *hash;
   gpg_error_t err;

   err = gcry_md_open(&mdh, GCRY_MD_SHA1, 0);
   if (err != GPG_ERR_NO_ERROR)
     return EINA_FALSE;

   gcry_md_write(mdh, data, data_len);

   hash = gcry_md_read(mdh, GCRY_MD_SHA1);
   if (!hash)
     {
        gcry_md_close(mdh);
        return EINA_FALSE;
     }

   memcpy(res, hash, hlen);

   gcry_md_close(mdh);

   return EINA_TRUE;
}

EAPI Eina_Bool
emile_binbuf_sha1(const Eina_Binbuf * data, unsigned char digest[20])
{
   Eina_Slice slice = eina_binbuf_slice_get(data);
   return emile_sha1(slice.mem, slice.len, digest);
}

EAPI Eina_Binbuf *
emile_binbuf_cipher(Emile_Cipher_Algorithm algo,
                    const Eina_Binbuf *data,
                    const char *key,
                    unsigned int length)
{
   /* Cipher declarations */
   Eina_Binbuf *result;
   unsigned char *pointer;
   unsigned char iv[MAX_IV_LEN];
   unsigned char ik[MAX_KEY_LEN];
   unsigned char key_material[MAX_IV_LEN + MAX_KEY_LEN];
   unsigned int salt;
   unsigned int tmp = 0;
   unsigned int crypted_length;
   int opened = 0;
   /* Gcrypt declarations */
   gcry_error_t err = 0;
   gcry_cipher_hd_t cipher;

   if (algo != EMILE_AES256_CBC) return NULL;
   if (!emile_cipher_init()) return NULL;

   /* Gcrypt salt generation */
   gcry_create_nonce((unsigned char *)&salt, sizeof(salt));

   result = eina_binbuf_new();
   if (!result) return NULL;

   emile_pbkdf2_sha1(key,
                     length,
                     (unsigned char *)&salt,
                     sizeof(unsigned int),
                     2048,
                     key_material,
                     MAX_KEY_LEN + MAX_IV_LEN);

   memcpy(iv, key_material, MAX_IV_LEN);
   memcpy(ik, key_material + MAX_IV_LEN, MAX_KEY_LEN);

   memset(key_material, 0, sizeof (key_material));

   crypted_length = ((((eina_binbuf_length_get(data) + sizeof (unsigned int)) >> 5) + 1) << 5)
     + sizeof (unsigned int);

   eina_binbuf_append_length(result, (unsigned char*) &salt, sizeof (salt));
   memset(&salt, 0, sizeof (salt));

   tmp = htonl(eina_binbuf_length_get(data));
   eina_binbuf_append_length(result, (unsigned char*) &tmp, sizeof (tmp));
   eina_binbuf_append_buffer(result, data);

   while (eina_binbuf_length_get(result) < crypted_length)
     {
        int r;

        r = rand();
        eina_binbuf_append_length(result, (unsigned char*) &r, sizeof (r));
     }
   eina_binbuf_remove(result, crypted_length, eina_binbuf_length_get(result));

   /* Gcrypt create the corresponding cipher
      AES with a 256 bit key, Cipher Block Chaining mode */
   err = gcry_cipher_open(&cipher, GCRY_CIPHER_AES256, GCRY_CIPHER_MODE_CBC, 0);
   if (err) goto on_error;

   opened = 1;
   err = gcry_cipher_setiv(cipher, iv, MAX_IV_LEN);
   if (err) goto on_error;

   err = gcry_cipher_setkey(cipher, ik, MAX_KEY_LEN);
   if (err) goto on_error;

   memset(iv, 0, sizeof (iv));
   memset(ik, 0, sizeof (ik));

   pointer = (unsigned char*) eina_binbuf_string_get(result);

   /* Gcrypt encrypt */
   err = gcry_cipher_encrypt(cipher, pointer + sizeof (int),
                             eina_binbuf_length_get(result) - sizeof (int),
                             NULL, 0);
   if (err) goto on_error;

   /* Gcrypt close the cipher */
   gcry_cipher_close(cipher);

   return result;

on_error:
   memset(iv, 0, sizeof (iv));
   memset(ik, 0, sizeof (ik));

   /* Gcrypt error */
   if (opened)
     gcry_cipher_close(cipher);

   /* General error */
   eina_binbuf_free(result);

   return NULL;
}

EAPI Eina_Binbuf *
emile_binbuf_decipher(Emile_Cipher_Algorithm algo,
                      const Eina_Binbuf *data,
                      const char *key,
                      unsigned int length)
{
   Eina_Binbuf *result = NULL;
   unsigned int *over;
   gcry_error_t err = 0;
   gcry_cipher_hd_t cipher;
   unsigned char ik[MAX_KEY_LEN];
   unsigned char iv[MAX_IV_LEN];
   unsigned char key_material[MAX_KEY_LEN + MAX_IV_LEN];
   unsigned int salt;
   unsigned int size;
   int tmp_len;
   int tmp = 0;

   if (algo != EMILE_AES256_CBC) return NULL;
   if (!emile_cipher_init()) return NULL;

   over = (unsigned int*) eina_binbuf_string_get(data);
   size = eina_binbuf_length_get(data);

   /* At least the salt and an AES block */
   if (size < sizeof(unsigned int) + 16)
     return NULL;

   /* Get the salt */
   salt = *over;

   /* Generate the iv and the key with the salt */
   emile_pbkdf2_sha1(key, length, (unsigned char *)&salt,
                     sizeof(unsigned int), 2048, key_material,
                     MAX_KEY_LEN + MAX_IV_LEN);

   memcpy(iv, key_material, MAX_IV_LEN);
   memcpy(ik, key_material + MAX_IV_LEN, MAX_KEY_LEN);

   memset(key_material, 0, sizeof (key_material));
   memset(&salt, 0, sizeof (salt));

   /* Align to AES block size if size is not align */
   tmp_len = size - sizeof (unsigned int);
   if ((tmp_len & 0x1F) != 0) goto on_error;

   result = eina_binbuf_new();
   if (!result) goto on_error;

   eina_binbuf_append_length(result, (unsigned char*) (over + 1), tmp_len);

   /* Gcrypt create the corresponding cipher */
   err = gcry_cipher_open(&cipher, GCRY_CIPHER_AES256, GCRY_CIPHER_MODE_CBC, 0);
   if (err) goto on_error;

   err = gcry_cipher_setiv(cipher, iv, MAX_IV_LEN);
   if (err) goto on_error;

   err = gcry_cipher_setkey(cipher, ik, MAX_KEY_LEN);
   if (err) goto on_error;

   memset(iv, 0, sizeof (iv));
   memset(ik, 0, sizeof (ik));

   /* Gcrypt decrypt */
   err = gcry_cipher_decrypt(cipher,
                             (void*) eina_binbuf_string_get(result), tmp_len,
                             (void*) (over + 1), tmp_len);
   if (err) goto on_error;

   /* Gcrypt close the cipher */
   gcry_cipher_close(cipher);

   /* Get the decrypted data size */
   tmp = *(unsigned int*)(eina_binbuf_string_get(result));
   tmp = ntohl(tmp);
   if (tmp > tmp_len || tmp <= 0)
     goto on_error;

   /* Remove header and padding  */
   eina_binbuf_remove(result, 0, sizeof (unsigned int));
   eina_binbuf_remove(result, tmp, eina_binbuf_length_get(result));

   return result;

on_error:
   memset(iv, 0, sizeof (iv));
   memset(ik, 0, sizeof (ik));

   eina_binbuf_free(result);

   return NULL;
}

// FIXME: handshaking and fun


EAPI Emile_SSL *
emile_cipher_server_listen(Emile_Cipher_Type t)
{
   Emile_SSL *r;
   int ret;

   if (t != EMILE_SSLv23 &&
       t != EMILE_TLSv1)
     return NULL;

   r = calloc(1, sizeof (Emile_SSL));
   if (!r) return NULL;

   ret = gnutls_certificate_allocate_credentials(&r->cert);
   if (ret) goto on_error;

   r->t = t;
   r->server = EINA_TRUE;

   return r;

 on_error:
   ERR("GNUTLS error: %s - %s.",
       gnutls_strerror_name(ret),
       gnutls_strerror(ret));
   emile_cipher_free(r);
   return NULL;
}

EAPI Emile_SSL *
emile_cipher_client_connect(Emile_SSL *server EINA_UNUSED, int fd EINA_UNUSED)
{
   return NULL;
}

EAPI Emile_SSL *
emile_cipher_server_connect(Emile_Cipher_Type t)
{
   const char *priority = "NORMAL:%VERIFY_ALLOW_X509_V1_CA_CRT";
   Emile_SSL *r;
   int ret;

   switch (t)
     {
      case EMILE_SSLv23:
         break;
      case EMILE_TLSv1:
         priority = "NORMAL:%VERIFY_ALLOW_X509_V1_CA_CRT:!VERS-SSL3.0";
         break;
      default:
         return NULL;
     }

   r = calloc(1, sizeof (Emile_SSL));
   if (!r) return NULL;

   r->server = EINA_FALSE;

   ret = gnutls_certificate_allocate_credentials(&r->cert);
   if (ret) goto on_error;

   ret = gnutls_init(&r->session, GNUTLS_CLIENT);
   if (ret) goto on_error;

   ret = gnutls_session_ticket_enable_client(r->session);
   if (ret) goto on_error;

   // FIXME: Delay that until later access

   ret = gnutls_server_name_set(r->session, GNUTLS_NAME_DNS,
                                r->name, strlen(r->name));
   if (ret) goto on_error;

   ret = gnutls_priority_set_direct(r->session, priority, NULL);
   if (ret) goto on_error;

   gnutls_handshake_set_private_extensions(r->session, 1);
   ret = gnutls_credentials_set(r->session, GNUTLS_CRD_CERTIFICATE, r->cert);

   return r;

 on_error:
   // FIXEM: cleanly destroy session
   free(r);
   return NULL;
}

EAPI Eina_Bool
emile_cipher_free(Emile_SSL *emile EINA_UNUSED)
{
   return EINA_TRUE;
}

EAPI Eina_Bool
emile_cipher_cafile_add(Emile_SSL *emile, const char *file)
{
   Eina_File_Direct_Info *info;
   Eina_Iterator *it;
   struct stat st;
   int count = 0;
   int ret;

   if (stat(file, &st)) return EINA_FALSE;
   if (S_ISDIR(st.st_mode))
     {
        it = eina_file_direct_ls(file);
        EINA_ITERATOR_FOREACH(it, info)
          {
             if (!(info->type == EINA_FILE_UNKNOWN ||
                   info->type == EINA_FILE_REG ||
                   info->type == EINA_FILE_LNK))
               continue ;

             ret = gnutls_certificate_set_x509_trust_file(emile->cert,
                                                          file,
                                                          GNUTLS_X509_FMT_PEM);
             if (ret > 0) count += ret;
          }
        eina_iterator_free(it);
     }
   else
     {
        ret = gnutls_certificate_set_x509_trust_file(emile->cert,
                                                     file,
                                                     GNUTLS_X509_FMT_PEM);
        if (ret > 0) count += ret;
     }

   if (!count) ERR("Could not load CA file from '%s'.", file);
   return !count ? EINA_FALSE : EINA_TRUE;
}

EAPI Eina_Bool
emile_cipher_cert_add(Emile_SSL *emile, const char *file)
{
   return eina_stringshare_replace(&emile->cert_file, file);
}

EAPI Eina_Bool
emile_cipher_privkey_add(Emile_SSL *emile, const char *file)
{
   int ret;

   ret = gnutls_certificate_set_x509_key_file(emile->cert,
                                              emile->cert_file,
                                              file,
                                              GNUTLS_X509_FMT_PEM);
   if (ret)
     ERR("Could not load certificate/key file ('%s'/'%s').",
         emile->cert_file, file);
   return ret ? EINA_FALSE : EINA_TRUE;
}

EAPI Eina_Bool
emile_cipher_crl_add(Emile_SSL *emile, const char *file)
{
   int ret;

   ret = gnutls_certificate_set_x509_crl_file(emile->cert, file,
                                              GNUTLS_X509_FMT_PEM);
   if (ret)
     ERR("Could not load CRL file from '%s'.", file);
   return ret ? EINA_FALSE : EINA_TRUE;
}

EAPI int
emile_cipher_read(Emile_SSL *emile, Eina_Binbuf *buffer)
{
   int num;

   if (!buffer || eina_binbuf_length_get(buffer) <= 0) return 0;
   if (emile->ssl_state == EMILE_SSL_STATE_HANDSHAKING)
     {
        DBG("Ongoing GNUTLS handshaking.");
        //_emile_cipher_handshaking(emile);
        if (emile->ssl_state == EMILE_SSL_STATE_ERROR)
          return -1;
        return 0;
     }

   num = gnutls_record_recv(emile->session,
                            (void*) eina_binbuf_string_get(buffer),
                            eina_binbuf_length_get(buffer));
   return num;
}

EAPI int
emile_cipher_write(Emile_SSL *emile EINA_UNUSED, const Eina_Binbuf *buffer EINA_UNUSED)
{
   return 0;
}

EAPI const char *
emile_cipher_error_get(const Emile_SSL *emile)
{
   return emile->last_error;
}

EAPI Eina_Bool
emile_cipher_verify_name_set(Emile_SSL *emile, const char *name)
{
   return eina_stringshare_replace(&emile->name, name);
}

EAPI const char *
emile_cipher_verify_name_get(const Emile_SSL *emile)
{
   return emile->name;
}

EAPI void
emile_cipher_verify_set(Emile_SSL *emile, Eina_Bool verify)
{
   emile->verify = verify;
}

EAPI void
emile_cipher_verify_basic_set(Emile_SSL *emile, Eina_Bool verify_basic)
{
   emile->verify_basic = verify_basic;
}

EAPI Eina_Bool
emile_cipher_verify_get(const Emile_SSL *emile)
{
   return emile->verify;
}

EAPI Eina_Bool
emile_cipher_verify_basic_get(const Emile_SSL *emile)
{
   return emile->verify_basic;
}