-
Notifications
You must be signed in to change notification settings - Fork 14
/
extra_key.go
52 lines (43 loc) · 1.9 KB
/
extra_key.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
package otr3
func (c *Conversation) processExtraSymmetricKeyTLV(t tlv, x dataMessageExtra) (toSend *tlv, err error) {
rest, usage, ok := ExtractWord(t.tlvValue[:t.tlvLength])
if ok {
c.receivedSymKey(usage, rest, x.key)
}
return nil, nil
}
// UseExtraSymmetricKey takes a usage parameter and optional usageData and returns the current symmetric key
// and a set of messages to send in order to ask the peer to use the same symmetric key for the usage defined
func (c *Conversation) UseExtraSymmetricKey(usage uint32, usageData []byte) ([]byte, []ValidMessage, error) {
if c.msgState != encrypted ||
c.keys.theirKeyID == 0 {
return nil, nil, newOtrError("cannot send message in current state")
}
t := tlv{
tlvType: tlvTypeExtraSymmetricKey,
tlvLength: 4 + uint16(len(usageData)),
tlvValue: append(AppendWord(nil, usage), usageData...),
}
toSend, x, err := c.createSerializedDataMessage(nil, messageFlagIgnoreUnreadable, []tlv{t})
return x.key, toSend, err
}
// ReceivedKeyHandler is an interface that will be invoked when an extra key is received
type ReceivedKeyHandler interface {
// ReceivedSymmetricKey will be called when a TLV requesting the use of a symmetric key is received
ReceivedSymmetricKey(usage uint32, usageData []byte, symkey []byte)
}
type dynamicReceivedKeyHandler struct {
eh func(usage uint32, usageData []byte, symkey []byte)
}
func (d dynamicReceivedKeyHandler) ReceivedSymmetricKey(usage uint32, usageData []byte, symkey []byte) {
d.eh(usage, usageData, symkey)
}
func (c *Conversation) receivedSymKey(usage uint32, usageData []byte, symkey []byte) {
if c.receivedKeyHandler != nil {
c.receivedKeyHandler.ReceivedSymmetricKey(usage, usageData, symkey)
}
}
// SetReceivedKeyHandler will set the handler for what is to happen when an extra symmetric key is received
func (c *Conversation) SetReceivedKeyHandler(keyHandler ReceivedKeyHandler) {
c.receivedKeyHandler = keyHandler
}