Skip to content

Commit fb46814

Browse files
authored
Merge pull request #30 from keslerm/tls-auth
Add the ability to specific SSL cert and key files for library Thank you @keslerm
2 parents 7d8bdd6 + 39361aa commit fb46814

2 files changed

Lines changed: 19 additions & 1 deletion

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_ssl_certificates*(ctx: MqttCtx, sslCert: string, sslKey: string) =
257+
```
258+
259+
Sets the SSL Certificate and Key files to use Mutual TLS authentication
250260

251261
____
252262

nmqtt.nim

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ type
2525
host: string
2626
port: Port
2727
sslOn: bool
28+
sslCert: string
29+
sslKey: 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.sslCert, ctx.sslKey)
10491051
wrapConnectedSocket(ctx.ssl, ctx.s, handshakeAsClient)
10501052
else:
10511053
ctx.wrn "Requested SSL session but ssl is not enabled"
@@ -1103,6 +1105,12 @@ proc set_host*(ctx: MqttCtx, host: string, port: int=1883, sslOn=false) =
11031105
ctx.port = Port(port)
11041106
ctx.sslOn = sslOn
11051107

1108+
proc set_ssl_certificates*(ctx: MqttCtx, sslCert: string, sslKey: string) =
1109+
# Sets the SSL Certificate and Key to use when connecting to the remote broker
1110+
# for mutal TLS authentication
1111+
ctx.sslCert = sslCert
1112+
ctx.sslKey = sslKey
1113+
11061114
proc set_auth*(ctx: MqttCtx, username: string, password: string) =
11071115
## Set the authentication for the host.
11081116
ctx.username = username

0 commit comments

Comments
 (0)