Fix dtlsRecv / read & add certificate request

This commit is contained in:
Ludovic Chenut 2023-11-14 16:02:44 +01:00
parent 6391a3f2e5
commit 1132a5e42d
No known key found for this signature in database
GPG Key ID: D9A59B1907F1D50C
2 changed files with 16 additions and 10 deletions

View File

@ -72,7 +72,7 @@ type
DataChannelConnection* = ref object DataChannelConnection* = ref object
readLoopFut: Future[void] readLoopFut: Future[void]
streams: Table[uint16, DataChannelStream] streams: Table[uint16, DataChannelStream]
conn: SctpConn conn*: SctpConn
incomingStreams: AsyncQueue[DataChannelStream] incomingStreams: AsyncQueue[DataChannelStream]
proc read*(stream: DataChannelStream): Future[seq[byte]] {.async.} = proc read*(stream: DataChannelStream): Future[seq[byte]] {.async.} =

View File

@ -67,12 +67,14 @@ proc dtlsSend*(ctx: pointer, buf: ptr byte, len: uint): cint {.cdecl.} =
result = len.cint result = len.cint
proc dtlsRecv*(ctx: pointer, buf: ptr byte, len: uint): cint {.cdecl.} = proc dtlsRecv*(ctx: pointer, buf: ptr byte, len: uint): cint {.cdecl.} =
trace "dtls receive", len let self = cast[DtlsConn](ctx)
var if self.dataRecv.len() == 0:
self = cast[DtlsConn](ctx) return MBEDTLS_ERR_SSL_WANT_READ
dataRecv = self.dataRecv.popFirstNoWait()
var dataRecv = self.dataRecv.popFirstNoWait()
copyMem(buf, addr dataRecv[0], dataRecv.len()) copyMem(buf, addr dataRecv[0], dataRecv.len())
result = dataRecv.len().cint result = dataRecv.len().cint
trace "dtls receive", len, result
proc init*(self: DtlsConn, conn: StunConn, laddr: TransportAddress) {.async.} = proc init*(self: DtlsConn, conn: StunConn, laddr: TransportAddress) {.async.} =
self.conn = conn self.conn = conn
@ -85,9 +87,12 @@ proc write*(self: DtlsConn, msg: seq[byte]) {.async.} =
proc read*(self: DtlsConn): Future[seq[byte]] {.async.} = proc read*(self: DtlsConn): Future[seq[byte]] {.async.} =
var res = newSeq[byte](8192) var res = newSeq[byte](8192)
while true:
let tmp = await self.dataRecv.popFirst() let tmp = await self.dataRecv.popFirst()
self.dataRecv.addFirstNoWait(tmp) self.dataRecv.addFirstNoWait(tmp)
let length = mbedtls_ssl_read(addr self.ssl, cast[ptr byte](addr res[0]), res.len().uint) let length = mbedtls_ssl_read(addr self.ssl, cast[ptr byte](addr res[0]), res.len().uint)
if length == MBEDTLS_ERR_SSL_WANT_READ:
continue
res.setLen(length) res.setLen(length)
return res return res
@ -236,6 +241,7 @@ proc accept*(self: Dtls): Future[DtlsConn] {.async.} =
mb_ssl_setup(res.ssl, res.config) mb_ssl_setup(res.ssl, res.config)
mb_ssl_session_reset(res.ssl) mb_ssl_session_reset(res.ssl)
mbedtls_ssl_set_verify(addr res.ssl, verify, cast[pointer](res)) mbedtls_ssl_set_verify(addr res.ssl, verify, cast[pointer](res))
mbedtls_ssl_conf_authmode(addr res.ssl, MBEDTLS_SSL_VERIFY_REQUIRED) # TODO: create template
mb_ssl_set_bio(res.ssl, cast[pointer](res), mb_ssl_set_bio(res.ssl, cast[pointer](res),
dtlsSend, dtlsRecv, nil) dtlsSend, dtlsRecv, nil)
while true: while true: