-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathoc_oscore_engine.c
1129 lines (973 loc) · 41.7 KB
/
oc_oscore_engine.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
// Copyright (c) 2020 Intel Corporation
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
*/
#if defined(OC_OSCORE)
#include "api/oc_events.h"
#include "mbedtls/ccm.h"
#include "messaging/coap/coap_signal.h"
#include "messaging/coap/engine.h"
#include "messaging/coap/transactions.h"
#include "oc_api.h"
#include "oc_client_state.h"
#include "oc_oscore.h"
#include "oc_oscore_context.h"
#include "oc_oscore_crypto.h"
//#include "oc_pstat.h"
//#include "oc_store.h"
#include "oc_tls.h"
#include "util/oc_process.h"
#include "oc_knx.h"
#include "api/oc_knx_sec.h"
OC_PROCESS(oc_oscore_handler, "OSCORE Process");
static bool g_ssn_in_use = false;
static uint64_t g_ssn = 0;
void
oc_oscore_set_next_ssn(uint64_t ssn)
{
g_ssn = ssn;
g_ssn_in_use = true;
}
uint64_t
oc_oscore_get_next_ssn()
{
return g_ssn;
}
bool
oc_oscore_is_g_ssn_in_use()
{
return g_ssn_in_use;
}
static void
increment_ssn_in_context(oc_oscore_context_t *ctx)
{
ctx->ssn++;
/* Store current SSN with frequency OSCORE_WRITE_FREQ_K
* Based on recommendations in RFC 8613, Appendix B.1. to prevent SSN reuse
*/
if (ctx->ssn % OSCORE_SSN_WRITE_FREQ_K == 0) {
// save ssn to persistent memory, using kid as part of the key
uint8_t key_buf[OSCORE_STORAGE_KEY_LEN];
memcpy(key_buf, OSCORE_STORAGE_PREFIX, OSCORE_STORAGE_PREFIX_LEN);
memcpy(key_buf + OSCORE_STORAGE_PREFIX_LEN, ctx->sendid, ctx->sendid_len);
key_buf[OSCORE_STORAGE_KEY_LEN - 1] = '\0';
#ifdef OC_USE_STORAGE
oc_storage_write(key_buf, (uint8_t *)&ctx->ssn, sizeof(ctx->ssn));
#endif
}
}
static oc_event_callback_retval_t
dump_cred(void *data)
{
(void)data;
// size_t device = (size_t)data;
// oc_sec_dump_cred(device);
return OC_EVENT_DONE;
}
static int
oc_oscore_recv_message(oc_message_t *message)
{
/* OSCORE layer receive path pseudocode
* ------------------------------------
* If incoming oc_message_t is an OSCORE message:
* OSCORE context = nil
* Parse OSCORE message
* If parse unsuccessful:
* Discard message and return error
* If packet is a request and is received over UDP:
* Check if packet is duplicate by mid; if so, discard
* If received kid param:
* Search for OSCORE context by kid
* Else:
* If message is response:
* Search for OSCORE context by token
* Else:
* OSCORE request lacks kid param; Return error
* If OSCORE context is nil, return error
* If unicast message protected using group OSCORE context, silently ignore
* Copy subjectuuid of OSCORE cred entry into oc_message_t->endpoint
* Set context->recvkey as the decryption key
* If received partial IV:
* If message is request:
* Check if replayed request and discard
* Compose AAD using received piv and context->recvid
* Copy received piv into oc_message_t->endpoint
* Compute nonce using received piv and context->recvid
* If message is response:
* if oc_message_t->endpoint.piv is 0:
* Copy request_piv from client cb/transaction into
oc_message_t->endpoint
* Compute nonce using request_piv and sendid
* Compose AAD using request_piv and sendid
* Decrypt OSCORE payload
* Parse inner/protected CoAP options/payload
* If non-UPDATE mcast message protected using OSCORE group context,
silently ignore
* Copy fields: type, version, mid, token, observe from the OSCORE packet to
* CoAP Packet
* Serialize full CoAP packet to oc_message
* Dispatch oc_message_t to the CoAP layer
*/
if (oscore_is_oscore_message(message) >= 0) {
OC_DBG_OSCORE("#################################: found OSCORE header");
oc_oscore_context_t *oscore_ctx = NULL;
// message->endpoint.flags |= SECURED;
message->endpoint.flags += OSCORE;
uint8_t *key = NULL;
int key_len = 0;
coap_packet_t oscore_pkt[1];
uint8_t AAD[OSCORE_AAD_MAX_LEN], AAD_len = 0, nonce[OSCORE_AEAD_NONCE_LEN];
/* Parse OSCORE message */
OC_DBG_OSCORE("### parse OSCORE message ###");
coap_status_t st = oscore_parse_outer_message(message, oscore_pkt);
if (st != COAP_NO_ERROR) {
OC_ERR("***error parsing OSCORE message***");
oscore_send_error(oscore_pkt, BAD_OPTION_4_02, &message->endpoint);
goto oscore_recv_error;
}
OC_DBG_OSCORE("### parsed OSCORE message ###");
if (oscore_pkt->transport_type == COAP_TRANSPORT_UDP &&
oscore_pkt->code <= OC_FETCH) {
if (oc_coap_check_if_duplicate(oscore_pkt->mid, message->endpoint.device,
message->endpoint.addr.ipv6.port,
message->endpoint.addr.ipv6.address)) {
OC_DBG("dropping duplicate request");
goto oscore_recv_error;
}
}
// needed for encrypting final ack of separate response
/*
if (oscore_pkt->code >= OC_GET && oscore_pkt->code <= OC_DELETE)
message->endpoint.rx_msg_is_response = false;
else
message->endpoint.rx_msg_is_response = true;
*/
uint8_t *request_piv = NULL, request_piv_len = 0;
/* If OSCORE packet contains kid... */
if (oscore_pkt->kid_len > 0) {
/* Search for OSCORE context by kid */
OC_DBG_OSCORE("--- got kid from incoming message");
OC_LOGbytes(oscore_pkt->kid, oscore_pkt->kid_len);
OC_DBG_OSCORE("### searching for OSCORE context by kid ###");
oscore_ctx = oc_oscore_find_context_by_kid_idctx(
oscore_ctx, message->endpoint.device, oscore_pkt->kid,
oscore_pkt->kid_len, oscore_pkt->kid_ctx, oscore_pkt->kid_ctx_len);
if (!oscore_ctx) {
// we do not have a cached context, so we have to make one
// find auth/at entry with corresponding kid
int idx = oc_core_find_at_entry_with_osc_id(0, oscore_pkt->kid,
oscore_pkt->kid_len);
if (idx == -1) {
OC_ERR("***Could not find Access Token matching KID, returning "
"UNAUTHORIZED***");
oscore_send_error(oscore_pkt, UNAUTHORIZED_4_01, &message->endpoint);
goto oscore_recv_error;
}
oc_auth_at_t *at_entry = oc_get_auth_at_entry(0, idx);
// create oscore recipient context from that entry
oscore_ctx = oc_oscore_add_context(
0, oc_string(at_entry->osc_rid), /* sender id (empty string) */
oc_byte_string_len(
at_entry->osc_rid), /* sender id len (ought to be 0)*/
oc_string(at_entry->osc_id), /* recipient id */
oc_byte_string_len(at_entry->osc_id), /* recipient id len */
0, "desc", oc_string(at_entry->osc_ms),
oc_byte_string_len(at_entry->osc_ms), oscore_pkt->kid_ctx,
oscore_pkt->kid_ctx_len, idx, false);
// if context is null, free one & try adding again
if (!oscore_ctx) {
oc_oscore_free_lru_recipient_context();
oscore_ctx = oc_oscore_add_context(
0, oc_string(at_entry->osc_rid), /* sender id (empty string) */
oc_byte_string_len(
at_entry->osc_rid), /* sender id len (ought to be 0)*/
oc_string(at_entry->osc_id), /* recipient id */
oc_byte_string_len(at_entry->osc_id), /* recipient id len */
0, "desc", oc_string(at_entry->osc_ms),
oc_byte_string_len(at_entry->osc_ms), oscore_pkt->kid_ctx,
oscore_pkt->kid_ctx_len, idx, false);
if (!oscore_ctx) {
OC_ERR("***Could not create oscore recipient context!***");
oscore_send_error(oscore_pkt, UNAUTHORIZED_4_01,
&message->endpoint);
goto oscore_recv_error;
}
}
}
} else {
/* If message is response */
if (oscore_pkt->code > OC_FETCH) {
/* Search for OSCORE context by token */
OC_DBG_OSCORE("### searching for OSCORE context by token ###");
oscore_ctx = oc_oscore_find_context_by_token_mid(
message->endpoint.device, oscore_pkt->token, oscore_pkt->token_len,
oscore_pkt->mid, &request_piv, &request_piv_len,
message->endpoint.flags & TCP);
} else {
/* OSCORE message is request and lacks kid, return error */
OC_ERR("***OSCORE protected request lacks kid param***");
oscore_send_error(oscore_pkt, BAD_OPTION_4_02, &message->endpoint);
goto oscore_recv_error;
}
}
if (!oscore_ctx) {
OC_ERR(
"***could not find matching OSCORE context, returning UNAUTHORIZED***");
oscore_send_error(oscore_pkt, UNAUTHORIZED_4_01, &message->endpoint);
goto oscore_recv_error;
}
// copy the serial number as return token, so that the reply can find
// the context again.
OC_DBG_OSCORE(
"--- setting endpoint serial number with found token & index");
// oc_endpoint_set_serial_number(&message->endpoint,
// (char *)oscore_ctx->token_id);
oc_endpoint_set_auth_at_index(&message->endpoint,
(int32_t)oscore_ctx->auth_at_index);
// oc_string_copy_from_char(&message->endpoint.serial_number,
// (char *)oscore_ctx->token_id);
oc_endpoint_set_oscore_id(&message->endpoint, oscore_ctx->token_id,
SERIAL_NUM_SIZE);
// PRINT("using send key!!\n");
// key = oscore_ctx->sendkey;
/* Use recipient key for decryption */
// if (key == NULL) {
// PRINT("using receive key!!\n");
key = oscore_ctx->recvkey;
//}
/* If received Partial IV in message */
if (oscore_pkt->piv_len > 0) {
/* If message is request */
if (oscore_pkt->code >= OC_GET && oscore_pkt->code <= OC_FETCH) {
uint64_t piv = 0;
oscore_read_piv(oscore_pkt->piv, oscore_pkt->piv_len, &piv);
/* Compose AAD using received piv and context->recvid */
oc_oscore_compose_AAD(oscore_ctx->recvid, oscore_ctx->recvid_len,
oscore_pkt->piv, oscore_pkt->piv_len, AAD,
&AAD_len);
OC_DBG_OSCORE(
"---composed AAD using received Partial IV and Recipient ID");
OC_LOGbytes_OSCORE(AAD, AAD_len);
}
OC_DBG_OSCORE("---got Partial IV from incoming message");
OC_LOGbytes_OSCORE(oscore_pkt->piv, oscore_pkt->piv_len);
/* Copy received piv into oc_message_t->endpoint for requests */
if (oscore_pkt->code >= OC_GET && oscore_pkt->code <= OC_DELETE) {
memcpy(message->endpoint.request_piv, oscore_pkt->piv,
oscore_pkt->piv_len);
message->endpoint.request_piv_len = oscore_pkt->piv_len;
OC_DBG_OSCORE("--- Caching PIV for later use...");
}
/* Compute nonce using received piv and context->recvid */
oc_oscore_AEAD_nonce(oscore_ctx->recvid, oscore_ctx->recvid_len,
oscore_pkt->piv, oscore_pkt->piv_len,
oscore_ctx->commoniv, nonce, OSCORE_AEAD_NONCE_LEN);
OC_DBG_OSCORE(
"---computed AEAD nonce using received Partial IV and Recipient ID");
OC_LOGbytes_OSCORE(nonce, OSCORE_AEAD_NONCE_LEN);
}
/* If message is response */
if (oscore_pkt->code > OC_FETCH) {
OC_DBG_OSCORE("---got request_piv from client callback");
OC_LOGbytes_OSCORE(request_piv, request_piv_len);
// the final ack of a separate response sequence is sent unencrypted
// if the request_piv_length in the endpoint is 0. So, we cannot copy
// it here in this case.
/*
if (message->endpoint.request_piv_len == 0)
{
// Copy request_piv from client cb/transaction into
// oc_message_t->endpoint
memcpy(message->endpoint.request_piv, request_piv, request_piv_len);
message->endpoint.request_piv_len = request_piv_len;
}
*/
if (oscore_pkt->piv_len == 0) {
/* Compute nonce using request_piv and context->sendid */
oc_oscore_AEAD_nonce(oscore_ctx->sendid, oscore_ctx->sendid_len,
request_piv, request_piv_len, oscore_ctx->commoniv,
nonce, OSCORE_AEAD_NONCE_LEN);
OC_DBG_OSCORE("---use AEAD nonce from request");
OC_LOGbytes_OSCORE(nonce, OSCORE_AEAD_NONCE_LEN);
}
/* Compose AAD using request_piv and context->sendid */
oc_oscore_compose_AAD(oscore_ctx->sendid, oscore_ctx->sendid_len,
request_piv, request_piv_len, AAD, &AAD_len);
OC_DBG_OSCORE("---composed AAD using request_piv and Sender ID");
OC_LOGbytes_OSCORE(AAD, AAD_len);
}
OC_DBG_OSCORE("### decrypting OSCORE payload ###");
/* Verify and decrypt OSCORE payload */
uint8_t *output = (uint8_t *)malloc(oscore_pkt->payload_len);
// int ret = oc_oscore_decrypt(oscore_pkt->payload, oscore_pkt->payload_len,
// OSCORE_AEAD_TAG_LEN, key, OSCORE_KEY_LEN,
// nonce, OSCORE_AEAD_NONCE_LEN, AAD, AAD_len,
// oscore_pkt->payload);
int ret = oc_oscore_decrypt(oscore_pkt->payload, oscore_pkt->payload_len,
OSCORE_AEAD_TAG_LEN, key, OSCORE_KEY_LEN, nonce,
OSCORE_AEAD_NONCE_LEN, AAD, AAD_len, output);
memcpy(oscore_pkt->payload, output, oscore_pkt->payload_len);
free(output);
if (ret != 0) {
OC_ERR("***error decrypting/verifying response : (%d)***", ret);
oscore_send_error(oscore_pkt, BAD_REQUEST_4_00, &message->endpoint);
goto oscore_recv_error;
}
OC_DBG_OSCORE("### successfully decrypted OSCORE payload ###");
/* Adjust payload length to size after decryption (i.e. exclude the tag)
*/
oscore_pkt->payload_len -= OSCORE_AEAD_TAG_LEN;
coap_packet_t coap_pkt[1];
OC_DBG_OSCORE("### parse inner message ###");
/* Parse inner (CoAP) message from the decrypted COSE payload */
st = oscore_parse_inner_message(oscore_pkt->payload,
oscore_pkt->payload_len, &coap_pkt);
if (st != COAP_NO_ERROR) {
OC_ERR("***error parsing inner message***");
oscore_send_error(oscore_pkt, BAD_OPTION_4_02, &message->endpoint);
goto oscore_recv_error;
}
OC_DBG_OSCORE("### successfully parsed inner message ###");
// if (c->credtype == OC_CREDTYPE_OSCORE_MCAST_SERVER &&
// coap_pkt->code != OC_POST) {
// OC_ERR("***non-UPDATE multicast request protected using group OSCORE "
// "context; silently ignore***");
// goto oscore_recv_error;
//}
/* Copy type, version, mid, token, observe fields from OSCORE packet to
* CoAP Packet */
coap_pkt->transport_type = oscore_pkt->transport_type;
coap_pkt->version = oscore_pkt->version;
coap_pkt->type = oscore_pkt->type;
coap_pkt->mid = oscore_pkt->mid;
memcpy(coap_pkt->token, oscore_pkt->token, oscore_pkt->token_len);
coap_pkt->token_len = oscore_pkt->token_len;
coap_pkt->observe = oscore_pkt->observe;
/* Also copy kid, kid_ctx and ssn, for replay protection */
message->endpoint.kid_len = oscore_pkt->kid_len;
memcpy(message->endpoint.kid, oscore_pkt->kid, oscore_pkt->kid_len);
message->endpoint.kid_ctx_len = oscore_pkt->kid_ctx_len;
memcpy(message->endpoint.kid_ctx, oscore_pkt->kid_ctx,
oscore_pkt->kid_ctx_len);
OC_DBG_OSCORE("### serializing CoAP message ###");
/* Serialize fully decrypted CoAP packet to message->data buffer */
message->length = coap_oscore_serialize_message(
(void *)coap_pkt, message->data, true, true, true);
OC_DBG_OSCORE("### setting OSCORE and OSCORE_DECRYPTED ###");
/* set the oscore encryption and decryption flags*/
message->endpoint.flags = message->endpoint.flags | OSCORE_DECRYPTED;
message->endpoint.flags = message->endpoint.flags | OSCORE;
message->endpoint.flags = message->endpoint.flags | IPV6;
PRINTipaddr_flags(message->endpoint);
OC_DBG_OSCORE(
"### serialized decrypted CoAP message to dispatch to the CoAP "
"layer ###");
}
OC_DBG_OSCORE("#################################");
/* Dispatch oc_message_t to the CoAP layer */
if (oc_process_post(&coap_engine, oc_events[INBOUND_RI_EVENT], message) ==
OC_PROCESS_ERR_FULL) {
goto oscore_recv_error;
}
return 0;
oscore_recv_error:
oc_message_unref(message);
return -1;
}
#ifdef OC_CLIENT
static int
oc_oscore_send_multicast_message(oc_message_t *message)
{
/* OSCORE layer secure multicast pseudocode
* ----------------------------------------
* Search for group OSCORE context
* If found OSCORE context:
* Set context->sendkey as the encryption key
* Parse CoAP message
* If parse unsuccessful, return error
* Use context->SSN as partial IV
* Use context-sendid as kid
* Compute nonce using partial IV and context->sendid
* Compute AAD using partial IV and context->sendid
* Make room for inner options and payload by moving CoAP payload to offset
* 2 * COAP_MAX_HEADER_SIZE
* Serialize OSCORE plain text at offset COAP_MAX_HEADER_SIZE
* Encrypt OSCORE plain text at offset COAP_MAX_HEADER_SIZE
* Set OSCORE packet payload to location COAP_MAX_HEADER_SIZE
* Set OSCORE packet payload length to the plain text size + tag length (8)
* Set OSCORE option in OSCORE packet
* Serialize OSCORE message to oc_message_t
* Dispatch oc_message_t to IP layer
*/
uint32_t group_address = 0;
group_address = message->endpoint.group_address;
if (group_address == 0) {
OC_ERR("group_address id == 0");
goto oscore_group_send_error;
}
oc_oscore_context_t *oscore_ctx =
oc_oscore_find_context_by_group_address(0, group_address);
PRINT("oc_oscore_send_multicast_message : group_address = %u\n",
group_address);
if (oscore_ctx) {
OC_DBG_OSCORE("#################################");
OC_DBG_OSCORE("found group OSCORE context %s",
oc_string_checked(oscore_ctx->desc));
/* Use sender key for encryption */
uint8_t *key = oscore_ctx->sendkey;
OC_DBG_OSCORE("### parse CoAP message ###");
/* Parse CoAP message */
coap_packet_t coap_pkt[1];
coap_status_t code = 0;
code = coap_udp_parse_message(coap_pkt, message->data,
(uint16_t)message->length);
if (code != COAP_NO_ERROR) {
OC_ERR("***error parsing CoAP packet***");
goto oscore_group_send_error;
}
OC_DBG_OSCORE("### parsed CoAP message ###");
uint8_t piv[OSCORE_PIV_LEN], piv_len = 0, kid[OSCORE_CTXID_LEN],
kid_len = 0, nonce[OSCORE_AEAD_NONCE_LEN],
AAD[OSCORE_AAD_MAX_LEN], AAD_len = 0;
OC_DBG_OSCORE("### protecting multicast request ###");
if (g_ssn_in_use) {
oscore_ctx->ssn = g_ssn;
g_ssn_in_use = false;
}
/* Use context->SSN as Partial IV */
oscore_store_piv(oscore_ctx->ssn, piv, &piv_len);
// OC_DBG_OSCORE("---using SSN as Partial IV: %lu", oscore_ctx->ssn);
OC_LOGbytes_OSCORE(piv, piv_len);
/* Increment SSN */
// oscore_ctx->ssn++;
increment_ssn_in_context(oscore_ctx);
/* Use context-sendid as kid */
memcpy(kid, oscore_ctx->sendid, oscore_ctx->sendid_len);
kid_len = oscore_ctx->sendid_len;
/* Compute nonce using partial IV and context->sendid */
oc_oscore_AEAD_nonce(oscore_ctx->sendid, oscore_ctx->sendid_len, piv,
piv_len, oscore_ctx->commoniv, nonce,
OSCORE_AEAD_NONCE_LEN);
OC_DBG_OSCORE(
"---computed AEAD nonce using Partial IV (SSN) and Sender ID");
OC_LOGbytes_OSCORE(nonce, OSCORE_AEAD_NONCE_LEN);
/* Compose AAD using partial IV and context->sendid */
oc_oscore_compose_AAD(oscore_ctx->sendid, oscore_ctx->sendid_len, piv,
piv_len, AAD, &AAD_len);
OC_DBG_OSCORE("---composed AAD using Partial IV (SSN) and Sender ID");
OC_LOGbytes_OSCORE(AAD, AAD_len);
/* Move CoAP payload to offset 2*COAP_MAX_HEADER_SIZE to accommodate for
Outer+Inner CoAP options in the OSCORE packet.
*/
if (coap_pkt->payload_len > 0) {
memmove(message->data + 2 * COAP_MAX_HEADER_SIZE, coap_pkt->payload,
coap_pkt->payload_len);
/* Store the new payload location in the CoAP packet */
coap_pkt->payload = message->data + 2 * COAP_MAX_HEADER_SIZE;
}
OC_DBG_OSCORE("### serializing OSCORE plaintext ###");
/* Serialize OSCORE plain text at offset COAP_MAX_HEADER_SIZE
(code, inner options, payload)
*/
size_t plaintext_size = oscore_serialize_plaintext(
coap_pkt, message->data + COAP_MAX_HEADER_SIZE);
OC_DBG_OSCORE("### serialized OSCORE plaintext: %zd bytes ###",
plaintext_size);
/* Set the OSCORE packet payload to point to location of the serialized
inner message.
*/
coap_pkt->payload = message->data + COAP_MAX_HEADER_SIZE;
coap_pkt->payload_len = plaintext_size;
/* Encrypt OSCORE plaintext */
OC_DBG_OSCORE("### encrypting OSCORE plaintext ###");
int ret =
oc_oscore_encrypt(coap_pkt->payload, coap_pkt->payload_len,
OSCORE_AEAD_TAG_LEN, key, OSCORE_KEY_LEN, nonce,
OSCORE_AEAD_NONCE_LEN, AAD, AAD_len, coap_pkt->payload);
if (ret != 0) {
OC_ERR("***error encrypting OSCORE plaintext***");
goto oscore_group_send_error;
}
OC_DBG_OSCORE("### successfully encrypted OSCORE plaintext ###");
/* Adjust payload length to include the size of the authentication tag */
coap_pkt->payload_len += OSCORE_AEAD_TAG_LEN;
/* Set the Outer code for the OSCORE packet (POST/FETCH:2.04/2.05) */
coap_pkt->code = OC_POST;
/* Wireshark fix - include the context ID on the wire as well */
/* otherwise cannot decode OSCORE messages that use implicit ID contexts */
uint8_t idctx[16], idctx_len;
memcpy(idctx, oscore_ctx->idctx, oscore_ctx->idctx_len);
idctx_len = oscore_ctx->idctx_len;
/* Set the OSCORE option */
coap_set_header_oscore(coap_pkt, piv, piv_len, kid, kid_len, idctx,
idctx_len);
/* Serialize OSCORE message to oc_message_t */
OC_DBG_OSCORE("### serializing OSCORE message ###");
message->length = oscore_serialize_message(coap_pkt, message->data);
OC_DBG_OSCORE("### serialized OSCORE message ###");
} else {
OC_ERR("*** could not find group OSCORE context ***");
goto oscore_group_send_error;
}
OC_DBG_OSCORE("#################################");
/* Dispatch oc_message_t to the IP layer */
OC_DBG_OSCORE("Outbound network event: forwarding to IP Connectivity layer");
oc_send_discovery_request(message);
oc_message_unref(message);
return 0;
oscore_group_send_error:
OC_ERR("received malformed CoAP packet from stack");
oc_message_unref(message);
return -1;
}
#endif /* OC_CLIENT */
static int
oc_oscore_send_message(oc_message_t *msg)
{
/* OSCORE layer sending path pseudocode
* ------------------------------------
* Search for OSCORE context by peer UUID
* If found OSCORE context:
* Set context->sendkey as the encryption key
* Clone incoming oc_message_t (*msg) from CoAP layer
* Parse CoAP message
* If parse unsuccessful, return error
* If CoAP message is request:
* Search for client cb by request token
* If found client cb:
* Use context->SSN as partial IV
* Use context-sendid as kid
* Copy partial IV into client cb
* Compute nonce using partial IV and context->sendid
* Compute AAD using partial IV and context->sendid
* Copy partial IV into incoming oc_message_t (*msg), if valid
* Else:
* Return error
* Else: (CoAP message is response)
* Use context->SSN as partial IV
* Compute nonce using partial IV and context->sendid
* Compute AAD using request_piv and context->recvid
* Copy partial IV into incoming oc_message_t (*msg), if valid
* Make room for inner options and payload by moving CoAP payload to offset
* 2 * COAP_MAX_HEADER_SIZE
* Store Observe option; if message is a notification, make Observe option
* value empty
* Serialize OSCORE plaintext at offset COAP_MAX_HEADER_SIZE
* Encrypt OSCORE plaintext at offset COAP_MAX_HEADER_SIZE
* Set OSCORE packet payload to location COAP_MAX_HEADER_SIZE
* Set OSCORE packet payload length to the plain text size + tag length (8)
* Set OSCORE option in OSCORE packet
* Reflect the Observe option (if present in the CoAP packet)
* Set the Outer code for the OSCORE packet (POST/FETCH:2.04/2.05)
* Serialize OSCORE message to oc_message_t
* Dispatch oc_message_t to the (TLS or) network layer
*/
oc_message_t *message = msg;
/* Is this is an inadvertent response to a secure multi cast message */
if (msg->endpoint.flags & MULTICAST) {
OC_DBG_OSCORE(
"### secure multicast requests do not elicit a response, discard "
"###");
oc_message_unref(msg);
return 0;
}
oc_oscore_context_t *oscore_ctx = NULL;
// most common case for unicast: we just get the cached index
int index = message->endpoint.auth_at_index - 1;
// get auth_at table entry at index
oc_auth_at_t *entry = oc_get_auth_at_entry(message->endpoint.device, index);
// if found, get the corresponding context
if (entry) {
OC_DBG_OSCORE("### Found auth at entry, getting context ###");
oscore_ctx = oc_oscore_find_context_by_kid(
NULL, message->endpoint.device, oc_string(entry->osc_id),
oc_byte_string_len(entry->osc_id));
}
// Search for OSCORE context using addressing information
PRINT("oc_oscore_send_message : SID ");
oc_char_println_hex(message->endpoint.oscore_id,
message->endpoint.oscore_id_len);
if (oscore_ctx == NULL) {
// search the oscore id, e.g. the SID
oscore_ctx = oc_oscore_find_context_by_oscore_id(
message->endpoint.device, message->endpoint.oscore_id,
message->endpoint.oscore_id_len);
}
// Search for OSCORE context using addressing information
if (oscore_ctx == NULL) {
oscore_ctx = oc_oscore_find_context_by_group_address(
message->endpoint.device, message->endpoint.group_address);
}
/* Clone incoming oc_message_t (*msg) from CoAP layer */
message = oc_internal_allocate_outgoing_message();
if (message == NULL) {
OC_ERR("***No memory to allocate outgoing message!***");
goto oscore_send_error;
}
message->length = msg->length;
memcpy(message->data, msg->data, msg->length);
memcpy(&message->endpoint, &msg->endpoint, sizeof(oc_endpoint_t));
bool msg_valid = false;
if (msg->ref_count > 1) {
msg_valid = true;
}
oc_message_unref(msg);
OC_DBG_OSCORE("### parse CoAP message ###");
/* Parse CoAP message */
coap_packet_t coap_pkt[1];
coap_status_t code = 0;
#ifdef OC_TCP
if (message->endpoint.flags & TCP) {
code = coap_tcp_parse_message(coap_pkt, message->data,
(uint32_t)message->length);
} else
#endif /* OC_TCP */
{
code = coap_udp_parse_message(coap_pkt, message->data,
(uint16_t)message->length);
}
if (code != COAP_NO_ERROR) {
OC_ERR("***error parsing CoAP packet***");
goto oscore_send_error;
}
OC_DBG_OSCORE("### parsed CoAP message ###");
// Search for context using transaction data
if (oscore_ctx == NULL) {
oscore_ctx = oc_oscore_find_context_by_token_mid(
message->endpoint.device, coap_pkt->token, coap_pkt->token_len,
coap_pkt->mid, NULL, NULL, false);
}
// We haven't found a context, so we free the message we just created
if (oscore_ctx == NULL) {
oc_message_unref(message);
OC_ERR("oc_oscore_send_message: No OSCORE context found. ERROR");
goto oscore_send_error;
}
if (oscore_ctx) {
OC_DBG_OSCORE("#################################");
OC_DBG_OSCORE("found OSCORE context corresponding to the peer serial "
"number or group_address id=%s",
oscore_ctx->token_id);
/* Use sender key for encryption */
uint8_t *key = oscore_ctx->sendkey;
uint8_t piv[OSCORE_PIV_LEN],
piv_len = 0, kid[OSCORE_CTXID_LEN], kid_len = 0, ctx_id[OSCORE_IDCTX_LEN],
ctx_id_len = 0, nonce[OSCORE_AEAD_NONCE_LEN], AAD[OSCORE_AAD_MAX_LEN],
AAD_len = 0;
/* If CoAP message is request */
if ((coap_pkt->code >= OC_GET && coap_pkt->code <= OC_DELETE)
#ifdef OC_TCP
|| coap_pkt->code == PING_7_02 || coap_pkt->code == ABORT_7_05 ||
coap_pkt->code == CSM_7_01
#endif /* OC_TCP */
) {
OC_DBG_OSCORE("### protecting outgoing request ###");
if (g_ssn_in_use) {
oscore_ctx->ssn = g_ssn;
g_ssn_in_use = false;
}
/* Request */
/* Use context->SSN as Partial IV */
oscore_store_piv(oscore_ctx->ssn, piv, &piv_len);
// oscore_store_piv(0, piv, &piv_len);
// OC_DBG_OSCORE("---using SSN as Partial IV: %lu", oscore_ctx->ssn);
OC_LOGbytes_OSCORE(piv, piv_len);
/* Increment SSN for the original request, retransmissions use the same
* SSN */
coap_transaction_t *transaction =
coap_get_transaction_by_token(coap_pkt->token, coap_pkt->token_len);
if (transaction && transaction->retrans_counter == 0)
increment_ssn_in_context(oscore_ctx);
else if (!transaction)
increment_ssn_in_context(oscore_ctx);
#ifdef OC_CLIENT
if (coap_pkt->code >= OC_GET && coap_pkt->code <= OC_DELETE) {
/* Find client cb for the request */
oc_client_cb_t *cb =
oc_ri_find_client_cb_by_token(coap_pkt->token, coap_pkt->token_len);
if (!cb) {
OC_ERR("**could not find client callback corresponding to request**");
goto oscore_send_error;
}
/* Copy partial IV into client cb */
memcpy(cb->piv, piv, piv_len);
cb->piv_len = piv_len;
}
#endif /* OC_CLIENT */
/* Use context-sendid as kid */
memcpy(kid, oscore_ctx->sendid, oscore_ctx->sendid_len);
kid_len = oscore_ctx->sendid_len;
/* use idctx as context_id */
memcpy(ctx_id, oscore_ctx->idctx, oscore_ctx->idctx_len);
ctx_id_len = oscore_ctx->idctx_len;
/* Compute nonce using partial IV and context->sendid */
oc_oscore_AEAD_nonce(oscore_ctx->sendid, oscore_ctx->sendid_len, piv,
piv_len, oscore_ctx->commoniv, nonce,
OSCORE_AEAD_NONCE_LEN);
OC_DBG_OSCORE(
"---computed AEAD nonce using Partial IV (SSN) and Sender ID");
OC_LOGbytes_OSCORE(nonce, OSCORE_AEAD_NONCE_LEN);
OC_DBG_OSCORE("---");
/* Compose AAD using partial IV and context->sendid */
oc_oscore_compose_AAD(oscore_ctx->sendid, oscore_ctx->sendid_len, piv,
piv_len, AAD, &AAD_len);
OC_DBG_OSCORE("---composed AAD using Partial IV (SSN) and Sender ID");
OC_LOGbytes_OSCORE(AAD, AAD_len);
OC_DBG_OSCORE("---");
/* Copy partial IV into incoming oc_message_t (*msg), if valid */
if (msg_valid) {
memcpy(msg->endpoint.request_piv, piv, piv_len);
msg->endpoint.request_piv_len = piv_len;
}
} else {
/* We are dealing with a response */
/* Request was not protected by OSCORE */
if (message->endpoint.request_piv_len == 0) {
OC_DBG("request was not protected by OSCORE");
goto oscore_send_dispatch;
}
OC_DBG("### protecting outgoing response ###");
/* Use context->SSN as partial IV */
oscore_store_piv(oscore_ctx->ssn, piv, &piv_len);
OC_DBG_OSCORE("---using SSN as Partial IV");
OC_LOGbytes_OSCORE(piv, piv_len);
OC_DBG_OSCORE("---");
/* Increment SSN for the original request, retransmissions use the same
* SSN */
coap_transaction_t *transaction =
coap_get_transaction_by_token(coap_pkt->token, coap_pkt->token_len);
bool is_initial_transmission =
transaction && transaction->retrans_counter == 0;
bool is_empty_ack =
coap_pkt->type == COAP_TYPE_ACK && coap_pkt->code == 0;
bool is_separate_response = coap_pkt->type == COAP_TYPE_CON;
bool is_not_transaction = !transaction;
if (is_initial_transmission || is_empty_ack || is_separate_response ||
is_not_transaction)
increment_ssn_in_context(oscore_ctx);
if (is_empty_ack || is_separate_response) {
// empty acks and separate responses use a new PIV
OC_DBG_OSCORE("---piv");
OC_LOGbytes_OSCORE(piv, piv_len);
oc_oscore_AEAD_nonce(oscore_ctx->sendid, oscore_ctx->sendid_len, piv,
piv_len, oscore_ctx->commoniv, nonce,
OSCORE_AEAD_NONCE_LEN);
/* Compute nonce using partial IV and sender ID of the sender ( =
* receiver ID )*/
OC_DBG_OSCORE(
"---computed AEAD nonce using new Partial IV (SSN) and Sender ID");
OC_LOGbytes_OSCORE(nonce, OSCORE_AEAD_NONCE_LEN);
} else {
// other responses reuse the PIV from the request
OC_DBG_OSCORE("---request_piv");
OC_LOGbytes_OSCORE(message->endpoint.request_piv,
message->endpoint.request_piv_len);
oc_oscore_AEAD_nonce(
oscore_ctx->recvid, oscore_ctx->recvid_len,
message->endpoint.request_piv, message->endpoint.request_piv_len,
oscore_ctx->commoniv, nonce, OSCORE_AEAD_NONCE_LEN);
/* Compute nonce using partial IV and sender ID of the sender ( =
* receiver ID )*/
OC_DBG_OSCORE(
"---computed AEAD nonce using new Partial IV (SSN) and Sender ID");
OC_LOGbytes_OSCORE(nonce, OSCORE_AEAD_NONCE_LEN);
}
// AAD always uses the request PIV
// This block, alongside endpoint.rx_msg_is_response, is needed for
// encrypting the final ack of a separate response sequence. For now, we
// have decided to send that ack in plaintext, so this is all commented
// out
/*
if (is_empty_ack && msg->endpoint.rx_msg_is_response)
{
// only fires when the client is sending the acknowledgement
// for the confirmable, separate response of the server
OC_DBG_OSCORE("--- is empty ACK, using details from the request");
// We have already sent an ACK for the original request, so the
transaction
// is gone and the other side cannot find the keying material. so for
this
// message only we include it again.
memcpy(kid, oscore_ctx->sendid, oscore_ctx->sendid_len);
kid_len = oscore_ctx->sendid_len;
memcpy(ctx_id, oscore_ctx->idctx, oscore_ctx->idctx_len);
ctx_id_len = oscore_ctx->idctx_len;
OC_DBG_OSCORE("--- Including KID:");
OC_LOGbytes_OSCORE(kid, kid_len);
oc_oscore_compose_AAD(oscore_ctx->sendid, oscore_ctx->sendid_len,
message->endpoint.request_piv,
message->endpoint.request_piv_len, AAD, &AAD_len);
}
else
*/
{
oc_oscore_compose_AAD(oscore_ctx->recvid, oscore_ctx->recvid_len,
message->endpoint.request_piv,
message->endpoint.request_piv_len, AAD, &AAD_len);
}
OC_DBG_OSCORE("---composed AAD using request piv and Recipient ID");
OC_LOGbytes_OSCORE(AAD, AAD_len);
/* Copy partial IV into incoming oc_message_t (*msg), if valid and if
* message is request */
if (msg_valid && coap_pkt->code >= OC_GET &&
coap_pkt->code <= OC_DELETE) {
memcpy(msg->endpoint.request_piv, piv, piv_len);
msg->endpoint.request_piv_len = piv_len;
OC_DBG_OSCORE("--- Caching PIV for later use...");
OC_LOGbytes_OSCORE(msg->endpoint.request_piv,
msg->endpoint.request_piv_len);
}
}
// store the inner CoAP code
uint8_t inner_code = coap_pkt->code;
/* Move CoAP payload to offset 2*COAP_MAX_HEADER_SIZE to accommodate for
Outer+Inner CoAP options in the OSCORE packet.
*/
if (coap_pkt->payload_len > 0) {
memmove(message->data + 2 * COAP_MAX_HEADER_SIZE, coap_pkt->payload,
coap_pkt->payload_len);
/* Store the new payload location in the CoAP packet */
coap_pkt->payload = message->data + 2 * COAP_MAX_HEADER_SIZE;
}
/* Store the observe option. Retain the inner observe option value
* for observe registrations and cancellations. Use an empty value for
* notifications.
*/
int32_t observe_option = coap_pkt->observe;
if (coap_pkt->observe > 1) {
coap_pkt->observe = 0;
OC_DBG(
"---response is a notification; making inner Observe option empty");
}
OC_DBG("### serializing OSCORE plaintext ###");
/* Serialize OSCORE plaintext at offset COAP_MAX_HEADER_SIZE
(code, inner options, payload)
*/
size_t plaintext_size = oscore_serialize_plaintext(
coap_pkt, message->data + COAP_MAX_HEADER_SIZE);
OC_DBG_OSCORE("### serialized OSCORE plaintext: %zd bytes ###",
plaintext_size);
/* Set the OSCORE packet payload to point to location of the serialized