1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
|
// Package session manages secure communication sessions with Forward Secrecy
package session
import (
"crypto/rand"
"encoding/hex"
"fmt"
"sync"
"time"
"github.com/flynn/noise"
)
// Session represents a secure communication session
type Session struct {
ID string
RemotePubKey string // Remote peer's public key (hex)
HandshakeState *noise.HandshakeState
CipherState *noise.CipherState
CreatedAt time.Time
LastActivity time.Time
IsInitiator bool
}
// SessionManager manages multiple sessions
type SessionManager struct {
sessions map[string]*Session // key = session ID
mu sync.RWMutex
}
// NewSessionManager creates a new session manager
func NewSessionManager() *SessionManager {
return &SessionManager{
sessions: make(map[string]*Session),
}
}
// CreateSession creates a new Noise Protocol session
func (sm *SessionManager) CreateSession(remotePubKey string, isInitiator bool) (*Session, error) {
sm.mu.Lock()
defer sm.mu.Unlock()
// Generate session ID
sessionID := generateSessionID()
// Create Noise handshake configuration
// Using Noise_XX_25519_ChaChaPoly_BLAKE2b pattern for mutual authentication
var hs *noise.HandshakeState
var err error
config := noise.Config{
CipherSuite: noise.NewCipherSuite(noise.DH25519, noise.CipherChaChaPoly, noise.HashBLAKE2b),
Pattern: noise.HandshakeXX,
Initiator: isInitiator,
}
// Generate ephemeral keypair
keypair, err := noise.DH25519.GenerateKeypair(rand.Reader)
if err != nil {
return nil, fmt.Errorf("failed to generate keypair: %w", err)
}
config.StaticKeypair = keypair
hs, err = noise.NewHandshakeState(config)
if err != nil {
return nil, fmt.Errorf("failed to create handshake state: %w", err)
}
session := &Session{
ID: sessionID,
RemotePubKey: remotePubKey,
HandshakeState: hs,
CreatedAt: time.Now(),
LastActivity: time.Now(),
IsInitiator: isInitiator,
}
sm.sessions[sessionID] = session
return session, nil
}
// GetSession retrieves a session by ID
func (sm *SessionManager) GetSession(sessionID string) (*Session, bool) {
sm.mu.RLock()
defer sm.mu.RUnlock()
session, exists := sm.sessions[sessionID]
return session, exists
}
// RemoveSession removes a session and securely wipes it
func (sm *SessionManager) RemoveSession(sessionID string) {
sm.mu.Lock()
defer sm.mu.Unlock()
if session, exists := sm.sessions[sessionID]; exists {
session.SecureWipe()
delete(sm.sessions, sessionID)
}
}
// SecureWipeAll securely wipes all sessions
func (sm *SessionManager) SecureWipeAll() {
sm.mu.Lock()
defer sm.mu.Unlock()
for _, session := range sm.sessions {
session.SecureWipe()
}
sm.sessions = make(map[string]*Session)
}
// ListSessions returns all active sessions
func (sm *SessionManager) ListSessions() []*Session {
sm.mu.RLock()
defer sm.mu.RUnlock()
sessions := make([]*Session, 0, len(sm.sessions))
for _, session := range sm.sessions {
sessions = append(sessions, session)
}
return sessions
}
// CleanupExpiredSessions removes sessions older than the specified duration
func (sm *SessionManager) CleanupExpiredSessions(maxAge time.Duration) int {
sm.mu.Lock()
defer sm.mu.Unlock()
now := time.Now()
removed := 0
for id, session := range sm.sessions {
if now.Sub(session.LastActivity) > maxAge {
session.SecureWipe()
delete(sm.sessions, id)
removed++
}
}
return removed
}
// UpdateActivity updates the last activity timestamp
func (sm *SessionManager) UpdateActivity(sessionID string) {
sm.mu.Lock()
defer sm.mu.Unlock()
if session, exists := sm.sessions[sessionID]; exists {
session.LastActivity = time.Now()
}
}
// Count returns the number of active sessions
func (sm *SessionManager) Count() int {
sm.mu.RLock()
defer sm.mu.RUnlock()
return len(sm.sessions)
}
// SecureWipe zeros out sensitive session data
func (s *Session) SecureWipe() {
// Wipe cipher state if exists
if s.CipherState != nil {
// Noise library doesn't expose key wiping, but we can nil the reference
s.CipherState = nil
}
// Wipe handshake state
if s.HandshakeState != nil {
s.HandshakeState = nil
}
// Zero out session ID
s.ID = ""
s.RemotePubKey = ""
}
// generateSessionID generates a random session ID
func generateSessionID() string {
id := make([]byte, 16)
rand.Read(id)
return hex.EncodeToString(id)
}
// String returns a safe string representation
func (s *Session) String() string {
role := "responder"
if s.IsInitiator {
role = "initiator"
}
return fmt.Sprintf("Session(id=%s, role=%s, remote=%s...)",
s.ID[:8], role, s.RemotePubKey[:16])
}
// String returns session manager info
func (sm *SessionManager) String() string {
sm.mu.RLock()
defer sm.mu.RUnlock()
return fmt.Sprintf("SessionManager(active=%d)", len(sm.sessions))
}
|