Skip to content

Commit

Permalink
upgrading dtcnode to 3.0.3 and fixing reconnect bug
Browse files Browse the repository at this point in the history
  • Loading branch information
Eduardo committed Dec 8, 2020
1 parent 7f1d6c2 commit 9ceb030
Show file tree
Hide file tree
Showing 7 changed files with 230 additions and 65 deletions.
6 changes: 3 additions & 3 deletions context_rsa.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,11 @@ import (
"crypto/rsa"
"encoding/binary"
"fmt"
"github.com/niclabs/dtcnode/v3/message"
"github.com/niclabs/tcrsa"
"io"
"reflect"

"github.com/niclabs/dtcnode/v3/message"
"github.com/niclabs/tcrsa"
)

type SignContextRSA struct {
Expand Down Expand Up @@ -184,7 +185,6 @@ func createRSAPublicKey(keyID string, pkAttrs Attributes, keyMeta *tcrsa.KeyMeta
&Attribute{C.CKA_END_DATE, make([]byte, 8)},
&Attribute{C.CKA_MODULUS_BITS, nil},
&Attribute{C.CKA_PUBLIC_EXPONENT, eBytes},

)

pkAttrs.Set(
Expand Down
15 changes: 7 additions & 8 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,12 @@ module github.com/niclabs/dtc/v3
go 1.12

require (
github.com/google/uuid v1.1.1
github.com/mattn/go-sqlite3 v1.10.0
github.com/miekg/pkcs11 v1.0.2
github.com/niclabs/dtcconfig v1.0.8 // indirect
github.com/niclabs/dtcnode/v3 v3.0.2
github.com/google/uuid v1.1.2
github.com/mattn/go-sqlite3 v1.14.5
github.com/miekg/pkcs11 v1.0.3
github.com/niclabs/dtcnode/v3 v3.0.3
github.com/niclabs/tcecdsa v0.0.7
github.com/niclabs/tcrsa v0.0.4
github.com/pebbe/zmq4 v1.0.0
github.com/spf13/viper v1.4.0
github.com/niclabs/tcrsa v0.0.5
github.com/pebbe/zmq4 v1.2.2
github.com/spf13/viper v1.7.1
)
233 changes: 192 additions & 41 deletions go.sum

Large diffs are not rendered by default.

3 changes: 2 additions & 1 deletion network/zmq/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ func (client *Client) ackOnly(msg *message.Message) error {
// It returns an error from the preliminary check or an error from fn.
func (client *Client) doMessage(fn func(msg *message.Message) error) func(msg *message.Message) error {
return func(msg *message.Message) error {
log.Printf("message received from node %s\n", msg.From)
log.Printf("message %s received from node %s\n", msg.ID, msg.From)
if pending, exists := client.pendingMessages[msg.ID]; exists {
delete(client.pendingMessages, msg.ID)
if err := msg.ResponseOK(pending); err != nil {
Expand All @@ -160,6 +160,7 @@ func (client *Client) doMessage(fn func(msg *message.Message) error) func(msg *m
}
} else {
log.Printf("unexpected message, ignoring (id: %s, type: %s, from: %s, responseOf: %s)", msg.ID, msg.Type.String(), msg.From, msg.ResponseOf)
return fmt.Errorf("unexpected message")
}
return nil
}
Expand Down
14 changes: 7 additions & 7 deletions network/zmq/ecdsa_node.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ func (node *Node) sendECDSAKeyShare(id string, key *tcecdsa.KeyShare, meta *tcec
if err != nil {
return nil, err
}
_, err = node.socket.SendMessage(msg.GetBytesLists()...)
_, err = node.sendMessage(msg.GetBytesLists()...)
if err != nil {
return nil, err
}
Expand All @@ -35,7 +35,7 @@ func (node *Node) ecdsaInitKeys(id string, initKeyMessages tcecdsa.KeyInitMessag
if err != nil {
return nil, err
}
if _, err := node.socket.SendMessage(msg.GetBytesLists()...); err != nil {
if _, err := node.sendMessage(msg.GetBytesLists()...); err != nil {
return nil, err
}
return msg, nil
Expand All @@ -46,7 +46,7 @@ func (node *Node) ecdsaRound1(id string, doc []byte) (msg *message.Message, err
if err != nil {
return nil, err
}
if _, err := node.socket.SendMessage(msg.GetBytesLists()...); err != nil {
if _, err := node.sendMessage(msg.GetBytesLists()...); err != nil {
return nil, err
}
return msg, nil
Expand All @@ -61,7 +61,7 @@ func (node *Node) ecdsaRound2(messages tcecdsa.Round1MessageList) (msg *message.
if err != nil {
return nil, err
}
if _, err := node.socket.SendMessage(msg.GetBytesLists()...); err != nil {
if _, err := node.sendMessage(msg.GetBytesLists()...); err != nil {
return nil, err
}
return msg, nil
Expand All @@ -76,7 +76,7 @@ func (node *Node) ecdsaRound3(messages tcecdsa.Round2MessageList) (msg *message.
if err != nil {
return nil, err
}
if _, err := node.socket.SendMessage(msg.GetBytesLists()...); err != nil {
if _, err := node.sendMessage(msg.GetBytesLists()...); err != nil {
return nil, err
}
return msg, nil
Expand All @@ -91,7 +91,7 @@ func (node *Node) ecdsaGetSignature(messages tcecdsa.Round3MessageList) (msg *me
if err != nil {
return nil, err
}
if _, err := node.socket.SendMessage(msg.GetBytesLists()...); err != nil {
if _, err := node.sendMessage(msg.GetBytesLists()...); err != nil {
return nil, err
}
return msg, nil
Expand All @@ -102,7 +102,7 @@ func (node *Node) deleteECDSAKeyShare(id string) (*message.Message, error) {
if err != nil {
return nil, err
}
_, err = node.socket.SendMessage(msg.GetBytesLists()...)
_, err = node.sendMessage(msg.GetBytesLists()...)
if err != nil {
return nil, err
}
Expand Down
18 changes: 16 additions & 2 deletions network/zmq/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@ package zmq

import (
"fmt"
"log"
"net"

"github.com/niclabs/dtc/v3/config"
"github.com/niclabs/dtcnode/v3/message"
"github.com/pebbe/zmq4"
"log"
"net"
)

// NodeState represents a code for the state of the node
type NodeState int

// Node represents a remote machine connection. It has all the data required to connect to a node, and a pointer to use the respective Client struct.
Expand Down Expand Up @@ -122,3 +124,15 @@ func (node *Node) listen() {
func (node *Node) stopReceiving() {
node.quit <- struct{}{}
}

func (node *Node) sendMessage(parts ...interface{}) (int, error) {
i, err := node.socket.SendMessage(parts...)
if n := zmq4.AsErrno(err); n == zmq4.EFSM {
err = node.connect() // Reconnecting (FSM was wating a reply that never came)
if err != nil { // Error reconnecting
return -1, err
}
return node.socket.SendMessage(parts...) // if fails, nothing to do
}
return i, err
}
6 changes: 3 additions & 3 deletions network/zmq/rsa_node.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ func (node *Node) sendRSAKeyShare(id string, key *tcrsa.KeyShare, meta *tcrsa.Ke
if err != nil {
return nil, err
}
_, err = node.socket.SendMessage(msg.GetBytesLists()...)
_, err = node.sendMessage(msg.GetBytesLists()...)
if err != nil {
return nil, err
}
Expand All @@ -31,7 +31,7 @@ func (node *Node) getRSASigShare(id string, doc []byte) (msg *message.Message, e
if err != nil {
return nil, err
}
if _, err := node.socket.SendMessage(msg.GetBytesLists()...); err != nil {
if _, err := node.sendMessage(msg.GetBytesLists()...); err != nil {
return nil, err
}
return msg, nil
Expand All @@ -42,7 +42,7 @@ func (node *Node) deleteRSAKeyShare(id string) (*message.Message, error) {
if err != nil {
return nil, err
}
_, err = node.socket.SendMessage(msg.GetBytesLists()...)
_, err = node.sendMessage(msg.GetBytesLists()...)
if err != nil {
return nil, err
}
Expand Down

0 comments on commit 9ceb030

Please sign in to comment.