Guest User

Untitled

a guest
Jan 14th, 2021
579
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 5.54 KB | None | 0 0
  1. /*
  2.  * Copyright (c) 2020 Nordic Semiconductor ASA
  3.  *
  4.  * SPDX-License-Identifier: LicenseRef-BSD-5-Clause-Nordic
  5.  */
  6.  
  7. #include <string.h>
  8. #include <zephyr.h>
  9. #include <stdlib.h>
  10. #include <stdio.h>
  11. #include <net/socket.h>
  12. #include <modem/bsdlib.h>
  13. #include <net/tls_credentials.h>
  14. #include <modem/lte_lc.h>
  15. #include <modem/at_cmd.h>
  16. #include <modem/at_notif.h>
  17. #include <modem/modem_key_mgmt.h>
  18.  
  19. #define HTTPS_PORT 443
  20. #define ONBOARDING_ENDPOINT  "device.connectivity-suite.cloud"
  21.  
  22.  
  23. #define RECV_BUF_SIZE 2048
  24. #define TLS_SEC_TAG 42
  25.  
  26. static char recv_buf[RECV_BUF_SIZE];
  27.  
  28. /* Certificate for `google.com` */
  29. static const char cert[] = {
  30.     #include "../cert/root-CA"
  31. };
  32.  
  33. BUILD_ASSERT(sizeof(cert) < KB(4), "Certificate too large");
  34.  
  35.  
  36. /* Initialize AT communications */
  37. int at_comms_init(void)
  38. {
  39.     int err;
  40.  
  41.     err = at_cmd_init();
  42.     if (err) {
  43.         printk("Failed to initialize AT commands, err %d\n", err);
  44.         return err;
  45.     }
  46.  
  47.     err = at_notif_init();
  48.     if (err) {
  49.         printk("Failed to initialize AT notifications, err %d\n", err);
  50.         return err;
  51.     }
  52.  
  53.     return 0;
  54. }
  55.  
  56. /* Provision certificate to modem */
  57. int cert_provision(void)
  58. {
  59.     int err;
  60.     bool exists;
  61.     uint8_t unused;
  62.  
  63.     err = modem_key_mgmt_exists(TLS_SEC_TAG,
  64.                     MODEM_KEY_MGMT_CRED_TYPE_CA_CHAIN,
  65.                     &exists, &unused);
  66.     if (err) {
  67.         printk("Failed to check for certificates err %d\n", err);
  68.         return err;
  69.     }
  70.  
  71.     if (exists) {
  72.         /* For the sake of simplicity we delete what is provisioned
  73.          * with our security tag and reprovision our certificate.
  74.          */
  75.         err = modem_key_mgmt_delete(TLS_SEC_TAG,
  76.                         MODEM_KEY_MGMT_CRED_TYPE_CA_CHAIN);
  77.         if (err) {
  78.             printk("Failed to delete existing certificate, err %d\n",
  79.                    err);
  80.         }
  81.     }
  82.  
  83.     printk("Provisioning certificate\n");
  84.  
  85.     /*  Provision certificate to the modem */
  86.     err = modem_key_mgmt_write(TLS_SEC_TAG,
  87.                    MODEM_KEY_MGMT_CRED_TYPE_CA_CHAIN,
  88.                    cert, sizeof(cert) - 1);
  89.     if (err) {
  90.         printk("Failed to provision certificate, err %d\n", err);
  91.         return err;
  92.     }
  93.  
  94.     return 0;
  95. }
  96.  
  97. /* Setup TLS options on a given socket */
  98. int tls_setup(int fd)
  99. {
  100.     int err;
  101.     int verify;
  102.  
  103.     /* Security tag that we have provisioned the certificate with */
  104.     const sec_tag_t tls_sec_tag[] = {
  105.         TLS_SEC_TAG,
  106.     };
  107.  
  108.     /* Set up TLS peer verification */
  109.     enum {
  110.         NONE = 0,
  111.         OPTIONAL = 1,
  112.         REQUIRED = 2,
  113.     };
  114.  
  115.     verify = NONE;
  116.  
  117.     err = setsockopt(fd, SOL_TLS, TLS_PEER_VERIFY, &verify, sizeof(verify));
  118.     if (err) {
  119.         printk("Failed to setup peer verification, err %d\n", errno);
  120.         return err;
  121.     }
  122.  
  123.     /* Associate the socket with the security tag
  124.      * we have provisioned the certificate with.
  125.      */
  126.     /*err = setsockopt(fd, SOL_TLS, TLS_SEC_TAG_LIST, tls_sec_tag,
  127.              sizeof(tls_sec_tag));
  128.     if (err) {
  129.         printk("Failed to setup TLS sec tag, err %d\n", errno);
  130.         return err;
  131.     }*/
  132.  
  133.     return 0;
  134. }
  135.  
  136. int blocking_recv(int fd, uint8_t *buf, uint32_t size, uint32_t flags)
  137. {
  138.     int err;
  139.  
  140.     do {
  141.                 printk("Receive\n\r");
  142.         err = recv(fd, buf, size, flags);
  143.     } while (err < 0 && errno == EAGAIN);
  144.  
  145.     return err;
  146. }
  147.  
  148. void main(void)
  149. {
  150.     int err;
  151.     int fd;
  152.     char *p;
  153.     int bytes;
  154.     size_t off;
  155.     struct addrinfo *res;
  156.     struct addrinfo hints = {
  157.         .ai_family = AF_INET,
  158.         .ai_socktype = SOCK_STREAM,
  159.     };
  160.  
  161.     printk("HTTPS client sample started\n\r");
  162.  
  163.     err = bsdlib_init();
  164.     if (err) {
  165.         printk("Failed to initialize bsdlib!");
  166.         return;
  167.     }
  168.  
  169.     /* Initialize AT comms in order to provision the certificate */
  170.     err = at_comms_init();
  171.     if (err) {
  172.         return;
  173.     }
  174.  
  175.         //err = cert_provision();
  176.     if (err) {
  177.         return;
  178.     }
  179.  
  180.     printk("Waiting for network.. ");
  181.     err = lte_lc_init_and_connect();
  182.     if (err) {
  183.         printk("Failed to connect to the LTE network, err %d\n", err);
  184.         return;
  185.     }
  186.     printk("OK\n");
  187.  
  188.     err = getaddrinfo(ONBOARDING_ENDPOINT, NULL, &hints, &res);
  189.     if (err) {
  190.         printk("getaddrinfo() failed, err %d\n", errno);
  191.         return;
  192.     }
  193.  
  194.     ((struct sockaddr_in *)res->ai_addr)->sin_port = htons(HTTPS_PORT);
  195.  
  196.     fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
  197.     if (fd == -1) {
  198.         printk("Failed to open socket!\n");
  199.         goto clean_up;
  200.     }
  201.  
  202.         char ipv4_addr[NET_IPV4_ADDR_LEN];
  203.         inet_ntop(AF_INET, &((struct sockaddr_in *)res->ai_addr)->sin_port, ipv4_addr,
  204.           sizeof(ipv4_addr));
  205.         printk("Connect to Addr IP: %s \n",ipv4_addr);
  206.  
  207.         //err = tls_setup(fd);
  208.     if (err) {
  209.         goto clean_up;
  210.     }
  211.  
  212.     err = connect(fd, res->ai_addr, sizeof(struct sockaddr_in));
  213.     if (err) {
  214.         printk("connect() failed, err: %d\n", errno);
  215.         goto clean_up;
  216.     }
  217.  
  218.     printk("send()\n");
  219.  
  220.         off = 0;
  221.  
  222.         char send_packet[100];
  223.         sprintf(send_packet, "GET /device-api/onboarding HTTP/1.1\r\n"
  224.                 "Host: %s\r\n"
  225.                 "Accept: text/csv\r\n\r\n", ONBOARDING_ENDPOINT);
  226.  
  227.         bytes = send(fd, &send_packet, strlen(send_packet), 0);
  228.  
  229.     printk("Sent %d bytes\n", bytes);
  230.  
  231.     off = 0;
  232.         k_sleep(K_MSEC(2000));
  233.  
  234.         int tot_num_bytes = 0;
  235.         do {
  236.                 /* TODO: make a proper timeout *
  237.                  * Current solution will just hang
  238.                  * until remote side closes connection */
  239.                 bytes =
  240.                         blocking_recv(fd, recv_buf, RECV_BUF_SIZE, 0);
  241.                 tot_num_bytes += bytes;
  242.                 if (bytes <= 0) {
  243.                         break;
  244.                 }
  245.         } while (tot_num_bytes > 0);
  246.  
  247.     printk("Received %d bytes\n", bytes);
  248.         printk("GOT %s", recv_buf);
  249.  
  250.     printk("Finished, closing socket.\n");
  251.  
  252. clean_up:
  253.     freeaddrinfo(res);
  254.     (void)close(fd);
  255. }
  256.  
Advertisement
Add Comment
Please, Sign In to add comment