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 101 102 103 104 105
  oc_device_info_t* d = oc_core_add_new_device("/oic/d", device_rt, device_name, spec_version,
                        data_model_version, NULL, NULL);
  if (!d) {
    return 1;
  }
  if (deviceid == NULL) {
    return 0;
  }
  oc_str_to_uuid(deviceid, &d->di);
106 107 108 109
  return ret;
}

static void
110 111
cloud_refresh_token_cb(oc_cloud_context_t *ctx, oc_cloud_status_t status,
                       void *data)
112
{
113
  (void)data;
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
  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);
151
  int ret = oc_cloud_refresh_token(ctx, cloud_refresh_token_cb, NULL);
152 153 154 155 156 157 158 159 160
  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
161 162
cloud_deregister_cb(oc_cloud_context_t *ctx, oc_cloud_status_t status,
                    void *data)
163
{
164
  (void)data;
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
  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);
199
  int ret = oc_cloud_deregister(ctx, cloud_deregister_cb, NULL);
200 201 202 203 204 205 206 207 208
  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
209
cloud_logout_cb(oc_cloud_context_t *ctx, oc_cloud_status_t status, void *data)
210
{
211
  (void)data;
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
  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);
243
  int ret = oc_cloud_logout(ctx, cloud_logout_cb, NULL);
244 245 246 247 248 249 250 251 252
  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
253
cloud_login_cb(oc_cloud_context_t *ctx, oc_cloud_status_t status, void *data)
254
{
255
  (void)data;
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
  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);
284
  int ret = oc_cloud_login(ctx, cloud_login_cb, NULL);
285 286 287 288 289 290 291 292 293
  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
294
cloud_register_cb(oc_cloud_context_t *ctx, oc_cloud_status_t status, void *data)
295
{
296
  (void)data;
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
  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);
322
  oc_cloud_provision_conf_resource(ctx, cis, auth_code, sid, apn);
323
  int ret = oc_cloud_register(ctx, cloud_register_cb, NULL);
324 325 326 327 328 329 330 331
  pthread_mutex_unlock(&app_sync_lock);
  if (ret < 0) {
    PRINT("\nCould not issue Cloud Register request\n");
  } else {
    PRINT("\nIssued Cloud Register request\n");
  }
}

332 333 334 335 336 337 338 339 340 341 342 343 344
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);
345
  char endpoint_string[267];
346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365
  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
366 367 368 369
{
  bool state;
};

370
struct switch_t bswitch = { 0 };
371 372

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

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

static void
395 396
post_switch(oc_request_t *request, oc_interface_mask_t iface_mask,
            void *user_data)
397 398
{
  (void)iface_mask;
399 400 401
  (void)user_data;
  PRINT("POST_switch:\n");
  bool state = false, bad_request = false;
402 403
  oc_rep_t *rep = request->request_payload;
  while (rep != NULL) {
404 405 406 407 408 409 410 411 412
    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;
        }
413
      }
414 415
      bad_request = true;
      break;
416 417 418
    }
    rep = rep->next;
  }
419 420 421 422 423 424 425 426 427 428 429 430 431 432

  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);
  }
433 434 435 436 437
}

static void
register_resources(void)
{
438 439 440 441
  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);
442 443
  oc_resource_set_discoverable(res1, true);
  oc_resource_set_observable(res1, true);
444 445
  oc_resource_set_request_handler(res1, OC_GET, get_switch, NULL);
  oc_resource_set_request_handler(res1, OC_POST, post_switch, NULL);
446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465
  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;
}

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
#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
511
  buffer[pem_len] = '\0';
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
  *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 */
}

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

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

  if (pthread_mutex_init(&mutex, NULL) < 0) {
    printf("pthread_mutex_init failed!\n");
    return NULL;
  }
Kishen Maloor's avatar
Kishen Maloor committed
593
  oc_set_con_res_announced(false);
594 595 596 597 598
  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);
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
  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
624
main(int argc, char *argv[])
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
  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]);
  }

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 686
  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:
687 688 689
      cloud_send_ping();
      break;
    case 8:
690 691 692 693 694 695
      oc_cloud_delete_resource(res1);
      break;
    case 9:
      oc_cloud_add_resource(res1);
      break;
    case 10:
696
      handle_signal(0);
697
      break;
698 699 700 701 702 703 704 705 706
    default:
      break;
    }
  }

  pthread_join(event_thread, NULL);

  return 0;
}