aboutsummaryrefslogtreecommitdiffstats
path: root/libtransport/src/implementation/p2psecure_socket_producer.cc
blob: aa14f9e37f0fccbc54b5b7bd2fdc01a7e14b1d9c (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
/*
 * Copyright (c) 2017-2020 Cisco and/or its affiliates.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at:
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <hicn/transport/core/interest.h>
#include <implementation/p2psecure_socket_producer.h>
// #include <implementation/tls_rtc_socket_producer.h>
#include <implementation/tls_socket_producer.h>
#include <interfaces/tls_rtc_socket_producer.h>
#include <interfaces/tls_socket_producer.h>
#include <openssl/bio.h>
#include <openssl/rand.h>
#include <openssl/ssl.h>

namespace transport {
namespace implementation {

/* Workaround to prevent content with expiry time equal to 0 to be lost when
 * pushed in the forwarder */
#define HICN_HANDSHAKE_CONTENT_EXPIRY_TIME 100;

P2PSecureProducerSocket::P2PSecureProducerSocket(
    interface::ProducerSocket *producer_socket)
    : ProducerSocket(producer_socket,
                     ProductionProtocolAlgorithms::BYTE_STREAM),
      mtx_(),
      cv_(),
      map_producers(),
      list_producers() {}

P2PSecureProducerSocket::P2PSecureProducerSocket(
    interface::ProducerSocket *producer_socket, bool rtc,
    const std::shared_ptr<auth::Identity> &identity)
    : ProducerSocket(producer_socket,
                     ProductionProtocolAlgorithms::BYTE_STREAM),
      rtc_(rtc),
      mtx_(),
      cv_(),
      map_producers(),
      list_producers() {
  /* Setup SSL context (identity and parameter to use TLS 1.3) */
  der_cert_ = parcKeyStore_GetDEREncodedCertificate(
      (identity->getSigner()->getParcKeyStore()));
  der_prk_ = parcKeyStore_GetDEREncodedPrivateKey(
      (identity->getSigner()->getParcKeyStore()));

  int cert_size = (int)parcBuffer_Limit(der_cert_);
  int prk_size = (int)parcBuffer_Limit(der_prk_);
  const uint8_t *cert =
      reinterpret_cast<uint8_t *>(parcBuffer_Overlay(der_cert_, cert_size));
  const uint8_t *prk =
      reinterpret_cast<uint8_t *>(parcBuffer_Overlay(der_prk_, prk_size));
  cert_509_ = d2i_X509(NULL, &cert, cert_size);
  pkey_rsa_ = d2i_AutoPrivateKey(NULL, &prk, prk_size);

  /* Set the callback so that when an interest is received we catch it and we
   * decrypt the payload before passing it to the application.  */
  ProducerSocket::setSocketOption(
      ProducerCallbacksOptions::INTEREST_INPUT,
      (ProducerInterestCallback)std::bind(
          &P2PSecureProducerSocket::onInterestCallback, this,
          std::placeholders::_1, std::placeholders::_2));
}

P2PSecureProducerSocket::~P2PSecureProducerSocket() {
  if (der_cert_) parcBuffer_Release(&der_cert_);
  if (der_prk_) parcBuffer_Release(&der_prk_);
}

void P2PSecureProducerSocket::initSessionSocket(
    std::unique_ptr<TLSProducerSocket> &producer) {
  producer->on_content_produced_application_ =
      this->on_content_produced_application_;
  producer->setSocketOption(CONTENT_OBJECT_EXPIRY_TIME,
                            this->content_object_expiry_time_);
  producer->setSocketOption(SIGNER, this->signer_);
  producer->setSocketOption(MAKE_MANIFEST, this->making_manifest_);
  producer->setSocketOption(DATA_PACKET_SIZE,
                            (uint32_t)(this->data_packet_size_));
  uint32_t output_buffer_size = 0;
  this->getSocketOption(GeneralTransportOptions::OUTPUT_BUFFER_SIZE,
                        output_buffer_size);
  producer->setSocketOption(GeneralTransportOptions::OUTPUT_BUFFER_SIZE,
                            output_buffer_size);

  if (!rtc_) {
    producer->setInterface(new interface::TLSProducerSocket(producer.get()));
  } else {
    // TODO
    // TLSRTCProducerSocket *rtc_producer =
    //     dynamic_cast<TLSRTCProducerSocket *>(producer.get());
    // rtc_producer->setInterface(
    //     new interface::TLSRTCProducerSocket(rtc_producer));
  }
}

void P2PSecureProducerSocket::onInterestCallback(interface::ProducerSocket &p,
                                                 Interest &interest) {
  std::unique_lock<std::mutex> lck(mtx_);
  std::unique_ptr<TLSProducerSocket> tls_producer;
  auto it = map_producers.find(interest.getName());

  if (it != map_producers.end()) {
    return;
  }

  if (!rtc_) {
    tls_producer =
        std::make_unique<TLSProducerSocket>(nullptr, this, interest.getName());
  } else {
    // TODO
    // tls_producer = std::make_unique<TLSRTCProducerSocket>(nullptr, this,
    //                                                       interest.getName());
  }

  initSessionSocket(tls_producer);
  TLSProducerSocket *tls_producer_ptr = tls_producer.get();
  map_producers.insert({interest.getName(), move(tls_producer)});

  TRANSPORT_LOGD("Start handshake at %s",
                 interest.getName().toString().c_str());

  if (!rtc_) {
    tls_producer_ptr->onInterest(*tls_producer_ptr, interest);
    tls_producer_ptr->async_accept();
  } else {
    // TODO
    // TLSRTCProducerSocket *rtc_producer_ptr =
    //     dynamic_cast<TLSRTCProducerSocket *>(tls_producer_ptr);
    // rtc_producer_ptr->onInterest(*rtc_producer_ptr, interest);
    // rtc_producer_ptr->async_accept();
  }
}

uint32_t P2PSecureProducerSocket::produceDatagram(
    const Name &content_name, std::unique_ptr<utils::MemBuf> &&buffer) {
  // TODO
  throw errors::NotImplementedException();

  if (!rtc_) {
    throw errors::RuntimeException(
        "RTC must be the transport protocol to start the production of current "
        "data. Aborting.");
  }

  std::unique_lock<std::mutex> lck(mtx_);

  if (list_producers.empty()) cv_.wait(lck);

  // TODO
  // for (auto it = list_producers.cbegin(); it != list_producers.cend(); it++)
  // {
  //   TLSRTCProducerSocket *rtc_producer =
  //       dynamic_cast<TLSRTCProducerSocket *>(it->get());
  //   rtc_producer->produce(utils::MemBuf::copyBuffer(buffer, buffer_size));
  // }

  return 0;
}

uint32_t P2PSecureProducerSocket::produceStream(
    const Name &content_name, std::unique_ptr<utils::MemBuf> &&buffer,
    bool is_last, uint32_t start_offset) {
  if (rtc_) {
    throw errors::RuntimeException(
        "RTC transport protocol is not compatible with the production of "
        "current data. Aborting.");
  }

  std::unique_lock<std::mutex> lck(mtx_);
  uint32_t segments = 0;

  if (list_producers.empty()) cv_.wait(lck);

  for (auto it = list_producers.cbegin(); it != list_producers.cend(); it++)
    segments += (*it)->produceStream(content_name, buffer->clone(), is_last,
                                     start_offset);

  return segments;
}

uint32_t P2PSecureProducerSocket::produceStream(const Name &content_name,
                                                const uint8_t *buffer,
                                                size_t buffer_size,
                                                bool is_last,
                                                uint32_t start_offset) {
  if (rtc_) {
    throw errors::RuntimeException(
        "RTC transport protocol is not compatible with the production of "
        "current data. Aborting.");
  }

  std::unique_lock<std::mutex> lck(mtx_);
  uint32_t segments = 0;
  if (list_producers.empty()) cv_.wait(lck);

  for (auto it = list_producers.cbegin(); it != list_producers.cend(); it++)
    segments += (*it)->produceStream(content_name, buffer, buffer_size, is_last,
                                     start_offset);

  return segments;
}

// void P2PSecureProducerSocket::asyncProduce(const Name &content_name,
//                                            const uint8_t *buf,
//                                            size_t buffer_size, bool is_last,
//                                            uint32_t *start_offset) {
//   if (rtc_) {
//     throw errors::RuntimeException(
//         "RTC transport protocol is not compatible with the production of "
//         "current data. Aborting.");
//   }

//   std::unique_lock<std::mutex> lck(mtx_);
//   if (list_producers.empty()) cv_.wait(lck);

//   for (auto it = list_producers.cbegin(); it != list_producers.cend(); it++)
//   {
//     (*it)->asyncProduce(content_name, buf, buffer_size, is_last,
//     start_offset);
//   }
// }

void P2PSecureProducerSocket::asyncProduce(
    Name content_name, std::unique_ptr<utils::MemBuf> &&buffer, bool is_last,
    uint32_t offset, uint32_t **last_segment) {
  if (rtc_) {
    throw errors::RuntimeException(
        "RTC transport protocol is not compatible with the production of "
        "current data. Aborting.");
  }

  std::unique_lock<std::mutex> lck(mtx_);
  if (list_producers.empty()) cv_.wait(lck);

  for (auto it = list_producers.cbegin(); it != list_producers.cend(); it++) {
    (*it)->asyncProduce(content_name, buffer->clone(), is_last, offset,
                        last_segment);
  }
}

/* Redefinition of socket options to avoid name hiding */
int P2PSecureProducerSocket::setSocketOption(
    int socket_option_key, ProducerInterestCallback socket_option_value) {
  if (!list_producers.empty()) {
    for (auto it = list_producers.cbegin(); it != list_producers.cend(); it++)
      (*it)->setSocketOption(socket_option_key, socket_option_value);
  }

  switch (socket_option_key) {
    case ProducerCallbacksOptions::INTEREST_INPUT:
      on_interest_input_decrypted_ = socket_option_value;
      return SOCKET_OPTION_SET;

    case ProducerCallbacksOptions::INTEREST_DROP:
      on_interest_dropped_input_buffer_ = socket_option_value;
      return SOCKET_OPTION_SET;

    case ProducerCallbacksOptions::INTEREST_PASS:
      on_interest_inserted_input_buffer_ = socket_option_value;
      return SOCKET_OPTION_SET;

    case ProducerCallbacksOptions::CACHE_HIT:
      on_interest_satisfied_output_buffer_ = socket_option_value;
      return SOCKET_OPTION_SET;

    case ProducerCallbacksOptions::CACHE_MISS:
      on_interest_process_decrypted_ = socket_option_value;
      return SOCKET_OPTION_SET;

    default:
      return SOCKET_OPTION_NOT_SET;
  }
}

int P2PSecureProducerSocket::setSocketOption(
    int socket_option_key,
    const std::shared_ptr<auth::Signer> &socket_option_value) {
  if (!list_producers.empty())
    for (auto it = list_producers.cbegin(); it != list_producers.cend(); it++)
      (*it)->setSocketOption(socket_option_key, socket_option_value);

  switch (socket_option_key) {
    case GeneralTransportOptions::SIGNER: {
      signer_.reset();
      signer_ = socket_option_value;

      return SOCKET_OPTION_SET;
    }
    default:
      return SOCKET_OPTION_NOT_SET;
  }
}

int P2PSecureProducerSocket::setSocketOption(int socket_option_key,
                                             uint32_t socket_option_value) {
  if (!list_producers.empty()) {
    for (auto it = list_producers.cbegin(); it != list_producers.cend(); it++)
      (*it)->setSocketOption(socket_option_key, socket_option_value);
  }
  switch (socket_option_key) {
    case GeneralTransportOptions::CONTENT_OBJECT_EXPIRY_TIME:
      content_object_expiry_time_ =
          socket_option_value;  // HICN_HANDSHAKE_CONTENT_EXPIRY_TIME;
      return SOCKET_OPTION_SET;
  }
  return ProducerSocket::setSocketOption(socket_option_key,
                                         socket_option_value);
}

int P2PSecureProducerSocket::setSocketOption(int socket_option_key,
                                             bool socket_option_value) {
  if (!list_producers.empty())
    for (auto it = list_producers.cbegin(); it != list_producers.cend(); it++)
      (*it)->setSocketOption(socket_option_key, socket_option_value);

  return ProducerSocket::setSocketOption(socket_option_key,
                                         socket_option_value);
}

int P2PSecureProducerSocket::setSocketOption(int socket_option_key,
                                             Name *socket_option_value) {
  if (!list_producers.empty())
    for (auto it = list_producers.cbegin(); it != list_producers.cend(); it++)
      (*it)->setSocketOption(socket_option_key, socket_option_value);

  return ProducerSocket::setSocketOption(socket_option_key,
                                         socket_option_value);
}

int P2PSecureProducerSocket::setSocketOption(
    int socket_option_key, ProducerContentObjectCallback socket_option_value) {
  if (!list_producers.empty())
    for (auto it = list_producers.cbegin(); it != list_producers.cend(); it++)
      (*it)->setSocketOption(socket_option_key, socket_option_value);

  return ProducerSocket::setSocketOption(socket_option_key,
                                         socket_option_value);
}

int P2PSecureProducerSocket::setSocketOption(
    int socket_option_key, ProducerContentCallback socket_option_value) {
  if (!list_producers.empty())
    for (auto it = list_producers.cbegin(); it != list_producers.cend(); it++)
      (*it)->setSocketOption(socket_option_key, socket_option_value);

  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 P2PSecureProducerSocket::setSocketOption(
    int socket_option_key, auth::CryptoHashType socket_option_value) {
  if (!list_producers.empty())
    for (auto it = list_producers.cbegin(); it != list_producers.cend(); it++)
      (*it)->setSocketOption(socket_option_key, socket_option_value);

  return ProducerSocket::setSocketOption(socket_option_key,
                                         socket_option_value);
}

int P2PSecureProducerSocket::setSocketOption(
    int socket_option_key, const std::string &socket_option_value) {
  if (!list_producers.empty())
    for (auto it = list_producers.cbegin(); it != list_producers.cend(); it++)
      (*it)->setSocketOption(socket_option_key, socket_option_value);

  return ProducerSocket::setSocketOption(socket_option_key,
                                         socket_option_value);
}

}  // namespace implementation
}  // namespace transport