Пример #1
0
/*---------------------------------------------------------------------------*/
void
init_dtls() {
  static dtls_handler_t cb = {
    .write = send_to_peer,
    .read  = read_from_peer,
    .event = NULL,
#ifdef DTLS_PSK
    .get_psk_info = get_psk_info,
#endif
#ifdef DTLS_ECC
    .get_ecdsa_key = NULL,
    .verify_ecdsa_key = NULL,
#endif
  };

  server_conn = udp_new(NULL, 0, NULL);
  udp_bind(server_conn, UIP_HTONS(DTLS_ECHO_PORT));

  dtls_set_log_level(DTLS_LOG_DEBUG);

  dtls_context = dtls_new_context(server_conn);
  if (dtls_context)
    dtls_set_handler(dtls_context, &cb);

  printf("DTLS server started\n");
}
Пример #2
0
void
init_dtls(session_t *dst) {
  static dtls_handler_t cb = {
    .write = send_to_peer,
    .read  = read_from_peer,
    .event = NULL,
    .get_key = get_key
  };
  PRINTF("DTLS client started\n");

  print_local_addresses();

  dst->size = sizeof(dst->addr) + sizeof(dst->port);
  dst->port = UIP_HTONS(20220);

  set_connection_address(&dst->addr);
  client_conn = udp_new(&dst->addr, 0, NULL);
  udp_bind(client_conn, dst->port);

  PRINTF("set connection address to ");
  PRINT6ADDR(&dst->addr);
  PRINTF(":%d\n", uip_ntohs(dst->port));

  set_log_level(LOG_DEBUG);

  dtls_context = dtls_new_context(client_conn);
  if (dtls_context)
    dtls_set_handler(dtls_context, &cb);
}
Пример #3
0
void
init_dtls() {
  static dtls_handler_t cb = {
    .write = send_to_peer,
    .read  = read_from_peer,
    .event = NULL,
#ifdef DTLS_PSK
    .get_psk_info = get_psk_info,
#endif /* DTLS_PSK */
#ifdef DTLS_ECC
    .get_ecdsa_key = get_ecdsa_key,
    .verify_ecdsa_key = verify_ecdsa_key
#endif /* DTLS_ECC */
  };
#if 0
  uip_ipaddr_t ipaddr;
  /* struct uip_ds6_addr *root_if; */
#endif /* UIP_CONF_ROUTER */

  PRINTF("DTLS server started\n");

#if 0  /* TEST */
  memset(&tmp_addr, 0, sizeof(rimeaddr_t));
  if(get_eui64_from_eeprom(tmp_addr.u8));
#if UIP_CONF_IPV6
  memcpy(&uip_lladdr.addr, &tmp_addr.u8, 8);
#endif
#endif /* TEST */

#if 0
/*   uip_ip6addr(&ipaddr, 0xaaaa, 0, 0, 0, 0, 0, 0, 0); */
/*   uip_ds6_set_addr_iid(&ipaddr, &uip_lladdr); */
/*   uip_ds6_addr_add(&ipaddr, 0, ADDR_AUTOCONF); */

/*   create_rpl_dag(&ipaddr); */
/* #else */
  /* uip_ds6_addr_add(&ipaddr, 0, ADDR_AUTOCONF); */

  uip_ip6addr(&ipaddr, 0xaaaa, 0,0,0,0x0200,0,0,0x0003);
  uip_ds6_addr_add(&ipaddr, 0, ADDR_MANUAL);

  create_rpl_dag(&ipaddr);
#endif /* UIP_CONF_ROUTER */

  server_conn = udp_new(NULL, 0, NULL);
  udp_bind(server_conn, UIP_HTONS(20220));

  dtls_set_log_level(DTLS_LOG_DEBUG);

  dtls_context = dtls_new_context(server_conn);
  if (dtls_context)
    dtls_set_handler(dtls_context, &cb);
}
Пример #4
0
dtls_context_t * get_dtls_context(dtls_connection_t * connList) {
    if (dtlsContext == NULL) {
        dtls_init();
        dtlsContext = dtls_new_context(connList);
        if (dtlsContext == NULL)
            fprintf(stderr, "Failed to create the DTLS context\r\n");
        dtls_set_handler(dtlsContext, &cb);
    }else{
        dtlsContext->app = connList;
    }
    return dtlsContext;
}
Пример #5
0
/*---------------------------------------------------------------------------*/
coap_context_t *
coap_context_new(uip_ipaddr_t *my_addr, uint16_t port)
{
  coap_context_t *ctx = NULL;
  int i;
  for(i = 0; i < MAX_CONTEXTS; i++) {
    if(!coap_contexts[i].is_used) {
      ctx = &coap_contexts[i];
      break;
    }
  }

  if(ctx == NULL) {
    PRINTF("coap-context: no free contexts\n");
    return NULL;
  }

  memset(ctx, 0, sizeof(coap_context_t));

  /* initialize context */
  ctx->dtls_context = dtls_new_context(ctx);
  if(ctx->dtls_context == NULL) {
    PRINTF("coap-context: failed to get DTLS context\n");
    uip_udp_remove(uip_udp_conn(ctx->buf));
    return NULL;
  }

  ctx->dtls_handler.write = send_to_peer;
  ctx->dtls_handler.read = get_from_peer;
  ctx->dtls_handler.event = event;

  dtls_set_handler(ctx->dtls_context, &ctx->dtls_handler);

#ifdef NETSTACK_CONF_WITH_IPV6
  memcpy(&ctx->my_addr.in6_addr, my_addr, sizeof(ctx->my_addr.in6_addr));
#else
  memcpy(&ctx->my_addr.in_addr, my_addr, sizeof(ctx->my_addr.in_addr));
#endif
  ctx->my_port = port;

  ctx->process = PROCESS_CURRENT();
  ctx->is_used = 1;
  PRINTF("Secure listening on port %u\n", port);

  return ctx;
}
Пример #6
0
void
init_dtls() {
  static dtls_handler_t cb = {
    .write = send_to_peer,
    .read  = read_from_peer,
    .event = NULL,
    .get_psk_key = get_psk_key
  };
#if UIP_CONF_ROUTER
  uip_ipaddr_t ipaddr;
#endif /* UIP_CONF_ROUTER */

  PRINTF("DTLS server started\n");

#if 0  /* TEST */
  memset(&tmp_addr, 0, sizeof(linkaddr_t));
  if(get_eui64_from_eeprom(tmp_addr.u8));
#if NETSTACK_CONF_WITH_IPV6
  memcpy(&uip_lladdr.addr, &tmp_addr.u8, 8);
#endif
#endif /* TEST */

#if UIP_CONF_ROUTER
  uip_ip6addr(&ipaddr, 0xaaaa, 0, 0, 0, 0, 0, 0, 0);
  uip_ds6_set_addr_iid(&ipaddr, &uip_lladdr);
  uip_ds6_addr_add(&ipaddr, 0, ADDR_AUTOCONF);
#endif /* UIP_CONF_ROUTER */

  server_conn = udp_new(NULL, 0, NULL);
  udp_bind(server_conn, UIP_HTONS(20220));

  dtls_set_log_level(LOG_DEBUG);

  dtls_context = dtls_new_context(server_conn);
  if (dtls_context)
    dtls_set_handler(dtls_context, &cb);
}
Пример #7
0
int 
main(int argc, char **argv) {
  dtls_context_t *dtls_context = NULL;
  fd_set rfds, wfds;
  struct timeval timeout;
  unsigned short port = DEFAULT_PORT;
  char port_str[NI_MAXSERV] = "0";
  log_t log_level = LOG_WARN;
  int fd, result;
  int on = 1;
  int opt, res;
  session_t dst;

  dtls_init();
  snprintf(port_str, sizeof(port_str), "%d", port);

  while ((opt = getopt(argc, argv, "p:o:v:")) != -1) {
    switch (opt) {
    case 'p' :
      strncpy(port_str, optarg, NI_MAXSERV-1);
      port_str[NI_MAXSERV - 1] = '\0';
      break;
    case 'o' :
      output_file.length = strlen(optarg);
      output_file.s = (unsigned char *)malloc(output_file.length + 1);
      
      if (!output_file.s) {
	dsrv_log(LOG_CRIT, "cannot set output file: insufficient memory\n");
	exit(-1);
      } else {
	/* copy filename including trailing zero */
	memcpy(output_file.s, optarg, output_file.length + 1);
      }
      break;
    case 'v' :
      log_level = strtol(optarg, NULL, 10);
      break;
    default:
      usage(argv[0], PACKAGE_VERSION);
      exit(1);
    }
  }

  set_log_level(log_level);
  
  if (argc <= optind) {
    usage(argv[0], PACKAGE_VERSION);
    exit(1);
  }
  
  memset(&dst, 0, sizeof(session_t));
  /* resolve destination address where server should be sent */
  res = resolve_address(argv[optind++], &dst.addr.sa);
  if (res < 0) {
    dsrv_log(LOG_EMERG, "failed to resolve address\n");
    exit(-1);
  }
  dst.size = res;

  /* use port number from command line when specified or the listen
     port, otherwise */
  dst.addr.sin.sin_port = htons(atoi(optind < argc ? argv[optind++] : port_str));

  
  /* init socket and set it to non-blocking */
  fd = socket(dst.addr.sa.sa_family, SOCK_DGRAM, 0);

  if (fd < 0) {
    dsrv_log(LOG_ALERT, "socket: %s\n", strerror(errno));
    return 0;
  }

  if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on) ) < 0) {
    dsrv_log(LOG_ALERT, "setsockopt SO_REUSEADDR: %s\n", strerror(errno));
  }
