Skip to content

Commit

Permalink
feat: add berty.tech/go/pkg/errcode package (#1440)
Browse files Browse the repository at this point in the history
feat: add berty.tech/go/pkg/errcode package
  • Loading branch information
moul authored Oct 23, 2019
2 parents 189f9bb + 4dd1975 commit df86844
Show file tree
Hide file tree
Showing 27 changed files with 632 additions and 141 deletions.
86 changes: 86 additions & 0 deletions api/errcode.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
syntax = "proto3";

package berty.errcode;

import "github.com/gogo/protobuf/gogoproto/gogo.proto";

option go_package = "berty.tech/go/pkg/errcode";

option (gogoproto.benchgen_all) = false;
option (gogoproto.compare_all) = false;
option (gogoproto.description_all) = false;
option (gogoproto.enum_stringer_all) = false;
option (gogoproto.enumdecl_all) = true;
option (gogoproto.equal_all) = false;
option (gogoproto.face_all) = false;
option (gogoproto.gogoproto_import) = false;
option (gogoproto.goproto_enum_prefix_all) = false;
option (gogoproto.goproto_enum_stringer_all) = false;
option (gogoproto.goproto_extensions_map_all) = false;
option (gogoproto.goproto_getters_all) = false;
option (gogoproto.goproto_registration) = false;
//option (gogoproto.goproto_sizecache_all) = false;
option (gogoproto.goproto_stringer_all) = false;
//option (gogoproto.goproto_unkeyed_all) = false;
option (gogoproto.goproto_unrecognized_all) = false;
option (gogoproto.gostring_all) = false;
option (gogoproto.marshaler_all) = false;
option (gogoproto.messagename_all) = false;
option (gogoproto.onlyone_all) = false;
option (gogoproto.populate_all) = false;
option (gogoproto.protosizer_all) = false;
option (gogoproto.sizer_all) = false;
option (gogoproto.stable_marshaler_all) = false;
option (gogoproto.stringer_all) = false;
option (gogoproto.testgen_all) = false;
option (gogoproto.typedecl_all) = false;
option (gogoproto.unmarshaler_all) = false;
option (gogoproto.unsafe_marshaler_all) = false;
option (gogoproto.unsafe_unmarshaler_all) = false;
option (gogoproto.verbose_equal_all) = false;

enum ErrCode {
Undefined = 0; // default value, should never be set manually

TODO = 666; // indicates that you plan to write a custom error handler later
ErrNotImplemented = 777;
ErrInternal = 999; // can be used to translate an "unknown" error (without Code), i.e., in gRPC

//
// Generic helpers (try to use a more specific error when possible)
//

ErrInvalidInput = 101;
ErrMissingInput = 102;

//
// Berty Chat (starting at 1001)
//

//
// Berty Protocol (starting at 2001)
//

ErrSigChainNoEntries = 2001;
ErrSigChainInvalidEntryType = 2002;
ErrSigChainAlreadyInitialized = 2003;
ErrSigChainPermission = 2004;
ErrSigChainOperationAlreadyDone = 2005;
ErrHandshakeNoPayload = 2006;
ErrHandshakeInvalidFlow = 2007;
ErrHandshakeInvalidFlowStepNotFound = 2008;
ErrHandshakeParams = 2009;
ErrHandshakeNoAuthReturned = 2010;
ErrHandshakeInvalidKeyType = 2011;
ErrHandshakeInvalidSignature = 2012;
ErrHandshakeSessionInvalid = 2013;
ErrHandshakeKeyNotInSigChain = 2014;
ErrHandshakeDecrypt = 2015;

//
// Chat Bridge (starting at 3001)
//

ErrBridgeInterrupted = 3001;
ErrBridgeNotRunning = 3002;
}
1 change: 1 addition & 0 deletions docs/gen.sum

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 2 additions & 3 deletions go/internal/handshake/crypto_module.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,16 @@ import (
"crypto/rand"

"berty.tech/go/internal/crypto"

"berty.tech/go/pkg/errcode"
p2pcrypto "github.com/libp2p/go-libp2p-core/crypto"

"golang.org/x/crypto/nacl/box"
)

func bytesSliceToArray(slice []byte) (*[32]byte, error) {
var arr [32]byte

if len(slice) != 32 {
return nil, ErrInvalidKeyType
return nil, errcode.ErrHandshakeInvalidKeyType
}

for i, c := range slice {
Expand Down
25 changes: 12 additions & 13 deletions go/internal/handshake/crypto_session.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,8 @@ import (
"encoding/binary"

"berty.tech/go/internal/crypto"

"berty.tech/go/pkg/errcode"
p2pcrypto "github.com/libp2p/go-libp2p-core/crypto"

"golang.org/x/crypto/nacl/box"
)

Expand All @@ -32,7 +31,7 @@ func (h *handshakeSession) SetOtherKeys(sign p2pcrypto.PubKey, box []byte) error
}

if sign.Type() != SupportedKeyType {
return ErrInvalidKeyType
return errcode.ErrHandshakeInvalidKeyType
}

h.otherSigningPublicKey = sign
Expand All @@ -51,7 +50,7 @@ func (h *handshakeSession) GetPublicKeys() (sign p2pcrypto.PubKey, box []byte) {

func computeValueToProvePubKey(keyToProve p2pcrypto.PubKey, receiverSigKey *[32]byte) ([]byte, error) {
if keyToProve == nil || receiverSigKey == nil {
return nil, ErrParams
return nil, errcode.ErrHandshakeParams
}

keyToProveBytes, err := keyToProve.Raw()
Expand All @@ -66,7 +65,7 @@ func computeValueToProvePubKey(keyToProve p2pcrypto.PubKey, receiverSigKey *[32]

func computeValueToProveDevicePubKeyAndSigChain(keyToProve *[32]byte, chain crypto.SigChainManager) ([]byte, error) {
if keyToProve == nil || chain == nil {
return nil, ErrParams
return nil, errcode.ErrHandshakeParams
}

sigChainBytes, err := chain.Marshal()
Expand All @@ -82,7 +81,7 @@ func computeValueToProveDevicePubKeyAndSigChain(keyToProve *[32]byte, chain cryp
func (h *handshakeSession) ProveOtherKey() ([]byte, error) {
// Step 3a (out) : sig_a1(B·b1)
if h.accountKeyToProve == nil {
return nil, ErrSessionInvalid
return nil, errcode.ErrHandshakeSessionInvalid
}

signedValue, err := computeValueToProvePubKey(h.accountKeyToProve, h.otherBoxPublicKey)
Expand Down Expand Up @@ -122,7 +121,7 @@ func (h *handshakeSession) CheckOwnKeyProof(sig []byte) error {
}

if !ok {
return ErrInvalidSignature
return errcode.ErrHandshakeInvalidSignature
}

return nil
Expand Down Expand Up @@ -157,7 +156,7 @@ func (h *handshakeSession) CheckOtherKeyProof(sig []byte, chain crypto.SigChainM
}

if !ok {
return ErrInvalidSignature
return errcode.ErrHandshakeInvalidSignature
}

entries := chain.ListCurrentPubKeys()
Expand All @@ -167,7 +166,7 @@ func (h *handshakeSession) CheckOtherKeyProof(sig []byte, chain crypto.SigChainM
}
}

return ErrKeyNotInSigChain
return errcode.ErrHandshakeKeyNotInSigChain
}

func (h *handshakeSession) ProveOtherKnownAccount() ([]byte, error) {
Expand Down Expand Up @@ -198,15 +197,15 @@ func (h *handshakeSession) CheckOwnKnownAccountProof(attemptedDeviceKey p2pcrypt
}

if !ok {
return ErrInvalidSignature
return errcode.ErrHandshakeInvalidSignature
}

return nil
}

func (h *handshakeSession) Encrypt(data []byte) ([]byte, error) {
if h.otherBoxPublicKey == nil || h.selfBoxPrivateKey == nil {
return nil, ErrSessionInvalid
return nil, errcode.ErrHandshakeSessionInvalid
}

nonce := h.getNonce()
Expand All @@ -220,14 +219,14 @@ func (h *handshakeSession) Encrypt(data []byte) ([]byte, error) {

func (h *handshakeSession) Decrypt(data []byte) ([]byte, error) {
if h.otherBoxPublicKey == nil || h.selfBoxPrivateKey == nil {
return nil, ErrSessionInvalid
return nil, errcode.ErrHandshakeSessionInvalid
}

nonce := h.getNonce()

out, ok := box.Open(nil, data, &nonce, h.otherBoxPublicKey, h.selfBoxPrivateKey)
if !ok {
return nil, ErrDecrypt
return nil, errcode.ErrHandshakeDecrypt
}

h.incrementNonce()
Expand Down
9 changes: 5 additions & 4 deletions go/internal/handshake/crypto_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"testing"

"berty.tech/go/internal/crypto"
"berty.tech/go/pkg/errcode"
p2pcrypto "github.com/libp2p/go-libp2p-core/crypto"
)

Expand Down Expand Up @@ -252,14 +253,14 @@ func TestHandshakeSession_Encrypt_Decrypt(t *testing.T) {

// Should not decode the message twice
decrypted, err = hss2.Decrypt(encrypted)
if err != ErrDecrypt || string(decrypted) != "" {
t.Fatalf("err should be ErrDecrypt and decrypted should be empty")
if err != errcode.ErrHandshakeDecrypt || string(decrypted) != "" {
t.Fatalf("err should be errcode.ErrHandshakeDecrypt and decrypted should be empty")
}

// Should not decode a random string
decrypted, err = hss2.Decrypt([]byte("blahblah"))
if err != ErrDecrypt || string(decrypted) != "" {
t.Fatalf("err should be ErrDecrypt and decrypted should be empty")
if err != errcode.ErrHandshakeDecrypt || string(decrypted) != "" {
t.Fatalf("err should be errcode.ErrHandshakeDecrypt and decrypted should be empty")
}

// Should be able to encode a second message
Expand Down
19 changes: 0 additions & 19 deletions go/internal/handshake/errors.go

This file was deleted.

16 changes: 7 additions & 9 deletions go/internal/handshake/net_flow.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"net"

"berty.tech/go/internal/crypto"

"berty.tech/go/pkg/errcode"
ggio "github.com/gogo/protobuf/io"
p2pcrypto "github.com/libp2p/go-libp2p-core/crypto"
inet "github.com/libp2p/go-libp2p-core/network"
Expand All @@ -29,7 +29,7 @@ type flow struct {

func newHandshakeFlow(ctx context.Context, conn net.Conn, devPubKey p2pcrypto.PubKey, ownSigChain crypto.SigChainManager, session *handshakeSession, steps map[HandshakeFrame_HandshakeStep]flowStep) (crypto.SigChainManager, p2pcrypto.PubKey, error) {
if conn == nil || session == nil || steps == nil {
return nil, nil, ErrParams
return nil, nil, errcode.ErrHandshakeParams
}

writer := ggio.NewDelimitedWriter(conn)
Expand All @@ -48,8 +48,6 @@ func newHandshakeFlow(ctx context.Context, conn net.Conn, devPubKey p2pcrypto.Pu
}

func (f *flow) close() error {
var retErr error

if f.writer != nil {
_ = f.writer.Close()
}
Expand All @@ -62,7 +60,7 @@ func (f *flow) close() error {
_ = f.session.Close()
}

return retErr
return nil
}

func (f *flow) performFlow(ctx context.Context) (crypto.SigChainManager, p2pcrypto.PubKey, error) {
Expand All @@ -75,7 +73,7 @@ func (f *flow) performFlow(ctx context.Context) (crypto.SigChainManager, p2pcryp
for nextStep != nil {
if *nextStep == HandshakeFrame_STEP_9_DONE {
if f.provedSigChain == nil || f.provedDevicePubKey == nil {
return nil, nil, ErrNoAuthReturned
return nil, nil, errcode.ErrHandshakeNoAuthReturned
}

return f.provedSigChain, f.provedDevicePubKey, nil
Expand All @@ -85,7 +83,7 @@ func (f *flow) performFlow(ctx context.Context) (crypto.SigChainManager, p2pcryp

step, ok := f.steps[*nextStep]
if !ok {
return nil, nil, ErrInvalidFlowStepNotFound
return nil, nil, errcode.ErrHandshakeInvalidFlowStepNotFound
}

var readMsg = &HandshakeFrame{}
Expand All @@ -103,11 +101,11 @@ func (f *flow) performFlow(ctx context.Context) (crypto.SigChainManager, p2pcryp
}

if *nextStep == currentStep {
return nil, nil, ErrInvalidFlow
return nil, nil, errcode.ErrHandshakeInvalidFlow
}
}

return nil, nil, ErrInvalidFlow
return nil, nil, errcode.ErrHandshakeInvalidFlow
}

func Request(ctx context.Context, conn net.Conn, devicePrivateKey p2pcrypto.PrivKey, sigChain crypto.SigChainManager, accountToReach p2pcrypto.PubKey, opts *crypto.Opts) (crypto.SigChainManager, p2pcrypto.PubKey, error) {
Expand Down
Loading

0 comments on commit df86844

Please sign in to comment.