{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "bb809c53",
   "metadata": {},
   "source": [
    "# CR82 · SSH tunnel endpoints and trust boundaries\n",
    "\n",
    "Local forwarding carries bytes from a client-side listening socket over SSH to a server-side destination. Remote forwarding reverses the listener location. Encryption applies to the SSH segment, not automatically to destination traffic.\n",
    "\n",
    "Foundational concepts; prerequisite: CR81.\n",
    "\n",
    "**Scope:** Topology model only; no tunnel or public listener created."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "id": "concept082",
   "source": [
    "## SSH tunnelling\n",
    "\n",
    "SSH forwarding transports another connection through an encrypted channel. Encryption applies between SSH endpoints; the connection from remote SSH endpoint to destination may have separate protection.\n",
    "\n",
    "**Mathematical / protocol foundations:** For local forwarding, a local listener accepts a connection, sends data through SSH, and the remote endpoint connects to the authorized destination. Host identity and destination service identity are distinct.\n",
    "\n",
    "**Worked teaching example and interpretation:** A local test service bound to loopback can be modeled as a separate endpoint behind an authenticated SSH server. Mark exactly which links are encrypted; do not assume end-to-end protection beyond the tunnel.\n",
    "\n",
    "Compare corresponding states; for randomized or timed runs compare invariants rather than literal random bytes or timings.\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "network82",
   "metadata": {},
   "source": [
    "## SSH forwarding direction\n",
    "\n",
    "For local forwarding the listener is client-side, destination is reached from server-side; remote forwarding reverses the listener and destination sides. The model assumes loopback bind and a permitted forwarding request. No SSH service or external endpoint is contacted."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "a7b2920e",
   "metadata": {},
   "outputs": [],
   "source": [
    "# ===== CR82 | Live algorithm and verification =====\n",
    "# The function returns actual modeled intermediate states and checks the result.\n",
    "# ------------------------------------------------------------\n",
    "# PSUT IoT Engineering Studio | CR82\n",
    "# Experiment: ssh tunnel endpoints and trust boundaries\n",
    "# Purpose: expose algorithm states and verify the experiment invariant.\n",
    "# ------------------------------------------------------------\n",
    "# CR82 | SSH tunnel endpoints and trust boundaries\n",
    "# Execute the named operation; expose states and assert the laboratory invariant.\n",
    "import json\n",
    "\n",
    "\n",
    "\n",
    "DEFAULT={'direction': 'local', 'destination_port': 8000}\n",
    "ALTERNATE={'direction': 'remote'}\n",
    "\n",
    "def run(overrides=None):\n",
    "    # Merge only explicitly supplied inputs with the lecture baseline.\n",
    "    params={**DEFAULT, **(overrides or {})}\n",
    "    direction=params['direction']\n",
    "    destination_port=params['destination_port']\n",
    "    if direction not in ('local','remote') or type(destination_port) is not int or not 1 <= destination_port <= 65535:raise ValueError('invalid forwarding direction')\n",
    "    listener='client loopback' if direction=='local' else 'server loopback';target='server loopback' if direction=='local' else 'client loopback';steps=[{'from':'client application' if direction=='local' else 'server application','to':listener,'protected_by_SSH':False},{'from':listener,'to':target,'protected_by_SSH':True},{'from':target,'to':f'authorized localhost:{destination_port}','protected_by_SSH':False}];result={'listener':listener,'destination':target,'public_listener':False}\n",
    "    # Independently check the stated mathematical or protocol invariant.\n",
    "    assert not result[\"public_listener\"], \"verification failed\"\n",
    "    return {\"steps\":steps,\"result\":result}\n",
    "\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d4dda66e",
   "metadata": {},
   "source": [
    "## Baseline calculation\n",
    "Inspect each trace row. The final result is separate from intermediate states."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "6a7c8966",
   "metadata": {},
   "outputs": [],
   "source": [
    "# ===== CR82 | Execute the baseline =====\n",
    "# ===== Execute the baseline =====\n",
    "# Run the default worked example; inspect the intermediate states in order.\n",
    "baseline=run()\n",
    "for step in baseline[\"steps\"]: print(step)\n",
    "print(\"Result:\",baseline[\"result\"])"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "088b7c28",
   "metadata": {},
   "source": [
    "## Controlled comparison\n",
    "Change the indicated input; leave other inputs unchanged. A baseline is the reference run. A mathematical verification substitutes the result back into its defining equation."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "9c1cbe61",
   "metadata": {},
   "outputs": [],
   "source": [
    "# ===== CR82 | Compare the specified alternative =====\n",
    "# ===== Compare the specified alternative =====\n",
    "# Override only the chosen input(s), leaving remaining parameters at baseline.\n",
    "comparison=run(ALTERNATE)\n",
    "for step in comparison[\"steps\"]: print(step)\n",
    "print(\"Result:\",comparison[\"result\"])"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e41b8905",
   "metadata": {},
   "source": [
    "## Controlled investigation\n",
    "\n",
    "Keep destination port 8000. Compare local and remote forwarding; then choose 8080 and repeat. Sketch client, server, loopback listener and destination. Mark the protected middle segment; explain why SSH does not automatically encrypt the endpoint-to-application segments.\n",
    "\n",
    "For local forwarding the listener is client-side, destination is reached from server-side; remote forwarding reverses the listener and destination sides. The model assumes loopback bind and a permitted forwarding request. No SSH service or external endpoint is contacted."
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "name": "python",
   "version": "3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}