| 1 |
# -*- coding: utf-8 -*- |
| 2 |
# Copyright: (c) 2023, Jordan Borean (@jborean93) <[email protected]> |
| 3 |
# MIT License (see LICENSE or https://opensource.org/licenses/MIT) |
| 4 |
|
| 5 |
import socket |
| 6 |
import sys |
| 7 |
|
| 8 |
import pytest |
| 9 |
|
| 10 |
import spnego |
| 11 |
import spnego._credssp |
| 12 |
import spnego._gss |
| 13 |
import spnego._sspi |
| 14 |
import spnego.channel_bindings |
| 15 |
import spnego.iov |
| 16 |
from spnego.exceptions import NoContextError |
| 17 |
|
| 18 |
|
| 19 |
def _kerb_exchange( |
| 20 |
client: spnego.ContextProxy, |
| 21 |
server: spnego.ContextProxy, |
| 22 |
) -> None: |
| 23 |
tok1 = client.step() |
| 24 |
assert tok1 |
| 25 |
assert not client.complete |
| 26 |
assert not server.complete |
| 27 |
|
| 28 |
tok2 = server.step(tok1) |
| 29 |
assert tok2 |
| 30 |
assert not client.complete |
| 31 |
assert not server.complete |
| 32 |
|
| 33 |
if client.protocol == "negotiate": |
| 34 |
tok3 = client.step(tok2) |
| 35 |
assert tok3 |
| 36 |
assert not client.complete |
| 37 |
assert not server.complete |
| 38 |
|
| 39 |
tok4 = server.step(tok3) |
| 40 |
assert tok4 |
| 41 |
assert not client.complete |
| 42 |
assert server.complete |
| 43 |
|
| 44 |
tok5 = client.step(tok4) |
| 45 |
assert not tok5 |
| 46 |
assert client.complete |
| 47 |
assert server.complete |
| 48 |
|
| 49 |
else: |
| 50 |
tok3 = client.step(tok2) |
| 51 |
assert tok3 |
| 52 |
assert client.complete |
| 53 |
assert not server.complete |
| 54 |
|
| 55 |
tok4 = server.step(tok3) |
| 56 |
assert not tok4 |
| 57 |
assert client.complete |
| 58 |
assert server.complete |
| 59 |
|
| 60 |
|
| 61 |
def _ntlm_exchange( |
| 62 |
client: spnego.ContextProxy, |
| 63 |
server: spnego.ContextProxy, |
| 64 |
) -> None: |
| 65 |
negotiate = client.step() |
| 66 |
assert negotiate |
| 67 |
assert not client.complete |
| 68 |
assert not server.complete |
| 69 |
|
| 70 |
challenge = server.step(negotiate) |
| 71 |
assert challenge |
| 72 |
assert not client.complete |
| 73 |
assert not server.complete |
| 74 |
|
| 75 |
authentication = client.step(challenge) |
| 76 |
assert authentication |
| 77 |
|
| 78 |
# If it's being wrapped by SPNEGO then an extra msg is expected |
| 79 |
if authentication.startswith(b"NTLMSSP\x00"): |
| 80 |
assert client.complete |
| 81 |
assert not server.complete |
| 82 |
|
| 83 |
final = server.step(authentication) |
| 84 |
assert not final |
| 85 |
assert client.complete |
| 86 |
assert server.complete |
| 87 |
|
| 88 |
else: |
| 89 |
assert not client.complete |
| 90 |
assert not server.complete |
| 91 |
|
| 92 |
mech_list = server.step(authentication) |
| 93 |
assert mech_list |
| 94 |
assert not client.complete |
| 95 |
assert server.complete |
| 96 |
|
| 97 |
final = client.step(mech_list) |
| 98 |
assert not final |
| 99 |
assert client.complete |
| 100 |
assert server.complete |
| 101 |
|
| 102 |
|
| 103 |
def _message_test( |
| 104 |
client: spnego.ContextProxy, |
| 105 |
server: spnego.ContextProxy, |
| 106 |
sign_header: bool = True, |
| 107 |
) -> None: |
| 108 |
sign_type = spnego.iov.BufferType.sign_only if sign_header else spnego.iov.BufferType.data_readonly |
| 109 |
|
| 110 |
wrap1 = client.wrap_iov( |
| 111 |
[ |
| 112 |
(sign_type, b"header"), |
| 113 |
b"data", |
| 114 |
(sign_type, b"sec_trailer"), |
| 115 |
spnego.iov.BufferType.header, |
| 116 |
] |
| 117 |
) |
| 118 |
assert wrap1.buffers[1].data != b"data" |
| 119 |
|
| 120 |
unwrap1 = server.unwrap_iov(wrap1.buffers) |
| 121 |
assert unwrap1.buffers[1].data == b"data" |
| 122 |
|
| 123 |
wrap2 = server.wrap_iov( |
| 124 |
[ |
| 125 |
(sign_type, b"header"), |
| 126 |
b"data", |
| 127 |
(sign_type, b"sec_trailer"), |
| 128 |
spnego.iov.BufferType.header, |
| 129 |
] |
| 130 |
) |
| 131 |
assert wrap2.buffers[1].data != b"data" |
| 132 |
|
| 133 |
unwrap2 = client.unwrap_iov(wrap2.buffers) |
| 134 |
assert unwrap2.buffers[1].data == b"data" |
| 135 |
|
| 136 |
|
| 137 |
@pytest.mark.parametrize("protocol", ["negotiate", "kerberos"]) |
| 138 |
def test_kerberos(protocol, kerb_cred): |
| 139 |
if sys.platform == "darwin": |
| 140 |
pytest.skip("Environment problem with GSS.Framework - skip") |
| 141 |
|
| 142 |
c = spnego.client( |
| 143 |
kerb_cred.user_princ, |
| 144 |
None, |
| 145 |
protocol=protocol, |
| 146 |
hostname=socket.getfqdn(), |
| 147 |
context_req=spnego.ContextReq.default | spnego.ContextReq.dce_style, |
| 148 |
) |
| 149 |
s = spnego.server( |
| 150 |
protocol=protocol, |
| 151 |
context_req=spnego.ContextReq.default | spnego.ContextReq.dce_style, |
| 152 |
) |
| 153 |
|
| 154 |
expected_no_context = "Cannot get message sizes until context has been established" |
| 155 |
with pytest.raises(NoContextError, match=expected_no_context): |
| 156 |
c.query_message_sizes() |
| 157 |
with pytest.raises(NoContextError, match=expected_no_context): |
| 158 |
s.query_message_sizes() |
| 159 |
|
| 160 |
_kerb_exchange(c, s) |
| 161 |
|
| 162 |
c_sizes = c.query_message_sizes() |
| 163 |
assert isinstance(c_sizes.header, int) |
| 164 |
|
| 165 |
s_sizes = s.query_message_sizes() |
| 166 |
assert isinstance(s_sizes.header, int) |
| 167 |
|
| 168 |
_message_test(c, s, sign_header=False) |
| 169 |
_message_test(c, s, sign_header=True) |
| 170 |
|
| 171 |
|
| 172 |
@pytest.mark.parametrize("protocol", ["negotiate", "ntlm"]) |
| 173 |
def test_ntlm(protocol, ntlm_cred): |
| 174 |
c = spnego.client( |
| 175 |
ntlm_cred[0], |
| 176 |
ntlm_cred[1], |
| 177 |
protocol=protocol, |
| 178 |
hostname=socket.getfqdn(), |
| 179 |
context_req=spnego.ContextReq.default | spnego.ContextReq.dce_style, |
| 180 |
) |
| 181 |
s = spnego.server( |
| 182 |
protocol=protocol, |
| 183 |
context_req=spnego.ContextReq.default | spnego.ContextReq.dce_style, |
| 184 |
options=spnego.NegotiateOptions.use_negotiate, |
| 185 |
) |
| 186 |
|
| 187 |
expected_no_context = "Cannot get message sizes until context has been established" |
| 188 |
with pytest.raises(NoContextError, match=expected_no_context): |
| 189 |
c.query_message_sizes() |
| 190 |
with pytest.raises(NoContextError, match=expected_no_context): |
| 191 |
s.query_message_sizes() |
| 192 |
|
| 193 |
_ntlm_exchange(c, s) |
| 194 |
|
| 195 |
c_sizes = c.query_message_sizes() |
| 196 |
assert c_sizes.header == 16 |
| 197 |
|
| 198 |
s_sizes = s.query_message_sizes() |
| 199 |
assert s_sizes.header == 16 |
| 200 |
|
| 201 |
_message_test(c, s, sign_header=False) |
| 202 |
_message_test(c, s, sign_header=True) |