lwm2m_objects_tlv.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498
  1. /**
  2. * Copyright (c) 2015 - 2018, Nordic Semiconductor ASA
  3. *
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without modification,
  7. * are permitted provided that the following conditions are met:
  8. *
  9. * 1. Redistributions of source code must retain the above copyright notice, this
  10. * list of conditions and the following disclaimer.
  11. *
  12. * 2. Redistributions in binary form, except as embedded into a Nordic
  13. * Semiconductor ASA integrated circuit in a product or a software update for
  14. * such product, must reproduce the above copyright notice, this list of
  15. * conditions and the following disclaimer in the documentation and/or other
  16. * materials provided with the distribution.
  17. *
  18. * 3. Neither the name of Nordic Semiconductor ASA nor the names of its
  19. * contributors may be used to endorse or promote products derived from this
  20. * software without specific prior written permission.
  21. *
  22. * 4. This software, with or without modification, must only be used with a
  23. * Nordic Semiconductor ASA integrated circuit.
  24. *
  25. * 5. Any software provided in binary form under this license must not be reverse
  26. * engineered, decompiled, modified and/or disassembled.
  27. *
  28. * THIS SOFTWARE IS PROVIDED BY NORDIC SEMICONDUCTOR ASA "AS IS" AND ANY EXPRESS
  29. * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  30. * OF MERCHANTABILITY, NONINFRINGEMENT, AND FITNESS FOR A PARTICULAR PURPOSE ARE
  31. * DISCLAIMED. IN NO EVENT SHALL NORDIC SEMICONDUCTOR ASA OR CONTRIBUTORS BE
  32. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  33. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
  34. * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  35. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  36. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
  37. * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  38. *
  39. */
  40. #include "lwm2m_objects_tlv.h"
  41. #include "lwm2m_tlv.h"
  42. static void index_buffer_len_update(uint32_t * index, uint32_t * buffer_len, uint32_t max_buffer)
  43. {
  44. *index += *buffer_len;
  45. *buffer_len = max_buffer - *index;
  46. }
  47. uint32_t lwm2m_tlv_server_decode(lwm2m_server_t * server, uint8_t * buffer, uint32_t buffer_len)
  48. {
  49. uint32_t err_code;
  50. lwm2m_tlv_t tlv;
  51. uint32_t index = 0;
  52. while (index < buffer_len)
  53. {
  54. err_code = lwm2m_tlv_decode(&tlv, &index, buffer, buffer_len);
  55. if (err_code != NRF_SUCCESS)
  56. {
  57. return err_code;
  58. }
  59. switch (tlv.id)
  60. {
  61. case LWM2M_SERVER_SHORT_SERVER_ID:
  62. {
  63. if (lwm2m_tlv_bytebuffer_to_uint16(tlv.value, tlv.length, &server->short_server_id) != NRF_SUCCESS)
  64. {
  65. return NRF_ERROR_INVALID_DATA;
  66. }
  67. break;
  68. }
  69. case LWM2M_SERVER_LIFETIME:
  70. {
  71. if (lwm2m_tlv_bytebuffer_to_uint32(tlv.value, tlv.length, &server->lifetime))
  72. {
  73. return NRF_ERROR_INVALID_DATA;
  74. }
  75. break;
  76. }
  77. case LWM2M_SERVER_DEFAULT_MIN_PERIOD:
  78. {
  79. if (lwm2m_tlv_bytebuffer_to_uint32(tlv.value, tlv.length, &server->default_minimum_period))
  80. {
  81. return NRF_ERROR_INVALID_DATA;
  82. }
  83. break;
  84. }
  85. case LWM2M_SERVER_DEFAULT_MAX_PERIOD:
  86. {
  87. if (lwm2m_tlv_bytebuffer_to_uint32(tlv.value, tlv.length, &server->default_maximum_period))
  88. {
  89. return NRF_ERROR_INVALID_DATA;
  90. }
  91. break;
  92. }
  93. case LWM2M_SERVER_DISABLE:
  94. {
  95. // Execute do nothing
  96. break;
  97. }
  98. case LWM2M_SERVER_DISABLE_TIMEOUT:
  99. {
  100. if (lwm2m_tlv_bytebuffer_to_uint32(tlv.value, tlv.length, &server->disable_timeout))
  101. {
  102. return NRF_ERROR_INVALID_DATA;
  103. }
  104. break;
  105. }
  106. case LWM2M_SERVER_NOTIFY_WHEN_DISABLED:
  107. {
  108. server->notification_storing_on_disabled = tlv.value[0];
  109. break;
  110. }
  111. case LWM2M_SERVER_BINDING:
  112. {
  113. // If original buffer is gone this will also be gone
  114. server->binding.len = tlv.length;
  115. server->binding.p_val = (char *) tlv.value;
  116. break;
  117. }
  118. case LWM2M_SERVER_REGISTRATION_UPDATE_TRIGGER:
  119. {
  120. // Execute do nothing
  121. break;
  122. }
  123. default:
  124. break;
  125. }
  126. }
  127. return NRF_SUCCESS;
  128. }
  129. uint32_t lwm2m_tlv_security_decode(lwm2m_security_t * p_security,
  130. uint8_t * p_buffer,
  131. uint32_t buffer_len)
  132. {
  133. uint32_t err_code;
  134. lwm2m_tlv_t tlv;
  135. uint32_t index = 0;
  136. while (index < buffer_len)
  137. {
  138. err_code = lwm2m_tlv_decode(&tlv, &index, p_buffer, buffer_len);
  139. if (err_code != NRF_SUCCESS)
  140. {
  141. return err_code;
  142. }
  143. switch (tlv.id)
  144. {
  145. case LWM2M_SECURITY_SERVER_URI:
  146. {
  147. p_security->server_uri.p_val = (char *)tlv.value;
  148. p_security->server_uri.len = tlv.length;
  149. break;
  150. }
  151. case LWM2M_SECURITY_BOOTSTRAP_SERVER:
  152. {
  153. p_security->bootstrap_server = tlv.value[0];
  154. break;
  155. }
  156. case LWM2M_SECURITY_SECURITY_MODE:
  157. {
  158. p_security->security_mode = tlv.value[0];
  159. break;
  160. }
  161. case LWM2M_SECURITY_PUBLIC_KEY:
  162. {
  163. p_security->public_key.p_val = tlv.value;
  164. p_security->public_key.len = tlv.length;
  165. break;
  166. }
  167. case LWM2M_SECURITY_SERVER_PUBLIC_KEY:
  168. {
  169. p_security->server_public_key.p_val = tlv.value;
  170. p_security->server_public_key.len = tlv.length;
  171. break;
  172. }
  173. case LWM2M_SECURITY_SECRET_KEY:
  174. {
  175. p_security->secret_key.p_val = tlv.value;
  176. p_security->secret_key.len = tlv.length;
  177. break;
  178. }
  179. case LWM2M_SECURITY_SMS_SECURITY_MODE:
  180. {
  181. p_security->sms_security_mode = tlv.value[0];
  182. break;
  183. }
  184. case LWM2M_SECURITY_SMS_BINDING_KEY_PARAM:
  185. {
  186. p_security->sms_binding_key_param.p_val = tlv.value;
  187. p_security->sms_binding_key_param.len = tlv.length;
  188. break;
  189. }
  190. case LWM2M_SECURITY_SMS_BINDING_SECRET_KEY:
  191. {
  192. p_security->sms_binding_secret_keys.p_val = tlv.value;
  193. p_security->sms_binding_secret_keys.len = tlv.length;
  194. break;
  195. }
  196. case LWM2M_SECURITY_SERVER_SMS_NUMBER:
  197. {
  198. uint32_t result = lwm2m_tlv_bytebuffer_to_uint32(tlv.value,
  199. tlv.length,
  200. &p_security->sms_number);
  201. if (result != NRF_SUCCESS)
  202. {
  203. return NRF_ERROR_INVALID_DATA;
  204. }
  205. break;
  206. }
  207. case LWM2M_SECURITY_SHORT_SERVER_ID:
  208. {
  209. uint32_t result = lwm2m_tlv_bytebuffer_to_uint16(tlv.value,
  210. tlv.length,
  211. &p_security->short_server_id);
  212. if (result != NRF_SUCCESS)
  213. {
  214. return NRF_ERROR_INVALID_DATA;
  215. }
  216. break;
  217. }
  218. case LWM2M_SECURITY_CLIENT_HOLD_OFF_TIME:
  219. {
  220. uint32_t result = lwm2m_tlv_bytebuffer_to_uint32(tlv.value,
  221. tlv.length,
  222. &p_security->client_hold_off_time);
  223. if (result != NRF_SUCCESS)
  224. {
  225. return NRF_ERROR_INVALID_DATA;
  226. }
  227. break;
  228. }
  229. default:
  230. break;
  231. }
  232. }
  233. return NRF_SUCCESS;
  234. }
  235. uint32_t lwm2m_tlv_server_encode(uint8_t * p_buffer,
  236. uint32_t * p_buffer_len,
  237. lwm2m_server_t * p_server)
  238. {
  239. uint32_t err_code;
  240. uint32_t max_buffer = *p_buffer_len;
  241. uint32_t index = 0;
  242. lwm2m_tlv_t tlv;
  243. tlv.id_type = TLV_TYPE_RESOURCE_VAL; // Type is the same for all.
  244. // Encode short server id.
  245. lwm2m_tlv_uint16_set(&tlv, p_server->short_server_id, LWM2M_SERVER_SHORT_SERVER_ID);
  246. err_code = lwm2m_tlv_encode(p_buffer + index, p_buffer_len, &tlv);
  247. if (err_code != NRF_SUCCESS)
  248. {
  249. return err_code;
  250. }
  251. index_buffer_len_update(&index, p_buffer_len, max_buffer);
  252. // Encode lifetime.
  253. lwm2m_tlv_uint32_set(&tlv, p_server->lifetime, LWM2M_SERVER_LIFETIME);
  254. err_code = lwm2m_tlv_encode(p_buffer + index, p_buffer_len, &tlv);
  255. if (err_code != NRF_SUCCESS)
  256. {
  257. return err_code;
  258. }
  259. index_buffer_len_update(&index, p_buffer_len, max_buffer);
  260. // Encode default minimum period.
  261. lwm2m_tlv_uint32_set(&tlv, p_server->default_minimum_period, LWM2M_SERVER_DEFAULT_MIN_PERIOD);
  262. err_code = lwm2m_tlv_encode(p_buffer + index, p_buffer_len, &tlv);
  263. if (err_code != NRF_SUCCESS)
  264. {
  265. return err_code;
  266. }
  267. index_buffer_len_update(&index, p_buffer_len, max_buffer);
  268. // Encode default maximum period.
  269. lwm2m_tlv_uint32_set(&tlv, p_server->default_maximum_period, LWM2M_SERVER_DEFAULT_MAX_PERIOD);
  270. err_code = lwm2m_tlv_encode(p_buffer + index, p_buffer_len, &tlv);
  271. if (err_code != NRF_SUCCESS)
  272. {
  273. return err_code;
  274. }
  275. index_buffer_len_update(&index, p_buffer_len, max_buffer);
  276. // Encode disable timeout.
  277. lwm2m_tlv_uint32_set(&tlv, p_server->disable_timeout, LWM2M_SERVER_DISABLE_TIMEOUT);
  278. err_code = lwm2m_tlv_encode(p_buffer + index, p_buffer_len, &tlv);
  279. if (err_code != NRF_SUCCESS)
  280. {
  281. return err_code;
  282. }
  283. index_buffer_len_update(&index, p_buffer_len, max_buffer);
  284. // Encode Notify when disabled.
  285. lwm2m_tlv_bool_set(&tlv, p_server->notification_storing_on_disabled, LWM2M_SERVER_NOTIFY_WHEN_DISABLED);
  286. err_code = lwm2m_tlv_encode(p_buffer + index, p_buffer_len, &tlv);
  287. if (err_code != NRF_SUCCESS)
  288. {
  289. return err_code;
  290. }
  291. index_buffer_len_update(&index, p_buffer_len, max_buffer);
  292. // Encode binding.
  293. lwm2m_tlv_string_set(&tlv, p_server->binding, LWM2M_SERVER_BINDING);
  294. err_code = lwm2m_tlv_encode(p_buffer + index, p_buffer_len, &tlv);
  295. if (err_code != NRF_SUCCESS)
  296. {
  297. return err_code;
  298. }
  299. index_buffer_len_update(&index, p_buffer_len, max_buffer);
  300. *p_buffer_len = index;
  301. return NRF_SUCCESS;
  302. }
  303. uint32_t lwm2m_tlv_security_encode(uint8_t * p_buffer,
  304. uint32_t * p_buffer_len,
  305. lwm2m_security_t * p_security)
  306. {
  307. uint32_t err_code;
  308. uint32_t max_buffer = *p_buffer_len;
  309. uint32_t index = 0;
  310. lwm2m_tlv_t tlv;
  311. tlv.id_type = TLV_TYPE_RESOURCE_VAL; // type is the same for all.
  312. lwm2m_tlv_string_set(&tlv, p_security->server_uri, LWM2M_SECURITY_SERVER_URI);
  313. err_code = lwm2m_tlv_encode(p_buffer + index, p_buffer_len, &tlv);
  314. if (err_code != NRF_SUCCESS)
  315. {
  316. return err_code;
  317. }
  318. index_buffer_len_update(&index, p_buffer_len, max_buffer);
  319. lwm2m_tlv_bool_set(&tlv, p_security->bootstrap_server, LWM2M_SECURITY_BOOTSTRAP_SERVER);
  320. err_code = lwm2m_tlv_encode(p_buffer + index, p_buffer_len, &tlv);
  321. if (err_code != NRF_SUCCESS)
  322. {
  323. return err_code;
  324. }
  325. index_buffer_len_update(&index, p_buffer_len, max_buffer);
  326. lwm2m_tlv_uint16_set(&tlv, p_security->security_mode, LWM2M_SECURITY_SECURITY_MODE);
  327. err_code = lwm2m_tlv_encode(p_buffer + index, p_buffer_len, &tlv);
  328. if (err_code != NRF_SUCCESS)
  329. {
  330. return err_code;
  331. }
  332. index_buffer_len_update(&index, p_buffer_len, max_buffer);
  333. lwm2m_tlv_opaque_set(&tlv, p_security->public_key, LWM2M_SECURITY_PUBLIC_KEY);
  334. err_code = lwm2m_tlv_encode(p_buffer + index, p_buffer_len, &tlv);
  335. if (err_code != NRF_SUCCESS)
  336. {
  337. return err_code;
  338. }
  339. index_buffer_len_update(&index, p_buffer_len, max_buffer);
  340. lwm2m_tlv_opaque_set(&tlv, p_security->server_public_key, LWM2M_SECURITY_SERVER_PUBLIC_KEY);
  341. err_code = lwm2m_tlv_encode(p_buffer + index, p_buffer_len, &tlv);
  342. if (err_code != NRF_SUCCESS)
  343. {
  344. return err_code;
  345. }
  346. index_buffer_len_update(&index, p_buffer_len, max_buffer);
  347. lwm2m_tlv_opaque_set(&tlv, p_security->secret_key, LWM2M_SECURITY_SECRET_KEY);
  348. err_code = lwm2m_tlv_encode(p_buffer + index, p_buffer_len, &tlv);
  349. if (err_code != NRF_SUCCESS)
  350. {
  351. return err_code;
  352. }
  353. index_buffer_len_update(&index, p_buffer_len, max_buffer);
  354. lwm2m_tlv_uint16_set(&tlv, p_security->sms_security_mode, LWM2M_SECURITY_SMS_SECURITY_MODE);
  355. err_code = lwm2m_tlv_encode(p_buffer + index, p_buffer_len, &tlv);
  356. if (err_code != NRF_SUCCESS)
  357. {
  358. return err_code;
  359. }
  360. index_buffer_len_update(&index, p_buffer_len, max_buffer);
  361. lwm2m_tlv_opaque_set(&tlv, p_security->sms_binding_key_param, LWM2M_SECURITY_SMS_BINDING_KEY_PARAM);
  362. err_code = lwm2m_tlv_encode(p_buffer + index, p_buffer_len, &tlv);
  363. if (err_code != NRF_SUCCESS)
  364. {
  365. return err_code;
  366. }
  367. index_buffer_len_update(&index, p_buffer_len, max_buffer);
  368. lwm2m_tlv_opaque_set(&tlv, p_security->sms_binding_secret_keys, LWM2M_SECURITY_SMS_BINDING_SECRET_KEY);
  369. err_code = lwm2m_tlv_encode(p_buffer + index, p_buffer_len, &tlv);
  370. if (err_code != NRF_SUCCESS)
  371. {
  372. return err_code;
  373. }
  374. index_buffer_len_update(&index, p_buffer_len, max_buffer);
  375. lwm2m_tlv_uint32_set(&tlv, p_security->sms_number, LWM2M_SECURITY_SERVER_SMS_NUMBER);
  376. err_code = lwm2m_tlv_encode(p_buffer + index, p_buffer_len, &tlv);
  377. if (err_code != NRF_SUCCESS)
  378. {
  379. return err_code;
  380. }
  381. index_buffer_len_update(&index, p_buffer_len, max_buffer);
  382. lwm2m_tlv_uint16_set(&tlv, p_security->short_server_id, LWM2M_SECURITY_SHORT_SERVER_ID);
  383. err_code = lwm2m_tlv_encode(p_buffer + index, p_buffer_len, &tlv);
  384. if (err_code != NRF_SUCCESS)
  385. {
  386. return err_code;
  387. }
  388. index_buffer_len_update(&index, p_buffer_len, max_buffer);
  389. lwm2m_tlv_uint32_set(&tlv, p_security->client_hold_off_time, LWM2M_SECURITY_CLIENT_HOLD_OFF_TIME);
  390. err_code = lwm2m_tlv_encode(p_buffer + index, p_buffer_len, &tlv);
  391. if (err_code != NRF_SUCCESS)
  392. {
  393. return err_code;
  394. }
  395. index_buffer_len_update(&index, p_buffer_len, max_buffer);
  396. return NRF_SUCCESS;
  397. }