| 1 |
# -*- coding: utf-8 -*- |
| 2 |
# Copyright: (c) 2020, Jordan Borean (@jborean93) <[email protected]> |
| 3 |
# MIT License (see LICENSE or https://opensource.org/licenses/MIT) |
| 4 |
|
| 5 |
import os |
| 6 |
import socket |
| 7 |
|
| 8 |
import pytest |
| 9 |
|
| 10 |
import spnego._sspi |
| 11 |
import spnego.iov |
| 12 |
from spnego.exceptions import InvalidCredentialError |
| 13 |
|
| 14 |
|
| 15 |
@pytest.mark.skipif("ntlm" not in spnego._sspi.SSPIProxy.available_protocols(), reason="Requires SSPI library") |
| 16 |
def test_build_iov_list(ntlm_cred): |
| 17 |
c = spnego._sspi.SSPIProxy(ntlm_cred[0], ntlm_cred[1], protocol="ntlm") |
| 18 |
c._security_trailer = 10 |
| 19 |
c._block_size = 2 |
| 20 |
|
| 21 |
actual = c._build_iov_list( |
| 22 |
[ |
| 23 |
(spnego.iov.BufferType.header, b"\x01"), |
| 24 |
(spnego.iov.BufferType.data, 5), |
| 25 |
(spnego.iov.BufferType.padding, True), |
| 26 |
spnego.iov.BufferType.header, |
| 27 |
spnego.iov.BufferType.stream, |
| 28 |
b"\x02", |
| 29 |
], |
| 30 |
c._convert_iov_buffer, |
| 31 |
) |
| 32 |
|
| 33 |
assert len(actual) == 6 |
| 34 |
assert actual[0].buffer_type == spnego.iov.BufferType.header |
| 35 |
assert actual[0].data == b"\x01" |
| 36 |
assert actual[1].buffer_type == spnego.iov.BufferType.data |
| 37 |
assert actual[1].data == b"\x00" * 5 |
| 38 |
assert actual[2].buffer_type == spnego.iov.BufferType.padding |
| 39 |
assert actual[2].data == b"\x00" * 2 |
| 40 |
assert actual[3].buffer_type == spnego.iov.BufferType.header |
| 41 |
assert actual[3].data == b"\x00" * 10 |
| 42 |
assert actual[4].buffer_type == spnego.iov.BufferType.stream |
| 43 |
assert actual[4].data == b"" |
| 44 |
assert actual[5].buffer_type == spnego.iov.BufferType.data |
| 45 |
assert actual[5].data == b"\x02" |
| 46 |
|
| 47 |
|
| 48 |
@pytest.mark.skipif("ntlm" not in spnego._sspi.SSPIProxy.available_protocols(), reason="Requires SSPI library") |
| 49 |
def test_build_iov_list_fail_auto_alloc(ntlm_cred): |
| 50 |
c = spnego._sspi.SSPIProxy(ntlm_cred[0], ntlm_cred[1], protocol="ntlm") |
| 51 |
c._security_trailer = 10 |
| 52 |
c._block_size = 2 |
| 53 |
|
| 54 |
with pytest.raises(ValueError, match="Cannot auto allocate buffer of type BufferType.data"): |
| 55 |
c._build_iov_list([(spnego.iov.BufferType.data, True)], c._convert_iov_buffer) |
| 56 |
|
| 57 |
|
| 58 |
def test_no_sspi_library(monkeypatch): |
| 59 |
monkeypatch.setattr(spnego._sspi, "HAS_SSPI", False) |
| 60 |
|
| 61 |
with pytest.raises(ImportError, match="SSPIProxy requires the Windows only sspilib python package"): |
| 62 |
spnego._sspi.SSPIProxy() |
| 63 |
|
| 64 |
|
| 65 |
@pytest.mark.skipif("ntlm" not in spnego._sspi.SSPIProxy.available_protocols(), reason="Requires SSPI library") |
| 66 |
def test_sspi_invalid_qop(): |
| 67 |
c = spnego._sspi.SSPIProxy("user", "pass") |
| 68 |
|
| 69 |
with pytest.raises(ValueError, match="Cannot set qop with SECQOP_WRAP_NO_ENCRYPT and encrypt=True"): |
| 70 |
c.wrap(b"\x00", True, qop=0x80000001) |
| 71 |
|
| 72 |
|
| 73 |
@pytest.mark.skipif("ntlm" not in spnego._sspi.SSPIProxy.available_protocols(), reason="Requires SSPI library") |
| 74 |
def test_sspi_wrap_no_encryption(ntlm_cred): |
| 75 |
c = spnego._sspi.SSPIProxy(ntlm_cred[0], ntlm_cred[1], hostname=socket.gethostname()) |
| 76 |
s = spnego._sspi.SSPIProxy(usage="accept") |
| 77 |
|
| 78 |
s.step(c.step(s.step(c.step()))) |
| 79 |
|
| 80 |
plaintext = os.urandom(16) |
| 81 |
enc_data = c.wrap(plaintext, encrypt=False) |
| 82 |
dec_data = s.unwrap(enc_data.data) |
| 83 |
assert dec_data.data == plaintext |
| 84 |
|
| 85 |
|
| 86 |
@pytest.mark.skipif("ntlm" not in spnego._sspi.SSPIProxy.available_protocols(), reason="Requires SSPI library") |
| 87 |
def test_sspi_no_valid_cred(): |
| 88 |
with pytest.raises(InvalidCredentialError, match="No applicable credentials available"): |
| 89 |
spnego._sspi.SSPIProxy(spnego.KerberosKeytab("user_princ", "ccache"), protocol="kerberos") |