Debian Codegraph @

crypto/olm/outboundgroupsession.go

raw
1 package olm
2
3 // #cgo LDFLAGS: -lolm -lstdc++
4 // #include <olm/olm.h>
5 import "C"
6
7 import (
8 "crypto/rand"
9 "unsafe"
10
11 "maunium.net/go/mautrix/id"
12 )
13
14 // OutboundGroupSession stores an outbound encrypted messaging session for a
15 // group.
16 type OutboundGroupSession struct {
17 int *C.OlmOutboundGroupSession
18 mem []byte
19 }
20
21 // OutboundGroupSessionFromPickled loads an OutboundGroupSession from a pickled
22 // base64 string. Decrypts the OutboundGroupSession using the supplied key.
23 // Returns error on failure. If the key doesn't match the one used to encrypt
24 // the OutboundGroupSession then the error will be "BAD_SESSION_KEY". If the
25 // base64 couldn't be decoded then the error will be "INVALID_BASE64".
26 func OutboundGroupSessionFromPickled(pickled, key []byte) (*OutboundGroupSession, error) {
27 if len(pickled) == 0 {
28 return nil, EmptyInput
29 }
30 s := NewBlankOutboundGroupSession()
31 return s, s.Unpickle(pickled, key)
32 }
33
34 // NewOutboundGroupSession creates a new outbound group session.
35 func NewOutboundGroupSession() *OutboundGroupSession {
36 s := NewBlankOutboundGroupSession()
37 random := make([]byte, s.createRandomLen()+1)
38 _, err := rand.Read(random)
39 if err != nil {
40 panic(NotEnoughGoRandom)
41 }
42 r := C.olm_init_outbound_group_session(
43 (*C.OlmOutboundGroupSession)(s.int),
44 (*C.uint8_t)(&random[0]),
45 C.size_t(len(random)))
46 if r == errorVal() {
47 panic(s.lastError())
48 }
49 return s
50 }
51
52 // outboundGroupSessionSize is the size of an outbound group session object in
53 // bytes.
54 func outboundGroupSessionSize() uint {
55 return uint(C.olm_outbound_group_session_size())
56 }
57
58 // newOutboundGroupSession initialises an empty OutboundGroupSession.
59 func NewBlankOutboundGroupSession() *OutboundGroupSession {
60 memory := make([]byte, outboundGroupSessionSize())
61 return &OutboundGroupSession{
62 int: C.olm_outbound_group_session(unsafe.Pointer(&memory[0])),
63 mem: memory,
64 }
65 }
66
67 // lastError returns an error describing the most recent error to happen to an
68 // outbound group session.
69 func (s *OutboundGroupSession) lastError() error {
70 return convertError(C.GoString(C.olm_outbound_group_session_last_error((*C.OlmOutboundGroupSession)(s.int))))
71 }
72
73 // Clear clears the memory used to back this OutboundGroupSession.
74 func (s *OutboundGroupSession) Clear() error {
75 r := C.olm_clear_outbound_group_session((*C.OlmOutboundGroupSession)(s.int))
76 if r == errorVal() {
77 return s.lastError()
78 } else {
79 return nil
80 }
81 }
82
83 // pickleLen returns the number of bytes needed to store an outbound group
84 // session.
85 func (s *OutboundGroupSession) pickleLen() uint {
86 return uint(C.olm_pickle_outbound_group_session_length((*C.OlmOutboundGroupSession)(s.int)))
87 }
88
89 // Pickle returns an OutboundGroupSession as a base64 string. Encrypts the
90 // OutboundGroupSession using the supplied key.
91 func (s *OutboundGroupSession) Pickle(key []byte) []byte {
92 if len(key) == 0 {
93 panic(NoKeyProvided)
94 }
95 pickled := make([]byte, s.pickleLen())
96 r := C.olm_pickle_outbound_group_session(
97 (*C.OlmOutboundGroupSession)(s.int),
98 unsafe.Pointer(&key[0]),
99 C.size_t(len(key)),
100 unsafe.Pointer(&pickled[0]),
101 C.size_t(len(pickled)))
102 if r == errorVal() {
103 panic(s.lastError())
104 }
105 return pickled[:r]
106 }
107
108 func (s *OutboundGroupSession) Unpickle(pickled, key []byte) error {
109 if len(key) == 0 {
110 return NoKeyProvided
111 }
112 r := C.olm_unpickle_outbound_group_session(
113 (*C.OlmOutboundGroupSession)(s.int),
114 unsafe.Pointer(&key[0]),
115 C.size_t(len(key)),
116 unsafe.Pointer(&pickled[0]),
117 C.size_t(len(pickled)))
118 if r == errorVal() {
119 return s.lastError()
120 }
121 return nil
122 }
123
124 func (s *OutboundGroupSession) GobEncode() ([]byte, error) {
125 pickled := s.Pickle(pickleKey)
126 length := unpaddedBase64.DecodedLen(len(pickled))
127 rawPickled := make([]byte, length)
128 _, err := unpaddedBase64.Decode(rawPickled, pickled)
129 return rawPickled, err
130 }
131
132 func (s *OutboundGroupSession) GobDecode(rawPickled []byte) error {
133 if s == nil || s.int == nil {
134 *s = *NewBlankOutboundGroupSession()
135 }
136 length := unpaddedBase64.EncodedLen(len(rawPickled))
137 pickled := make([]byte, length)
138 unpaddedBase64.Encode(pickled, rawPickled)
139 return s.Unpickle(pickled, pickleKey)
140 }
141
142 func (s *OutboundGroupSession) MarshalJSON() ([]byte, error) {
143 pickled := s.Pickle(pickleKey)
144 quotes := make([]byte, len(pickled)+2)
145 quotes[0] = '"'
146 quotes[len(quotes)-1] = '"'
147 copy(quotes[1:len(quotes)-1], pickled)
148 return quotes, nil
149 }
150
151 func (s *OutboundGroupSession) UnmarshalJSON(data []byte) error {
152 if len(data) == 0 || data[0] != '"' || data[len(data)-1] != '"' {
153 return InputNotJSONString
154 }
155 if s == nil || s.int == nil {
156 *s = *NewBlankOutboundGroupSession()
157 }
158 return s.Unpickle(data[1:len(data)-1], pickleKey)
159 }
160
161 // createRandomLen returns the number of random bytes needed to create an
162 // Account.
163 func (s *OutboundGroupSession) createRandomLen() uint {
164 return uint(C.olm_init_outbound_group_session_random_length((*C.OlmOutboundGroupSession)(s.int)))
165 }
166
167 // encryptMsgLen returns the size of the next message in bytes for the given
168 // number of plain-text bytes.
169 func (s *OutboundGroupSession) encryptMsgLen(plainTextLen int) uint {
170 return uint(C.olm_group_encrypt_message_length((*C.OlmOutboundGroupSession)(s.int), C.size_t(plainTextLen)))
171 }
172
173 // Encrypt encrypts a message using the Session. Returns the encrypted message
174 // as base64.
175 func (s *OutboundGroupSession) Encrypt(plaintext []byte) []byte {
176 if len(plaintext) == 0 {
177 panic(EmptyInput)
178 }
179 message := make([]byte, s.encryptMsgLen(len(plaintext)))
180 r := C.olm_group_encrypt(
181 (*C.OlmOutboundGroupSession)(s.int),
182 (*C.uint8_t)(&plaintext[0]),
183 C.size_t(len(plaintext)),
184 (*C.uint8_t)(&message[0]),
185 C.size_t(len(message)))
186 if r == errorVal() {
187 panic(s.lastError())
188 }
189 return message[:r]
190 }
191
192 // sessionIdLen returns the number of bytes needed to store a session ID.
193 func (s *OutboundGroupSession) sessionIdLen() uint {
194 return uint(C.olm_outbound_group_session_id_length((*C.OlmOutboundGroupSession)(s.int)))
195 }
196
197 // ID returns a base64-encoded identifier for this session.
198 func (s *OutboundGroupSession) ID() id.SessionID {
199 sessionID := make([]byte, s.sessionIdLen())
200 r := C.olm_outbound_group_session_id(
201 (*C.OlmOutboundGroupSession)(s.int),
202 (*C.uint8_t)(&sessionID[0]),
203 C.size_t(len(sessionID)))
204 if r == errorVal() {
205 panic(s.lastError())
206 }
207 return id.SessionID(sessionID[:r])
208 }
209
210 // MessageIndex returns the message index for this session. Each message is
211 // sent with an increasing index; this returns the index for the next message.
212 func (s *OutboundGroupSession) MessageIndex() uint {
213 return uint(C.olm_outbound_group_session_message_index((*C.OlmOutboundGroupSession)(s.int)))
214 }
215
216 // sessionKeyLen returns the number of bytes needed to store a session key.
217 func (s *OutboundGroupSession) sessionKeyLen() uint {
218 return uint(C.olm_outbound_group_session_key_length((*C.OlmOutboundGroupSession)(s.int)))
219 }
220
221 // Key returns the base64-encoded current ratchet key for this session.
222 func (s *OutboundGroupSession) Key() string {
223 sessionKey := make([]byte, s.sessionKeyLen())
224 r := C.olm_outbound_group_session_key(
225 (*C.OlmOutboundGroupSession)(s.int),
226 (*C.uint8_t)(&sessionKey[0]),
227 C.size_t(len(sessionKey)))
228 if r == errorVal() {
229 panic(s.lastError())
230 }
231 return string(sessionKey[:r])
232 }