Skip to content

Commit 2565170

Browse files
author
Mika Tervonen
committed
Add CBOR parsing to simpleenrolment message
1 parent e38c70f commit 2565170

File tree

3 files changed

+45
-13
lines changed

3 files changed

+45
-13
lines changed

source/6LoWPAN/Thread/thread_extension.c

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -789,7 +789,9 @@ static int thread_extension_relay_tx_cb(int8_t service_id, uint8_t source_addres
789789
uint8_t *udp_data_ptr;
790790
uint16_t udp_data_len;
791791
uint8_t *iid_ptr;
792+
uint8_t iid_len;
792793
uint16_t port;
794+
uint8_t port_len;
793795
int8_t socket_id;
794796
(void)source_address;
795797
(void)source_port;
@@ -798,12 +800,12 @@ static int thread_extension_relay_tx_cb(int8_t service_id, uint8_t source_addres
798800
if (!cur) {
799801
return -1;
800802
}
803+
iid_len = thread_meshcop_tlv_find(request_ptr->payload_ptr, request_ptr->payload_len, MESHCOP_TLV_JOINER_IID, &iid_ptr);
804+
port_len = thread_meshcop_tlv_data_get_uint16(request_ptr->payload_ptr, request_ptr->payload_len, MESHCOP_TLV_JOINER_UDP_PORT, &port);
805+
udp_data_len = thread_meshcop_tlv_find(request_ptr->payload_ptr, request_ptr->payload_len, MESHCOP_TLV_JOINER_ENCAPSULATION, &udp_data_ptr);
801806
// unwrap message and send to joiner socket.
802-
if (8 > thread_meshcop_tlv_find(request_ptr->payload_ptr, request_ptr->payload_len, MESHCOP_TLV_JOINER_IID, &iid_ptr) ||
803-
2 > thread_meshcop_tlv_data_get_uint16(request_ptr->payload_ptr, request_ptr->payload_len, MESHCOP_TLV_JOINER_UDP_PORT, &port) ||
804-
0 == (udp_data_len = thread_meshcop_tlv_find(request_ptr->payload_ptr, request_ptr->payload_len, MESHCOP_TLV_JOINER_ENCAPSULATION, &udp_data_ptr))
805-
) {
806-
tr_err("Relay TX invalid message");
807+
if (8 > iid_len || 2 > port_len || 0 == udp_data_len ) {
808+
tr_err("Relay TX invalid message iid:%d, port:%d data_len:%d", iid_len,port_len, udp_data_len);
807809
return -1;
808810
}
809811
if (strncmp(THREAD_URI_BBR_NMK_TX_NTF, (const char *)request_ptr->uri_path_ptr, request_ptr->uri_path_len) == 0) {

source/6LoWPAN/Thread/thread_extension_bootstrap.c

Lines changed: 37 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -185,21 +185,39 @@ static int thread_joiner_application_simple_enroll_response_cb(int8_t service_id
185185
{
186186
(void) source_address;
187187
(void) source_port;
188+
uint8_t *ptr;
189+
uint16_t len, flen;
188190

189191
// re-attach in any case and close the secure connection
190192
thread_extension_bootstrap_network_reattach(service_id, 1000);
191193
coap_service_close_secure_connection(service_id, source_address, source_port);
192194

193195
protocol_interface_info_entry_t *cur = protocol_stack_interface_info_get_by_id(thread_extension_bootstrap_find_id_by_service(service_id));
194196

195-
tr_debug("Simple enrollment received");
197+
tr_debug("Simple enrollment received %s",trace_array(response_ptr->payload_ptr, response_ptr->payload_len));
196198

197199
if (!cur || !cur->thread_info || !response_ptr) {
198200
return -1;
199201
}
202+
ptr = response_ptr->payload_ptr;
203+
len = response_ptr->payload_len;
204+
// CBOR format check
205+
if (*ptr == 0x58) {
206+
flen = *(ptr + 1);
207+
ptr += 2;
208+
len -= 2;
209+
} else if (*ptr == 0x59) {
210+
flen = common_read_16_bit(ptr + 1);
211+
ptr += 3;
212+
len -= 3;
213+
} else {
214+
// no shorter than 23 byte certificates supported
215+
flen = 0;
216+
}
200217

201-
if (0 > thread_extension_bootstrap_network_certificate_set(cur, response_ptr->payload_ptr, response_ptr->payload_len)) {
202-
tr_warn("ae response parse failed");
218+
if ( flen != len ||
219+
0 > thread_extension_bootstrap_network_certificate_set(cur, ptr, len)) {
220+
tr_warn("ae response parse failed, len %d != %d",len,flen);
203221
}
204222

205223
return 0;
@@ -269,16 +287,26 @@ static int thread_joiner_application_rat_response_cb(int8_t service_id, uint8_t
269287
// TODO Verify nonce
270288

271289
coap_service_request_send(service_id, COAP_REQUEST_OPTIONS_SECURE_BYPASS, source_address, source_port,
272-
COAP_MSG_TYPE_CONFIRMABLE, COAP_MSG_CODE_REQUEST_GET, ".well-known/est/csrattrs", COAP_CT_NONE, NULL, 0, thread_joiner_application_csrattrs_response_cb);
290+
COAP_MSG_TYPE_CONFIRMABLE, COAP_MSG_CODE_REQUEST_GET, ".well-known/est/csrattrs", THREAD_CONTENT_FORMAT_CSRATTRS, NULL, 0, thread_joiner_application_csrattrs_response_cb);
273291

274292
return 0;
275293
}
276-
294+
/*A2 # map(2)
295+
67 # text(7)
296+
76657273696F6E # "version"
297+
61 # text(1)
298+
31 # "1"
299+
65 # text(5)
300+
6E6F6E6365 # "nonce"
301+
48 # bytes(8)
302+
13ADD904605D973E # "\x13\xAD\xD9\x04`]\x97>"
303+
*
304+
*/
277305
static int thread_joiner_application_rat_request_build(uint8_t *rat_payload, int length)
278306
{
279307
uint8_t *ptr = rat_payload;
280308

281-
if (length < 25) {
309+
if (length < 30) {
282310
return 0;
283311
}
284312

@@ -288,7 +316,8 @@ static int thread_joiner_application_rat_request_build(uint8_t *rat_payload, int
288316
*rat_payload++ = 0x67;
289317
memcpy(rat_payload, "version", 7);
290318
rat_payload += 7;
291-
*rat_payload++ = 0x01;
319+
*rat_payload++ = 0x61;
320+
*rat_payload++ = 0x31;
292321

293322
// text (5) "nonce" + bytes (8) random nonce
294323
// todo: save nonce to verify response against reply.
@@ -306,7 +335,7 @@ static int thread_joiner_application_rat_request_build(uint8_t *rat_payload, int
306335
static int thread_joiner_application_ae_commission_start(int8_t interface_id, uint8_t parent_address[16], uint16_t port, thread_joiner_application_commission_done_cb *done_cb)
307336
{
308337
protocol_interface_info_entry_t *cur = protocol_stack_interface_info_get_by_id(interface_id);
309-
uint8_t rat_payload[25];
338+
uint8_t rat_payload[30];
310339
int rat_len;
311340

312341
if (!done_cb || !cur) {

source/6LoWPAN/Thread/thread_extension_constants.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@ typedef struct discovery_additional_info {
109109
#define TMFCOP_TLV_COMMISSIONER_SESSION_ID 15
110110

111111
#define THREAD_CONTENT_FORMAT_AUDITNONCE (sn_coap_content_format_e)65000
112+
#define THREAD_CONTENT_FORMAT_CSRATTRS (sn_coap_content_format_e)65002
112113
#define THREAD_CONTENT_FORMAT_PKCS10 (sn_coap_content_format_e)65003
113114

114115
#define THREAD_VERSION_1_2 3

0 commit comments

Comments
 (0)