/// NTLMv2, the challenge-response half of NTLM.
///
/// Only NTLMv2 is implemented. NTLMv1 and LM are not offered: both are weaker
/// than the protocol they replaced and every supported server negotiates v2.
///
/// The structure follows MS-NLMP: the NT hash is MD4 over the UTF-16LE
/// password, the v2 hash mixes in the uppercased user and the domain, and the
/// response is HMAC-MD5 over the server challenge plus a client blob.
///|
/// NT hash: MD4 of the UTF-16LE password. This is the value a domain
/// controller stores, so it must never be logged or written to disk.
pub fn nt_hash(password : String) -> Bytes {
@crypto.md4(@utf16.encode(password, endianness=Little))
}
///|
/// NTLMv2 hash: HMAC-MD5 over `UPPER(user) + domain`, keyed by the NT hash.
/// Both strings are UTF-16LE, and the user name is uppercased with the
/// Unicode rules, not the ASCII ones.
pub fn ntlmv2_hash(user : String, domain : String, password : String) -> Bytes {
let identity = @crypto.concat(
@utf16.encode(user.to_upper(), endianness=Little),
@utf16.encode(domain, endianness=Little),
)
@crypto.hmac_md5(nt_hash(password), identity)
}
///|
/// Length of the AV pair sequence, excluding a terminating end-of-list pair.
///
/// Pairs are walked by their own length fields, so a pair whose value happens
/// to end in zero bytes (`MsvAvFlags = 0`, say) is not mistaken for the
/// terminator. Truncated input is returned as-is; validating the server's
/// target info is the server's job, not this function's.
fn av_pairs_len(info : Bytes) -> Int {
let mut i = 0
while i + 4 <= info.length() {
let id = info[i].to_int() | (info[i + 1].to_int() << 8)
let len = info[i + 2].to_int() | (info[i + 3].to_int() << 8)
if id == 0 && len == 0 {
return i
}
i = i + 4 + len
}
info.length()
}
///|
fn take_prefix(data : Bytes, n : Int) -> Bytes {
let out = FixedArray::make(n, b'\x00')
for i = 0; i < n; i = i + 1 {
out[i] = data[i]
}
FixedArray::unsafe_reinterpret_as_bytes(out)
}
///|
fn copy_range(data : Bytes, start : Int, length : Int) -> Bytes {
let out = FixedArray::make(length, b'\x00')
for i = 0; i < length; i = i + 1 {
out[i] = data[start + i]
}
FixedArray::unsafe_reinterpret_as_bytes(out)
}
///|
/// One AV pair: 2-byte id, 2-byte length, value.
fn av_pair_bytes(id : Int, value : Bytes) -> Bytes {
let head = FixedArray::make(4, b'\x00')
head[0] = (id & 0xFF).to_byte()
head[1] = ((id >> 8) & 0xFF).to_byte()
head[2] = (value.length() & 0xFF).to_byte()
head[3] = ((value.length() >> 8) & 0xFF).to_byte()
@crypto.concat(FixedArray::unsafe_reinterpret_as_bytes(head), value)
}
///|
/// `MsvAvFlags` (AV id 6) from a target-info buffer, or 0 when absent.
///
/// The buffer may or may not end with the end-of-list pair; walking by the
/// pairs' own lengths makes that difference irrelevant.
pub fn av_flags_of(target_info : Bytes) -> UInt {
av_flags_at(target_info, 0)
}
///|
fn av_flags_at(data : Bytes, start : Int) -> UInt {
let mut at = start
while at + 4 <= data.length() {
let id = data[at].to_int() | (data[at + 1].to_int() << 8)
let len = data[at + 2].to_int() | (data[at + 3].to_int() << 8)
if id == 0 {
break
}
if id == 6 && len == 4 && at + 8 <= data.length() {
return @crypto.read_u32le(data, at + 4)
}
at = at + 4 + len
}
0U
}
///|
/// `MsvAvTimestamp` (AV id 7) from a target-info buffer, or `None`.
pub fn av_timestamp_of(target_info : Bytes) -> UInt64? {
let mut at = 0
while at + 4 <= target_info.length() {
let id = target_info[at].to_int() | (target_info[at + 1].to_int() << 8)
let len = target_info[at + 2].to_int() | (target_info[at + 3].to_int() << 8)
if id == 0 {
break
}
if id == 7 && len == 8 && at + 12 <= target_info.length() {
return Some(@crypto.read_u64le(target_info, at + 4))
}
at = at + 4 + len
}
None
}
///|
/// Set `MsvAvFlags` in a target-info buffer, replacing an existing pair and
/// dropping the end-of-list pair.
///
/// MS-NLMP 3.1.5.2.1 asks the client to set bit 0x2 of this pair when it is
/// going to send a MIC; the flag lives in the blob's AV pairs so that the
/// server can tell a signed answer from an unsigned one. Returning the pairs
/// without the terminator is what `make_blob` wants, and adding the pair here
/// means the NT proof covers it.
pub fn with_av_flags(target_info : Bytes, flags : UInt) -> Bytes {
let end = av_pairs_len(target_info)
let pairs = if end == target_info.length() {
target_info
} else {
take_prefix(target_info, end)
}
let mut kept : Bytes = b""
let mut at = 0
while at + 4 <= pairs.length() {
let id = pairs[at].to_int() | (pairs[at + 1].to_int() << 8)
let len = pairs[at + 2].to_int() | (pairs[at + 3].to_int() << 8)
let next = at + 4 + len
if next > pairs.length() {
break
}
if id != 6 {
kept = @crypto.concat(kept, copy_range(pairs, at, next - at))
}
at = next
}
@crypto.concat(kept, av_pair_bytes(6, u32le(flags)))
}
///|
/// Whether the blob inside an NTLMv2 response says a MIC is present.
///
/// MS-NLMP 3.2.5.1.2: an AUTHENTICATE_MESSAGE indicates the presence of a MIC
/// when its target info has `MsvAvFlags` with bit 0x2 set. A server that only
/// looks at its own configuration never checks a MIC it did not ask for, which
/// is the shape of CVE-2019-1040; the flag is the message's own claim and has
/// to be honoured.
pub fn blob_claims_mic(nt_response : Bytes) -> Bool {
// 16-byte proof, then a 28-byte blob header before the AV pairs.
if nt_response.length() < 48 {
return false
}
(av_flags_at(nt_response, 44) & 0x2U) != 0U
}
///|
/// The client blob from MS-NLMP 3.3.2: a 28-byte header, the server's target
/// info, an end-of-list pair, and four reserved bytes.
///
/// `target_info` may arrive with or without its terminator; it is normalised
/// here so callers can pass `Type2::av_pairs()` or the raw buffer from the
/// wire and get the same blob either way.
pub fn make_blob(
timestamp : UInt64,
client_challenge : Bytes,
target_info : Bytes,
) -> Bytes {
if client_challenge.length() != 8 {
abort("make_blob: the client challenge must be 8 bytes")
}
let pairs_len = av_pairs_len(target_info)
let pairs = if pairs_len == target_info.length() {
target_info
} else {
take_prefix(target_info, pairs_len)
}
// 28-byte header + AV pairs + 4-byte EOL + 4-byte Reserved4.
let out = FixedArray::make(28 + pairs.length() + 8, b'\x00')
out[0] = b'\x01'
out[1] = b'\x01'
// Bytes 2..8 are reserved and stay zero.
for i = 0; i < 8; i = i + 1 {
out[8 + i] = ((timestamp >> (i * 8)) & 0xFFUL).to_byte()
}
for i = 0; i < 8; i = i + 1 {
out[16 + i] = client_challenge[i]
}
// Bytes 24..28 are reserved and stay zero.
for i = 0; i < pairs.length(); i = i + 1 {
out[28 + i] = pairs[i]
}
// The end-of-list pair and Reserved4 are both four zero bytes, already
// there from the initialiser.
FixedArray::unsafe_reinterpret_as_bytes(out)
}
///|
/// NTLMv2 response: `HMAC-MD5(v2 hash, server challenge + blob) || blob`.
pub fn ntlmv2_response(
user : String,
domain : String,
password : String,
server_challenge : Bytes,
blob : Bytes,
) -> Bytes {
let proof = @crypto.hmac_md5(
ntlmv2_hash(user, domain, password),
@crypto.concat(server_challenge, blob),
)
@crypto.concat(proof, blob)
}
///|
/// LMv2 response: `HMAC-MD5(v2 hash, server challenge + client challenge) || client challenge`.
/// Still sent by some clients for compatibility; it proves the same secret
/// without protecting any payload.
pub fn lmv2_response(
user : String,
domain : String,
password : String,
server_challenge : Bytes,
client_challenge : Bytes,
) -> Bytes {
@crypto.concat(
@crypto.hmac_md5(
ntlmv2_hash(user, domain, password),
@crypto.concat(server_challenge, client_challenge),
),
client_challenge,
)
}
///|
/// Session base key: HMAC-MD5 over the NT proof, keyed by the v2 hash.
/// The key exchange in the type 3 message starts from this value.
pub fn session_base_key(
user : String,
domain : String,
password : String,
nt_proof : Bytes,
) -> Bytes {
@crypto.hmac_md5(ntlmv2_hash(user, domain, password), nt_proof)
}
///|
/// MIC over the three messages, keyed by the exported session key.
///
/// MS-NLMP 3.1.5.1.2: the HMAC covers NEGOTIATE, CHALLENGE and AUTHENTICATE,
/// and the MIC field inside AUTHENTICATE is zero while it is computed. The
/// caller passes the message already in that state; `encode_type3_with_mic`
/// with an empty MIC produces exactly such a message.
pub fn compute_mic(
session_key : Bytes,
type1 : Bytes,
type2 : Bytes,
type3 : Bytes,
) -> Bytes {
@crypto.hmac_md5(
session_key,
@crypto.concat(type1, @crypto.concat(type2, type3)),
)
}
///|
/// Check the MIC a client put in its authenticate message.
///
/// The session key is the exported session key: the session base key when no
/// key exchange was negotiated, which is the only mode this library drives.
pub fn verify_mic(
session_key : Bytes,
type1 : Bytes,
type2 : Bytes,
type3 : Bytes,
) -> Bool {
if type3.length() < 80 {
return false
}
// Where the MIC sits depends on whether the optional Version field is
// present, and the payload offset is the only reliable evidence: the
// NEGOTIATE_VERSION flag is set by peers that then omit the field.
let mic_at = match mic_offset(type3) {
Some(at) => at
None => return false
}
if type3.length() < mic_at + 16 {
return false
}
let zeroed = FixedArray::make(type3.length(), b'\x00')
for i = 0; i < type3.length(); i = i + 1 {
zeroed[i] = type3[i]
}
for i = 0; i < 16; i = i + 1 {
zeroed[mic_at + i] = b'\x00'
}
let expected = compute_mic(
session_key,
type1,
type2,
FixedArray::unsafe_reinterpret_as_bytes(zeroed),
)
let mut same = true
for i = 0; i < 16; i = i + 1 {
if type3[mic_at + i] != expected[i] {
same = false
}
}
same
}
///|
/// Server-side check: recompute the proof over the blob the client sent and
/// compare. Written for the local mock server and for the tests; a real
/// deployment would do this against the stored NT hash instead of a password.
pub fn verify_response(
user : String,
domain : String,
password : String,
server_challenge : Bytes,
response : Bytes,
) -> Bool {
if response.length() <= 16 {
return false
}
let proof = FixedArray::make(16, b'\x00')
for i = 0; i < 16; i = i + 1 {
proof[i] = response[i]
}
let blob = FixedArray::make(response.length() - 16, b'\x00')
for i = 0; i < blob.length(); i = i + 1 {
blob[i] = response[16 + i]
}
let expected = @crypto.hmac_md5(
ntlmv2_hash(user, domain, password),
@crypto.concat(
server_challenge,
FixedArray::unsafe_reinterpret_as_bytes(blob),
),
)
let mut same = true
for i = 0; i < 16; i = i + 1 {
if expected[i] != proof[i] {
same = false
}
}
same
}