moonsso

    NTLMv2 challenge-response for MoonBit, without FFI

    ntlm
    ntlmv2
    authentication
    windows
    spnego
    Download zip
    Author
    Version
    0.4.1
    License
    Apache-2.0
    Last updated
    46 minutes ago
    Downloads
    30

    No dependencies

    #MoonSSO

    NTLMv2 for MoonBit: challenge-response authentication against Windows domains, written in MoonBit with no FFI and no dependencies outside the standard library. The package is published as Derk2006/moonsso.

    NTLM is the authentication scheme a lot of intranets still speak. A server that wants MoonBit on the inside of such a network has to answer a type 2 challenge, and until now there was nothing in the ecosystem that could.

    #What it does

    • nt_hash, ntlmv2_hash — the two one-way functions the protocol is built from, MD4 and HMAC-MD5 respectively.
    • make_blob, ntlmv2_response, lmv2_response, session_base_key — the client side of the exchange.
    • parse_type2, encode_type2, parse_type3, encode_type3 — the wire format, with security buffers bounds-checked.
    • verify_response — the server side: recompute the proof over the blob the client sent and compare.
    • compute_mic, verify_mic — the message integrity code over the three messages, placed where the payload offset says it goes.
    • signing_key, mac, verify_mac, mech_list_mic, verify_mech_list_mic, acceptor_mech_list_mic, verify_acceptor_mech_list_mic — NTLM's signature function (MS-NLMP 3.4.5) and the mechListMIC built on it, in both directions.
    • wrap_init, wrap_response, wrap_targ, unwrap — the SPNEGO wrapper from RFC 4178, both the NegTokenInit a client opens with and the NegTokenTarg both sides use afterwards. unwrap also takes a bare NTLM message.
    • parse_neg_targ, wrap_final, NegState — all four NegTokenTarg fields: negState, supportedMech, the response token and mechListMIC. The server's first token names NTLMSSP as RFC 4178 requires, and mech_list_der / offered_mech_list give the exact bytes a MIC covers.
    • Authorization / WWW-Authenticate handling — several schemes in one header, quoted commas, case-insensitive scheme names.
    • Client and Server — the whole 401 → type 1 → type 2 → type 3 → 200 conversation, in either scheme, including the request-mic round when a server wants the mechanism list signed.
    • moonsso CLI — describe a challenge, build an answer, check one, answer a real WWW-Authenticate value, show the negotiation, show a mechListMIC exchange.

    #What it does not do

    • NTLMv1 and LM. Both are weaker than the protocol that replaced them and every server that matters negotiates v2. Refusing them is a feature.
    • Kerberos. Planned, not present. src/crypto already carries the RC4 and HMAC-MD5 that RC4-HMAC needs, which is why it is laid out this way.
    • Message signing and sealing. RC4 is implemented because the protocol uses it, not because it should be used for anything new.
    • mechListMIC for mechanisms other than NTLM. The value is computed and verified here — RFC 4178 leaves it to the GSS mechanism, and for NTLM that mechanism is the signing key derived from the exported session key, which is what src/ntlm/sign.mbt implements. A mechanism this library does not implement cannot produce one, and nothing is invented for it.
    • Transport security. NTLM authenticates a client to a server. It does not encrypt the channel and does not replace TLS.
    • Sockets. The HTTP layer works on header values, not on connections. What to do with a 401 is the caller's loop; what to put in the header is this library's job.
    • Replay policy. Server verifies the proof, the MIC and the mechanism list, but it has no clock, so it does not reject a blob whose timestamp is outside a maximum skew. A deployment that keeps accounts must add that check next to the credential store.

    #Install

    moon add Derk2006/moonsso

    Then import the module root. It re-exports the wire format, the SPNEGO wrapper and the HTTP conversation:

    // moon.pkg
    import {
    "Derk2006/moonsso" @moonsso,
    }

    // or, to pull in only one layer:
    // "Derk2006/moonsso/src/ntlm" @ntlm
    // "Derk2006/moonsso/src/spnego" @spnego
    // "Derk2006/moonsso/src/http" @http

    #HTTP

    fn conversation() -> Bool raise {
    let challenge : Bytes = b"\x01\x23\x45\x67\x89\xab\xcd\xef"
    let client = @moonsso.Client::new("User", "Domain", "Password")
    let server = @moonsso.Server::new("User", "Domain", "Password", challenge)

    let first = client.start() // Authorization: Negotiate <type 1>
    let www = server.challenge_for(first) // WWW-Authenticate: Negotiate <type 2>
    let answer = client.answer(www) // Authorization: Negotiate <type 3>
    server.accept(answer) // true if the password is right
    }

    Client::new starts from a zero client challenge and timestamp so that a demo is reproducible. A real connection should pass fresh values — Client::with_fixed_challenge(user, domain, password, eight_random_bytes,filetime) — because the library does not read a clock or an entropy source (the wasm-gc backend has no entropy source in the standard library). The parameters are not a testing-only hook; they are how randomness enters the protocol. When the challenge carries MsvAvTimestamp, that value replaces the timestamp argument: MS-NLMP 3.1.5.2.1 makes the server's time the blob's time, and a server that sent one compares the two.

    The same code with .bare() keeps the whole exchange on plain NTLM; the default client follows whichever scheme the server used.

    moonsso negotiate prints the exchange:

    GET / HTTP/1.1 401 Unauthorized WWW-Authenticate: Negotiate GET / HTTP/1.1 Authorization: Negotiate YEAGBisGAQUFAqA2MDSgDjAMBgorBgEEAYI3AgIKoiIEIE5UTE1TU1AAAQAAAAeCiAAAAAAAIAAAAAAAAAAgAAAA 401 Unauthorized WWW-Authenticate: Negotiate oXkwd6ADCgEBoQwGCisGAQQBgjcCAgqiYgRgTlRMTVNTUAACAAAADAAMADAAAAABgoggASNFZ4mrze8AAAAAAAAAACQAJAA8AAAARABvAG0AYQBpAG4AAgAMAEQAbwBtAGEAaQBuAAEADABTAEUAUgBWAEUAUgAAAAAA GET / HTTP/1.1 Authorization: Negotiate oYGxMIGuooGrBIGoTlRMTVNTUAADAAAAAAAAAFQAAABUAFQAVAAAAAwADABAAAAACAAIAEwAAAAAAAAAVAAAAAAAAACoAAAAB4KIAEQAbwBtAGEAaQBuAFUAcwBlAHIAOmnZuiG/HYEGKhytdhkLtgEBAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAACAAwARABvAG0AYQBpAG4AAQAMAFMARQBSAFYARQBSAAAAAAAAAAAA 200 OK

    The second WWW-Authenticate is a NegTokenTarg with negState accept-incomplete (1), supportedMech 1.3.6.1.4.1.311.2.2.10 and the type 2 message in responseToken.

    #MIC

    A server asks for a message integrity code by setting MsvAvFlags bit 1 in the challenge, or by sending an MsvAvTimestamp pair. MS-NLMP 3.1.5.1.2 gives both as signals, so Client::answer reads both and signs on its own; a server that wants to check one calls require_mic:

    let server = @moonsso.Server::new("User", "Domain", "Password", challenge).require_mic()

    moonsso negotiate # no MIC: the challenge does not ask for one moonsso negotiate --mic # the challenge asks, the answer carries 16 bytes

    Signing also changes the blob, and both changes are part of the answer the server checks: the Timestamp field takes the value of the challenge's MsvAvTimestamp when there is one, and MsvAvFlags gets bit 0x2 so the acceptor can tell a signed message from an unsigned one. A client that sent a MIC without setting that bit would be accepted by servers that trust their own configuration and quietly ignored by those that trust the message — the difference CVE-2019-1040 is made of. Server::accept therefore verifies a MIC the blob claims even when the server did not require one, and refuses a demanded MIC the blob does not acknowledge.

    The value is HMAC-MD5 over the negotiate message, the challenge and the authenticate message, keyed by the exported session key, with the MIC field zeroed while it is computed. Where the field sits depends on whether the optional Version field is present — at 64 without it, at 72 with it. The reader decides from where the payload starts rather than from NEGOTIATE_VERSION, because pyspnego sets that flag and writes no field; the writer emits the layout its flags imply, with a Version field that passes MS-NLMP 2.2.1.1 rather than eight zero bytes.

    #The mechanism list MIC

    A server that wants the offered mechanism list protected answers the first message with negState = request-mic. The client then signs the DER of that list with NTLM's signature function, keyed by the signing key derived from the exported session key, and the server verifies it before it completes:

    let server = @moonsso.Server::new("User", "Domain", "Password", challenge)
    .require_mech_list_mic()

    moonsso negotiate --mechlist-mic # four requests: request-mic, answer, accept-completed, 200

    The acceptor sends its own MIC back over the same list, keyed in the other direction, and the client checks it. offered_mech_list returns the bytes a peer actually sent, so a client that offers several mechanisms is verified against its own encoding rather than against this library's.

    #CLI

    Describe a challenge received in WWW-Authenticate (SPNEGO or bare):

    moonsso type2 "TlRMTVNTUAACAAAADAAMADAAAAABgoggASNFZ4mrze8AAAAAAAAAACQAJAA8AAAARABPAE0AQQBJAE4AAgAMAEQATwBNAEEASQBOAAEADABTAEUAUgBWAEUAUgAAAAAA"

    message : challenge (2) flags : 0x20888201 UNICODE NTLM ALWAYS_SIGN EXTENDED_SESSION_SECURITY TARGET_INFO 128 challenge: 0123456789abcdef target : DOMAIN av pairs : 32 bytes target info (hex): 02000c0044004f004d00410049004e0001000c00530045005200560045005200

    Build an answer:

    moonsso respond --user User --domain Domain --password Password \ --challenge 0123456789abcdef --client-challenge aaaaaaaaaaaaaaaa \ --target-info 02000c0044004f004d00410049004e0001000c005300450052005600450052 \ --spnego

    ntlmv2 response: ef708ba61dcf311ac7d78604310b4f1f01010000000000000000000000000000aaaaaaaaaaaaaaaa0000000002000c0044004f004d00410049004e0001000c0053004500520056004500520000000000000000 type3 (base64):TlRMTVNTUAADAAAAGAAYAFQAAABTAFMAbAAAAAwADABAAAAACAAIAEwAAAAAAAAAVAAAAAAAAAC/AAAAF4KIAEQAbwBtAGEAaQBuAFUAcwBlAHIAhsNQl6yc7BAlVHZKV8zMGaqqqqqqqqqq73CLph3PMRrH14YEMQtPHwEBAAAAAAAAAAAAAAAAAACqqqqqqqqqqgAAAAACAAwARABPAE0AQQBJAE4AAQAMAFMARQBSAFYARQBSAAAAAAAAAAA= authorization : Negotiate oYHIMIHFooHCBIG/TlRMTVNTUAADAAAAGAAYAFQAAABTAFMAbAAAAAwADABAAAAACAAIAEwAAAAAAAAAVAAAAAAAAAC/AAAAF4KIAEQAbwBtAGEAaQBuAFUAcwBlAHIAhsNQl6yc7BAlVHZKV8zMGaqqqqqqqqqq73CLph3PMRrH14YEMQtPHwEBAAAAAAAAAAAAAAAAAACqqqqqqqqqqgAAAAACAAwARABPAE0AQQBJAE4AAQAMAFMARQBSAFYARQBSAAAAAAAAAAA=

    Check one that arrived from a client, straight from the header:

    moonsso verify --user User --domain Domain --password Password \ --challenge 0123456789abcdef \ --authorization "Negotiate oYGxMIGuooGrBIGoTlRMTVNTUAADAAAAAAAAAFQAAABUAFQAVAAAAAwADABAAAAACAAIAEwAAAAAAAAAVAAAAAAAAACoAAAAB4KIAEQAbwBtAGEAaQBuAFUAcwBlAHIAOmnZuiG/HYEGKhytdhkLtgEBAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAACAAwARABvAG0AYQBpAG4AAQAMAFMARQBSAFYARQBSAAAAAAAAAAAA"

    Answer a challenge that came from a real server, without writing a program:

    moonsso answer --user User --domain Domain --password Password \ --www "Negotiate oXkwd6ADCgEBoQwGCisGAQQBgjcCAgqiYgRgTlRMTVNTUAACAAAADAAMADAAAAABgoggASNFZ4mrze8AAAAAAAAAACQAJAA8AAAARABvAG0AYQBpAG4AAgAMAEQAbwBtAGEAaQBuAAEADABTAEUAUgBWAEUAUgAAAAAA"

    moonsso unwrap <token> names the layers of any token and, for a type 2 message, shows the challenge inside. answer prints the full Authorization value and, when the challenge asks for one, the MIC it put in the message.

    #Library

    /// Build the type 3 message for a decoded type 2 token.
    fn answer(type2 : Bytes, client_challenge : Bytes) -> Bytes raise {
    let msg = @moonsso.parse_type2(type2)
    let blob = @moonsso.make_blob(0UL, client_challenge, msg.av_pairs())
    let proof = @moonsso.ntlmv2_response(
    "User", "Domain", "Password", msg.server_challenge(), blob,
    )
    let lmv2 = @moonsso.lmv2_response(
    "User", "Domain", "Password", msg.server_challenge(), client_challenge,
    )
    @moonsso.encode_type3("User", "Domain", "WS01", lmv2, proof, b"", msg.flags())
    }

    token_from_header takes the base64 text, not the whole header line. If all you have is a WWW-Authenticate value, @moonsso.parse_authorization(header) splits off the scheme and hands you the bytes. client_challenge is eight bytes; use a fresh random value per connection. make_blob's first argument is the Windows timestamp, 0UL meaning "not recorded". The HTTP layer does all of this for you; the snippet is here for callers that already own their request loop.

    #Verification

    Tests are known-answer tests wherever a specification provides them:

    SourceCovers
    RFC 1320MD4, 7 vectors
    RFC 1321MD5, 7 vectors
    RFC 2202HMAC-MD5, 4 vectors
    RC4 reference vectorskeystream, 3 vectors
    MS-NLMP 4.2.2NT hash, NTLMv2 hash, LMv2 response

    The wire format is additionally checked by replaying a type 3 message produced by a different implementation (node-ntlm-client) through parse_type3 and verify_response. That fixture and the pyspnego MIC fixture described below are the only byte strings in the NTLM test suite this project did not produce itself, which is what makes them worth keeping.

    The SPNEGO encoding is checked by openssl asn1parse, which parses the DER independently of this code: CI asserts that the OIDs 1.3.6.1.5.5.2 and 1.3.6.1.4.1.311.2.2.10 appear where RFC 4178 puts them. The NTLMv2 proof produced by the HTTP conversation is re-derived from MS-NLMP in Python, with a from-scratch MD4, and must match byte for byte.

    That script, scripts/verify_ntlmv2.py, also walks the client blob pair by pair and insists on the full tail: AV pairs, an end-of-list pair, then the four reserved bytes. A blob that ends one field early fails the build instead of being accepted by our own verifier.

    pyspnego 0.13, an independent implementation, is used in both directions: its pure-Python acceptor has to accept the type 3 this library produces, and it also has to accept a signed answer produced by moonsso answer from a challenge it issued itself (both run in CI by scripts/interop_pyspnego.py). In the other direction, a type 3 it produces for a challenge that asks for a MIC has to pass verify_mic; a fixture is pinned in src/ntlm/interop_mic_test.mbt. Its SPNEGO client also produces a mechListMIC for a request-mic challenge, and that answer has to pass this library's acceptor — pinned in src/http/interop_mechlist_test.mbt.

    moon test

    #Status

    0.4.1, December 2026. The client response, the server check, the MIC, the mechListMIC in both directions and the HTTP conversation work in SPNEGO and bare NTLM; Kerberos is the next milestone. 0.4.1 is a correctness pass over 0.4.0: the client uses the challenge's MsvAvTimestamp and records the MIC in MsvAvFlags, the authenticate message carries the negotiated flags rather than the whole offer, the acceptor verifies a MIC the message claims, and flags that cannot be signed are a rejection instead of an assertion. 0.4.0 added the mechanism-level signature function (MS-NLMP 3.4.5), request-mic handling and the four-request HTTP round it implies.

    #License

    Apache-2.0.

    NegState

    The four values negState can take (RFC 4178 section 4.2.2). A server sends accept-incomplete while it still expects a token, request-mic when it wants a mechListMIC before it will finish, and accept-completed on the last message.

    NegTarg

    The fields of a NegTokenTarg that matter over HTTP.

    Type2

    Re-exports the NTLM, SPNEGO and HTTP API so that importing Derk2006/moonsso is enough for the common case; the src/... paths stay importable for finer control.

    Type3

    Re-exports the NTLM, SPNEGO and HTTP API so that importing Derk2006/moonsso is enough for the common case; the src/... paths stay importable for finer control.

    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.

    authorization_header

    fn authorization_header(scheme : String, token : Bytes) -> String

    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.

    challenge_header

    fn challenge_header(scheme : String) -> String

    challenge_header_with_token

    fn challenge_header_with_token(scheme : String, token : Bytes) -> String

    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.

    describe

    fn describe(token : Bytes) -> String

    A one-line description of a token, for the CLI and for error messages.

    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.

    is_ntlmssp

    fn is_ntlmssp(token : Bytes) -> Bool

    is_spnego

    fn is_spnego(token : Bytes) -> Bool

    A SPNEGO token starts with either the GSS application tag (0x60) or one of the NegotiationToken choice tags.

    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_der

    fn mech_list_der() -> Bytes

    The DER of the MechTypeList this library offers: a SEQUENCE holding the NTLMSSP OID. This is the byte string a mechListMIC is computed over, so wrap_init and the MIC both go through here rather than each building its own encoding — two encoders that agree today can disagree after an edit.

    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.

    offered_mech_list

    fn offered_mech_list(token : Bytes) -> Bytes raise
    DerError

    The MechTypeList an incoming NegTokenInit carried, as the exact DER bytes it arrived in.

    A mechListMIC covers what the initiator sent, not what this library would have encoded, so an acceptor has to check against these bytes rather than against mech_list_der(). A peer that offers two mechanisms would otherwise be rejected for a MIC it computed correctly.

    parse_authorization

    fn parse_authorization(header : String) ->
    Challenge
    raise

    parse_challenges

    fn parse_challenges(header : String) -> Array[
    Challenge
    ]

    Every challenge in a WWW-Authenticate header, in the order offered.

    parse_neg_targ

    Parse a NegTokenTarg, either with its [1] wrapper or as the bare SEQUENCE some stacks emit. Unknown context tags are skipped rather than rejected: a peer may add fields this code does not model.

    parse_type2

    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

    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.

    preferred

    The scheme a client should prefer: SPNEGO first, then raw NTLM.

    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).

    trim

    fn trim(text : String) -> String

    unwrap

    fn unwrap(token : Bytes) -> Bytes raise
    DerError

    Pull the NTLM message out of whatever the peer sent: a bare NTLMSSP message, a GSS-wrapped NegTokenInit, or a NegTokenTarg.

    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.

    wrap_challenge

    fn wrap_challenge(ntlm_token : Bytes, state :
    NegState
    ) -> Bytes

    The acceptor's first NegTokenTarg: negState, the NTLMSSP mechanism, and the challenge. This is the shape RFC 4178 asks for; a bare NegTokenTarg without supportedMech is accepted by many clients but is not conformant.

    wrap_final

    fn wrap_final(state :
    NegState
    , mech_list_mic : Bytes) -> Bytes

    The acceptor's last NegTokenTarg: negState and its own mechListMIC, with no responseToken — the negotiation has no further NTLM message to carry, and an empty [2] would be a field where RFC 4178 allows none.

    wrap_init

    fn wrap_init(ntlm_token : Bytes) -> Bytes

    The client's opening message: GSS-API wrapper around a NegTokenInit whose only offered mechanism is NTLMSSP.

    wrap_response

    fn wrap_response(ntlm_token : Bytes) -> Bytes

    A NegTokenTarg with no negState, which is what a client sends last.

    wrap_response_state

    fn wrap_response_state(ntlm_token : Bytes, state : Int) -> Bytes

    A NegTokenTarg carrying negState — a server uses 1 (accept-incomplete) with its challenge.

    wrap_targ

    fn wrap_targ(ntlm_token : Bytes, state :
    NegState
    ?, mech_list_mic : Bytes?) -> Bytes

    A NegTokenTarg with only the fields HTTP needs: state, response token and an optional mechListMIC.

    wrap_targ_with_mech

    fn wrap_targ_with_mech(ntlm_token : Bytes, state :
    NegState
    ?, supported_mech : Bytes?, mech_list_mic : Bytes?) -> Bytes

    A NegTokenTarg with a state, the selected mechanism and, when the server asked for one, a mechListMIC.

    RFC 4178 requires supportedMech in the first NegTokenTarg the acceptor sends and forbids it in later ones; callers that are not the acceptor pass None.

    The MIC value is computed by src/ntlm — mech_list_mic over mech_list_der() for the initiator, acceptor_mech_list_mic for the acceptor — because RFC 4178 hands the value to the GSS mechanism, and the mechanism here is NTLM. Passing a MIC this layer invented would be worse than passing none.

    Source Files