38
38
#include "common_functions.h"
39
39
#include "coap_service_api.h"
40
40
#include "thread_meshcop_lib.h"
41
+ #include "randLIB.h"
41
42
#include "6LoWPAN/Thread/thread_common.h"
42
43
#include "6LoWPAN/Thread/thread_bootstrap.h"
43
44
#include "6LoWPAN/Thread/thread_joiner_application.h"
@@ -171,6 +172,25 @@ static int thread_joiner_application_simple_enroll_response_cb(int8_t service_id
171
172
172
173
return 0 ;
173
174
}
175
+
176
+ static int thread_joiner_application_csrattrs_response_cb (int8_t service_id , uint8_t source_address [static 16 ], uint16_t source_port , sn_coap_hdr_s * response_ptr )
177
+ {
178
+ (void ) response_ptr ;
179
+
180
+ protocol_interface_info_entry_t * cur = protocol_stack_interface_info_get_by_id (thread_extension_bootstrap_find_id_by_service (service_id ));
181
+
182
+ if (!cur || !cur -> thread_info ) {
183
+ return -1 ;
184
+ }
185
+ tr_info ("Receiving csrattrs response sending simpleenroll" );
186
+
187
+ // TODO add certificate template to this message with included Private/Public key pair
188
+ coap_service_request_send (service_id , COAP_REQUEST_OPTIONS_SECURE_BYPASS , source_address , source_port ,
189
+ COAP_MSG_TYPE_CONFIRMABLE , COAP_MSG_CODE_REQUEST_POST , ".well-known/est/simpleenroll" , THREAD_CONTENT_FORMAT_PKCS10 , NULL , 0 , thread_joiner_application_simple_enroll_response_cb );
190
+
191
+ return 0 ;
192
+ }
193
+
174
194
static int thread_joiner_application_rat_response_cb (int8_t service_id , uint8_t source_address [static 16 ], uint16_t source_port , sn_coap_hdr_s * response_ptr )
175
195
{
176
196
(void ) response_ptr ;
@@ -180,17 +200,52 @@ static int thread_joiner_application_rat_response_cb(int8_t service_id, uint8_t
180
200
if (!cur || !cur -> thread_info ) {
181
201
return -1 ;
182
202
}
183
- tr_info ("Receiving RAT response sending simpleenroll" );
203
+ tr_info ("Receiving RAT response sending csrattrs request" );
204
+ // TODO Parse CA certificate from RAT response
205
+
206
+ // TODO Verify nonce
184
207
185
208
// TODO add certificate template to this message with included Private/Public key pair
186
209
coap_service_request_send (service_id , COAP_REQUEST_OPTIONS_SECURE_BYPASS , source_address , source_port ,
187
- COAP_MSG_TYPE_CONFIRMABLE , COAP_MSG_CODE_REQUEST_POST , ".well-known/est/simpleenroll " , COAP_CT_OCTET_STREAM , NULL , 0 , thread_joiner_application_simple_enroll_response_cb );
210
+ COAP_MSG_TYPE_CONFIRMABLE , COAP_MSG_CODE_REQUEST_GET , ".well-known/est/csrattrs " , COAP_CT_NONE , NULL , 0 , thread_joiner_application_csrattrs_response_cb );
188
211
189
212
return 0 ;
190
213
}
214
+
215
+ static int thread_joiner_application_rat_request_build (uint8_t * rat_payload , int length )
216
+ {
217
+ uint8_t * ptr = rat_payload ;
218
+
219
+ if (length < 25 ) {
220
+ return 0 ;
221
+ }
222
+
223
+ * rat_payload ++ = 0xa2 ; // map (2)
224
+
225
+ // text (7) "version" + unsigned (1) "1"
226
+ * rat_payload ++ = 0x67 ;
227
+ memcpy (rat_payload , "version" , 7 );
228
+ rat_payload += 7 ;
229
+ * rat_payload ++ = 0x01 ;
230
+
231
+ // text (5) "nonce" + bytes (8) random nonce
232
+ // todo: save nonce to verify response against reply.
233
+ * rat_payload ++ = 0x65 ;
234
+ memcpy (rat_payload , "nonce" , 5 );
235
+ rat_payload += 5 ;
236
+
237
+ * rat_payload ++ = 0x48 ;
238
+ common_write_64_bit (randLIB_get_64bit (), rat_payload );
239
+ rat_payload += 8 ;
240
+
241
+ return rat_payload - ptr ;
242
+ }
243
+
191
244
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 )
192
245
{
193
246
protocol_interface_info_entry_t * cur = protocol_stack_interface_info_get_by_id (interface_id );
247
+ uint8_t rat_payload [25 ];
248
+ int rat_len ;
194
249
195
250
if (!done_cb || !cur ) {
196
251
return -1 ;
@@ -209,9 +264,15 @@ static int thread_joiner_application_ae_commission_start(int8_t interface_id, ui
209
264
memcpy (thread_info (cur )-> extension_credentials_ptr -> ccm_addr , parent_address , 16 );
210
265
thread_info (cur )-> extension_credentials_ptr -> ccm_port = port ;
211
266
267
+ rat_len = thread_joiner_application_rat_request_build (rat_payload , sizeof (rat_payload ));
268
+ if (rat_len == 0 ) {
269
+ tr_debug ("RAT request payload build failed" );
270
+ return -1 ;
271
+ }
272
+
212
273
// todo: This might not be needed if no extra certificate processing made by device and should directly call simpleenroll
213
274
coap_service_request_send (thread_info (cur )-> extension_credentials_ptr -> coap_service_secure_session_id , COAP_REQUEST_OPTIONS_SECURE_BYPASS , parent_address , port ,
214
- COAP_MSG_TYPE_CONFIRMABLE , COAP_MSG_CODE_REQUEST_POST , ".well-known/est/rat" , COAP_CT_OCTET_STREAM , NULL , 0 , thread_joiner_application_rat_response_cb );
275
+ COAP_MSG_TYPE_CONFIRMABLE , COAP_MSG_CODE_REQUEST_POST , ".well-known/est/rat" , THREAD_CONTENT_FORMAT_AUDITNONCE , rat_payload , rat_len , thread_joiner_application_rat_response_cb );
215
276
216
277
return 0 ;
217
278
}
0 commit comments