starter.go 9.02 KB
Newer Older
1
2
3
4
5
6
package sign

import (
	"errors"
	"fmt"
	"strconv"
7
	"sync"
8
	"time"
9

Axel's avatar
Axel committed
10
11
	"dfss"
	cAPI "dfss/dfssc/api"
12
	"dfss/dfssc/common"
13
	"dfss/dfssc/security"
14
	dAPI "dfss/dfssd/api"
Axel's avatar
Axel committed
15
	pAPI "dfss/dfssp/api"
16
	"dfss/dfssp/contract"
Axel's avatar
Axel committed
17
18
	tAPI "dfss/dfsst/api"
	"dfss/dfsst/entities"
19
	"dfss/net"
20
	"github.com/spf13/viper"
21
	"golang.org/x/net/context"
Axel's avatar
Axel committed
22
	"google.golang.org/grpc"
23
24
)

25
26
27
// Limit the buffer size of the channels
const chanBufferSize = 100

28
29
// SignatureManager handles the signature of a contract.
type SignatureManager struct {
Axel's avatar
Axel committed
30
31
32
33
34
	auth           *security.AuthContainer
	contract       *contract.JSON // contains the contractUUID, the list of the signers' hashes, the hash of the contract
	platform       pAPI.PlatformClient
	platformConn   *grpc.ClientConn
	ttp            tAPI.TTPClient
35
	ttpData        *pAPI.LaunchSignature_TTP
Axel's avatar
Axel committed
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
	peersConn      map[string]*grpc.ClientConn
	peers          map[string]*cAPI.ClientClient
	hashToID       map[string]uint32
	nbReady        int
	cServer        *grpc.Server
	cServerIface   clientServer
	sequence       []uint32
	lastValidIndex int // the last index at which we sent a promise
	currentIndex   int
	myID           uint32
	uuid           string
	keyHash        [][]byte
	mail           string
	archives       *Archives
	seal           []byte
	cancelled      bool
	finished       bool
Loïck Bonniot's avatar
Loïck Bonniot committed
53
54
55
56

	// Callbacks
	OnSignerStatusUpdate func(mail string, status SignerStatus, data string)
	OnProgressUpdate     func(current int, end int)
57
	Cancel               chan interface{}
58
59
}

60
// Archives stores the received and sent messages, as evidence if needed
61
type Archives struct {
Axel's avatar
Axel committed
62
	receivedPromises   []*cAPI.Promise // TODO: improve by using a map
63
	sentSignatures     []*cAPI.Signature
64
	receivedSignatures []*cAPI.Signature
65
	mutex              sync.Mutex
66
67
68
}

// NewSignatureManager populates a SignatureManager and connects to the platform.
ElyKar's avatar
ElyKar committed
69
func NewSignatureManager(passphrase string, c *contract.JSON) (*SignatureManager, error) {
70
	m := &SignatureManager{
ElyKar's avatar
ElyKar committed
71
72
		auth:     security.NewAuthContainer(passphrase),
		contract: c,
73
		archives: &Archives{
74
			receivedPromises:   make([]*cAPI.Promise, 0),
75
			sentSignatures:     make([]*cAPI.Signature, 0),
76
			receivedSignatures: make([]*cAPI.Signature, 0),
77
		},
78
		Cancel: make(chan interface{}),
79
	}
Axel's avatar
Axel committed
80
	var err error
81
	_, _, _, err = m.auth.LoadFiles()
82
83
84
85
	if err != nil {
		return nil, err
	}

86
	m.mail = m.auth.Cert.Subject.CommonName
87
	dAPI.SetIdentifier(m.mail)
Axel's avatar
Axel committed
88
	m.cServer = m.GetServer()
89
	go func() { _ = net.Listen("0.0.0.0:"+strconv.Itoa(viper.GetInt("local_port")), m.cServer) }()
Axel's avatar
Axel committed
90

Axel's avatar
Axel committed
91
	connp, err := net.Connect(viper.GetString("platform_addrport"), m.auth.Cert, m.auth.Key, m.auth.CA, nil)
92
93
94
95
	if err != nil {
		return nil, err
	}

Axel's avatar
Axel committed
96
97
98
	m.platform = pAPI.NewPlatformClient(connp)
	m.platformConn = connp

99
	m.peersConn = make(map[string]*grpc.ClientConn)
Axel's avatar
Axel committed
100
	m.peers = make(map[string]*cAPI.ClientClient)
101
	for _, u := range c.Signers {
102
		if u.Email != m.auth.Cert.Subject.CommonName {
103
104
			m.peers[u.Email] = nil
		}
105
106
	}

Axel's avatar
Axel committed
107
108
109
110
	// Initialize TTP AuthContainer
	// This is needed to use platform seal verification client-side
	entities.AuthContainer = m.auth

111
112
113
114
115
	return m, nil
}

