object_security.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. /*******************************************************************************
  2. *
  3. * Copyright (c) 2013, 2014, 2015 Intel Corporation and others.
  4. * All rights reserved. This program and the accompanying materials
  5. * are made available under the terms of the Eclipse Public License v1.0
  6. * and Eclipse Distribution License v1.0 which accompany this distribution.
  7. *
  8. * The Eclipse Public License is available at
  9. * http://www.eclipse.org/legal/epl-v10.html
  10. * The Eclipse Distribution License is available at
  11. * http://www.eclipse.org/org/documents/edl-v10.php.
  12. *
  13. * Contributors:
  14. * David Navarro, Intel Corporation - initial API and implementation
  15. * Bosch Software Innovations GmbH - Please refer to git log
  16. * Pascal Rieux - Please refer to git log
  17. *
  18. *******************************************************************************/
  19. /*
  20. * Resources:
  21. *
  22. * Name | ID | Operations | Instances | Mandatory | Type | Range | Units |
  23. * Server URI | 0 | | Single | Yes | String | | |
  24. * Bootstrap Server | 1 | | Single | Yes | Boolean | | |
  25. * Security Mode | 2 | | Single | Yes | Integer | 0-3 | |
  26. * Public Key or ID | 3 | | Single | Yes | Opaque | | |
  27. * Server Public Key or ID | 4 | | Single | Yes | Opaque | | |
  28. * Secret Key | 5 | | Single | Yes | Opaque | | |
  29. * SMS Security Mode | 6 | | Single | Yes | Integer | 0-255 | |
  30. * SMS Binding Key Param. | 7 | | Single | Yes | Opaque | 6 B | |
  31. * SMS Binding Secret Keys | 8 | | Single | Yes | Opaque | 32-48 B | |
  32. * Server SMS Number | 9 | | Single | Yes | Integer | | |
  33. * Short Server ID | 10 | | Single | No | Integer | 1-65535 | |
  34. * Client Hold Off Time | 11 | | Single | Yes | Integer | | s |
  35. *
  36. */
  37. /*
  38. * Here we implement a very basic LWM2M Security Object which only knows NoSec security mode.
  39. */
  40. #include "liblwm2m.h"
  41. #include <stdlib.h>
  42. #include <string.h>
  43. #include <stdio.h>
  44. typedef struct _security_instance_
  45. {
  46. struct _security_instance_ * next; // matches lwm2m_list_t::next
  47. uint16_t instanceId; // matches lwm2m_list_t::id
  48. char * uri;
  49. bool isBootstrap;
  50. uint16_t shortID;
  51. uint32_t clientHoldOffTime;
  52. } security_instance_t;
  53. static uint8_t prv_get_value(lwm2m_data_t * dataP,
  54. security_instance_t * targetP)
  55. {
  56. switch (dataP->id)
  57. {
  58. case LWM2M_SECURITY_URI_ID:
  59. lwm2m_data_encode_string(targetP->uri, dataP);
  60. return COAP_205_CONTENT;
  61. case LWM2M_SECURITY_BOOTSTRAP_ID:
  62. lwm2m_data_encode_bool(targetP->isBootstrap, dataP);
  63. return COAP_205_CONTENT;
  64. case LWM2M_SECURITY_SECURITY_ID:
  65. lwm2m_data_encode_int(LWM2M_SECURITY_MODE_NONE, dataP);
  66. return COAP_205_CONTENT;
  67. case LWM2M_SECURITY_PUBLIC_KEY_ID:
  68. // Here we return an opaque of 1 byte containing 0
  69. {
  70. uint8_t value = 0;
  71. lwm2m_data_encode_opaque(&value, 1, dataP);
  72. }
  73. return COAP_205_CONTENT;
  74. case LWM2M_SECURITY_SERVER_PUBLIC_KEY_ID:
  75. // Here we return an opaque of 1 byte containing 0
  76. {
  77. uint8_t value = 0;
  78. lwm2m_data_encode_opaque(&value, 1, dataP);
  79. }
  80. return COAP_205_CONTENT;
  81. case LWM2M_SECURITY_SECRET_KEY_ID:
  82. // Here we return an opaque of 1 byte containing 0
  83. {
  84. uint8_t value = 0;
  85. lwm2m_data_encode_opaque(&value, 1, dataP);
  86. }
  87. return COAP_205_CONTENT;
  88. case LWM2M_SECURITY_SMS_SECURITY_ID:
  89. lwm2m_data_encode_int(LWM2M_SECURITY_MODE_NONE, dataP);
  90. return COAP_205_CONTENT;
  91. case LWM2M_SECURITY_SMS_KEY_PARAM_ID:
  92. // Here we return an opaque of 6 bytes containing a buggy value
  93. {
  94. char * value = "12345";
  95. lwm2m_data_encode_opaque((uint8_t *)value, 6, dataP);
  96. }
  97. return COAP_205_CONTENT;
  98. case LWM2M_SECURITY_SMS_SECRET_KEY_ID:
  99. // Here we return an opaque of 32 bytes containing a buggy value
  100. {
  101. char * value = "1234567890abcdefghijklmnopqrstu";
  102. lwm2m_data_encode_opaque((uint8_t *)value, 32, dataP);
  103. }
  104. return COAP_205_CONTENT;
  105. case LWM2M_SECURITY_SMS_SERVER_NUMBER_ID:
  106. lwm2m_data_encode_int(0, dataP);
  107. return COAP_205_CONTENT;
  108. case LWM2M_SECURITY_SHORT_SERVER_ID:
  109. lwm2m_data_encode_int(targetP->shortID, dataP);
  110. return COAP_205_CONTENT;
  111. case LWM2M_SECURITY_HOLD_OFF_ID:
  112. lwm2m_data_encode_int(targetP->clientHoldOffTime, dataP);
  113. return COAP_205_CONTENT;
  114. default:
  115. return COAP_404_NOT_FOUND;
  116. }
  117. }
  118. static uint8_t prv_security_read(uint16_t instanceId,
  119. int * numDataP,
  120. lwm2m_data_t ** dataArrayP,
  121. lwm2m_object_t * objectP)
  122. {
  123. security_instance_t * targetP;
  124. uint8_t result;
  125. int i;
  126. targetP = (security_instance_t *)lwm2m_list_find(objectP->instanceList, instanceId);
  127. if (NULL == targetP) return COAP_404_NOT_FOUND;
  128. // is the server asking for the full instance ?
  129. if (*numDataP == 0)
  130. {
  131. uint16_t resList[] = {LWM2M_SECURITY_URI_ID,
  132. LWM2M_SECURITY_BOOTSTRAP_ID,
  133. LWM2M_SECURITY_SECURITY_ID,
  134. LWM2M_SECURITY_PUBLIC_KEY_ID,
  135. LWM2M_SECURITY_SERVER_PUBLIC_KEY_ID,
  136. LWM2M_SECURITY_SECRET_KEY_ID,
  137. LWM2M_SECURITY_SMS_SECURITY_ID,
  138. LWM2M_SECURITY_SMS_KEY_PARAM_ID,
  139. LWM2M_SECURITY_SMS_SECRET_KEY_ID,
  140. LWM2M_SECURITY_SMS_SERVER_NUMBER_ID,
  141. LWM2M_SECURITY_SHORT_SERVER_ID,
  142. LWM2M_SECURITY_HOLD_OFF_ID};
  143. int nbRes = sizeof(resList)/sizeof(uint16_t);
  144. *dataArrayP = lwm2m_data_new(nbRes);
  145. if (*dataArrayP == NULL) return COAP_500_INTERNAL_SERVER_ERROR;
  146. *numDataP = nbRes;
  147. for (i = 0 ; i < nbRes ; i++)
  148. {
  149. (*dataArrayP)[i].id = resList[i];
  150. }
  151. }
  152. i = 0;
  153. do
  154. {
  155. result = prv_get_value((*dataArrayP) + i, targetP);
  156. i++;
  157. } while (i < *numDataP && result == COAP_205_CONTENT);
  158. return result;
  159. }
  160. lwm2m_object_t * get_security_object()
  161. {
  162. lwm2m_object_t * securityObj;
  163. securityObj = (lwm2m_object_t *)lwm2m_malloc(sizeof(lwm2m_object_t));
  164. if (NULL != securityObj)
  165. {
  166. security_instance_t * targetP;
  167. memset(securityObj, 0, sizeof(lwm2m_object_t));
  168. securityObj->objID = 0;
  169. // Manually create an hardcoded instance
  170. targetP = (security_instance_t *)lwm2m_malloc(sizeof(security_instance_t));
  171. if (NULL == targetP)
  172. {
  173. lwm2m_free(securityObj);
  174. return NULL;
  175. }
  176. memset(targetP, 0, sizeof(security_instance_t));
  177. targetP->instanceId = 0;
  178. targetP->uri = strdup("coap://localhost:5683");
  179. targetP->isBootstrap = false;
  180. targetP->shortID = 123;
  181. targetP->clientHoldOffTime = 10;
  182. securityObj->instanceList = LWM2M_LIST_ADD(securityObj->instanceList, targetP);
  183. securityObj->readFunc = prv_security_read;
  184. }
  185. return securityObj;
  186. }
  187. void free_security_object(lwm2m_object_t * objectP)
  188. {
  189. while (objectP->instanceList != NULL)
  190. {
  191. security_instance_t * securityInstance = (security_instance_t *)objectP->instanceList;
  192. objectP->instanceList = objectP->instanceList->next;
  193. if (NULL != securityInstance->uri)
  194. {
  195. lwm2m_free(securityInstance->uri);
  196. }
  197. lwm2m_free(securityInstance);
  198. }
  199. lwm2m_free(objectP);
  200. }
  201. char * get_server_uri(lwm2m_object_t * objectP,
  202. uint16_t secObjInstID)
  203. {
  204. security_instance_t * targetP = (security_instance_t *)LWM2M_LIST_FIND(objectP->instanceList, secObjInstID);
  205. if (NULL != targetP)
  206. {
  207. return lwm2m_strdup(targetP->uri);
  208. }
  209. return NULL;
  210. }