File: iov_wrapping.py

package info (click to toggle)
python-pyspnego 0.10.2-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,648 kB
  • sloc: python: 16,191; sh: 182; makefile: 11
file content (57 lines) | stat: -rw-r--r-- 1,386 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#!/usr/bin/python

import struct

import spnego
import spnego.iov


def exchange_data(data: bytes) -> bytes:
    # Insert code to send to acceptor and receive token
    return b""


def main() -> None:
    client = spnego.client("username", "password", hostname="server")

    in_token = None
    while client.complete:
        out_token = client.step(in_token)
        if not out_token:
            break

        in_token = exchange_data(out_token)

    print("Negotiated protocol: %s" % client.negotiated_protocol)

    enc_result = client.wrap_iov(
        [
            spnego.iov.BufferType.header,
            b"my secret",
            spnego.iov.BufferType.padding,
        ]
    )

    header = enc_result.buffers[0].data or b""
    enc_data = enc_result.buffers[1].data or b""
    padding = enc_result.buffers[2].data or b""
    enc_payload = struct.pack("<I", len(header)) + header + enc_data + padding

    resp = exchange_data(enc_payload)
    header_len = struct.unpack("<I", resp[:4])[0]
    header = resp[4 : 4 + header_len]
    enc_data = resp[4 + header_len :]

    dec_result = client.unwrap_iov(
        [
            spnego.iov.IOVBuffer(spnego.iov.BufferType.header, header),
            enc_data,
        ]
    )
    dec_data = dec_result.buffers[1].data or b""

    print("Server response: %s" % dec_data.decode("utf-8"))


if __name__ == "__main__":
    main()