summary refs log tree commit diff stats
diff options
context:
space:
mode:
-rw-r--r--examples/ssl/extradata.nim14
-rw-r--r--lib/pure/net.nim14
-rw-r--r--lib/wrappers/openssl.nim4
3 files changed, 32 insertions, 0 deletions
diff --git a/examples/ssl/extradata.nim b/examples/ssl/extradata.nim
new file mode 100644
index 000000000..f86dc57f2
--- /dev/null
+++ b/examples/ssl/extradata.nim
@@ -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
diff --git a/lib/pure/net.nim b/lib/pure/net.nim
index d1016011e..5498ebb7d 100644
--- a/lib/pure/net.nim
+++ b/lib/pure/net.nim
@@ -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.
diff --git a/lib/wrappers/openssl.nim b/lib/wrappers/openssl.nim
index 90610eb74..9f24ca58d 100644
--- a/lib/wrappers/openssl.nim
+++ b/lib/wrappers/openssl.nim
@@ -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.}