-
Notifications
You must be signed in to change notification settings - Fork 423
/
Copy pathcoap_oscore.c
2289 lines (2083 loc) · 76 KB
/
coap_oscore.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
/* -*- Mode: C; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/*
* coap_oscore.c -- Object Security for Constrained RESTful Environments
* (OSCORE) support for libcoap
*
* Copyright (C) 2019-2021 Olaf Bergmann <[email protected]>
* Copyright (C) 2021-2025 Jon Shallow <[email protected]>
*
* SPDX-License-Identifier: BSD-2-Clause
*
* This file is part of the CoAP library libcoap. Please see README for terms
* of use.
*/
/**
* @file coap_oscore.c
* @brief CoAP OSCORE handling
*/
#include "coap3/coap_libcoap_build.h"
#if COAP_OSCORE_SUPPORT
#include <ctype.h>
#define AAD_BUF_LEN 200 /* length of aad_buffer */
static oscore_ctx_t *coap_oscore_init(coap_context_t *c_context,
coap_oscore_conf_t *oscore_conf);
#if COAP_CLIENT_SUPPORT
int
coap_oscore_initiate(coap_session_t *session, coap_oscore_conf_t *oscore_conf) {
if (oscore_conf) {
oscore_ctx_t *osc_ctx;
if (oscore_conf->recipient_id_count == 0) {
coap_log_warn("OSCORE: Recipient ID must be defined for a client\n");
return 0;
}
if (oscore_conf->rfc8613_b_2) {
/* Need to replace id_context with random value */
coap_binary_t *id_context = coap_new_binary(8);
if (id_context == NULL)
return 0;
coap_delete_bin_const(oscore_conf->id_context);
coap_prng_lkd(id_context->s, id_context->length);
oscore_conf->id_context = (coap_bin_const_t *)id_context;
session->b_2_step = COAP_OSCORE_B_2_STEP_1;
coap_log_oscore("Appendix B.2 client step 1 (Generated ID1)\n");
}
osc_ctx = coap_oscore_init(session->context, oscore_conf);
if (osc_ctx == NULL) {
return 0;
}
session->recipient_ctx = osc_ctx->recipient_chain;
session->oscore_encryption = 1;
}
return 1;
}
COAP_API coap_session_t *
coap_new_client_session_oscore(coap_context_t *ctx,
const coap_address_t *local_if,
const coap_address_t *server,
coap_proto_t proto,
coap_oscore_conf_t *oscore_conf) {
coap_session_t *session;
coap_lock_lock(ctx, return NULL);
session = coap_new_client_session_oscore_lkd(ctx, local_if, server, proto, oscore_conf);
coap_lock_unlock(ctx);
return session;
}
coap_session_t *
coap_new_client_session_oscore_lkd(coap_context_t *ctx,
const coap_address_t *local_if,
const coap_address_t *server,
coap_proto_t proto,
coap_oscore_conf_t *oscore_conf) {
coap_session_t *session =
coap_new_client_session_lkd(ctx, local_if, server, proto);
if (!session)
return NULL;
if (coap_oscore_initiate(session, oscore_conf) == 0) {
coap_session_release_lkd(session);
return NULL;
}
return session;
}
COAP_API coap_session_t *
coap_new_client_session_oscore_psk(coap_context_t *ctx,
const coap_address_t *local_if,
const coap_address_t *server,
coap_proto_t proto,
coap_dtls_cpsk_t *psk_data,
coap_oscore_conf_t *oscore_conf) {
coap_session_t *session;
coap_lock_lock(ctx, return NULL);
session = coap_new_client_session_oscore_psk_lkd(ctx, local_if, server, proto, psk_data,
oscore_conf);
coap_lock_unlock(ctx);
return session;
}
coap_session_t *
coap_new_client_session_oscore_psk_lkd(coap_context_t *ctx,
const coap_address_t *local_if,
const coap_address_t *server,
coap_proto_t proto,
coap_dtls_cpsk_t *psk_data,
coap_oscore_conf_t *oscore_conf) {
coap_session_t *session;
coap_lock_check_locked(ctx);
session = coap_new_client_session_psk2_lkd(ctx, local_if, server, proto, psk_data);
if (!session)
return NULL;
if (coap_oscore_initiate(session, oscore_conf) == 0) {
coap_session_release_lkd(session);
return NULL;
}
return session;
}
COAP_API coap_session_t *
coap_new_client_session_oscore_pki(coap_context_t *ctx,
const coap_address_t *local_if,
const coap_address_t *server,
coap_proto_t proto,
coap_dtls_pki_t *pki_data,
coap_oscore_conf_t *oscore_conf) {
coap_session_t *session;
coap_lock_lock(ctx, return NULL);
session = coap_new_client_session_oscore_pki_lkd(ctx, local_if, server, proto, pki_data,
oscore_conf);
coap_lock_unlock(ctx);
return session;
}
coap_session_t *
coap_new_client_session_oscore_pki_lkd(coap_context_t *ctx,
const coap_address_t *local_if,
const coap_address_t *server,
coap_proto_t proto,
coap_dtls_pki_t *pki_data,
coap_oscore_conf_t *oscore_conf) {
coap_session_t *session;
coap_lock_check_locked(ctx);
session = coap_new_client_session_pki_lkd(ctx, local_if, server, proto, pki_data);
if (!session)
return NULL;
if (coap_oscore_initiate(session, oscore_conf) == 0) {
coap_session_release_lkd(session);
return NULL;
}
return session;
}
#endif /* COAP_CLIENT_SUPPORT */
#if COAP_SERVER_SUPPORT
COAP_API int
coap_context_oscore_server(coap_context_t *context,
coap_oscore_conf_t *oscore_conf) {
int ret;
coap_lock_lock(context, return 0);
ret = coap_context_oscore_server_lkd(context, oscore_conf);
coap_lock_unlock(context);
return ret;
}
int
coap_context_oscore_server_lkd(coap_context_t *context,
coap_oscore_conf_t *oscore_conf) {
oscore_ctx_t *osc_ctx;
coap_lock_check_locked(context);
osc_ctx = coap_oscore_init(context, oscore_conf);
/* osc_ctx already added to context->osc_ctx */
if (osc_ctx)
return 1;
return 0;
}
#endif /* COAP_SERVER_SUPPORT */
int
coap_rebuild_pdu_for_proxy(coap_pdu_t *pdu) {
coap_uri_t uri;
coap_opt_iterator_t opt_iter;
coap_opt_t *option;
uint8_t option_value_buffer[15];
coap_optlist_t *optlist_chain = NULL;
if ((option =
coap_check_option(pdu, COAP_OPTION_PROXY_URI, &opt_iter)) == NULL)
return 1;
/* Need to break down into the component parts, but keep data safe */
memset(&uri, 0, sizeof(uri));
if (coap_split_proxy_uri(coap_opt_value(option),
coap_opt_length(option),
&uri) < 0 || uri.scheme >= COAP_URI_SCHEME_LAST) {
coap_log_warn("Proxy URI '%.*s' not decodable\n",
coap_opt_length(option),
(const char *)coap_opt_value(option));
goto error;
}
if (!coap_remove_option(pdu, COAP_OPTION_PROXY_URI))
goto error;
if (!coap_insert_option(pdu,
COAP_OPTION_URI_HOST,
uri.host.length,
uri.host.s))
goto error;
if (uri.port != (coap_uri_scheme_is_secure(&uri) ? COAPS_DEFAULT_PORT : COAP_DEFAULT_PORT) &&
!coap_insert_option(pdu,
COAP_OPTION_URI_PORT,
coap_encode_var_safe(option_value_buffer,
sizeof(option_value_buffer),
uri.port & 0xffff),
option_value_buffer))
goto error;
if (uri.path.length) {
/* Add in the Uri-Path options */
if (!coap_path_into_optlist(uri.path.s, uri.path.length, COAP_OPTION_URI_PATH,
&optlist_chain))
goto error;
}
if (uri.query.length) {
/* Add in the Uri-Query options */
if (!coap_query_into_optlist(uri.query.s, uri.query.length, COAP_OPTION_URI_QUERY,
&optlist_chain))
goto error;
}
if (!coap_add_optlist_pdu(pdu, &optlist_chain))
goto error;
if (!coap_insert_option(pdu,
COAP_OPTION_PROXY_SCHEME,
strlen(coap_uri_scheme[uri.scheme].name),
(const uint8_t *)coap_uri_scheme[uri.scheme].name))
goto error;
coap_delete_optlist(optlist_chain);
return 1;
error:
coap_delete_optlist(optlist_chain);
return 0;
}
static void
dump_cose(cose_encrypt0_t *cose, const char *message) {
#if COAP_MAX_LOGGING_LEVEL < _COAP_LOG_OSCORE
(void)cose;
(void)message;
#else /* COAP_MAX_LOGGING_LEVEL >= _COAP_LOG_OSCORE */
if (coap_get_log_level() >= COAP_LOG_OSCORE) {
char buffer[30];
coap_log_oscore("%s Cose information\n", message);
oscore_log_char_value(COAP_LOG_OSCORE, "alg",
cose_get_alg_name(cose->alg, buffer, sizeof(buffer)));
oscore_log_hex_value(COAP_LOG_OSCORE, "key", &cose->key);
oscore_log_hex_value(COAP_LOG_OSCORE, "partial_iv", &cose->partial_iv);
oscore_log_hex_value(COAP_LOG_OSCORE, "key_id", &cose->key_id);
oscore_log_hex_value(COAP_LOG_OSCORE, "kid_context", &cose->kid_context);
oscore_log_hex_value(COAP_LOG_OSCORE,
"oscore_option",
&cose->oscore_option);
oscore_log_hex_value(COAP_LOG_OSCORE, "nonce", &cose->nonce);
oscore_log_hex_value(COAP_LOG_OSCORE, "external_aad", &cose->external_aad);
oscore_log_hex_value(COAP_LOG_OSCORE, "aad", &cose->aad);
}
#endif /* COAP_MAX_LOGGING_LEVEL >= _COAP_LOG_OSCORE */
}
COAP_API coap_pdu_t *
coap_oscore_new_pdu_encrypted(coap_session_t *session,
coap_pdu_t *pdu,
coap_bin_const_t *kid_context,
oscore_partial_iv_t send_partial_iv) {
coap_pdu_t *ret_pdu;
coap_lock_lock(session->context, return NULL);
ret_pdu = coap_oscore_new_pdu_encrypted_lkd(session, pdu, kid_context, send_partial_iv);
coap_lock_unlock(session->context);
return ret_pdu;
}
/*
* Take current PDU, create a new one approriately separated as per RFC8613
* and then encrypt / integrity check the OSCORE data
*/
coap_pdu_t *
coap_oscore_new_pdu_encrypted_lkd(coap_session_t *session,
coap_pdu_t *pdu,
coap_bin_const_t *kid_context,
oscore_partial_iv_t send_partial_iv) {
uint8_t coap_request = COAP_PDU_IS_REQUEST(pdu) || COAP_PDU_IS_PING(pdu);
coap_pdu_code_t code =
coap_request ? COAP_REQUEST_CODE_POST : COAP_RESPONSE_CODE(204);
coap_pdu_t *osc_pdu;
coap_pdu_t *plain_pdu = NULL;
coap_bin_const_t pdu_token;
coap_opt_iterator_t opt_iter;
coap_opt_t *option;
uint8_t pdu_code = pdu->code;
size_t length;
const uint8_t *data;
uint8_t *ciphertext_buffer = NULL;
size_t ciphertext_len = 0;
uint8_t aad_buffer[AAD_BUF_LEN];
uint8_t nonce_buffer[13];
coap_bin_const_t aad;
coap_bin_const_t nonce;
oscore_recipient_ctx_t *rcp_ctx;
oscore_ctx_t *osc_ctx;
cose_encrypt0_t cose[1];
uint8_t group_flag = 0;
int show_pdu = 0;
int doing_observe = 0;
uint32_t observe_value = 0;
oscore_association_t *association = NULL;
oscore_sender_ctx_t *snd_ctx;
uint8_t external_aad_buffer[200];
coap_bin_const_t external_aad;
uint8_t oscore_option[48];
size_t oscore_option_len;
/* Check that OSCORE has not already been done */
if (coap_check_option(pdu, COAP_OPTION_OSCORE, &opt_iter))
return NULL;
if (coap_check_option(pdu, COAP_OPTION_OBSERVE, &opt_iter))
doing_observe = 1;
coap_log_debug("PDU to encrypt\n");
coap_show_pdu(COAP_LOG_DEBUG, pdu);
osc_pdu = coap_pdu_init(pdu->type == COAP_MESSAGE_NON &&
session->b_2_step != COAP_OSCORE_B_2_NONE ?
COAP_MESSAGE_CON : pdu->type,
code,
pdu->mid,
pdu->used_size + coap_oscore_overhead(session, pdu));
if (osc_pdu == NULL)
return NULL;
cose_encrypt0_init(cose); /* clears cose memory */
pdu_token = coap_pdu_get_token(pdu);
if (coap_request) {
/*
* RFC8613 8.1 Step 1. Protecting the client's request
* Get the Sender Context
*/
rcp_ctx = session->recipient_ctx;
if (rcp_ctx == NULL)
goto error;
osc_ctx = rcp_ctx->osc_ctx;
assert(osc_ctx);
snd_ctx = osc_ctx->sender_context;
} else {
/*
* RFC8613 8.3 Step 1. Protecting the server's response
* Get the Sender Context
*/
association = oscore_find_association(session, &pdu_token);
if (association == NULL)
goto error;
rcp_ctx = association->recipient_ctx;
osc_ctx = rcp_ctx->osc_ctx;
snd_ctx = osc_ctx->sender_context;
cose_encrypt0_set_partial_iv(cose, association->partial_iv);
cose_encrypt0_set_aad(cose, association->aad);
}
cose_encrypt0_set_alg(cose, osc_ctx->aead_alg);
if (coap_request || doing_observe ||
send_partial_iv == OSCORE_SEND_PARTIAL_IV) {
uint8_t partial_iv_buffer[8];
size_t partial_iv_len;
coap_bin_const_t partial_iv;
partial_iv_len = coap_encode_var_safe8(partial_iv_buffer,
sizeof(partial_iv_buffer),
snd_ctx->seq);
if (snd_ctx->seq == 0) {
/* Need to special case */
partial_iv_buffer[0] = '\000';
partial_iv_len = 1;
}
partial_iv.s = partial_iv_buffer;
partial_iv.length = partial_iv_len;
cose_encrypt0_set_partial_iv(cose, &partial_iv);
}
if (coap_request)
cose_encrypt0_set_kid_context(cose, osc_ctx->id_context);
cose_encrypt0_set_key_id(cose, snd_ctx->sender_id);
/* nonce (needs to have sender information correctly set up) */
if (coap_request || doing_observe ||
send_partial_iv == OSCORE_SEND_PARTIAL_IV) {
/*
* 8.1 Step 3 or RFC8613 8.3.1 Step A
* Compose the AEAD nonce
*
* Requires in COSE object as appropriate
* key_id (kid) (sender)
* partial_iv (sender)
* common_iv (already in osc_ctx)
*/
nonce.s = nonce_buffer;
nonce.length = 13;
oscore_generate_nonce(cose, osc_ctx, nonce_buffer, 13);
cose_encrypt0_set_nonce(cose, &nonce);
if (!oscore_increment_sender_seq(osc_ctx))
goto error;
if (osc_ctx->save_seq_num_func) {
if (osc_ctx->sender_context->seq > osc_ctx->sender_context->next_seq) {
/* Only update at ssn_freq rate */
osc_ctx->sender_context->next_seq += osc_ctx->ssn_freq;
osc_ctx->save_seq_num_func(osc_ctx->sender_context->next_seq,
osc_ctx->save_seq_num_func_param);
}
}
} else {
/*
* 8.3 Step 3.
* Use nonce from request
*/
cose_encrypt0_set_nonce(cose, association->nonce);
}
/* OSCORE_option (needs to be before AAD as included in AAD if group) */
/* cose is modified for encode option in response message */
if (!coap_request) {
/* no kid on response */
cose_encrypt0_set_key_id(cose, NULL);
if (!doing_observe && send_partial_iv == OSCORE_SEND_NO_IV)
cose_encrypt0_set_partial_iv(cose, NULL);
}
if (kid_context) {
cose_encrypt0_set_kid_context(cose, kid_context);
}
oscore_option_len =
oscore_encode_option_value(oscore_option, sizeof(oscore_option), cose,
group_flag,
session->b_2_step != COAP_OSCORE_B_2_NONE);
if (!coap_request) {
/* Reset what was just unset as appropriate for AAD */
cose_encrypt0_set_key_id(cose, rcp_ctx->recipient_id);
cose_encrypt0_set_partial_iv(cose, association->partial_iv);
}
if (kid_context)
cose_encrypt0_set_kid_context(cose, osc_ctx->id_context);
/*
* RFC8613 8.1/8.3 Step 2(a) (5.4).
* Compose the External AAD and then AAD
*
* OSCORE_option requires
* partial_iv (cose partial_iv)
* kid_context (cose kid_context)
* key_id (cose key_id)
* group_flag
*
* Non Group (based on osc_tx->mode) requires the following
* aead_alg (osc_ctx)
* request_kid (request key_id using cose)
* request_piv (request partial_iv using cose)
* options (none at present)
* Group (based on osc_tx->mode) requires the following
* aead_alg (osc_ctx) (pairwise mode)
* sign_enc_alg (osc_ctx) (group mode)
* sign_alg (osc_ctx) (group mode)
* alg_pairwise_key_agreement (osc_ctx) (pairwise mode)
* request_kid (request key_id using cose)
* request_piv (request partial_iv using cose)
* options (none at present)
* request_kid_context (osc_ctx id_context)
* OSCORE_option (parameter)
* test_gs_public_key (osc_ctx sender_context public_key)
* gm_public_key (osc_ctx gm_public_key)
*
* Note: No I options at present
*/
if (coap_request || osc_ctx->mode != OSCORE_MODE_SINGLE ||
send_partial_iv == OSCORE_SEND_PARTIAL_IV) {
/* External AAD */
external_aad.s = external_aad_buffer;
external_aad.length = oscore_prepare_e_aad(osc_ctx,
cose,
NULL,
0,
NULL,
external_aad_buffer,
sizeof(external_aad_buffer));
cose_encrypt0_set_external_aad(cose, &external_aad);
/* AAD */
aad.s = aad_buffer;
aad.length = oscore_prepare_aad(external_aad_buffer,
external_aad.length,
aad_buffer,
sizeof(aad_buffer));
assert(aad.length < AAD_BUF_LEN);
cose_encrypt0_set_aad(cose, &aad);
}
/*
* RFC8613 8.1/8.3 Step 2(b) (5.3).
*
* Set up temp plaintext pdu, the data including token, options and
* optional payload will get encrypted as COSE ciphertext.
*/
plain_pdu = coap_pdu_init(pdu->type,
pdu->code,
pdu->mid,
pdu->used_size + 1 /* pseudo-token with actual code */);
if (plain_pdu == NULL)
goto error;
coap_add_token(osc_pdu, pdu_token.length, pdu_token.s);
/* First byte of plain is real CoAP code. Pretend it is token */
coap_add_token(plain_pdu, 1, &pdu_code);
/* Copy across the Outer/Inner Options to respective PDUs */
coap_option_iterator_init(pdu, &opt_iter, COAP_OPT_ALL);
while ((option = coap_option_next(&opt_iter))) {
switch (opt_iter.number) {
case COAP_OPTION_URI_HOST:
case COAP_OPTION_URI_PORT:
case COAP_OPTION_PROXY_SCHEME:
case COAP_OPTION_HOP_LIMIT:
/* Outer only */
if (!coap_insert_option(osc_pdu,
opt_iter.number,
coap_opt_length(option),
coap_opt_value(option)))
goto error;
break;
case COAP_OPTION_OBSERVE:
/* Make as Outer option as-is */
if (!coap_insert_option(osc_pdu,
opt_iter.number,
coap_opt_length(option),
coap_opt_value(option)))
goto error;
if (coap_request) {
/* Make as Inner option (unchanged) */
if (!coap_insert_option(plain_pdu,
opt_iter.number,
coap_opt_length(option),
coap_opt_value(option)))
goto error;
osc_pdu->code = COAP_REQUEST_CODE_FETCH;
} else {
/* Make as Inner option but empty */
if (!coap_insert_option(plain_pdu, opt_iter.number, 0, NULL))
goto error;
osc_pdu->code = COAP_RESPONSE_CODE(205);
}
show_pdu = 1;
doing_observe = 1;
observe_value = coap_decode_var_bytes(coap_opt_value(option),
coap_opt_length(option));
break;
case COAP_OPTION_PROXY_URI:
/*
* Should have already been caught by doing
* coap_rebuild_pdu_for_proxy() before calling
* coap_oscore_new_pdu_encrypted_lkd()
*/
assert(0);
break;
default:
/* Make as Inner option */
if (!coap_insert_option(plain_pdu,
opt_iter.number,
coap_opt_length(option),
coap_opt_value(option)))
goto error;
break;
}
}
/* Add in data to plain */
if (coap_get_data(pdu, &length, &data)) {
if (!coap_add_data(plain_pdu, length, data))
goto error;
}
if (show_pdu) {
coap_log_oscore("OSCORE payload\n");
coap_show_pdu(COAP_LOG_OSCORE, plain_pdu);
}
/*
* 8.1/8.3 Step 4.
* Encrypt the COSE object.
*
* Requires in COSE object as appropriate
* alg (already set)
* key (sender key)
* nonce (already set)
* aad (already set)
* plaintext
*/
cose_encrypt0_set_key(cose, snd_ctx->sender_key);
cose_encrypt0_set_plaintext(cose, plain_pdu->token, plain_pdu->used_size);
dump_cose(cose, "Pre encrypt");
ciphertext_buffer =
coap_malloc_type(COAP_OSCORE_BUF, OSCORE_CRYPTO_BUFFER_SIZE);
if (ciphertext_buffer == NULL)
goto error;
ciphertext_len = cose_encrypt0_encrypt(cose,
ciphertext_buffer,
plain_pdu->used_size + AES_CCM_TAG);
if ((int)ciphertext_len <= 0) {
coap_log_warn("OSCORE: Encryption Failure, result code: %d \n",
(int)ciphertext_len);
goto error;
}
assert(ciphertext_len < OSCORE_CRYPTO_BUFFER_SIZE);
/* Add in OSCORE option (previously computed) */
if (!coap_insert_option(osc_pdu,
COAP_OPTION_OSCORE,
oscore_option_len,
oscore_option))
goto error;
/* Add now encrypted payload */
if (!coap_add_data(osc_pdu, ciphertext_len, ciphertext_buffer))
goto error;
coap_free_type(COAP_OSCORE_BUF, ciphertext_buffer);
ciphertext_buffer = NULL;
coap_delete_pdu_lkd(plain_pdu);
plain_pdu = NULL;
if (association && association->is_observe == 0)
oscore_delete_association(session, association);
association = NULL;
if (!(session->block_mode & COAP_BLOCK_CACHE_RESPONSE)) {
/*
* If this is a response ACK with data, make it a separate response
* by sending an Empty ACK and changing osc_pdu's MID and type. This
* then allows lost response ACK (now CON) with data to be recovered.
*/
if (coap_request == 0 && osc_pdu->type == COAP_MESSAGE_ACK &&
COAP_RESPONSE_CLASS(pdu->code) == 2 &&
COAP_PROTO_NOT_RELIABLE(session->proto)) {
coap_pdu_t *empty = coap_pdu_init(COAP_MESSAGE_ACK,
0,
osc_pdu->mid,
0);
if (empty) {
if (coap_send_internal(session, empty, NULL) != COAP_INVALID_MID) {
osc_pdu->mid = coap_new_message_id_lkd(session);
osc_pdu->type = COAP_MESSAGE_CON;
}
}
}
}
if (!coap_pdu_encode_header(osc_pdu, session->proto)) {
goto error;
}
/*
* Set up an association for handling a response if this is a request
*/
if (coap_request) {
association = oscore_find_association(session, &pdu_token);
if (association) {
/* Refresh the association */
coap_delete_bin_const(association->nonce);
association->nonce =
coap_new_bin_const(cose->nonce.s, cose->nonce.length);
if (association->nonce == NULL)
goto error;
coap_delete_bin_const(association->aad);
association->aad = coap_new_bin_const(cose->aad.s, cose->aad.length);
if (association->aad == NULL)
goto error;
if (doing_observe && observe_value == 1) {
coap_delete_bin_const(association->obs_partial_iv);
association->obs_partial_iv = association->partial_iv;
} else {
coap_delete_bin_const(association->partial_iv);
}
association->partial_iv =
coap_new_bin_const(cose->partial_iv.s, cose->partial_iv.length);
if (association->partial_iv == NULL)
goto error;
association->recipient_ctx = rcp_ctx;
coap_delete_pdu_lkd(association->sent_pdu);
if (session->b_2_step != COAP_OSCORE_B_2_NONE && !session->done_b_1_2) {
size_t size;
association->sent_pdu = coap_pdu_duplicate_lkd(pdu, session,
pdu_token.length,
pdu_token.s, NULL);
if (association->sent_pdu == NULL)
goto error;
if (coap_get_data(pdu, &size, &data)) {
coap_add_data(association->sent_pdu, size, data);
}
} else {
association->sent_pdu = NULL;
}
} else if (!oscore_new_association(session,
pdu,
&pdu_token,
rcp_ctx,
&cose->aad,
&cose->nonce,
&cose->partial_iv,
doing_observe)) {
goto error;
}
session->done_b_1_2 = 1;
}
return osc_pdu;
error:
if (ciphertext_buffer)
coap_free_type(COAP_OSCORE_BUF, ciphertext_buffer);
coap_delete_pdu_lkd(osc_pdu);
coap_delete_pdu_lkd(plain_pdu);
return NULL;
}
static void
build_and_send_error_pdu(coap_session_t *session,
coap_pdu_t *rcvd,
coap_pdu_code_t code,
const char *diagnostic,
uint8_t *echo_data,
coap_bin_const_t *kid_context,
int encrypt_oscore) {
coap_pdu_t *err_pdu = NULL;
coap_bin_const_t token;
int oscore_encryption = session->oscore_encryption;
unsigned char buf[4];
token = coap_pdu_get_token(rcvd);
err_pdu = coap_pdu_init(rcvd->type == COAP_MESSAGE_NON ? COAP_MESSAGE_NON :
COAP_MESSAGE_ACK,
code,
rcvd->mid,
token.length + 2 + 8 +
(diagnostic ? strlen(diagnostic) : 0));
if (!err_pdu)
return;
coap_add_token(err_pdu, token.length, token.s);
if (echo_data) {
coap_add_option_internal(err_pdu, COAP_OPTION_ECHO, 8, echo_data);
} else if (kid_context == NULL) {
coap_add_option_internal(err_pdu,
COAP_OPTION_MAXAGE,
coap_encode_var_safe(buf, sizeof(buf), 0),
buf);
}
if (diagnostic)
coap_add_data(err_pdu, strlen(diagnostic), (const uint8_t *)diagnostic);
session->oscore_encryption = encrypt_oscore;
if ((echo_data || kid_context) && encrypt_oscore) {
coap_pdu_t *osc_pdu;
osc_pdu =
coap_oscore_new_pdu_encrypted_lkd(session, err_pdu, kid_context,
echo_data ? 1 : 0);
if (!osc_pdu)
goto fail_resp;
session->oscore_encryption = 0;
coap_send_internal(session, osc_pdu, NULL);
coap_delete_pdu_lkd(err_pdu);
err_pdu = NULL;
} else {
coap_send_internal(session, err_pdu, NULL);
err_pdu = NULL;
}
fail_resp:
session->oscore_encryption = oscore_encryption;
coap_delete_pdu_lkd(err_pdu);
return;
}
/* pdu contains incoming message with encrypted COSE ciphertext payload
* function returns decrypted message
* and verifies signature, if present
* returns NULL when decryption,verification fails
*/
coap_pdu_t *
coap_oscore_decrypt_pdu(coap_session_t *session,
coap_pdu_t *pdu) {
coap_pdu_t *decrypt_pdu = NULL;
coap_pdu_t *plain_pdu = NULL;
const uint8_t *osc_value; /* value of OSCORE option */
uint8_t osc_size; /* size of OSCORE OPTION */
coap_opt_iterator_t opt_iter;
coap_opt_t *opt = NULL;
cose_encrypt0_t cose[1];
oscore_ctx_t *osc_ctx = NULL;
uint8_t aad_buffer[AAD_BUF_LEN];
uint8_t nonce_buffer[13];
coap_bin_const_t aad;
coap_bin_const_t nonce;
int pltxt_size = 0;
int got_resp_piv = 0;
int doing_resp_observe = 0;
uint8_t coap_request = COAP_PDU_IS_REQUEST(pdu);
coap_bin_const_t pdu_token;
uint8_t *st_encrypt;
size_t encrypt_len;
size_t tag_len;
oscore_recipient_ctx_t *rcp_ctx = NULL;
oscore_association_t *association = NULL;
uint8_t external_aad_buffer[100];
coap_bin_const_t external_aad;
oscore_sender_ctx_t *snd_ctx = NULL;
#if COAP_CLIENT_SUPPORT
coap_pdu_t *sent_pdu = NULL;
#endif /* COAP_CLIENT_SUPPORT */
opt = coap_check_option(pdu, COAP_OPTION_OSCORE, &opt_iter);
assert(opt);
if (opt == NULL)
return NULL;
if (session->context->p_osc_ctx == NULL) {
coap_log_warn("OSCORE: Not enabled\n");
if (!coap_request)
coap_handle_event_lkd(session->context,
COAP_EVENT_OSCORE_NOT_ENABLED,
session);
return NULL;
}
if (pdu->data == NULL) {
coap_log_warn("OSCORE: No protected payload\n");
if (!coap_request)
coap_handle_event_lkd(session->context,
COAP_EVENT_OSCORE_NO_PROTECTED_PAYLOAD,
session);
return NULL;
}
osc_size = coap_opt_length(opt);
osc_value = coap_opt_value(opt);
cose_encrypt0_init(cose); /* clear cose memory */
/* PDU code will be filled in after decryption */
decrypt_pdu =
coap_pdu_init(pdu->type, 0, pdu->mid, pdu->used_size);
if (decrypt_pdu == NULL) {
if (!coap_request)
coap_handle_event_lkd(session->context,
COAP_EVENT_OSCORE_INTERNAL_ERROR,
session);
goto error;
}
/* Copy across the Token */
pdu_token = coap_pdu_get_token(pdu);
coap_add_token(decrypt_pdu, pdu_token.length, pdu_token.s);
/*
* 8.2/8.4 Step 1.
* Copy outer options across, except E and OSCORE options
*/
coap_option_iterator_init(pdu, &opt_iter, COAP_OPT_ALL);
while ((opt = coap_option_next(&opt_iter))) {
switch (opt_iter.number) {
/* 'E' options skipped */
case COAP_OPTION_IF_MATCH:
case COAP_OPTION_ETAG:
case COAP_OPTION_IF_NONE_MATCH:
case COAP_OPTION_OBSERVE:
case COAP_OPTION_LOCATION_PATH:
case COAP_OPTION_URI_PATH:
case COAP_OPTION_CONTENT_FORMAT:
case COAP_OPTION_MAXAGE:
case COAP_OPTION_URI_QUERY:
case COAP_OPTION_ACCEPT:
case COAP_OPTION_LOCATION_QUERY:
case COAP_OPTION_BLOCK2:
case COAP_OPTION_BLOCK1:
case COAP_OPTION_SIZE2:
case COAP_OPTION_SIZE1:
case COAP_OPTION_NORESPONSE:
case COAP_OPTION_ECHO:
case COAP_OPTION_RTAG:
/* OSCORE does not get copied across */
case COAP_OPTION_OSCORE:
break;
default:
if (!coap_add_option_internal(decrypt_pdu,
opt_iter.number,
coap_opt_length(opt),
coap_opt_value(opt))) {
if (!coap_request)
coap_handle_event_lkd(session->context,
COAP_EVENT_OSCORE_INTERNAL_ERROR,
session);
goto error;
}
break;
}
}
if (coap_request) {
uint64_t incoming_seq;
/*
* 8.2 Step 2
* Decompress COSE object
* Get Recipient Context based on kid and optional kid_context
*/
if (oscore_decode_option_value(osc_value, osc_size, cose) == 0) {
coap_log_warn("OSCORE: OSCORE Option cannot be decoded.\n");
build_and_send_error_pdu(session,
pdu,
COAP_RESPONSE_CODE(402),
"Failed to decode COSE",
NULL,
NULL,
0);
goto error_no_ack;
}
osc_ctx = oscore_find_context(session->context,
cose->key_id,
&cose->kid_context,
NULL,
&rcp_ctx);
if (!osc_ctx) {
if (cose->kid_context.length > 0) {
const uint8_t *ptr;
size_t length;
/* Appendix B.2 protocol check - Is the recipient key_id known */
osc_ctx = oscore_find_context(session->context,
cose->key_id,
NULL,
session->oscore_r2 != 0 ? (uint8_t *)&session->oscore_r2 : NULL,
&rcp_ctx);
ptr = cose->kid_context.s;
length = cose->kid_context.length;
if (ptr && osc_ctx && osc_ctx->rfc8613_b_2 &&
osc_ctx->mode == OSCORE_MODE_SINGLE) {
/* Processing Appendix B.2 protocol */
/* Need to CBOR unwrap kid_context */
coap_bin_const_t kid_context;
kid_context.length = oscore_cbor_get_element_size(&ptr, &length);
kid_context.s = ptr;
cose_encrypt0_set_kid_context(cose, (coap_bin_const_t *)&kid_context);
if (session->oscore_r2 != 0) {
/* B.2 step 4 */
coap_bin_const_t *kc = coap_new_bin_const(cose->kid_context.s,
cose->kid_context.length);
if (kc == NULL)
goto error;
session->b_2_step = COAP_OSCORE_B_2_STEP_4;
coap_log_oscore("Appendix B.2 server step 4 (R2 || R3)\n");
oscore_update_ctx(osc_ctx, kc);
} else {
session->b_2_step = COAP_OSCORE_B_2_STEP_2;
coap_log_oscore("Appendix B.2 server step 2 (ID1)\n");
osc_ctx = oscore_duplicate_ctx(session->context,