File: extra_key.go

package info (click to toggle)
golang-github-twstrike-otr3 0.0~git20161015.0.744856d-3.1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, forky, sid, trixie
  • size: 1,080 kB
  • sloc: ansic: 127; makefile: 76
file content (47 lines) | stat: -rw-r--r-- 1,720 bytes parent folder | download | duplicates (3)
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
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)
	}
}