Commit 62c29f82 authored by Kishen Maloor's avatar Kishen Maloor

Merge branch 'master' into fargo

Signed-off-by: Kishen Maloor's avatarKishen Maloor <kishen.maloor@intel.com>
parents 8a659eef 21fe3cb6
Pipeline #222 passed with stage
in 49 seconds
......@@ -21,43 +21,20 @@
#include "oc_api.h"
#include "oc_cloud_internal.h"
#include "oc_rep.h"
#include "oc_config.h"
#ifdef OC_DYNAMIC_ALLOCATION
#include <stdlib.h>
#endif /* OC_DYNAMIC_ALLOCATION */
#ifndef OC_STORAGE
#error Preprocessor macro OC_CLOUD is defined but OC_STORAGE is not defined \
check oc_config.h and make sure OC_STORAGE is defined if OC_CLOUD is defined.
#endif
#define CLOUD_STORE_NAME "cloud"
#define CLOUD_TAG_MAX (32)
#ifndef OC_SECURITY
// dummy storage
int
oc_storage_config(const char *store)
{
(void)store;
return 0;
}
long
oc_storage_read(const char *store, uint8_t *buf, size_t size)
{
(void)store;
(void)buf;
(void)size;
return -1;
}
long
oc_storage_write(const char *store, uint8_t *buf, size_t size)
{
(void)store;
(void)buf;
(void)size;
return -1;
}
#endif
static int cloud_store_load_internal(const char *store_name,
oc_cloud_store_t *store);
static void gen_cloud_tag(const char *name, size_t device, char *cloud_tag);
......
......@@ -496,6 +496,52 @@ oc_string_to_endpoint(oc_string_t *endpoint_str, oc_endpoint_t *endpoint,
return -1;
}
int
oc_endpoint_string_parse_path(oc_string_t *endpoint_str, oc_string_t *path)
{
if (!endpoint_str) {
return -1;
}
if (!path) {
return -1;
}
const char *address = NULL;
address = strstr(oc_string(*endpoint_str), "://");
if(!address) {
return -1;
}
// 3 is string length of "://"
address += 3;
size_t len = oc_string_len(*endpoint_str) - (address - oc_string(*endpoint_str));
// the smallest possible address is '0' anything smaller is invalid.
if(len < 1) {
return -1;
}
/* Extract a uri path if available */
const char *path_start = NULL;
const char *query_start = NULL;
path_start = memchr(address, '/', len);
if (!path_start) {
// no path found return error
return -1;
}
query_start = memchr((address + (path_start - address)), '?',
(len - (path_start - address)));
if (query_start) {
oc_new_string(path, path_start, (query_start - path_start));
} else {
oc_new_string(path, path_start, (len - (path_start - address)));
}
return 0;
}
int
oc_ipv6_endpoint_is_link_local(oc_endpoint_t *endpoint)
{
......
......@@ -22,14 +22,19 @@
#include "oc_introspection_internal.h"
#include <inttypes.h>
#include <stdio.h>
#include "oc_config.h"
#ifndef OC_IDD_API
#include "server_introspection.dat.h"
#else /* OC_IDD_API */
#if !defined(OC_STORAGE) && defined(OC_IDD_API)
#error Preprocessor macro OC_IDD_API is defined but OC_STORAGE is not defined \
check oc_config.h and make sure OC_STORAGE is defined if OC_IDD_API is defined.
#endif
#define MAX_TAG_LENGTH 20
#ifdef OC_SECURITY
static void
gen_idd_tag(const char *name, size_t device_index, char *idd_tag)
{
......@@ -39,19 +44,13 @@ gen_idd_tag(const char *name, size_t device_index, char *idd_tag)
(idd_tag_len < MAX_TAG_LENGTH) ? idd_tag_len + 1 : MAX_TAG_LENGTH;
idd_tag[idd_tag_len] = '\0';
}
#endif /* OC_SECURITY */
void
oc_set_introspection_data(size_t device, uint8_t *IDD, size_t IDD_size)
{
(void) device;
(void) IDD;
(void) IDD_size;
#ifdef OC_SECURITY
char idd_tag[MAX_TAG_LENGTH];
gen_idd_tag("IDD", device, idd_tag);
oc_storage_write(idd_tag, IDD, IDD_size);
#endif /* OC_SECURITY */
}
#endif /*OC_IDD_API*/
......@@ -74,12 +73,10 @@ oc_core_introspection_data_handler(oc_request_t *request,
IDD_size = -1;
}
#else /* OC_IDD_API */
#ifdef OC_SECURITY
char idd_tag[MAX_TAG_LENGTH];
gen_idd_tag("IDD", request->resource->device, idd_tag);
IDD_size = oc_storage_read(
idd_tag, request->response->response_buffer->buffer, OC_MAX_APP_DATA_SIZE);
#endif /* OC_SECURITY */
#endif /* OC_IDD_API */
if (IDD_size >= 0 && IDD_size < OC_MAX_APP_DATA_SIZE) {
......
......@@ -1229,7 +1229,7 @@ oc_ri_find_client_cb_by_token(uint8_t *token, uint8_t token_len)
return cb;
}
static bool
bool
oc_ri_is_client_cb_valid(oc_client_cb_t *client_cb)
{
oc_client_cb_t *cb = oc_list_head(client_cbs);
......@@ -1316,7 +1316,6 @@ oc_ri_invoke_client_cb(void *response, oc_client_cb_t *cb,
struct oc_memb rep_objects = { sizeof(oc_rep_t), 0, 0, 0, 0 };
#endif /* OC_DYNAMIC_ALLOCATION */
oc_rep_set_pool(&rep_objects);
if (payload_len) {
if (cb->discovery) {
if (oc_ri_process_discovery_payload(payload, payload_len, cb->handler,
......@@ -1401,7 +1400,6 @@ oc_ri_invoke_client_cb(void *response, oc_client_cb_t *cb,
}
}
}
return true;
}
......
......@@ -22,6 +22,11 @@
#include "oc_swupdate_internal.h"
#include "security/oc_pstat.h"
#ifndef OC_STORAGE
#error Preprocessor macro OC_SOFTWARE_UPDATE is defined but OC_STORAGE is not defined \
check oc_config.h and make sure OC_STORAGE is defined if OC_SOFTWARE_UPDATE is defined.
#endif
#ifdef OC_DYNAMIC_ALLOCATION
#include "port/oc_assert.h"
#include <stdlib.h>
......
This diff is collapsed.
......@@ -176,9 +176,9 @@ main(void)
oc_clock_time_t next_event;
#ifdef OC_SECURITY
#ifdef OC_STORAGE
oc_storage_config("./client_block_linux_creds");
#endif /* OC_SECURITY */
#endif /* OC_STORAGE */
oc_set_mtu_size(300);
oc_set_max_app_data_size(2048);
......
......@@ -427,9 +427,9 @@ main(void)
oc_clock_time_t next_event;
#ifdef OC_SECURITY
#ifdef OC_STORAGE
oc_storage_config("./client_collections_linux_creds");
#endif /* OC_SECURITY */
#endif /* OC_STORAGE */
init = oc_main_init(&handler);
if (init < 0)
......
......@@ -163,9 +163,9 @@ main(void)
oc_clock_time_t next_event;
#ifdef OC_SECURITY
#ifdef OC_STORAGE
oc_storage_config("./client_creds");
#endif /* OC_SECURITY */
#endif /* OC_STORAGE */
oc_set_con_res_announced(false);
init = oc_main_init(&handler);
......
......@@ -394,9 +394,9 @@ main(void)
.signal_event_loop =
signal_event_loop };
#ifdef OC_SECURITY
#ifdef OC_STORAGE
oc_storage_config("./client_multithread_linux_creds");
#endif /* OC_SECURITY */
#endif /* OC_STORAGE */
if (pthread_mutex_init(&mutex, NULL) < 0) {
printf("pthread_mutex_init failed!\n");
......
......@@ -565,7 +565,9 @@ ocf_event_thread(void *data)
.register_resources =
register_resources };
#ifdef OC_STORAGE
oc_storage_config("./cloud_tests_creds");
#endif /* OC_STORAGE */
if (pthread_mutex_init(&mutex, NULL) < 0) {
printf("pthread_mutex_init failed!\n");
......
......@@ -296,7 +296,9 @@ main(void)
.signal_event_loop = signal_event_loop,
.register_resources =
register_resources };
#ifdef OC_STORAGE
oc_storage_config("./cloud_server_creds/");
#endif /* OC_STORAGE */
ret = oc_main_init(&handler);
if (ret < 0)
......
......@@ -347,9 +347,9 @@ main(void)
oc_clock_time_t next_event;
#ifdef OC_SECURITY
#ifdef OC_STORAGE
oc_storage_config("./multi_device_client_creds");
#endif /* OC_SECURITY */
#endif /* OC_STORAGE */
init = oc_main_init(&handler);
if (init < 0)
......
......@@ -228,9 +228,9 @@ main(void)
oc_clock_time_t next_event;
#ifdef OC_SECURITY
#ifdef OC_STORAGE
oc_storage_config("./multi_device_server_creds");
#endif /* OC_SECURITY */
#endif /* OC_STORAGE */
init = oc_main_init(&handler);
if (init < 0)
......
......@@ -142,9 +142,9 @@ main(void)
oc_clock_time_t next_event;
#ifdef OC_SECURITY
#ifdef OC_STORAGE
oc_storage_config("./server_block_linux_creds");
#endif /* OC_SECURITY */
#endif /* OC_STORAGE */
oc_set_mtu_size(200);
oc_set_max_app_data_size(2048);
......
......@@ -338,9 +338,9 @@ main(void)
oc_clock_time_t next_event;
#ifdef OC_SECURITY
#ifdef OC_STORAGE
oc_storage_config("./server_collections_linux_creds");
#endif /* OC_SECURITY */
#endif /* OC_STORAGE */
init = oc_main_init(&handler);
if (init < 0)
......
......@@ -144,9 +144,9 @@ main(void)
oc_clock_time_t next_event;
#ifdef OC_SECURITY
#ifdef OC_STORAGE
oc_storage_config("./server_creds");
#endif /* OC_SECURITY */
#endif /* OC_STORAGE */
init = oc_main_init(&handler);
if (init < 0)
......
......@@ -267,9 +267,9 @@ main(void)
.register_resources =
register_resources };
#ifdef OC_SECURITY
#ifdef OC_STORAGE
oc_storage_config("./server_multithread_linux_creds");
#endif /* OC_SECURITY */
#endif /* OC_STORAGE */
if (pthread_mutex_init(&mutex, NULL) < 0) {
printf("pthread_mutex_init failed!\n");
......
......@@ -261,9 +261,9 @@ main(void)
oc_clock_time_t next_event;
#ifdef OC_SECURITY
#ifdef OC_STORAGE
oc_storage_config("./simpleclient_creds");
#endif /* OC_SECURITY */
#endif /* OC_STORAGE */
init = oc_main_init(&handler);
if (init < 0)
......
......@@ -260,9 +260,9 @@ main(void)
oc_clock_time_t next_event;
#ifdef OC_SECURITY
#ifdef OC_STORAGE
oc_storage_config("./simpleclient_creds/");
#endif /* OC_SECURITY */
#endif /* OC_STORAGE */
init = oc_main_init(&handler);
if (init < 0)
......
......@@ -155,9 +155,9 @@ main(void)
oc_clock_time_t next_event;
#ifdef OC_SECURITY
#ifdef OC_STORAGE
oc_storage_config("./simpleserver_creds");
#endif /* OC_SECURITY */
#endif /* OC_STORAGE */
init = oc_main_init(&handler);
if (init < 0)
......
......@@ -391,9 +391,9 @@ main(void)
oc_set_mtu_size(16384);
oc_set_max_app_data_size(16384);
#ifdef OC_SECURITY
#ifdef OC_STORAGE
oc_storage_config("./simpleserver_pki_creds");
#endif /* OC_SECURITY */
#endif /* OC_STORAGE */
oc_set_factory_presets_cb(factory_presets_cb, NULL);
......
......@@ -151,9 +151,9 @@ main(void)
oc_clock_time_t next_event;
#ifdef OC_SECURITY
#ifdef OC_STORAGE
oc_storage_config("./simpleserver_creds/");
#endif /* OC_SECURITY */
#endif /* OC_STORAGE */
init = oc_main_init(&handler);
if (init < 0)
......
......@@ -696,9 +696,9 @@ main(void)
oc_set_con_res_announced(false);
oc_set_max_app_data_size(16384);
#ifdef OC_SECURITY
#ifdef OC_STORAGE
oc_storage_config("./smart_home_server_linux_creds");
#endif /* OC_SECURITY */
#endif /* OC_STORAGE */
oc_set_factory_presets_cb(factory_presets_cb, NULL);
#ifdef OC_SECURITY
......
......@@ -310,9 +310,9 @@ main(void)
oc_set_con_res_announced(false);
oc_set_max_app_data_size(16384);
#ifdef OC_SECURITY
#ifdef OC_STORAGE
oc_storage_config("./smart_home_server_with_mock_swupdate_creds");
#endif /* OC_SECURITY */
#endif /* OC_STORAGE */
oc_set_factory_presets_cb(factory_presets_cb, NULL);
#ifdef OC_SECURITY
......
......@@ -412,10 +412,10 @@ main(void)
.signal_event_loop = signal_event_loop,
.requests_entry = issue_requests };
#ifdef OC_SECURITY
#ifdef OC_STORAGE
oc_storage_config("./smart_lock_creds");
oc_set_random_pin_callback(random_pin_cb, NULL);
#endif /* OC_SECURITY */
#endif /* OC_STORAGE */
oc_set_con_res_announced(false);
init = oc_main_init(&handler);
......
......@@ -141,9 +141,9 @@ main(void)
oc_clock_time_t next_event;
#ifdef OC_SECURITY
#ifdef OC_STORAGE
oc_storage_config("./temp_sensor_creds");
#endif /* OC_SECURITY */
#endif /* OC_STORAGE */
int init = oc_main_init(&handler);
......
......@@ -252,7 +252,100 @@ oc_clock_time_t oc_main_poll(void);
*/
void oc_main_shutdown(void);
/**
* Callback invoked by the stack initialization to perform any
* "factory settings", e.g., this may be used to load a manufacturer
* certificate.
*
* The following example illustrates the method of loading a manufacturer
* certificate chain (end-entity certificate, intermediate CA certificate, and
* root CA certificate) using oc_pki_xxx APIs.
*
* Example:
* ```
* 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
* }
* ```
* @param[in] device number of the device
* @param[in] data context pointer that comes from the
* oc_set_factory_presets_cb() function
*
* @see oc_set_factory_presets_cb
* @see oc_pki_add_mfg_cert
* @see oc_pki_add_mfg_intermediate_cert
* @see oc_pki_add_mfg_trust_anchor
* @see oc_pki_set_security_profile
*/
typedef void (*oc_factory_presets_cb_t)(size_t device, void *data);
/**
* Set the factory presets callback.
*
* The factory presets callback is called by the stack to enable per-device
* presets.
*
* @note oc_set_factory_presets_cb() must be called before oc_main_init().
*
* @param[in] cb oc_factory_presets_cb_t function pointer to be called
* @param[in] data context pointer that is passed to the oc_factory_presets_cb_t
* the pointer must be a valid point till after oc_main_init()
* call completes.
*/
void oc_set_factory_presets_cb(oc_factory_presets_cb_t cb, void *data);
/**
......@@ -333,7 +426,7 @@ int oc_add_device(const char *uri, const char *rt, const char *name,
* @param[in] init_platform_cb callback function invoked during
* oc_init_platform(). The purpose is to add additional device properties that
* are not supplied to oc_init_platform() function call.
* @param data context pointer that is passed to the oc_init_platform_cb_t
* @param[in] data context pointer that is passed to the oc_init_platform_cb_t
*
* @return
* - `0` on success
......@@ -361,29 +454,110 @@ int oc_init_platform(const char *mfg_name,
#define oc_set_custom_platform_property(prop, value) \
oc_rep_set_text_string(root, prop, value)
/**
* Callback invoked when an onboarding client requests device ownership via the
* Random PIN Ownership Transfer Method (OTM). The purpose of the callback is
* to allow the device to display the random PIN to the user onboarding the
* device.
*
* Example:
* ```
* #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
*
* int main(void) {
* ...
* #ifdef OC_SECURITY
* oc_storage_config("./server_creds");
* oc_set_random_pin_callback(random_pin_cb, NULL);
* #endif // OC_SECURITY
* // handler code omitted from example code see oc_main_init
* oc_main_init(&handler)
* ...
* return 0;
* }
* ```
*
* @param[in] data context pointer that comes from the oc_init_platform()
* function
*
* @see oc_set_random_pin_callback
*/
typedef void (*oc_random_pin_cb_t)(const unsigned char *pin, size_t pin_len,
void *data);
/**
* Set the random pin callback for Random PIN Ownership Transfer Method (OTM)
*
* @note This should be set before invoking oc_main_init().
*
* @param[in] cb callback function invoked when client requests Random PIN OTM
* @param[in] data context pointer that is passed to the oc_random_pin_cb_t the
* context pointer must be a valid pointer as long as the device
* is in 'Ready For Ownership Transfer Method' (RFOTM) state.
*
* @see oc_random_pin_cb_t
* @see oc_main_init
*/
void oc_set_random_pin_callback(oc_random_pin_cb_t cb, void *data);
/**
@brief Returns whether the oic.wk.con res is announced.
@return true if announced (default) or false if not
@see oc_set_con_res_announced
@see oc_set_con_write_cb
*/
* Returns whether the oic.wk.con resource is advertised.
*
* @return
* - true if advertised (default)
* - false if not
*
* @see oc_set_con_res_announced
* @see oc_set_con_write_cb
*/
bool oc_get_con_res_announced(void);
/**
@brief Sets whether the oic.wk.con res is announed.
@brief Sets whether the oic.wk.con res is announced.
@note This should be set before invoking \c oc_main_init().
@param announce true to announce (default) or false if not
@param[in] announce true to announce (default) or false if not
@see oc_get_con_res_announced
@see oc_set_con_write_cb
*/