#if 0
  flags = fcntl(fd, F_GETFL, 0);
  if (flags < 0 || fcntl(fd, F_SETFL, flags | O_NONBLOCK) < 0) {
    dsrv_log(LOG_ALERT, "fcntl: %s\n", strerror(errno));
    goto error;
  }
#endif
  on = 1;
#ifdef IPV6_RECVPKTINFO
  if (setsockopt(fd, IPPROTO_IPV6, IPV6_RECVPKTINFO, &on, sizeof(on) ) < 0) {
#else /* IPV6_RECVPKTINFO */
  if (setsockopt(fd, IPPROTO_IPV6, IPV6_PKTINFO, &on, sizeof(on) ) < 0) {
#endif /* IPV6_RECVPKTINFO */
    dsrv_log(LOG_ALERT, "setsockopt IPV6_PKTINFO: %s\n", strerror(errno));
  }

  dtls_context = dtls_new_context(&fd);
  if (!dtls_context) {
    dsrv_log(LOG_EMERG, "cannot create context\n");
    exit(-1);
  }

  dtls_set_handler(dtls_context, &cb);

  dtls_connect(dtls_context, &dst);

  while (1) {
    FD_ZERO(&rfds);
    FD_ZERO(&wfds);

    FD_SET(fileno(stdin), &rfds);
    FD_SET(fd, &rfds);
    /* FD_SET(fd, &wfds); */
    
    timeout.tv_sec = 5;
    timeout.tv_usec = 0;
    
    result = select(fd+1, &rfds, &wfds, 0, &timeout);
    
    if (result < 0) {		/* error */
      if (errno != EINTR)
	perror("select");
    } else if (result == 0) {	/* timeout */
    } else {			/* ok */
      if (FD_ISSET(fd, &wfds))
	/* FIXME */;
      else if (FD_ISSET(fd, &rfds))
	dtls_handle_read(dtls_context);
      else if (FD_ISSET(fileno(stdin), &rfds))
	handle_stdin();
    }

    if (len)
      try_send(dtls_context, &dst);
  }
  
  dtls_free_context(dtls_context);
  exit(0);
}
Пример #8
0
int main()
{
    int result;

    lwm2m_object_t * objArray[4];

    printf("Start\n");
    ethSetup();

    printf("Initializing tinydtls\n");

    // fake loading of PSK..
    psk_id_length = strlen(PSK_DEFAULT_IDENTITY);
    psk_key_length = strlen(PSK_DEFAULT_KEY);
    memcpy(psk_id, PSK_DEFAULT_IDENTITY, psk_id_length);
    memcpy(psk_key, PSK_DEFAULT_KEY, psk_key_length);
    printf("Init\n");

    dtls_init();
    printf("New context\n");
    dtls_context = dtls_new_context(&lwm2mH);

    if (dtls_context == NULL) {
        printf("error creating the dtls context\n");
    }
    printf("Setting handlers\n");

    dtls_set_handler(dtls_context, &cb);

    if (!dtls_context) {
        printf("can't create dtls_context\n");
        exit(-1);
    }

    printf("Initialazing Wakaama\n");

    // create objects
    objArray[0] = get_security_object(123, "coaps://5.39.83.206:5684", false);
    securityObjP = objArray[0];

    objArray[1] = get_server_object(123, "U", 20, false);
    serverObject = objArray[1];
    objArray[2] = get_object_device();
    objArray[3] = get_object_firmware();

    /*
     * The liblwm2m library is now initialized with the functions that will be in
     * charge of communication
     */
    lwm2mH = lwm2m_init(prv_connect_server, prv_buffer_send, NULL);
    if (NULL == lwm2mH)
    {
        fprintf(stderr, "lwm2m_init() failed\r\n");
        return -1;
    }

    // configure the liblwm2m lib

    result = lwm2m_configure(lwm2mH, "julien", NULL, 4, objArray);

    if (result != 0)
    {
        printf("lwm2m_configure() failed: 0x%X\n", result);
        return -1;
    }

    // start

    result = lwm2m_start(lwm2mH);
    if (result != 0)
    {
        printf("lwm2m_start() failed: 0x%X\n", result);
        return -1;
    }

    // main loop

    while (true) {
        char buffer[1024];
        Endpoint server;

        printf("loop...\n");
        struct timeval timeout;
        timeout.tv_sec = 10;
        timeout.tv_usec = 0;

        result = lwm2m_step(lwm2mH, &timeout);
        if (result != 0)
        {
            printf("lwm2m_step error %d\n", result);
        }
        int n = udp.receiveFrom(server, buffer, sizeof(buffer));
        printf("Received packet from: %s of size %d\n", server.get_address(), n);
        if (n>0) {
            // TODO: find connection
            connection_t * connP = connList;
            while(connP != NULL) {
                if (strcmp(connP->host, server.get_address()) == 0)
                {

                    printf("found connection\n");
                    // is it a secure connection?
                    if (connP->dtlsSession != NULL) {
						printf("dtls session\n");
						result = dtls_handle_message(dtls_context, connP->dtlsSession, buffer, n);
						printf("dtls handle message %d\n",result);
					} else {
						printf("nosec session\n");
						lwm2m_handle_packet(lwm2mH, (uint8_t*)buffer, n, (void*)connP);
					}
                    break;
                }
            }

            if (connP == NULL) printf("no connection\n");
        }
    }
}
Пример #9
0
int 
main(int argc, char **argv) {
  dtls_context_t *the_context = NULL;
  log_t log_level = DTLS_LOG_WARN;
  fd_set rfds, wfds;
  struct timeval timeout;
  int fd, opt, result;
  int on = 1;
  int ecdh_anon_enalbe = DTLS_CIPHER_DISABLE;
  struct sockaddr_in6 listen_addr;

  memset(&listen_addr, 0, sizeof(struct sockaddr_in6));

  /* fill extra field for 4.4BSD-based systems (see RFC 3493, section 3.4) */
#if defined(SIN6_LEN) || defined(HAVE_SOCKADDR_IN6_SIN6_LEN)
  listen_addr.sin6_len = sizeof(struct sockaddr_in6);
#endif

  listen_addr.sin6_family = AF_INET6;
  listen_addr.sin6_port = htons(DEFAULT_PORT);
  listen_addr.sin6_addr = in6addr_any;

  while ((opt = getopt(argc, argv, "A:p:v:a:")) != -1) {
    switch (opt) {
    case 'A' :
      if (resolve_address(optarg, (struct sockaddr *)&listen_addr) < 0) {
	fprintf(stderr, "cannot resolve address\n");
	exit(-1);
      }
      break;
    case 'p' :
      listen_addr.sin6_port = htons(atoi(optarg));
      break;
    case 'v' :
      log_level = strtol(optarg, NULL, 10);
      break;
    case 'a':
      if( strcmp(optarg, "enable") == 0)
          ecdh_anon_enalbe = DTLS_CIPHER_ENABLE;
      break;
    default:
      usage(argv[0], dtls_package_version());
      exit(1);
    }
  }

  dtls_set_log_level(log_level);

  /* init socket and set it to non-blocking */
  fd = socket(listen_addr.sin6_family, SOCK_DGRAM, 0);

  if (fd < 0) {
    dtls_alert("socket: %s\n", strerror(errno));
    return 0;
  }

  if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on) ) < 0) {
    dtls_alert("setsockopt SO_REUSEADDR: %s\n", strerror(errno));
  }