// ConnectToPeers tries to fetch the list of users for this contract, and tries to establish a connection to each peer.
func (m *SignatureManager) ConnectToPeers() error {
116
117
118
119
120
	localIps, err := net.ExternalInterfaceAddr()
	if err != nil {
		return err
	}

Axel's avatar
Axel committed
121
	stream, err := m.platform.JoinSignature(context.Background(), &pAPI.JoinSignatureRequest{
122
		ContractUuid: m.contract.UUID,
ElyKar's avatar
ElyKar committed
123
		Port:         uint32(viper.GetInt("local_port")),
124
		Ip:           localIps,
125
126
	})
	if err != nil {
127
128
		m.finished = true
		m.closeConnections()
129
130
131
		return err
	}

132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
	c := make(chan error)
	go connectToPeersLoop(m, stream, c)

	select {
	case err = <-c:
		if err != nil {
			m.finished = true
			m.closeConnections()
		}
		return err
	case <-m.Cancel:
		m.cancelled = true
		m.closeConnections()
		return errors.New("Signature cancelled")
	}
}

func connectToPeersLoop(m *SignatureManager, stream pAPI.Platform_JoinSignatureClient, c chan error) {
	for !m.cancelled {
151
152
		userConnected, err := stream.Recv()
		if err != nil {
153
154
			c <- err
			return
155
156
		}
		errorCode := userConnected.GetErrorCode()
Axel's avatar
Axel committed
157
		if errorCode.Code != pAPI.ErrorCode_SUCCESS {
158
159
			c <- errors.New(errorCode.Message)
			return
160
161
162
		}
		ready, err := m.addPeer(userConnected.User)
		if err != nil {
163
			continue // Unable to connect to this user, ignore it for the moment
164
165
		}
		if ready {
166
167
			c <- nil
			return
168
169
170
171
172
		}
	}
}

// addPeer stores a peer from the platform and tries to establish a connection to this peer.
Axel's avatar
Axel committed
173
func (m *SignatureManager) addPeer(user *pAPI.User) (ready bool, err error) {
174
175
176
177
	if user == nil {
		err = errors.New("unexpected user format")
		return
	}
178
179
180
	if _, ok := m.peers[user.Email]; !ok {
		return // Ignore if unknown
	}
181

182
183
184
185
186
187
188
189
190
191
	var conn *grpc.ClientConn
	for _, ip := range user.Ip {
		addrPort := ip + ":" + strconv.Itoa(int(user.Port))
		m.OnSignerStatusUpdate(user.Email, StatusConnecting, addrPort)

		// This is an certificate authentificated TLS connection
		conn, err = net.Connect(addrPort, m.auth.Cert, m.auth.Key, m.auth.CA, user.KeyHash)
		if err == nil {
			break
		}
192
193
194
195
196

		if m.cancelled {
			err = errors.New("Signature cancelled")
			break
		}
197
	}
198

Axel's avatar
Axel committed
199
	if err != nil {
Loïck Bonniot's avatar
Loïck Bonniot committed
200
		m.OnSignerStatusUpdate(user.Email, StatusError, err.Error())
Axel's avatar
Axel committed
201
202
203
204
205
		return false, err
	}

	// Sending Hello message
	client := cAPI.NewClientClient(conn)
206
	lastConnection := m.peers[user.Email]
Axel's avatar
Axel committed
207
	m.peers[user.Email] = &client
208
209
210
	// The connection is encapsulated into the interface, so we
	// need to create another way to access it
	m.peersConn[user.Email] = conn
211

212
	ctx, cancel := context.WithTimeout(context.Background(), net.DefaultTimeout)
213
214
	defer cancel()
	msg, err := client.Discover(ctx, &cAPI.Hello{Version: dfss.Version})
Axel's avatar
Axel committed
215
	if err != nil {
Loïck Bonniot's avatar
Loïck Bonniot committed
216
		m.OnSignerStatusUpdate(user.Email, StatusError, err.Error())
Axel's avatar
Axel committed
217
218
		return false, err
	}
219

Axel's avatar
Axel committed
220
	// Printing answer: application version
221
	// TODO check certificate
Loïck Bonniot's avatar
Loïck Bonniot committed
222
	m.OnSignerStatusUpdate(user.Email, StatusConnected, msg.Version)
223
224

	// Check if we have any other peer to connect to
225
226
227
228
	if lastConnection == nil {
		m.nbReady++
		if m.nbReady == len(m.contract.Signers)-1 {
			return true, nil
229
230
231
		}
	}

232
	return false, nil
233
}
Loïck Bonniot's avatar
Loïck Bonniot committed
234
235
236

