cloud_certification_tests.c 17 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
/****************************************************************************
 *
 * Copyright (c) 2019 Intel Corporation
 * Copyright 2019 Jozef Kralik All Rights Reserved.
 * Copyright 2018 Samsung Electronics All Rights Reserved.
 *
 * 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 "oc_api.h"
#include "oc_core_res.h"
23
#include "oc_pki.h"
24 25 26 27 28 29 30
#include "port/oc_clock.h"
#include "rd_client.h"
#include <pthread.h>
#include <signal.h>
#include <stdio.h>

// define application specific values.
31 32
static const char *spec_version = "ocf.2.0.5";
static const char *data_model_version = "ocf.res.1.3.0,ocf.sh.1.3.0";
33

34 35
static const char *device_rt = "oic.d.switch";
static const char *device_name = "Cloud Switch";
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55

static const char *manufacturer = "ocfcloud.com";

pthread_mutex_t mutex;
pthread_cond_t cv;
static pthread_t event_thread;
static pthread_mutex_t app_sync_lock;

oc_resource_t *res1;
oc_resource_t *res2;

static struct timespec ts;
static int quit;

#define ACCESS_TOKEN_KEY "accesstoken"
#define REFRESH_TOKEN_KEY "refreshtoken"
#define REDIRECTURI_KEY "redirecturi"
#define USER_ID_KEY "uid"
#define EXPIRESIN_KEY "expiresin"

56 57 58 59 60 61
static const char *cis;
static const char *auth_code;
static const char *sid;
static const char *apn;
static const char *deviceid;

62 63 64 65 66 67 68 69 70 71 72 73 74 75
static void
display_menu(void)
{
  PRINT("\n\n################################################\nOCF "
        "Cloud-connected Device Certification Test "
        "Tool\n################################################\n");
  PRINT("[0] Display this menu\n");
  PRINT("-----------------------------------------------\n");
  PRINT("[1] Cloud Register\n");
  PRINT("[2] Cloud Login\n");
  PRINT("[3] Cloud Logout\n");
  PRINT("[4] Cloud DeRegister\n");
  PRINT("[5] Cloud Refresh Token\n");
  PRINT("[6] Publish Resources\n");
76
  PRINT("[7] Send Ping\n");
77 78
  PRINT("[8] Unpublish switch resource\n");
  PRINT("[9] Publish switch resource\n");
79 80
  PRINT("-----------------------------------------------\n");
  PRINT("-----------------------------------------------\n");
81
  PRINT("[10] Exit\n");
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
  PRINT("################################################\n");
  PRINT("\nSelect option: \n");
}

#define SCANF(...)                                                             \
  do {                                                                         \
    if (scanf(__VA_ARGS__) != 1) {                                             \
      PRINT("ERROR Invalid input\n");                                          \
    }                                                                          \
  } while (0)

static int
app_init(void)
{
  int ret = oc_init_platform(manufacturer, NULL, NULL);
97 98 99 100
  ret |= oc_add_device("/oic/d", device_rt, device_name, spec_version,
                       data_model_version, NULL, NULL);
  if (ret || !deviceid) {
    return ret;
101
  }
102 103 104

  oc_device_info_t *info = oc_core_get_device_info(0);
  oc_str_to_uuid(deviceid, &info->di);
105 106 107 108
  return ret;
}

static void
109 110
cloud_refresh_token_cb(oc_cloud_context_t *ctx, oc_cloud_status_t status,
                       void *data)
111
{
112
  (void)data;
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
  PRINT("\nCloud Refresh Token status flags:\n");
  if (status & OC_CLOUD_REGISTERED) {
    PRINT("\t\t-Registered\n");
  }
  if (status & OC_CLOUD_TOKEN_EXPIRY) {
    PRINT("\t\t-Token Expiry: ");
    if (ctx) {
      PRINT("%d\n", oc_cloud_get_token_expiry(ctx));
    } else {
      PRINT("\n");
    }
  }
  if (status & OC_CLOUD_FAILURE) {
    PRINT("\t\t-Failure\n");
  }
  if (status & OC_CLOUD_LOGGED_IN) {
    PRINT("\t\t-Logged In\n");
  }
  if (status & OC_CLOUD_LOGGED_OUT) {
    PRINT("\t\t-Logged Out\n");
  }
  if (status & OC_CLOUD_DEREGISTERED) {
    PRINT("\t\t-DeRegistered\n");
  }
  if (status & OC_CLOUD_REFRESHED_TOKEN) {
    PRINT("\t\t-Refreshed Token\n");
  }
}

static void
cloud_refresh_token(void)
{
  oc_cloud_context_t *ctx = oc_cloud_get_context(0);
  if (!ctx) {
    return;
  }
  pthread_mutex_lock(&app_sync_lock);
150
  int ret = oc_cloud_refresh_token(ctx, cloud_refresh_token_cb, NULL);
151 152 153 154 155 156 157 158 159
  pthread_mutex_unlock(&app_sync_lock);
  if (ret < 0) {
    PRINT("\nCould not issue Refresh Token request\n");
  } else {
    PRINT("\nIssued Refresh Token request\n");
  }
}

static void
160 161
cloud_deregister_cb(oc_cloud_context_t *ctx, oc_cloud_status_t status,
                    void *data)
162
{
163
  (void)data;
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
  PRINT("\nCloud DeRegister status flags:\n");
  if (status & OC_CLOUD_REGISTERED) {
    PRINT("\t\t-Registered\n");
  }
  if (status & OC_CLOUD_TOKEN_EXPIRY) {
    PRINT("\t\t-Token Expiry: ");
    if (ctx) {
      PRINT("%d\n", oc_cloud_get_token_expiry(ctx));
    } else {
      PRINT("\n");
    }
  }
  if (status & OC_CLOUD_FAILURE) {
    PRINT("\t\t-Failure\n");
  }
  if (status & OC_CLOUD_LOGGED_IN) {
    PRINT("\t\t-Logged In\n");
  }
  if (status & OC_CLOUD_LOGGED_OUT) {
    PRINT("\t\t-Logged Out\n");
  }
  if (status & OC_CLOUD_DEREGISTERED) {
    PRINT("\t\t-DeRegistered\n");
  }
}

static void
cloud_deregister(void)
{
  oc_cloud_context_t *ctx = oc_cloud_get_context(0);
  if (!ctx) {
    return;
  }
  pthread_mutex_lock(&app_sync_lock);
198
  int ret = oc_cloud_deregister(ctx, cloud_deregister_cb, NULL);
199 200 201 202 203 204 205 206 207
  pthread_mutex_unlock(&app_sync_lock);
  if (ret < 0) {
    PRINT("\nCould not issue Cloud DeRegister request\n");
  } else {
    PRINT("\nIssued Cloud DeRegister request\n");
  }
}

static void
208
cloud_logout_cb(oc_cloud_context_t *ctx, oc_cloud_status_t status, void *data)
209
{
210
  (void)data;
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
  PRINT("\nCloud Logout status flags:\n");
  if (status & OC_CLOUD_REGISTERED) {
    PRINT("\t\t-Registered\n");
  }
  if (status & OC_CLOUD_TOKEN_EXPIRY) {
    PRINT("\t\t-Token Expiry: ");
    if (ctx) {
      PRINT("%d\n", oc_cloud_get_token_expiry(ctx));
    } else {
      PRINT("\n");
    }
  }
  if (status & OC_CLOUD_FAILURE) {
    PRINT("\t\t-Failure\n");
  }
  if (status & OC_CLOUD_LOGGED_IN) {
    PRINT("\t\t-Logged In\n");
  }
  if (status & OC_CLOUD_LOGGED_OUT) {
    PRINT("\t\t-Logged Out\n");
  }
}

static void
cloud_logout(void)
{
  oc_cloud_context_t *ctx = oc_cloud_get_context(0);
  if (!ctx) {
    return;
  }
  pthread_mutex_lock(&app_sync_lock);
242
  int ret = oc_cloud_logout(ctx, cloud_logout_cb, NULL);
243 244 245 246 247 248 249 250 251
  pthread_mutex_unlock(&app_sync_lock);
  if (ret < 0) {
    PRINT("\nCould not issue Cloud Logout request\n");
  } else {
    PRINT("\nIssued Cloud Logout request\n");
  }
}

static void
252
cloud_login_cb(oc_cloud_context_t *ctx, oc_cloud_status_t status, void *data)
253
{
254
  (void)data;
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
  PRINT("\nCloud Login status flags:\n");
  if (status & OC_CLOUD_REGISTERED) {
    PRINT("\t\t-Registered\n");
  }
  if (status & OC_CLOUD_TOKEN_EXPIRY) {
    PRINT("\t\t-Token Expiry: ");
    if (ctx) {
      PRINT("%d\n", oc_cloud_get_token_expiry(ctx));
    } else {
      PRINT("\n");
    }
  }
  if (status & OC_CLOUD_FAILURE) {
    PRINT("\t\t-Failure\n");
  }
  if (status & OC_CLOUD_LOGGED_IN) {
    PRINT("\t\t-Logged In\n");
  }
}

static void
cloud_login(void)
{
  oc_cloud_context_t *ctx = oc_cloud_get_context(0);
  if (!ctx) {
    return;
  }
  pthread_mutex_lock(&app_sync_lock);
283
  int ret = oc_cloud_login(ctx, cloud_login_cb, NULL);
284 285 286 287 288 289 290 291 292
  pthread_mutex_unlock(&app_sync_lock);
  if (ret < 0) {
    PRINT("\nCould not issue Cloud Login request\n");
  } else {
    PRINT("\nIssued Cloud Login request\n");
  }
}

static void
293
cloud_register_cb(oc_cloud_context_t *ctx, oc_cloud_status_t status, void *data)
294
{
295
  (void)data;
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
  PRINT("\nCloud Register status flags:\n");
  if (status & OC_CLOUD_REGISTERED) {
    PRINT("\t\t-Registered\n");
  }
  if (status & OC_CLOUD_TOKEN_EXPIRY) {
    PRINT("\t\t-Token Expiry: ");
    if (ctx) {
      PRINT("%d\n", oc_cloud_get_token_expiry(ctx));
    } else {
      PRINT("\n");
    }
  }
  if (status & OC_CLOUD_FAILURE) {
    PRINT("\t\t-Failure\n");
  }
}

static void
cloud_register(void)
{
  oc_cloud_context_t *ctx = oc_cloud_get_context(0);
  if (!ctx) {
    return;
  }
  pthread_mutex_lock(&app_sync_lock);
321
  oc_cloud_provision_conf_resource(ctx, cis, auth_code, sid, apn);
322
  int ret = oc_cloud_register(ctx, cloud_register_cb, NULL);
323 324 325 326 327 328 329 330
  pthread_mutex_unlock(&app_sync_lock);
  if (ret < 0) {
    PRINT("\nCould not issue Cloud Register request\n");
  } else {
    PRINT("\nIssued Cloud Register request\n");
  }
}

331 332 333 334 335 336 337 338 339 340 341 342 343
static void
ping_handler(oc_client_response_t *data)
{
  (void)data;
  PRINT("\nReceived Pong\n");
}

static void
cloud_send_ping(void)
{
  PRINT("\nEnter receiving endpoint: ");
  char addr[256];
  SCANF("%255s", addr);
344
  char endpoint_string[267];
345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364
  sprintf(endpoint_string, "coap+tcp://%s", addr);
  oc_string_t ep_string;
  oc_new_string(&ep_string, endpoint_string, strlen(endpoint_string));
  oc_endpoint_t endpoint;
  int ret = oc_string_to_endpoint(&ep_string, &endpoint, NULL);
  oc_free_string(&ep_string);
  if (ret < 0) {
    PRINT("\nERROR parsing endpoint string\n");
    return;
  }

  if (oc_send_ping(false, &endpoint, 10, ping_handler, NULL)) {
    PRINT("\nSuccessfully issued Ping request\n");
    return;
  }

  PRINT("\nERROR issuing Ping request\n");
}

struct switch_t
365 366 367 368
{
  bool state;
};

369
struct switch_t bswitch = { 0 };
370 371

static void
372 373
get_switch(oc_request_t *request, oc_interface_mask_t iface_mask,
           void *user_data)
374
{
375 376
  (void)user_data;
  PRINT("GET_switch:\n");
377
  oc_rep_start_root_object();
378
  switch (iface_mask) {
379 380 381
  case OC_IF_BASELINE:
    oc_process_baseline_interface(request->resource);
  /* fall through */
382 383
  case OC_IF_A:
    oc_rep_set_boolean(root, value, bswitch.state);
384 385 386 387 388
    break;
  default:
    break;
  }
  oc_rep_end_root_object();
389

390 391 392 393
  oc_send_response(request, OC_STATUS_OK);
}

static void
394 395
post_switch(oc_request_t *request, oc_interface_mask_t iface_mask,
            void *user_data)
396 397
{
  (void)iface_mask;
398 399 400
  (void)user_data;
  PRINT("POST_switch:\n");
  bool state = false, bad_request = false;
401 402
  oc_rep_t *rep = request->request_payload;
  while (rep != NULL) {
403 404 405 406 407 408 409 410 411
    switch (rep->type) {
    case OC_REP_BOOL:
      state = rep->value.boolean;
      break;
    default:
      if (oc_string_len(rep->name) > 2) {
        if (strncmp(oc_string(rep->name), "x.", 2) == 0) {
          break;
        }
412
      }
413 414
      bad_request = true;
      break;
415 416 417
    }
    rep = rep->next;
  }
418 419 420 421 422 423 424 425 426 427 428 429 430 431

  if (!bad_request) {
    bswitch.state = state;
  }

  oc_rep_start_root_object();
  oc_rep_set_boolean(root, value, bswitch.state);
  oc_rep_end_root_object();

  if (!bad_request) {
    oc_send_response(request, OC_STATUS_CHANGED);
  } else {
    oc_send_response(request, OC_STATUS_BAD_REQUEST);
  }
432 433 434 435 436
}

static void
register_resources(void)
{
437 438 439 440
  res1 = oc_new_resource(NULL, "/switch/1", 1, 0);
  oc_resource_bind_resource_type(res1, "oic.r.switch.binary");
  oc_resource_bind_resource_interface(res1, OC_IF_A);
  oc_resource_set_default_interface(res1, OC_IF_A);
441 442
  oc_resource_set_discoverable(res1, true);
  oc_resource_set_observable(res1, true);
443 444
  oc_resource_set_request_handler(res1, OC_GET, get_switch, NULL);
  oc_resource_set_request_handler(res1, OC_POST, post_switch, NULL);
445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464
  oc_cloud_add_resource(res1); /* Publish resource to the Cloud RD */
  oc_add_resource(res1);
}

static void
signal_event_loop(void)
{
  pthread_mutex_lock(&mutex);
  pthread_cond_signal(&cv);
  pthread_mutex_unlock(&mutex);
}

void
handle_signal(int signal)
{
  (void)signal;
  signal_event_loop();
  quit = 1;
}

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
#ifdef OC_SECURITY
void
random_pin_cb(const unsigned char *pin, size_t pin_len, void *data)
{
  (void)data;
  PRINT("\n\nRandom PIN: %.*s\n\n", (int)pin_len, pin);
}
#endif /* OC_SECURITY */

