Add secio oversized payload fragmentation

Cannot be used because Connection Read size hard limit tho
This commit is contained in:
Giovanni Petrantoni 2020-03-25 16:53:35 +09:00
parent 1550bea1cc
commit b132eae939
1 changed files with 23 additions and 14 deletions

View File

@ -202,20 +202,29 @@ method readMessage(sconn: SecioConn): Future[seq[byte]] {.async.} =
method writeMessage(sconn: SecioConn, message: seq[byte]) {.async.} = method writeMessage(sconn: SecioConn, message: seq[byte]) {.async.} =
## Write message ``message`` to secure connection ``sconn``. ## Write message ``message`` to secure connection ``sconn``.
try:
var
left = message.len
offset = 0
while left > 0:
let
chunkSize = if left > SecioMaxMessageSize: SecioMaxMessageSize else: left
let macsize = sconn.writerMac.sizeDigest() let macsize = sconn.writerMac.sizeDigest()
var msg = newSeq[byte](len(message) + 4 + macsize) var msg = newSeq[byte](chunkSize + 4 + macsize)
sconn.writerCoder.encrypt(message, msg.toOpenArray(4, 4 + len(message) - 1)) sconn.writerCoder.encrypt(message.toOpenArray(offset, offset + chunkSize - 1),
let mo = 4 + len(message) msg.toOpenArray(4, 4 + chunkSize - 1))
sconn.writerMac.update(msg.toOpenArray(4, 4 + len(message) - 1)) left = left - chunkSize
offset = offset + chunkSize
let mo = 4 + chunkSize
sconn.writerMac.update(msg.toOpenArray(4, 4 + chunkSize - 1))
sconn.writerMac.finish(msg.toOpenArray(mo, mo + macsize - 1)) sconn.writerMac.finish(msg.toOpenArray(mo, mo + macsize - 1))
sconn.writerMac.reset() sconn.writerMac.reset()
let length = len(message) + macsize let length = chunkSize + macsize
msg[0] = byte((length shr 24) and 0xFF) msg[0] = byte((length shr 24) and 0xFF)
msg[1] = byte((length shr 16) and 0xFF) msg[1] = byte((length shr 16) and 0xFF)
msg[2] = byte((length shr 8) and 0xFF) msg[2] = byte((length shr 8) and 0xFF)
msg[3] = byte(length and 0xFF) msg[3] = byte(length and 0xFF)
trace "Writing message", message = msg.shortLog trace "Writing message", message = msg.shortLog, left, offset
try:
await sconn.write(msg) await sconn.write(msg)
except AsyncStreamWriteError: except AsyncStreamWriteError:
trace "Could not write to connection" trace "Could not write to connection"