summaryrefslogtreecommitdiff
path: root/src/lib/ecore_con/efl_net_socket_ssl.c
blob: b91f6649d399583e270b75c47b1ce605d36b7d1a (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
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
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
#define EFL_NET_SOCKET_SSL_PROTECTED 1
#define EFL_IO_READER_PROTECTED 1
#define EFL_IO_WRITER_PROTECTED 1
#define EFL_IO_CLOSER_PROTECTED 1
#define EFL_NET_SOCKET_PROTECTED 1

#ifdef HAVE_CONFIG_H
# include <config.h>
#endif

#include "Ecore.h"
#include "Ecore_Con.h"
#include "ecore_con_private.h"

typedef struct _Efl_Net_Ssl_Conn Efl_Net_Ssl_Conn;

/**
 * Setups the SSL context
 *
 * Update the given lists, removing invalid entries. If all entries
 * failed in a list, return EINVAL.
 *
 * @internal
 */
static Eina_Error efl_net_ssl_conn_setup(Efl_Net_Ssl_Conn *conn, Eina_Bool is_dialer, Efl_Net_Socket *sock, Efl_Net_Ssl_Context *context);

/**
 * Cleans up the SSL associated to this context.
 * @internal
 */
static void efl_net_ssl_conn_teardown(Efl_Net_Ssl_Conn *conn);

/**
 * Send data to remote peer.
 *
 * This should be called once handshake is finished, otherwise it may
 * lead the handshake to fail.
 *
 * @param slice[inout] takes the amount of bytes to write and
 *        source memory, will store written length in slice->len.
 *
 * @internal
 */
static Eina_Error efl_net_ssl_conn_write(Efl_Net_Ssl_Conn *conn, Eina_Slice *slice);

/**
 * Receive data from remote peer.
 *
 * This should be called once handshake is finished, otherwise it may
 * lead the handshake to fail.
 *
 * Note that even if the socket 'can_read', eventually it couldn't
 * decipher a byte and it will return slice->len == 0 with EAGAIN as
 * error.
 *
 * @param slice[inout] takes the amount of bytes to read and
 *        destination memory, will store read length in slice->len.
 *
 * @internal
 */
static Eina_Error efl_net_ssl_conn_read(Efl_Net_Ssl_Conn *conn, Eina_Rw_Slice *slice);

/**
 * Attempt to finish the handshake.
 *
 * This should not block, if it's not finished yet, just set done =
 * false.
 *
 * Errors, such as failed handshake, should be returned as Eina_Error.
 *
 * @internal
 */
static Eina_Error efl_net_ssl_conn_handshake(Efl_Net_Ssl_Conn *conn, Eina_Bool *done);

/**
 * Configure how to verify peer.
 *
 * @internal
 */
static Eina_Error efl_net_ssl_conn_verify_mode_set(Efl_Net_Ssl_Conn *conn, Efl_Net_Ssl_Verify_Mode verify_mode);

/**
 * Configure whenever to check for hostname.
 *
 * @internal
 */
static Eina_Error efl_net_ssl_conn_hostname_verify_set(Efl_Net_Ssl_Conn *conn, Eina_Bool hostname_verify);

/**
 * Overrides the hostname to use.
 *
 * @note duplicate hostname if needed!
 *
 * @internal
 */
static Eina_Error efl_net_ssl_conn_hostname_override_set(Efl_Net_Ssl_Conn *conn, const char *hostname);

#if HAVE_OPENSSL
#include "efl_net_ssl_conn-openssl.c"
#elif HAVE_GNUTLS
#include "efl_net_ssl_conn-gnutls.c"
#else
#include "efl_net_ssl_conn-none.c"
#endif

#define MY_CLASS EFL_NET_SOCKET_SSL_CLASS

typedef struct _Efl_Net_Socket_Ssl_Data
{
   Eo *sock;
   Efl_Net_Ssl_Context *context;
   const char *hostname_override;
   Efl_Net_Ssl_Conn ssl_conn;
   Efl_Net_Ssl_Verify_Mode verify_mode;
   Eina_Bool hostname_verify;
   Eina_Bool did_handshake;
   Eina_Bool torndown;
   Eina_Bool can_read;
   Eina_Bool eos;
   Eina_Bool can_write;
} Efl_Net_Socket_Ssl_Data;

static void
efl_net_socket_ssl_sock_eos(void *data, const Efl_Event *event EINA_UNUSED)
{
   Eo *o = data;
   efl_io_reader_eos_set(o, EINA_TRUE);
}

static void
efl_net_socket_ssl_handshake_try(Eo *o, Efl_Net_Socket_Ssl_Data *pd)
{
   Eina_Error err;

   if (pd->torndown) return;

   DBG("SSL=%p handshake...", o);

   err = efl_net_ssl_conn_handshake(&pd->ssl_conn, &pd->did_handshake);
   if (err)
     {
        WRN("SSL=%p failed handshake: %s", o, eina_error_msg_get(err));
        efl_event_callback_call(o, EFL_NET_SOCKET_SSL_EVENT_SSL_ERROR, &err);
        efl_io_closer_close(o);
        return;
     }
   if (!pd->did_handshake) return;

   DBG("SSL=%p finished handshake", o);
   efl_io_reader_can_read_set(o, efl_io_reader_can_read_get(pd->sock));
   efl_io_writer_can_write_set(o, efl_io_writer_can_write_get(pd->sock));

   efl_event_callback_call(o, EFL_NET_SOCKET_SSL_EVENT_SSL_READY, NULL);
}

static void
efl_net_socket_ssl_sock_can_read_changed(void *data, const Efl_Event *event EINA_UNUSED)
{
   Eo *o = data;
   Efl_Net_Socket_Ssl_Data *pd = efl_data_scope_get(o, MY_CLASS);

   efl_ref(o); /* we're emitting callbacks then continuing the workflow */

   if (!efl_io_reader_can_read_get(pd->sock))
     goto end;

   if (pd->did_handshake)
     efl_io_reader_can_read_set(o, EINA_TRUE);
   else
     efl_net_socket_ssl_handshake_try(o, pd);

 end:
   efl_unref(o);
}

static void
efl_net_socket_ssl_sock_can_write_changed(void *data, const Efl_Event *event EINA_UNUSED)
{
   Eo *o = data;
   Efl_Net_Socket_Ssl_Data *pd = efl_data_scope_get(o, MY_CLASS);

   efl_ref(o); /* we're emitting callbacks then continuing the workflow */

   if (!efl_io_writer_can_write_get(pd->sock))
     goto end;

   if (pd->did_handshake)
     efl_io_writer_can_write_set(o, EINA_TRUE);
   else
     efl_net_socket_ssl_handshake_try(o, pd);

 end:
   efl_unref(o);
}

static void
efl_net_socket_ssl_sock_closed(void *data, const Efl_Event *event EINA_UNUSED)
{
   Eo *o = data;
   efl_event_callback_call(o, EFL_IO_CLOSER_EVENT_CLOSED, NULL);
}

static void
efl_net_socket_ssl_sock_del(void *data, const Efl_Event *event EINA_UNUSED)
{
   Eo *o = data;
   Efl_Net_Socket_Ssl_Data *pd = efl_data_scope_get(o, MY_CLASS);
   pd->sock = NULL;
   pd->torndown = EINA_TRUE;
   efl_net_ssl_conn_teardown(&pd->ssl_conn);
}

EFL_CALLBACKS_ARRAY_DEFINE(efl_net_socket_ssl_sock_cbs,
                           {EFL_IO_READER_EVENT_EOS, efl_net_socket_ssl_sock_eos},
                           {EFL_IO_READER_EVENT_CAN_READ_CHANGED, efl_net_socket_ssl_sock_can_read_changed},
                           {EFL_IO_WRITER_EVENT_CAN_WRITE_CHANGED, efl_net_socket_ssl_sock_can_write_changed},
                           {EFL_IO_CLOSER_EVENT_CLOSED, efl_net_socket_ssl_sock_closed},
                           {EFL_EVENT_DEL, efl_net_socket_ssl_sock_del});

static void
efl_net_socket_ssl_sock_resolved(void *data, const Efl_Event *event EINA_UNUSED)
{
   Eo *o = data;
   Efl_Net_Socket_Ssl_Data *pd = efl_data_scope_get(o, MY_CLASS);

   if (pd->torndown) return;

   efl_event_callback_call(o, EFL_NET_DIALER_EVENT_RESOLVED, NULL);
}

static void
efl_net_socket_ssl_sock_connected(void *data, const Efl_Event *event EINA_UNUSED)
{
   Eo *o = data;
   Efl_Net_Socket_Ssl_Data *pd = efl_data_scope_get(o, MY_CLASS);
   Eina_Error err;

   if (pd->torndown) return;

   efl_ref(o); /* we're emitting callbacks then continuing the workflow */

   err = efl_net_ssl_conn_handshake(&pd->ssl_conn, &pd->did_handshake);
   if (err)
     {
        WRN("SSL=%p failed handshake: %s", o, eina_error_msg_get(err));
        efl_io_closer_close(o);
        return;
     }

   efl_unref(o);
}

EFL_CALLBACKS_ARRAY_DEFINE(efl_net_socket_ssl_sock_dialer_cbs,
                           {EFL_NET_DIALER_EVENT_RESOLVED, efl_net_socket_ssl_sock_resolved},
                           {EFL_NET_DIALER_EVENT_CONNECTED, efl_net_socket_ssl_sock_connected});

static void
_efl_net_socket_ssl_context_del(void *data, const Efl_Event *event EINA_UNUSED)
{
   Eo *o = data;
   Efl_Net_Socket_Ssl_Data *pd = efl_data_scope_get(o, MY_CLASS);
   pd->context = NULL;
}

EOLIAN static void
_efl_net_socket_ssl_adopt(Eo *o, Efl_Net_Socket_Ssl_Data *pd, Efl_Net_Socket *sock, Efl_Net_Ssl_Context *context)
{
   Eina_Error err;
   char *tmp = NULL;
   const char *hostname;
   Eina_Bool is_dialer;

   EINA_SAFETY_ON_TRUE_RETURN(pd->sock != NULL);
   EINA_SAFETY_ON_FALSE_RETURN(efl_isa(sock, EFL_NET_SOCKET_INTERFACE));
   EINA_SAFETY_ON_FALSE_RETURN(efl_isa(context, EFL_NET_SSL_CONTEXT_CLASS));
   EINA_SAFETY_ON_TRUE_RETURN(efl_finalized_get(o));

   is_dialer = efl_isa(sock, EFL_NET_DIALER_INTERFACE);
   err = efl_net_ssl_conn_setup(&pd->ssl_conn, is_dialer, sock, context);
   if (err)
     {
        ERR("ssl=%p failed to adopt socket (is_dialer=%d) sock=%p", o, is_dialer, sock);
        return;
     }

   pd->context = efl_ref(context);
   efl_event_callback_add(context, EFL_EVENT_DEL, _efl_net_socket_ssl_context_del, o);

   DBG("ssl=%p adopted socket (is_dialer=%d) sock=%p, ssl_conn=%p", o, is_dialer, sock, &pd->ssl_conn);

   if (pd->hostname_verify == 0xff)
     pd->hostname_verify = efl_net_ssl_context_hostname_verify_get(context);

   hostname = pd->hostname_override;
   if (!hostname)
     hostname = pd->hostname_override = eina_stringshare_ref(efl_net_ssl_context_hostname_get(context));
   if (!hostname)
     {
        const char *remote_address = (is_dialer ?
                                      efl_net_dialer_address_dial_get(sock) :
                                      efl_net_socket_address_remote_get(sock));
        if (remote_address)
          {
             const char *host, *port;

             tmp = strdup(remote_address);
             EINA_SAFETY_ON_NULL_RETURN(tmp);
             if (efl_net_ip_port_split(tmp, &host, &port))
               hostname = host;
          }
     }

   if ((uint8_t)pd->verify_mode == 0xff) pd->verify_mode = efl_net_ssl_context_verify_mode_get(context);
   efl_net_ssl_conn_verify_mode_set(&pd->ssl_conn, pd->verify_mode);
   efl_net_ssl_conn_hostname_verify_set(&pd->ssl_conn, pd->hostname_verify);
   efl_net_ssl_conn_hostname_override_set(&pd->ssl_conn, hostname);
   free(tmp);

   pd->sock = efl_ref(sock);
   efl_event_callback_array_add(sock, efl_net_socket_ssl_sock_cbs(), o);

   if (efl_isa(sock, EFL_NET_DIALER_INTERFACE))
     efl_event_callback_array_add(sock, efl_net_socket_ssl_sock_dialer_cbs(), o);

   efl_net_socket_ssl_sock_can_read_changed(o, NULL);
   efl_net_socket_ssl_sock_can_write_changed(o, NULL);
   if (efl_io_closer_closed_get(sock))
     efl_event_callback_call(o, EFL_IO_CLOSER_EVENT_CLOSED, NULL);
}

EOLIAN static Eina_Bool
_efl_net_socket_ssl_adopted_get(const Eo *o EINA_UNUSED, Efl_Net_Socket_Ssl_Data *pd, Efl_Net_Socket **sock, Efl_Net_Ssl_Context **context)
{
   if (sock) *sock = pd->sock;
   if (context) *context = pd->context;
   return !!pd->sock;
}

static Efl_Net_Ssl_Verify_Mode
_efl_net_socket_ssl_verify_mode_get(const Eo *o EINA_UNUSED, Efl_Net_Socket_Ssl_Data *pd)
{
   return pd->verify_mode;
}

static void
_efl_net_socket_ssl_verify_mode_set(Eo *o EINA_UNUSED, Efl_Net_Socket_Ssl_Data *pd, Efl_Net_Ssl_Verify_Mode verify_mode)
{
   pd->verify_mode = verify_mode;
   if (!efl_finalized_get(o)) return;
   EINA_SAFETY_ON_TRUE_RETURN(pd->torndown);

   efl_net_ssl_conn_verify_mode_set(&pd->ssl_conn, pd->verify_mode);
}

static Eina_Bool
_efl_net_socket_ssl_hostname_verify_get(const Eo *o EINA_UNUSED, Efl_Net_Socket_Ssl_Data *pd)
{
   return pd->hostname_verify;
}

static void
_efl_net_socket_ssl_hostname_verify_set(Eo *o EINA_UNUSED, Efl_Net_Socket_Ssl_Data *pd, Eina_Bool hostname_verify)
{
   pd->hostname_verify = hostname_verify;
   if (!efl_finalized_get(o)) return;
   EINA_SAFETY_ON_TRUE_RETURN(pd->torndown);

   efl_net_ssl_conn_hostname_verify_set(&pd->ssl_conn, pd->hostname_verify);
}

static const char *
_efl_net_socket_ssl_hostname_override_get(const Eo *o EINA_UNUSED, Efl_Net_Socket_Ssl_Data *pd)
{
   return pd->hostname_override;
}

static void
_efl_net_socket_ssl_hostname_override_set(Eo *o EINA_UNUSED, Efl_Net_Socket_Ssl_Data *pd, const char* hostname_override)
{
   char *tmp = NULL;
   const char *hostname;

   eina_stringshare_replace(&pd->hostname_override, hostname_override);

   if (!efl_finalized_get(o)) return;
   EINA_SAFETY_ON_TRUE_RETURN(pd->torndown);

   hostname = pd->hostname_override;
   if (!hostname)
     {
        const char *remote_address = (efl_isa(pd->sock, EFL_NET_DIALER_INTERFACE) ?
                                      efl_net_dialer_address_dial_get(pd->sock) :
                                      efl_net_socket_address_remote_get(pd->sock));
        if (remote_address)
          {
             const char *host, *port;

             tmp = strdup(remote_address);
             EINA_SAFETY_ON_NULL_RETURN(tmp);
             if (efl_net_ip_port_split(tmp, &host, &port))
               hostname = host;
          }
     }

   efl_net_ssl_conn_hostname_override_set(&pd->ssl_conn, hostname);
   free(tmp);
}

EOLIAN static Efl_Object *
_efl_net_socket_ssl_efl_object_finalize(Eo *o, Efl_Net_Socket_Ssl_Data *pd EINA_UNUSED)
{
   Eina_Error err;

   o = efl_finalize(efl_super(o, MY_CLASS));
   if (!o) return NULL;

   if (!pd->sock)
     {
        ERR("no Efl.Net.Socket was adopted by this SSL=%p", o);
        return NULL;
     }

   if (efl_isa(pd->sock, EFL_NET_DIALER_INTERFACE))
     {
        if (!efl_net_dialer_connected_get(pd->sock))
          return o;
     }

   err = efl_net_ssl_conn_handshake(&pd->ssl_conn, &pd->did_handshake);
   if (err)
     {
        WRN("SSL=%p failed handshake", o);
        return NULL;
     }

   return o;
}

EOLIAN static Eo *
_efl_net_socket_ssl_efl_object_constructor(Eo *o, Efl_Net_Socket_Ssl_Data *pd)
{
   pd->hostname_verify = 0xff;
   pd->verify_mode = 0xff;
   return efl_constructor(efl_super(o, MY_CLASS));
}

EOLIAN static void
_efl_net_socket_ssl_efl_object_invalidate(Eo *o, Efl_Net_Socket_Ssl_Data *pd EINA_UNUSED)
{
   if (efl_io_closer_close_on_invalidate_get(o) &&
       (!efl_io_closer_closed_get(o)))
     {
        efl_event_freeze(o);
        efl_io_closer_close(o);
        efl_event_thaw(o);
     }

   efl_invalidate(efl_super(o, MY_CLASS));
}

EOLIAN static void
_efl_net_socket_ssl_efl_object_destructor(Eo *o, Efl_Net_Socket_Ssl_Data *pd)
{
   efl_destructor(efl_super(o, MY_CLASS));

   pd->torndown = EINA_TRUE;
   efl_net_ssl_conn_teardown(&pd->ssl_conn);
   if (pd->sock)
     {
        efl_event_callback_array_del(pd->sock, efl_net_socket_ssl_sock_cbs(), o);
        if (efl_isa(pd->sock, EFL_NET_DIALER_INTERFACE))
          efl_event_callback_array_del(pd->sock, efl_net_socket_ssl_sock_dialer_cbs(), o);
        efl_unref(pd->sock);
        pd->sock = NULL;
     }

   if (pd->context)
     {
        efl_event_callback_del(pd->context, EFL_EVENT_DEL, _efl_net_socket_ssl_context_del, o);
        efl_unref(pd->context);
        pd->context = NULL;
     }

   eina_stringshare_replace(&pd->hostname_override, NULL);
}

EOLIAN static Eina_Error
_efl_net_socket_ssl_efl_io_closer_close(Eo *o, Efl_Net_Socket_Ssl_Data *pd)
{
   EINA_SAFETY_ON_NULL_RETURN_VAL(pd->sock, EBADF);

   efl_io_reader_can_read_set(o, EINA_FALSE);
   efl_io_reader_eos_set(o, EINA_TRUE);
   pd->torndown = EINA_TRUE;
   efl_net_ssl_conn_teardown(&pd->ssl_conn);
   if (efl_io_closer_closed_get(pd->sock))
     return 0;
   return efl_io_closer_close(pd->sock);
}

EOLIAN static Eina_Bool
_efl_net_socket_ssl_efl_io_closer_closed_get(const Eo *o EINA_UNUSED, Efl_Net_Socket_Ssl_Data *pd)
{
   return (!pd->sock) || efl_io_closer_closed_get(pd->sock);
}

EOLIAN static Eina_Error
_efl_net_socket_ssl_efl_io_reader_read(Eo *o, Efl_Net_Socket_Ssl_Data *pd, Eina_Rw_Slice *rw_slice)
{
   Eina_Error err;

   EINA_SAFETY_ON_NULL_RETURN_VAL(rw_slice, EINVAL);
   EINA_SAFETY_ON_TRUE_RETURN_VAL(pd->torndown, EBADF);

   if (!pd->did_handshake)
     {
        rw_slice->mem = NULL;
        rw_slice->len = 0;
        return EAGAIN;
     }

   err = efl_net_ssl_conn_read(&pd->ssl_conn, rw_slice);

   if (rw_slice->len == 0)
     {
        efl_io_reader_can_read_set(o, EINA_FALSE);
        if (err == 0)
          efl_io_reader_eos_set(o, EINA_TRUE);
     }

   return err;
}

EOLIAN static void
_efl_net_socket_ssl_efl_io_reader_can_read_set(Eo *o, Efl_Net_Socket_Ssl_Data *pd, Eina_Bool can_read)
{
   EINA_SAFETY_ON_NULL_RETURN(pd->sock);
   if (pd->can_read == can_read) return;
   pd->can_read = can_read;
   efl_event_callback_call(o, EFL_IO_READER_EVENT_CAN_READ_CHANGED, NULL);
}

EOLIAN static Eina_Bool
_efl_net_socket_ssl_efl_io_reader_can_read_get(const Eo *o EINA_UNUSED, Efl_Net_Socket_Ssl_Data *pd)
{
   return pd->can_read;
}

EOLIAN static Eina_Bool
_efl_net_socket_ssl_efl_io_reader_eos_get(const Eo *o EINA_UNUSED, Efl_Net_Socket_Ssl_Data *pd)
{
   return pd->eos;
}

EOLIAN static void
_efl_net_socket_ssl_efl_io_reader_eos_set(Eo *o, Efl_Net_Socket_Ssl_Data *pd, Eina_Bool is_eos)
{
   EINA_SAFETY_ON_NULL_RETURN(pd->sock);
   if (pd->eos == is_eos) return;
   pd->eos = is_eos;
   if (is_eos)
     efl_event_callback_call(o, EFL_IO_READER_EVENT_EOS, NULL);
}

EOLIAN static Eina_Error
_efl_net_socket_ssl_efl_io_writer_write(Eo *o, Efl_Net_Socket_Ssl_Data *pd, Eina_Slice *ro_slice, Eina_Slice *remaining)
{
   Eina_Error err;

   EINA_SAFETY_ON_NULL_RETURN_VAL(ro_slice, EINVAL);
   EINA_SAFETY_ON_TRUE_RETURN_VAL(pd->torndown, EBADF);

   if (!pd->did_handshake)
     {
        if (remaining) *remaining = *ro_slice;
        ro_slice->mem = NULL;
        ro_slice->len = 0;
        return EAGAIN;
     }

   if (remaining) *remaining = *ro_slice;

   err = efl_net_ssl_conn_write(&pd->ssl_conn, ro_slice);

   if (remaining)
     {
        remaining->bytes += ro_slice->len;
        remaining->len -= ro_slice->len;
     }

   if (ro_slice->len == 0)
     efl_io_writer_can_write_set(o, EINA_FALSE);

   return err;
}

EOLIAN static void
_efl_net_socket_ssl_efl_io_writer_can_write_set(Eo *o, Efl_Net_Socket_Ssl_Data *pd, Eina_Bool can_write)
{
   EINA_SAFETY_ON_NULL_RETURN(pd->sock);
   if (pd->can_write == can_write) return;
   pd->can_write = can_write;
   efl_event_callback_call(o, EFL_IO_WRITER_EVENT_CAN_WRITE_CHANGED, NULL);
}

EOLIAN static Eina_Bool
_efl_net_socket_ssl_efl_io_writer_can_write_get(const Eo *o EINA_UNUSED, Efl_Net_Socket_Ssl_Data *pd)
{
   return pd->can_write;
}

EOLIAN static Eina_Bool
_efl_net_socket_ssl_efl_io_closer_close_on_exec_set(Eo *o EINA_UNUSED, Efl_Net_Socket_Ssl_Data *pd, Eina_Bool close_on_exec)
{
   if (pd->sock) efl_io_closer_close_on_exec_set(pd->sock, close_on_exec);
   return EINA_TRUE;
}

EOLIAN static Eina_Bool
_efl_net_socket_ssl_efl_io_closer_close_on_exec_get(const Eo *o EINA_UNUSED, Efl_Net_Socket_Ssl_Data *pd)
{
   return pd->sock && efl_io_closer_close_on_exec_get(pd->sock);
}

EOLIAN static void
_efl_net_socket_ssl_efl_io_closer_close_on_invalidate_set(Eo *o EINA_UNUSED, Efl_Net_Socket_Ssl_Data *pd, Eina_Bool close_on_invalidate)
{
   if (pd->sock) efl_io_closer_close_on_invalidate_set(pd->sock, close_on_invalidate);
}

EOLIAN static Eina_Bool
_efl_net_socket_ssl_efl_io_closer_close_on_invalidate_get(const Eo *o EINA_UNUSED, Efl_Net_Socket_Ssl_Data *pd)
{
   return pd->sock && efl_io_closer_close_on_invalidate_get(pd->sock);
}

EOLIAN static const char *
_efl_net_socket_ssl_efl_net_socket_address_local_get(const Eo *o EINA_UNUSED, Efl_Net_Socket_Ssl_Data *pd)
{
   if (!pd->sock) return "unbound";
   return efl_net_socket_address_local_get(pd->sock);
}

EOLIAN static const char *
_efl_net_socket_ssl_efl_net_socket_address_remote_get(const Eo *o EINA_UNUSED, Efl_Net_Socket_Ssl_Data *pd)
{
   if (!pd->sock) return "unbound";
   return efl_net_socket_address_remote_get(pd->sock);
}

#include "efl_net_socket_ssl.eo.c"