Skip to content

Commit

Permalink
Merge pull request #8 from status-im/feature/dont-delete-key-until-co…
Browse files Browse the repository at this point in the history
…nfirmed

Don't delete key without explicit request
  • Loading branch information
cammellos authored Feb 14, 2019
2 parents 4dcb6cb + ad7500e commit f2aeb83
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 3 deletions.
20 changes: 18 additions & 2 deletions session.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ type Session interface {

// RatchetDecrypt is called to AEAD-decrypt messages.
RatchetDecrypt(m Message, associatedData []byte) ([]byte, error)

//DeleteMk remove a message key from the database
DeleteMk(Key, uint32) error
}

type sessionState struct {
Expand Down Expand Up @@ -101,6 +104,11 @@ func (s *sessionState) RatchetEncrypt(plaintext, ad []byte) (Message, error) {
return Message{h, ct}, nil
}

// DeleteMk deletes a message key
func (s *sessionState) DeleteMk(dh Key, n uint32) error {
return s.MkSkipped.DeleteMk(dh, uint(n))
}

// RatchetDecrypt is called to decrypt messages.
func (s *sessionState) RatchetDecrypt(m Message, ad []byte) ([]byte, error) {
// Is the message one of the skipped?
Expand All @@ -114,7 +122,6 @@ func (s *sessionState) RatchetDecrypt(m Message, ad []byte) ([]byte, error) {
if err != nil {
return nil, fmt.Errorf("can't decrypt skipped message: %s", err)
}
_ = s.MkSkipped.DeleteMk(m.Header.DH, uint(m.Header.N))
if err := s.store(); err != nil {
return nil, err
}
Expand Down Expand Up @@ -149,11 +156,20 @@ func (s *sessionState) RatchetDecrypt(m Message, ad []byte) ([]byte, error) {
return nil, fmt.Errorf("can't decrypt: %s", err)
}

// Append current key, waiting for confirmation
skippedKeys := append(skippedKeys1, skippedKeys2...)
skippedKeys = append(skippedKeys, skippedKey{
key: sc.DHr,
nr: uint(m.Header.N),
mk: mk,
seq: sc.KeysCount,
})

// Increment the number of keys
sc.KeysCount++

// Apply changes.
if err := s.applyChanges(sc, s.id, append(skippedKeys1, skippedKeys2...)); err != nil {
if err := s.applyChanges(sc, s.id, skippedKeys); err != nil {
return nil, err
}

Expand Down
2 changes: 1 addition & 1 deletion session_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ func TestSession_RatchetDecrypt_CommunicationSkippedMessages(t *testing.T) {

bobSkippedCount, err = bob.MkSkipped.Count(bob.DHr)
require.NoError(t, err)
require.EqualValues(t, 1, bobSkippedCount)
require.EqualValues(t, 2, bobSkippedCount)

_, err = bob.RatchetDecrypt(m5, nil) // Too many messages
require.NotNil(t, err)
Expand Down

0 comments on commit f2aeb83

Please sign in to comment.