@@ -21,7 +21,6 @@ import (
2121 "encoding/base64"
2222 "encoding/hex"
2323 "encoding/json"
24- "errors"
2524 "fmt"
2625 "io"
2726 "net/http"
@@ -30,6 +29,7 @@ import (
3029 "unicode"
3130
3231 "github.com/google/uuid"
32+ "github.com/rs/zerolog"
3333 "google.golang.org/protobuf/proto"
3434
3535 "go.mau.fi/mautrix-signal/pkg/libsignalgo"
@@ -80,30 +80,30 @@ type GroupAuth struct {
8080}
8181
8282func (cli * Client ) fetchNewGroupCreds (ctx context.Context , today time.Time ) (* GroupCredentials , error ) {
83+ log := zerolog .Ctx (ctx ).With ().
84+ Str ("action" , "fetch new group creds" ).
85+ Logger ()
8386 sevenDaysOut := today .Add (7 * 24 * time .Hour )
8487 path := fmt .Sprintf ("/v1/certificate/auth/group?redemptionStartSeconds=%d&redemptionEndSeconds=%d" , today .Unix (), sevenDaysOut .Unix ())
8588 authRequest := web .CreateWSRequest (http .MethodGet , path , nil , nil , nil )
8689 resp , err := cli .AuthedWS .SendRequest (ctx , authRequest )
8790 if err != nil {
88- zlog .Err (err ).Msg ("SendRequest error" )
89- return nil , err
91+ return nil , fmt .Errorf ("SendRequest error: %w" , err )
9092 }
9193 if * resp .Status != 200 {
92- err := fmt .Errorf ("bad status code: %d" , * resp .Status )
93- zlog .Err (err ).Msg ("bad status code fetching group creds" )
94- return nil , err
94+ return nil , fmt .Errorf ("bad status code fetching group creds: %d" , * resp .Status )
9595 }
9696
9797 var creds GroupCredentials
9898 err = json .Unmarshal (resp .Body , & creds )
9999 if err != nil {
100- zlog .Err (err ).Msg ("json.Unmarshal error" )
100+ log .Err (err ).Msg ("json.Unmarshal error" )
101101 return nil , err
102102 }
103103 // make sure pni matches device pni
104104 if creds .PNI != cli .Store .PNI {
105105 err := fmt .Errorf ("creds.PNI != d.PNI" )
106- zlog .Err (err ).Msg ("creds.PNI != d.PNI" )
106+ log .Err (err ).Msg ("creds.PNI != d.PNI" )
107107 return nil , err
108108 }
109109 return & creds , nil
@@ -121,36 +121,35 @@ func (cli *Client) getCachedAuthorizationForToday(today time.Time) *GroupCredent
121121 return & cred
122122 }
123123 }
124- zlog .Info ().Msg ("No cached credential found for today" )
125124 return nil
126125}
127126
128127func (cli * Client ) GetAuthorizationForToday (ctx context.Context , masterKey libsignalgo.GroupMasterKey ) (* GroupAuth , error ) {
128+ log := zerolog .Ctx (ctx ).With ().
129+ Str ("action" , "get authorization for today" ).
130+ Logger ()
129131 // Timestamps for the start of today, and 7 days later
130132 today := time .Now ().Truncate (24 * time .Hour )
131133
132134 todayCred := cli .getCachedAuthorizationForToday (today )
133135 if todayCred == nil {
134136 creds , err := cli .fetchNewGroupCreds (ctx , today )
135137 if err != nil {
136- zlog .Err (err ).Msg ("fetchNewGroupCreds error" )
137- return nil , err
138+ return nil , fmt .Errorf ("fetchNewGroupCreds error: %w" , err )
138139 }
139140 cli .GroupCredentials = creds
140141 todayCred = cli .getCachedAuthorizationForToday (today )
141142 }
142143 if todayCred == nil {
143- err := errors .New ("Couldn't get credential for today" )
144- zlog .Err (err ).Msg ("GetAuthorizationForToday error" )
145- return nil , err
144+ return nil , fmt .Errorf ("couldn't get credential for today" )
146145 }
147146
148147 //TODO: cache cred after unmarshalling
149148 redemptionTime := uint64 (todayCred .RedemptionTime )
150149 credential := todayCred .Credential
151150 authCredentialResponse , err := libsignalgo .NewAuthCredentialWithPniResponse (credential )
152151 if err != nil {
153- zlog .Err (err ).Msg ("NewAuthCredentialWithPniResponse error" )
152+ log .Err (err ).Msg ("NewAuthCredentialWithPniResponse error" )
154153 return nil , err
155154 }
156155
@@ -163,14 +162,14 @@ func (cli *Client) GetAuthorizationForToday(ctx context.Context, masterKey libsi
163162 * authCredentialResponse ,
164163 )
165164 if err != nil {
166- zlog .Err (err ).Msg ("ReceiveAuthCredentialWithPni error" )
165+ log .Err (err ).Msg ("ReceiveAuthCredentialWithPni error" )
167166 return nil , err
168167 }
169168
170169 // get auth presentation
171170 groupSecretParams , err := libsignalgo .DeriveGroupSecretParamsFromMasterKey (masterKey )
172171 if err != nil {
173- zlog .Err (err ).Msg ("DeriveGroupSecretParamsFromMasterKey error" )
172+ log .Err (err ).Msg ("DeriveGroupSecretParamsFromMasterKey error" )
174173 return nil , err
175174 }
176175 authCredentialPresentation , err := libsignalgo .CreateAuthCredentialWithPniPresentation (
@@ -180,12 +179,12 @@ func (cli *Client) GetAuthorizationForToday(ctx context.Context, masterKey libsi
180179 * authCredential ,
181180 )
182181 if err != nil {
183- zlog .Err (err ).Msg ("CreateAuthCredentialWithPniPresentation error" )
182+ log .Err (err ).Msg ("CreateAuthCredentialWithPniPresentation error" )
184183 return nil , err
185184 }
186185 groupPublicParams , err := groupSecretParams .GetPublicParams ()
187186 if err != nil {
188- zlog .Err (err ).Msg ("GetPublicParams error" )
187+ log .Err (err ).Msg ("GetPublicParams error" )
189188 return nil , err
190189 }
191190
@@ -199,8 +198,7 @@ func masterKeyToBytes(groupMasterKey types.SerializedGroupMasterKey) libsignalgo
199198 // We are very tricksy, groupMasterKey is just base64 encoded group master key :O
200199 masterKeyBytes , err := base64 .StdEncoding .DecodeString (string (groupMasterKey ))
201200 if err != nil {
202- //zlog.Err(err).Msg("")
203- zlog .Fatal ().Err (err ).Msg ("We should always be able to decode groupMasterKey into masterKeyBytes" )
201+ panic (fmt .Errorf ("we should always be able to decode groupMasterKey into masterKeyBytes: %w" , err ))
204202 }
205203 return libsignalgo .GroupMasterKey (masterKeyBytes )
206204}
@@ -212,39 +210,37 @@ func masterKeyFromBytes(masterKey libsignalgo.GroupMasterKey) types.SerializedGr
212210func groupIdentifierFromMasterKey (masterKey types.SerializedGroupMasterKey ) (types.GroupIdentifier , error ) {
213211 groupSecretParams , err := libsignalgo .DeriveGroupSecretParamsFromMasterKey (masterKeyToBytes (masterKey ))
214212 if err != nil {
215- zlog .Err (err ).Msg ("DeriveGroupSecretParamsFromMasterKey error" )
216- return "" , err
213+ return "" , fmt .Errorf ("DeriveGroupSecretParamsFromMasterKey error: %w" , err )
217214 }
218215 // Get the "group identifier" that isn't just the master key
219216 groupPublicParams , err := groupSecretParams .GetPublicParams ()
220217 if err != nil {
221- zlog .Err (err ).Msg ("GetPublicParams error" )
222- return "" , err
218+ return "" , fmt .Errorf ("GetPublicParams error: %w" , err )
223219 }
224220 groupIdentifier , err := libsignalgo .GetGroupIdentifier (* groupPublicParams )
225221 if err != nil {
226- zlog .Err (err ).Msg ("GetGroupIdentifier error" )
227- return "" , err
222+ return "" , fmt .Errorf ("GetGroupIdentifier error: %w" , err )
228223 }
229224 base64GroupIdentifier := base64 .StdEncoding .EncodeToString (groupIdentifier [:])
230225 gid := types .GroupIdentifier (base64GroupIdentifier )
231226 return gid , nil
232227}
233228
234- func decryptGroup (encryptedGroup * signalpb.Group , groupMasterKey types.SerializedGroupMasterKey ) (* Group , error ) {
229+ func decryptGroup (ctx context.Context , encryptedGroup * signalpb.Group , groupMasterKey types.SerializedGroupMasterKey ) (* Group , error ) {
230+ log := zerolog .Ctx (ctx ).With ().Str ("action" , "decrypt group" ).Logger ()
235231 decryptedGroup := & Group {
236232 groupMasterKey : groupMasterKey ,
237233 }
238234
239235 groupSecretParams , err := libsignalgo .DeriveGroupSecretParamsFromMasterKey (masterKeyToBytes (groupMasterKey ))
240236 if err != nil {
241- zlog .Err (err ).Msg ("DeriveGroupSecretParamsFromMasterKey error" )
237+ log .Err (err ).Msg ("DeriveGroupSecretParamsFromMasterKey error" )
242238 return nil , err
243239 }
244240
245241 gid , err := groupIdentifierFromMasterKey (groupMasterKey )
246242 if err != nil {
247- zlog .Err (err ).Msg ("groupIdentifierFromMasterKey error" )
243+ log .Err (err ).Msg ("groupIdentifierFromMasterKey error" )
248244 return nil , err
249245 }
250246 decryptedGroup .GroupIdentifier = gid
@@ -283,13 +279,13 @@ func decryptGroup(encryptedGroup *signalpb.Group, groupMasterKey types.Serialize
283279 encryptedUserID := libsignalgo .UUIDCiphertext (member .UserId )
284280 userID , err := groupSecretParams .DecryptUUID (encryptedUserID )
285281 if err != nil {
286- zlog .Err (err ).Msg ("DecryptUUID UserId error" )
282+ log .Err (err ).Msg ("DecryptUUID UserId error" )
287283 return nil , err
288284 }
289285 encryptedProfileKey := libsignalgo .ProfileKeyCiphertext (member .ProfileKey )
290286 profileKey , err := groupSecretParams .DecryptProfileKey (encryptedProfileKey , userID )
291287 if err != nil {
292- zlog .Err (err ).Msg ("DecryptProfileKey ProfileKey error" )
288+ log .Err (err ).Msg ("DecryptProfileKey ProfileKey error" )
293289 return nil , err
294290 }
295291 decryptedGroup .Members = append (decryptedGroup .Members , & GroupMember {
@@ -306,16 +302,14 @@ func decryptGroup(encryptedGroup *signalpb.Group, groupMasterKey types.Serialize
306302func decryptGroupPropertyIntoBlob (groupSecretParams libsignalgo.GroupSecretParams , encryptedProperty []byte ) (* signalpb.GroupAttributeBlob , error ) {
307303 decryptedProperty , err := groupSecretParams .DecryptBlobWithPadding (encryptedProperty )
308304 if err != nil {
309- zlog .Err (err ).Msg ("DecryptBlobWithPadding error" )
310- return nil , err
305+ return nil , fmt .Errorf ("error decrypting blob with padding: %w" , err )
311306 }
312- propertyBlob := & signalpb.GroupAttributeBlob {}
313- err = proto .Unmarshal (decryptedProperty , propertyBlob )
307+ var propertyBlob signalpb.GroupAttributeBlob
308+ err = proto .Unmarshal (decryptedProperty , & propertyBlob )
314309 if err != nil {
315- zlog .Err (err ).Msg ("Unmarshal error" )
316- return nil , err
310+ return nil , fmt .Errorf ("error unmarshalling blob: %w" , err )
317311 }
318- return propertyBlob , nil
312+ return & propertyBlob , nil
319313}
320314
321315func cleanupStringProperty (property string ) string {
@@ -335,8 +329,7 @@ func cleanupStringMapping(r rune) rune {
335329func decryptGroupAvatar (encryptedAvatar []byte , groupMasterKey types.SerializedGroupMasterKey ) ([]byte , error ) {
336330 groupSecretParams , err := libsignalgo .DeriveGroupSecretParamsFromMasterKey (masterKeyToBytes (groupMasterKey ))
337331 if err != nil {
338- zlog .Err (err ).Msg ("DeriveGroupSecretParamsFromMasterKey error" )
339- return nil , err
332+ return nil , fmt .Errorf ("error deriving group secret params from master key: %w" , err )
340333 }
341334 avatarBlob , err := decryptGroupPropertyIntoBlob (groupSecretParams , encryptedAvatar )
342335 if err != nil {
@@ -360,13 +353,10 @@ func groupMetadataForDataMessage(group Group) *signalpb.GroupContextV2 {
360353func (cli * Client ) fetchGroupByID (ctx context.Context , gid types.GroupIdentifier ) (* Group , error ) {
361354 groupMasterKey , err := cli .Store .GroupStore .MasterKeyFromGroupIdentifier (ctx , gid )
362355 if err != nil {
363- zlog .Err (err ).Msg ("Failed to get group master key" )
364- return nil , err
356+ return nil , fmt .Errorf ("failed to get group master key: %w" , err )
365357 }
366358 if groupMasterKey == "" {
367- err := fmt .Errorf ("No group master key found for group identifier" )
368- zlog .Err (err ).Str ("gid" , string (gid )).Msg ("" )
369- return nil , err
359+ return nil , fmt .Errorf ("No group master key found for group identifier %s" , gid )
370360 }
371361 masterKeyBytes := masterKeyToBytes (groupMasterKey )
372362 groupAuth , err := cli .GetAuthorizationForToday (ctx , masterKeyBytes )
@@ -381,38 +371,31 @@ func (cli *Client) fetchGroupByID(ctx context.Context, gid types.GroupIdentifier
381371 }
382372 response , err := web .SendHTTPRequest (ctx , http .MethodGet , "/v1/groups" , opts )
383373 if err != nil {
384- zlog .Err (err ).Msg ("RetrieveGroupById SendHTTPRequest error" )
385374 return nil , err
386375 }
387376 if response .StatusCode != 200 {
388- err := fmt .Errorf ("RetrieveGroupById SendHTTPRequest bad status: %v" , response .StatusCode )
389- zlog .Err (err ).Msg ("" )
390- return nil , err
377+ return nil , fmt .Errorf ("fetchGroupByID SendHTTPRequest bad status: %d" , response .StatusCode )
391378 }
392- encryptedGroup := & signalpb.Group {}
379+ var encryptedGroup signalpb.Group
393380 groupBytes , err := io .ReadAll (response .Body )
394381 if err != nil {
395- zlog .Err (err ).Msg ("RetrieveGroupById ReadAll error" )
396382 return nil , err
397383 }
398- err = proto .Unmarshal (groupBytes , encryptedGroup )
384+ err = proto .Unmarshal (groupBytes , & encryptedGroup )
399385 if err != nil {
400- zlog .Err (err ).Msg ("RetrieveGroupById Unmarshal error" )
401- return nil , err
386+ return nil , fmt .Errorf ("failed to unmarshal group: %w" , err )
402387 }
403388
404- group , err := decryptGroup (encryptedGroup , groupMasterKey )
389+ group , err := decryptGroup (ctx , & encryptedGroup , groupMasterKey )
405390 if err != nil {
406- zlog .Err (err ).Msg ("RetrieveGroupById decryptGroup error" )
407- return nil , err
391+ return nil , fmt .Errorf ("failed to decrypt group: %w" , err )
408392 }
409393
410394 // Store the profile keys in case they're new
411395 for _ , member := range group .Members {
412396 err = cli .Store .ProfileKeyStore .StoreProfileKey (ctx , member .UserID , member .ProfileKey )
413397 if err != nil {
414- zlog .Err (err ).Msg ("DecryptGroup StoreProfileKey error" )
415- //return nil, err
398+ return nil , fmt .Errorf ("failed to store profile key: %w" , err )
416399 }
417400 }
418401 return group , nil
@@ -469,13 +452,11 @@ func (cli *Client) RetrieveGroupByID(ctx context.Context, gid types.GroupIdentif
469452func (cli * Client ) StoreMasterKey (ctx context.Context , groupMasterKey types.SerializedGroupMasterKey ) (types.GroupIdentifier , error ) {
470453 groupIdentifier , err := groupIdentifierFromMasterKey (groupMasterKey )
471454 if err != nil {
472- zlog .Err (err ).Msg ("groupIdentifierFromMasterKey error" )
473- return "" , err
455+ return "" , fmt .Errorf ("groupIdentifierFromMasterKey error: %w" , err )
474456 }
475457 err = cli .Store .GroupStore .StoreMasterKey (ctx , groupIdentifier , groupMasterKey )
476458 if err != nil {
477- zlog .Err (err ).Msg ("StoreMasterKey error" )
478- return "" , err
459+ return "" , fmt .Errorf ("StoreMasterKey error: %w" , err )
479460 }
480461 return groupIdentifier , nil
481462}
0 commit comments