mirror of
https://github.com/nim-lang/Nim.git
synced 2026-06-09 21:28:13 +00:00
Merge branch 'tls-psk' of https://github.com/zielmicha/nim into zielmicha-tls-psk
This commit is contained in:
14
examples/ssl/extradata.nim
Normal file
14
examples/ssl/extradata.nim
Normal file
@@ -0,0 +1,14 @@
|
||||
# Stores extra data inside the SSL context.
|
||||
import net
|
||||
|
||||
# Our unique index for storing foos
|
||||
let fooIndex = getSslContextExtraDataIndex()
|
||||
# And another unique index for storing foos
|
||||
let barIndex = getSslContextExtraDataIndex()
|
||||
echo "got indexes ", fooIndex, " ", barIndex
|
||||
|
||||
let ctx = newContext()
|
||||
assert ctx.getExtraData(fooIndex) == nil
|
||||
let foo: int = 5
|
||||
ctx.setExtraData(fooIndex, cast[pointer](foo))
|
||||
assert cast[int](ctx.getExtraData(fooIndex)) == foo
|
||||
16
examples/ssl/pskclient.nim
Normal file
16
examples/ssl/pskclient.nim
Normal file
@@ -0,0 +1,16 @@
|
||||
# Create connection encrypted using preshared key (TLS-PSK).
|
||||
import net
|
||||
|
||||
static: assert defined(ssl)
|
||||
|
||||
let sock = newSocket()
|
||||
sock.connect("localhost", Port(8800))
|
||||
|
||||
proc clientFunc(identityHint: string): tuple[identity: string, psk: string] =
|
||||
echo "identity hint ", identityHint.repr
|
||||
return ("foo", "psk-of-foo")
|
||||
|
||||
let context = newContext(cipherList="PSK-AES256-CBC-SHA")
|
||||
context.clientGetPskFunc = clientFunc
|
||||
context.wrapConnectedSocket(sock, handshakeAsClient)
|
||||
context.destroyContext()
|
||||
20
examples/ssl/pskserver.nim
Normal file
20
examples/ssl/pskserver.nim
Normal file
@@ -0,0 +1,20 @@
|
||||
# Accept connection encrypted using preshared key (TLS-PSK).
|
||||
import net
|
||||
|
||||
static: assert defined(ssl)
|
||||
|
||||
let sock = newSocket()
|
||||
sock.bindAddr(Port(8800))
|
||||
sock.listen()
|
||||
|
||||
let context = newContext(cipherList="PSK-AES256-CBC-SHA")
|
||||
context.pskIdentityHint = "hello"
|
||||
context.serverGetPskFunc = proc(identity: string): string = "psk-of-" & identity
|
||||
|
||||
while true:
|
||||
var client = new(Socket)
|
||||
sock.accept(client)
|
||||
sock.setSockOpt(OptReuseAddr, true)
|
||||
echo "accepted connection"
|
||||
context.wrapConnectedSocket(client, handshakeAsServer)
|
||||
echo "got connection with identity ", client.getPskIdentity()
|
||||
Reference in New Issue
Block a user