#if defined(OC_SECURITY) && defined(OC_PKI)
static int
read_pem(const char *file_path, char *buffer, size_t *buffer_len)
{
  FILE *fp = fopen(file_path, "r");
  if (fp == NULL) {
    PRINT("ERROR: unable to read PEM\n");
    return -1;
  }
  if (fseek(fp, 0, SEEK_END) != 0) {
    PRINT("ERROR: unable to read PEM\n");
    fclose(fp);
    return -1;
  }
  long pem_len = ftell(fp);
  if (pem_len < 0) {
    PRINT("ERROR: could not obtain length of file\n");
    fclose(fp);
    return -1;
  }
  if (pem_len > (long)*buffer_len) {
    PRINT("ERROR: buffer provided too small\n");
    fclose(fp);
    return -1;
  }
  if (fseek(fp, 0, SEEK_SET) != 0) {
    PRINT("ERROR: unable to read PEM\n");
    fclose(fp);
    return -1;
  }
  if (fread(buffer, 1, pem_len, fp) < (size_t)pem_len) {
    PRINT("ERROR: unable to read PEM\n");
    fclose(fp);
    return -1;
  }
  fclose(fp);
Kishen Maloor's avatar
Kishen Maloor committed
510
  buffer[pem_len] = '\0';
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
  *buffer_len = (size_t)pem_len;
  return 0;
}
#endif /* OC_SECURITY && OC_PKI */

void
factory_presets_cb(size_t device, void *data)
{
  (void)device;
  (void)data;
#if defined(OC_SECURITY) && defined(OC_PKI)
  char cert[8192];
  size_t cert_len = 8192;
  if (read_pem("pki_certs/ee.pem", cert, &cert_len) < 0) {
    PRINT("ERROR: unable to read certificates\n");
    return;
  }

  char key[4096];
  size_t key_len = 4096;
  if (read_pem("pki_certs/key.pem", key, &key_len) < 0) {
    PRINT("ERROR: unable to read private key");
    return;
  }

  int ee_credid = oc_pki_add_mfg_cert(0, (const unsigned char *)cert, cert_len,
                                      (const unsigned char *)key, key_len);

  if (ee_credid < 0) {
    PRINT("ERROR installing manufacturer EE cert\n");
    return;
  }

  cert_len = 8192;
  if (read_pem("pki_certs/subca1.pem", cert, &cert_len) < 0) {
    PRINT("ERROR: unable to read certificates\n");
    return;
  }

  int subca_credid = oc_pki_add_mfg_intermediate_cert(
    0, ee_credid, (const unsigned char *)cert, cert_len);

  if (subca_credid < 0) {
    PRINT("ERROR installing intermediate CA cert\n");
    return;
  }

  cert_len = 8192;
  if (read_pem("pki_certs/rootca1.pem", cert, &cert_len) < 0) {
    PRINT("ERROR: unable to read certificates\n");
    return;
  }

  int rootca_credid =
    oc_pki_add_mfg_trust_anchor(0, (const unsigned char *)cert, cert_len);
  if (rootca_credid < 0) {
    PRINT("ERROR installing root cert\n");
    return;
  }

  oc_pki_set_security_profile(0, OC_SP_BLACK, OC_SP_BLACK, ee_credid);
#endif /* OC_SECURITY && OC_PKI */
}

