aboutsummaryrefslogtreecommitdiffstats
path: root/libtransport/src/hicn/transport/interfaces/tls_socket_producer.cc
blob: ad85ec6eaeeeebb70ed87f43fdb6ae6221884418 (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
#include <hicn/transport/core/interest.h>
#include <hicn/transport/interfaces/p2psecure_socket_producer.h>
#include <hicn/transport/interfaces/tls_socket_producer.h>

#include <openssl/bio.h>
#include <openssl/rand.h>
#include <openssl/ssl.h>

namespace transport {

namespace interface {

/* Return the number of read bytes in readbytes */
int TLSProducerSocket::read(BIO *b, char *buf, size_t size, size_t *readbytes) {
  int ret;

  if (size > INT_MAX) size = INT_MAX;

  ret = TLSProducerSocket::readOld(b, buf, (int)size);

  if (ret <= 0) {
    *readbytes = 0;
    return ret;
  }

  *readbytes = (size_t)ret;

  return 1;
}

/* Return the number of read bytes in the return param */
int TLSProducerSocket::readOld(BIO *b, char *buf, int size) {
  TLSProducerSocket *socket;
  socket = (TLSProducerSocket *)BIO_get_data(b);

  /* take a lock on the mutex. It will be unlocked by */
  std::unique_lock<std::mutex> lck(socket->mtx_);
  if (!socket->something_to_read_) {
    (socket->cv_).wait(lck);
  }

  /* Either there already is something to read, or the thread has been waken up
   */
  /* must return the payload in the interest */

  utils::MemBuf *membuf = socket->packet_->next();
  int size_to_read;
  if ((int)membuf->length() > size) {
    size_to_read = size;
  } else {
    size_to_read = membuf->length();
    socket->something_to_read_ = false;
  }

  std::memcpy(buf, membuf->data(), size_to_read);
  membuf->trimStart(size_to_read);

  return size_to_read;
}

/* Return the number of written bytes in written */
int TLSProducerSocket::write(BIO *b, const char *buf, size_t size,
                             size_t *written) {
  int ret;

  if (size > INT_MAX) size = INT_MAX;

  ret = TLSProducerSocket::writeOld(b, buf, (int)size);

  if (ret <= 0) {
    *written = 0;
    return ret;
  }

  *written = (size_t)ret;

  return 1;
}

/* Return the number of written bytes in the return param */
int TLSProducerSocket::writeOld(BIO *b, const char *buf, int num) {
  TLSProducerSocket *socket;
  socket = (TLSProducerSocket *)BIO_get_data(b);

  if ((SSL_in_before(socket->ssl_) || SSL_in_init(socket->ssl_)) &&
      socket->first_) {
    //! socket->tls_chunks_ corresponds to is_last
    socket->tls_chunks_--;
    bool making_manifest = socket->parent_->making_manifest_;
    socket->parent_->setSocketOption(GeneralTransportOptions::MAKE_MANIFEST,
                                     false);
    socket->parent_->ProducerSocket::produce(
        socket->name_, (const uint8_t *)buf, num, socket->tls_chunks_ == 0,
        socket->last_segment_);
    socket->parent_->setSocketOption(GeneralTransportOptions::MAKE_MANIFEST,
                                     making_manifest);
    socket->first_ = false;
  } else {
    socket->still_writing_ = true;

    std::unique_ptr<utils::MemBuf> mbuf =
        utils::MemBuf::copyBuffer(buf, (std::size_t)num, 0, 0);
    auto a = mbuf.release();
    socket->async_thread_.add([socket = socket, a]() {
      socket->tls_chunks_--;
      socket->to_call_oncontentproduced_--;
      auto mbuf = std::unique_ptr<utils::MemBuf>(a);
      socket->last_segment_ += socket->ProducerSocket::produce(
          socket->name_, std::move(mbuf), socket->tls_chunks_ == 0,
          socket->last_segment_);
      ProducerContentCallback on_content_produced_application;
      socket->getSocketOption(ProducerCallbacksOptions::CONTENT_PRODUCED,
                              on_content_produced_application);
      if (socket->to_call_oncontentproduced_ == 0 &&
          on_content_produced_application) {
        on_content_produced_application(*socket, std::error_code(), 0);
      }
    });
  }

  return num;
}

TLSProducerSocket::TLSProducerSocket(P2PSecureProducerSocket *parent,
                                     const Name &handshake_name)
    : ProducerSocket(),
      on_content_produced_application_(),
      mtx_(),
      cv_(),
      something_to_read_(),
      name_(),
      last_segment_(0),
      parent_(parent),
      first_(true),
      handshake_name_(handshake_name),
      tls_chunks_(0),
      to_call_oncontentproduced_(0),
      still_writing_(false),
      encryption_thread_() {
  const SSL_METHOD *meth = TLS_server_method();
  ctx_ = SSL_CTX_new(meth);

  /*
   * Setup SSL context (identity and parameter to use TLS 1.3)
   */
  SSL_CTX_use_certificate(ctx_, parent->cert_509_);
  SSL_CTX_use_PrivateKey(ctx_, parent->pkey_rsa_);

  int result =
      SSL_CTX_set_ciphersuites(ctx_,
                               "TLS_AES_256_GCM_SHA384:TLS_CHACHA20_POLY1305_"
                               "SHA256:TLS_AES_128_GCM_SHA256");
  if (result != 1) {
    throw errors::RuntimeException(
        "Unable to set cipher list on TLS subsystem. Aborting.");
  }

  // We force it to be TLS 1.3
  SSL_CTX_set_min_proto_version(ctx_, TLS1_3_VERSION);
  SSL_CTX_set_max_proto_version(ctx_, TLS1_3_VERSION);
  SSL_CTX_set_verify(ctx_, SSL_VERIFY_NONE, NULL);
  SSL_CTX_set_num_tickets(ctx_, 0);

  result = SSL_CTX_add_custom_ext(
      ctx_, 100, SSL_EXT_CLIENT_HELLO | SSL_EXT_TLS1_3_ENCRYPTED_EXTENSIONS,
      TLSProducerSocket::addHicnKeyIdCb, TLSProducerSocket::freeHicnKeyIdCb,
      this, TLSProducerSocket::parseHicnKeyIdCb, NULL);

  ssl_ = SSL_new(ctx_);
  /*
   * Setup this producer socker as the bio that TLS will use to write and read
   * data (in stream mode)
   */
  BIO_METHOD *bio_meth =
      BIO_meth_new(BIO_TYPE_ACCEPT, "secure producer socket");
  BIO_meth_set_read(bio_meth, TLSProducerSocket::readOld);
  BIO_meth_set_write(bio_meth, TLSProducerSocket::writeOld);
  BIO_meth_set_ctrl(bio_meth, TLSProducerSocket::ctrl);
  BIO *bio = BIO_new(bio_meth);
  BIO_set_init(bio, 1);
  BIO_set_data(bio, this);
  SSL_set_bio(ssl_, bio, bio);
  /*
   * Set the callback so that when an interest is received we catch it and we
   * decrypt the payload before passing it to the application.
   */
  this->ProducerSocket::setSocketOption(
      ProducerCallbacksOptions::CACHE_MISS,
      (ProducerInterestCallback)std::bind(&TLSProducerSocket::cacheMiss, this,
                                          std::placeholders::_1,
                                          std::placeholders::_2));
  this->ProducerSocket::setSocketOption(
      ProducerCallbacksOptions::CONTENT_PRODUCED,
      (ProducerContentCallback)bind(
          &TLSProducerSocket::onContentProduced, this, std::placeholders::_1,
          std::placeholders::_2, std::placeholders::_3));
}

void TLSProducerSocket::accept() {
  if (SSL_in_before(ssl_) || SSL_in_init(ssl_)) {
    tls_chunks_ = 1;
    int result = SSL_accept(ssl_);
    if (result != 1)
      throw errors::RuntimeException("Unable to perform client handshake");
  }
  TRANSPORT_LOGD("Handshake performed!");
  parent_->list_secure_producers.push_front(
      std::move(parent_->map_secure_producers[handshake_name_]));
  parent_->map_secure_producers.erase(handshake_name_);

  ProducerInterestCallback on_interest_process_decrypted;
  getSocketOption(ProducerCallbacksOptions::CACHE_MISS,
                  on_interest_process_decrypted);

  if (on_interest_process_decrypted) {
    Interest inter(std::move(packet_));
    on_interest_process_decrypted(*this, inter);
  } else {
    throw errors::RuntimeException(
        "On interest process unset. Unable to perform handshake");
  }
}

int TLSProducerSocket::async_accept() {
  if (!async_thread_.stopped()) {
    async_thread_.add([this]() { this->accept(); });
  } else {
    throw errors::RuntimeException(
        "Async thread not running, impossible to perform handshake");
  }

  return 1;
}

void TLSProducerSocket::onInterest(ProducerSocket &p, Interest &interest) {
  /* Based on the state machine of (D)TLS, we know what action to do */
  if (SSL_in_before(ssl_) || SSL_in_init(ssl_)) {
    std::unique_lock<std::mutex> lck(mtx_);
    name_ = interest.getName();
    something_to_read_ = true;
    packet_ = interest.acquireMemBufReference();
    if (head_) {
      payload_->prependChain(interest.getPayload());
    } else {
      payload_ = interest.getPayload();  // std::move(interest.getPayload());
    }
    cv_.notify_one();
  } else {
    name_ = interest.getName();
    packet_ = interest.acquireMemBufReference();
    payload_ = interest.getPayload();
    something_to_read_ = true;

    if (interest.getPayload()->length() > 0)
      SSL_read(
          ssl_,
          const_cast<unsigned char *>(interest.getPayload()->writableData()),
          interest.getPayload()->length());
  }

  ProducerInterestCallback on_interest_input_decrypted;
  getSocketOption(ProducerCallbacksOptions::INTEREST_INPUT,
                  on_interest_input_decrypted);
  if (on_interest_input_decrypted)
    (on_interest_input_decrypted)(*this, interest);
}

void TLSProducerSocket::cacheMiss(ProducerSocket &p, Interest &interest) {
  if (SSL_in_before(ssl_) || SSL_in_init(ssl_)) {
    std::unique_lock<std::mutex> lck(mtx_);
    name_ = interest.getName();
    something_to_read_ = true;
    packet_ = interest.acquireMemBufReference();
    payload_ = interest.getPayload();
    cv_.notify_one();
  } else {
    name_ = interest.getName();
    packet_ = interest.acquireMemBufReference();
    payload_ = interest.getPayload();
    something_to_read_ = true;

    if (interest.getPayload()->length() > 0)
      SSL_read(
          ssl_,
          const_cast<unsigned char *>(interest.getPayload()->writableData()),
          interest.getPayload()->length());

    if (on_interest_process_decrypted_ != VOID_HANDLER)
      on_interest_process_decrypted_(*this, interest);
  }
}

void TLSProducerSocket::onContentProduced(ProducerSocket &p,
                                          const std::error_code &err,
                                          uint64_t bytes_written) {}

uint32_t TLSProducerSocket::produce(Name content_name,
                                    std::unique_ptr<utils::MemBuf> &&buffer,
                                    bool is_last, uint32_t start_offset) {
  if (SSL_in_before(ssl_) || SSL_in_init(ssl_)) {
    throw errors::RuntimeException(
        "New handshake on the same P2P secure producer socket not supported");
  }
  size_t buf_size = buffer->length();
  name_ = served_namespaces_.front().mapName(content_name);

  tls_chunks_ = to_call_oncontentproduced_ =
      ceil((float)buf_size / (float)SSL3_RT_MAX_PLAIN_LENGTH);

  if (!is_last) {
    tls_chunks_++;
  }

  last_segment_ = start_offset;

  SSL_write(ssl_, buffer->data(), buf_size);
  BIO *wbio = SSL_get_wbio(ssl_);
  int i = BIO_flush(wbio);
  (void)i;  // To shut up gcc 5

  return 0;
}

void TLSProducerSocket::asyncProduce(const Name &content_name,
                                     const uint8_t *buf, size_t buffer_size,
                                     bool is_last, uint32_t *start_offset) {
  if (!encryption_thread_.stopped()) {
    encryption_thread_.add([this, content_name, buffer = buf,
                            size = buffer_size, is_last, start_offset]() {
      if (start_offset != NULL) {
        produce(content_name, buffer, size, is_last, *start_offset);
      } else {
        produce(content_name, buffer, size, is_last, 0);
      }
    });
  }
}

void TLSProducerSocket::asyncProduce(Name content_name,
                                     std::unique_ptr<utils::MemBuf> &&buffer,
                                     bool is_last, uint32_t offset,
                                     uint32_t **last_segment) {
  if (!encryption_thread_.stopped()) {
    auto a = buffer.release();
    encryption_thread_.add(
        [this, content_name, a, is_last, offset, last_segment]() {
          auto buf = std::unique_ptr<utils::MemBuf>(a);
          if (last_segment != NULL) {
            *last_segment = &last_segment_;
          }
          produce(content_name, std::move(buf), is_last, offset);
        });
  }
}

void TLSProducerSocket::asyncProduce(ContentObject &content_object) {
  throw errors::RuntimeException("API not supported");
}

void TLSProducerSocket::produce(ContentObject &content_object) {
  throw errors::RuntimeException("API not supported");
}

long TLSProducerSocket::ctrl(BIO *b, int cmd, long num, void *ptr) {
  if (cmd == BIO_CTRL_FLUSH) {
  }
  return 1;
}

int TLSProducerSocket::addHicnKeyIdCb(SSL *s, unsigned int ext_type,
                                      unsigned int context,
                                      const unsigned char **out, size_t *outlen,
                                      X509 *x, size_t chainidx, int *al,
                                      void *add_arg) {
  TLSProducerSocket *socket = reinterpret_cast<TLSProducerSocket *>(add_arg);
  if (ext_type == 100) {
    ip_prefix_t ip_prefix =
        socket->parent_->served_namespaces_.front().toIpPrefixStruct();
    int inet_family =
        socket->parent_->served_namespaces_.front().getAddressFamily();
    uint16_t prefix_len_bits =
        socket->parent_->served_namespaces_.front().getPrefixLength();
    uint8_t prefix_len_bytes = prefix_len_bits / 8;
    uint8_t prefix_len_u32 = prefix_len_bits / 32;

    ip_prefix_t *out_ip = (ip_prefix_t *)malloc(sizeof(ip_prefix_t));
    out_ip->family = inet_family;
    out_ip->len = prefix_len_bits + 32;
    u8 *out_ip_buf = const_cast<u8 *>(
        ip_address_get_buffer(&(out_ip->address), inet_family));
    *out = reinterpret_cast<unsigned char *>(out_ip);

    RAND_bytes((unsigned char *)&socket->key_id_, 4);

    memcpy(out_ip_buf, ip_address_get_buffer(&(ip_prefix.address), inet_family),
           prefix_len_bytes);
    memcpy((out_ip_buf + prefix_len_bytes), &socket->key_id_, 4);
    *outlen = sizeof(ip_prefix_t);

    ip_address_t mask = {};
    ip_address_t keyId_component = {};
    u32 *mask_buf;
    u32 *keyId_component_buf;
    switch (inet_family) {
      case AF_INET:
        mask_buf = &(mask.v4.as_u32);
        keyId_component_buf = &(keyId_component.v4.as_u32);
        break;
      case AF_INET6:
        mask_buf = mask.v6.as_u32;
        keyId_component_buf = keyId_component.v6.as_u32;
        break;
      default:
        throw errors::RuntimeException("Unknown protocol");
    }

    if (prefix_len_bits > (inet_family == AF_INET6 ? IPV6_ADDR_LEN_BITS - 32
                                                   : IPV4_ADDR_LEN_BITS - 32))
      throw errors::RuntimeException(
          "Not enough space in the content name to add key_id");

    mask_buf[prefix_len_u32] = 0xffffffff;
    keyId_component_buf[prefix_len_u32] = socket->key_id_;
    socket->last_segment_ = 0;

    socket->on_interest_process_decrypted_ =
        socket->parent_->on_interest_process_decrypted_;

    socket->registerPrefix(
        Prefix(socket->parent_->served_namespaces_.front().getName(
                   Name(inet_family, (uint8_t *)&mask),
                   Name(inet_family, (uint8_t *)&keyId_component),
                   socket->parent_->served_namespaces_.front().getName()),
               out_ip->len));
    socket->connect();
  }
  return 1;
}

void TLSProducerSocket::freeHicnKeyIdCb(SSL *s, unsigned int ext_type,
                                        unsigned int context,
                                        const unsigned char *out,
                                        void *add_arg) {
  free(const_cast<unsigned char *>(out));
}

int TLSProducerSocket::parseHicnKeyIdCb(SSL *s, unsigned int ext_type,
                                        unsigned int context,
                                        const unsigned char *in, size_t inlen,
                                        X509 *x, size_t chainidx, int *al,
                                        void *add_arg) {
  return 1;
}

int TLSProducerSocket::setSocketOption(
    int socket_option_key, ProducerInterestCallback socket_option_value) {
  return rescheduleOnIOService(
      socket_option_key, socket_option_value,
      [this](int socket_option_key,
             ProducerInterestCallback socket_option_value) -> int {
        int result = SOCKET_OPTION_SET;
        switch (socket_option_key) {
          case ProducerCallbacksOptions::INTEREST_INPUT:
            on_interest_input_decrypted_ = socket_option_value;
            break;

          case ProducerCallbacksOptions::INTEREST_DROP:
            on_interest_dropped_input_buffer_ = socket_option_value;
            break;

          case ProducerCallbacksOptions::INTEREST_PASS:
            on_interest_inserted_input_buffer_ = socket_option_value;
            break;

          case ProducerCallbacksOptions::CACHE_HIT:
            on_interest_satisfied_output_buffer_ = socket_option_value;
            break;

          case ProducerCallbacksOptions::CACHE_MISS:
            on_interest_process_decrypted_ = socket_option_value;
            break;

          default:
            result = SOCKET_OPTION_NOT_SET;
            break;
        }
        return result;
      });
}

int TLSProducerSocket::setSocketOption(
    int socket_option_key, ProducerContentCallback socket_option_value) {
  return rescheduleOnIOService(
      socket_option_key, socket_option_value,
      [this](int socket_option_key,
             ProducerContentCallback socket_option_value) -> int {
        switch (socket_option_key) {
          case ProducerCallbacksOptions::CONTENT_PRODUCED:
            on_content_produced_application_ = socket_option_value;
            break;

          default:
            return SOCKET_OPTION_NOT_SET;
        }

        return SOCKET_OPTION_SET;
      });
}

int TLSProducerSocket::getSocketOption(
    int socket_option_key, ProducerContentCallback **socket_option_value) {
  return rescheduleOnIOService(
      socket_option_key, socket_option_value,
      [this](int socket_option_key,
             ProducerContentCallback **socket_option_value) -> int {
        switch (socket_option_key) {
          case ProducerCallbacksOptions::CONTENT_PRODUCED:
            *socket_option_value = &on_content_produced_application_;
            break;

          default:
            return SOCKET_OPTION_NOT_GET;
        }

        return SOCKET_OPTION_GET;
      });
}

int TLSProducerSocket::getSocketOption(
    int socket_option_key, ProducerContentCallback &socket_option_value) {
  return rescheduleOnIOServiceWithReference(
      socket_option_key, socket_option_value,
      [this](int socket_option_key,
             ProducerContentCallback &socket_option_value) -> int {
        switch (socket_option_key) {
          case ProducerCallbacksOptions::CONTENT_PRODUCED:
            socket_option_value = on_content_produced_application_;
            break;

          default:
            return SOCKET_OPTION_NOT_GET;
        }

        return SOCKET_OPTION_GET;
      });
}

int TLSProducerSocket::getSocketOption(
    int socket_option_key, ProducerInterestCallback &socket_option_value) {
  // Reschedule the function on the io_service to avoid race condition in case
  // setSocketOption is called while the io_service is running.
  return rescheduleOnIOServiceWithReference(
      socket_option_key, socket_option_value,
      [this](int socket_option_key,
             ProducerInterestCallback &socket_option_value) -> int {
        switch (socket_option_key) {
          case ProducerCallbacksOptions::INTEREST_INPUT:
            socket_option_value = on_interest_input_decrypted_;
            break;

          case ProducerCallbacksOptions::INTEREST_DROP:
            socket_option_value = on_interest_dropped_input_buffer_;
            break;

          case ProducerCallbacksOptions::INTEREST_PASS:
            socket_option_value = on_interest_inserted_input_buffer_;
            break;

          case ProducerCallbacksOptions::CACHE_HIT:
            socket_option_value = on_interest_satisfied_output_buffer_;
            break;

          case ProducerCallbacksOptions::CACHE_MISS:
            socket_option_value = on_interest_process_decrypted_;
            break;

          default:
            return SOCKET_OPTION_NOT_GET;
        }

        return SOCKET_OPTION_GET;
      });
}

}  // namespace interface

}  // namespace transport