Feat: One-way TLS transport for the device port

Add optional one-way TLS on the device port (6543), enabled per client via a
pinned trust anchor, so new clients encrypt their transport while legacy
plaintext clients keep working unchanged.

Server (server/go): sniff each accepted connection and wrap TLS peers
(ClientHello 0x16 0x03 + handshake type 0x01) in crypto/tls with MinVersion
TLS 1.2; everything else stays on the plaintext path. TLS is enabled only when
both YAMA_TLS_CERT and YAMA_TLS_KEY are set, and a cert/key load failure fails
startup loudly rather than silently serving plaintext.

Client (client/, Windows): Schannel one-way TLS. When the trust anchor is
present — registry value Software\<exehash>\settings\tls_cert (REG_SZ holding
the PEM), with fallback to settings.ini [settings] tls_cert or yama-ca.pem
beside the exe — every outbound connection (main + sub-connections) completes
a TLS 1.2 handshake before the app protocol and is strictly pinned to that
anchor via a chain engine with hExclusiveRoot; any handshake/cert failure is
fail-closed (no plaintext downgrade). Send uses EncryptMessage; Recv uses
DecryptMessage and reads plaintext from the stream-mode DATA segment (bufs[1])
rather than the record header.

Co-Authored-By: deepseek-v4-pro
This commit is contained in:
yuanyuanxianganddeepseek-v4-pro committed 2026-10-14 21:10:21 +02:00
1 parent 8d4c118ae6
commit ca0d7626d2
16 files changed
+1418 -5

No files matched your search

+22 -1
View File
@@ -2,6 +2,7 @@ package server
import (
"context"
"crypto/tls"
"io"
"net"
"sync"
@@ -40,6 +41,13 @@ type Config struct {
PerIPMaxConnections int // max concurrent connections per source IP (0 = unlimited)
PerIPConnRateLimit int // max new connections per PerIPConnRateWindow per IP (0 = unlimited)
PerIPConnRateWindow time.Duration // window for PerIPConnRateLimit
// TLSCertFile / TLSKeyFile enable optional one-way TLS on the device port.
// When both are set the server loads the cert and auto-detects TLS clients
// by their ClientHello; plaintext clients keep working unchanged. Both empty
// (the default) disables TLS and behaves exactly as before.
TLSCertFile string // server certificate (PEM)
TLSKeyFile string // server private key (PEM)
}
// DefaultConfig returns default configuration
@@ -75,6 +83,10 @@ type Server struct {
parser *protocol.Parser
ipLimit *ipLimiter
// tlsCfg is non-nil when one-way TLS is enabled (cert + key configured).
// wrapTLS consults it to sniff and wrap incoming connections.
tlsCfg *tls.Config
running atomic.Bool
wg sync.WaitGroup
ctx context.Context
@@ -234,6 +246,12 @@ func (s *Server) SetLogger(l *logger.Logger) {
// Start starts the server
func (s *Server) Start() error {
// Load optional one-way TLS cert up front so a bad path/key fails startup
// loudly rather than silently serving plaintext.
if err := s.loadTLSConfig(); err != nil {
return err
}
addr := net.JoinHostPort("0.0.0.0", itoa(s.config.Port))
listener, err := net.Listen("tcp", addr)
if err != nil {
@@ -360,7 +378,10 @@ func (s *Server) acceptLoop() {
}
// handleConnection handles a single connection
func (s *Server) handleConnection(conn net.Conn) {
func (s *Server) handleConnection(rawConn net.Conn) {
// Sniff and (when TLS is enabled) wrap TLS peers; plaintext passes through.
conn := s.wrapTLS(rawConn)
// Create context
ctx := connection.NewContext(conn, nil)