-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathSecureBlockwiseLayer.cs
More file actions
679 lines (580 loc) · 28 KB
/
SecureBlockwiseLayer.cs
File metadata and controls
679 lines (580 loc) · 28 KB
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
/*
* Copyright (c) 2019-2020, Jim Schaad <ietf@augustcellars.com>
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY.
*
* This file is part of the CoAP.NET, a CoAP framework in C#.
* Please see README for more information.
*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Timers;
using Com.AugustCellars.CoAP.Stack;
using Com.AugustCellars.CoAP.Log;
using Com.AugustCellars.CoAP.Net;
namespace Com.AugustCellars.CoAP.OSCOAP
{
public class SecureBlockwiseLayer : AbstractLayer
{
static readonly ILogger log = LogManager.GetLogger(typeof(SecureBlockwiseLayer));
private int _maxMessageSize;
private int _defaultBlockSize;
private int _blockTimeout;
public SecureBlockwiseLayer(ICoapConfig config)
{
_maxMessageSize = config.OSCOAP_MaxMessageSize;
_defaultBlockSize = config.OSCOAP_DefaultBlockSize;
_blockTimeout = config.OSCOAP_BlockwiseStatusLifetime;
if (log.IsDebugEnabled) {
log.Debug("SecureBlockwiseLayer uses MaxMessageSize: " + _maxMessageSize + " and DefaultBlockSize:" + _defaultBlockSize);
}
config.PropertyChanged += ConfigChanged;
}
void ConfigChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
{
ICoapConfig config = (ICoapConfig) sender;
if (string.Equals(e.PropertyName, "OSCOAP_MaxMessageSize")) {
_maxMessageSize = config.OSCOAP_MaxMessageSize;
}
else if (string.Equals(e.PropertyName, "OSCOAP_DefaultBlockSize")) {
_defaultBlockSize = config.OSCOAP_DefaultBlockSize;
}
else if (string.Equals(e.PropertyName, "OSCOAP_BlockwiseStatusLifetime")) {
_blockTimeout = config.OSCOAP_BlockwiseStatusLifetime;
}
}
/// <inheritdoc/>
public override void SendRequest(INextLayer nextLayer, Exchange exchange, Request request)
{
// This assumes we don't change individual options - if this is not true then we need to do a deep copy.
exchange.PreSecurityOptions = request.GetOptions().ToList();
if ((request.Oscoap == null) && (exchange.OscoreContext == null)) {
base.SendRequest(nextLayer, exchange, request);
}
else if (request.HasOption(OptionType.Block2) && request.Block2.NUM > 0) {
// This is the case if the user has explicitly added a block option
// for random access.
// Note: We do not regard it as random access when the block num is
// 0. This is because the user might just want to do early block
// size negotiation but actually wants to receive all blocks.
log.Debug("Request carries explicit defined block2 option: create random access blockwise status");
BlockwiseStatus status = new BlockwiseStatus(request.ContentFormat);
BlockOption block2 = request.Block2;
status.CurrentSZX = block2.SZX;
status.CurrentNUM = block2.NUM;
status.IsRandomAccess = true;
exchange.OSCOAP_ResponseBlockStatus = status;
base.SendRequest(nextLayer, exchange, request);
}
else if (RequiresBlockwise(request)) {
// This must be a large POST or PUT request
log.Debug(m => m($"Request payload {request.PayloadSize}/{_maxMessageSize} requires Blockwise."));
BlockwiseStatus status = FindRequestBlockStatus(exchange, request);
Request block = GetNextRequestBlock(request, exchange.PreSecurityOptions, status);
exchange.OscoreRequestBlockStatus = status;
exchange.CurrentRequest = block;
log.Debug($"Block message to send: {block}");
base.SendRequest(nextLayer, exchange, block);
}
else {
exchange.CurrentRequest = request;
base.SendRequest(nextLayer, exchange, request);
}
}
/// <inheritdoc/>
public override void ReceiveRequest(INextLayer nextLayer, Exchange exchange, Request request)
{
if ((request.Oscoap == null) && (exchange.OscoreContext == null)) {
base.ReceiveRequest(nextLayer, exchange, request);
}
else if (request.HasOption(OptionType.Block1)) {
// This must be a large POST or PUT request
BlockOption block1 = request.Block1;
if (log.IsDebugEnabled) {
log.Debug("Request contains block1 option " + block1);
}
BlockwiseStatus status = FindRequestBlockStatus(exchange, request);
if (block1.NUM == 0 && status.CurrentNUM > 0) {
// reset the blockwise transfer
log.Debug("Block1 num is 0, the client has restarted the blockwise transfer. Reset status.");
status = new BlockwiseStatus(request.ContentType);
exchange.OscoreRequestBlockStatus = status;
}
if (block1.NUM == status.CurrentNUM) {
if (request.ContentType == status.ContentFormat) {
status.AddBlock(request.Payload);
}
else {
Response error = Response.CreateResponse(request, StatusCode.RequestEntityIncomplete);
error.AddOption(new BlockOption(OptionType.Block1, block1.NUM, block1.SZX, block1.M));
error.SetPayload("Changed Content-Format");
exchange.CurrentResponse = error;
base.SendResponse(nextLayer, exchange, error);
return;
}
status.CurrentNUM = status.CurrentNUM + 1;
if (block1.M) {
log.Debug("There are more blocks to come. Acknowledge this block.");
Response piggybacked = Response.CreateResponse(request, StatusCode.Continue);
piggybacked.AddOption(new BlockOption(OptionType.Block1, block1.NUM, block1.SZX, true));
piggybacked.Last = false;
exchange.CurrentResponse = piggybacked;
base.SendResponse(nextLayer, exchange, piggybacked);
// do not assemble and deliver the request yet
}
else {
log.Debug("This was the last block. Deliver request");
// Remember block to acknowledge. TODO: We might make this a boolean flag in status.
exchange.Block1ToAck = block1;
// Block2 early negotiation
EarlyBlock2Negotiation(exchange, request);
// Assemble and deliver
Request assembled = new Request(request.Method);
AssembleMessage(status, assembled, request);
exchange.Request = assembled;
base.ReceiveRequest(nextLayer, exchange, assembled);
}
}
else {
// ERROR, wrong number, Incomplete
if (log.IsWarnEnabled) {
log.Warn("Wrong block number. Expected " + status.CurrentNUM + " but received " + block1.NUM + ". Respond with 4.08 (Request Entity Incomplete).");
}
Response error = Response.CreateResponse(request, StatusCode.RequestEntityIncomplete);
error.AddOption(new BlockOption(OptionType.Block1, block1.NUM, block1.SZX, block1.M));
error.SetPayload("Wrong block number");
exchange.CurrentResponse = error;
base.SendResponse(nextLayer, exchange, error);
}
}
else if (exchange.Response != null && request.HasOption(OptionType.Block2)) {
// The response has already been generated and the client just wants
// the next block of it
BlockOption block2 = request.Block2;
Response response = exchange.Response;
BlockwiseStatus status = FindResponseBlockStatus(exchange, response);
status.CurrentNUM = block2.NUM;
status.CurrentSZX = block2.SZX;
Response block = GetNextResponseBlock(response, status);
block.Token = request.Token;
block.RemoveOptions(OptionType.Observe);
if (status.Complete) {
// clean up blockwise status
if (log.IsDebugEnabled) {
log.Debug("Ongoing is complete " + status);
}
exchange.OSCOAP_ResponseBlockStatus = null;
ClearBlockCleanup(exchange);
}
else {
if (log.IsDebugEnabled) {
log.Debug("Ongoing is continuing " + status);
}
}
exchange.CurrentResponse = block;
base.SendResponse(nextLayer, exchange, block);
}
else {
EarlyBlock2Negotiation(exchange, request);
exchange.Request = request;
base.ReceiveRequest(nextLayer, exchange, request);
}
}
/// <inheritdoc/>
public override void SendResponse(INextLayer nextLayer, Exchange exchange, Response response)
{
if ((exchange.OscoreContext == null) && (response.Oscoap == null)) {
base.SendResponse(nextLayer, exchange, response);
return;
}
BlockOption block1 = exchange.Block1ToAck;
if (block1 != null) {
exchange.Block1ToAck = null;
}
if (RequiresBlockwise(exchange, response)) {
if (log.IsDebugEnabled) {
log.Debug("Response payload " + response.PayloadSize + "/" + _maxMessageSize + " requires Blockwise");
}
BlockwiseStatus status = FindResponseBlockStatus(exchange, response);
Response block = GetNextResponseBlock(response, status);
if (block1 != null) // in case we still have to ack the last block1
{
block.SetOption(block1);
}
if (block.Token == null) {
block.Token = exchange.Request.Token;
}
if (status.Complete) {
// clean up blockwise status
if (log.IsDebugEnabled) {
log.Debug("Ongoing finished on first block " + status);
}
exchange.OSCOAP_ResponseBlockStatus = null;
ClearBlockCleanup(exchange);
}
else {
if (log.IsDebugEnabled) {
log.Debug("Ongoing started " + status);
}
}
exchange.CurrentResponse = block;
base.SendResponse(nextLayer, exchange, block);
}
else {
if (block1 != null) {
response.SetOption(block1);
}
exchange.CurrentResponse = response;
// Block1 transfer completed
ClearBlockCleanup(exchange);
base.SendResponse(nextLayer, exchange, response);
}
}
/// <inheritdoc/>
public override void ReceiveResponse(INextLayer nextLayer, Exchange exchange, Response response)
{
if ((exchange.OscoreContext == null) && (response.Oscoap == null)) {
base.ReceiveResponse(nextLayer, exchange, response);
return;
}
// do not continue fetching blocks if canceled
if (exchange.Request.IsCancelled) {
// reject (in particular for Block+Observe)
if (response.Type != MessageType.ACK) {
if (log.IsDebugEnabled) {
log.Debug("Rejecting blockwise transfer for canceled Exchange");
}
EmptyMessage rst = EmptyMessage.NewRST(response);
SendEmptyMessage(nextLayer, exchange, rst);
// Matcher sets exchange as complete when RST is sent
}
return;
}
if (!response.HasOption(OptionType.Block1) && !response.HasOption(OptionType.Block2)) {
// There is no block1 or block2 option, therefore it is a normal response
exchange.Response = response;
base.ReceiveResponse(nextLayer, exchange, response);
return;
}
BlockOption block1 = response.Block1;
if (block1 != null) {
// TODO: What if request has not been sent blockwise (server error)
log.Debug(m => m("Response acknowledges block {block1}"));
BlockwiseStatus status = exchange.OscoreRequestBlockStatus;
if (!status.Complete) {
// TODO: the response code should be CONTINUE. Otherwise deliver
// Send next block
int currentSize = 1 << (4 + status.CurrentSZX);
int nextNum = status.CurrentNUM + currentSize / block1.Size;
log.Debug(m => m($"Send next block num = {nextNum}"));
status.CurrentNUM = nextNum;
status.CurrentSZX = block1.SZX;
Request nextBlock = GetNextRequestBlock(exchange.Request, exchange.PreSecurityOptions, status);
if (nextBlock.Token == null) {
nextBlock.Token = response.Token; // reuse same token
}
// notify blocking progress
exchange.Request.FireResponding(response);
exchange.CurrentRequest = nextBlock;
log.Debug(m => m($"ReceiveResponse: Block message to send: {nextBlock}"));
base.SendRequest(nextLayer, exchange, nextBlock);
// do not deliver response
}
else if (!response.HasOption(OptionType.Block2)) {
// All request block have been acknowledged and we receive a piggy-backed
// response that needs no blockwise transfer. Thus, deliver it.
base.ReceiveResponse(nextLayer, exchange, response);
}
else {
log.Debug("Response has Block2 option and is therefore sent blockwise");
}
}
BlockOption block2 = response.Block2;
if (block2 != null) {
BlockwiseStatus status = FindResponseBlockStatus(exchange, response);
if (block2.NUM == status.CurrentNUM) {
// We got the block we expected :-)
status.AddBlock(response.Payload);
int? obs = response.Observe;
if (obs.HasValue) {
status.Observe = obs.Value;
}
// notify blocking progress
exchange.Request.FireResponding(response);
if (status.IsRandomAccess) {
// The client has requested this specific block and we deliver it
exchange.Response = response;
base.ReceiveResponse(nextLayer, exchange, response);
}
else if (block2.M) {
log.Debug("Request the next response block");
Request request = exchange.Request;
int num = block2.NUM + 1;
int szx = block2.SZX;
bool m = false;
Request block = new Request(request.Method);
// NON could make sense over SMS or similar transports
block.Type = request.Type;
block.Destination = request.Destination;
block.SetOptions(exchange.PreSecurityOptions /* request.GetOptions()*/);
block.SetOption(new BlockOption(OptionType.Block2, num, szx, m));
// we use the same token to ease traceability (GET without Observe no longer cancels relations)
block.Token = response.Token;
// make sure not to use Observe for block retrieval
block.RemoveOptions(OptionType.Observe);
status.CurrentNUM = num;
exchange.CurrentRequest = block;
log.Debug(m1=>m1($"ReceiveResponse: Block request is {block}"));
base.SendRequest(nextLayer, exchange, block);
}
else {
if (log.IsDebugEnabled) {
log.Debug("We have received all " + status.BlockCount + " blocks of the response. Assemble and deliver.");
}
Response assembled = new Response(response.StatusCode);
AssembleMessage(status, assembled, response);
assembled.Type = response.Type;
// set overall transfer RTT
assembled.RTT = (DateTime.Now - exchange.Timestamp).TotalMilliseconds;
// Check if this response is a notification
int observe = status.Observe;
if (observe != BlockwiseStatus.NoObserve) {
assembled.AddOption(Option.Create(OptionType.Observe, observe));
// This is necessary for notifications that are sent blockwise:
// Reset block number AND container with all blocks
exchange.OSCOAP_ResponseBlockStatus = null;
}
if (log.IsDebugEnabled) {
log.Debug("Assembled response: " + assembled);
}
exchange.Response = assembled;
base.ReceiveResponse(nextLayer, exchange, assembled);
}
}
else {
// ERROR, wrong block number (server error)
// TODO: This scenario is not specified in the draft.
// Currently, we reject it and cancel the request.
if (log.IsWarnEnabled) {
log.Warn("Wrong block number. Expected " + status.CurrentNUM + " but received " + block2.NUM + ". Reject response; exchange has failed.");
}
if (response.Type == MessageType.CON) {
EmptyMessage rst = EmptyMessage.NewRST(response);
base.SendEmptyMessage(nextLayer, exchange, rst);
}
exchange.Request.IsCancelled = true;
}
}
}
private void EarlyBlock2Negotiation(Exchange exchange, Request request)
{
// Call this method when a request has completely arrived (might have
// been sent in one piece without blockwise).
if (request.HasOption(OptionType.Block2)) {
BlockOption block2 = request.Block2;
BlockwiseStatus status2 = new BlockwiseStatus(request.ContentType, block2.NUM, block2.SZX);
if (log.IsDebugEnabled) {
log.Debug("Request with early block negotiation " + block2 + ". Create and set new Block2 status: " + status2);
}
exchange.OSCOAP_ResponseBlockStatus = status2;
}
}
/// <summary>
/// Notice:
/// This method is used by SendRequest and ReceiveRequest.
/// Be careful, making changes to the status in here.
/// </summary>
private BlockwiseStatus FindRequestBlockStatus(Exchange exchange, Request request)
{
BlockwiseStatus status = exchange.OscoreRequestBlockStatus;
if (status == null) {
status = new BlockwiseStatus(request.ContentType);
status.CurrentSZX = BlockOption.EncodeSZX(_defaultBlockSize);
exchange.OscoreRequestBlockStatus = status;
if (log.IsDebugEnabled) {
log.Debug("There is no assembler status yet. Create and set new Block1 status: " + status);
}
}
else {
if (log.IsDebugEnabled) {
log.Debug("Current Block1 status: " + status);
}
}
// sets a timeout to complete exchange
PrepareBlockCleanup(exchange);
return status;
}
/// <summary>
/// Notice:
/// This method is used by SendResponse and ReceiveResponse.
/// Be careful, making changes to the status in here.
/// </summary>
private BlockwiseStatus FindResponseBlockStatus(Exchange exchange, Response response)
{
BlockwiseStatus status = exchange.OSCOAP_ResponseBlockStatus;
if (status == null) {
status = new BlockwiseStatus(response.ContentType);
status.CurrentSZX = BlockOption.EncodeSZX(_defaultBlockSize);
exchange.OSCOAP_ResponseBlockStatus = status;
if (log.IsDebugEnabled) {
log.Debug("There is no blockwise status yet. Create and set new Block2 status: " + status);
}
}
else {
if (log.IsDebugEnabled) {
log.Debug("Current Block2 status: " + status);
}
}
// sets a timeout to complete exchange
PrepareBlockCleanup(exchange);
return status;
}
private Request GetNextRequestBlock(Request request, List<Option> originalOptions, BlockwiseStatus status)
{
int num = status.CurrentNUM;
int szx = status.CurrentSZX;
Request block = new Request(request.Method);
block.SetOptions(originalOptions);
block.Destination = request.Destination;
block.Token = request.Token;
block.Type = MessageType.CON;
int currentSize = 1 << (4 + szx);
int from = num * currentSize;
int to = Math.Min((num + 1) * currentSize, request.PayloadSize);
int length = to - from;
byte[] blockPayload = new byte[length];
Array.Copy(request.Payload, from, blockPayload, 0, length);
block.Payload = blockPayload;
bool m = to < request.PayloadSize;
block.AddOption(new BlockOption(OptionType.Block1, num, szx, m));
status.Complete = !m;
return block;
}
private Response GetNextResponseBlock(Response response, BlockwiseStatus status)
{
Response block;
int szx = status.CurrentSZX;
int num = status.CurrentNUM;
if (response.HasOption(OptionType.Observe)) {
// a blockwise notification transmits the first block only
block = response;
}
else {
block = new Response(response.StatusCode);
block.Destination = response.Destination;
block.Token = response.Token;
block.SetOptions(response.GetOptions());
block.TimedOut += (o, e) => response.IsTimedOut = true;
}
int payloadSize = response.PayloadSize;
int currentSize = 1 << (4 + szx);
int from = num * currentSize;
if (payloadSize > 0 && payloadSize > from) {
int to = Math.Min((num + 1) * currentSize, response.PayloadSize);
int length = to - from;
byte[] blockPayload = new byte[length];
bool m = to < response.PayloadSize;
block.SetBlock2(szx, m, num);
// crop payload -- do after calculation of m in case block==response
Array.Copy(response.Payload, from, blockPayload, 0, length);
block.Payload = blockPayload;
// do not complete notifications
block.Last = !m && !response.HasOption(OptionType.Observe);
status.Complete = !m;
}
else {
block.AddOption(new BlockOption(OptionType.Block2, num, szx, false));
block.Last = true;
status.Complete = true;
}
return block;
}
private void AssembleMessage(BlockwiseStatus status, Message message, Message last)
{
// The assembled request will contain the options of the last block
message.ID = last.ID;
message.Source = last.Source;
message.Token = last.Token;
message.Type = last.Type;
message.SetOptions(last.GetOptions());
int length = 0;
foreach (byte[] block in status.Blocks) {
length += block.Length;
}
byte[] payload = new byte[length];
int offset = 0;
foreach (byte[] block in status.Blocks) {
Array.Copy(block, 0, payload, offset, block.Length);
offset += block.Length;
}
message.Payload = payload;
}
private bool RequiresBlockwise(Request request)
{
if (request.Method == Method.PUT || request.Method == Method.POST) {
return request.PayloadSize > _maxMessageSize;
}
else {
return false;
}
}
private bool RequiresBlockwise(Exchange exchange, Response response)
{
return response.PayloadSize > _maxMessageSize
|| exchange.OSCOAP_ResponseBlockStatus != null;
}
/// <summary>
/// Schedules a clean-up task.
/// Use the <see cref="ICoapConfig.BlockwiseStatusLifetime"/> to set the timeout.
/// </summary>
protected void PrepareBlockCleanup(Exchange exchange)
{
Timer timer = new Timer();
timer.AutoReset = false;
timer.Interval = _blockTimeout;
timer.Elapsed += (o, e) => BlockwiseTimeout(exchange);
Timer old = exchange.Set("BlockCleanupTimer", timer) as Timer;
if (old != null) {
try {
old.Stop();
old.Dispose();
}
catch (ObjectDisposedException) {
// ignore
}
}
timer.Start();
}
/// <summary>
/// Clears the clean-up task.
/// </summary>
protected void ClearBlockCleanup(Exchange exchange)
{
Timer timer = exchange.Remove("BlockCleanupTimer") as Timer;
if (timer != null) {
try {
timer.Dispose();
}
catch (ObjectDisposedException) {
// ignore
}
}
}
private void BlockwiseTimeout(Exchange exchange)
{
if (exchange.Request == null) {
if (log.IsInfoEnabled) {
log.Info("Block1 transfer timed out: " + exchange.CurrentRequest);
}
}
else {
if (log.IsInfoEnabled) {
log.Info("Block2 transfer timed out: " + exchange.Request);
}
}
exchange.Complete = true;
}
}
}