575 576 577 578
static void *
ocf_event_thread(void *data)
{
  (void)data;
George Nash's avatar
George Nash committed
579 580 581 582
  static const oc_handler_t handler = { .init = app_init,
                                        .signal_event_loop = signal_event_loop,
                                        .register_resources =
                                          register_resources };
583

584
#ifdef OC_STORAGE
585
  oc_storage_config("./cloud_tests_creds");
586
#endif /* OC_STORAGE */
587 588 589 590 591

  if (pthread_mutex_init(&mutex, NULL) < 0) {
    printf("pthread_mutex_init failed!\n");
    return NULL;
  }
Kishen Maloor's avatar
Kishen Maloor committed
592
  oc_set_con_res_announced(false);
593 594 595 596 597
  oc_set_factory_presets_cb(factory_presets_cb, NULL);
#ifdef OC_SECURITY
  oc_set_random_pin_callback(random_pin_cb, NULL);
#endif /* OC_SECURITY */
  oc_set_max_app_data_size(16384);
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
  int init = oc_main_init(&handler);
  if (init < 0)
    return NULL;

  oc_clock_time_t next_event;
  while (quit != 1) {
    pthread_mutex_lock(&app_sync_lock);
    next_event = oc_main_poll();
    pthread_mutex_unlock(&app_sync_lock);

    pthread_mutex_lock(&mutex);
    if (next_event == 0) {
      pthread_cond_wait(&cv, &mutex);
    } else {
      ts.tv_sec = (next_event / OC_CLOCK_SECOND);
      ts.tv_nsec = (next_event % OC_CLOCK_SECOND) * 1.e09 / OC_CLOCK_SECOND;
      pthread_cond_timedwait(&cv, &mutex, &ts);
    }
    pthread_mutex_unlock(&mutex);
  }
  oc_main_shutdown();
  return NULL;
}

int
623
main(int argc, char *argv[])
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
  if (argc > 1) {
    device_name = argv[1];
    PRINT("device_name: %s\n", argv[1]);
  }
  if (argc > 2) {
    auth_code = argv[2];
    PRINT("auth_code: %s\n", argv[2]);
  }
  if (argc > 3) {
    cis = argv[3];
    PRINT("cis : %s\n", argv[3]);
  }
  if (argc > 4) {
    sid = argv[4];
    PRINT("sid: %s\n", argv[4]);
  }
  if (argc > 5) {
    apn = argv[5];
    PRINT("apn: %s\n", argv[5]);
  }
  if (argc > 6) {
    deviceid = argv[6];
    PRINT("deviceID: %s\n", argv[6]);
  }

650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685
  struct sigaction sa;
  sigfillset(&sa.sa_mask);
  sa.sa_flags = 0;
  sa.sa_handler = handle_signal;
  sigaction(SIGINT, &sa, NULL);

  if (pthread_create(&event_thread, NULL, &ocf_event_thread, NULL) != 0) {
    return -1;
  }

  int c;
  while (quit != 1) {
    display_menu();
    SCANF("%d", &c);
    switch (c) {
    case 0:
      continue;
    case 1:
      cloud_register();
      break;
    case 2:
      cloud_login();
      break;
    case 3:
      cloud_logout();
      break;
    case 4:
      cloud_deregister();
      break;
    case 5:
      cloud_refresh_token();
      break;
    case 6:
      oc_cloud_publish_resources(0);
      break;
    case 7:
686 687 688
      cloud_send_ping();
      break;
    case 8:
689 690 691 692 693 694
      oc_cloud_delete_resource(res1);
      break;
    case 9:
      oc_cloud_add_resource(res1);
      break;
    case 10:
695
      handle_signal(0);
696
      break;
697 698 699 700 701 702 703 704 705
    default:
      break;
    }
  }

  pthread_join(event_thread, NULL);

  return 0;
}