
    i                        S SK r S SKrS SKJrJrJrJr  S SKJr  S SK	J
r
  SS.S\S\S\S	\S
\S\R                  \   S\R                  \\4   4S jjrS\S\S\S	\S
\S\4S jrS\S	\S
\S\4S jrS\S\S\S	\S
\S\4S jrg)    N)	RC4Handlecrc32hmac_md5rc4)NegotiateFlags)OperationNotAvailableError)to_signflagshandlesigning_keyseq_numb_datar	   returnc                J    [        X5      n[        XX#U(       a  UOU5      nXg4$ )aj  Create a sealed NTLM message.

Creates a sealed NTLM message as documented at `NTLM Message Confidentiality`_.

Args:
    flags: The negotiated flags between the initiator and acceptor.
    handle: The RC4 handle for the negotiated context.
    signing_key: The key used to sign the message.
    seq_num: The sequence number for the message.
    b_data: The data/message bytes to seal.

Returns:
    Tuple[bytes, bytes]: The sealed message bytes and the message signature.

.. _NTLM Message Confidentiality:
    https://docs.microsoft.com/en-us/openspecs/windows_protocols/ms-nlmp/115f9c7d-bc30-4262-ae96-254555c14ea6
)r   sign)r
   r   r   r   r   r	   seal_msg	signatures           S/home/james-whalen/.local/lib/python3.13/site-packages/spnego/_ntlm_raw/security.pysealr      s*    4 6"HUKW'RXYI    c                     U [         R                  -  S:X  a!  U [         R                  -  S:X  a	  [        SS9egU [         R                  -  (       a  [        XX#U5      $ [        XU5      $ )ac  Create a NTLM signature.

Creates a NTLM signature as documented at `NTLM Message Integrity`_ and appends it to the end of the message.

Args:
    flags: The negotiated flags between the initiator and acceptor.
    handle: The RC4 handle for the negotiated context.
    signing_key: The key used to sign the message.
    seq_num: The sequence number for the signature.
    b_data: The data/message bytes to sign.

Returns:
    bytes: The data with the signature appended.

.. _NTLM Message Integrity:
    https://docs.microsoft.com/en-us/openspecs/windows_protocols/ms-nlmp/131b0062-7958-460e-bca5-c7a9f9086652
r   zSigning without integrity.)context_msgs                  )r   r   always_signr   extended_session_security_mac_with_ess_mac_without_ess)r
   r   r   r   r   s        r   r   r   +   sg    0 ~"""a'>---2,9UVV &	99	9UK&II  88r   c                     [        U5      n[        U S5        [        X5      n[        R                  " S[        U S5      5      S   n[        R                  " SXA-  5      nSU-   U-   $ )a   NTLM MAC without Extended Session Security

Generates the NTLM signature when Extended Session Security has not been negotiated. The structure of the signature
is documented at `NTLM signature without ESS`_.

The algorithm as documented by `MAC without ESS`_ is::

    Define MAC(Handle, SigningKey, SeqNum, Message) as
        Set NTLMSSP_MESSAGE_SIGNATURE.Version to 0x00000001
        Set NTLMSSP_MESSAGE_SIGNATURE.Checksum to CRC32(Message)
        Set NTLMSSP_MESSAGE_SIGNATURE.RandomPad RC4(Handle, RandomPad)
        Set NTLMSSP_MESSAGE_SIGNATURE.Checksum to RC4(Handle, NTLMSSP_MESSAGE_SIGNATURE.Checksum)
        Set NTLMSSP_MESSAGE_SIGNATURE.SeqNum to RC4(Handle, 0x00000000)

        If (connection oriented)
            Set NTLMSSP_MESSAGE_SIGNATURE.SeqNum to NTLMSSP_MESSAGE_SIGNATURE.SeqNum XOR SeqNum
            Set SeqNum to SeqNum + 1

        Else
            Set NTLMSSP_MESSAGE_SIGNATURE.SeqNum to NTLMSSP_MESSAGE_SIGNATURE.SeqNum XOR (app supplied SeqNum)

        Endif

        Set NTLMSSP_MESSAGE_SIGNATURE.RandomPad to 0

    EndDefine

Args:
    handle: The RC4 handle for the negotiated context.
    seq_num: The sequence number for the signature.
    b_data: The data/message bytes to sign.

Returns:
    bytes: The NTLM signature.

.. _NTLM signature without ESS:
    https://docs.microsoft.com/en-us/openspecs/windows_protocols/ms-nlmp/83fbd0e7-8ab0-4873-8cbe-795249b46b8a

.. _MAC without ESS:
    https://docs.microsoft.com/en-us/openspecs/windows_protocols/ms-nlmp/0b1fb6a6-7224-4d5b-af35-fdd45c0791e5
s       <Ir   s          )r   r   structunpackpack)r   r   r   checksumtemp_seq_num	b_seq_nums         r   r   r   Q   sc    \ V}H#$6$H==s63F'GHKLD,"89I4x?)KKr   c                     [         R                  " SU5      n[        X%U-   5      SS nU [        R                  -  (       a  UR                  U5      nSU-   U-   $ )a  NTLM MAC with Extended Session Security

Generates the NTLM signature when Extended Session Security has been negotiated. The structure of the signature is
documented at `NTLM signature with ESS`_.

The algorithm as documented by `MAC with ESS`_ is::

    Define MAC(Handle, SigningKey, SeqNum, Message) as
        Set NTLMSSP_MESSAGE_SIGNATURE.Version to 0x00000001
        Set NTLMSSP_MESSAGE_SIGNATURE.Checksum to HMAC_MD5(SigningKey, ConcatenationOf(SeqNum, Message))[0..7]
        Set NTLMSSP_MESSAGE_SIGNATURE.SeqNum to SeqNum
        Set SeqNum to SeqNum + 1
    EndDefine

    # When NegotiateFlags.key_exch

    Define MAC(Handle, SigningKey, SeqNum, Message) as
        Set NTLMSSP_MESSAGE_SIGNATURE.Version to 0x00000001
        Set NTLMSSP_MESSAGE_SIGNATURE.Checksum to RC4(Handle,
            HMAC_MD5(SigningKey, ConcatenationOf(SeqNum, Message))[0..7])
        Set NTLMSSP_MESSAGE_SIGNATURE.SeqNum to SeqNum
        Set SeqNum to SeqNum + 1
    EndDefine

Args:
    flags: The negotiated flags between the initiator and acceptor.
    handle: The RC4 handle for the negotiated context.
    signing_key: The key used to sign the message.
    seq_num: The sequence number for the signature.
    b_data: The data/message bytes to sign.

Returns:
    bytes: The NTLM with ESS signature.

.. _NTLM signature with ESS:
    https://docs.microsoft.com/en-us/openspecs/windows_protocols/ms-nlmp/2c3b4689-d6f1-4dc6-85c9-0bf01ea34d9f

.. _MAC with ESS:
    https://docs.microsoft.com/en-us/openspecs/windows_protocols/ms-nlmp/a92716d5-d164-4960-9e15-300f4eef44a8
r   N   s      )r   r!   r   r   key_exchupdate)r
   r   r   r   r   r$   r"   s          r   r   r      sW    R D'*I%78!<H~&&&==*)I55r   )r   typingspnego._ntlm_raw.cryptor   r   r   r   spnego._ntlm_raw.messagesr   spnego.exceptionsr   intbytesOptionalTupler   r   r   r    r   r   <module>r2      s     C C 4 8 '+  	
  __U# \\%,>#9#9#9 #9 	#9
 #9 #9L5L5L5L 5L 	5Lp/6 /6i /6e /6c /6[` /6ej /6r   