2019-09-06 06:51:46 +00:00
|
|
|
## Nim-LibP2P
|
2019-09-24 17:48:23 +00:00
|
|
|
## Copyright (c) 2019 Status Research & Development GmbH
|
2019-09-06 06:51:46 +00:00
|
|
|
## Licensed under either of
|
|
|
|
## * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE))
|
|
|
|
## * MIT license ([LICENSE-MIT](LICENSE-MIT))
|
|
|
|
## at your option.
|
|
|
|
## This file may not be copied, modified, or distributed except according to
|
|
|
|
## those terms.
|
|
|
|
|
2020-09-06 08:31:47 +00:00
|
|
|
import std/[options, strformat]
|
2020-07-07 11:14:11 +00:00
|
|
|
import chronos, chronicles, bearssl
|
2020-03-09 17:27:57 +00:00
|
|
|
import ../protocol,
|
2020-05-07 20:37:46 +00:00
|
|
|
../../stream/streamseq,
|
2020-06-19 17:29:43 +00:00
|
|
|
../../stream/connection,
|
2020-06-24 15:08:44 +00:00
|
|
|
../../multiaddress,
|
2020-05-07 20:37:46 +00:00
|
|
|
../../peerinfo
|
2019-09-06 06:51:46 +00:00
|
|
|
|
2020-09-21 09:16:29 +00:00
|
|
|
export protocol
|
|
|
|
|
2020-06-20 10:56:55 +00:00
|
|
|
logScope:
|
|
|
|
topics = "secure"
|
|
|
|
|
2019-09-06 06:51:46 +00:00
|
|
|
type
|
|
|
|
Secure* = ref object of LPProtocol # base type for secure managers
|
2020-04-03 20:36:51 +00:00
|
|
|
|
2020-03-09 17:27:57 +00:00
|
|
|
SecureConn* = ref object of Connection
|
2020-06-19 17:29:43 +00:00
|
|
|
stream*: Connection
|
2020-05-07 20:37:46 +00:00
|
|
|
buf: StreamSeq
|
2020-03-09 17:27:57 +00:00
|
|
|
|
2020-09-06 08:31:47 +00:00
|
|
|
func shortLog*(conn: SecureConn): auto =
|
|
|
|
if conn.isNil: "SecureConn(nil)"
|
|
|
|
elif conn.peerInfo.isNil: $conn.oid
|
|
|
|
else: &"{shortLog(conn.peerInfo.peerId)}:{conn.oid}"
|
|
|
|
chronicles.formatIt(SecureConn): shortLog(it)
|
|
|
|
|
|
|
|
proc init*(T: type SecureConn,
|
|
|
|
conn: Connection,
|
|
|
|
peerInfo: PeerInfo,
|
|
|
|
observedAddr: Multiaddress,
|
|
|
|
timeout: Duration = DefaultConnectionTimeout): T =
|
|
|
|
result = T(stream: conn,
|
2020-06-24 15:08:44 +00:00
|
|
|
peerInfo: peerInfo,
|
|
|
|
observedAddr: observedAddr,
|
2020-08-10 22:17:11 +00:00
|
|
|
closeEvent: conn.closeEvent,
|
2020-10-27 17:21:03 +00:00
|
|
|
timeout: timeout,
|
|
|
|
dir: conn.dir)
|
2020-06-24 15:08:44 +00:00
|
|
|
result.initStream()
|
|
|
|
|
2020-06-19 17:29:43 +00:00
|
|
|
method initStream*(s: SecureConn) =
|
|
|
|
if s.objName.len == 0:
|
|
|
|
s.objName = "SecureConn"
|
|
|
|
|
|
|
|
procCall Connection(s).initStream()
|
|
|
|
|
|
|
|
method close*(s: SecureConn) {.async.} =
|
2020-10-27 17:21:03 +00:00
|
|
|
trace "closing secure conn", s, dir = s.dir
|
2020-06-19 17:29:43 +00:00
|
|
|
if not(isNil(s.stream)):
|
|
|
|
await s.stream.close()
|
|
|
|
|
2020-07-09 20:21:47 +00:00
|
|
|
await procCall Connection(s).close()
|
|
|
|
|
2020-03-09 17:27:57 +00:00
|
|
|
method readMessage*(c: SecureConn): Future[seq[byte]] {.async, base.} =
|
|
|
|
doAssert(false, "Not implemented!")
|
|
|
|
|
2020-03-04 19:45:14 +00:00
|
|
|
method handshake(s: Secure,
|
|
|
|
conn: Connection,
|
2020-04-23 01:27:29 +00:00
|
|
|
initiator: bool): Future[SecureConn] {.async, base.} =
|
2020-02-25 05:06:57 +00:00
|
|
|
doAssert(false, "Not implemented!")
|
|
|
|
|
2020-07-08 00:33:05 +00:00
|
|
|
proc handleConn*(s: Secure,
|
|
|
|
conn: Connection,
|
|
|
|
initiator: bool): Future[Connection] {.async, gcsafe.} =
|
2020-03-08 03:14:56 +00:00
|
|
|
var sconn = await s.handshake(conn, initiator)
|
2020-09-04 16:30:45 +00:00
|
|
|
|
|
|
|
proc cleanup() {.async.} =
|
|
|
|
try:
|
|
|
|
await conn.join()
|
|
|
|
await sconn.close()
|
|
|
|
except CatchableError as exc:
|
2020-09-06 08:31:47 +00:00
|
|
|
trace "error cleaning up secure connection", err = exc.msg, sconn
|
2020-09-04 16:30:45 +00:00
|
|
|
|
2020-07-09 08:53:19 +00:00
|
|
|
if not isNil(sconn):
|
2020-09-04 16:30:45 +00:00
|
|
|
# All the errors are handled inside `cleanup()` procedure.
|
|
|
|
asyncSpawn cleanup()
|
2020-02-25 05:06:57 +00:00
|
|
|
|
2020-06-24 15:08:44 +00:00
|
|
|
return sconn
|
2020-02-25 05:06:57 +00:00
|
|
|
|
|
|
|
method init*(s: Secure) {.gcsafe.} =
|
2020-07-07 11:14:11 +00:00
|
|
|
procCall LPProtocol(s).init()
|
|
|
|
|
2020-02-25 05:06:57 +00:00
|
|
|
proc handle(conn: Connection, proto: string) {.async, gcsafe.} =
|
2020-09-06 08:31:47 +00:00
|
|
|
trace "handling connection upgrade", proto, conn
|
2020-02-25 05:06:57 +00:00
|
|
|
try:
|
2020-07-08 00:33:05 +00:00
|
|
|
# We don't need the result but we
|
|
|
|
# definitely need to await the handshake
|
2020-03-24 06:34:02 +00:00
|
|
|
discard await s.handleConn(conn, false)
|
2020-09-06 08:31:47 +00:00
|
|
|
trace "connection secured", conn
|
2020-06-29 15:15:31 +00:00
|
|
|
except CancelledError as exc:
|
2020-09-06 08:31:47 +00:00
|
|
|
warn "securing connection canceled", conn
|
2020-06-29 15:15:31 +00:00
|
|
|
await conn.close()
|
2020-08-06 18:14:40 +00:00
|
|
|
raise exc
|
2020-02-25 05:06:57 +00:00
|
|
|
except CatchableError as exc:
|
2020-09-06 08:31:47 +00:00
|
|
|
warn "securing connection failed", err = exc.msg, conn
|
2020-05-23 16:51:54 +00:00
|
|
|
await conn.close()
|
2020-02-25 05:06:57 +00:00
|
|
|
|
|
|
|
s.handler = handle
|
|
|
|
|
2020-07-08 00:33:05 +00:00
|
|
|
method secure*(s: Secure,
|
|
|
|
conn: Connection,
|
|
|
|
initiator: bool):
|
2020-09-16 09:55:25 +00:00
|
|
|
Future[Connection] {.base, gcsafe.} =
|
|
|
|
s.handleConn(conn, initiator)
|
2020-05-07 20:37:46 +00:00
|
|
|
|
|
|
|
method readOnce*(s: SecureConn,
|
|
|
|
pbytes: pointer,
|
|
|
|
nbytes: int):
|
|
|
|
Future[int] {.async, gcsafe.} =
|
2020-09-16 09:55:25 +00:00
|
|
|
doAssert(nbytes > 0, "nbytes must be positive integer")
|
2020-06-29 15:15:31 +00:00
|
|
|
|
|
|
|
if s.buf.data().len() == 0:
|
2020-10-19 05:13:14 +00:00
|
|
|
let (buf, err) = try:
|
|
|
|
(await s.readMessage(), nil)
|
|
|
|
except CatchableError as exc:
|
|
|
|
(@[], exc)
|
|
|
|
|
|
|
|
if not isNil(err):
|
2020-10-21 01:08:24 +00:00
|
|
|
if not (err of LPStreamEOFError):
|
2020-10-27 17:21:03 +00:00
|
|
|
warn "error while reading message from secure connection, closing.",
|
|
|
|
error=err.name,
|
|
|
|
message=err.msg,
|
2020-10-21 01:08:24 +00:00
|
|
|
connection=s
|
2020-10-19 05:13:14 +00:00
|
|
|
await s.close()
|
|
|
|
raise err
|
2020-10-27 17:21:03 +00:00
|
|
|
|
2020-08-15 05:36:15 +00:00
|
|
|
s.activity = true
|
2020-10-19 05:13:14 +00:00
|
|
|
|
2020-06-29 15:15:31 +00:00
|
|
|
if buf.len == 0:
|
|
|
|
raise newLPStreamIncompleteError()
|
2020-10-27 17:21:03 +00:00
|
|
|
|
2020-06-29 15:15:31 +00:00
|
|
|
s.buf.add(buf)
|
|
|
|
|
|
|
|
var p = cast[ptr UncheckedArray[byte]](pbytes)
|
|
|
|
return s.buf.consumeTo(toOpenArray(p, 0, nbytes - 1))
|