Skip to content

PSK broken in net.nim, pskClientCallback can't access extraInternal #11280

@lullius

Description

@lullius

Setting clientGetPskFunc= on SSLContext ctx and retrieving it works as expected.

Example

import net


proc clientFunc(hint: string): tuple[identity: string, psk: string] =
    echo "identity hint: ", hint
    return ("identity", "psk")

var ctx = newContext()
ctx.clientGetPskFunc=clientFunc
var f = ctx.clientGetPskFunc
echo f("my_hint")

Output

identity hint: my_hint
(identity: "identity", psk: "psk")

But when trying to wrap a socket, pskClientCallback in net.nim can't access the context's SslContextExtraInternal. It gets the context like this:

    let ctx = SSLContext(context: ssl.SSL_get_SSL_CTX)

Then it tries to get the identity and psk:

    let (identityString, pskString) = (ctx.clientGetPskFunc)(hintString)

clientGetPskFunc calls getExtraInternal which returns ctx.extraInternal, but the context's extraInternal was never set.

Running the server and client in this example or running this code results in a SIGSEGV:

Example

import net


proc clientFunc(hint: string): tuple[identity: string, psk: string] =
    echo "identity hint: ", hint
    return ("identity", "psk")

var ctx = newContext()
ctx.clientGetPskFunc=clientFunc
let sock = newSocket()
sock.connect("localhost", Port(8800))
ctx.wrapConnectedSocket(sock, handshakeAsClient)

Output from this client:

Current Output

Traceback (most recent call last)
pskclient.nim(15)        pskclient
net.nim(674)             wrapConnectedSocket
net.nim(587)             pskClientCallback
net.nim(581)             clientGetPskFunc
SIGSEGV: Illegal storage access. (Attempt to read from nil?)

Expected Output

Should connect using PSK.

If I hardcode the identity and psk directly into pskClientCallback everything works as expected.

I'm not sure, but this issue may be related to something dom96 mentioned in #4565. Comment:

There is a reason that assert is there, fixing that reason isn't trivial: https://github.com/nim-lang/Nim/pull/4565/files#diff-df7c4ae903eab01bc8c4f64f425b48c3R374. IIRC the problem is that the PSK client callback doesn't have access to the underlying SSL context and giving it access is also not so easy.

Everything above was compiled with -d:ssl.

Additional Information

$ nim -v
Nim Compiler Version 0.19.9 [Linux: amd64]
Compiled at 2019-05-19

$ uname -r
5.0.13-arch1-1-ARCH

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions