-
Notifications
You must be signed in to change notification settings - Fork 1
/
vfy.go
254 lines (226 loc) · 7.13 KB
/
vfy.go
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
package vfy
import (
"context"
"errors"
"fmt"
"log"
"strings"
"github.com/adem-wg/adem-proto/pkg/consts"
"github.com/adem-wg/adem-proto/pkg/ident"
"github.com/adem-wg/adem-proto/pkg/tokens"
"github.com/adem-wg/adem-proto/pkg/util"
"github.com/lestrrat-go/jwx/v2/jwa"
"github.com/lestrrat-go/jwx/v2/jwk"
"github.com/lestrrat-go/jwx/v2/jws"
"github.com/lestrrat-go/jwx/v2/jwt"
)
type VerificationResults struct {
results []VerificationResult
protected []*ident.AI
issuer string
endorsedBy []string
}
func ResultInvalid() VerificationResults {
return VerificationResults{results: []VerificationResult{INVALID}}
}
func (res VerificationResults) Print() {
lns := []string{"Verified set of tokens. Results:"}
resultsStrs := make([]string, 0, len(res.results))
for _, r := range res.results {
resultsStrs = append(resultsStrs, r.String())
}
lns = append(lns, fmt.Sprintf("- Security levels: %s", strings.Join(resultsStrs, ", ")))
if len(res.protected) > 0 {
assets := make([]string, 0, len(res.protected))
for _, ass := range res.protected {
assets = append(assets, ass.String())
}
lns = append(lns, fmt.Sprintf("- Protected assets: %s", strings.Join(assets, ", ")))
}
if res.issuer != "" {
lns = append(lns, fmt.Sprintf("- Issuer of emblem: %s", res.issuer))
}
if len(res.endorsedBy) > 0 {
lns = append(lns, fmt.Sprintf("- Issuer endorsed by: %s", strings.Join(res.endorsedBy, ", ")))
}
log.Print(strings.Join(lns, "\n"))
}
type VerificationResult byte
func (vr VerificationResult) String() string {
switch vr {
case UNSIGNED:
return "UNSIGNED"
case INVALID:
return "INVALID"
case SIGNED:
return "SIGNED"
case ORGANIZATIONAL:
return "ORGANIZATIONAL"
case ENDORSED:
return "ENDORSED"
case SIGNED_TRUSTED:
return "SIGNED_TRUSTED"
case ORGANIZATIONAL_TRUSTED:
return "ORGANIZATIONAL_TRUSTED"
case ENDORSED_TRUSTED:
return "ENDORSED_TRUSTED"
default:
return ""
}
}
const UNSIGNED VerificationResult = 0
const INVALID VerificationResult = 1
const SIGNED VerificationResult = 2
const ORGANIZATIONAL VerificationResult = 3
const ENDORSED VerificationResult = 4
const SIGNED_TRUSTED VerificationResult = 5
const ORGANIZATIONAL_TRUSTED VerificationResult = 6
const ENDORSED_TRUSTED VerificationResult = 7
var ErrTokenNonCompact = errors.New("token is not in compact serialization")
type TokenVerificationResult struct {
token *ADEMToken
err error
}
// Verify an ADEM token's signature. Designed to be called asynchronously.
// Results will be returned to the [results] channel. Verification keys will be
// obtained from [km].
// Every call to [vfyToken] will write to [results] exactly once.
func vfyToken(rawToken []byte, km *keyManager, results chan *TokenVerificationResult) {
result := TokenVerificationResult{}
defer func() { results <- &result }()
jwtT, err := jwt.Parse(rawToken, jwt.WithKeyProvider(km))
if err != nil {
result.err = err
return
}
if msg, err := jws.Parse(rawToken); err != nil {
result.err = err
return
} else if len(msg.Signatures()) > 1 {
result.err = ErrTokenNonCompact
return
} else if ademT, err := MkADEMToken(km, msg.Signatures()[0], jwtT); err != nil {
result.err = err
return
} else {
result.token = ademT
}
}
// Verify a slice of ADEM tokens.
func VerifyTokens(rawTokens [][]byte, trustedKeys jwk.Set) VerificationResults {
// Early termination for empty rawTokens slice
if len(rawTokens) == 0 {
return ResultInvalid()
}
// Ensure trustedKeys is non-nil
if trustedKeys == nil {
trustedKeys = jwk.NewSet()
}
// We maintain a thread count for termination purposes. It might be that we
// cannot verify all token's verification key and must cancel verification.
threadCount := len(rawTokens)
km := NewKeyManager(len(rawTokens))
// Put trusted public keys into key manager. This allows for termination for
// tokens without issuer.
ctx := context.TODO()
iter := trustedKeys.Keys(ctx)
for iter.Next(ctx) {
km.put(iter.Pair().Value.(jwk.Key))
}
results := make(chan *TokenVerificationResult)
// Start verification threads
for _, rawToken := range rawTokens {
go vfyToken(rawToken, km, results)
}
// Wait until all verification threads obtained a verification key promise.
km.waitForInit()
ts := []*ADEMToken{}
for {
// [waiting] is the number of unresolved promises in the key manager, i.e.,
// blocked threads that wait for a verification key.
// [threadCount] is the number of threads that could still provide
// a new verification key in the [results] channel.
// If there are as many waiting threads as threads that could result in a
// new verification, we miss verification keys and verification will be
// aborted.
if waiting := km.waiting(); waiting > 0 && waiting == threadCount {
km.killListeners()
} else if result := <-results; result == nil {
// All threads have terminated
break
} else {
// We got a new non-nil result from <-results, and hence, one thread must
// have terminated. Decrement the counter accordingly.
threadCount -= 1
// Every call to [vfyToken] will write exactly one result. Hence, only
// close the [results] channel, when all threads have terminated.
if threadCount == 0 {
close(results)
}
if result.err != nil {
log.Printf("discarding invalid token: %s", result.err)
} else {
ts = append(ts, result.token)
if k, ok := result.token.Token.Get("key"); ok {
km.put(k.(tokens.EmbeddedKey).Key)
}
}
}
}
var emblem *ADEMToken
var protected []*ident.AI
endorsements := []*ADEMToken{}
for _, t := range ts {
if t.Headers.ContentType() == string(consts.EmblemCty) {
if emblem != nil {
// Multiple emblems
log.Print("Token set contains multiple emblems")
return ResultInvalid()
} else if err := jwt.Validate(t.Token, jwt.WithValidator(tokens.EmblemValidator)); err != nil {
log.Printf("Invalid emblem: %s", err)
return ResultInvalid()
} else {
emblem = t
}
ass, _ := emblem.Token.Get("ass")
protected = ass.([]*ident.AI)
if emblem.Headers.Algorithm() == jwa.NoSignature {
return VerificationResults{
results: []VerificationResult{UNSIGNED},
protected: protected,
}
}
} else if t.Headers.ContentType() == string(consts.EndorsementCty) {
err := jwt.Validate(t.Token, jwt.WithValidator(tokens.EndorsementValidator))
if err != nil {
log.Printf("Invalid endorsement: %s", err)
} else {
endorsements = append(endorsements, t)
}
} else {
log.Printf("Token has wrong type: %s", t.Headers.ContentType())
}
}
if emblem == nil {
log.Print("no emblem found")
return ResultInvalid()
}
vfyResults, root := verifySignedOrganizational(emblem, endorsements, trustedKeys)
if util.Contains(vfyResults, INVALID) {
return ResultInvalid()
}
var endorsedResults []VerificationResult
var endorsedBy []string
if util.Contains(vfyResults, ORGANIZATIONAL) {
endorsedResults, endorsedBy = verifyEndorsed(emblem, root, endorsements, trustedKeys)
}
if util.Contains(endorsedResults, INVALID) {
return ResultInvalid()
}
return VerificationResults{
results: append(vfyResults, endorsedResults...),
issuer: root.Token.Issuer(),
endorsedBy: endorsedBy,
protected: protected,
}
}