From c9c0e06eed439a60df0ef7aa13c0b97e19cdd3a7 Mon Sep 17 00:00:00 2001 From: Adam Langley Date: Wed, 17 Oct 2012 14:58:16 -0400 Subject: [PATCH] go.crypto/otr: expose IsEncrypted. It's useful to expose this bit of state because, although callers can keep track of it themselves, it's already in the Conversation so it seems wasteful to make them do so. R=golang-dev, ioerror CC=golang-dev https://golang.org/cl/6724056 --- otr/otr.go | 7 +++++++ otr/otr_test.go | 14 ++++++++++++++ 2 files changed, 21 insertions(+) diff --git a/otr/otr.go b/otr/otr.go index e4890d5..30b39fb 100644 --- a/otr/otr.go +++ b/otr/otr.go @@ -508,6 +508,13 @@ func (c *Conversation) End() (toSend [][]byte) { panic("unreachable") } +// IsEncrypted returns true if a message passed to Send would be encrypted +// before transmission. This result remains valid until the next call to +// Receive or End, which may change the state of the Conversation. +func (c *Conversation) IsEncrypted() bool { + return c.state == stateEncrypted +} + var fragmentError = errors.New("otr: invalid OTR fragment") // processFragment processes a fragmented OTR message and possibly returns a diff --git a/otr/otr_test.go b/otr/otr_test.go index 1478609..1e2bbfc 100644 --- a/otr/otr_test.go +++ b/otr/otr_test.go @@ -139,6 +139,13 @@ func TestConversation(t *testing.T) { var err error alicesMessage = append(alicesMessage, []byte(QueryMessage)) + if alice.IsEncrypted() { + t.Error("Alice believes that the conversation is secure before we've started") + } + if bob.IsEncrypted() { + t.Error("Bob believes that the conversation is secure before we've started") + } + for round := 0; len(alicesMessage) > 0 || len(bobsMessage) > 0; round++ { bobsMessage = nil for i, msg := range alicesMessage { @@ -180,6 +187,13 @@ func TestConversation(t *testing.T) { t.Errorf("Session identifiers don't match. Alice has %x, Bob has %x", alice.SSID[:], bob.SSID[:]) } + if !alice.IsEncrypted() { + t.Error("Alice doesn't believe that the conversation is secure") + } + if !bob.IsEncrypted() { + t.Error("Bob doesn't believe that the conversation is secure") + } + var testMessage = []byte("hello Bob") alicesMessage, err = alice.Send(testMessage) for i, msg := range alicesMessage {