Skip to content

Commit bfae843

Browse files
committed
Add the ability to specific SSL cert and key files for library
1 parent 7d8bdd6 commit bfae843

2 files changed

Lines changed: 17 additions & 2 deletions

File tree

README.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,7 @@ let ctx = newMqttCtx("nmqttClient")
178178
ctx.set_host("test.mosquitto.org", 1883)
179179
#ctx.set_auth("username", "password")
180180
#ctx.set_ping_interval(30)
181+
#ctx.set_ssl_certificates("cert.crt", "private.key")
181182
182183
proc mqttSub() {.async.} =
183184
await ctx.start()
@@ -247,6 +248,15 @@ proc set_ping_interval*(ctx: MqttCtx, txInterval: int) =
247248

248249
Set the clients ping interval in seconds. Default is 60 seconds.
249250

251+
____
252+
253+
### set_ssl_certificates*
254+
255+
```nim
256+
proc set_ping_interval*(ctx: MqttCtx, sslCertFile: string, sslKeyFile: string) =
257+
```
258+
259+
Sets the SSL Certificate and Key files to use Mutual TLS authentication
250260

251261
____
252262

nmqtt.nim

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ type
2525
host: string
2626
port: Port
2727
sslOn: bool
28+
sslCertFile: string
29+
sslKeyFile: string
2830
verbosity: int
2931
beenConnected: bool
3032
username: string
@@ -1045,7 +1047,7 @@ proc connectBroker(ctx: MqttCtx) {.async.} =
10451047
ctx.s = await asyncnet.dial(ctx.host, ctx.port)
10461048
if ctx.sslOn:
10471049
when defined(ssl):
1048-
ctx.ssl = newContext(protSSLv23, CVerifyNone)
1050+
ctx.ssl = newContext(protSSLv23, CVerifyNone, ctx.sslCertFile, ctx.sslKeyFile)
10491051
wrapConnectedSocket(ctx.ssl, ctx.s, handshakeAsClient)
10501052
else:
10511053
ctx.wrn "Requested SSL session but ssl is not enabled"
@@ -1101,7 +1103,10 @@ proc set_host*(ctx: MqttCtx, host: string, port: int=1883, sslOn=false) =
11011103
## Set the MQTT host
11021104
ctx.host = host
11031105
ctx.port = Port(port)
1104-
ctx.sslOn = sslOn
1106+
1107+
proc set_ssl_certificates*(ctx: MqttCtx, sslCertFile: string, sslKeyFile: string) =
1108+
ctx.sslCertFile = sslCertFile
1109+
ctx.sslKeyFile = sslKeyFile
11051110

11061111
proc set_auth*(ctx: MqttCtx, username: string, password: string) =
11071112
## Set the authentication for the host.

0 commit comments

Comments
 (0)