# dbgr18/n2-sctp-middleware architecture

> Architecture diagram of the GitHub repository dbgr18/n2-sctp-middleware, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-04-08). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/dbgr18/n2-sctp-middleware
- Repository: https://github.com/dbgr18/n2-sctp-middleware

## Overview

1. **Repository purpose**
   - This repo implements a 5G N2 “zero trust” bridge between a UERANSIM gNB and a free5GC AMF.
   - The system splits into a **control plane** for certificate enrollment and a **data plane** for NGAP forwarding.

2. **Top-level executable boundaries**
   - `cmd/gnb_proxy/main.go`: runs the gNB-side proxy.
   - `cmd/middleware/main.go`: runs the server-side middleware that acts as CA, enrollment service, and traffic relay.
   - `cmd/mock_gnb/main.go`: a separate test binary, likely used to simulate a gNB or exercise the proxy path.

3. **Control plane flow: JWT + CSR enrollment**
   - `gnb_proxy` generates or loads a local private key, creates a CSR, and calls `middleware /enroll` over HTTPS.
   - The request is authorized with a short-lived JWT.
   - `middleware` verifies the JWT, validates the CSR, and signs a client certificate using its Root CA.

4. **Data plane flow: SCTP over QUIC mTLS**
   - UERANSIM gNB connects to `gnb_proxy` using **SCTP**.
   - `gnb_proxy` encapsulates SCTP payloads, including stream/PPID metadata, into **QUIC stream** frames.
   - `middleware` unwraps the QUIC stream and forwards the traffic to the AMF over **SCTP**.
   - Return traffic follows the reverse path.

5. **Security and trust model**
   - `middleware` owns the trust root: it auto-generates or loads the Root CA and its own server certificate.
   - `gnb_proxy` holds only its own private key locally; its client cert is obtained through enrollment.
   - Both sides trust the same CA file, enabling **mTLS** on the QUIC channel.
   - JWT is only for enrollment; it does not protect the data plane.

6. **Role of `middleware`**
   - It is the central security and forwarding boundary.
   - Responsibilities:
     - initialize/load CA material,
     - expose `POST /enroll` over HTTPS,
     - require and verify client certificates on QUIC,
     - bridge QUIC traffic to AMF SCTP and back.

7. **Role of `gnb_proxy`**
   - It is the gNB-facing edge component.
   - Responsibilities:
     - accept local SCTP from UERANSIM,
     - ensure a usable client certificate exists,
     - perform enrollment when needed,
     - establish QUIC mTLS to middleware,
     - relay NGAP payloads across the encrypted channel.

8. **Deployment topology encoded in config**
   - `config.yaml` contains all key endpoints and file paths.
   - Important groups:
     - Enrollment HTTPS endpoint and JWT settings.
     - QUIC listen/connect addresses and keepalive/timeout knobs.
     - SCTP listen/bind/target addresses for gNB Proxy and AMF.
     - PKI file locations for CA, server cert, and client cert/key.
   - The README’s default `dummy0` topology uses multiple IPs on one host to simulate separate gNB, proxy, middleware, and AMF nodes.

9. **External systems and integration points**
   - **UERANSIM gNB** is the SCTP client that must point its NGAP target at `gnb_proxy`.
   - **free5GC AMF** is the SCTP server that receives forwarded NGAP from `middleware`.
   - The repo is therefore not a full 5G stack; it is an interconnect/security layer between two external 5G components.

10. **State and persistence boundaries**
   - The system persists security state on the local filesystem:
     - CA cert/key,
     - middleware server cert/key,
     - gnb_proxy client cert/key.
   - This file-based PKI state is what lets the proxy restart without re-enrolling unless certs expire or are missing.

11. **Operational startup order**
   - `middleware` should start first so it can create the CA and serve enrollment + QUIC.
   - `gnb_proxy` starts next, enrolls if necessary, then brings up the QUIC session.
   - Only after that should UERANSIM gNB connect to the proxy.
   - The repo’s included shell script `setup_dummy0.sh` supports the single-host test topology.

12. **Useful diagram anchors**
   - Draw two parallel lanes:
     - **Control plane:** `gnb_proxy -> HTTPS /enroll -> middleware CA/signing`
     - **Data plane:** `UERANSIM SCTP -> gnb_proxy -> QUIC mTLS -> middleware -> SCTP -> AMF`
   - Show `middleware` as both a security authority and traffic relay, while `gnb_proxy` is the edge termination point for the gNB.

