-
Notifications
You must be signed in to change notification settings - Fork 80
/
verifier.go
476 lines (393 loc) · 13.6 KB
/
verifier.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
package gui
import (
"crypto/rand"
"fmt"
"log"
"math/big"
"regexp"
"github.com/coyim/coyim/i18n"
"github.com/coyim/coyim/session/access"
"github.com/coyim/coyim/xmpp/jid"
"github.com/coyim/gotk3adapter/gtki"
)
type verifier struct {
parentWindow gtki.Window
session access.Session
notifier *notifier
pinWindow *pinWindow
answerSMPWindow *answerSMPWindow
smpFailed *smpFailedNotification
waitingForPeer *waitingForPeerNotification
peerRequestsSMP *peerRequestsSMPNotification
unverifiedWarning *unverifiedWarning
verificationSuccess *verificationSuccessNotification
peerName func() string
peerToSendTo func() jid.WithResource
}
type notifier struct {
notificationArea gtki.Box
}
func (n *notifier) notify(i gtki.InfoBar) {
n.notificationArea.Add(i)
}
// TODO: unify repeated stuff
func newVerifier(u *gtkUI, conv *conversationPane) *verifier {
v := &verifier{
parentWindow: conv.transientParent,
session: conv.account.session,
notifier: ¬ifier{conv.notificationArea},
peerName: func() string {
p, ok := conv.currentPeer()
if !ok {
return ""
}
return p.NameForPresentation()
},
peerToSendTo: func() jid.WithResource {
return conv.currentPeerForSending().(jid.WithResource)
},
}
v.buildPinWindow()
v.buildAnswerSMPDialog()
// A function is used below because we cannot check whether a contact is verified
// when newVerifier is called.
v.buildUnverifiedWarning(func() bool {
return conv.isEncrypted() && !conv.hasVerifiedKey()
})
v.buildWaitingForPeerNotification()
v.buildPeerRequestsSMPNotification()
v.buildSMPFailedDialog()
return v
}
type pinWindow struct {
b *builder
dialog gtki.Dialog
prompt gtki.Label
pin gtki.Label
smpImage gtki.Image
padlockImage1 gtki.Image
padlockImage2 gtki.Image
alertImage gtki.Image
}
func (v *verifier) buildPinWindow() {
v.pinWindow = &pinWindow{
b: newBuilder("GeneratePIN"),
}
v.pinWindow.b.getItems(
"dialog", &v.pinWindow.dialog,
"prompt", &v.pinWindow.prompt,
"pin", &v.pinWindow.pin,
"smp_image", &v.pinWindow.smpImage,
"padlock_image1", &v.pinWindow.padlockImage1,
"padlock_image2", &v.pinWindow.padlockImage2,
"alert_image", &v.pinWindow.alertImage,
)
v.pinWindow.dialog.HideOnDelete()
v.pinWindow.dialog.SetTransientFor(v.parentWindow)
addBoldHeaderStyle(v.pinWindow.pin)
v.pinWindow.b.ConnectSignals(map[string]interface{}{
"close_share_pin": func() {
v.showWaitingForPeerToCompleteSMPDialog()
v.pinWindow.dialog.Hide()
},
})
setImageFromFile(v.pinWindow.smpImage, "smp.svg")
setImageFromFile(v.pinWindow.padlockImage1, "padlock.svg")
setImageFromFile(v.pinWindow.padlockImage2, "padlock.svg")
setImageFromFile(v.pinWindow.alertImage, "alert.svg")
}
func (v *verifier) updateUnverifiedWarning() {
v.unverifiedWarning.update()
}
func (v *verifier) hideUnverifiedWarning() {
v.unverifiedWarning.infobar.Hide()
}
// TODO: check on linux
type unverifiedWarning struct {
b *builder
infobar gtki.Box
closeInfobar gtki.Box
notification gtki.Box
label gtki.Label
image gtki.Image
button gtki.Button
shouldShowVerificationBar func() bool
}
// TODO: how will this work with i18n?
var question = "Please enter the PIN that I shared with you."
var coyIMQuestion = regexp.MustCompile("Please enter the PIN that I shared with you.")
func (u *unverifiedWarning) update() {
if u.shouldShowVerificationBar() {
u.infobar.Show()
u.label.Show()
u.image.ShowAll()
} else {
u.infobar.Hide()
}
}
func (v *verifier) buildUnverifiedWarning(shouldShowVerificationBar func() bool) {
v.unverifiedWarning = &unverifiedWarning{
b: newBuilder("UnverifiedWarning"),
}
v.unverifiedWarning.shouldShowVerificationBar = shouldShowVerificationBar
v.unverifiedWarning.b.getItems(
"verify-infobar", &v.unverifiedWarning.infobar,
"verify-close-infobar", &v.unverifiedWarning.closeInfobar,
"verify-notification", &v.unverifiedWarning.notification,
"verify-message", &v.unverifiedWarning.label,
"verify-image", &v.unverifiedWarning.image,
"verify-button", &v.unverifiedWarning.button,
)
v.unverifiedWarning.b.ConnectSignals(map[string]interface{}{
"on_press_image": v.hideUnverifiedWarning,
})
prov := providerWithCSS("box { background-color: #fff3f3; color: #000000; border: 2px; }")
updateWithStyle(v.unverifiedWarning.infobar, prov)
prov = providerWithCSS("box { background-color: #e5d7d6; }")
updateWithStyle(v.unverifiedWarning.closeInfobar, prov)
setImageFromFile(v.unverifiedWarning.image, "warning.svg")
v.unverifiedWarning.label.SetLabel(i18n.Local("Make sure no one else\nis reading your messages"))
v.unverifiedWarning.button.Connect("clicked", v.showPINDialog)
v.notifier.notify(v.unverifiedWarning.infobar)
}
func (v *verifier) smpError(err error) {
v.hideUnverifiedWarning()
v.showCannotGeneratePINDialog(err)
}
func (v *verifier) showPINDialog() {
v.unverifiedWarning.infobar.Hide()
pin, err := createPIN()
if err != nil {
v.pinWindow.dialog.Hide()
v.smpError(err)
return
}
v.pinWindow.pin.SetText(pin)
v.pinWindow.prompt.SetMarkup(i18n.Localf("Share this one-time PIN with <b>%s</b>", v.peerName()))
v.session.StartSMP(v.peerToSendTo(), question, pin)
v.pinWindow.dialog.ShowAll()
v.waitingForPeer.label.SetLabel(i18n.Localf("Waiting for peer to finish \nsecuring the channel..."))
v.waitingForPeer.infobar.ShowAll()
}
func createPIN() (string, error) {
val, err := rand.Int(rand.Reader, big.NewInt(int64(1000000)))
if err != nil {
log.Printf("Error encountered when creating a new PIN: %v", err)
return "", err
}
return fmt.Sprintf("%06d", val), err
}
type waitingForPeerNotification struct {
b *builder
infobar gtki.InfoBar
label gtki.Label
image gtki.Image
button gtki.Button
}
func (v *verifier) buildWaitingForPeerNotification() {
v.waitingForPeer = &waitingForPeerNotification{
b: newBuilder("WaitingSMPComplete"),
}
v.waitingForPeer.b.getItems(
"smp-waiting-infobar", &v.waitingForPeer.infobar,
"smp-waiting-label", &v.waitingForPeer.label,
"smp-waiting-image", &v.waitingForPeer.image,
"smp-waiting-button", &v.waitingForPeer.button,
)
prov := providerWithCSS("box { background-color: #fff3f3; color: #000000; border: 2px; }")
updateWithStyle(v.waitingForPeer.infobar, prov)
v.waitingForPeer.label.SetText(i18n.Localf("Waiting for peer to finish \nsecuring the channel..."))
setImageFromFile(v.waitingForPeer.image, "waiting.svg")
v.waitingForPeer.button.Connect("clicked", func() {
v.cancelSMP()
})
v.notifier.notify(v.waitingForPeer.infobar)
}
func (v *verifier) showWaitingForPeerToCompleteSMPDialog() {
v.waitingForPeer.label.SetLabel(i18n.Localf("Waiting for peer to finish \nsecuring the channel..."))
v.hideUnverifiedWarning()
v.waitingForPeer.infobar.ShowAll()
}
func (v *verifier) showCannotGeneratePINDialog(err error) {
b := newBuilder("CannotVerifyWithSMP")
infobar := b.getObj("smp-error-infobar").(gtki.InfoBar)
label := b.getObj("smp-error-label").(gtki.Label)
image := b.getObj("smp-error-image").(gtki.Image)
button := b.getObj("smp-error-button").(gtki.Button)
prov := providerWithCSS("box { background-color: #fff3f3; color: #000000; border: 2px; }")
updateWithStyle(infobar, prov)
label.SetText(i18n.Local("Unable to verify at this time."))
log.Printf("Cannot recover from error: %v. Quitting SMP verification.", err)
setImageFromFile(image, "failure.svg")
button.Connect("clicked", infobar.Destroy)
infobar.ShowAll()
v.notifier.notify(infobar)
}
type answerSMPWindow struct {
b *builder
dialog gtki.Dialog
question gtki.Label
answer gtki.Entry
submitButton gtki.Button
smpImage gtki.Image
padlockImage1 gtki.Image
padlockImage2 gtki.Image
alertImage gtki.Image
}
func (v *verifier) buildAnswerSMPDialog() {
v.answerSMPWindow = &answerSMPWindow{
b: newBuilder("AnswerSMPQuestion"),
}
v.answerSMPWindow.b.getItems(
"dialog", &v.answerSMPWindow.dialog,
"question_from_peer", &v.answerSMPWindow.question,
"button_submit", &v.answerSMPWindow.submitButton,
"answer", &v.answerSMPWindow.answer,
"smp_image", &v.answerSMPWindow.smpImage,
"padlock_image1", &v.answerSMPWindow.padlockImage1,
"padlock_image2", &v.answerSMPWindow.padlockImage2,
"alert_image", &v.answerSMPWindow.alertImage,
)
v.answerSMPWindow.dialog.HideOnDelete()
v.answerSMPWindow.dialog.SetTransientFor(v.parentWindow)
v.answerSMPWindow.submitButton.SetSensitive(false)
setImageFromFile(v.answerSMPWindow.smpImage, "smp.svg")
setImageFromFile(v.answerSMPWindow.padlockImage1, "padlock.svg")
setImageFromFile(v.answerSMPWindow.padlockImage2, "padlock.svg")
setImageFromFile(v.answerSMPWindow.alertImage, "alert.svg")
v.answerSMPWindow.b.ConnectSignals(map[string]interface{}{
"text_changing": func() {
answer, _ := v.answerSMPWindow.answer.GetText()
v.answerSMPWindow.submitButton.SetSensitive(len(answer) > 0)
},
"close_share_pin": func() {
answer, _ := v.answerSMPWindow.answer.GetText()
v.removeInProgressDialogs()
v.session.FinishSMP(v.peerToSendTo(), answer)
v.showWaitingForPeerToCompleteSMPDialog()
},
})
}
func (v *verifier) showAnswerSMPDialog(question string) {
if "" == question {
v.answerSMPWindow.question.SetMarkup(i18n.Localf("Enter the secret that <b>%s</b> shared with you", v.peerName()))
} else if coyIMQuestion.MatchString(question) {
v.answerSMPWindow.question.SetMarkup(i18n.Localf("Type the PIN that <b>%s</b> sent you. It can be used only once.", v.peerName()))
} else {
v.answerSMPWindow.question.SetMarkup(i18n.Localf("Enter the answer to\n<b>%s</b>", question))
}
v.answerSMPWindow.answer.SetText("")
v.answerSMPWindow.dialog.ShowAll()
}
type peerRequestsSMPNotification struct {
b *builder
infobar gtki.Box
closeInfobar gtki.Box
notification gtki.Box
label gtki.Label
image gtki.Image
button gtki.Button
}
func (p *peerRequestsSMPNotification) show() {
p.infobar.Show()
p.closeInfobar.Show()
p.label.Show()
}
func (v *verifier) buildPeerRequestsSMPNotification() {
v.peerRequestsSMP = &peerRequestsSMPNotification{
b: newBuilder("PeerRequestsSMP"),
}
v.peerRequestsSMP.b.getItems(
"smp-requested-infobar", &v.peerRequestsSMP.infobar,
"smp-requested-close-infobar", &v.peerRequestsSMP.closeInfobar,
"smp-requested-notification", &v.peerRequestsSMP.notification,
"smp-requested-message", &v.peerRequestsSMP.label,
"smp-requested-image", &v.peerRequestsSMP.image,
"smp-requested-button", &v.peerRequestsSMP.button,
)
prov := providerWithCSS("box { background-color: #fff3f3; color: #000000; border: 2px; }")
updateWithStyle(v.peerRequestsSMP.infobar, prov)
prov = providerWithCSS("box { background-color: #e5d7d6; }")
updateWithStyle(v.peerRequestsSMP.closeInfobar, prov)
v.peerRequestsSMP.b.ConnectSignals(map[string]interface{}{
"on_press_close_image": v.cancelSMP,
})
setImageFromFile(v.peerRequestsSMP.image, "waiting.svg")
v.notifier.notify(v.peerRequestsSMP.infobar)
}
func (v *verifier) displayRequestForSecret(question string) {
v.hideUnverifiedWarning()
v.peerRequestsSMP.label.SetLabel(i18n.Localf("Finish verifying the \nsecurity of this channel..."))
v.peerRequestsSMP.button.Connect("clicked", func() {
v.showAnswerSMPDialog(question)
})
v.peerRequestsSMP.show()
}
type verificationSuccessNotification struct {
b *builder
dialog gtki.Dialog
label gtki.Label
image gtki.Image
button gtki.Button
}
func (v *verifier) displayVerificationSuccess() {
v.verificationSuccess = &verificationSuccessNotification{
b: newBuilder("VerificationSucceeded"),
}
v.verificationSuccess.b.getItems(
"verif-success-dialog", &v.verificationSuccess.dialog,
"verif-success-label", &v.verificationSuccess.label,
"verif-success-image", &v.verificationSuccess.image,
"verif-success-button", &v.verificationSuccess.button,
)
v.verificationSuccess.button.Connect("clicked", v.verificationSuccess.dialog.Destroy)
v.verificationSuccess.label.SetMarkup(i18n.Localf("Hooray! No one is listening to your conversations with <b>%s</b>", v.peerName()))
setImageFromFile(v.verificationSuccess.image, "smpsuccess.svg")
v.hideUnverifiedWarning()
v.verificationSuccess.dialog.SetTransientFor(v.parentWindow)
v.verificationSuccess.dialog.ShowAll()
}
type smpFailedNotification struct {
dialog gtki.Dialog
label gtki.Label
button gtki.Button
}
// TODO: make this consistent
func (v *verifier) buildSMPFailedDialog() {
builder := newBuilder("VerificationFailed")
v.smpFailed = &smpFailedNotification{
dialog: builder.getObj("verif-failure-dialog").(gtki.Dialog),
label: builder.getObj("verif-failure-label").(gtki.Label),
button: builder.getObj("verif-failure-button").(gtki.Button),
}
v.smpFailed.dialog.SetTransientFor(v.parentWindow)
v.smpFailed.dialog.HideOnDelete()
v.smpFailed.dialog.Connect("response", func() {
v.updateUnverifiedWarning()
v.smpFailed.dialog.Hide()
})
v.smpFailed.button.Connect("clicked", func() {
v.updateUnverifiedWarning()
v.smpFailed.dialog.Hide()
})
}
func (v *verifier) displayVerificationFailure() {
v.smpFailed.label.SetMarkup(i18n.Localf("We could not verify this channel with <b>%s</b>.", v.peerName()))
v.smpFailed.dialog.ShowAll()
}
func (v *verifier) updateInProgressDialogs(encrypted bool) {
if !encrypted {
v.removeInProgressDialogs()
}
}
func (v *verifier) removeInProgressDialogs() {
v.peerRequestsSMP.infobar.Hide()
v.waitingForPeer.infobar.Hide()
v.pinWindow.dialog.Hide()
v.answerSMPWindow.dialog.Hide()
}
func (v *verifier) cancelSMP() {
v.removeInProgressDialogs()
v.session.AbortSMP(v.peerToSendTo())
v.updateUnverifiedWarning()
}