-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathassertion.go
312 lines (263 loc) · 8.53 KB
/
assertion.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
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 sdk
import (
"encoding/json"
"errors"
"fmt"
"github.com/gowebpki/jcs"
"github.com/lestrrat-go/jwx/v2/jwa"
"github.com/lestrrat-go/jwx/v2/jwt"
"github.com/opentdf/platform/lib/ocrypto"
)
// AssertionConfig is a shadow of Assertion with the addition of the signing key.
// It is used on creation
type AssertionConfig struct {
ID string `validate:"required"`
Type AssertionType `validate:"required"`
Scope Scope `validate:"required"`
AppliesToState AppliesToState `validate:"required"`
Statement Statement
SigningKey AssertionKey
}
type Assertion struct {
ID string `json:"id"`
Type AssertionType `json:"type"`
Scope Scope `json:"scope"`
AppliesToState AppliesToState `json:"appliesToState,omitempty"`
Statement Statement `json:"statement"`
Binding Binding `json:"binding,omitempty"`
}
var errAssertionVerifyKeyFailure = errors.New("assertion: failed to verify with provided key")
// Sign signs the assertion with the given hash and signature using the key.
// It returns an error if the signing fails.
// The assertion binding is updated with the method and the signature.
func (a *Assertion) Sign(hash, sig string, key AssertionKey) error {
tok := jwt.New()
if err := tok.Set(kAssertionHash, hash); err != nil {
return fmt.Errorf("failed to set assertion hash: %w", err)
}
if err := tok.Set(kAssertionSignature, sig); err != nil {
return fmt.Errorf("failed to set assertion signature: %w", err)
}
// sign the hash and signature
signedTok, err := jwt.Sign(tok, jwt.WithKey(jwa.KeyAlgorithmFrom(key.Alg.String()), key.Key))
if err != nil {
return fmt.Errorf("signing assertion failed: %w", err)
}
// set the binding
a.Binding.Method = JWS.String()
a.Binding.Signature = string(signedTok)
return nil
}
// Verify checks the binding signature of the assertion and
// returns the hash and the signature. It returns an error if the verification fails.
func (a Assertion) Verify(key AssertionKey) (string, string, error) {
tok, err := jwt.Parse([]byte(a.Binding.Signature),
jwt.WithKey(jwa.KeyAlgorithmFrom(key.Alg.String()), key.Key),
)
if err != nil {
return "", "", fmt.Errorf("%w: %w", errAssertionVerifyKeyFailure, err)
}
hashClaim, found := tok.Get(kAssertionHash)
if !found {
return "", "", fmt.Errorf("hash claim not found")
}
hash, ok := hashClaim.(string)
if !ok {
return "", "", fmt.Errorf("hash claim is not a string")
}
sigClaim, found := tok.Get(kAssertionSignature)
if !found {
return "", "", fmt.Errorf("signature claim not found")
}
sig, ok := sigClaim.(string)
if !ok {
return "", "", fmt.Errorf("signature claim is not a string")
}
return hash, sig, nil
}
// GetHash returns the hash of the assertion in hex format.
func (a Assertion) GetHash() ([]byte, error) {
// Clear out the binding
a.Binding = Binding{}
// Marshal the assertion to JSON
assertionJSON, err := json.Marshal(a)
if err != nil {
return nil, fmt.Errorf("json.Marshal failed: %w", err)
}
// Unmarshal the JSON into a map to manipulate it
var jsonObject map[string]interface{}
if err := json.Unmarshal(assertionJSON, &jsonObject); err != nil {
return nil, fmt.Errorf("json.Unmarshal failed: %w", err)
}
// Remove the binding key
delete(jsonObject, "binding")
// Marshal the map back to JSON
assertionJSON, err = json.Marshal(jsonObject)
if err != nil {
return nil, fmt.Errorf("json.Marshal failed: %w", err)
}
// Transform the JSON using JCS
transformedJSON, err := jcs.Transform(assertionJSON)
if err != nil {
return nil, fmt.Errorf("jcs.Transform failed: %w", err)
}
return ocrypto.SHA256AsHex(transformedJSON), nil
}
func (s *Statement) MarshalJSON() ([]byte, error) {
// Define a custom struct for serialization
type Alias Statement
aux := &struct {
Value interface{} `json:"value,omitempty"`
*Alias
}{
Alias: (*Alias)(s),
}
if s.Format == "json-structured" {
raw := json.RawMessage(s.Value)
var tmp interface{}
// Attempt to decode Value to validate it's actual json
if err := json.Unmarshal(raw, &tmp); err == nil {
aux.Value = raw
} else {
aux.Value = s.Value
}
} else {
aux.Value = s.Value
}
return json.Marshal(aux)
}
func (s *Statement) UnmarshalJSON(data []byte) error {
// Define a custom struct for deserialization
type Alias Statement
aux := &struct {
Value json.RawMessage `json:"value,omitempty"`
*Alias
}{
Alias: (*Alias)(s),
}
if err := json.Unmarshal(data, &aux); err != nil {
return err
}
// Attempt to decode Value as an object
var temp map[string]interface{}
if json.Unmarshal(aux.Value, &temp) == nil {
// Re-encode the object as a string and assign to Value
objAsString, err := json.Marshal(temp)
if err != nil {
return err
}
s.Value = string(objAsString)
} else {
// Assign raw string to Value
var str string
if err := json.Unmarshal(aux.Value, &str); err != nil {
return fmt.Errorf("value is neither a valid JSON object nor a string: %s", string(aux.Value))
}
s.Value = str
}
return nil
}
// Statement includes information applying to the scope of the assertion.
// It could contain rights, handling instructions, or general metadata.
type Statement struct {
// Format describes the payload encoding format. (e.g. json)
Format string `json:"format,omitempty" validate:"required"`
// Schema describes the schema of the payload. (e.g. tdf)
Schema string `json:"schema,omitempty" validate:"required"`
// Value is the payload of the assertion.
Value string `json:"value,omitempty" validate:"required"`
}
// Binding enforces cryptographic integrity of the assertion.
// So the can't be modified or copied to another tdf.
type Binding struct {
// Method used to bind the assertion. (e.g. jws)
Method string `json:"method,omitempty"`
// Signature of the assertion.
Signature string `json:"signature,omitempty"`
}
// AssertionType represents the type of the assertion.
type AssertionType string
const (
HandlingAssertion AssertionType = "handling"
BaseAssertion AssertionType = "other"
)
// String returns the string representation of the assertion type.
func (at AssertionType) String() string {
return string(at)
}
// Scope represents the object which the assertion applies to.
type Scope string
const (
TrustedDataObj Scope = "tdo"
Paylaod Scope = "payload"
)
// String returns the string representation of the scope.
func (s Scope) String() string {
return string(s)
}
// AppliesToState indicates whether the assertion applies to encrypted or unencrypted data.
type AppliesToState string
const (
Encrypted AppliesToState = "encrypted"
Unencrypted AppliesToState = "unencrypted"
)
// String returns the string representation of the applies to state.
func (ats AppliesToState) String() string {
return string(ats)
}
// BindingMethod represents the method used to bind the assertion.
type BindingMethod string
const (
JWS BindingMethod = "jws"
)
// String returns the string representation of the binding method.
func (bm BindingMethod) String() string {
return string(bm)
}
// AssertionKeyAlg represents the algorithm of an assertion key.
type AssertionKeyAlg string
const (
AssertionKeyAlgRS256 AssertionKeyAlg = "RS256"
AssertionKeyAlgHS256 AssertionKeyAlg = "HS256"
)
// String returns the string representation of the algorithm.
func (a AssertionKeyAlg) String() string {
return string(a)
}
// AssertionKey represents a key for assertions.
type AssertionKey struct {
// Algorithm of the key.
Alg AssertionKeyAlg
// Key value.
Key interface{}
}
// Algorithm returns the algorithm of the key.
func (k AssertionKey) Algorithm() AssertionKeyAlg {
return k.Alg
}
// IsEmpty returns true if the key and the algorithm are empty.
func (k AssertionKey) IsEmpty() bool {
return k.Key == nil && k.Alg == ""
}
// AssertionVerificationKeys represents the verification keys for assertions.
type AssertionVerificationKeys struct {
// Default key to use if the key for the assertion ID is not found.
DefaultKey AssertionKey
// Map of assertion ID to key.
Keys map[string]AssertionKey
}
// Returns the key for the given assertion ID or the default key if the key is not found.
// If the default key is not set, it returns error.
func (k AssertionVerificationKeys) Get(assertionID string) (AssertionKey, error) {
if key, ok := k.Keys[assertionID]; ok {
return key, nil
}
if k.DefaultKey.IsEmpty() {
return AssertionKey{}, nil
}
return k.DefaultKey, nil
}
// IsEmpty returns true if the default key and the keys map are empty.
func (k AssertionVerificationKeys) IsEmpty() bool {
return k.DefaultKey.IsEmpty() && len(k.Keys) == 0
}