Main entry points: [`cmd/gnb_proxy/main.go`](https://github.com/dbgr18/n2-sctp-middleware/blob/main/cmd/gnb_proxy/main.go) (gNB proxy), [`cmd/middleware/main.go`](https://github.com/dbgr18/n2-sctp-middleware/blob/main/cmd/middleware/main.go) (Middleware), [`config.yaml`](https://github.com/dbgr18/n2-sctp-middleware/blob/main/config.yaml) (Config), [`setup_dummy0.sh`](https://github.com/dbgr18/n2-sctp-middleware/blob/main/setup_dummy0.sh) (Dummy setup)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_grp_control["Control plane"]
  node_n_enroll["Enrollment API<br/>HTTPS /enroll"]
  node_n_jwt(("JWT auth<br/>token gate"))
  node_n_csr["CSR flow<br/>key enrollment"]
  node_n_ca[("Root CA<br/>trust anchor")]
  node_n_server_pki["Server certs<br/>server identity"]
  node_n_client_pki["Client certs<br/>proxy identity"]
end

subgraph group_grp_data["Data plane"]
  node_n_sctp_ingress["SCTP ingress<br/>gNB-facing socket"]
  node_n_quic_link["QUIC mTLS<br/>encrypted transport"]
  node_n_relay{{"Relay core<br/>packet bridge"}}
  node_n_amf_sctp["AMF SCTP<br/>core network socket"]
end

subgraph group_grp_ops["Operations"]
  node_n_gnb_proxy_bin["gNB proxy<br/>binary<br/>[main.go]"]
  node_n_middleware_bin["Middleware<br/>binary<br/>[main.go]"]
  node_n_mock_gnb_bin["Mock gNB<br/>binary<br/>[main.go]"]
  node_n_config["Config<br/>[config.yaml]"]
  node_n_setup["Dummy setup<br/>deploy script<br/>[setup_dummy0.sh]"]
end

subgraph group_grp_external["External systems"]
  node_n_ueransim(("UERANSIM gNB<br/>external gNB"))
  node_n_amf(("free5GC AMF<br/>external AMF"))
end

node_n_config -->|"drives"| node_n_gnb_proxy_bin
node_n_config -->|"drives"| node_n_middleware_bin
node_n_config -->|"bootstraps"| node_n_setup
node_n_middleware_bin -->|"owns"| node_n_ca
node_n_middleware_bin -->|"serves with"| node_n_server_pki
node_n_gnb_proxy_bin -->|"persists"| node_n_client_pki
node_n_gnb_proxy_bin -->|"requests"| node_n_csr
node_n_csr -->|"POST /enroll"| node_n_enroll
node_n_jwt -->|"authorizes"| node_n_enroll
node_n_enroll -->|"signs with"| node_n_ca
node_n_enroll -->|"issues"| node_n_client_pki
node_n_ueransim -->|"connects"| node_n_sctp_ingress
node_n_sctp_ingress -->|"terminates at"| node_n_gnb_proxy_bin
node_n_gnb_proxy_bin -->|"establishes"| node_n_quic_link
node_n_middleware_bin -->|"accepts"| node_n_quic_link
node_n_gnb_proxy_bin -->|"encapsulates"| node_n_relay
node_n_relay -->|"forwards"| node_n_middleware_bin
node_n_middleware_bin -->|"bridges to"| node_n_amf_sctp
node_n_amf_sctp -->|"delivers to"| node_n_amf
node_n_amf -->|"returns"| node_n_amf_sctp
node_n_setup -->|"starts first"| node_n_middleware_bin
node_n_setup -->|"starts next"| node_n_gnb_proxy_bin

click node_n_gnb_proxy_bin "https://github.com/dbgr18/n2-sctp-middleware/blob/main/cmd/gnb_proxy/main.go"
click node_n_middleware_bin "https://github.com/dbgr18/n2-sctp-middleware/blob/main/cmd/middleware/main.go"
click node_n_mock_gnb_bin "https://github.com/dbgr18/n2-sctp-middleware/blob/main/cmd/mock_gnb/main.go"
click node_n_config "https://github.com/dbgr18/n2-sctp-middleware/blob/main/config.yaml"
click node_n_setup "https://github.com/dbgr18/n2-sctp-middleware/blob/main/setup_dummy0.sh"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_n_enroll,node_n_jwt,node_n_csr,node_n_ca,node_n_server_pki,node_n_client_pki toneBlue
class node_n_sctp_ingress,node_n_quic_link,node_n_relay,node_n_amf_sctp toneAmber
class node_n_gnb_proxy_bin,node_n_middleware_bin,node_n_mock_gnb_bin,node_n_config,node_n_setup toneMint
class node_n_ueransim,node_n_amf toneRose
```

## Components

### Control plane

Certificate enrollment and trust bootstrap

- **Enrollment API**: HTTPS /enroll · JWT-authorized CSR enrollment endpoint exposed by middleware
- **JWT auth**: token gate · Short-lived authorization used only for enrollment
- **CSR flow**: key enrollment · Client key generation, CSR creation, and certificate request on gNB proxy
- **Root CA**: trust anchor · Middleware-owned CA used to sign server and client certificates
- **Server certs**: server identity · Middleware server certificate and private key for HTTPS and QUIC mTLS
- **Client certs**: proxy identity · gNB proxy client certificate and private key persisted locally

### Data plane

NGAP relay over QUIC and SCTP

- **SCTP ingress**: gNB-facing socket · UERANSIM connects here with NGAP over SCTP
- **QUIC mTLS**: encrypted transport · Authenticated bidirectional tunnel carrying encapsulated SCTP payloads
- **Relay core**: packet bridge · Encapsulates SCTP metadata into QUIC streams and unwraps return traffic
- **AMF SCTP**: core network socket · Middleware forwards NGAP to the free5GC AMF over SCTP

### Operations

Top-level binaries and deployment config

- [gNB proxy](https://github.com/dbgr18/n2-sctp-middleware/blob/main/cmd/gnb_proxy/main.go): binary · Edge process that terminates local SCTP and connects to middleware over QUIC mTLS · `cmd/gnb_proxy/main.go`
- [Middleware](https://github.com/dbgr18/n2-sctp-middleware/blob/main/cmd/middleware/main.go): binary · Central process that owns PKI, serves enrollment, and relays traffic · `cmd/middleware/main.go`
- [Mock gNB](https://github.com/dbgr18/n2-sctp-middleware/blob/main/cmd/mock_gnb/main.go): binary · Test binary for exercising the proxy path · `cmd/mock_gnb/main.go`
- [Config](https://github.com/dbgr18/n2-sctp-middleware/blob/main/config.yaml): deployment config · Shared endpoints, PKI paths, transport addresses, and timeout knobs · `config.yaml`
- [Dummy setup](https://github.com/dbgr18/n2-sctp-middleware/blob/main/setup_dummy0.sh): deploy script · Single-host topology bootstrap for the dummy0 test environment · `setup_dummy0.sh`

### External systems

5G endpoints outside this repository

- **UERANSIM gNB**: external gNB · Upstream SCTP client that sends NGAP into the proxy
- **free5GC AMF**: external AMF · Downstream SCTP server receiving forwarded NGAP

## Connections

- Config → gNB proxy: drives · Config supplies listen/connect addresses, file locations, and transport knobs
- Config → Middleware: drives · Middleware loads CA, server cert, and relay endpoints from config
- Config → Dummy setup: bootstraps · Script uses the dummy topology described in configuration and README
- Middleware → Root CA: owns · Middleware creates or loads the trust root
- Middleware → Server certs: serves with · Middleware uses its server certificate for HTTPS and QUIC mTLS
- gNB proxy → Client certs: persists · Proxy stores its local private key and enrolled client certificate
- gNB proxy → CSR flow: requests · Proxy generates or loads a key and creates a CSR when needed
- CSR flow → Enrollment API: POST /enroll · CSR and JWT are sent to the enrollment endpoint
- JWT auth → Enrollment API: authorizes · Enrollment only accepts short-lived JWTs
- Enrollment API → Root CA: signs with · Middleware signs the client certificate using its root CA
- Enrollment API → Client certs: issues · Enrollment returns a client certificate chain for the proxy
- UERANSIM gNB → SCTP ingress: connects · UERANSIM gNB opens SCTP toward the proxy
- SCTP ingress → gNB proxy: terminates at · Proxy receives upstream NGAP over local SCTP
- gNB proxy → QUIC mTLS: establishes · Proxy connects to middleware over QUIC with mTLS
- Middleware → QUIC mTLS: accepts · Middleware validates client certificates on the QUIC channel
- gNB proxy → Relay core: encapsulates · Proxy wraps SCTP payloads and stream metadata into QUIC frames
- Relay core → Middleware: forwards · Middleware unwraps relay traffic and routes it onward
- Middleware → AMF SCTP: bridges to · Middleware forwards NGAP over SCTP to the AMF side
- AMF SCTP → free5GC AMF: delivers to · AMF receives forwarded NGAP traffic
- free5GC AMF → AMF SCTP: returns · Response traffic follows the reverse path back through the middleware
- Dummy setup → Middleware: starts first · Dummy topology expects middleware to initialize CA and listeners before the proxy
- Dummy setup → gNB proxy: starts next · Proxy enrolls and establishes the QUIC session after middleware is up
