This repository has been archived by the owner on Jul 22, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 26
/
textsecure.go
673 lines (601 loc) · 19.3 KB
/
textsecure.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
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
// Copyright (c) 2014 Canonical Ltd.
// Licensed under the GPLv3, see the COPYING file for details.
// Package textsecure implements the TextSecure client protocol.
package textsecure
import (
"encoding/base64"
"errors"
"fmt"
"io"
"os"
"strings"
"bytes"
"github.com/signal-golang/mimemagic"
"github.com/golang/protobuf/proto"
"github.com/signal-golang/textsecure/axolotl"
"github.com/signal-golang/textsecure/config"
"github.com/signal-golang/textsecure/contacts"
crayfish "github.com/signal-golang/textsecure/crayfish"
"github.com/signal-golang/textsecure/helpers"
"github.com/signal-golang/textsecure/profiles"
signalservice "github.com/signal-golang/textsecure/protobuf"
"github.com/signal-golang/textsecure/registration"
rootCa "github.com/signal-golang/textsecure/rootCa"
"github.com/signal-golang/textsecure/transport"
"github.com/signal-golang/textsecure/unidentifiedAccess"
log "github.com/sirupsen/logrus"
)
// Generate a random 16 byte string used for HTTP Basic Authentication to the server
func generatePassword() string {
b := make([]byte, 16)
randBytes(b[:])
return helpers.Base64EncWithoutPadding(b)
}
// Generate a random 14 bit integer
func generateRegistrationID() uint32 {
return randUint32() & 0x3fff
}
// Generate a 256 bit AES and a 160 bit HMAC-SHA1 key
// to be used to secure the communication with the server
func generateSignalingKey() []byte {
b := make([]byte, 52)
randBytes(b[:])
//set signaling key version
b[0] = 1
return b
}
func encodeKey(key []byte) string {
return helpers.Base64EncWithoutPadding(append([]byte{5}, key[:]...))
}
// ErrBadPublicKey is raised when a given public key is not in the
// expected format.
var ErrBadPublicKey = errors.New("public key not formatted correctly")
func decodeKey(s string) ([]byte, error) {
b, err := helpers.Base64DecodeNonPadded(s)
if err != nil {
return nil, err
}
if len(b) != 33 || b[0] != 5 {
return nil, ErrBadPublicKey
}
return b[1:], nil
}
func decodeSignature(s string) ([]byte, error) {
b, err := helpers.Base64DecodeNonPadded(s)
if err != nil {
return nil, err
}
if len(b) != 64 {
return nil, fmt.Errorf("signature is %d, not 64 bytes", len(b))
}
return b, nil
}
func needsRegistration() bool {
return !textSecureStore.valid()
}
var identityKey *axolotl.IdentityKeyPair
type attachmentPointerV3 struct {
cdnKey string
cdnNr uint32
ct string
keys []byte
digest []byte
size uint32
voiceNote bool
}
type outgoingMessage struct {
destination string
msg string
group *groupMessage
groupV2 *signalservice.GroupContextV2
attachment *attachmentPointerV3
flags uint32
expireTimer uint32
timestamp *uint64
}
// LinkedDevices returns the list of linked devices
func LinkedDevices() ([]DeviceInfo, error) {
return getLinkedDevices()
}
// UnlinkDevice removes a linked device
func UnlinkDevice(id int) error {
return unlinkDevice(id)
}
// NewDeviceVerificationCode returns the verification code for linking devices
func NewDeviceVerificationCode() (string, error) {
return getNewDeviceVerificationCode()
}
// AddDevice links a new device
func AddDevice(ephemeralID, publicKey, verificationCode string) error {
return addNewDevice(ephemeralID, publicKey, verificationCode)
}
// SendMessage sends the given text message to the given contact.
func SendMessage(uuid, msg string, timer uint32) (uint64, error) {
omsg := &outgoingMessage{
destination: uuid,
msg: msg,
expireTimer: timer,
}
return sendMessage(omsg)
}
// MIMETypeFromReader returns the mime type that is inside the reader
func MIMETypeFromReader(r io.Reader) (mime string, reader io.Reader) {
var buf bytes.Buffer
io.CopyN(&buf, r, 1024)
mime = mimemagic.Match("", buf.Bytes())
return mime, io.MultiReader(&buf, r)
}
// SendAttachment sends the contents of a reader, along
// with an optional message to a given contact.
func SendAttachment(uuid string, msg string, r io.Reader, timer uint32) (uint64, error) {
ct, r := MIMETypeFromReader(r)
a, err := uploadAttachment(r, ct)
if err != nil {
return 0, err
}
omsg := &outgoingMessage{
destination: uuid,
msg: msg,
attachment: a,
expireTimer: timer,
}
return sendMessage(omsg)
}
// SendVoiceNote sends a voice note
func SendVoiceNote(uuid, msg string, r io.Reader, timer uint32) (uint64, error) {
ct, r := MIMETypeFromReader(r)
a, err := uploadVoiceNote(r, ct)
if err != nil {
return 0, err
}
omsg := &outgoingMessage{
destination: uuid,
msg: msg,
attachment: a,
expireTimer: timer,
}
return sendMessage(omsg)
}
// EndSession terminates the session with the given peer.
func EndSession(uuid string, msg string) (uint64, error) {
omsg := &outgoingMessage{
destination: uuid,
msg: msg,
flags: uint32(signalservice.DataMessage_END_SESSION),
}
ts, err := sendMessage(omsg)
if err != nil {
return 0, err
}
uuidClean, err := recID(uuid)
if err != nil {
return 0, err
}
textSecureStore.DeleteAllSessions(uuidClean)
return ts, nil
}
// Attachment represents an attachment received from a peer
type Attachment struct {
R io.Reader
MimeType string
FileName string
}
// Client contains application specific data and callbacks.
type Client struct {
GetPhoneNumber func() string
GetVerificationCode func() string
GetPin func() string
GetStoragePassword func() string
GetCaptchaToken func() string
GetConfig func() (*config.Config, error)
GetLocalContacts func() ([]contacts.Contact, error)
MessageHandler func(*Message)
TypingMessageHandler func(*Message)
ReceiptMessageHandler func(*Message)
CallMessageHandler func(*Message)
ReceiptHandler func(string, uint32, uint64)
SyncReadHandler func(string, uint64)
SyncSentHandler func(*Message, uint64)
RegistrationDone func()
GetUsername func() string
}
var (
client *Client
)
// setupLogging sets the logging verbosity level based on configuration
// and environment variables
func setupLogging() {
loglevel := config.ConfigFile.LogLevel
if loglevel == "" || os.Getenv("TEXTSECURE_LOGLEVEL") != "" {
loglevel = os.Getenv("TEXTSECURE_LOGLEVEL")
}
fmt.Printf("INFO[0000] [textsecure] Setting log level to %s\n", loglevel)
switch strings.ToUpper(loglevel) {
case "DEBUG":
log.SetLevel(log.DebugLevel)
case "INFO":
log.SetLevel(log.InfoLevel)
case "WARN":
log.SetLevel(log.WarnLevel)
case "ERROR":
log.SetLevel(log.ErrorLevel)
default:
log.SetLevel(log.InfoLevel)
}
log.SetFormatter(&log.TextFormatter{
FullTimestamp: true,
TimestampFormat: "2006/01/02 15:04:05",
})
}
// Setup initializes the package.
func Setup(c *Client) error {
var err error
client = c
config.ConfigFile, err = loadConfig()
go crayfish.Run()
if err != nil {
return err
}
setupLogging()
err = setupStore()
if err != nil {
return err
}
if needsRegistration() {
registration.Registration = registration.RegistrationInfo{
RegistrationID: generateRegistrationID(),
}
textSecureStore.SetLocalRegistrationID(registration.Registration.RegistrationID)
registration.Registration.Password = generatePassword()
textSecureStore.storeHTTPPassword(registration.Registration.Password)
registration.Registration.SignalingKey = generateSignalingKey()
textSecureStore.storeHTTPSignalingKey(registration.Registration.SignalingKey)
identityKey = axolotl.GenerateIdentityKeyPair()
err := textSecureStore.SetIdentityKeyPair(identityKey)
if err != nil {
return err
}
err = registerDevice()
if err != nil {
return err
}
}
registration.Registration.RegistrationID, err = textSecureStore.GetLocalRegistrationID()
if err != nil {
return err
}
registration.Registration.Password, err = textSecureStore.loadHTTPPassword()
if err != nil {
return err
}
registration.Registration.SignalingKey, err = textSecureStore.loadHTTPSignalingKey()
if err != nil {
return err
}
client.RegistrationDone()
rootCa.SetupCA(config.ConfigFile.RootCA)
transport.SetupTransporter(config.ConfigFile.Server, config.ConfigFile.UUID, registration.Registration.Password, config.ConfigFile.UserAgent, config.ConfigFile.ProxyServer)
transport.SetupCDNTransporter(SIGNAL_CDN2_URL, config.ConfigFile.UUID, registration.Registration.Password, config.ConfigFile.UserAgent, config.ConfigFile.ProxyServer)
transport.SetupDirectoryTransporter(DIRECTORY_URL, config.ConfigFile.UUID, registration.Registration.Password, config.ConfigFile.UserAgent, config.ConfigFile.ProxyServer)
transport.SetupStorageTransporter(STORAGE_URL, config.ConfigFile.UUID, registration.Registration.Password, config.ConfigFile.UserAgent, config.ConfigFile.ProxyServer)
transport.SetupServiceTransporter(SIGNAL_SERVICE_URL, config.ConfigFile.UUID, registration.Registration.Password, config.ConfigFile.UserAgent, config.ConfigFile.ProxyServer)
identityKey, err = textSecureStore.GetIdentityKeyPair()
// check if we have a uuid and if not get it
// config.ConfigFile = checkUUID(config.ConfigFile)
profileChanged := false
// check for a profileKey
if len(config.ConfigFile.ProfileKey) == 0 {
config.ConfigFile.ProfileKey = profiles.GenerateProfileKey()
saveConfig(config.ConfigFile)
profileChanged = true
}
// check if a username is set
if config.ConfigFile.Name == "" {
config.ConfigFile.Name = client.GetUsername()
profileChanged = true
saveConfig(config.ConfigFile)
}
if profileChanged {
profiles.UpdateProfile(config.ConfigFile.ProfileKey, config.ConfigFile.UUID, config.ConfigFile.Name)
} else {
config.ConfigFile.ProfileKey = profiles.GenerateProfileKey()
saveConfig(config.ConfigFile)
profiles.UpdateProfile(config.ConfigFile.ProfileKey, config.ConfigFile.UUID, config.ConfigFile.Name)
}
// check for unidentified access
if len(config.ConfigFile.Certificate) == 0 {
err = renewSenderCertificate()
if err != nil {
return err
}
} else {
err := unidentifiedAccess.CheckCertificate(config.ConfigFile.Certificate)
if err != nil {
err = renewSenderCertificate()
if err != nil {
return err
}
}
}
if len(config.ConfigFile.ProfileKeyCredential) == 0 {
log.Infoln("[textsecure] Generating profile key credential")
profiles.UpdateProfile(config.ConfigFile.ProfileKey, config.ConfigFile.UUID, config.ConfigFile.Name)
profile, err := profiles.GetProfileAndCredential(config.ConfigFile.UUID, config.ConfigFile.ProfileKey)
if err != nil {
return err
}
config.ConfigFile.ProfileKeyCredential = []byte(profile.Credential)
saveConfig(config.ConfigFile)
} else {
log.Infoln("[textsecure] Using existing profile key credential", len(config.ConfigFile.ProfileKeyCredential))
}
return err
}
func renewSenderCertificate() error {
log.Infoln("Get new uidentified sender certificate")
cert, err := transport.GetSenderCertificate()
if err != nil {
return err
}
config.ConfigFile.Certificate = cert.Certificate
saveConfig(config.ConfigFile)
log.Debug(fmt.Sprintf("[textsecure] Sender certificate: %s", cert))
return nil
}
func RegisterWithCrayfish(regisrationInfo *registration.RegistrationInfo, phoneNumber, captcha string) error {
err := crayfish.Instance.CrayfishRegister(regisrationInfo, phoneNumber, captcha)
if err != nil {
return err
}
return nil
}
func registerDevice() error {
log.Debugln("[texsecure] register Device")
var err error
config.ConfigFile, err = loadConfig()
if err != nil {
return err
}
rootCa.SetupCA(config.ConfigFile.RootCA)
log.Debugln("[textsecure] Crayfish registration starting")
client.GetConfig()
phoneNumber := client.GetPhoneNumber()
captcha := client.GetCaptchaToken()
name := client.GetUsername()
registration.Registration.Name = name
err = RegisterWithCrayfish(®istration.Registration, phoneNumber, captcha)
if err != nil {
log.Errorln("[textsecure] Crayfish registration failed", err)
return err
}
code := client.GetVerificationCode()
crayfishRegistration, err := crayfish.Instance.CrayfishRegisterWithCode(®istration.Registration, phoneNumber, captcha, code)
if err != nil {
log.Errorln("[textsecure] Crayfish registration failed", err)
return err
}
config.ConfigFile.Tel = crayfishRegistration.Tel
config.ConfigFile.UUID = crayfishRegistration.UUID
config.ConfigFile.Name = name
config.ConfigFile.AccountCapabilities = config.AccountCapabilities{
// Uuid: false,
Gv2: true,
Storage: false,
Gv1Migration: false,
SenderKey: false,
AnnouncementGroup: true,
ChangeNumber: false,
}
err = saveConfig(config.ConfigFile)
if err != nil {
return err
}
log.Debugln("[textsecure] Crayfish registration done")
transport.SetupTransporter(config.ConfigFile.Server, config.ConfigFile.UUID, registration.Registration.Password, config.ConfigFile.UserAgent, config.ConfigFile.ProxyServer)
log.Debugln("[textsecure] generate keys")
err = generatePreKeys()
if err != nil {
return err
}
err = generatePreKeyState()
if err != nil {
return err
}
err = registerPreKeys()
if err != nil {
return err
}
config.ConfigFile.ProfileKey = profiles.GenerateProfileKey()
// config.ConfigFile = checkUUID(config.ConfigFile)
saveConfig(config.ConfigFile)
client.RegistrationDone()
if client.RegistrationDone != nil {
log.Infoln("[textsecure] RegistrationDone")
client.RegistrationDone()
}
return nil
}
func handleReceipt(env *signalservice.Envelope) {
if client.ReceiptHandler != nil {
client.ReceiptHandler(env.GetSourceUuid(), env.GetSourceDevice(), env.GetTimestamp())
}
}
// recID removes the + from phone numbers
func recID(source string) (string, error) {
if len(source) == 0 {
return "", errors.New("invalid recipient id")
} else if len(source) > 0 && source[0] == '+' {
log.Errorln("[textsecure] invalid recipient id", source)
return source[1:], nil
}
return source, nil
}
// EndSessionFlag signals that this message resets the session
var EndSessionFlag uint32 = 1
// ProfileKeyUpdatedFlag signals that this message updates the profile key
var ProfileKeyUpdatedFlag = signalservice.DataMessage_PROFILE_KEY_UPDATE
func handleFlags(src string, dm *signalservice.DataMessage) (uint32, error) {
flags := uint32(0)
if dm.GetFlags() == uint32(signalservice.DataMessage_END_SESSION) {
flags = EndSessionFlag
srcClean, err := recID(src)
if err != nil {
return 0, err
}
textSecureStore.DeleteAllSessions(srcClean)
textSecureStore.DeleteAllSessions(src)
}
if dm.GetFlags() == uint32(signalservice.DataMessage_PROFILE_KEY_UPDATE) {
err := contacts.UpdateProfileKey(src, dm.GetProfileKey())
if err != nil {
return 0, err
}
flags = uint32(signalservice.DataMessage_PROFILE_KEY_UPDATE)
}
return flags, nil
}
// MessageTypeNotImplementedError is raised in the unlikely event that an unhandled protocol message type is received.
type MessageTypeNotImplementedError struct {
typ uint32
}
func (err MessageTypeNotImplementedError) Error() string {
return fmt.Sprintf("not implemented message type %d", err.typ)
}
// ErrInvalidMACForMessage signals an incoming message with invalid MAC.
var ErrInvalidMACForMessage = errors.New("invalid MAC for incoming message")
// decryptReceivedMessage decrypts a received message.
func decryptReceivedMessage(msg []byte) ([]byte, error) {
// decrypt signalservice envelope
macpos := len(msg) - 10
tmac := msg[macpos:]
aesKey := registration.Registration.SignalingKey[:32]
macKey := registration.Registration.SignalingKey[32:]
hasError := false
if !axolotl.ValidTruncMAC(msg[:macpos], tmac, macKey) {
hasError = true
//return ErrInvalidMACForMessage
}
plaintext := []byte{}
var err error
// check if the message is using the signaling key
if hasError {
plaintext = msg
} else {
ciphertext := msg[1:macpos]
plaintext, err = axolotl.Decrypt(aesKey, ciphertext)
if err != nil {
return nil, err
}
}
return plaintext, nil
}
func createEnvelope(plaintext []byte) (*signalservice.Envelope, error) {
env := &signalservice.Envelope{}
err := proto.Unmarshal(plaintext, env)
if err != nil {
return nil, err
}
return env, nil
}
// Authenticate and decrypt a received message
func handleReceivedMessage(env *signalservice.Envelope) error {
recid := env.GetSourceUuid()
sc := axolotl.NewSessionCipher(textSecureStore, textSecureStore, textSecureStore, textSecureStore, recid, env.GetSourceDevice())
switch *env.Type {
case signalservice.Envelope_RECEIPT:
handleReceipt(env)
return nil
case signalservice.Envelope_CIPHERTEXT:
msg := env.GetContent()
if msg == nil {
return errors.New("[textsecure] Legacy messages unsupported")
}
wm, err := axolotl.LoadWhisperMessage(msg)
if err != nil {
log.Infof("[textsecure] Incoming WhisperMessage %s.\n", err)
return err
}
b, err := sc.SessionDecryptWhisperMessage(wm)
if _, ok := err.(axolotl.DuplicateMessageError); ok {
log.Infof("[textsecure] Incoming WhisperMessage %s. Ignoring.\n", err)
return nil
}
if _, ok := err.(axolotl.InvalidMessageError); ok {
// try the legacy way
log.Infof("[textsecure] Incoming WhisperMessage try legacy decrypting")
recid, err := recID(env.GetSourceUuid())
if err != nil {
recid = env.GetSourceUuid()
}
sc := axolotl.NewSessionCipher(textSecureStore, textSecureStore, textSecureStore, textSecureStore, recid, env.GetSourceDevice())
b, err = sc.SessionDecryptWhisperMessage(wm)
if _, ok := err.(axolotl.DuplicateMessageError); ok {
log.Infof("[textsecure] Incoming WhisperMessage %s. Ignoring.\n", err)
return nil
}
}
if err != nil {
return err
}
b = stripPadding(b)
err = handleMessage(env.GetSourceUuid(), env.GetSourceUuid(), env.GetServerTimestamp(), b)
if err != nil {
return err
}
case signalservice.Envelope_PREKEY_BUNDLE:
msg := env.GetContent()
pkwm, err := axolotl.LoadPreKeyWhisperMessage(msg)
if err != nil {
return err
}
b, err := sc.SessionDecryptPreKeyWhisperMessage(pkwm)
if _, ok := err.(axolotl.DuplicateMessageError); ok {
log.Infof("[textsecure] Incoming PreKeyWhisperMessage %s. Ignoring.\n", err)
return nil
}
if _, ok := err.(axolotl.PreKeyNotFoundError); ok {
log.Infof("[textsecure] Incoming PreKeyWhisperMessage %s. Ignoring.\n", err)
return nil
}
if _, ok := err.(axolotl.InvalidMessageError); ok {
log.Infof("[textsecure] Incoming PreKeyWhisperMessage %s. Ignoring.\n", err)
return nil
}
if err != nil {
return err
}
b = stripPadding(b)
err = handleMessage(env.GetSourceUuid(), env.GetSourceUuid(), env.GetServerTimestamp(), b)
if err != nil {
return err
}
case signalservice.Envelope_UNIDENTIFIED_SENDER:
if registration.Registration.SignalingKey[0] != 1 {
log.Errorln("failed to handle message, signalingkey has wrong version, please re-register to update your signaling-key")
}
p, _ := proto.Marshal(env)
log.Debugf("[textsecure] Incoming UnidentifiedSenderMessage %s.\n", *env.DestinationUuid)
data, err := crayfish.Instance.HandleEnvelope(p)
if err != nil {
return err
}
content, err := base64.StdEncoding.DecodeString(data.Message)
if err != nil {
return err
}
env.Content = content
log.Println("[textsecure] handleReceivedMessage:", data.Sender.UUID)
log.Debugln("[textsecure] handleReceivedMessage content length: ", len(content))
if len(content) == 0 {
err = errors.New("[textsecure] handleReceivedMessage content length is 0")
return err
}
err = handleMessage("", data.Sender.UUID, uint64(data.Timestamp), content)
if err != nil {
return err
}
default:
return MessageTypeNotImplementedError{uint32(*env.Type)}
}
return nil
}