2023-10-31 06:31:59 +00:00
|
|
|
|
# SPDX-License-Identifier: MIT
|
|
|
|
|
#
|
2018-02-04 14:41:30 +00:00
|
|
|
|
# Copyright (c) 2017 PySecretHandshake contributors (see AUTHORS for more details)
|
|
|
|
|
#
|
|
|
|
|
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
|
# of this software and associated documentation files (the "Software"), to deal
|
|
|
|
|
# in the Software without restriction, including without limitation the rights
|
|
|
|
|
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
|
# copies of the Software, and to permit persons to whom the Software is
|
|
|
|
|
# furnished to do so, subject to the following conditions:
|
|
|
|
|
#
|
|
|
|
|
# The above copyright notice and this permission notice shall be included in all
|
|
|
|
|
# copies or substantial portions of the Software.
|
|
|
|
|
#
|
|
|
|
|
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
|
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
|
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
|
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
|
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
|
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
|
|
|
# SOFTWARE.
|
|
|
|
|
|
2023-10-29 08:55:39 +00:00
|
|
|
|
"""Tests for the networking components"""
|
|
|
|
|
|
2018-02-04 14:41:30 +00:00
|
|
|
|
from asyncio import Event, wait_for
|
2023-10-29 09:47:21 +00:00
|
|
|
|
import os
|
2023-10-30 04:47:28 +00:00
|
|
|
|
from typing import Any, Awaitable, Callable, Tuple
|
2018-02-04 14:41:30 +00:00
|
|
|
|
|
|
|
|
|
from nacl.signing import SigningKey
|
2023-10-29 09:47:21 +00:00
|
|
|
|
import pytest
|
2023-10-30 04:47:28 +00:00
|
|
|
|
from pytest_mock import MockerFixture
|
2018-02-04 14:41:30 +00:00
|
|
|
|
|
2023-10-29 08:55:39 +00:00
|
|
|
|
from secret_handshake import SHSClient, SHSServer
|
2023-10-30 04:47:28 +00:00
|
|
|
|
from secret_handshake.boxstream import BoxStreamKeys
|
2023-10-29 08:55:39 +00:00
|
|
|
|
|
2023-10-30 12:36:22 +00:00
|
|
|
|
from .helpers import AsyncBuffer
|
2018-02-04 14:41:30 +00:00
|
|
|
|
|
|
|
|
|
|
2023-10-29 08:55:39 +00:00
|
|
|
|
class DummyCrypto:
|
2018-02-04 14:41:30 +00:00
|
|
|
|
"""Dummy crypto module, pretends everything is fine."""
|
2023-10-29 08:47:57 +00:00
|
|
|
|
|
2023-10-30 04:47:28 +00:00
|
|
|
|
def verify_server_challenge(self, _: bytes) -> bool:
|
2023-10-29 08:55:39 +00:00
|
|
|
|
"""Verify the server challenge"""
|
|
|
|
|
|
2018-02-04 14:41:30 +00:00
|
|
|
|
return True
|
|
|
|
|
|
2023-10-30 04:47:28 +00:00
|
|
|
|
def verify_challenge(self, _: bytes) -> bool:
|
2023-10-29 08:55:39 +00:00
|
|
|
|
"""Verify the challenge data"""
|
|
|
|
|
|
2018-02-04 14:41:30 +00:00
|
|
|
|
return True
|
|
|
|
|
|
2023-10-30 04:47:28 +00:00
|
|
|
|
def verify_server_accept(self, _: bytes) -> bool:
|
2023-10-29 08:55:39 +00:00
|
|
|
|
"""Verify server’s accept message"""
|
2023-10-30 04:47:28 +00:00
|
|
|
|
|
2018-02-04 14:41:30 +00:00
|
|
|
|
return True
|
|
|
|
|
|
2023-10-30 04:47:28 +00:00
|
|
|
|
def generate_challenge(self) -> bytes:
|
2023-10-29 08:55:39 +00:00
|
|
|
|
"""Generate authentication challenge"""
|
|
|
|
|
|
2023-10-29 08:47:57 +00:00
|
|
|
|
return b"CHALLENGE"
|
2018-02-04 14:41:30 +00:00
|
|
|
|
|
2023-10-30 04:47:28 +00:00
|
|
|
|
def generate_client_auth(self) -> bytes:
|
2023-10-29 08:55:39 +00:00
|
|
|
|
"""Generate client authentication data"""
|
|
|
|
|
|
2023-10-29 08:47:57 +00:00
|
|
|
|
return b"AUTH"
|
2018-02-04 14:41:30 +00:00
|
|
|
|
|
2023-10-30 04:47:28 +00:00
|
|
|
|
def verify_client_auth(self, _: bytes) -> bool:
|
2023-10-29 08:55:39 +00:00
|
|
|
|
"""Verify client authentication data"""
|
|
|
|
|
|
2018-02-04 14:41:30 +00:00
|
|
|
|
return True
|
|
|
|
|
|
2023-10-30 04:47:28 +00:00
|
|
|
|
def generate_accept(self) -> bytes:
|
2023-10-29 08:55:39 +00:00
|
|
|
|
"""Generate an ACCEPT message"""
|
|
|
|
|
|
2023-10-29 08:47:57 +00:00
|
|
|
|
return b"ACCEPT"
|
2018-02-04 14:41:30 +00:00
|
|
|
|
|
2023-10-30 04:47:28 +00:00
|
|
|
|
def get_box_keys(self) -> BoxStreamKeys:
|
2023-10-29 08:55:39 +00:00
|
|
|
|
"""Get box keys"""
|
|
|
|
|
|
2018-02-04 14:41:30 +00:00
|
|
|
|
return {
|
2023-10-29 08:47:57 +00:00
|
|
|
|
"encrypt_key": b"x" * 32,
|
|
|
|
|
"encrypt_nonce": b"x" * 32,
|
|
|
|
|
"decrypt_key": b"x" * 32,
|
|
|
|
|
"decrypt_nonce": b"x" * 32,
|
2023-10-30 04:47:28 +00:00
|
|
|
|
"shared_secret": b"x" * 32,
|
2018-02-04 14:41:30 +00:00
|
|
|
|
}
|
|
|
|
|
|
2023-10-30 04:47:28 +00:00
|
|
|
|
def clean(self) -> None:
|
2023-10-29 08:55:39 +00:00
|
|
|
|
"""Clean up internal data"""
|
2018-02-04 14:41:30 +00:00
|
|
|
|
|
|
|
|
|
|
2023-10-30 04:47:28 +00:00
|
|
|
|
def _dummy_boxstream(stream: AsyncBuffer, **_: Any) -> AsyncBuffer:
|
|
|
|
|
"""Identity boxstream, no transformation."""
|
|
|
|
|
|
2018-02-04 14:41:30 +00:00
|
|
|
|
return stream
|
|
|
|
|
|
|
|
|
|
|
2023-10-30 04:47:28 +00:00
|
|
|
|
def _client_stream_mocker() -> (
|
|
|
|
|
Tuple[AsyncBuffer, AsyncBuffer, Callable[[str, int], Awaitable[Tuple[AsyncBuffer, AsyncBuffer]]]]
|
|
|
|
|
):
|
2023-10-29 08:47:57 +00:00
|
|
|
|
reader = AsyncBuffer(b"xxx")
|
|
|
|
|
writer = AsyncBuffer(b"xxx")
|
2018-02-04 14:41:30 +00:00
|
|
|
|
|
2023-10-30 04:47:28 +00:00
|
|
|
|
async def _create_mock_streams(
|
|
|
|
|
host: str, port: int # pylint: disable=unused-argument
|
|
|
|
|
) -> Tuple[AsyncBuffer, AsyncBuffer]:
|
2018-02-04 14:41:30 +00:00
|
|
|
|
return reader, writer
|
|
|
|
|
|
|
|
|
|
return reader, writer, _create_mock_streams
|
|
|
|
|
|
|
|
|
|
|
2023-10-30 04:47:28 +00:00
|
|
|
|
def _server_stream_mocker() -> (
|
|
|
|
|
Tuple[
|
|
|
|
|
AsyncBuffer,
|
|
|
|
|
AsyncBuffer,
|
|
|
|
|
Callable[[Callable[[AsyncBuffer, AsyncBuffer], Awaitable[None]], str, int], Awaitable[None]],
|
|
|
|
|
]
|
|
|
|
|
):
|
2023-10-29 08:47:57 +00:00
|
|
|
|
reader = AsyncBuffer(b"xxx")
|
|
|
|
|
writer = AsyncBuffer(b"xxx")
|
2018-02-04 14:41:30 +00:00
|
|
|
|
|
2023-10-30 04:47:28 +00:00
|
|
|
|
async def _create_mock_server(
|
|
|
|
|
cb: Callable[[AsyncBuffer, AsyncBuffer], Awaitable[None]],
|
|
|
|
|
host: str, # pylint: disable=unused-argument
|
|
|
|
|
port: int, # pylint: disable=unused-argument
|
|
|
|
|
) -> None:
|
2018-02-04 14:41:30 +00:00
|
|
|
|
await cb(reader, writer)
|
|
|
|
|
|
|
|
|
|
return reader, writer, _create_mock_server
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.asyncio
|
2023-10-30 04:47:28 +00:00
|
|
|
|
async def test_client(mocker: MockerFixture) -> None:
|
2023-10-29 08:55:39 +00:00
|
|
|
|
"""Test the client"""
|
|
|
|
|
|
|
|
|
|
reader, _, _create_mock_streams = _client_stream_mocker()
|
2023-10-30 04:47:28 +00:00
|
|
|
|
mocker.patch("secret_handshake.network.open_connection", new=_create_mock_streams)
|
2023-10-29 08:47:57 +00:00
|
|
|
|
mocker.patch("secret_handshake.boxstream.BoxStream", new=_dummy_boxstream)
|
|
|
|
|
mocker.patch("secret_handshake.boxstream.UnboxStream", new=_dummy_boxstream)
|
2018-02-04 14:41:30 +00:00
|
|
|
|
|
2023-10-29 08:47:57 +00:00
|
|
|
|
client = SHSClient("shop.local", 1111, SigningKey.generate(), os.urandom(32))
|
2023-10-30 04:47:28 +00:00
|
|
|
|
client.crypto = DummyCrypto() # type: ignore[assignment]
|
2018-02-04 14:41:30 +00:00
|
|
|
|
|
|
|
|
|
await client.open()
|
2023-10-29 08:47:57 +00:00
|
|
|
|
reader.append(b"TEST")
|
|
|
|
|
assert (await client.read()) == b"TEST"
|
2018-02-04 14:41:30 +00:00
|
|
|
|
client.disconnect()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.asyncio
|
2023-10-30 04:47:28 +00:00
|
|
|
|
async def test_server(mocker: MockerFixture) -> None:
|
2023-10-29 08:55:39 +00:00
|
|
|
|
"""Test the server"""
|
2018-02-04 14:41:30 +00:00
|
|
|
|
|
|
|
|
|
resolve = Event()
|
|
|
|
|
|
2023-10-30 04:47:28 +00:00
|
|
|
|
async def _on_connect(_: Any) -> None:
|
2018-02-04 14:41:30 +00:00
|
|
|
|
server.disconnect()
|
|
|
|
|
resolve.set()
|
|
|
|
|
|
2023-10-29 08:55:39 +00:00
|
|
|
|
_, _, _create_mock_server = _server_stream_mocker()
|
2023-10-30 04:47:28 +00:00
|
|
|
|
mocker.patch("secret_handshake.network.start_server", new=_create_mock_server)
|
2023-10-29 08:47:57 +00:00
|
|
|
|
mocker.patch("secret_handshake.boxstream.BoxStream", new=_dummy_boxstream)
|
|
|
|
|
mocker.patch("secret_handshake.boxstream.UnboxStream", new=_dummy_boxstream)
|
2018-02-04 14:41:30 +00:00
|
|
|
|
|
2023-10-29 08:47:57 +00:00
|
|
|
|
server = SHSServer("shop.local", 1111, SigningKey.generate(), os.urandom(32))
|
2023-10-30 04:47:28 +00:00
|
|
|
|
server.crypto = DummyCrypto() # type: ignore[assignment]
|
2018-02-04 14:41:30 +00:00
|
|
|
|
|
|
|
|
|
server.on_connect(_on_connect)
|
|
|
|
|
|
|
|
|
|
await server.listen()
|
|
|
|
|
await wait_for(resolve.wait(), 5)
|