-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.go
More file actions
312 lines (275 loc) · 8.92 KB
/
Copy pathserver.go
File metadata and controls
312 lines (275 loc) · 8.92 KB
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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
package securelog
import (
"crypto/tls"
"encoding/gob"
"encoding/json"
"fmt"
"io"
"net/http"
"strings"
"sync"
pb "github.com/karasz/securelog/proto"
"google.golang.org/protobuf/proto"
)
// Server provides HTTPS endpoints for the trusted server T.
// It receives commitments, closures, and log files for verification.
type Server struct {
TrustedServer *TrustedServer
mu sync.RWMutex
stores map[string]Store // Map of logID -> Store for verification
tlsConfig *tls.Config
}
// NewServer creates a new HTTPS server for trusted server T.
func NewServer() *Server {
return &Server{
TrustedServer: NewTrustedServer(),
stores: make(map[string]Store),
}
}
// SetTLSConfig clones cfg and stores it for use when serving HTTPS requests.
// If cfg is nil a default configuration will be used.
func (s *Server) SetTLSConfig(cfg *tls.Config) {
if cfg == nil {
s.tlsConfig = nil
return
}
s.tlsConfig = cfg.Clone()
}
// RegisterStore associates a log ID with its storage backend.
// Required before verification can be performed.
func (s *Server) RegisterStore(logID string, store Store) {
s.mu.Lock()
defer s.mu.Unlock()
s.stores[logID] = store
}
// isProtobuf checks if the request content type is protobuf.
func isProtobuf(r *http.Request) bool {
contentType := r.Header.Get("Content-Type")
return strings.HasPrefix(contentType, "application/x-protobuf") ||
strings.HasPrefix(contentType, "application/protobuf")
}
// decodeInitCommitment decodes InitCommitment from either Gob or Protobuf.
func decodeInitCommitment(r *http.Request) (InitCommitment, error) {
if isProtobuf(r) {
body, err := io.ReadAll(r.Body)
if err != nil {
return InitCommitment{}, fmt.Errorf("read body: %w", err)
}
var pbCommit pb.InitCommitment
if err := proto.Unmarshal(body, &pbCommit); err != nil {
return InitCommitment{}, fmt.Errorf("unmarshal protobuf: %w", err)
}
return FromProtoInitCommitment(&pbCommit)
}
// Default to Gob for backward compatibility
var commit InitCommitment
if err := gob.NewDecoder(r.Body).Decode(&commit); err != nil {
return InitCommitment{}, fmt.Errorf("decode gob: %w", err)
}
return commit, nil
}
// decodeOpenMessage decodes OpenMessage from either Gob or Protobuf.
func decodeOpenMessage(r *http.Request) (OpenMessage, error) {
if isProtobuf(r) {
body, err := io.ReadAll(r.Body)
if err != nil {
return OpenMessage{}, fmt.Errorf("read body: %w", err)
}
var pbOpen pb.OpenMessage
if err := proto.Unmarshal(body, &pbOpen); err != nil {
return OpenMessage{}, fmt.Errorf("unmarshal protobuf: %w", err)
}
return FromProtoOpenMessage(&pbOpen)
}
// Default to Gob
var open OpenMessage
if err := gob.NewDecoder(r.Body).Decode(&open); err != nil {
return OpenMessage{}, fmt.Errorf("decode gob: %w", err)
}
return open, nil
}
// decodeCloseMessage decodes CloseMessage from either Gob or Protobuf.
func decodeCloseMessage(r *http.Request) (CloseMessage, error) {
if isProtobuf(r) {
body, err := io.ReadAll(r.Body)
if err != nil {
return CloseMessage{}, fmt.Errorf("read body: %w", err)
}
var pbClose pb.CloseMessage
if err := proto.Unmarshal(body, &pbClose); err != nil {
return CloseMessage{}, fmt.Errorf("unmarshal protobuf: %w", err)
}
return FromProtoCloseMessage(&pbClose)
}
// Default to Gob
var closeMsg CloseMessage
if err := gob.NewDecoder(r.Body).Decode(&closeMsg); err != nil {
return CloseMessage{}, fmt.Errorf("decode gob: %w", err)
}
return closeMsg, nil
}
// decodeVerifyRequest decodes verify request from either Gob or Protobuf.
func decodeVerifyRequest(r *http.Request) (string, []Record, error) {
// Extract logID from path
logID := r.URL.Path[len("/api/v1/logs/"):]
logID = logID[:len(logID)-len("/verify")]
if isProtobuf(r) {
body, err := io.ReadAll(r.Body)
if err != nil {
return "", nil, fmt.Errorf("read body: %w", err)
}
var pbReq pb.VerifyRequest
if err := proto.Unmarshal(body, &pbReq); err != nil {
return "", nil, fmt.Errorf("unmarshal protobuf: %w", err)
}
records, err := FromProtoRecords(pbReq.Records)
if err != nil {
return "", nil, fmt.Errorf("convert records: %w", err)
}
return pbReq.LogId, records, nil
}
// Default to Gob
var records []Record
if err := gob.NewDecoder(r.Body).Decode(&records); err != nil {
return "", nil, fmt.Errorf("decode gob: %w", err)
}
return logID, records, nil
}
// encodeVerifyResponse encodes verify response in the appropriate format.
func encodeVerifyResponse(w http.ResponseWriter, r *http.Request, logID string, verified bool, errMsg string) error {
if isProtobuf(r) {
resp := &pb.VerifyResponse{
Verified: verified,
ErrorMessage: errMsg,
}
data, err := proto.Marshal(resp)
if err != nil {
return err
}
w.Header().Set("Content-Type", "application/x-protobuf")
w.WriteHeader(http.StatusOK)
_, err = w.Write(data)
return err
}
// Default to JSON
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
return json.NewEncoder(w).Encode(map[string]any{
"status": "verified",
"log_id": logID,
"verified": verified,
})
}
// HandleRegister handles POST /api/v1/logs/register - initial commitment.
// Supports both Gob and Protocol Buffer encoding.
func (s *Server) HandleRegister(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodPost {
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
return
}
commit, err := decodeInitCommitment(r)
if err != nil {
http.Error(w, fmt.Sprintf("Invalid commitment: %v", err), http.StatusBadRequest)
return
}
s.TrustedServer.RegisterLog(commit)
w.WriteHeader(http.StatusOK)
_ = json.NewEncoder(w).Encode(map[string]string{
"status": "registered",
"log_id": commit.LogID,
})
}
// HandleOpen handles POST /api/v1/logs/open - log opening notification.
// Supports both Gob and Protocol Buffer encoding.
func (s *Server) HandleOpen(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodPost {
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
return
}
open, err := decodeOpenMessage(r)
if err != nil {
http.Error(w, fmt.Sprintf("Invalid open message: %v", err), http.StatusBadRequest)
return
}
s.TrustedServer.RegisterOpen(open)
w.WriteHeader(http.StatusOK)
_ = json.NewEncoder(w).Encode(map[string]string{
"status": "opened",
"log_id": open.LogID,
})
}
// HandleClose handles POST /api/v1/logs/close - log closure notification.
// Supports both Gob and Protocol Buffer encoding.
func (s *Server) HandleClose(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodPost {
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
return
}
closeMsg, err := decodeCloseMessage(r)
if err != nil {
http.Error(w, fmt.Sprintf("Invalid closure: %v", err), http.StatusBadRequest)
return
}
if err := s.TrustedServer.AcceptClosure(closeMsg); err != nil {
http.Error(w, fmt.Sprintf("Accept closure failed: %v", err), http.StatusBadRequest)
return
}
w.WriteHeader(http.StatusOK)
_ = json.NewEncoder(w).Encode(map[string]string{
"status": "closed",
"log_id": closeMsg.LogID,
})
}
// HandleVerify handles POST /api/v1/logs/{logID}/verify - final verification.
// Supports both Gob and Protocol Buffer encoding.
func (s *Server) HandleVerify(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodPost {
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
return
}
logID, records, err := decodeVerifyRequest(r)
if err != nil {
http.Error(w, fmt.Sprintf("Invalid request: %v", err), http.StatusBadRequest)
return
}
// Perform verification
if err := s.TrustedServer.FinalVerify(logID, records); err != nil {
// Send error response in appropriate format
if encErr := encodeVerifyResponse(w, r, logID, false, err.Error()); encErr != nil {
http.Error(w, fmt.Sprintf("Verification failed: %v", err), http.StatusUnauthorized)
}
return
}
// Send success response
if err := encodeVerifyResponse(w, r, logID, true, ""); err != nil {
http.Error(w, fmt.Sprintf("Failed to encode response: %v", err), http.StatusInternalServerError)
}
}
// SetupRoutes configures HTTP routes for the trusted server.
func (s *Server) SetupRoutes(mux *http.ServeMux) {
mux.HandleFunc("/api/v1/logs/register", s.HandleRegister)
mux.HandleFunc("/api/v1/logs/open", s.HandleOpen)
mux.HandleFunc("/api/v1/logs/close", s.HandleClose)
mux.HandleFunc("/api/v1/logs/", s.HandleVerify) // Catch-all for verify
}
func (s *Server) tlsConfigWithDefaults() *tls.Config {
if s.tlsConfig == nil {
return &tls.Config{MinVersion: tls.VersionTLS12}
}
cfg := s.tlsConfig.Clone()
if cfg.MinVersion == 0 {
cfg.MinVersion = tls.VersionTLS12
}
return cfg
}
// ListenAndServeTLS starts the HTTPS server for trusted server T.
func (s *Server) ListenAndServeTLS(addr, certFile, keyFile string) error {
mux := http.NewServeMux()
s.SetupRoutes(mux)
server := &http.Server{
Addr: addr,
Handler: mux,
TLSConfig: s.tlsConfigWithDefaults(),
}
return server.ListenAndServeTLS(certFile, keyFile)
}