Skip to content

Commit 4539fd8

Browse files
committed
Don't use bcrypt on Windows
1 parent 32e0058 commit 4539fd8

4 files changed

Lines changed: 24 additions & 10 deletions

File tree

nmqtt.nim

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -712,9 +712,16 @@ proc onConnect(ctx: MqttCtx, pkt: Pkt) {.async.} =
712712
# Check password and username
713713
if mqttbroker.passwords.len() > 0:
714714
let pass = mqttbroker.passwords.getOrDefault(ctx.username)
715-
if pass == "" or pass[0..59] != makePassword(ctx.password, pass[60..pass.len-1], pass[0..59]):
716-
await denyConnect(ctx, ConnRefBadUserPwd)
717-
return
715+
when defined(Windows):
716+
## TODO: Windows is using MD5 for storing the password, which is not
717+
## safe in any way.
718+
if pass == "" or pass[0..31] != makePassword(ctx.password, pass[32..pass.len-1], ""):
719+
await denyConnect(ctx, ConnRefBadUserPwd)
720+
return
721+
else:
722+
if pass == "" or pass[0..59] != makePassword(ctx.password, pass[60..pass.len-1], pass[0..59]):
723+
await denyConnect(ctx, ConnRefBadUserPwd)
724+
return
718725

719726
# 3.1.2.2 Protocol Level
720727
if ctx.proto != "MQTT":

nmqtt.nimble

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,10 @@ skipDirs = @["tests", "nmqtt"]
1111

1212
# Dependencies
1313
requires "nim >= 1.0.6"
14-
requires "bcrypt >= 0.2.1"
1514
requires "cligen >= 0.9.45"
15+
when not defined(Windows):
16+
requires "bcrypt >= 0.2.1"
17+
1618

1719
from strutils import format
1820

nmqtt/nmqtt_password.nim

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,9 @@ proc nmqttPassword(adduser=false, batch=false, deluser=false, args: seq[string])
5757
## Main handler
5858
echo "Running nmqtt_password v" & nmqttVersion
5959

60+
when defined(Windows):
61+
echo "\nWARNING: On Windows passwords will only be hashed with MD5.\n"
62+
6063
if args.len() == 0:
6164
echo "Error, missing parameters. Run again with --help."
6265
quit()

nmqtt/utils/passwords.nim

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,19 @@
1-
import md5, bcrypt, random
1+
import md5, random
2+
3+
when not defined(Windows):
4+
import bcrypt
25

36

47
var urandom: File
58
let useUrandom = urandom.open("/dev/urandom")
69

710

8-
template makeSessionKey*(): string =
9-
## Creates a random key to be used to authorize a session.
10-
bcrypt.hash(makeSalt(), genSalt(8))
11-
1211
template makePassword*(password, salt: string, comparingTo = ""): string =
1312
## Creates an MD5 hash by combining password and salt.
14-
bcrypt.hash(getMD5(salt & getMD5(password)), if comparingTo != "": comparingTo else: genSalt(8))
13+
when defined(Windows):
14+
getMD5(salt & getMD5(password))
15+
else:
16+
bcrypt.hash(getMD5(salt & getMD5(password)), if comparingTo != "": comparingTo else: genSalt(8))
1517

1618
proc makeSalt*(): string =
1719
## Generate random salt. Uses cryptographically secure /dev/urandom

0 commit comments

Comments
 (0)