How dbgr18/n2-sctp-middleware works
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: gnb_proxy/main.go gNB proxy · middleware/main.go Middleware · config.yaml Config · setup_dummy0.sh Dummy setup
Components 17
Control plane
Certificate enrollment and trust bootstrap
- Enrollment API · JWT-authorized CSR enrollment endpoint exposed by middleware
- JWT auth · Short-lived authorization used only for enrollment
- CSR flow · Client key generation, CSR creation, and certificate request on gNB proxy
- Root CA · Middleware-owned CA used to sign server and client certificates
- Server certs · Middleware server certificate and private key for HTTPS and QUIC mTLS
- Client certs · gNB proxy client certificate and private key persisted locally
Data plane
NGAP relay over QUIC and SCTP
- SCTP ingress · UERANSIM connects here with NGAP over SCTP
- QUIC mTLS · Authenticated bidirectional tunnel carrying encapsulated SCTP payloads
- Relay core · Encapsulates SCTP metadata into QUIC streams and unwraps return traffic
- AMF SCTP · Middleware forwards NGAP to the free5GC AMF over SCTP
Operations
Top-level binaries and deployment config
- gNB proxy · Edge process that terminates local SCTP and connects to middleware over QUIC mTLS
cmd/gnb_proxy/main.go - Middleware · Central process that owns PKI, serves enrollment, and relays traffic
cmd/middleware/main.go - Mock gNB · Test binary for exercising the proxy path
cmd/mock_gnb/main.go - Config · Shared endpoints, PKI paths, transport addresses, and timeout knobs
config.yaml - Dummy setup · Single-host topology bootstrap for the dummy0 test environment
setup_dummy0.sh
External systems
5G endpoints outside this repository
- UERANSIM gNB · Upstream SCTP client that sends NGAP into the proxy
- free5GC AMF · Downstream SCTP server receiving forwarded NGAP
Connections 22
- 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