-
Notifications
You must be signed in to change notification settings - Fork 51
/
Copy patheventcrypto.go
391 lines (331 loc) · 10.9 KB
/
eventcrypto.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
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
/* Copyright 2016-2017 Vector Creations Ltd
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package gomatrixserverlib
import (
"bytes"
"context"
"crypto/sha256"
"encoding/base64"
"encoding/json"
"fmt"
"github.com/matrix-org/gomatrixserverlib/spec"
"github.com/tidwall/gjson"
"github.com/tidwall/sjson"
"golang.org/x/crypto/ed25519"
)
func VerifyAllEventSignatures(ctx context.Context, events []PDU, verifier JSONVerifier, userIDForSender spec.UserIDForSender) []error {
errors := make([]error, 0, len(events))
for _, e := range events {
errors = append(errors, VerifyEventSignatures(ctx, e, verifier, userIDForSender))
}
return errors
}
func VerifyEventSignatures(ctx context.Context, e PDU, verifier JSONVerifier, userIDForSender spec.UserIDForSender) error {
if userIDForSender == nil {
panic("UserIDForSender func is nil")
}
var serverName spec.ServerName
needed := map[spec.ServerName]struct{}{}
verImpl, err := GetRoomVersion(e.Version())
if err != nil {
return err
}
// The sender should have signed the event in all cases.
switch e.Version() {
case RoomVersionPseudoIDs:
needed[spec.ServerName(e.SenderID())] = struct{}{}
default:
sender, err := userIDForSender(e.RoomID(), e.SenderID())
if err != nil {
return fmt.Errorf("invalid sender userID: %w", err)
}
if sender != nil {
serverName = sender.Domain()
needed[serverName] = struct{}{}
}
// In room versions 1 and 2, we should also check that the server
// that created the event is included too. This is probably the
// same as the sender.
format := verImpl.EventIDFormat()
if format == EventIDFormatV1 {
_, serverName, err = SplitID('$', e.EventID())
if err != nil {
return fmt.Errorf("failed to split event ID: %w", err)
}
needed[serverName] = struct{}{}
}
}
// Special checks for membership events.
if e.Type() == spec.MRoomMember {
membership, err := e.Membership()
if err != nil {
return fmt.Errorf("failed to get membership of membership event: %w", err)
}
// Validate the MXIDMapping is signed correctly
if verImpl.Version() == RoomVersionPseudoIDs && membership == spec.Join {
mapping, err := getMXIDMapping(e)
if err != nil {
return err
}
err = validateMXIDMappingSignatures(ctx, e, *mapping, verifier, verImpl)
if err != nil {
return err
}
}
// For invites, the invited server should have signed the event.
if membership == spec.Invite {
switch e.Version() {
case RoomVersionPseudoIDs:
needed[spec.ServerName(*e.StateKey())] = struct{}{}
default:
_, serverName, err = SplitID('@', *e.StateKey())
if err != nil {
return fmt.Errorf("failed to split state key: %w", err)
}
needed[serverName] = struct{}{}
}
}
// For restricted join rules, the authorising server should have signed.
if membership == spec.Join {
auth, err := verImpl.RestrictedJoinServername(e.Content())
if err != nil {
return err
}
if auth != "" {
needed[auth] = struct{}{}
}
}
}
redactedJSON, err := verImpl.RedactEventJSON(e.JSON())
if err != nil {
return fmt.Errorf("failed to redact event: %w", err)
}
var toVerify []VerifyJSONRequest
for serverName := range needed {
v := VerifyJSONRequest{
Message: redactedJSON,
AtTS: e.OriginServerTS(),
ServerName: serverName,
ValidityCheckingFunc: verImpl.SignatureValidityCheck,
}
toVerify = append(toVerify, v)
}
if verImpl.Version() == RoomVersionPseudoIDs {
// we already verified the mxid_mapping at this stage, so replace the KeyRing verifier
// with the self verifier to validate pseudoID events
verifier = JSONVerifierSelf{}
}
results, err := verifier.VerifyJSONs(ctx, toVerify)
if err != nil {
return fmt.Errorf("failed to verify JSONs: %w", err)
}
for _, result := range results {
if result.Error != nil {
return result.Error
}
}
return nil
}
func getMXIDMapping(e PDU) (*MXIDMapping, error) {
var content MemberContent
err := json.Unmarshal(e.Content(), &content)
if err != nil {
return nil, err
}
// if there is no mapping, we can't check the signature
if content.MXIDMapping == nil {
return nil, fmt.Errorf("missing mxid_mapping")
}
return content.MXIDMapping, nil
}
// validateMXIDMappingSignatures validates that the MXIDMapping is correctly signed
func validateMXIDMappingSignatures(ctx context.Context, e PDU, mapping MXIDMapping, verifier JSONVerifier, verImpl IRoomVersion) error {
mappingBytes, err := json.Marshal(mapping)
if err != nil {
return err
}
var toVerify []VerifyJSONRequest
for s := range mapping.Signatures {
v := VerifyJSONRequest{
Message: mappingBytes,
AtTS: e.OriginServerTS(),
ServerName: s,
ValidityCheckingFunc: verImpl.SignatureValidityCheck,
}
toVerify = append(toVerify, v)
}
// check that the mapping is correctly signed by the server
results, err := verifier.VerifyJSONs(ctx, toVerify)
if err != nil {
return fmt.Errorf("failed to verify MXIDMapping: %w", err)
}
for _, result := range results {
if result.Error != nil {
return fmt.Errorf("failed to verify MXIDMapping: %w", result.Error)
}
}
return err
}
func extractAuthorisedViaServerName(content []byte) (spec.ServerName, error) {
if v := gjson.GetBytes(content, "join_authorised_via_users_server"); v.Exists() {
_, serverName, err := SplitID('@', v.String())
if err != nil {
return "", fmt.Errorf("failed to split authorised server: %w", err)
}
return serverName, nil
}
return "", nil
}
func emptyAuthorisedViaServerName([]byte) (spec.ServerName, error) { return "", nil }
// addContentHashesToEvent sets the "hashes" key of the event with a SHA-256 hash of the unredacted event content.
// This hash is used to detect whether the unredacted content of the event is valid.
// Returns the event JSON with a "hashes" key added to it.
func addContentHashesToEvent(eventJSON []byte) ([]byte, error) {
var event map[string]spec.RawJSON
if err := json.Unmarshal(eventJSON, &event); err != nil {
return nil, err
}
unsignedJSON := event["unsigned"]
signatures := event["signatures"]
delete(event, "signatures")
delete(event, "unsigned")
delete(event, "hashes")
hashableEventJSON, err := json.Marshal(event)
if err != nil {
return nil, err
}
hashableEventJSON, err = CanonicalJSON(hashableEventJSON)
if err != nil {
return nil, err
}
sha256Hash := sha256.Sum256(hashableEventJSON)
hashes := struct {
Sha256 spec.Base64Bytes `json:"sha256"`
}{spec.Base64Bytes(sha256Hash[:])}
hashesJSON, err := json.Marshal(&hashes)
if err != nil {
return nil, err
}
if len(unsignedJSON) > 0 {
event["unsigned"] = unsignedJSON
}
if len(signatures) > 0 {
event["signatures"] = signatures
}
event["hashes"] = spec.RawJSON(hashesJSON)
return json.Marshal(event)
}
// checkEventContentHash checks if the unredacted content of the event matches the SHA-256 hash under the "hashes" key.
// Assumes that eventJSON has been canonicalised already.
func checkEventContentHash(eventJSON []byte) error {
var err error
result := gjson.GetBytes(eventJSON, "hashes.sha256")
var hash spec.Base64Bytes
if err = hash.Decode(result.Str); err != nil {
return err
}
hashableEventJSON := eventJSON
for _, key := range []string{"signatures", "unsigned", "hashes"} {
if hashableEventJSON, err = sjson.DeleteBytes(hashableEventJSON, key); err != nil {
return err
}
}
sha256Hash := sha256.Sum256(hashableEventJSON)
if !bytes.Equal(sha256Hash[:], []byte(hash)) {
return fmt.Errorf("Invalid Sha256 content hash: %v != %v", sha256Hash[:], []byte(hash))
}
return nil
}
// ReferenceSha256HashOfEvent returns the SHA-256 hash of the redacted event content.
// This is used when referring to this event from other events.
func referenceOfEvent(eventJSON []byte, roomVersion RoomVersion) (eventReference, error) {
verImpl, err := GetRoomVersion(roomVersion)
if err != nil {
return eventReference{}, err
}
redactedJSON, err := verImpl.RedactEventJSON(eventJSON)
if err != nil {
return eventReference{}, err
}
var event map[string]spec.RawJSON
if err = json.Unmarshal(redactedJSON, &event); err != nil {
return eventReference{}, err
}
delete(event, "signatures")
delete(event, "unsigned")
hashableEventJSON, err := json.Marshal(event)
if err != nil {
return eventReference{}, err
}
hashableEventJSON, err = CanonicalJSON(hashableEventJSON)
if err != nil {
return eventReference{}, err
}
sha256Hash := sha256.Sum256(hashableEventJSON)
var eventID string
eventFormat := verImpl.EventFormat()
eventIDFormat := verImpl.EventIDFormat()
switch eventFormat {
case EventFormatV1:
if err = json.Unmarshal(event["event_id"], &eventID); err != nil {
return eventReference{}, err
}
case EventFormatV2:
var encoder *base64.Encoding
switch eventIDFormat {
case EventIDFormatV2:
encoder = base64.RawStdEncoding.WithPadding(base64.NoPadding)
case EventIDFormatV3:
encoder = base64.RawURLEncoding.WithPadding(base64.NoPadding)
default:
return eventReference{}, UnsupportedRoomVersionError{Version: roomVersion}
}
eventID = fmt.Sprintf("$%s", encoder.EncodeToString(sha256Hash[:]))
default:
return eventReference{}, UnsupportedRoomVersionError{Version: roomVersion}
}
return eventReference{eventID, sha256Hash[:]}, nil
}
// SignEvent adds a ED25519 signature to the event for the given key.
func signEvent(signingName string, keyID KeyID, privateKey ed25519.PrivateKey, eventJSON []byte, roomVersion RoomVersion) ([]byte, error) {
verImpl, err := GetRoomVersion(roomVersion)
if err != nil {
return nil, err
}
// Redact the event before signing so signature that will remain valid even if the event is redacted.
redactedJSON, err := verImpl.RedactEventJSON(eventJSON)
if err != nil {
return nil, err
}
// Sign the JSON, this adds a "signatures" key to the redacted event.
// TODO: Make an internal version of SignJSON that returns just the signatures so that we don't have to parse it out of the JSON.
signedJSON, err := SignJSON(signingName, keyID, privateKey, redactedJSON)
if err != nil {
return nil, err
}
var signedEvent struct {
Signatures spec.RawJSON `json:"signatures"`
}
if err := json.Unmarshal(signedJSON, &signedEvent); err != nil {
return nil, err
}
// Unmarshal the event JSON so that we can replace the signatures key.
var event map[string]spec.RawJSON
if err := json.Unmarshal(eventJSON, &event); err != nil {
return nil, err
}
event["signatures"] = signedEvent.Signatures
return json.Marshal(event)
}