@@ -305,31 +305,35 @@ func (s *ServerSession) DecodeRequestHeader(reader io.Reader) (*protocol.Request
305
305
}
306
306
307
307
// DecodeRequestBody returns Reader from which caller can fetch decrypted body.
308
- func (s * ServerSession ) DecodeRequestBody (request * protocol.RequestHeader , reader io.Reader ) buf.Reader {
308
+ func (s * ServerSession ) DecodeRequestBody (request * protocol.RequestHeader , reader io.Reader ) ( buf.Reader , error ) {
309
309
var sizeParser crypto.ChunkSizeDecoder = crypto.PlainChunkSizeParser {}
310
310
if request .Option .Has (protocol .RequestOptionChunkMasking ) {
311
311
sizeParser = NewShakeSizeParser (s .requestBodyIV [:])
312
312
}
313
313
var padding crypto.PaddingLengthGenerator
314
314
if request .Option .Has (protocol .RequestOptionGlobalPadding ) {
315
- padding = sizeParser .(crypto.PaddingLengthGenerator )
315
+ var ok bool
316
+ padding , ok = sizeParser .(crypto.PaddingLengthGenerator )
317
+ if ! ok {
318
+ return nil , newError ("invalid option: RequestOptionGlobalPadding" )
319
+ }
316
320
}
317
321
318
322
switch request .Security {
319
323
case protocol .SecurityType_NONE :
320
324
if request .Option .Has (protocol .RequestOptionChunkStream ) {
321
325
if request .Command .TransferType () == protocol .TransferTypeStream {
322
- return crypto .NewChunkStreamReader (sizeParser , reader )
326
+ return crypto .NewChunkStreamReader (sizeParser , reader ), nil
323
327
}
324
328
325
329
auth := & crypto.AEADAuthenticator {
326
330
AEAD : new (NoOpAuthenticator ),
327
331
NonceGenerator : crypto .GenerateEmptyBytes (),
328
332
AdditionalDataGenerator : crypto .GenerateEmptyBytes (),
329
333
}
330
- return crypto .NewAuthenticationReader (auth , sizeParser , reader , protocol .TransferTypePacket , padding )
334
+ return crypto .NewAuthenticationReader (auth , sizeParser , reader , protocol .TransferTypePacket , padding ), nil
331
335
}
332
- return buf .NewReader (reader )
336
+ return buf .NewReader (reader ), nil
333
337
334
338
case protocol .SecurityType_LEGACY :
335
339
aesStream := crypto .NewAesDecryptionStream (s .requestBodyKey [:], s .requestBodyIV [:])
@@ -340,9 +344,9 @@ func (s *ServerSession) DecodeRequestBody(request *protocol.RequestHeader, reade
340
344
NonceGenerator : crypto .GenerateEmptyBytes (),
341
345
AdditionalDataGenerator : crypto .GenerateEmptyBytes (),
342
346
}
343
- return crypto .NewAuthenticationReader (auth , sizeParser , cryptionReader , request .Command .TransferType (), padding )
347
+ return crypto .NewAuthenticationReader (auth , sizeParser , cryptionReader , request .Command .TransferType (), padding ), nil
344
348
}
345
- return buf .NewReader (cryptionReader )
349
+ return buf .NewReader (cryptionReader ), nil
346
350
347
351
case protocol .SecurityType_AES128_GCM :
348
352
aead := crypto .NewAesGcm (s .requestBodyKey [:])
@@ -362,7 +366,7 @@ func (s *ServerSession) DecodeRequestBody(request *protocol.RequestHeader, reade
362
366
}
363
367
sizeParser = NewAEADSizeParser (lengthAuth )
364
368
}
365
- return crypto .NewAuthenticationReader (auth , sizeParser , reader , request .Command .TransferType (), padding )
369
+ return crypto .NewAuthenticationReader (auth , sizeParser , reader , request .Command .TransferType (), padding ), nil
366
370
367
371
case protocol .SecurityType_CHACHA20_POLY1305 :
368
372
aead , _ := chacha20poly1305 .New (GenerateChacha20Poly1305Key (s .requestBodyKey [:]))
@@ -384,10 +388,10 @@ func (s *ServerSession) DecodeRequestBody(request *protocol.RequestHeader, reade
384
388
}
385
389
sizeParser = NewAEADSizeParser (lengthAuth )
386
390
}
387
- return crypto .NewAuthenticationReader (auth , sizeParser , reader , request .Command .TransferType (), padding )
391
+ return crypto .NewAuthenticationReader (auth , sizeParser , reader , request .Command .TransferType (), padding ), nil
388
392
389
393
default :
390
- panic ( "Unknown security type. " )
394
+ return nil , newError ( "invalid option: Security " )
391
395
}
392
396
}
393
397
@@ -448,31 +452,35 @@ func (s *ServerSession) EncodeResponseHeader(header *protocol.ResponseHeader, wr
448
452
}
449
453
450
454
// EncodeResponseBody returns a Writer that auto-encrypt content written by caller.
451
- func (s * ServerSession ) EncodeResponseBody (request * protocol.RequestHeader , writer io.Writer ) buf.Writer {
455
+ func (s * ServerSession ) EncodeResponseBody (request * protocol.RequestHeader , writer io.Writer ) ( buf.Writer , error ) {
452
456
var sizeParser crypto.ChunkSizeEncoder = crypto.PlainChunkSizeParser {}
453
457
if request .Option .Has (protocol .RequestOptionChunkMasking ) {
454
458
sizeParser = NewShakeSizeParser (s .responseBodyIV [:])
455
459
}
456
460
var padding crypto.PaddingLengthGenerator
457
461
if request .Option .Has (protocol .RequestOptionGlobalPadding ) {
458
- padding = sizeParser .(crypto.PaddingLengthGenerator )
462
+ var ok bool
463
+ padding , ok = sizeParser .(crypto.PaddingLengthGenerator )
464
+ if ! ok {
465
+ return nil , newError ("invalid option: RequestOptionGlobalPadding" )
466
+ }
459
467
}
460
468
461
469
switch request .Security {
462
470
case protocol .SecurityType_NONE :
463
471
if request .Option .Has (protocol .RequestOptionChunkStream ) {
464
472
if request .Command .TransferType () == protocol .TransferTypeStream {
465
- return crypto .NewChunkStreamWriter (sizeParser , writer )
473
+ return crypto .NewChunkStreamWriter (sizeParser , writer ), nil
466
474
}
467
475
468
476
auth := & crypto.AEADAuthenticator {
469
477
AEAD : new (NoOpAuthenticator ),
470
478
NonceGenerator : crypto .GenerateEmptyBytes (),
471
479
AdditionalDataGenerator : crypto .GenerateEmptyBytes (),
472
480
}
473
- return crypto .NewAuthenticationWriter (auth , sizeParser , writer , protocol .TransferTypePacket , padding )
481
+ return crypto .NewAuthenticationWriter (auth , sizeParser , writer , protocol .TransferTypePacket , padding ), nil
474
482
}
475
- return buf .NewWriter (writer )
483
+ return buf .NewWriter (writer ), nil
476
484
477
485
case protocol .SecurityType_LEGACY :
478
486
if request .Option .Has (protocol .RequestOptionChunkStream ) {
@@ -481,9 +489,9 @@ func (s *ServerSession) EncodeResponseBody(request *protocol.RequestHeader, writ
481
489
NonceGenerator : crypto .GenerateEmptyBytes (),
482
490
AdditionalDataGenerator : crypto .GenerateEmptyBytes (),
483
491
}
484
- return crypto .NewAuthenticationWriter (auth , sizeParser , s .responseWriter , request .Command .TransferType (), padding )
492
+ return crypto .NewAuthenticationWriter (auth , sizeParser , s .responseWriter , request .Command .TransferType (), padding ), nil
485
493
}
486
- return & buf.SequentialWriter {Writer : s .responseWriter }
494
+ return & buf.SequentialWriter {Writer : s .responseWriter }, nil
487
495
488
496
case protocol .SecurityType_AES128_GCM :
489
497
aead := crypto .NewAesGcm (s .responseBodyKey [:])
@@ -503,7 +511,7 @@ func (s *ServerSession) EncodeResponseBody(request *protocol.RequestHeader, writ
503
511
}
504
512
sizeParser = NewAEADSizeParser (lengthAuth )
505
513
}
506
- return crypto .NewAuthenticationWriter (auth , sizeParser , writer , request .Command .TransferType (), padding )
514
+ return crypto .NewAuthenticationWriter (auth , sizeParser , writer , request .Command .TransferType (), padding ), nil
507
515
508
516
case protocol .SecurityType_CHACHA20_POLY1305 :
509
517
aead , _ := chacha20poly1305 .New (GenerateChacha20Poly1305Key (s .responseBodyKey [:]))
@@ -525,9 +533,9 @@ func (s *ServerSession) EncodeResponseBody(request *protocol.RequestHeader, writ
525
533
}
526
534
sizeParser = NewAEADSizeParser (lengthAuth )
527
535
}
528
- return crypto .NewAuthenticationWriter (auth , sizeParser , writer , request .Command .TransferType (), padding )
536
+ return crypto .NewAuthenticationWriter (auth , sizeParser , writer , request .Command .TransferType (), padding ), nil
529
537
530
538
default :
531
- panic ( "Unknown security type. " )
539
+ return nil , newError ( "invalid option: Security " )
532
540
}
533
541
}
0 commit comments