Derk2006/moonsso/src/ntlm does not have a README file

    MessageError

    pub suberror MessageError {
    Truncated(Int)
    BadSignature
    BadMessageType(Int)
    BadField(Int, Int, Int)
    }

    The three NTLM wire messages.

    Only the fields this project actually consumes are modelled. A type 2 message is parsed so the client can answer it; a type 3 message is parsed so a server (and the test suite) can check what a client sent. Encoding a type 1 and a type 2 is enough to drive a real client, which is how the implementation is tested against curl.

    Type2

    pub struct Type2 {
    flags : UInt
    server_challenge : Bytes
    target_name : Bytes
    target_info : Bytes
    }

    Type2::av_flags

    fn Type2::av_flags(self : Type2) -> UInt

    MsvAvFlags (AV id 6) from the challenge, or 0 when the server sent none.

    Bit 1 of the value tells the client to put a MIC in the authenticate message; a server that sets it and then reads the field is the only way this code path is exercised in practice.

    Type2::av_pairs

    fn Type2::av_pairs(self : Type2) -> Bytes

    Target info without the trailing end-of-list pair, ready for make_blob.

    The list is walked pair by pair rather than by looking at the last four bytes, so a final pair whose value is zero (MsvAvFlags = 0) survives.

    Type2::flags

    fn Type2::flags(self : Type2) -> UInt

    Type2::has_timestamp

    fn Type2::has_timestamp(self : Type2) -> Bool

    Whether the challenge carries MsvAvTimestamp (AV id 7).

    Type2::requires_mic

    fn Type2::requires_mic(self : Type2) -> Bool

    Whether the challenge asks for a MIC in the authenticate message.

    MS-NLMP 3.1.5.1.2 gives two signals. MsvAvFlags bit 1 is the explicit one; a MsvAvTimestamp pair is the other, and a server that sends one expects the answer to be signed even when the flag is absent. Treating only the flag as authoritative leaves those servers with an unsigned answer, so both are checked.

    Type2::server_challenge

    fn Type2::server_challenge(self : Type2) -> Bytes

    Type2::target_name

    fn Type2::target_name(self : Type2) -> Bytes

    Type2::timestamp

    fn Type2::timestamp(self : Type2) -> UInt64?

    MsvAvTimestamp (AV id 7) from the challenge, or None when absent.

    MS-NLMP 3.1.5.2.1: if this pair is present, the client must use its value as the timestamp of the NTLMv2 blob rather than the current time. A server that sends a timestamp checks the blob against it, so an answer built with a different time is rejected even when the password is right.

    Type3

    pub struct Type3 {
    flags : UInt
    lm_response : Bytes
    nt_response : Bytes
    domain : String
    user : String
    workstation : String
    mic : Bytes
    }

    Type3::domain

    fn Type3::domain(self : Type3) -> String

    Type3::flags

    fn Type3::flags(self : Type3) -> UInt

    Type3::lm_response

    fn Type3::lm_response(self : Type3) -> Bytes

    Type3::mic

    fn Type3::mic(self : Type3) -> Bytes

    The MIC carried by this message, or empty when it carries none.

    Type3::nt_response

    fn Type3::nt_response(self : Type3) -> Bytes

    Type3::user

    fn Type3::user(self : Type3) -> String

    Type3::workstation

    fn Type3::workstation(self : Type3) -> String

    acceptor_mech_list_mic

    fn acceptor_mech_list_mic(session_base_key : Bytes, flags : UInt, mech_list_der : Bytes) -> Bytes

    The mechListMIC an acceptor sends back, keyed with the server-to-client signing key.

    av_flags_of

    fn av_flags_of(target_info : Bytes) -> UInt

    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.

    av_timestamp_of

    fn av_timestamp_of(target_info : Bytes) -> UInt64?

    MsvAvTimestamp (AV id 7) from a target-info buffer, or None.

    blob_claims_mic

    fn blob_claims_mic(nt_response : Bytes) -> Bool

    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.

    can_sign

    fn can_sign(flags : UInt) -> Bool

    Whether this library can produce a signature under these flags.

    mac needs extended session security and refuses key exchange, because the checksum would have to be RC4-encrypted with a key derived from the encrypted random session key — a value this library does not negotiate. Callers that read flags off the wire use this to reject instead of tripping the assertion inside mac.

    compute_mic

    fn compute_mic(session_key : Bytes, type1 : Bytes, type2 : Bytes, type3 : Bytes) -> Bytes

    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.

    encode_type1

    fn encode_type1(flags : UInt) -> Bytes

    Type 1, the client's opening message. Emitted with empty domain and workstation: both are optional and servers do not require them.

    NEGOTIATE_VERSION makes the eight-byte Version field part of the header, so the two security buffers move to 40. Encoding a flag the layout does not follow would produce a message a strict server rejects.

    encode_type2

    fn encode_type2(flags : UInt, server_challenge : Bytes, target_name : Bytes, target_info : Bytes) -> Bytes

    Build a challenge message, used by the mock server and by the tests.

    As in encode_type1, setting NEGOTIATE_VERSION inserts the eight-byte Version field before the payload and moves both security buffers past it.

    encode_type3

    fn encode_type3(user : String, domain : String, workstation : String, lm_response : Bytes, nt_response : Bytes, session_key : Bytes, flags : UInt) -> Bytes

    Build a type 3 message. session_key may be empty: without NEGOTIATE_KEY_EXCH the field is unused, and every server this project has been tried against accepts an empty one.

    encode_type3_with_mic

    fn encode_type3_with_mic(user : String, domain : String, workstation : String, lm_response : Bytes, nt_response : Bytes, session_key : Bytes, flags : UInt, mic : Bytes) -> Bytes

    Build an authenticate message, optionally carrying a MIC.

    MS-NLMP 2.2.1.3 puts an eight-byte Version field and then a sixteen-byte MIC between NegotiateFlags and the payload. Both are optional, but a message that carries a MIC has to carry the Version field before it, otherwise the MIC lands at 64 and the server reads it as the tail of the Version. Passing 16 bytes here writes both and starts the payload at 88; passing an empty value writes neither. Any other length is treated as "no MIC" — the caller has nothing valid to say at that size.

    lmv2_response

    fn lmv2_response(user : String, domain : String, password : String, server_challenge : Bytes, client_challenge : Bytes) -> Bytes

    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.

    mac

    fn mac(signing_key : Bytes, flags : UInt, seq_num : UInt, message : Bytes) -> Bytes

    MS-NLMP 3.4.5.2 MAC with extended session security:

    Version = 0x00000001 Checksum = HMAC_MD5(SigningKey, SeqNum || Message)[0..8] SeqNum = SeqNum

    Sixteen bytes: version, checksum, sequence number. When key exchange is negotiated the checksum would be RC4-encrypted first; this library never sets NEGOTIATE_KEY_EXCH, so asking for it here is a programming error rather than a value that would fail later in a confusing place.

    make_blob

    fn make_blob(timestamp : UInt64, client_challenge : Bytes, target_info : Bytes) -> Bytes

    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.

    mech_list_mic

    fn mech_list_mic(session_base_key : Bytes, flags : UInt, mech_list_der : Bytes) -> Bytes

    The mechListMIC an initiator puts in its last NegTokenTarg: the MAC over the DER of the mechanism list it offered, keyed with the client-to-server signing key and sequence number zero (the first signature of the context).

    message_type

    fn message_type(data : Bytes) -> Int raise MessageError

    nt_hash

    fn nt_hash(password : String) -> Bytes

    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.

    ntlmv2_hash

    fn ntlmv2_hash(user : String, domain : String, password : String) -> Bytes

    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.

    ntlmv2_response

    fn ntlmv2_response(user : String, domain : String, password : String, server_challenge : Bytes, blob : Bytes) -> Bytes

    NTLMv2 response: HMAC-MD5(v2 hash, server challenge + blob) || blob.

    parse_type2

    fn parse_type2(data : Bytes) -> Type2 raise MessageError

    Parse a type 2 challenge. target_info is returned exactly as sent, including its end-of-list pair; make_blob wants it without.

    parse_type3

    fn parse_type3(data : Bytes) -> Type3 raise MessageError

    Parse a type 3 message from any client. Strings are decoded as UTF-16LE; malformed units are replaced rather than rejected, because a diagnostic tool should still be able to show what arrived.

    session_base_key

    fn session_base_key(user : String, domain : String, password : String, nt_proof : Bytes) -> Bytes

    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.

    signing_key

    fn signing_key(session_base_key : Bytes, flags : UInt, initiator : Bool) -> Bytes

    The signing key for one direction, MS-NLMP 3.4.5.3: MD5(ExportedSessionKey || magic).

    initiator selects client-to-server when true and server-to-client when false, so a client and a server derive different keys from the same session.

    token_from_header

    fn token_from_header(text : String) -> Bytes raise

    token_to_header

    fn token_to_header(token : Bytes) -> String

    Tokens travel base64-encoded inside Authorization and WWW-Authenticate. These two helpers exist so callers do not have to know which base64 flavour the protocol uses (standard, padded, with whitespace tolerated on the way in).

    verify_acceptor_mech_list_mic

    fn verify_acceptor_mech_list_mic(session_base_key : Bytes, flags : UInt, mech_list_der : Bytes, mic : Bytes) -> Bool

    The initiator's check of the acceptor's mechListMIC.

    verify_mac

    fn verify_mac(signing_key : Bytes, flags : UInt, seq_num : UInt, message : Bytes, signature : Bytes) -> Bool

    Compare a signature in constant time.

    verify_mech_list_mic

    fn verify_mech_list_mic(session_base_key : Bytes, flags : UInt, mech_list_der : Bytes, mic : Bytes) -> Bool

    The acceptor's check of that value. Same list, same sequence number, the other direction's key.

    verify_mic

    fn verify_mic(session_key : Bytes, type1 : Bytes, type2 : Bytes, type3 : Bytes) -> Bool

    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.

    verify_response

    fn verify_response(user : String, domain : String, password : String, server_challenge : Bytes, response : Bytes) -> Bool

    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.

    with_av_flags

    fn with_av_flags(target_info : Bytes, flags : UInt) -> Bytes

    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.