#!/bin/sh
# Build a SIP digest authentication (RFC 2617, MD5) for a known password and
# check that "sippts dcrack" recovers it from a wordlist.
set -e

WORKDIR="${AUTOPKGTEST_TMP:-$(mktemp -d)}"
cd "$WORKDIR"

md5() { printf '%s' "$1" | md5sum | cut -d' ' -f1; }

SIP_USER=alice
REALM=asterisk
PASS=s3cr3tP4ss
METHOD=REGISTER
URI=sip:127.0.0.1
NONCE=4b1b9c2e7f3a

HA1=$(md5 "${SIP_USER}:${REALM}:${PASS}")
HA2=$(md5 "${METHOD}:${URI}")
RESPONSE=$(md5 "${HA1}:${NONCE}:${HA2}")

# ipsrc"ipdst"username"realm"method"uri"nonce"cnonce"nc"qop"algorithm"response
printf '%s"%s"%s"%s"%s"%s"%s"%s"%s"%s"%s"%s\n' \
    127.0.0.1 127.0.0.2 "$SIP_USER" "$REALM" "$METHOD" "$URI" "$NONCE" \
    "" "" "" MD5 "$RESPONSE" > dump.txt
printf 'password\n123456\n%s\nadmin\n' "$PASS" > wordlist.txt

sippts dcrack -f dump.txt -w wordlist.txt -nocolor -oj results.json

cat results.json

grep -q '"username": "alice"' results.json
grep -q '"password": "s3cr3tP4ss"' results.json
