Skip to content

Commit 2382e8c

Browse files
committed
Remove all the annoying ErrGPG things and provide with better error messages
1 parent 6557048 commit 2382e8c

11 files changed

+43
-48
lines changed

conversation_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ func Test_Send_returnsErrorIfFaislToGenerateDataMsg(t *testing.T) {
136136
s, err := c.Send(msg)
137137

138138
assertNil(t, s)
139-
assertEquals(t, err, ErrGPGConflict)
139+
assertEquals(t, err, newOtrConflictError("invalid key id for remote peer"))
140140
}
141141

142142
func Test_send_appendWhitespaceTagsWhenAllowedbyThePolicy(t *testing.T) {

data_message.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ func (c *Conversation) genDataMsg(message []byte, tlvs ...tlv) (dataMsg, dataMes
1212

1313
func (c *Conversation) genDataMsgWithFlag(message []byte, flag byte, tlvs ...tlv) (dataMsg, dataMessageExtra, error) {
1414
if c.msgState != encrypted {
15-
return dataMsg{}, dataMessageExtra{}, ErrGPGConflict
15+
return dataMsg{}, dataMessageExtra{}, newOtrConflictError("cannot send message in unencrypted state")
1616
}
1717

1818
keys, err := c.keys.calculateDHSessionKeys(c.keys.ourKeyID-1, c.keys.theirKeyID)

data_message_test.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ func Test_genDataMsg_returnsErrorIfFailsToCalculateDHSessionKey(t *testing.T) {
136136
c := newConversation(otrV3{}, rand.Reader)
137137
c.msgState = encrypted
138138
_, _, err := c.genDataMsg(nil)
139-
assertEquals(t, err, ErrGPGConflict)
139+
assertEquals(t, err, newOtrConflictError("invalid key id for local peer"))
140140
}
141141

142142
func Test_genDataMsg_returnsErrorIfFailsToGenerateInstanceTag(t *testing.T) {
@@ -241,7 +241,7 @@ func Test_processDataMessage_returnsErrorIfDataMessageHasWrongCounter(t *testing
241241
c.msgState = encrypted
242242
_, _, err := c.receiveDecoded(msg)
243243

244-
assertEquals(t, err, ErrGPGConflict)
244+
assertDeepEquals(t, err, newOtrConflictError("counter regressed"))
245245
}
246246

247247
func Test_processDataMessage_signalsThatMessageIsUnreadableForAGPGConflictError(t *testing.T) {
@@ -336,7 +336,7 @@ func Test_processDataMessage_shouldNotRotateKeysWhenDecryptFails(t *testing.T) {
336336
bob.msgState = encrypted
337337
_, _, err := bob.receiveDecoded(msg)
338338

339-
assertDeepEquals(t, err, ErrGPGConflict)
339+
assertDeepEquals(t, err, newOtrConflictError("bad signature MAC in encrypted signature"))
340340
assertDeepEquals(t, bobCurrentDHKeys, bob.keys.ourCurrentDHKeys)
341341
assertDeepEquals(t, bobPreviousDHKeys, bob.keys.ourPreviousDHKeys)
342342

@@ -454,5 +454,5 @@ func Test_processDataMessage_returnErrorWhenOurKeyIDUnexpected(t *testing.T) {
454454
bob.msgState = encrypted
455455
_, _, err := bob.receiveDecoded(datamsg)
456456

457-
assertDeepEquals(t, err, ErrGPGConflict)
457+
assertDeepEquals(t, err, newOtrConflictError("mismatched key id for local peer"))
458458
}

errors.go

+16-21
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,6 @@
11
package otr3
22

3-
import (
4-
"errors"
5-
"fmt"
6-
)
7-
8-
var (
9-
// GPG_ERR_NO_ERROR is matched to nil
10-
// GPG_ERR_ENOMEM doesn't make any sense in golang
11-
12-
// ErrGPGUnusableSecretKey maps to GPG_ERR_UNUSABLE_SECKEY in libotr
13-
ErrGPGUnusableSecretKey = errors.New("GPG Error: Unusable secret key (54)")
14-
// ErrGPGInvalidValue maps to GPG_ERR_INV_VALUE in libotr
15-
ErrGPGInvalidValue = errors.New("GPG Error: Invalid value (55)")
16-
// ErrGPGConflict maps to GPG_ERR_CONFLICT in libotr
17-
ErrGPGConflict = errors.New("GPG Error: Conflict (70)")
18-
// ErrGPGEntityExist maps to GPG_ERR_EEXIST in libotr
19-
ErrGPGEntityExist = errors.New("GPG Error: Entity exist (32803)")
20-
)
3+
import "fmt"
214

225
var errCantAuthenticateWithoutEncryption = newOtrError("can't authenticate a peer without a secure conversation established")
236
var errCorruptEncryptedSignature = newOtrError("corrupt encrypted signature")
@@ -34,15 +17,20 @@ var errWrongProtocolVersion = newOtrError("wrong protocol version")
3417

3518
// OtrError is an error in the OTR library
3619
type OtrError struct {
37-
msg string
20+
msg string
21+
conflict bool
3822
}
3923

4024
func newOtrError(s string) error {
41-
return OtrError{s}
25+
return OtrError{msg: s, conflict: false}
26+
}
27+
28+
func newOtrConflictError(s string) error {
29+
return OtrError{msg: s, conflict: true}
4230
}
4331

4432
func newOtrErrorf(format string, a ...interface{}) error {
45-
return OtrError{fmt.Sprintf(format, a...)}
33+
return OtrError{msg: fmt.Sprintf(format, a...), conflict: false}
4634
}
4735

4836
func (oe OtrError) Error() string {
@@ -57,3 +45,10 @@ func firstError(es ...error) error {
5745
}
5846
return nil
5947
}
48+
49+
func isConflict(e error) bool {
50+
if oe, ok := e.(OtrError); ok {
51+
return oe.conflict
52+
}
53+
return false
54+
}

heartbeat_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -79,5 +79,5 @@ func Test_potentialHeartbeat_returnsAnErrorIfWeCantPutTogetherAMessage(t *testin
7979
plain := []byte("Foo plain")
8080

8181
_, err := c.potentialHeartbeat(plain)
82-
assertDeepEquals(t, err, ErrGPGConflict)
82+
assertDeepEquals(t, err, newOtrConflictError("invalid key id for local peer"))
8383
}

key_management.go

+6-6
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ func (k *keyManagementContext) checkMessageCounter(message dataMsg) error {
140140
theirNextCounter := binary.BigEndian.Uint64(message.topHalfCtr[:])
141141

142142
if theirNextCounter <= counter.theirCounter {
143-
return ErrGPGConflict
143+
return newOtrConflictError("counter regressed")
144144
}
145145

146146
counter.theirCounter = theirNextCounter
@@ -255,7 +255,7 @@ func calculateDHSessionKeys(ourPrivKey, ourPubKey, theirPubKey *big.Int) session
255255

256256
func (k *keyManagementContext) pickOurKeys(ourKeyID uint32) (privKey, pubKey *big.Int, err error) {
257257
if ourKeyID == 0 || k.ourKeyID == 0 {
258-
return nil, nil, ErrGPGConflict
258+
return nil, nil, newOtrConflictError("invalid key id for local peer")
259259
}
260260

261261
switch ourKeyID {
@@ -264,28 +264,28 @@ func (k *keyManagementContext) pickOurKeys(ourKeyID uint32) (privKey, pubKey *bi
264264
case k.ourKeyID - 1:
265265
privKey, pubKey = k.ourPreviousDHKeys.priv, k.ourPreviousDHKeys.pub
266266
default:
267-
err = ErrGPGConflict
267+
err = newOtrConflictError("mismatched key id for local peer")
268268
}
269269

270270
return privKey, pubKey, err
271271
}
272272

273273
func (k *keyManagementContext) pickTheirKey(theirKeyID uint32) (pubKey *big.Int, err error) {
274274
if theirKeyID == 0 || k.theirKeyID == 0 {
275-
return nil, ErrGPGConflict
275+
return nil, newOtrConflictError("invalid key id for remote peer")
276276
}
277277

278278
switch theirKeyID {
279279
case k.theirKeyID:
280280
pubKey = k.theirCurrentDHPubKey
281281
case k.theirKeyID - 1:
282282
if k.theirPreviousDHPubKey == nil {
283-
err = ErrGPGConflict
283+
err = newOtrConflictError("no previous key for remote peer found")
284284
} else {
285285
pubKey = k.theirPreviousDHPubKey
286286
}
287287
default:
288-
err = ErrGPGConflict
288+
err = newOtrConflictError("mismatched key id for remote peer")
289289
}
290290

291291
return pubKey, err

key_management_test.go

+10-10
Original file line numberDiff line numberDiff line change
@@ -64,10 +64,10 @@ func Test_calculateDHSessionKeys_failsWhenOurOrTheyKeyIsUnknown(t *testing.T) {
6464
}
6565

6666
_, err := c.calculateDHSessionKeys(2, 1)
67-
assertDeepEquals(t, err, ErrGPGConflict)
67+
assertDeepEquals(t, err, newOtrConflictError("mismatched key id for local peer"))
6868

6969
_, err = c.calculateDHSessionKeys(1, 3)
70-
assertDeepEquals(t, err, ErrGPGConflict)
70+
assertDeepEquals(t, err, newOtrConflictError("mismatched key id for remote peer"))
7171
}
7272

7373
func Test_calculateDHSessionKeys_failsWhenTheirPreviousPubliKeyIsNull(t *testing.T) {
@@ -77,36 +77,36 @@ func Test_calculateDHSessionKeys_failsWhenTheirPreviousPubliKeyIsNull(t *testing
7777
}
7878
_, err := c.calculateDHSessionKeys(2, 1)
7979

80-
assertEquals(t, err, ErrGPGConflict)
80+
assertEquals(t, err, newOtrConflictError("no previous key for remote peer found"))
8181
}
8282

8383
func Test_pickTheirKey_shouldFailsForInvalidSenderID(t *testing.T) {
8484
c := keyManagementContext{}
8585

8686
c.theirKeyID = uint32(0)
8787
_, err := c.pickTheirKey(uint32(0x00000000))
88-
assertEquals(t, err, ErrGPGConflict)
88+
assertEquals(t, err, newOtrConflictError("invalid key id for remote peer"))
8989

9090
c.theirKeyID = uint32(2)
9191
_, err = c.pickTheirKey(uint32(0x00000000))
92-
assertEquals(t, err, ErrGPGConflict)
92+
assertEquals(t, err, newOtrConflictError("invalid key id for remote peer"))
9393

9494
c.theirKeyID = uint32(1)
9595
c.theirPreviousDHPubKey = nil
9696
_, err = c.pickTheirKey(uint32(0x00000000))
97-
assertEquals(t, err, ErrGPGConflict)
97+
assertEquals(t, err, newOtrConflictError("invalid key id for remote peer"))
9898
}
9999

100100
func Test_pickOurKeys_shouldFailsForInvalidRecipientID(t *testing.T) {
101101
c := keyManagementContext{}
102102

103103
c.ourKeyID = uint32(0x00000000)
104104
_, _, err := c.pickOurKeys(uint32(0x00000000))
105-
assertEquals(t, err, ErrGPGConflict)
105+
assertEquals(t, err, newOtrConflictError("invalid key id for local peer"))
106106

107107
c.ourKeyID = uint32(3)
108108
_, _, err = c.pickOurKeys(uint32(0x00000001))
109-
assertEquals(t, err, ErrGPGConflict)
109+
assertEquals(t, err, newOtrConflictError("mismatched key id for local peer"))
110110
}
111111

112112
func Test_calculateAKEKeys(t *testing.T) {
@@ -283,12 +283,12 @@ func Test_checkMessageCounter_messageIsInvalidWhenCounterIsNotLargerThanTheLastR
283283
msg.topHalfCtr[7] = 2
284284

285285
err := c.checkMessageCounter(msg)
286-
assertEquals(t, err, ErrGPGConflict)
286+
assertEquals(t, err, newOtrConflictError("counter regressed"))
287287
assertEquals(t, ctr.theirCounter, uint64(2))
288288

289289
msg.topHalfCtr[7] = 1
290290
err = c.checkMessageCounter(msg)
291-
assertEquals(t, err, ErrGPGConflict)
291+
assertEquals(t, err, newOtrConflictError("counter regressed"))
292292
assertEquals(t, ctr.theirCounter, uint64(2))
293293
}
294294

messages.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ func (c dataMsg) checkSign(key macKey, header []byte) error {
146146
copy(authenticatorCalculated[:], mac.Sum(nil))
147147

148148
if subtle.ConstantTimeCompare(c.authenticator[:], authenticatorCalculated[:]) == 0 {
149-
return ErrGPGConflict
149+
return newOtrConflictError("bad signature MAC in encrypted signature")
150150
}
151151
return nil
152152
}

messages_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ func Test_dataMsgCheckSignWithError(t *testing.T) {
106106
authenticator: [20]byte{0x6e, 0x6, 0x76, 0x45, 0xbb, 0x94, 0x5c, 0xa2, 0xfc, 0x13, 0xa9, 0xfa, 0x58, 0xb7, 0xd7, 0x23, 0xee, 0xab, 0x62, 0xe8},
107107
}
108108
macKey := macKey{0x00, 0x01, 0x02, 0x03, 0x00, 0x01, 0x02, 0x03, 0x00, 0x01, 0x02, 0x03, 0x00, 0x01, 0x02, 0x03, 0x00, 0x01, 0x02, 0x03}
109-
assertDeepEquals(t, m.checkSign(macKey, []byte{}), ErrGPGConflict)
109+
assertDeepEquals(t, m.checkSign(macKey, []byte{}), newOtrConflictError("bad signature MAC in encrypted signature"))
110110
}
111111

112112
func Test_dataMsgDeserialze(t *testing.T) {

receive.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ func (c *Conversation) receiveDataMessage(messageHeader, messageBody []byte) (pl
175175

176176
func (c *Conversation) notifyDataMessageError(err error) {
177177
var e ErrorCode
178-
if err == ErrGPGConflict {
178+
if isConflict(err) {
179179
c.messageEvent(MessageEventReceivedMessageUnreadable)
180180
e = ErrorCodeMessageUnreadable
181181
} else {

resend_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,7 @@ func Test_maybeRetransmit_returnsErrorIfWeFailAtGeneratingDataMsg(t *testing.T)
185185
c.keys.ourKeyID = 0
186186
_, err := c.maybeRetransmit()
187187

188-
assertEquals(t, err, ErrGPGConflict)
188+
assertEquals(t, err, newOtrConflictError("invalid key id for local peer"))
189189
}
190190

191191
func Test_maybeRetransmit_signalsMessageEventWhenResendingMessage(t *testing.T) {

0 commit comments

Comments
 (0)