mirror of
https://github.com/nim-lang/Nim.git
synced 2026-02-14 15:23:27 +00:00
net.nim: support storing arbitrary data inside SSLContext
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
|
||||
@@ -243,6 +243,20 @@ when defined(ssl):
|
||||
newCTX.loadCertificates(certFile, keyFile)
|
||||
return SSLContext(newCTX)
|
||||
|
||||
proc getSslContextExtraDataIndex*(): cint =
|
||||
## Retrieves unique index for storing extra data in SSLContext.
|
||||
return SSL_CTX_get_ex_new_index(0, nil, nil, nil, nil)
|
||||
|
||||
proc setExtraData*(ctx: SSLContext, index: cint, data: pointer) =
|
||||
## Stores arbitrary data inside SSLContext. The unique `index`
|
||||
## should be retrieved using getSslContextExtraDataIndex.
|
||||
if SslCtx(ctx).SSL_CTX_set_ex_data(index, data) == -1:
|
||||
raiseSSLError()
|
||||
|
||||
proc getExtraData*(ctx: SSLContext, index: cint): pointer =
|
||||
## Retrieves arbitrary data stored inside SSLContext.
|
||||
return SslCtx(ctx).SSL_CTX_get_ex_data(index)
|
||||
|
||||
proc wrapSocket*(ctx: SSLContext, socket: Socket) =
|
||||
## Wraps a socket in an SSL context. This function effectively turns
|
||||
## ``socket`` into an SSL socket.
|
||||
|
||||
@@ -216,6 +216,10 @@ proc SSL_CTX_use_PrivateKey_file*(ctx: SslCtx,
|
||||
proc SSL_CTX_check_private_key*(ctx: SslCtx): cInt{.cdecl, dynlib: DLLSSLName,
|
||||
importc.}
|
||||
|
||||
proc SSL_CTX_get_ex_new_index*(argl: clong, argp: pointer, new_func: pointer, dup_func: pointer, free_func: pointer): cint {.cdecl, dynlib: DLLSSLName, importc.}
|
||||
proc SSL_CTX_set_ex_data*(ssl: SslCtx, idx: cint, arg: pointer): cint {.cdecl, dynlib: DLLSSLName, importc.}
|
||||
proc SSL_CTX_get_ex_data*(ssl: SslCtx, idx: cint): pointer {.cdecl, dynlib: DLLSSLName, importc.}
|
||||
|
||||
proc SSL_set_fd*(ssl: SslPtr, fd: SocketHandle): cint{.cdecl, dynlib: DLLSSLName, importc.}
|
||||
|
||||
proc SSL_shutdown*(ssl: SslPtr): cInt{.cdecl, dynlib: DLLSSLName, importc.}
|
||||
|
||||
Reference in New Issue
Block a user