#if 0
  flags = fcntl(fd, F_GETFL, 0);
  if (flags < 0 || fcntl(fd, F_SETFL, flags | O_NONBLOCK) < 0) {
    dtls_alert("fcntl: %s\n", strerror(errno));
    goto error;
  }
#endif
  on = 1;
#ifdef IPV6_RECVPKTINFO
  if (setsockopt(fd, IPPROTO_IPV6, IPV6_RECVPKTINFO, &on, sizeof(on) ) < 0) {
#else /* IPV6_RECVPKTINFO */
  if (setsockopt(fd, IPPROTO_IPV6, IPV6_PKTINFO, &on, sizeof(on) ) < 0) {
#endif /* IPV6_RECVPKTINFO */
    dtls_alert("setsockopt IPV6_PKTINFO: %s\n", strerror(errno));
  }

  if (bind(fd, (struct sockaddr *)&listen_addr, sizeof(listen_addr)) < 0) {
    dtls_alert("bind: %s\n", strerror(errno));
    goto error;
  }

  dtls_init();

  the_context = dtls_new_context(&fd);

  /* enable/disable tls_ecdh_anon_with_aes_128_cbc_sha */
  dtls_enables_anon_ecdh(the_context, ecdh_anon_enalbe);

  dtls_set_handler(the_context, &cb);

  while (1) {
    FD_ZERO(&rfds);
    FD_ZERO(&wfds);

    FD_SET(fd, &rfds);
    /* FD_SET(fd, &wfds); */
    
    timeout.tv_sec = 5;
    timeout.tv_usec = 0;
    
    result = select( fd+1, &rfds, &wfds, 0, &timeout);
    
    if (result < 0) {		/* error */
      if (errno != EINTR)
	perror("select");
    } else if (result == 0) {	/* timeout */
    } else {			/* ok */
      if (FD_ISSET(fd, &wfds))
	;
      else if (FD_ISSET(fd, &rfds)) {
	dtls_handle_read(the_context);
      }
    }
  }
  
 error:
  dtls_free_context(the_context);
  exit(0);
}
Пример #10
0
static int
get_key(struct dtls_context_t *ctx,
        const session_t *session,
        const unsigned char *id, size_t id_len,
        const dtls_key_t **result) {

  static const dtls_key_t psk = {
    .type = DTLS_KEY_PSK,
    .key.psk.id = (unsigned char *)DTLS_IDENTITY_HINT,
    .key.psk.id_length = DTLS_IDENTITY_HINT_LENGTH,
    .key.psk.key = (unsigned char *)DTLS_PSK_KEY,
    .key.psk.key_length = DTLS_PSK_KEY_LENGTH
  };

  *result = &psk;
  return 0;
}
#else
static int
get_psk_key(struct dtls_context_t *ctx,
            const session_t *session,
            const unsigned char *id, size_t id_len,
            const dtls_psk_key_t **result) {

  static const dtls_psk_key_t psk = {
    .id = (unsigned char *)DTLS_IDENTITY_HINT,
    .id_length = DTLS_IDENTITY_HINT_LENGTH,
    .key = (unsigned char *)DTLS_PSK_KEY,
    .key_length = DTLS_PSK_KEY_LENGTH
  };

  *result = &psk;
  return 0;
}
#endif
/*-----------------------------------------------------------------------------------*/
void
coap_init_communication_layer(uint16_t port)
{
  static dtls_handler_t cb = {
    .write = send_to_peer,
    .read  = read_from_peer,
    .event = NULL,
#if DTLS_VERSION_0_4_0
    .get_key = get_key
#else
    .get_psk_key = get_psk_key,
    .get_ecdsa_key = NULL,
    .verify_ecdsa_key = NULL
#endif
  };

  server_conn = udp_new(NULL, 0, NULL);
  udp_bind(server_conn, port);

  dtls_set_log_level(LOG_DEBUG);

  dtls_context = dtls_new_context(server_conn);
  if (dtls_context)
    dtls_set_handler(dtls_context, &cb);

  /* new connection with remote host */
  printf("COAP-DTLS listening on port %u\n", uip_ntohs(server_conn->lport));
}
/*-----------------------------------------------------------------------------------*/
static int
send_to_peer(struct dtls_context_t *ctx,
             session_t *session, uint8 *data, size_t len) {

  struct uip_udp_conn *conn = (struct uip_udp_conn *)dtls_get_app_data(ctx);

  uip_ipaddr_copy(&conn->ripaddr, &session->addr);
  conn->rport = session->port;

  uip_udp_packet_send(conn, data, len);

  /* Restore server connection to allow data from any node */
  memset(&conn->ripaddr, 0, sizeof(conn->ripaddr));
  memset(&conn->rport, 0, sizeof(conn->rport));

  return len;
}
/*-----------------------------------------------------------------------------------*/
void
coap_send_message(context_t * ctx, uip_ipaddr_t *addr, uint16_t port, uint8_t *data, uint16_t length)
{
  session_t session;

  dtls_session_init(&session);
  uip_ipaddr_copy(&session.addr, addr);
  session.port = port;

  dtls_write(ctx, &session, data, length);
}
/*-----------------------------------------------------------------------------------*/
static int
read_from_peer(struct dtls_context_t *ctx,
               session_t *session, uint8 *data, size_t len) {
  uip_len = len;
  memmove(uip_appdata, data, len);
  coap_receive(ctx);
  return 0;
}
/*-----------------------------------------------------------------------------------*/
void
coap_handle_receive()
{
  session_t session;

  if(uip_newdata()) {
    dtls_session_init(&session);
    uip_ipaddr_copy(&session.addr, &UIP_IP_BUF->srcipaddr);
    session.port = UIP_UDP_BUF->srcport;

    dtls_handle_message(dtls_context, &session, uip_appdata, uip_datalen());
  }
}