-
Notifications
You must be signed in to change notification settings - Fork 36
Expand file tree
/
Copy pathhandler.go
More file actions
609 lines (513 loc) · 14.8 KB
/
handler.go
File metadata and controls
609 lines (513 loc) · 14.8 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
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
// Copyright 2022-2026 Contributors to the Veraison project.
// SPDX-License-Identifier: Apache-2.0
package api
import (
"crypto/rand"
"encoding/base64"
"encoding/json"
"errors"
"fmt"
"io"
"net/http"
"path"
"strconv"
"strings"
"time"
"github.com/denisbrodbeck/machineid"
"github.com/gin-gonic/gin"
"github.com/google/uuid"
"github.com/lestrrat-go/jwx/v2/jwk"
"github.com/veraison/cmw"
"github.com/veraison/services/capability"
"github.com/veraison/services/log"
"github.com/veraison/services/verification/sessionmanager"
"github.com/veraison/services/verification/verifier"
"go.uber.org/zap"
)
const (
ChallengeResponseSessionMediaType = "application/vnd.veraison.challenge-response-session+json"
)
var (
ErrInternal = errors.New("internal error")
)
var (
tenantID = "0"
defaultCacheMaxAge = 60 * time.Second
)
type IHandler interface {
NewChallengeResponse(c *gin.Context)
SubmitEvidence(c *gin.Context)
GetSession(c *gin.Context)
DelSession(c *gin.Context)
GetWellKnownVerificationInfo(c *gin.Context)
}
type Handler struct {
SessionManager sessionmanager.ISessionManager
Verifier verifier.IVerifier
WkCacheMaxAge time.Duration
logger *zap.SugaredLogger
}
func NewHandler(sm sessionmanager.ISessionManager, v verifier.IVerifier, wkCacheMaxAge string) IHandler {
logger := log.Named("api-handler")
return &Handler{
SessionManager: sm,
Verifier: v,
WkCacheMaxAge: capability.ParseCacheMaxAge(wkCacheMaxAge, defaultCacheMaxAge, logger),
logger: logger,
}
}
var (
ConfigNonceSize uint8 = 32
ConfigSessionTTL, _ = time.ParseDuration("2m30s")
)
// mintSessionID creates a version 1 UUID based on a unique machine ID, clock
// sequence and current time. Routing to the correct node can therefore happen
// based on the NodeID part of the UUID (i.e., octets 10-15).
func mintSessionID() (uuid.UUID, error) {
mid, err := machineid.ID()
if err != nil {
return uuid.UUID{}, err
}
uuid.SetNodeID([]byte(mid))
return uuid.NewUUID()
}
// mintNonce creates a random nonce of nonceSz bytes. nonceSz must be strictly
// positive
func mintNonce(nonceSz uint8) ([]byte, error) {
if nonceSz == 0 {
return nil, errors.New("nonce size cannot be 0")
}
n := make([]byte, nonceSz)
_, err := rand.Read(n)
if err != nil {
return nil, fmt.Errorf("%w: %v", ErrInternal, err)
}
return n, nil
}
// aToU8 attempts at converting the supplied string into an uint8 value
func aToU8(v string) (uint8, error) {
u8, err := strconv.ParseUint(v, 10, 8)
if err != nil {
return ^uint8(0), err
}
return uint8(u8), nil
}
// b64ToBytes attempts at converting the supplied b64-encoded string into a byte
// slice
func b64ToBytes(v string) ([]byte, error) {
b, err := base64.URLEncoding.DecodeString(v)
if err != nil {
return nil, err
}
return b, nil
}
// parseNonceRequest tries to devise the nonce value to be used for the session
// given the user-supplied query parameters
func parseNonceRequest(nonceParam, nonceSizeParam string) ([]byte, error) {
// both nonce and nonceSize have been supplied
if nonceParam != "" && nonceSizeParam != "" {
return nil, errors.New("nonce and nonceSize are mutually exclusive")
}
// no explicit request was made, use the default nonce size to mint a
// new nonce
if nonceParam == "" && nonceSizeParam == "" {
return mintNonce(ConfigNonceSize)
}
// a nonceSize was supplied, try to use it to mint a new nonce
if nonceSizeParam != "" {
nonceSize, err := aToU8(nonceSizeParam)
if err != nil || nonceSize < 8 || nonceSize > 64 {
return nil, errors.New("nonceSize must be in range 8..64")
}
return mintNonce(nonceSize)
}
// nonce was supplied, try to see if the encoding is valid
nonce, err := b64ToBytes(nonceParam)
if err != nil {
return nil, errors.New("nonce must be valid base64")
}
nonceLen := len(nonce)
if nonceLen < 8 || nonceLen > 64 {
return nil, fmt.Errorf(
"nonce must be between 8 and 64 bytes long; got %d",
nonceLen,
)
}
return nonce, nil
}
func newSession(nonce []byte, supportedMediaTypes []string, ttl time.Duration) (uuid.UUID, []byte, error) {
id, err := mintSessionID()
if err != nil {
return uuid.UUID{}, nil, err
}
session := &ChallengeResponseSession{
id: id.String(),
Status: StatusWaiting, // start in waiting status
Nonce: nonce,
Expiry: time.Now().Add(ttl), // RFC3339 format, with sub-second precision added if present
Accept: supportedMediaTypes,
}
jsonSession, err := json.Marshal(session)
if err != nil {
return uuid.UUID{}, nil, err
}
return id, jsonSession, nil
}
func lookupSession(sm sessionmanager.ISessionManager, id uuid.UUID, tenantID string) (*ChallengeResponseSession, error) {
session, err := sm.GetSession(id, tenantID)
if err != nil {
return nil, err
}
var s ChallengeResponseSession
err = json.Unmarshal(session, &s)
if err != nil {
return nil, err
}
return &s, nil
}
func storeSession(sm sessionmanager.ISessionManager, session *ChallengeResponseSession, id uuid.UUID, tenantID string) ([]byte, error) {
b, err := json.Marshal(session)
if err != nil {
return nil, err
}
err = sm.SetSession(id, tenantID, b, ConfigSessionTTL)
if err != nil {
return nil, err
}
return b, nil
}
func mustStoreSession(sm sessionmanager.ISessionManager, session *ChallengeResponseSession, id uuid.UUID, tenantID string) []byte {
s, err := storeSession(sm, session, id, tenantID)
if err != nil {
panic(err)
}
return s
}
func readSessionIDFromRequestURI(c *gin.Context) (uuid.UUID, error) {
uriPathSegment := c.Param("id")
id, err := uuid.Parse(uriPathSegment)
if err != nil {
return uuid.UUID{}, fmt.Errorf("invalid session id (%s) in path segment: %w", c.Request.URL.Path, err)
}
return id, nil
}
func (o *Handler) GetSession(c *gin.Context) {
// do content negotiation (accept application/vnd.veraison.challenge-response-session+json)
offered := c.NegotiateFormat(ChallengeResponseSessionMediaType)
if offered != ChallengeResponseSessionMediaType {
ReportProblem(c,
http.StatusNotAcceptable,
fmt.Sprintf("the only supported output format is %s", ChallengeResponseSessionMediaType),
)
return
}
id, err := readSessionIDFromRequestURI(c)
if err != nil {
ReportProblem(c,
http.StatusBadRequest,
err.Error(),
)
return
}
// load session from request URI
session, err := lookupSession(o.SessionManager, id, tenantID)
if err != nil {
ReportProblem(c,
http.StatusNotFound,
err.Error(),
)
return
}
c.Header("Content-Type", ChallengeResponseSessionMediaType)
c.JSON(http.StatusOK, session)
}
func (o *Handler) DelSession(c *gin.Context) {
id, err := readSessionIDFromRequestURI(c)
if err != nil {
ReportProblem(c,
http.StatusBadRequest,
err.Error(),
)
return
}
if err = o.SessionManager.DelSession(id, tenantID); err != nil {
ReportProblem(c,
http.StatusInternalServerError,
err.Error(),
)
return
}
c.Status(http.StatusNoContent)
}
var cmwMap = map[string]bool{
"application/cmw": true,
"application/cmw+json": true,
"application/cmw+cbor": true,
"application/vnd.veraison.cmw": true,
"application/vnd.veraison.cmw+cbor": true,
"application/vnd.veraison.cmw+json": true,
}
func isCMW(mt string) bool {
_, ok := cmwMap[mt]
return ok
}
func (o *Handler) SubmitEvidence(c *gin.Context) {
// do content negotiation (accept application/vnd.veraison.challenge-response-session+json)
offered := c.NegotiateFormat(ChallengeResponseSessionMediaType)
if offered != ChallengeResponseSessionMediaType {
ReportProblem(c,
http.StatusNotAcceptable,
fmt.Sprintf("the only supported output format is %s", ChallengeResponseSessionMediaType),
)
return
}
// read body (i.e., evidence)
evidence, err := io.ReadAll(c.Request.Body)
if err != nil || len(evidence) == 0 {
o.logger.Error("unable to read evidence from the request body: %v", err)
ReportProblem(c,
http.StatusBadRequest,
"unable to read evidence from the request body",
)
return
}
// read content-type and check against supported attestation formats
mediaType := c.Request.Header.Get("Content-Type")
if isCMW(mediaType) {
var w cmw.CMW
if err := w.Deserialize(evidence); err != nil {
ReportProblem(c,
http.StatusBadRequest,
fmt.Sprintf("could not unwrap the CMW: %v", err),
)
return
}
// Here we only deal with CMW records and tags. Collection CMWs are
// dealt with in the non-exceptional path.
if w.GetKind() == cmw.KindMonad {
mediaType, err = w.GetMonadType()
if err != nil {
ReportProblem(c,
http.StatusBadRequest,
fmt.Sprintf("could not extract CMW media type: %v", err),
)
}
evidence, err = w.GetMonadValue()
if err != nil {
ReportProblem(c,
http.StatusBadRequest,
fmt.Sprintf("could not extract CMW value: %v", err),
)
}
}
}
isSupported, err := o.Verifier.IsSupportedMediaType(mediaType)
if err != nil {
status := http.StatusInternalServerError
if errors.Unwrap(err) == verifier.ErrInputParam {
status = http.StatusBadRequest
}
ReportProblem(c, status, fmt.Sprintf("could not check media type with verifier: %v", err))
return
}
if !isSupported {
supportedMediaTypes, err := o.Verifier.SupportedMediaTypes()
if err != nil {
ReportProblem(c,
http.StatusInternalServerError,
fmt.Sprintf("could not get supported media types from verifier: %v",
err),
)
return
}
c.Header("Accept", strings.Join(supportedMediaTypes, ", "))
ReportProblem(c,
http.StatusUnsupportedMediaType,
fmt.Sprintf("no active plugin found for %s", mediaType),
)
return
}
id, err := readSessionIDFromRequestURI(c)
if err != nil {
ReportProblem(c,
http.StatusBadRequest,
err.Error(),
)
return
}
// load session from request URI
session, err := lookupSession(o.SessionManager, id, tenantID)
if err != nil {
ReportProblem(c,
http.StatusNotFound,
err.Error(),
)
return
}
// Forward the evidence to the verifier. We expect the verifier to be
// able to cope with bad evidence, so the error here should only be
// reported if something in the verifier or the connection goes wrong.
// Any problems with the evidence are expected to be reported via the
// attestation result.
attestationResult, err := o.Verifier.ProcessEvidence(tenantID, session.Nonce,
evidence, mediaType)
if err != nil {
o.logger.Error(err)
session.SetStatus(StatusFailed)
mustStoreSession(o.SessionManager, session, id, tenantID)
ReportProblem(c,
http.StatusInternalServerError,
"error encountered while processing evidence",
)
return
}
// From here onwards, any signalling to the client (including failure
// is done through the session object.
session.SetEvidence(mediaType, evidence)
// async (202)
if attestationResult == nil {
session.SetStatus(StatusProcessing)
s := mustStoreSession(o.SessionManager, session, id, tenantID)
sendChallengeResponseSessionWithStatus(c, http.StatusAccepted, s)
return
}
// sync (200)
session.SetStatus(StatusComplete)
session.SetResult(attestationResult)
s := mustStoreSession(o.SessionManager, session, id, tenantID)
sendChallengeResponseSessionWithStatus(c, http.StatusOK, s)
}
func (o *Handler) NewChallengeResponse(c *gin.Context) {
offered := c.NegotiateFormat(ChallengeResponseSessionMediaType)
if offered != ChallengeResponseSessionMediaType {
ReportProblem(c,
http.StatusNotAcceptable,
fmt.Sprintf("the only supported output format is %s", ChallengeResponseSessionMediaType),
)
return
}
// parse query to devise the nonce
nonce, err := parseNonceRequest(c.Query("nonce"), c.Query("nonceSize"))
if err != nil {
status := http.StatusBadRequest
if errors.Is(err, ErrInternal) {
status = http.StatusInternalServerError
}
ReportProblem(c,
status,
fmt.Sprintf("failed handling nonce request: %s", err),
)
return
}
supportedMediaTypes, err := o.Verifier.SupportedMediaTypes()
if err != nil {
ReportProblem(c,
http.StatusInternalServerError,
fmt.Sprintf("could not get media types form verifier: %v", err),
)
return
}
id, session, err := newSession(nonce, supportedMediaTypes, ConfigSessionTTL)
if err != nil {
ReportProblem(c,
http.StatusInternalServerError,
err.Error(),
)
return
}
err = o.SessionManager.SetSession(id, tenantID, session, ConfigSessionTTL)
if err != nil {
ReportProblem(c,
http.StatusInternalServerError,
err.Error(),
)
return
}
sendChallengeResponseSessionCreated(c, id.String(), session)
}
func sendChallengeResponseSessionWithStatus(c *gin.Context, status int, jsonSession []byte) {
c.Data(status, ChallengeResponseSessionMediaType, jsonSession)
}
func sendChallengeResponseSessionCreated(c *gin.Context, id string, jsonSession []byte) {
c.Header("Location", path.Join("session", id))
sendChallengeResponseSessionWithStatus(c, http.StatusCreated, jsonSession)
}
func (o *Handler) getKey() (jwk.Key, error) {
var key jwk.Key
protoKey, err := o.Verifier.GetPublicKey()
if err != nil {
return key, err
}
key, err = jwk.ParseKey([]byte(protoKey.Key))
if err != nil {
return key, err
}
return key, nil
}
func (o *Handler) getVerificationMediaTypes() ([]string, error) {
return o.Verifier.SupportedMediaTypes()
}
func (o *Handler) getVerificationServerVersionAndState() (string, string, error) {
vtsState, err := o.Verifier.GetVTSState()
if err != nil {
return "", "", err
}
version := vtsState.ServerVersion
state := vtsState.Status.String()
return version, state, nil
}
func getVerificationEndpoints() map[string]string {
return publicApiMap
}
func (o *Handler) GetWellKnownVerificationInfo(c *gin.Context) {
offered := c.NegotiateFormat(capability.WellKnownMediaType)
if offered != capability.WellKnownMediaType && offered != gin.MIMEJSON {
ReportProblem(c,
http.StatusNotAcceptable,
fmt.Sprintf("the only supported output format is %s", capability.WellKnownMediaType),
)
return
}
// Get public key
key, err := o.getKey()
if err != nil {
ReportProblem(c,
http.StatusInternalServerError,
err.Error(),
)
return
}
// Get verification media types
mediaTypes, err := o.getVerificationMediaTypes()
if err != nil {
ReportProblem(c,
http.StatusInternalServerError,
err.Error(),
)
return
}
// Get verification server version and state
version, state, err := o.getVerificationServerVersionAndState()
if err != nil {
ReportProblem(c,
http.StatusInternalServerError,
err.Error(),
)
return
}
// Get verification endpoints
endpoints := getVerificationEndpoints()
// Get final object with well known information
obj, err := capability.NewWellKnownInfoObj(key, mediaTypes, nil, version, state, endpoints)
if err != nil {
ReportProblem(c,
http.StatusInternalServerError,
err.Error(),
)
return
}
c.Header("Cache-Control", fmt.Sprintf("max-age=%d", int64(o.WkCacheMaxAge.Seconds())))
c.Header("Expires", time.Now().Add(o.WkCacheMaxAge).UTC().Format(time.RFC1123))
c.Header("Content-Type", capability.WellKnownMediaType)
c.JSON(http.StatusOK, obj)
}