{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "a39d1eee",
   "metadata": {},
   "source": [
    "# CR81 · SSH transport, user authentication and channels\n",
    "\n",
    "SSH first authenticates the server host key at transport layer, then authenticates the user, then opens multiplexed channels. Host-key trust and user-key authorization are distinct.\n",
    "\n",
    "Foundational concepts; prerequisite: CR75.\n",
    "\n",
    "**Scope:** SSH state model; no sshd started or credentials installed."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "id": "concept081",
   "source": [
    "## SSH authentication layers\n",
    "\n",
    "SSH first establishes an encrypted transport and verifies server host identity, then authenticates the user, then opens channels. Host and user keys serve different trust decisions.\n",
    "\n",
    "**Mathematical / protocol foundations:** Version exchange and key exchange establish session material; host signature proves possession of host key, checked against a trusted entry. User authentication can use a public key or other configured method. Channel open is distinct from authentication.\n",
    "\n",
    "**Worked teaching example and interpretation:** A valid user credential must not override a host-key mismatch. Compare isolated modeled decisions; avoid connecting to unauthorized external hosts.\n",
    "\n",
    "Compare corresponding states; for randomized or timed runs compare invariants rather than literal random bytes or timings.\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "network81",
   "metadata": {},
   "source": [
    "## SSH authentication order\n",
    "\n",
    "Models the SSH transport, server host identity, user authentication and connection layers. Server host-key authentication and user authorization solve different problems. This is a policy state machine, not an SSH client or server."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "8fdcbbf7",
   "metadata": {},
   "outputs": [],
   "source": [
    "# ===== CR81 | Live algorithm and verification =====\n",
    "# The function returns actual modeled intermediate states and checks the result.\n",
    "# ------------------------------------------------------------\n",
    "# PSUT IoT Engineering Studio | CR81\n",
    "# Experiment: ssh transport user authentication and channels\n",
    "# Purpose: expose algorithm states and verify the experiment invariant.\n",
    "# ------------------------------------------------------------\n",
    "# CR81 | SSH transport, user authentication and channels\n",
    "# Execute the named operation; expose states and assert the laboratory invariant.\n",
    "import json\n",
    "\n",
    "\n",
    "\n",
    "DEFAULT={'host_trusted': True, 'user_authorized': True}\n",
    "ALTERNATE={'host_trusted': False}\n",
    "\n",
    "def run(overrides=None):\n",
    "    # Merge only explicitly supplied inputs with the lecture baseline.\n",
    "    params={**DEFAULT, **(overrides or {})}\n",
    "    host_trusted=params['host_trusted']\n",
    "    user_authorized=params['user_authorized']\n",
    "    if type(host_trusted) is not bool or type(user_authorized) is not bool:\n",
    "        raise ValueError('Invalid classroom input')\n",
    "    steps=[{'state':'version exchange and key exchange'},{'state':'verify host key','trusted':host_trusted}]\n",
    "    if host_trusted:steps.append({'state':'verify user public-key proof','authorized':user_authorized})\n",
    "    accepted=host_trusted and user_authorized;steps.append({'state':'open session channel' if accepted else 'abort'});result={'channel_open':accepted}\n",
    "    # Independently check the stated mathematical or protocol invariant.\n",
    "    assert result[\"channel_open\"]==(host_trusted and user_authorized), \"verification failed\"\n",
    "    return {\"steps\":steps,\"result\":result}\n",
    "\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c6383cf6",
   "metadata": {},
   "source": [
    "## Baseline calculation\n",
    "Inspect each trace row. The final result is separate from intermediate states."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "04ea2c8b",
   "metadata": {},
   "outputs": [],
   "source": [
    "# ===== CR81 | 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": "ceb3f9bd",
   "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": "4386e9ae",
   "metadata": {},
   "outputs": [],
   "source": [
    "# ===== CR81 | 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": "86ef4c41",
   "metadata": {},
   "source": [
    "## Controlled investigation\n",
    "\n",
    "Select trusted/untrusted host and authorized/unauthorized user in all four combinations. Record whether host key checking succeeds, whether user authentication is reached, and whether the session channel opens. Explain why accepting an unknown host is not a substitute for user proof.\n",
    "\n",
    "Models the SSH transport, server host identity, user authentication and connection layers. Server host-key authentication and user authorization solve different problems. This is a policy state machine, not an SSH client or server."
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "name": "python",
   "version": "3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}