Debian Codegraph @

event/encryption.go

raw
1 // Copyright (c) 2020 Tulir Asokan
2 //
3 // This Source Code Form is subject to the terms of the Mozilla Public
4 // License, v. 2.0. If a copy of the MPL was not distributed with this
5 // file, You can obtain one at http://mozilla.org/MPL/2.0/.
6
7 package event
8
9 import (
10 "encoding/json"
11
12 "maunium.net/go/mautrix/id"
13 )
14
15 // EncryptionEventContent represents the content of a m.room.encryption state event.
16 // https://spec.matrix.org/v1.2/client-server-api/#mroomencryption
17 type EncryptionEventContent struct {
18 // The encryption algorithm to be used to encrypt messages sent in this room. Must be 'm.megolm.v1.aes-sha2'.
19 Algorithm id.Algorithm `json:"algorithm"`
20 // How long the session should be used before changing it. 604800000 (a week) is the recommended default.
21 RotationPeriodMillis int64 `json:"rotation_period_ms,omitempty"`
22 // How many messages should be sent before changing the session. 100 is the recommended default.
23 RotationPeriodMessages int `json:"rotation_period_msgs,omitempty"`
24 }
25
26 // EncryptedEventContent represents the content of a m.room.encrypted message event.
27 // https://spec.matrix.org/v1.2/client-server-api/#mroomencrypted
28 //
29 // Note that sender_key and device_id are deprecated in Megolm events as of https://github.com/matrix-org/matrix-spec-proposals/pull/3700
30 type EncryptedEventContent struct {
31 Algorithm id.Algorithm `json:"algorithm"`
32 SenderKey id.SenderKey `json:"sender_key,omitempty"`
33 DeviceID id.DeviceID `json:"device_id,omitempty"` // Only present for Megolm events
34 SessionID id.SessionID `json:"session_id,omitempty"` // Only present for Megolm events
35 Ciphertext json.RawMessage `json:"ciphertext"`
36
37 MegolmCiphertext []byte `json:"-"`
38 OlmCiphertext OlmCiphertexts `json:"-"`
39
40 RelatesTo *RelatesTo `json:"m.relates_to,omitempty"`
41 }
42
43 type OlmCiphertexts map[id.Curve25519]struct {
44 Body string `json:"body"`
45 Type id.OlmMsgType `json:"type"`
46 }
47
48 type serializableEncryptedEventContent EncryptedEventContent
49
50 func (content *EncryptedEventContent) UnmarshalJSON(data []byte) error {
51 err := json.Unmarshal(data, (*serializableEncryptedEventContent)(content))
52 if err != nil {
53 return err
54 }
55 switch content.Algorithm {
56 case id.AlgorithmOlmV1:
57 content.OlmCiphertext = make(OlmCiphertexts)
58 return json.Unmarshal(content.Ciphertext, &content.OlmCiphertext)
59 case id.AlgorithmMegolmV1:
60 if len(content.Ciphertext) == 0 || content.Ciphertext[0] != '"' || content.Ciphertext[len(content.Ciphertext)-1] != '"' {
61 return id.InputNotJSONString
62 }
63 content.MegolmCiphertext = content.Ciphertext[1 : len(content.Ciphertext)-1]
64 }
65 return nil
66 }
67
68 func (content *EncryptedEventContent) MarshalJSON() ([]byte, error) {
69 var err error
70 switch content.Algorithm {
71 case id.AlgorithmOlmV1:
72 content.Ciphertext, err = json.Marshal(content.OlmCiphertext)
73 case id.AlgorithmMegolmV1:
74 content.Ciphertext = make([]byte, len(content.MegolmCiphertext)+2)
75 content.Ciphertext[0] = '"'
76 content.Ciphertext[len(content.Ciphertext)-1] = '"'
77 copy(content.Ciphertext[1:len(content.Ciphertext)-1], content.MegolmCiphertext)
78 }
79 if err != nil {
80 return nil, err
81 }
82 return json.Marshal((*serializableEncryptedEventContent)(content))
83 }
84
85 // RoomKeyEventContent represents the content of a m.room_key to_device event.
86 // https://spec.matrix.org/v1.2/client-server-api/#mroom_key
87 type RoomKeyEventContent struct {
88 Algorithm id.Algorithm `json:"algorithm"`
89 RoomID id.RoomID `json:"room_id"`
90 SessionID id.SessionID `json:"session_id"`
91 SessionKey string `json:"session_key"`
92 }
93
94 // ForwardedRoomKeyEventContent represents the content of a m.forwarded_room_key to_device event.
95 // https://spec.matrix.org/v1.2/client-server-api/#mforwarded_room_key
96 type ForwardedRoomKeyEventContent struct {
97 RoomKeyEventContent
98 SenderKey id.SenderKey `json:"sender_key"`
99 SenderClaimedKey id.Ed25519 `json:"sender_claimed_ed25519_key"`
100 ForwardingKeyChain []string `json:"forwarding_curve25519_key_chain"`
101 }
102
103 type KeyRequestAction string
104
105 const (
106 KeyRequestActionRequest = "request"
107 KeyRequestActionCancel = "request_cancellation"
108 )
109
110 // RoomKeyRequestEventContent represents the content of a m.room_key_request to_device event.
111 // https://spec.matrix.org/v1.2/client-server-api/#mroom_key_request
112 type RoomKeyRequestEventContent struct {
113 Body RequestedKeyInfo `json:"body"`
114 Action KeyRequestAction `json:"action"`
115 RequestingDeviceID id.DeviceID `json:"requesting_device_id"`
116 RequestID string `json:"request_id"`
117 }
118
119 type RequestedKeyInfo struct {
120 Algorithm id.Algorithm `json:"algorithm"`
121 RoomID id.RoomID `json:"room_id"`
122 SenderKey id.SenderKey `json:"sender_key"`
123 SessionID id.SessionID `json:"session_id"`
124 }
125
126 type RoomKeyWithheldCode string
127
128 const (
129 RoomKeyWithheldBlacklisted RoomKeyWithheldCode = "m.blacklisted"
130 RoomKeyWithheldUnverified RoomKeyWithheldCode = "m.unverified"
131 RoomKeyWithheldUnauthorized RoomKeyWithheldCode = "m.unauthorized"
132 RoomKeyWithheldUnavailable RoomKeyWithheldCode = "m.unavailable"
133 RoomKeyWithheldNoOlmSession RoomKeyWithheldCode = "m.no_olm"
134 )
135
136 type RoomKeyWithheldEventContent struct {
137 RoomID id.RoomID `json:"room_id,omitempty"`
138 Algorithm id.Algorithm `json:"algorithm"`
139 SessionID id.SessionID `json:"session_id,omitempty"`
140 SenderKey id.SenderKey `json:"sender_key"`
141 Code RoomKeyWithheldCode `json:"code"`
142 Reason string `json:"reason,omitempty"`
143 }
144
145 type DummyEventContent struct{}