blob: a846f68499669f5daf064926ff15e58ff244a528 [file] [log] [blame]
b.liue9582032025-04-17 19:18:16 +08001/*******************************************************************************
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/*
21 * Resources:
22 *
23 * Name | ID | Operations | Instances | Mandatory | Type | Range | Units |
24 * Server URI | 0 | | Single | Yes | String | | |
25 * Bootstrap Server | 1 | | Single | Yes | Boolean | | |
26 * Security Mode | 2 | | Single | Yes | Integer | 0-3 | |
27 * Public Key or ID | 3 | | Single | Yes | Opaque | | |
28 * Server Public Key or ID | 4 | | Single | Yes | Opaque | | |
29 * Secret Key | 5 | | Single | Yes | Opaque | | |
30 * SMS Security Mode | 6 | | Single | Yes | Integer | 0-255 | |
31 * SMS Binding Key Param. | 7 | | Single | Yes | Opaque | 6 B | |
32 * SMS Binding Secret Keys | 8 | | Single | Yes | Opaque | 32-48 B | |
33 * Server SMS Number | 9 | | Single | Yes | Integer | | |
34 * Short Server ID | 10 | | Single | No | Integer | 1-65535 | |
35 * Client Hold Off Time | 11 | | Single | Yes | Integer | | s |
36 *
37 */
38
39/*
40 * Here we implement a very basic LWM2M Security Object which only knows NoSec security mode.
41 */
42
43#include "liblwm2m.h"
44
45#include <stdlib.h>
46#include <string.h>
47#include <stdio.h>
48
49
50typedef struct _security_instance_
51{
52 struct _security_instance_ * next; // matches lwm2m_list_t::next
53 uint16_t instanceId; // matches lwm2m_list_t::id
54 char * uri;
55 bool isBootstrap;
56 uint16_t shortID;
57 uint32_t clientHoldOffTime;
58} security_instance_t;
59
60static uint8_t prv_get_value(lwm2m_tlv_t * tlvP,
61 security_instance_t * targetP)
62{
63 // There are no multiple instance ressources
64 tlvP->type = LWM2M_TYPE_RESOURCE;
65
66 switch (tlvP->id)
67 {
68 case LWM2M_SECURITY_URI_ID:
69 tlvP->value = (uint8_t*)targetP->uri;
70 tlvP->length = strlen(targetP->uri);
71 tlvP->flags = LWM2M_TLV_FLAG_STATIC_DATA;
72 tlvP->dataType = LWM2M_TYPE_STRING;
73 return COAP_205_CONTENT;
74
75 case LWM2M_SECURITY_BOOTSTRAP_ID:
76 lwm2m_tlv_encode_bool(targetP->isBootstrap, tlvP);
77 if (0 != tlvP->length) return COAP_205_CONTENT;
78 else return COAP_500_INTERNAL_SERVER_ERROR;
79
80 case LWM2M_SECURITY_SECURITY_ID:
81 lwm2m_tlv_encode_int(LWM2M_SECURITY_MODE_NONE, tlvP);
82 if (0 != tlvP->length) return COAP_205_CONTENT;
83 else return COAP_500_INTERNAL_SERVER_ERROR;
84
85 case LWM2M_SECURITY_PUBLIC_KEY_ID:
86 // Here we return an opaque of 1 byte containing 0
87 tlvP->value = (uint8_t*)"";
88 tlvP->length = 1;
89 tlvP->flags = LWM2M_TLV_FLAG_STATIC_DATA;
90 tlvP->dataType = LWM2M_TYPE_OPAQUE;
91 return COAP_205_CONTENT;
92
93 case LWM2M_SECURITY_SERVER_PUBLIC_KEY_ID:
94 // Here we return an opaque of 1 byte containing 0
95 tlvP->value = (uint8_t*)"";
96 tlvP->length = 1;
97 tlvP->flags = LWM2M_TLV_FLAG_STATIC_DATA;
98 tlvP->dataType = LWM2M_TYPE_OPAQUE;
99 return COAP_205_CONTENT;
100
101 case LWM2M_SECURITY_SECRET_KEY_ID:
102 // Here we return an opaque of 1 byte containing 0
103 tlvP->value = (uint8_t*)"";
104 tlvP->length = 1;
105 tlvP->flags = LWM2M_TLV_FLAG_STATIC_DATA;
106 tlvP->dataType = LWM2M_TYPE_OPAQUE;
107 return COAP_205_CONTENT;
108
109 case LWM2M_SECURITY_SMS_SECURITY_ID:
110 lwm2m_tlv_encode_int(LWM2M_SECURITY_MODE_NONE, tlvP);
111 if (0 != tlvP->length) return COAP_205_CONTENT;
112 else return COAP_500_INTERNAL_SERVER_ERROR;
113
114 case LWM2M_SECURITY_SMS_KEY_PARAM_ID:
115 // Here we return an opaque of 6 bytes containing a buggy value
116 tlvP->value = (uint8_t*)"12345";
117 tlvP->length = 6;
118 tlvP->flags = LWM2M_TLV_FLAG_STATIC_DATA;
119 tlvP->dataType = LWM2M_TYPE_OPAQUE;
120 return COAP_205_CONTENT;
121
122 case LWM2M_SECURITY_SMS_SECRET_KEY_ID:
123 // Here we return an opaque of 32 bytes containing a buggy value
124 tlvP->value = (uint8_t*)"1234567890abcdefghijklmnopqrstu";
125 tlvP->length = 32;
126 tlvP->flags = LWM2M_TLV_FLAG_STATIC_DATA;
127 tlvP->dataType = LWM2M_TYPE_OPAQUE;
128 return COAP_205_CONTENT;
129
130 case LWM2M_SECURITY_SMS_SERVER_NUMBER_ID:
131 lwm2m_tlv_encode_int(0, tlvP);
132 if (0 != tlvP->length) return COAP_205_CONTENT;
133 else return COAP_500_INTERNAL_SERVER_ERROR;
134
135 case LWM2M_SECURITY_SHORT_SERVER_ID:
136 lwm2m_tlv_encode_int(targetP->shortID, tlvP);
137 if (0 != tlvP->length) return COAP_205_CONTENT;
138 else return COAP_500_INTERNAL_SERVER_ERROR;
139
140 case LWM2M_SECURITY_HOLD_OFF_ID:
141 lwm2m_tlv_encode_int(targetP->clientHoldOffTime, tlvP);
142 if (0 != tlvP->length) return COAP_205_CONTENT;
143 else return COAP_500_INTERNAL_SERVER_ERROR;
144
145 default:
146 return COAP_404_NOT_FOUND;
147 }
148}
149
150static uint8_t prv_security_read(uint16_t instanceId,
151 int * numDataP,
152 lwm2m_tlv_t ** dataArrayP,
153 lwm2m_object_t * objectP)
154{
155 security_instance_t * targetP;
156 uint8_t result;
157 int i;
158
159 targetP = (security_instance_t *)lwm2m_list_find(objectP->instanceList, instanceId);
160 if (NULL == targetP) return COAP_404_NOT_FOUND;
161
162 // is the server asking for the full instance ?
163 if (*numDataP == 0)
164 {
165 uint16_t resList[] = {LWM2M_SECURITY_URI_ID,
166 LWM2M_SECURITY_BOOTSTRAP_ID,
167 LWM2M_SECURITY_SECURITY_ID,
168 LWM2M_SECURITY_PUBLIC_KEY_ID,
169 LWM2M_SECURITY_SERVER_PUBLIC_KEY_ID,
170 LWM2M_SECURITY_SECRET_KEY_ID,
171 LWM2M_SECURITY_SMS_SECURITY_ID,
172 LWM2M_SECURITY_SMS_KEY_PARAM_ID,
173 LWM2M_SECURITY_SMS_SECRET_KEY_ID,
174 LWM2M_SECURITY_SMS_SERVER_NUMBER_ID,
175 LWM2M_SECURITY_SHORT_SERVER_ID,
176 LWM2M_SECURITY_HOLD_OFF_ID};
177 int nbRes = sizeof(resList)/sizeof(uint16_t);
178
179 *dataArrayP = lwm2m_tlv_new(nbRes);
180 if (*dataArrayP == NULL) return COAP_500_INTERNAL_SERVER_ERROR;
181 *numDataP = nbRes;
182 for (i = 0 ; i < nbRes ; i++)
183 {
184 (*dataArrayP)[i].id = resList[i];
185 }
186 }
187
188 i = 0;
189 do
190 {
191 result = prv_get_value((*dataArrayP) + i, targetP);
192 i++;
193 } while (i < *numDataP && result == COAP_205_CONTENT);
194
195 return result;
196}
197
198static void prv_security_close(lwm2m_object_t * objectP)
199{
200 while (objectP->instanceList != NULL)
201 {
202 security_instance_t * securityInstance = (security_instance_t *)objectP->instanceList;
203 objectP->instanceList = objectP->instanceList->next;
204 if (NULL != securityInstance->uri)
205 {
206 lwm2m_free(securityInstance->uri);
207 }
208 lwm2m_free(securityInstance);
209 }
210}
211
212lwm2m_object_t * get_security_object()
213{
214 lwm2m_object_t * securityObj;
215
216 securityObj = (lwm2m_object_t *)lwm2m_malloc(sizeof(lwm2m_object_t));
217
218 if (NULL != securityObj)
219 {
220 security_instance_t * targetP;
221
222 memset(securityObj, 0, sizeof(lwm2m_object_t));
223
224 securityObj->objID = 0;
225
226 // Manually create an hardcoded instance
227 targetP = (security_instance_t *)lwm2m_malloc(sizeof(security_instance_t));
228 if (NULL == targetP)
229 {
230 lwm2m_free(securityObj);
231 return NULL;
232 }
233
234 memset(targetP, 0, sizeof(security_instance_t));
235 targetP->instanceId = 0;
236 targetP->uri = strdup("coap://localhost:5683");
237 targetP->isBootstrap = false;
238 targetP->shortID = 123;
239 targetP->clientHoldOffTime = 10;
240
241 securityObj->instanceList = LWM2M_LIST_ADD(securityObj->instanceList, targetP);
242
243 securityObj->readFunc = prv_security_read;
244 securityObj->closeFunc = prv_security_close;
245 }
246
247 return securityObj;
248}
249
250char * get_server_uri(lwm2m_object_t * objectP,
251 uint16_t secObjInstID)
252{
253 security_instance_t * targetP = (security_instance_t *)LWM2M_LIST_FIND(objectP->instanceList, secObjInstID);
254
255 if (NULL != targetP)
256 {
257 return lwm2m_strdup(targetP->uri);
258 }
259
260 return NULL;
261}