// SendReadySign sends the READY signal to the platform, and wait (potentially a long time) for START signal.
func (m *SignatureManager) SendReadySign() (signatureUUID string, err error) {
237
	ctx, cancel := context.WithTimeout(context.Background(), 10*time.Minute)
Loïck Bonniot's avatar
Loïck Bonniot committed
238
	defer cancel()
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254

	c := make(chan *pAPI.LaunchSignature)
	go func() {
		launch, _ := m.platform.ReadySign(ctx, &pAPI.ReadySignRequest{
			ContractUuid: m.contract.UUID,
		})
		c <- launch
	}()

	var launch *pAPI.LaunchSignature
	select {
	case launch = <-c: // OK
	case <-m.Cancel:
		m.cancelled = true
		m.closeConnections()
		err = errors.New("Signature cancelled")
Loïck Bonniot's avatar
Loïck Bonniot committed
255
256
257
258
		return
	}

	errorCode := launch.GetErrorCode()
Loïck Bonniot's avatar
Loïck Bonniot committed
259
	if errorCode.Code != pAPI.ErrorCode_SUCCESS {
Loïck Bonniot's avatar
Loïck Bonniot committed
260
		err = errors.New(errorCode.Code.String() + " " + errorCode.Message)
261
262
		m.finished = true
		m.closeConnections()
Loïck Bonniot's avatar
Loïck Bonniot committed
263
264
265
		return
	}

266
267
268
	// Check signers from platform data
	if len(m.contract.Signers) != len(launch.KeyHash) {
		err = errors.New("Corrupted DFSS file: bad number of signers, unable to sign safely")
269
270
		m.finished = true
		m.closeConnections()
271
272
273
274
275
276
		return
	}

	for i, s := range m.contract.Signers {
		if s.Hash != fmt.Sprintf("%x", launch.KeyHash[i]) {
			err = errors.New("Corrupted DFSS file: signer " + s.Email + " has an invalid hash, unable to sign safely")
277
278
			m.finished = true
			m.closeConnections()
279
280
281
282
			return
		}
	}

283
284
285
	// Connect to TTP, if any
	err = m.connectToTTP(launch.Ttp)

286
287
	m.sequence = launch.Sequence
	m.uuid = launch.SignatureUuid
288
	m.keyHash = launch.KeyHash
289
	m.seal = launch.Seal
290
	signatureUUID = m.uuid
Loïck Bonniot's avatar
Loïck Bonniot committed
291
292
	return
}
293

294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
// connectToTTP : tries to open a connection with the ttp specified in the contract.
func (m *SignatureManager) connectToTTP(ttp *pAPI.LaunchSignature_TTP) error {
	if ttp == nil {
		m.ttpData = &pAPI.LaunchSignature_TTP{
			Addrport: "",
			Hash:     []byte{},
		}
		return nil
	}

	// TODO check that the connection spots missing TTP and returns an error quickly enough
	conn, err := net.Connect(ttp.Addrport, m.auth.Cert, m.auth.Key, m.auth.CA, ttp.Hash)
	if err != nil {
		return err
	}

	m.ttpData = ttp
	m.ttp = tAPI.NewTTPClient(conn)
	return nil
}

315
// Initialize computes the values needed for the start of the signing
Axel's avatar
Axel committed
316
func (m *SignatureManager) Initialize() (int, error) {
317
318
	myID, err := m.FindID()
	if err != nil {
Axel's avatar
Axel committed
319
		return 0, err
320
	}
Axel's avatar
Axel committed
321
	m.myID = myID
322

323
	m.currentIndex, err = common.FindNextIndex(m.sequence, myID, -1)
324
	if err != nil {
Axel's avatar
Axel committed
325
		return 0, err
326
327
	}

328
	nextIndex, err := common.FindNextIndex(m.sequence, myID, m.currentIndex)
329
	if err != nil {
Axel's avatar
Axel committed
330
		return 0, err
331
332
	}

Axel's avatar
Axel committed
333
334
335
	m.lastValidIndex = 0

	return nextIndex, nil
336
337
338
339
}

// FindID finds the sequence id for the user's email and the contract to sign
func (m *SignatureManager) FindID() (uint32, error) {
340
341
	signers := m.contract.Signers
	for id, signer := range signers {
342
		if signer.Email == m.mail {
343
344
345
346
347
			return uint32(id), nil
		}
	}
	return 0, errors.New("Mail couldn't be found amongst signers")
}
348
349
350
351
352

// IsTerminated returns true if the signature is cancelled or finished
func (m *SignatureManager) IsTerminated() bool {
	return m.cancelled || m.finished
}