Commit bd1ddace authored by Loïck Bonniot's avatar Loïck Bonniot

[p][c] Tiny improvements for signature starter

- Use uint32 instead of int for sequence generation
- Send sequence in READY message
- Check that DFSS file has not been compromised
- Remove sequence from DFSS file
- Use more explicit output for client during signature
parent eb6b96df
package sign
import (
"fmt"
"dfss"
cAPI "dfss/dfssc/api"
pAPI "dfss/dfssp/api"
"dfss/net"
"golang.org/x/net/context"
"google.golang.org/grpc"
)
......@@ -34,7 +31,6 @@ func (s *clientServer) TreatSignature(ctx context.Context, in *cAPI.Signature) (
//
// Handle incoming Discover messages
func (s *clientServer) Discover(ctx context.Context, in *cAPI.Hello) (*cAPI.Hello, error) {
fmt.Println("Recieved:", in.Version)
return &cAPI.Hello{Version: dfss.Version}, nil
}
......
......@@ -5,6 +5,7 @@ import (
"crypto/x509"
"errors"
"fmt"
"log"
"strconv"
"time"
......@@ -28,6 +29,8 @@ type SignatureManager struct {
cServer *grpc.Server
cert, ca *x509.Certificate
key *rsa.PrivateKey
sequence []uint32
uuid string
}
// NewSignatureManager populates a SignatureManager and connects to the platform.
......@@ -44,7 +47,7 @@ func NewSignatureManager(fileCA, fileCert, fileKey, addrPort, passphrase string,
}
m.cServer = m.GetServer()
go func() { _ = net.Listen("0.0.0.0:"+strconv.Itoa(port), m.cServer) }()
go func() { log.Fatalln(net.Listen("0.0.0.0:"+strconv.Itoa(port), m.cServer)) }()
conn, err := net.Connect(m.auth.AddrPort, m.cert, m.key, m.ca)
if err != nil {
......@@ -55,7 +58,9 @@ func NewSignatureManager(fileCA, fileCert, fileKey, addrPort, passphrase string,
m.peers = make(map[string]*cAPI.ClientClient)
for _, u := range c.Signers {
m.peers[u.Email] = nil
if u.Email != m.cert.Subject.CommonName {
m.peers[u.Email] = nil
}
}
return m, nil
......@@ -82,7 +87,7 @@ func (m *SignatureManager) ConnectToPeers() error {
}
ready, err := m.addPeer(userConnected.User)
if err != nil {
return err
continue // Unable to connect to this user, ignore it for the moment
}
if ready {
break
......@@ -98,9 +103,12 @@ func (m *SignatureManager) addPeer(user *pAPI.User) (ready bool, err error) {
err = errors.New("unexpected user format")
return
}
if _, ok := m.peers[user.Email]; !ok {
return // Ignore if unknown
}
addrPort := user.Ip + ":" + strconv.Itoa(int(user.Port))
fmt.Println("Trying to connect with", user.Email, "/", addrPort)
fmt.Println("- Trying to connect with", user.Email, "/", addrPort)
conn, err := net.Connect(addrPort, m.cert, m.key, m.ca)
if err != nil {
......@@ -110,12 +118,17 @@ func (m *SignatureManager) addPeer(user *pAPI.User) (ready bool, err error) {
// Sending Hello message
client := cAPI.NewClientClient(conn)
m.peers[user.Email] = &client
msg, err := client.Discover(context.Background(), &cAPI.Hello{Version: dfss.Version})
ctx, cancel := context.WithTimeout(context.Background(), time.Minute)
defer cancel()
msg, err := client.Discover(ctx, &cAPI.Hello{Version: dfss.Version})
if err != nil {
return false, err
}
// Printing answer: application version
fmt.Println("Recieved:", msg.Version)
// TODO check certificate
fmt.Println(" Successfully connected!", "[", msg.Version, "]")
// Check if we have any other peer to connect to
for _, u := range m.peers {
......@@ -129,7 +142,7 @@ func (m *SignatureManager) addPeer(user *pAPI.User) (ready bool, err error) {
// 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) {
ctx, cancel := context.WithTimeout(context.Background(), 10 * time.Minute)
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Minute)
defer cancel()
launch, err := m.platform.ReadySign(ctx, &pAPI.ReadySignRequest{
ContractUuid: m.contract.UUID,
......@@ -144,6 +157,21 @@ func (m *SignatureManager) SendReadySign() (signatureUUID string, err error) {
return
}
signatureUUID = launch.SignatureUuid
// 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")
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")
return
}
}
m.sequence = launch.Sequence
m.uuid = launch.SignatureUuid
signatureUUID = m.uuid
return
}
......@@ -211,6 +211,7 @@ type LaunchSignature struct {
ErrorCode *ErrorCode `protobuf:"bytes,1,opt,name=errorCode" json:"errorCode,omitempty"`
SignatureUuid string `protobuf:"bytes,2,opt,name=signatureUuid" json:"signatureUuid,omitempty"`
KeyHash [][]byte `protobuf:"bytes,3,rep,name=keyHash,proto3" json:"keyHash,omitempty"`
Sequence []uint32 `protobuf:"varint,4,rep,name=sequence" json:"sequence,omitempty"`
}
func (m *LaunchSignature) Reset() { *m = LaunchSignature{} }
......@@ -471,44 +472,45 @@ var _Platform_serviceDesc = grpc.ServiceDesc{
}
var fileDescriptor0 = []byte{
// 621 bytes of a gzipped FileDescriptorProto
0x1f, 0x8b, 0x08, 0x00, 0x00, 0x09, 0x6e, 0x88, 0x02, 0xff, 0x94, 0x54, 0x5d, 0x6f, 0xd3, 0x30,
0x14, 0x6d, 0xda, 0xec, 0xa3, 0x77, 0x6b, 0x17, 0xb9, 0x03, 0x95, 0x4a, 0x43, 0x93, 0x85, 0x04,
0x42, 0xa8, 0x9d, 0x8a, 0x84, 0x04, 0x6f, 0x59, 0xa9, 0xb6, 0x21, 0x54, 0x4d, 0xee, 0x0a, 0x12,
0x6f, 0x21, 0xf1, 0xd6, 0x68, 0xcd, 0x07, 0xb6, 0x23, 0xb4, 0x27, 0xf8, 0x25, 0xfc, 0x2d, 0x7e,
0x0e, 0xd8, 0x4e, 0x9c, 0x26, 0xa3, 0x42, 0xda, 0x1e, 0x32, 0xdf, 0xeb, 0xeb, 0x73, 0x7c, 0xce,
0xbd, 0x2e, 0x1c, 0x05, 0xd7, 0x9c, 0x8f, 0xd4, 0x27, 0x1d, 0x79, 0x69, 0x38, 0x4a, 0x57, 0x9e,
0xb8, 0x4e, 0x58, 0x34, 0x4c, 0x59, 0x22, 0x12, 0xd4, 0x92, 0x39, 0xec, 0xc2, 0x01, 0xa1, 0x37,
0x21, 0x17, 0x94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0, 0xe1, 0xe6, 0x68, 0xc5, 0x98, 0x7a, 0xb2, 0x2a, 0x59, 0x0d, 0xc0, 0x5a,
0xf2, 0xf8, 0x77, 0x13, 0x76, 0x2f, 0x8b, 0x27, 0x8d, 0xc6, 0xb0, 0x6b, 0x06, 0x15, 0x1d, 0x6a,
0xce, 0x7b, 0xcf, 0x7a, 0x70, 0xef, 0x26, 0xb8, 0x81, 0x46, 0x60, 0xab, 0x77, 0x81, 0x1c, 0xbd,
0x53, 0x79, 0x22, 0x83, 0x5e, 0x0d, 0x21, 0x9f, 0x7c, 0x79, 0xe0, 0x25, 0xc0, 0x22, 0x66, 0x86,
0x06, 0x72, 0x40, 0x35, 0xec, 0x1b, 0xc0, 0xdf, 0xc1, 0x7e, 0x75, 0xfc, 0x51, 0x5f, 0x57, 0x6c,
0x78, 0x11, 0x1b, 0xce, 0x9e, 0x42, 0xa7, 0x36, 0xc1, 0xe8, 0x89, 0x2e, 0xd9, 0x34, 0xd5, 0x03,
0x54, 0xce, 0x4a, 0x39, 0x9f, 0xb8, 0x71, 0x62, 0x49, 0xfe, 0x76, 0xd9, 0x56, 0xf4, 0xa8, 0xd0,
0x53, 0x6f, 0xf3, 0x20, 0x37, 0xea, 0x5e, 0x17, 0x71, 0xe3, 0xeb, 0xb6, 0xfe, 0x81, 0x7c, 0xfd,
0x37, 0x00, 0x00, 0xff, 0xff, 0xe1, 0xe6, 0x41, 0xec, 0x41, 0x05, 0x00, 0x00,
// 636 bytes of a gzipped FileDescriptorProto
0x1f, 0x8b, 0x08, 0x00, 0x00, 0x09, 0x6e, 0x88, 0x02, 0xff, 0x94, 0x54, 0x6d, 0x6b, 0x13, 0x41,
0x10, 0xce, 0x25, 0xd7, 0x97, 0x4c, 0x9b, 0xf4, 0xd8, 0x54, 0x89, 0x81, 0x4a, 0x59, 0x04, 0x45,
0x24, 0x29, 0x11, 0x04, 0xfd, 0x76, 0x8d, 0xa1, 0xad, 0x48, 0x28, 0x9b, 0x46, 0xc1, 0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25, 0xf3, 0xb7, 0xcb, 0x9e, 0xa3, 0x47, 0x05, 0x9f, 0xfa,
0x0c, 0x0c, 0x72, 0xa1, 0xd6, 0x3a, 0x8c, 0x1b, 0x5f, 0xb7, 0xf5, 0xaf, 0xe7, 0xeb, 0xbf, 0x01,
0x00, 0x00, 0xff, 0xff, 0x3f, 0xa2, 0x07, 0x22, 0x5e, 0x05, 0x00, 0x00,
}
......@@ -94,4 +94,5 @@ message LaunchSignature {
ErrorCode errorCode = 1;
string signatureUuid = 2;
repeated bytes keyHash = 3;
repeated uint32 sequence = 4;
}
......@@ -29,13 +29,12 @@ type TTPJSON struct {
// JSON is the structure used to store contract information in JSON format
type JSON struct {
UUID string
Date *time.Time
Comment string
File *FileJSON
Signers []SignerJSON
Sequence []uint
TTP *TTPJSON
UUID string
Date *time.Time
Comment string
File *FileJSON
Signers []SignerJSON
TTP *TTPJSON
}
// GetJSON returns indented json from a contract and some ttp information (nil allowed)
......
......@@ -48,7 +48,6 @@ allow multiline and accents: éÉ`,
"Hash": "bb"
}
],
"Sequence": null,
"TTP": null
}`
......
......@@ -103,4 +103,3 @@ func sendUserToStream(stream *api.Platform_JoinSignatureServer, contractUUID str
User: user,
})
}
......@@ -14,9 +14,10 @@ import (
// readySignal is the structure that is transmitted accross goroutines
type readySignal struct {
ready bool // If true, this is the ready signal. If not, this is a new connection signal
data string // Various data (CN or SignatureUUID)
chain [][]byte // Only used to broadcast hash chain (signers hashes in order)
ready bool // If true, this is the ready signal. If not, this is a new connection signal
data string // Various data (CN or SignatureUUID)
chain [][]byte // Only used to broadcast hash chain (signers hashes in order)
sequence []uint32 // Only used to broadcast signature sequence
}
// ReadySign is the last job of the platform before the signature can occur.
......@@ -108,9 +109,10 @@ func masterReadyRoutine(db *mgdb.MongoManager, rooms *common.WaitingGroupMap, co
ready := FindAndUpdatePendingSigner(cn, &signersReady, &contract.Signers)
if ready {
rooms.Broadcast(roomID, &readySignal{
ready: true,
data: bson.NewObjectId().Hex(),
chain: contract.GetHashChain(),
ready: true,
data: bson.NewObjectId().Hex(),
chain: contract.GetHashChain(),
sequence: GenerateSignSequence(len(contract.Signers)),
})
work = false
}
......
package contract
import (
"dfss/dfssp/entities"
)
// GenerateSignSequence for the contract signature
//
// The generated sequence is an array of integers refering to the User array.
func GenerateSignSequence(users []*entities.User) []int {
return SquaredSignEngine(len(users))
func GenerateSignSequence(n int) []uint32 {
return SquaredSignEngine(uint32(n))
}
// SquaredSignEngine is a basic ^2 engine for sequence generation
func SquaredSignEngine(n int) []int {
sequence := make([]int, n*n)
func SquaredSignEngine(n uint32) []uint32 {
sequence := make([]uint32, n*n)
for i := 0; i < n; i++ {
for k := 0; k < n; k++ {
sequence[i*n+k] = k
var i, k uint32
for i = 0; i < n; i++ {
for k = 0; k < n; k++ {
sequence[i*n+k] = uint32(k)
}
}
......@@ -25,17 +22,18 @@ func SquaredSignEngine(n int) []int {
}
// SquaredSignEngineSlice is the same as the above with slicing
func SquaredSignEngineSlice(n int) []int {
baseSequence := make([]int, n)
func SquaredSignEngineSlice(n uint32) []uint32 {
baseSequence := make([]uint32, n)
// populate base slice
for i := 0; i < n; i++ {
var i uint32
for i = 0; i < n; i++ {
baseSequence[i] = i
}
sequence := make([]int, 0, n*n)
sequence := make([]uint32, 0, n*n)
// append n-1 time the slice to itself
for i := 0; i < n; i++ {
for i = 0; i < n; i++ {
sequence = append(sequence, baseSequence...)
}
......
......@@ -2,30 +2,18 @@ package contract_test
import (
"dfss/dfssp/contract"
"dfss/dfssp/entities"
"github.com/bmizerany/assert"
"testing"
)
var refSeq = []int{0, 1, 2, 0, 1, 2, 0, 1, 2} // for n = 3
var refSeq = []uint32{0, 1, 2, 0, 1, 2, 0, 1, 2} // for n = 3
func TestGenerateSignSequence(t *testing.T) {
// initialise fixtures
var users = make([]*entities.User, 3)
users[0] = entities.NewUser()
users[1] = entities.NewUser()
users[2] = entities.NewUser()
users[0].Email = "user1@example.com"
users[1].Email = "user2@example.com"
users[2].Email = "user3@example.com"
assert.Equal(t, refSeq, contract.GenerateSignSequence(users))
assert.Equal(t, refSeq, contract.GenerateSignSequence(3))
}
// Perform sequence generation in a loop
func BenchmarkSquaredEngine(b *testing.B) {
for i := 0; i < b.N; i++ {
_ = contract.SquaredSignEngine(10)
}
......@@ -33,7 +21,6 @@ func BenchmarkSquaredEngine(b *testing.B) {
// Perform sequence generation in a loop with slicing
func BenchmarkSquaredEngineSlice(b *testing.B) {
for i := 0; i < b.N; i++ {
_ = contract.SquaredSignEngineSlice(10)
}
......
......@@ -32,15 +32,15 @@ func TestSignContract(t *testing.T) {
// Register clients
clients := make([]*exec.Cmd, 3)
client1, err := createClient(workingDir, ca, 0)
client1, err := createClient(workingDir, ca, 9091)
assert.Equal(t, nil, err)
err = registerAndAuth(client1, "client1@example.com", "password", "", true, true)
assert.Equal(t, nil, err)
client2, err := createClient(workingDir, ca, 0)
client2, err := createClient(workingDir, ca, 9092)
assert.Equal(t, nil, err)
err = registerAndAuth(client2, "client2@example.com", "password", "", true, true)
assert.Equal(t, nil, err)
client3, err := createClient(workingDir, ca, 0)
client3, err := createClient(workingDir, ca, 9093)
assert.Equal(t, nil, err)
err = registerAndAuth(client3, "client3@example.com", "password", "", true, true)
assert.Equal(t, nil, err)
......@@ -80,6 +80,7 @@ func TestSignContract(t *testing.T) {
go func(c *exec.Cmd, i int) {
time.Sleep(time.Duration(i*2) * time.Second)
c.Stdin = strings.NewReader("password\nyes\n")
c.Stderr = os.Stderr
output, err := c.Output()
if err != nil {
output = nil
......
......@@ -73,7 +73,7 @@ func createClient(tmpDir string, ca []byte, port int) (*exec.Cmd, error) {
// Prepare the client command.
// The last argument is up to you!
cmd := exec.Command(path, "-ca", caPath, "-cert", certPath, "-host", "127.0.0.1:"+testPort, "-key", keyPath, "-port", strconv.Itoa(port), "-v")
cmd := exec.Command(path, "-ca", caPath, "-cert", certPath, "-host", "127.0.0.1:"+testPort, "-key", keyPath, "-port", strconv.Itoa(port), "-v")
return cmd, nil
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment